UNPKG

@thi.ng/pointfree

Version:

Pointfree functional composition / Forth style stack execution engine

61 lines (60 loc) 1.51 kB
import { and, or } from "./logic.js"; import { $ } from "./safe.js"; import { dup, dup2, dup3, over, swap } from "./stack.js"; import { $stackFn, defWord, exec } from "./word.js"; const dip = (ctx) => { const stack = ctx[0]; $(stack, 2); const q = stack.pop(); const x = stack.pop(); ctx = $stackFn(q)(ctx); ctx[0].push(x); return ctx; }; const dip2 = defWord([swap, [dip], dip]); const dip3 = defWord([swap, [dip2], dip]); const dip4 = defWord([swap, [dip3], dip]); const keep = defWord([over, [exec], dip]); const keep2 = defWord([[dup2], dip, dip2]); const keep3 = defWord([[dup3], dip, dip3]); const bi = defWord([[keep], dip, exec]); const bi2 = defWord([[keep2], dip, exec]); const bi3 = defWord([[keep3], dip, exec]); const tri = defWord([[[keep], dip, keep], dip, exec]); const tri2 = defWord([[[keep2], dip, keep2], dip, exec]); const tri3 = defWord([[[keep3], dip, keep3], dip, exec]); const bis = defWord([[dip], dip, exec]); const bis2 = defWord([[dip2], dip, exec]); const tris = defWord([[[dip2], dip, dip], dip, exec]); const tris2 = defWord([[dip4], dip2, bis2]); const bia = defWord([dup, bis]); const bia2 = defWord([dup, bis2]); const tria = defWord([dup, dup, tris]); const tria2 = defWord([dup, dup, tris2]); const both = defWord([bia, and]); const either = defWord([bia, or]); export { bi, bi2, bi3, bia, bia2, bis, bis2, both, dip, dip2, dip3, dip4, either, keep, keep2, keep3, tri, tri2, tri3, tria, tria2, tris, tris2 };