@thi.ng/pointfree
Version:
Pointfree functional composition / Forth style stack execution engine
30 lines (29 loc) • 763 B
JavaScript
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
import { $ } from "./safe.js";
const pushenv = (ctx) => (ctx[0].push(ctx[2]), ctx);
const load = (ctx) => {
const stack = ctx[0];
$(stack, 1);
const id = stack.pop();
!ctx[2].hasOwnProperty(id) && illegalArgs(`unknown var: ${id}`);
stack.push(ctx[2][id]);
return ctx;
};
const store = (ctx) => ($(ctx[0], 2), ctx[2][ctx[0].pop()] = ctx[0].pop(), ctx);
const defLoadKey = (key) => (ctx) => {
!ctx[2].hasOwnProperty(key) && illegalArgs(`unknown var: ${key.toString()}`);
ctx[0].push(ctx[2][key]);
return ctx;
};
const defStoreKey = (key) => (ctx) => {
$(ctx[0], 1);
ctx[2][key] = ctx[0].pop();
return ctx;
};
export {
defLoadKey,
defStoreKey,
load,
pushenv,
store
};