@thi.ng/pointfree 2.0.36 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +101 -81
- package/README.md +14 -5
- package/array.d.ts +1 -1
- package/array.js +10 -7
- package/binary.d.ts +7 -7
- package/binary.js +1 -1
- package/cond.d.ts +1 -1
- package/cond.js +8 -5
- package/context.d.ts +1 -1
- package/dataflow.d.ts +23 -23
- package/dataflow.js +4 -4
- package/env.d.ts +1 -1
- package/env.js +7 -3
- package/index.d.ts +18 -18
- package/index.js +18 -18
- package/io.d.ts +1 -1
- package/io.js +1 -1
- package/logic.d.ts +14 -14
- package/logic.js +1 -1
- package/loop.d.ts +1 -1
- package/loop.js +2 -2
- package/math.d.ts +2 -2
- package/math.js +3 -3
- package/ops.d.ts +1 -1
- package/ops.js +3 -3
- package/package.json +82 -26
- package/run.d.ts +1 -1
- package/run.js +3 -2
- package/safe.d.ts +2 -2
- package/safe.js +2 -2
- package/stack.d.ts +1 -1
- package/stack.js +1 -1
- package/string.d.ts +1 -1
- package/string.js +1 -1
- package/word.d.ts +1 -1
- package/word.js +9 -8
- package/lib/index.js +0 -785
- package/lib/index.js.map +0 -1
- package/lib/index.umd.js +0 -1
- package/lib/index.umd.js.map +0 -1
package/lib/index.js
DELETED
|
@@ -1,785 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var checks = require('@thi.ng/checks');
|
|
6
|
-
var errors = require('@thi.ng/errors');
|
|
7
|
-
var api = require('@thi.ng/api');
|
|
8
|
-
var compose = require('@thi.ng/compose');
|
|
9
|
-
var equiv$1 = require('@thi.ng/equiv');
|
|
10
|
-
|
|
11
|
-
exports.ensureStack = void 0;
|
|
12
|
-
exports.ensureStackN = void 0;
|
|
13
|
-
const safeMode = (state) => {
|
|
14
|
-
if (state) {
|
|
15
|
-
exports.ensureStackN = (m, n) => m < n && errors.illegalState(`stack underflow`);
|
|
16
|
-
exports.ensureStack = (stack, n) => exports.ensureStackN(stack.length, n);
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
exports.ensureStack = exports.ensureStackN = api.NO_OP;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
safeMode(true);
|
|
23
|
-
|
|
24
|
-
const defOp1 = (op) => {
|
|
25
|
-
return (ctx) => {
|
|
26
|
-
const stack = ctx[0];
|
|
27
|
-
const n = stack.length - 1;
|
|
28
|
-
exports.ensureStackN(n, 0);
|
|
29
|
-
stack[n] = op(stack[n]);
|
|
30
|
-
return ctx;
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
const defOp2 = (op) => (ctx) => {
|
|
34
|
-
const stack = ctx[0];
|
|
35
|
-
const n = stack.length - 2;
|
|
36
|
-
exports.ensureStackN(n, 0);
|
|
37
|
-
stack[n] = op(stack.pop(), stack[n]);
|
|
38
|
-
return ctx;
|
|
39
|
-
};
|
|
40
|
-
const defOp2v = (f) => (ctx) => {
|
|
41
|
-
exports.ensureStack(ctx[0], 2);
|
|
42
|
-
const stack = ctx[0];
|
|
43
|
-
const b = stack.pop();
|
|
44
|
-
const n = stack.length - 1;
|
|
45
|
-
const a = stack[n];
|
|
46
|
-
const isa = checks.isArray(a);
|
|
47
|
-
const isb = checks.isArray(b);
|
|
48
|
-
stack[n] =
|
|
49
|
-
isa && isb
|
|
50
|
-
? op2vAB(f, a, b)
|
|
51
|
-
: isb && !isa
|
|
52
|
-
? op2vB(f, a, b)
|
|
53
|
-
: isa && !isb
|
|
54
|
-
? op2vA(f, a, b)
|
|
55
|
-
: errors.illegalArgs("at least one arg must be an array");
|
|
56
|
-
return ctx;
|
|
57
|
-
};
|
|
58
|
-
const op2vAB = (f, a, b) => {
|
|
59
|
-
const res = new Array(Math.min(a.length, b.length));
|
|
60
|
-
for (let i = res.length - 1; i >= 0; i--) {
|
|
61
|
-
res[i] = f(b[i], a[i]);
|
|
62
|
-
}
|
|
63
|
-
return res;
|
|
64
|
-
};
|
|
65
|
-
const op2vA = (f, a, b) => {
|
|
66
|
-
const res = new Array(a.length);
|
|
67
|
-
for (let i = res.length - 1; i >= 0; i--) {
|
|
68
|
-
res[i] = f(b, a[i]);
|
|
69
|
-
}
|
|
70
|
-
return res;
|
|
71
|
-
};
|
|
72
|
-
const op2vB = (f, a, b) => {
|
|
73
|
-
const res = new Array(b.length);
|
|
74
|
-
for (let i = res.length - 1; i >= 0; i--) {
|
|
75
|
-
res[i] = f(b[i], a);
|
|
76
|
-
}
|
|
77
|
-
return res;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const __xsp = (id) => (ctx) => (ctx[0].push(ctx[id].length), ctx);
|
|
81
|
-
const __dup = (id) => __copy(id, id);
|
|
82
|
-
const __dup2 = (id) => (ctx) => {
|
|
83
|
-
const stack = ctx[id];
|
|
84
|
-
let n = stack.length - 2;
|
|
85
|
-
exports.ensureStackN(n, 0);
|
|
86
|
-
stack.push(stack[n], stack[n + 1]);
|
|
87
|
-
return ctx;
|
|
88
|
-
};
|
|
89
|
-
const __dup3 = (id) => (ctx) => {
|
|
90
|
-
const stack = ctx[id];
|
|
91
|
-
let n = stack.length - 3;
|
|
92
|
-
exports.ensureStackN(n, 0);
|
|
93
|
-
stack.push(stack[n], stack[n + 1], stack[n + 2]);
|
|
94
|
-
return ctx;
|
|
95
|
-
};
|
|
96
|
-
const __drop = (id, n = 1) => (ctx) => (exports.ensureStack(ctx[id], 1), (ctx[id].length -= n), ctx);
|
|
97
|
-
const __swap = (i) => (ctx) => {
|
|
98
|
-
const stack = ctx[i];
|
|
99
|
-
const n = stack.length - 2;
|
|
100
|
-
exports.ensureStackN(n, 0);
|
|
101
|
-
const a = stack[n];
|
|
102
|
-
stack[n] = stack[n + 1];
|
|
103
|
-
stack[n + 1] = a;
|
|
104
|
-
return ctx;
|
|
105
|
-
};
|
|
106
|
-
const __swap2 = (i) => (ctx) => {
|
|
107
|
-
const stack = ctx[i];
|
|
108
|
-
let n = stack.length - 1;
|
|
109
|
-
exports.ensureStackN(n, 3);
|
|
110
|
-
let a = stack[n];
|
|
111
|
-
stack[n] = stack[n - 2];
|
|
112
|
-
stack[n - 2] = a;
|
|
113
|
-
n--;
|
|
114
|
-
a = stack[n];
|
|
115
|
-
stack[n] = stack[n - 2];
|
|
116
|
-
stack[n - 2] = a;
|
|
117
|
-
return ctx;
|
|
118
|
-
};
|
|
119
|
-
const __over = (id) => (ctx) => {
|
|
120
|
-
const stack = ctx[id];
|
|
121
|
-
const n = stack.length - 2;
|
|
122
|
-
exports.ensureStackN(n, 0);
|
|
123
|
-
stack.push(stack[n]);
|
|
124
|
-
return ctx;
|
|
125
|
-
};
|
|
126
|
-
const __move = (src, dest) => (ctx) => (exports.ensureStack(ctx[src], 1), ctx[dest].push(ctx[src].pop()), ctx);
|
|
127
|
-
const __move2 = (a, b) => (ctx) => {
|
|
128
|
-
const src = ctx[a];
|
|
129
|
-
exports.ensureStack(src, 2);
|
|
130
|
-
const v = src.pop();
|
|
131
|
-
ctx[b].push(src.pop(), v);
|
|
132
|
-
return ctx;
|
|
133
|
-
};
|
|
134
|
-
const __copy = (src, dest) => (ctx) => (exports.ensureStack(ctx[src], 1), ctx[dest].push(tos(ctx[src])), ctx);
|
|
135
|
-
const __copy2 = (a, b) => (ctx) => {
|
|
136
|
-
const src = ctx[a];
|
|
137
|
-
const n = src.length - 2;
|
|
138
|
-
exports.ensureStackN(n, 0);
|
|
139
|
-
ctx[b].push(src[n], src[n + 1]);
|
|
140
|
-
return ctx;
|
|
141
|
-
};
|
|
142
|
-
const __incdec = (id, n) => (ctx) => (exports.ensureStack(ctx[id], 1), (ctx[id][ctx[id].length - 1] += n), ctx);
|
|
143
|
-
const tos = (stack) => stack[stack.length - 1];
|
|
144
|
-
const nop = (ctx) => ctx;
|
|
145
|
-
const dsp = __xsp(0);
|
|
146
|
-
const pick = (ctx) => {
|
|
147
|
-
const stack = ctx[0];
|
|
148
|
-
let n = stack.length - 1;
|
|
149
|
-
exports.ensureStackN(n, 0);
|
|
150
|
-
exports.ensureStackN((n -= stack.pop() + 1), 0);
|
|
151
|
-
stack.push(stack[n]);
|
|
152
|
-
return ctx;
|
|
153
|
-
};
|
|
154
|
-
const drop = __drop(0);
|
|
155
|
-
const drop2 = __drop(0, 2);
|
|
156
|
-
const dropif = (ctx) => (exports.ensureStack(ctx[0], 1), tos(ctx[0]) && ctx[0].length--, ctx);
|
|
157
|
-
const defPush = (...args) => (ctx) => (ctx[0].push(...args), ctx);
|
|
158
|
-
const dup = __dup(0);
|
|
159
|
-
const dup2 = __dup2(0);
|
|
160
|
-
const dup3 = __dup3(0);
|
|
161
|
-
const dupif = (ctx) => {
|
|
162
|
-
exports.ensureStack(ctx[0], 1);
|
|
163
|
-
const x = tos(ctx[0]);
|
|
164
|
-
x && ctx[0].push(x);
|
|
165
|
-
return ctx;
|
|
166
|
-
};
|
|
167
|
-
const swap = __swap(0);
|
|
168
|
-
const swap2 = __swap2(0);
|
|
169
|
-
const nip = (ctx) => {
|
|
170
|
-
const stack = ctx[0];
|
|
171
|
-
const n = stack.length - 2;
|
|
172
|
-
exports.ensureStackN(n, 0);
|
|
173
|
-
stack[n] = stack.pop();
|
|
174
|
-
return ctx;
|
|
175
|
-
};
|
|
176
|
-
const tuck = (ctx) => {
|
|
177
|
-
exports.ensureStack(ctx[0], 2);
|
|
178
|
-
const stack = ctx[0];
|
|
179
|
-
const a = stack.pop();
|
|
180
|
-
stack.push(a, stack.pop(), a);
|
|
181
|
-
return ctx;
|
|
182
|
-
};
|
|
183
|
-
const rot = (ctx) => {
|
|
184
|
-
const stack = ctx[0];
|
|
185
|
-
const n = stack.length - 1;
|
|
186
|
-
exports.ensureStackN(n, 2);
|
|
187
|
-
const c = stack[n - 2];
|
|
188
|
-
stack[n - 2] = stack[n - 1];
|
|
189
|
-
stack[n - 1] = stack[n];
|
|
190
|
-
stack[n] = c;
|
|
191
|
-
return ctx;
|
|
192
|
-
};
|
|
193
|
-
const invrot = (ctx) => {
|
|
194
|
-
const stack = ctx[0];
|
|
195
|
-
const n = stack.length - 1;
|
|
196
|
-
exports.ensureStackN(n, 2);
|
|
197
|
-
const c = stack[n];
|
|
198
|
-
stack[n] = stack[n - 1];
|
|
199
|
-
stack[n - 1] = stack[n - 2];
|
|
200
|
-
stack[n - 2] = c;
|
|
201
|
-
return ctx;
|
|
202
|
-
};
|
|
203
|
-
const over = __over(0);
|
|
204
|
-
const inc = __incdec(0, 1);
|
|
205
|
-
const dec = __incdec(0, -1);
|
|
206
|
-
const rsp = __xsp(1);
|
|
207
|
-
const rdup = __dup(1);
|
|
208
|
-
const rdup2 = __dup2(1);
|
|
209
|
-
const rdup3 = __dup3(1);
|
|
210
|
-
const rdrop = __drop(1);
|
|
211
|
-
const rdrop2 = __drop(1, 2);
|
|
212
|
-
const movdr = __move(0, 1);
|
|
213
|
-
const movrd = __move(1, 0);
|
|
214
|
-
const cpdr = __copy(0, 1);
|
|
215
|
-
const cprd = __copy(1, 0);
|
|
216
|
-
const movdr2 = __move2(0, 1);
|
|
217
|
-
const movrd2 = __move2(1, 0);
|
|
218
|
-
const cpdr2 = __copy2(0, 1);
|
|
219
|
-
const cprd2 = __copy2(1, 0);
|
|
220
|
-
const rswap = __swap(1);
|
|
221
|
-
const rswap2 = __swap2(1);
|
|
222
|
-
const rover = __over(1);
|
|
223
|
-
const rinc = __incdec(1, 1);
|
|
224
|
-
const rdec = __incdec(1, -1);
|
|
225
|
-
|
|
226
|
-
const $stackFn = (f) => (checks.isArray(f) ? defWord(f) : f);
|
|
227
|
-
const compile = (prog) => prog.length > 0
|
|
228
|
-
? compose.compL.apply(null, (prog.map((w) => !checks.isFunction(w)
|
|
229
|
-
? (ctx) => (ctx[0].push(w), ctx)
|
|
230
|
-
: w)))
|
|
231
|
-
: (ctx) => ctx;
|
|
232
|
-
const unwrap = ([stack], n = 1) => n === 1 ? tos(stack) : stack.slice(Math.max(0, stack.length - n));
|
|
233
|
-
const defWord = (prog, env, mergeEnv = true) => {
|
|
234
|
-
const w = compile(prog);
|
|
235
|
-
return env
|
|
236
|
-
? mergeEnv
|
|
237
|
-
? (ctx) => (w([ctx[0], ctx[1], Object.assign(Object.assign({}, ctx[2]), env)]), ctx)
|
|
238
|
-
: (ctx) => (w([ctx[0], ctx[1], Object.assign({}, env)]), ctx)
|
|
239
|
-
: w;
|
|
240
|
-
};
|
|
241
|
-
const defWordU = (prog, n = 1, env, mergeEnv = true) => {
|
|
242
|
-
const w = compile(prog);
|
|
243
|
-
return env
|
|
244
|
-
? mergeEnv
|
|
245
|
-
? (ctx) => unwrap(w([ctx[0], ctx[1], Object.assign(Object.assign({}, ctx[2]), env)]), n)
|
|
246
|
-
: (ctx) => unwrap(w([ctx[0], ctx[1], Object.assign({}, env)]), n)
|
|
247
|
-
: (ctx) => unwrap(w(ctx), n);
|
|
248
|
-
};
|
|
249
|
-
const exec = (ctx) => (exports.ensureStack(ctx[0], 1), $stackFn(ctx[0].pop())(ctx));
|
|
250
|
-
const $try = (ctx) => {
|
|
251
|
-
const stack = ctx[0];
|
|
252
|
-
exports.ensureStack(stack, 2);
|
|
253
|
-
const err = stack.pop();
|
|
254
|
-
try {
|
|
255
|
-
return exec(ctx);
|
|
256
|
-
}
|
|
257
|
-
catch (e) {
|
|
258
|
-
stack.push(e, err);
|
|
259
|
-
return exec(ctx);
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
const execjs = (ctx) => {
|
|
263
|
-
const stack = ctx[0];
|
|
264
|
-
exports.ensureStack(stack, 1);
|
|
265
|
-
const [fn, ...args] = stack.pop();
|
|
266
|
-
stack.push(fn(...args));
|
|
267
|
-
return ctx;
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
const list = (ctx) => (ctx[0].push([]), ctx);
|
|
271
|
-
const obj = (ctx) => (ctx[0].push({}), ctx);
|
|
272
|
-
const pushl = (ctx) => {
|
|
273
|
-
exports.ensureStack(ctx[0], 2);
|
|
274
|
-
const stack = ctx[0];
|
|
275
|
-
const a = stack.pop();
|
|
276
|
-
a.unshift(stack.pop());
|
|
277
|
-
stack.push(a);
|
|
278
|
-
return ctx;
|
|
279
|
-
};
|
|
280
|
-
const pushr = (ctx) => {
|
|
281
|
-
const stack = ctx[0];
|
|
282
|
-
const n = stack.length - 2;
|
|
283
|
-
exports.ensureStackN(n, 0);
|
|
284
|
-
stack[n].push(stack[n + 1]);
|
|
285
|
-
stack.length--;
|
|
286
|
-
return ctx;
|
|
287
|
-
};
|
|
288
|
-
const popr = (ctx) => {
|
|
289
|
-
const stack = ctx[0];
|
|
290
|
-
const n = stack.length - 1;
|
|
291
|
-
exports.ensureStackN(n, 0);
|
|
292
|
-
const a = stack[n];
|
|
293
|
-
!a.length && errors.illegalState("can't pop empty array");
|
|
294
|
-
stack.push(a.pop());
|
|
295
|
-
return ctx;
|
|
296
|
-
};
|
|
297
|
-
const pull = defWord([popr, swap]);
|
|
298
|
-
const pull2 = defWord([pull, pull]);
|
|
299
|
-
const pull3 = defWord([pull2, pull]);
|
|
300
|
-
const pull4 = defWord([pull2, pull2]);
|
|
301
|
-
const vadd = defOp2v((b, a) => a + b);
|
|
302
|
-
const vsub = defOp2v((b, a) => a - b);
|
|
303
|
-
const vmul = defOp2v((b, a) => a * b);
|
|
304
|
-
const vdiv = defOp2v((b, a) => a / b);
|
|
305
|
-
const split = (ctx) => {
|
|
306
|
-
const stack = ctx[0];
|
|
307
|
-
const n = stack.length - 2;
|
|
308
|
-
exports.ensureStackN(n, 0);
|
|
309
|
-
const a = stack[n];
|
|
310
|
-
const b = stack[n + 1];
|
|
311
|
-
stack[n + 1] = a.splice(b, a.length - b);
|
|
312
|
-
return ctx;
|
|
313
|
-
};
|
|
314
|
-
const cat = (ctx) => {
|
|
315
|
-
const stack = ctx[0];
|
|
316
|
-
const n = stack.length - 2;
|
|
317
|
-
exports.ensureStackN(n, 0);
|
|
318
|
-
stack[n] = stack[n].concat(stack.pop());
|
|
319
|
-
return ctx;
|
|
320
|
-
};
|
|
321
|
-
const catr = (ctx) => {
|
|
322
|
-
const stack = ctx[0];
|
|
323
|
-
const n = stack.length - 2;
|
|
324
|
-
exports.ensureStackN(n, 0);
|
|
325
|
-
stack[n] = stack.pop().concat(stack[n]);
|
|
326
|
-
return ctx;
|
|
327
|
-
};
|
|
328
|
-
const mapl = (ctx) => {
|
|
329
|
-
exports.ensureStack(ctx[0], 2);
|
|
330
|
-
const stack = ctx[0];
|
|
331
|
-
const w = $stackFn(stack.pop());
|
|
332
|
-
const list = stack.pop();
|
|
333
|
-
const n = list.length;
|
|
334
|
-
for (let i = 0; i < n; i++) {
|
|
335
|
-
ctx[0].push(list[i]);
|
|
336
|
-
ctx = w(ctx);
|
|
337
|
-
}
|
|
338
|
-
return ctx;
|
|
339
|
-
};
|
|
340
|
-
const mapll = (ctx) => {
|
|
341
|
-
exports.ensureStack(ctx[0], 2);
|
|
342
|
-
let stack = ctx[0];
|
|
343
|
-
const w = $stackFn(stack.pop());
|
|
344
|
-
const list = stack.pop();
|
|
345
|
-
const n = list.length;
|
|
346
|
-
let r = 0;
|
|
347
|
-
for (let i = 0; i < n; i++) {
|
|
348
|
-
let m = stack.length;
|
|
349
|
-
stack.push(list[i]);
|
|
350
|
-
ctx = w(ctx);
|
|
351
|
-
stack = ctx[0];
|
|
352
|
-
r += stack.length - m;
|
|
353
|
-
}
|
|
354
|
-
stack.push(stack.splice(stack.length - r, r));
|
|
355
|
-
return ctx;
|
|
356
|
-
};
|
|
357
|
-
const foldl = defWord([invrot, mapl]);
|
|
358
|
-
const collect = (ctx) => {
|
|
359
|
-
const stack = ctx[0];
|
|
360
|
-
let n = stack.length - 1, m;
|
|
361
|
-
exports.ensureStackN(n, 0);
|
|
362
|
-
exports.ensureStackN((n -= m = stack.pop()), 0);
|
|
363
|
-
stack.push(stack.splice(n, m));
|
|
364
|
-
return ctx;
|
|
365
|
-
};
|
|
366
|
-
const defTuple = (n) => defWord([n, collect]);
|
|
367
|
-
const vec2 = defTuple(2);
|
|
368
|
-
const vec3 = defTuple(3);
|
|
369
|
-
const vec4 = defTuple(4);
|
|
370
|
-
const defJoin = (sep = "") => defOp1((x) => x.join(sep));
|
|
371
|
-
const join = defOp2((sep, buf) => buf.join(sep));
|
|
372
|
-
const length = defOp1((x) => x.length);
|
|
373
|
-
const copy = defOp1((x) => checks.isArray(x)
|
|
374
|
-
? x.slice()
|
|
375
|
-
: checks.isPlainObject(x)
|
|
376
|
-
? Object.assign({}, x) : errors.illegalArgs(`can't copy type ${typeof x}`));
|
|
377
|
-
const at = defOp2((b, a) => a[b]);
|
|
378
|
-
const setat = (ctx) => {
|
|
379
|
-
const stack = ctx[0];
|
|
380
|
-
const n = stack.length - 3;
|
|
381
|
-
exports.ensureStackN(n, 0);
|
|
382
|
-
stack[n + 1][stack[n + 2]] = stack[n];
|
|
383
|
-
stack[n] = stack[n + 1];
|
|
384
|
-
stack.length -= 2;
|
|
385
|
-
return ctx;
|
|
386
|
-
};
|
|
387
|
-
const bindkeys = (ctx) => {
|
|
388
|
-
const stack = ctx[0];
|
|
389
|
-
exports.ensureStack(stack, 2);
|
|
390
|
-
const obj = stack.pop();
|
|
391
|
-
const keys = stack.pop();
|
|
392
|
-
exports.ensureStack(stack, keys.length);
|
|
393
|
-
for (let i = keys.length - 1; i >= 0; i--) {
|
|
394
|
-
obj[keys[i]] = stack.pop();
|
|
395
|
-
}
|
|
396
|
-
stack.push(obj);
|
|
397
|
-
return ctx;
|
|
398
|
-
};
|
|
399
|
-
|
|
400
|
-
const bitand = defOp2((b, a) => a & b);
|
|
401
|
-
const bitor = defOp2((b, a) => a | b);
|
|
402
|
-
const bitxor = defOp2((b, a) => a ^ b);
|
|
403
|
-
const bitnot = defOp1((x) => ~x);
|
|
404
|
-
const lsl = defOp2((b, a) => a << b);
|
|
405
|
-
const lsr = defOp2((b, a) => a >> b);
|
|
406
|
-
const lsru = defOp2((b, a) => a >>> b);
|
|
407
|
-
|
|
408
|
-
const defCond = (_then, _else = nop) => (ctx) => (exports.ensureStack(ctx[0], 1), $stackFn(ctx[0].pop() ? _then : _else)(ctx));
|
|
409
|
-
const condq = (ctx) => {
|
|
410
|
-
const stack = ctx[0];
|
|
411
|
-
exports.ensureStack(stack, 3);
|
|
412
|
-
const _else = stack.pop();
|
|
413
|
-
const _then = stack.pop();
|
|
414
|
-
return $stackFn(stack.pop() ? _then : _else)(ctx);
|
|
415
|
-
};
|
|
416
|
-
const whenq = (ctx) => {
|
|
417
|
-
const stack = ctx[0];
|
|
418
|
-
exports.ensureStack(stack, 2);
|
|
419
|
-
const _then = stack.pop();
|
|
420
|
-
return stack.pop() ? $stackFn(_then)(ctx) : ctx;
|
|
421
|
-
};
|
|
422
|
-
const defCases = (cases) => (ctx) => {
|
|
423
|
-
exports.ensureStack(ctx[0], 1);
|
|
424
|
-
const stack = ctx[0];
|
|
425
|
-
const tos = stack.pop();
|
|
426
|
-
const cas = cases[tos];
|
|
427
|
-
if (cas !== undefined) {
|
|
428
|
-
return $stackFn(cas)(ctx);
|
|
429
|
-
}
|
|
430
|
-
if (cases.default) {
|
|
431
|
-
stack.push(tos);
|
|
432
|
-
return $stackFn(cases.default)(ctx);
|
|
433
|
-
}
|
|
434
|
-
return errors.illegalState(`no matching case for: ${tos}`);
|
|
435
|
-
};
|
|
436
|
-
const casesq = (ctx) => {
|
|
437
|
-
const stack = ctx[0];
|
|
438
|
-
exports.ensureStack(stack, 2);
|
|
439
|
-
return defCases(stack.pop())(ctx);
|
|
440
|
-
};
|
|
441
|
-
|
|
442
|
-
const ctx = (stack = [], env = {}) => [
|
|
443
|
-
stack,
|
|
444
|
-
[],
|
|
445
|
-
env,
|
|
446
|
-
];
|
|
447
|
-
|
|
448
|
-
const eq = defOp2((b, a) => a === b);
|
|
449
|
-
const equiv = defOp2(equiv$1.equiv);
|
|
450
|
-
const neq = defOp2((b, a) => a !== b);
|
|
451
|
-
const and = defOp2((b, a) => !!a && !!b);
|
|
452
|
-
const or = defOp2((b, a) => !!a || !!b);
|
|
453
|
-
const not = defOp1((x) => !x);
|
|
454
|
-
const lt = defOp2((b, a) => a < b);
|
|
455
|
-
const gt = defOp2((b, a) => a > b);
|
|
456
|
-
const lteq = defOp2((b, a) => a <= b);
|
|
457
|
-
const gteq = defOp2((b, a) => a >= b);
|
|
458
|
-
const iszero = defOp1((x) => x === 0);
|
|
459
|
-
const ispos = defOp1((x) => x > 0);
|
|
460
|
-
const isneg = defOp1((x) => x < 0);
|
|
461
|
-
const isnull = defOp1((x) => x == null);
|
|
462
|
-
|
|
463
|
-
const dip = (ctx) => {
|
|
464
|
-
const stack = ctx[0];
|
|
465
|
-
exports.ensureStack(stack, 2);
|
|
466
|
-
const q = stack.pop();
|
|
467
|
-
const x = stack.pop();
|
|
468
|
-
ctx = $stackFn(q)(ctx);
|
|
469
|
-
ctx[0].push(x);
|
|
470
|
-
return ctx;
|
|
471
|
-
};
|
|
472
|
-
const dip2 = defWord([swap, [dip], dip]);
|
|
473
|
-
const dip3 = defWord([swap, [dip2], dip]);
|
|
474
|
-
const dip4 = defWord([swap, [dip3], dip]);
|
|
475
|
-
const keep = defWord([over, [exec], dip]);
|
|
476
|
-
const keep2 = defWord([[dup2], dip, dip2]);
|
|
477
|
-
const keep3 = defWord([[dup3], dip, dip3]);
|
|
478
|
-
const bi = defWord([[keep], dip, exec]);
|
|
479
|
-
const bi2 = defWord([[keep2], dip, exec]);
|
|
480
|
-
const bi3 = defWord([[keep3], dip, exec]);
|
|
481
|
-
const tri = defWord([[[keep], dip, keep], dip, exec]);
|
|
482
|
-
const tri2 = defWord([[[keep2], dip, keep2], dip, exec]);
|
|
483
|
-
const tri3 = defWord([[[keep3], dip, keep3], dip, exec]);
|
|
484
|
-
const bis = defWord([[dip], dip, exec]);
|
|
485
|
-
const bis2 = defWord([[dip2], dip, exec]);
|
|
486
|
-
const tris = defWord([[[dip2], dip, dip], dip, exec]);
|
|
487
|
-
const tris2 = defWord([[dip4], dip2, bis2]);
|
|
488
|
-
const bia = defWord([dup, bis]);
|
|
489
|
-
const bia2 = defWord([dup, bis2]);
|
|
490
|
-
const tria = defWord([dup, dup, tris]);
|
|
491
|
-
const tria2 = defWord([dup, dup, tris2]);
|
|
492
|
-
const both = defWord([bia, and]);
|
|
493
|
-
const either = defWord([bia, or]);
|
|
494
|
-
|
|
495
|
-
const pushenv = (ctx) => (ctx[0].push(ctx[2]), ctx);
|
|
496
|
-
const load = (ctx) => {
|
|
497
|
-
const stack = ctx[0];
|
|
498
|
-
exports.ensureStack(stack, 1);
|
|
499
|
-
const id = stack.pop();
|
|
500
|
-
!ctx[2].hasOwnProperty(id) && errors.illegalArgs(`unknown var: ${id}`);
|
|
501
|
-
stack.push(ctx[2][id]);
|
|
502
|
-
return ctx;
|
|
503
|
-
};
|
|
504
|
-
const store = (ctx) => (exports.ensureStack(ctx[0], 2), (ctx[2][ctx[0].pop()] = ctx[0].pop()), ctx);
|
|
505
|
-
const defLoadKey = (key) => (ctx) => {
|
|
506
|
-
!ctx[2].hasOwnProperty(key) &&
|
|
507
|
-
errors.illegalArgs(`unknown var: ${key.toString()}`);
|
|
508
|
-
ctx[0].push(ctx[2][key]);
|
|
509
|
-
return ctx;
|
|
510
|
-
};
|
|
511
|
-
const defStoreKey = (key) => (ctx) => (exports.ensureStack(ctx[0], 1), (ctx[2][key] = ctx[0].pop()), ctx);
|
|
512
|
-
|
|
513
|
-
const print = (ctx) => (exports.ensureStack(ctx[0], 1), console.log(ctx[0].pop()), ctx);
|
|
514
|
-
const printds = (ctx) => (console.log(ctx[0]), ctx);
|
|
515
|
-
const printrs = (ctx) => (console.log(ctx[1]), ctx);
|
|
516
|
-
|
|
517
|
-
const defLoop = (test, body) => {
|
|
518
|
-
const _test = $stackFn(test);
|
|
519
|
-
const _body = $stackFn(body);
|
|
520
|
-
return (ctx) => {
|
|
521
|
-
while (true) {
|
|
522
|
-
ctx = _test(ctx);
|
|
523
|
-
exports.ensureStack(ctx[0], 1);
|
|
524
|
-
if (!ctx[0].pop()) {
|
|
525
|
-
return ctx;
|
|
526
|
-
}
|
|
527
|
-
ctx = _body(ctx);
|
|
528
|
-
}
|
|
529
|
-
};
|
|
530
|
-
};
|
|
531
|
-
const loopq = (ctx) => {
|
|
532
|
-
const stack = ctx[0];
|
|
533
|
-
exports.ensureStack(stack, 2);
|
|
534
|
-
const body = stack.pop();
|
|
535
|
-
return defLoop(stack.pop(), body)(ctx);
|
|
536
|
-
};
|
|
537
|
-
const dotimes = (ctx) => {
|
|
538
|
-
let stack = ctx[0];
|
|
539
|
-
exports.ensureStack(stack, 2);
|
|
540
|
-
const w = $stackFn(stack.pop());
|
|
541
|
-
for (let i = 0, n = stack.pop(); i < n; i++) {
|
|
542
|
-
ctx[0].push(i);
|
|
543
|
-
ctx = w(ctx);
|
|
544
|
-
}
|
|
545
|
-
return ctx;
|
|
546
|
-
};
|
|
547
|
-
|
|
548
|
-
const add = defOp2((b, a) => a + b);
|
|
549
|
-
const mul = defOp2((b, a) => a * b);
|
|
550
|
-
const sub = defOp2((b, a) => a - b);
|
|
551
|
-
const div = defOp2((b, a) => a / b);
|
|
552
|
-
const oneover = defWord([1, swap, div]);
|
|
553
|
-
const mod = defOp2((b, a) => a % b);
|
|
554
|
-
const min = defOp2(Math.min);
|
|
555
|
-
const max = defOp2(Math.max);
|
|
556
|
-
const neg = defOp1((x) => -x);
|
|
557
|
-
const pow = defOp2((b, a) => Math.pow(a, b));
|
|
558
|
-
const sqrt = defOp1(Math.sqrt);
|
|
559
|
-
const exp = defOp1(Math.exp);
|
|
560
|
-
const log = defOp1(Math.log);
|
|
561
|
-
const sin = defOp1(Math.sin);
|
|
562
|
-
const cos = defOp1(Math.cos);
|
|
563
|
-
const tan = defOp1(Math.tan);
|
|
564
|
-
const tanh = defOp1(Math.tanh);
|
|
565
|
-
const floor = defOp1(Math.floor);
|
|
566
|
-
const ceil = defOp1(Math.ceil);
|
|
567
|
-
const hypot = defOp2(Math.hypot);
|
|
568
|
-
const atan2 = defOp2(Math.atan2);
|
|
569
|
-
const rand = (ctx) => (ctx[0].push(Math.random()), ctx);
|
|
570
|
-
const even = defOp1((x) => !(x & 1));
|
|
571
|
-
const odd = defOp1((x) => !!(x & 1));
|
|
572
|
-
|
|
573
|
-
const run = (prog, ctx = [[], [], {}]) => {
|
|
574
|
-
if (checks.isFunction(prog)) {
|
|
575
|
-
return prog(ctx);
|
|
576
|
-
}
|
|
577
|
-
for (let p = checks.isArray(prog) ? prog : [prog], n = p.length, i = 0, w; i < n; i++) {
|
|
578
|
-
if (checks.isFunction((w = p[i]))) {
|
|
579
|
-
ctx = w(ctx);
|
|
580
|
-
}
|
|
581
|
-
else {
|
|
582
|
-
ctx[0].push(w);
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
return ctx;
|
|
586
|
-
};
|
|
587
|
-
const runU = (prog, ctx, n = 1) => unwrap(run(prog, ctx), n);
|
|
588
|
-
const runE = (prog, ctx) => run(prog, ctx)[2];
|
|
589
|
-
|
|
590
|
-
const ismatch = (ctx) => {
|
|
591
|
-
const stack = ctx[0];
|
|
592
|
-
const n = stack.length - 2;
|
|
593
|
-
exports.ensureStackN(n, 0);
|
|
594
|
-
stack[n] = new RegExp(stack[n + 1]).test(stack[n]);
|
|
595
|
-
stack.length--;
|
|
596
|
-
return ctx;
|
|
597
|
-
};
|
|
598
|
-
const fromjson = (ctx) => {
|
|
599
|
-
const stack = ctx[0];
|
|
600
|
-
exports.ensureStack(stack, 1);
|
|
601
|
-
stack.push(JSON.parse(stack.pop()));
|
|
602
|
-
return ctx;
|
|
603
|
-
};
|
|
604
|
-
const tojson = (ctx) => {
|
|
605
|
-
const stack = ctx[0];
|
|
606
|
-
exports.ensureStack(stack, 1);
|
|
607
|
-
stack.push(JSON.stringify(stack.pop(), null, 4));
|
|
608
|
-
return ctx;
|
|
609
|
-
};
|
|
610
|
-
|
|
611
|
-
exports.$ = exports.ensureStack;
|
|
612
|
-
exports.$n = exports.ensureStackN;
|
|
613
|
-
exports.$stackFn = $stackFn;
|
|
614
|
-
exports.$try = $try;
|
|
615
|
-
exports.add = add;
|
|
616
|
-
exports.and = and;
|
|
617
|
-
exports.at = at;
|
|
618
|
-
exports.atan2 = atan2;
|
|
619
|
-
exports.bi = bi;
|
|
620
|
-
exports.bi2 = bi2;
|
|
621
|
-
exports.bi3 = bi3;
|
|
622
|
-
exports.bia = bia;
|
|
623
|
-
exports.bia2 = bia2;
|
|
624
|
-
exports.bindkeys = bindkeys;
|
|
625
|
-
exports.bis = bis;
|
|
626
|
-
exports.bis2 = bis2;
|
|
627
|
-
exports.bitand = bitand;
|
|
628
|
-
exports.bitnot = bitnot;
|
|
629
|
-
exports.bitor = bitor;
|
|
630
|
-
exports.bitxor = bitxor;
|
|
631
|
-
exports.both = both;
|
|
632
|
-
exports.casesq = casesq;
|
|
633
|
-
exports.cat = cat;
|
|
634
|
-
exports.catr = catr;
|
|
635
|
-
exports.ceil = ceil;
|
|
636
|
-
exports.collect = collect;
|
|
637
|
-
exports.condq = condq;
|
|
638
|
-
exports.copy = copy;
|
|
639
|
-
exports.cos = cos;
|
|
640
|
-
exports.cpdr = cpdr;
|
|
641
|
-
exports.cpdr2 = cpdr2;
|
|
642
|
-
exports.cprd = cprd;
|
|
643
|
-
exports.cprd2 = cprd2;
|
|
644
|
-
exports.ctx = ctx;
|
|
645
|
-
exports.dec = dec;
|
|
646
|
-
exports.defCases = defCases;
|
|
647
|
-
exports.defCond = defCond;
|
|
648
|
-
exports.defJoin = defJoin;
|
|
649
|
-
exports.defLoadKey = defLoadKey;
|
|
650
|
-
exports.defLoop = defLoop;
|
|
651
|
-
exports.defPush = defPush;
|
|
652
|
-
exports.defStoreKey = defStoreKey;
|
|
653
|
-
exports.defTuple = defTuple;
|
|
654
|
-
exports.defWord = defWord;
|
|
655
|
-
exports.defWordU = defWordU;
|
|
656
|
-
exports.dip = dip;
|
|
657
|
-
exports.dip2 = dip2;
|
|
658
|
-
exports.dip3 = dip3;
|
|
659
|
-
exports.dip4 = dip4;
|
|
660
|
-
exports.div = div;
|
|
661
|
-
exports.dotimes = dotimes;
|
|
662
|
-
exports.drop = drop;
|
|
663
|
-
exports.drop2 = drop2;
|
|
664
|
-
exports.dropif = dropif;
|
|
665
|
-
exports.dsp = dsp;
|
|
666
|
-
exports.dup = dup;
|
|
667
|
-
exports.dup2 = dup2;
|
|
668
|
-
exports.dup3 = dup3;
|
|
669
|
-
exports.dupif = dupif;
|
|
670
|
-
exports.either = either;
|
|
671
|
-
exports.eq = eq;
|
|
672
|
-
exports.equiv = equiv;
|
|
673
|
-
exports.even = even;
|
|
674
|
-
exports.exec = exec;
|
|
675
|
-
exports.execjs = execjs;
|
|
676
|
-
exports.exp = exp;
|
|
677
|
-
exports.floor = floor;
|
|
678
|
-
exports.foldl = foldl;
|
|
679
|
-
exports.fromjson = fromjson;
|
|
680
|
-
exports.gt = gt;
|
|
681
|
-
exports.gteq = gteq;
|
|
682
|
-
exports.hypot = hypot;
|
|
683
|
-
exports.inc = inc;
|
|
684
|
-
exports.invrot = invrot;
|
|
685
|
-
exports.ismatch = ismatch;
|
|
686
|
-
exports.isneg = isneg;
|
|
687
|
-
exports.isnull = isnull;
|
|
688
|
-
exports.ispos = ispos;
|
|
689
|
-
exports.iszero = iszero;
|
|
690
|
-
exports.join = join;
|
|
691
|
-
exports.keep = keep;
|
|
692
|
-
exports.keep2 = keep2;
|
|
693
|
-
exports.keep3 = keep3;
|
|
694
|
-
exports.length = length;
|
|
695
|
-
exports.list = list;
|
|
696
|
-
exports.load = load;
|
|
697
|
-
exports.log = log;
|
|
698
|
-
exports.loopq = loopq;
|
|
699
|
-
exports.lsl = lsl;
|
|
700
|
-
exports.lsr = lsr;
|
|
701
|
-
exports.lsru = lsru;
|
|
702
|
-
exports.lt = lt;
|
|
703
|
-
exports.lteq = lteq;
|
|
704
|
-
exports.map2 = defOp2;
|
|
705
|
-
exports.mapl = mapl;
|
|
706
|
-
exports.mapll = mapll;
|
|
707
|
-
exports.maptos = defOp1;
|
|
708
|
-
exports.max = max;
|
|
709
|
-
exports.min = min;
|
|
710
|
-
exports.mod = mod;
|
|
711
|
-
exports.movdr = movdr;
|
|
712
|
-
exports.movdr2 = movdr2;
|
|
713
|
-
exports.movrd = movrd;
|
|
714
|
-
exports.movrd2 = movrd2;
|
|
715
|
-
exports.mul = mul;
|
|
716
|
-
exports.neg = neg;
|
|
717
|
-
exports.neq = neq;
|
|
718
|
-
exports.nip = nip;
|
|
719
|
-
exports.nop = nop;
|
|
720
|
-
exports.not = not;
|
|
721
|
-
exports.obj = obj;
|
|
722
|
-
exports.odd = odd;
|
|
723
|
-
exports.oneover = oneover;
|
|
724
|
-
exports.op2v = defOp2v;
|
|
725
|
-
exports.or = or;
|
|
726
|
-
exports.over = over;
|
|
727
|
-
exports.pick = pick;
|
|
728
|
-
exports.popr = popr;
|
|
729
|
-
exports.pow = pow;
|
|
730
|
-
exports.print = print;
|
|
731
|
-
exports.printds = printds;
|
|
732
|
-
exports.printrs = printrs;
|
|
733
|
-
exports.pull = pull;
|
|
734
|
-
exports.pull2 = pull2;
|
|
735
|
-
exports.pull3 = pull3;
|
|
736
|
-
exports.pull4 = pull4;
|
|
737
|
-
exports.pushenv = pushenv;
|
|
738
|
-
exports.pushl = pushl;
|
|
739
|
-
exports.pushr = pushr;
|
|
740
|
-
exports.rand = rand;
|
|
741
|
-
exports.rdec = rdec;
|
|
742
|
-
exports.rdrop = rdrop;
|
|
743
|
-
exports.rdrop2 = rdrop2;
|
|
744
|
-
exports.rdup = rdup;
|
|
745
|
-
exports.rdup2 = rdup2;
|
|
746
|
-
exports.rdup3 = rdup3;
|
|
747
|
-
exports.rinc = rinc;
|
|
748
|
-
exports.rot = rot;
|
|
749
|
-
exports.rover = rover;
|
|
750
|
-
exports.rsp = rsp;
|
|
751
|
-
exports.rswap = rswap;
|
|
752
|
-
exports.rswap2 = rswap2;
|
|
753
|
-
exports.run = run;
|
|
754
|
-
exports.runE = runE;
|
|
755
|
-
exports.runU = runU;
|
|
756
|
-
exports.safeMode = safeMode;
|
|
757
|
-
exports.setat = setat;
|
|
758
|
-
exports.sin = sin;
|
|
759
|
-
exports.split = split;
|
|
760
|
-
exports.sqrt = sqrt;
|
|
761
|
-
exports.store = store;
|
|
762
|
-
exports.sub = sub;
|
|
763
|
-
exports.swap = swap;
|
|
764
|
-
exports.swap2 = swap2;
|
|
765
|
-
exports.tan = tan;
|
|
766
|
-
exports.tanh = tanh;
|
|
767
|
-
exports.tojson = tojson;
|
|
768
|
-
exports.tos = tos;
|
|
769
|
-
exports.tri = tri;
|
|
770
|
-
exports.tri2 = tri2;
|
|
771
|
-
exports.tri3 = tri3;
|
|
772
|
-
exports.tria = tria;
|
|
773
|
-
exports.tria2 = tria2;
|
|
774
|
-
exports.tris = tris;
|
|
775
|
-
exports.tris2 = tris2;
|
|
776
|
-
exports.tuck = tuck;
|
|
777
|
-
exports.unwrap = unwrap;
|
|
778
|
-
exports.vadd = vadd;
|
|
779
|
-
exports.vdiv = vdiv;
|
|
780
|
-
exports.vec2 = vec2;
|
|
781
|
-
exports.vec3 = vec3;
|
|
782
|
-
exports.vec4 = vec4;
|
|
783
|
-
exports.vmul = vmul;
|
|
784
|
-
exports.vsub = vsub;
|
|
785
|
-
exports.whenq = whenq;
|