UNPKG

@thi.ng/pointfree

Version:

Pointfree functional composition / Forth style stack execution engine

76 lines 2.73 kB
import type { StackContext, StackEnv, StackFn, StackProc, StackProgram } from "./api.js"; export declare const $stackFn: (f: StackProc) => StackFn; /** * Takes a result tuple returned by {@link run} and unwraps one or more * items from result stack. If no `n` is given, defaults to single value * (TOS) and returns it as is. Returns an array for all other `n`. * * @param result - * @param n - */ export declare const unwrap: ([stack]: StackContext, n?: number) => any; /** * Higher order word. Takes a StackProgram and returns it as StackFn to * be used like any word. Unknown stack effect. * * If the optional `env` is given, uses a shallow copy of that * environment (one per invocation) instead of the current one passed by * {@link run} at runtime. If `mergeEnv` is true (default), the user * provided env will be merged with the current env (also shallow * copies). This is useful in conjunction with {@link pushenv} and {@link store} * or `storekey()` to save results of sub procedures in the main env. * * Note: The provided (or merged) env is only active within the * execution scope of the word. * * Stack effect: `( ? -- ? )` * * @param prog - * @param env - * @param mergeEnv - */ export declare const defWord: (prog: StackProgram, env?: StackEnv, mergeEnv?: boolean) => StackFn; /** * Like {@link defWord}, but automatically calls {@link unwrap} on result * context to produced unwrapped value/tuple. * * @remarks * **Importatant:** Words defined with this function CANNOT be used as part of a * larger stack program, only for standalone use. * * @param prog - * @param n - * @param env - * @param mergeEnv - */ export declare const defWordU: (prog: StackProgram, n?: number, env?: StackEnv, mergeEnv?: boolean) => (ctx: StackContext) => any; /** * Executes TOS as stack function and places result back on d-stack. TOS * MUST be a valid word or quotation. * * Stack effect: `( x -- x() )` * * @param ctx - */ export declare const exec: (ctx: StackContext) => StackContext; /** * Expects a body and error handler quotation on stack. Executes body * within an implicit `try .. catch` and if an error was thrown pushes * it on stack and executes error quotation. * * Stack effect: `( body catch -- ? )` * * @param ctx - */ export declare const $try: (ctx: StackContext) => StackContext; /** * Expects TOS to be a quotation with a vanilla JS function as first * element. Calls fn with all remaining items in quot as arguments and * pushes result back on d-stack (even if fn returned `undefined`). * * Stack effect: `( [f ...] -- f(...) )` * * @param ctx - */ export declare const execjs: (ctx: StackContext) => StackContext; //# sourceMappingURL=word.d.ts.map