import type { IObjectOf } from "@thi.ng/api" ; import { illegalState } from "@thi.ng/errors/illegal-state" ; import type { StackContext , StackProc } from "./api.js" ; import { $ } from "./safe.js" ; import { nop } from "./stack.js" ; import { $stackFn } from "./word.js" ; param _then param _else export const defCond = ( _then , _else = nop ) => ( ctx ) => { $ ( ctx [ 0 ] , 1 ) ; return $stackFn ( ctx [ 0 ] . pop ( ) ? _then : _else ) ( ctx ) ; } ; /**
 * Non-HOF version of  cond , expects `test` result and both branches on
 * d-stack. Executes `thenq` word/quotation if `test` is truthy, else
 * runs `elseq`.
 *
 * ( test thenq elseq -- ? )
 *
 *  param ctx export const condq = ( ctx ) => { const stack = ctx [ 0 ] ; $ ( stack , 3 ) ; const _else = stack . pop ( ) ; const _then = stack . pop ( ) ; return $stackFn ( stack . pop ( ) ? _then : _else ) ( ctx ) ; } ; /**
 * Similar to  condq , but only expects `test` result and truthy branch
 * d-stack. Executes word/quotation if `test` is truthy, else does nothing.
 *
 * ( test whenq -- ? )
 *
 *  param ctx export const whenq = ( ctx ) => { const stack = ctx [ 0 ] ; $ ( stack , 2 ) ; const _then = stack . pop ( ) ; return stack . pop ( ) ? $stackFn ( _then ) ( ctx ) : ctx ; } ; param cases export const defCases = ( cases ) => ( ctx ) => { $ ( ctx [ 0 ] , 1 ) ; const stack = ctx [ 0 ] ; const tos = stack . pop ( ) ; const cas = cases [ tos ] ; if ( cas !== undefined ) { return $stackFn ( cas ) ( ctx ) ; } if ( cases . default ) { stack . push ( tos ) ; return $stackFn ( cases . default ) ( ctx ) ; } return illegalState ( `no matching case for: ${ tos }` ) ; } ; export const casesq = ( ctx ) => { const stack = ctx [ 0 ] ; $ ( stack , 2 ) ; return defCases ( stack . pop ( ) ) ( ctx ) ; } ;