import type { StackContext , StackProc } from "./api.js" ; import { $ } from "./safe.js" ; import { $stackFn } from "./word.js" ; example param test param body export const defLoop = ( test , body ) => { const _test = $stackFn ( test ) ; const _body = $stackFn ( body ) ; return ( ctx ) => { while ( true ) { ctx = _test ( ctx ) ; $ ( ctx [ 0 ] , 1 ) ; if ( ! ctx [ 0 ] . pop ( ) ) { return ctx ; } ctx = _body ( ctx ) ; } } ; } ; /**
 * Non-HOF version of  loop . Expects test result and body quotation /
 * word on d-stack.
 *
 * ( testq bodyq -- ? )
 *
 *  param ctx export const loopq = ( ctx ) => { const stack = ctx [ 0 ] ; $ ( stack , 2 ) ; const body = stack . pop ( ) ; return defLoop ( stack . pop ( ) , body ) ( ctx ) ; } ; example param body export const dotimes = ( ctx ) => { let stack = ctx [ 0 ] ; $ ( stack , 2 ) ; const w = $stackFn ( stack . pop ( ) ) ; for ( let i = 0 , n = stack . pop ( ) ; i < n ; i ++ ) { ctx [ 0 ] . push ( i ) ; ctx = w ( ctx ) ; } return ctx ; } ;