@mark1russell7/vex-compiler 0.7.0 → 0.8.0
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/build/index.d.ts.map +1 -1
- package/build/index.js +482 -61
- package/build/index.js.map +1 -1
- package/package.json +4 -4
- package/src/facts.ts +25 -1
- package/src/lower.ts +59 -11
- package/src/memo.ts +18 -6
- package/src/module.ts +332 -46
package/build/index.js
CHANGED
|
@@ -1,10 +1,33 @@
|
|
|
1
|
-
import { MAX_NESTING, addrExprs, axisTargets, children, compile, findMethod, fixAddr, isFieldName, isListOp, isOpName, isStackOverflow, isVexList, liftsAt, matchesKind, moveExpr, nestingOf, ok, resolveAddr, safeIs, shapeError, vexList } from "@mark1russell7/vex";
|
|
1
|
+
import { MAX_NESTING, addrExprs, axisTargets, children, compile, findMethod, fixAddr, isFieldName, isListOp, isOpName, isStackOverflow, isVexList, liftsAt, listOf, lit, matchesKind, moveExpr, nestingOf, ok, resolveAddr, safeIs, shapeError, vexList } from "@mark1russell7/vex";
|
|
2
2
|
//#region src/facts.ts
|
|
3
3
|
/**
|
|
4
4
|
* The facts of the options that the printer uses (docs/COMPILER.md §13.2). For each op site, they give the domain
|
|
5
5
|
* whose op the interpreter takes first, and the kinds and the lifts of its parameters. The facts come from the op
|
|
6
6
|
* tables, so the same options always give the same facts, and the same source.
|
|
7
7
|
*/
|
|
8
|
+
/**
|
|
9
|
+
* The most bodies of functions that tier 2 puts at the calls of one program. A function that calls another function
|
|
10
|
+
* twice doubles its body, so a chain of such functions grows fast. A module prints each function once.
|
|
11
|
+
*/
|
|
12
|
+
const MAX_CALLS = 1e4;
|
|
13
|
+
/** The list ops that give a list or read a position of a list (LIST.SORT, LIST.TAKE, LIST.NTH). */
|
|
14
|
+
const ORDER_OPS = /* @__PURE__ */ new Set([
|
|
15
|
+
"sortBy",
|
|
16
|
+
"take",
|
|
17
|
+
"nth"
|
|
18
|
+
]);
|
|
19
|
+
/**
|
|
20
|
+
* This function gives the reason that tier 2 cannot take a program with lists as values. The reason is the axis `list`
|
|
21
|
+
* or an op on the order of a list. Tier 1 evaluates such a program. It uses a stack, not recursion.
|
|
22
|
+
*/
|
|
23
|
+
function listReason(expr) {
|
|
24
|
+
const todo = [expr];
|
|
25
|
+
for (let x = todo.pop(); x !== void 0; x = todo.pop()) {
|
|
26
|
+
if ((x.tag === "each" || x.tag === "fold") && listOf(x.axis) !== void 0) return "the program has the axis \"list\", and tier 2 does not have lists as values yet";
|
|
27
|
+
if (x.tag === "app" && ORDER_OPS.has(x.op)) return `the program has the list op "${x.op}", and tier 2 does not have lists as values yet`;
|
|
28
|
+
todo.push(...children(x));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
8
31
|
/** This function gives the kind of the values of a domain. */
|
|
9
32
|
const kindOfDomain = (d) => d.primitive ?? `domain:${d.name}`;
|
|
10
33
|
/** This function gives a kind if it is a primitive type, else `undefined`. */
|
|
@@ -491,14 +514,16 @@ function reads(e, outer, inner, top) {
|
|
|
491
514
|
case "var": return inner.has(e.name) ? 0 : outer.has(e.name) ? INTERNAL : inLoop(FREE, top);
|
|
492
515
|
case "ext":
|
|
493
516
|
case "fold":
|
|
494
|
-
case "iterate":
|
|
517
|
+
case "iterate":
|
|
518
|
+
case "fn":
|
|
519
|
+
case "call": return OPAQUE;
|
|
495
520
|
case "ref": return addrExprs(e.at).reduce((r, x) => r | reads(x, outer, inner, top), addressReads(e.at, top));
|
|
496
521
|
case "app": return e.args.reduce((r, x) => r | reads(x, outer, inner, top), 0);
|
|
497
522
|
case "rec": return Object.values(e.fields).reduce((r, x) => r | reads(x, outer, inner, top), 0);
|
|
498
523
|
case "let": return Object.values(e.bind).reduce((r, x) => r | reads(x, outer, inner, top), 0) | reads(e.body, outer, /* @__PURE__ */ new Set([...inner, ...Object.keys(e.bind)]), top);
|
|
499
524
|
case "each": {
|
|
500
525
|
const { base, tests } = parts(e.axis);
|
|
501
|
-
const targets = base.t === "table" ? OPAQUE : base.t === "all" && (e.at ?? []).length === 0 ? 0 : addressReads(e.at, top);
|
|
526
|
+
const targets = base.t === "table" || listOf(e.axis) !== void 0 ? OPAQUE : base.t === "all" && (e.at ?? []).length === 0 ? 0 : addressReads(e.at, top);
|
|
502
527
|
const at = addrExprs(e.at).reduce((r, x) => r | reads(x, outer, inner, top), targets);
|
|
503
528
|
return [e.body, ...tests].reduce((r, x) => r | reads(x, outer, inner, false), at);
|
|
504
529
|
}
|
|
@@ -529,7 +554,7 @@ function keysOf(e, top, outer, pure, root = false) {
|
|
|
529
554
|
}
|
|
530
555
|
case "each": {
|
|
531
556
|
const { base, tests } = parts(e.axis);
|
|
532
|
-
if (base.t === "table" || (base.t !== "all" || (e.at ?? []).length > 0) && addressReads(e.at, top) !== 0) return void 0;
|
|
557
|
+
if (base.t === "table" || listOf(e.axis) !== void 0 || (base.t !== "all" || (e.at ?? []).length > 0) && addressReads(e.at, top) !== 0) return void 0;
|
|
533
558
|
if (addrExprs(e.at).some((x) => reads(x, outer, /* @__PURE__ */ new Set(), top) !== 0)) return void 0;
|
|
534
559
|
return all([e.body, ...tests].map((x) => keysOf(x, false, outer, pure)));
|
|
535
560
|
}
|
|
@@ -538,7 +563,9 @@ function keysOf(e, top, outer, pure, root = false) {
|
|
|
538
563
|
case "var":
|
|
539
564
|
case "ext":
|
|
540
565
|
case "fold":
|
|
541
|
-
case "iterate":
|
|
566
|
+
case "iterate":
|
|
567
|
+
case "fn":
|
|
568
|
+
case "call": return;
|
|
542
569
|
}
|
|
543
570
|
}
|
|
544
571
|
/** This function tells if an expression has a loop outside its keys and outside the parts that have a memo of their own. */
|
|
@@ -554,7 +581,9 @@ function loopOutside(e, keys) {
|
|
|
554
581
|
case "ref": return addrExprs(e.at).some((x) => loopOutside(x, keys));
|
|
555
582
|
case "lit":
|
|
556
583
|
case "var":
|
|
557
|
-
case "ext":
|
|
584
|
+
case "ext":
|
|
585
|
+
case "fn":
|
|
586
|
+
case "call": return false;
|
|
558
587
|
}
|
|
559
588
|
}
|
|
560
589
|
/** This function gives the `let` expressions above an `each`, and the `each`, or `undefined` for another expression. */
|
|
@@ -666,6 +695,8 @@ function lower(expr, facts = factsOf({}), schema = {}) {
|
|
|
666
695
|
const shape = shapeError(expr);
|
|
667
696
|
if (shape !== void 0) throw new Unsupported(shape);
|
|
668
697
|
if (nestingOf(expr) > MAX_NESTING) throw new Unsupported(`the expression is nested deeper than ${MAX_NESTING} levels`);
|
|
698
|
+
const lists = listReason(expr);
|
|
699
|
+
if (lists !== void 0) throw new Unsupported(lists);
|
|
669
700
|
const l = new Lowerer(memoNodes(expr), facts, schema);
|
|
670
701
|
const b = block(RETURN);
|
|
671
702
|
l.check(b, call$1("has", local(0), local(1)), "the origin is a key of the space");
|
|
@@ -674,6 +705,7 @@ function lower(expr, facts = factsOf({}), schema = {}) {
|
|
|
674
705
|
origin: local(1),
|
|
675
706
|
focus: local(1),
|
|
676
707
|
env: /* @__PURE__ */ new Map(),
|
|
708
|
+
fns: /* @__PURE__ */ new Map(),
|
|
677
709
|
rec: prim("get", local(0), local(1))
|
|
678
710
|
};
|
|
679
711
|
const value = l.expr(expr, top, [], b);
|
|
@@ -691,6 +723,8 @@ var Lowerer = class {
|
|
|
691
723
|
locals = 4;
|
|
692
724
|
labels = 0;
|
|
693
725
|
memos = 0;
|
|
726
|
+
/** The number of bodies of functions that the lowering put at calls (MAX_CALLS). */
|
|
727
|
+
#calls = 0;
|
|
694
728
|
#memo;
|
|
695
729
|
/** The keys of the nodes of the memo whose code the lowering makes, with their values. */
|
|
696
730
|
#keys = /* @__PURE__ */ new Map();
|
|
@@ -801,6 +835,7 @@ var Lowerer = class {
|
|
|
801
835
|
case "ref": return this.#ref(e, s, path, b);
|
|
802
836
|
case "var": {
|
|
803
837
|
const bound = s.env.get(e.name);
|
|
838
|
+
if (bound === void 0 && s.fns.has(e.name)) return this.fail(b);
|
|
804
839
|
if (bound === void 0) {
|
|
805
840
|
const x = this.checked(b, call$1("var", local(2), this.constant(e.name)), "a variable");
|
|
806
841
|
return this.#declared(b, x, primitiveOf(this.#schema.vars?.[e.name]), "a variable of the schema");
|
|
@@ -808,13 +843,13 @@ var Lowerer = class {
|
|
|
808
843
|
if (bound.ok !== void 0) this.check(b, bound.ok, "a binding with a value");
|
|
809
844
|
return bound.value;
|
|
810
845
|
}
|
|
811
|
-
case "let": {
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
846
|
+
case "let": return this.expr(e.body, {
|
|
847
|
+
...s,
|
|
848
|
+
...this.#bind(e, s, path, b)
|
|
849
|
+
}, [...path, Object.keys(e.bind).length], b);
|
|
850
|
+
/* v8 ignore next -- @preserve */
|
|
851
|
+
case "fn": return this.fail(b);
|
|
852
|
+
case "call": return this.#call(e, s, path, b);
|
|
818
853
|
case "rec": {
|
|
819
854
|
const values = Object.values(e.fields).map((x, i) => this.expr(x, s, [...path, i], b));
|
|
820
855
|
return this.value(b, call$1("rec", this.constant(Object.keys(e.fields)), array(...values)));
|
|
@@ -967,10 +1002,38 @@ var Lowerer = class {
|
|
|
967
1002
|
*/
|
|
968
1003
|
#bind(e, s, path, b) {
|
|
969
1004
|
const env = new Map(s.env);
|
|
1005
|
+
const fns = new Map(s.fns);
|
|
970
1006
|
Object.entries(e.bind).forEach(([name, x], i) => {
|
|
971
|
-
|
|
1007
|
+
if (x.tag === "fn") {
|
|
1008
|
+
fns.set(name, {
|
|
1009
|
+
fn: x,
|
|
1010
|
+
path: [...path, i],
|
|
1011
|
+
scope: s
|
|
1012
|
+
});
|
|
1013
|
+
env.delete(name);
|
|
1014
|
+
} else env.set(name, this.#attempt(b, (inner) => this.expr(x, s, [...path, i], inner)));
|
|
972
1015
|
});
|
|
973
|
-
return
|
|
1016
|
+
return {
|
|
1017
|
+
env,
|
|
1018
|
+
fns
|
|
1019
|
+
};
|
|
1020
|
+
}
|
|
1021
|
+
/**
|
|
1022
|
+
* This method lowers a call (FN.CALL). The body of the function goes at the place of the call, with the values of the
|
|
1023
|
+
* arguments for its parameters. The body sees the names of the place of its binding. A call that is not valid goes
|
|
1024
|
+
* to tier 1.
|
|
1025
|
+
*/
|
|
1026
|
+
#call(e, s, path, b) {
|
|
1027
|
+
const bound = s.env.has(e.fn) ? void 0 : s.fns.get(e.fn);
|
|
1028
|
+
if (bound?.fn.params.length !== e.args.length) return this.fail(b);
|
|
1029
|
+
if (++this.#calls > 1e4) throw new Unsupported(`the calls put the bodies of functions in more than ${String(MAX_CALLS)} places`);
|
|
1030
|
+
const env = new Map(bound.scope.env);
|
|
1031
|
+
bound.fn.params.forEach((p, i) => env.set(p, this.#attempt(b, (inner) => this.expr(e.args[i], s, [...path, i], inner))));
|
|
1032
|
+
return this.expr(bound.fn.body, {
|
|
1033
|
+
...s,
|
|
1034
|
+
env,
|
|
1035
|
+
fns: bound.scope.fns
|
|
1036
|
+
}, [...bound.path, 0], b);
|
|
974
1037
|
}
|
|
975
1038
|
#app(e, s, path, b) {
|
|
976
1039
|
const { op, args } = e;
|
|
@@ -1119,7 +1182,7 @@ var Lowerer = class {
|
|
|
1119
1182
|
for (let x = e.args[0]; x.tag === "let"; x = x.body) {
|
|
1120
1183
|
inner = {
|
|
1121
1184
|
...inner,
|
|
1122
|
-
|
|
1185
|
+
...this.#bind(x, inner, at, b)
|
|
1123
1186
|
};
|
|
1124
1187
|
at = [...at, Object.keys(x.bind).length];
|
|
1125
1188
|
}
|
|
@@ -1258,6 +1321,11 @@ var Lowerer = class {
|
|
|
1258
1321
|
});
|
|
1259
1322
|
nonEmpty();
|
|
1260
1323
|
return local(acc);
|
|
1324
|
+
case "sortBy":
|
|
1325
|
+
case "take":
|
|
1326
|
+
case "nth":
|
|
1327
|
+
/* v8 ignore next -- @preserve */
|
|
1328
|
+
throw new Unsupported(`the list op "${op}"`);
|
|
1261
1329
|
}
|
|
1262
1330
|
}
|
|
1263
1331
|
/**
|
|
@@ -1333,6 +1401,7 @@ var Lowerer = class {
|
|
|
1333
1401
|
origin: base.t === "table" ? key : s.origin,
|
|
1334
1402
|
focus: key,
|
|
1335
1403
|
env: s.env,
|
|
1404
|
+
fns: s.fns,
|
|
1336
1405
|
rec: prim("recordAt", records, slot, space, key)
|
|
1337
1406
|
};
|
|
1338
1407
|
whereTests(axis).forEach((test, i) => {
|
|
@@ -1797,6 +1866,17 @@ const HELPERS = {
|
|
|
1797
1866
|
X: "function X(t, n) { return t >= 0 && t < n ? t : F(); }",
|
|
1798
1867
|
I: "function I(t, n) { return Number.isInteger(t) && t >= 0 && t < n ? t : F(); }",
|
|
1799
1868
|
K: "function K(k, n) { return typeof k === \"string\" && /^(0|[1-9]\\d*)$/.test(k) && +k < n ? +k : F(); }",
|
|
1869
|
+
V: "function V(l, s) { if (s && l.some((x) => x[1] === E)) F(); return l.filter((x) => x[1] !== E); }",
|
|
1870
|
+
SB: [
|
|
1871
|
+
"function SB(l, s) {",
|
|
1872
|
+
" const o = V(l, s);",
|
|
1873
|
+
" const t = o.length > 0 ? typeof o[0][1] : \"number\";",
|
|
1874
|
+
" for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
|
|
1875
|
+
" return o.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
|
|
1876
|
+
"}"
|
|
1877
|
+
].join("\n"),
|
|
1878
|
+
TK: "function TK(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(0, n) : F(); }",
|
|
1879
|
+
NT: "function NT(l, i, s) { const o = V(l, s); return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F(); }",
|
|
1800
1880
|
M1: [
|
|
1801
1881
|
"function M1(f) {",
|
|
1802
1882
|
" const m = new Map();",
|
|
@@ -1933,7 +2013,11 @@ const NEEDS = {
|
|
|
1933
2013
|
K: ["F"],
|
|
1934
2014
|
M: ["G"],
|
|
1935
2015
|
M1: ["G"],
|
|
1936
|
-
S: ["M1", "F"]
|
|
2016
|
+
S: ["M1", "F"],
|
|
2017
|
+
V: ["F"],
|
|
2018
|
+
SB: ["V", "F"],
|
|
2019
|
+
TK: ["V", "F"],
|
|
2020
|
+
NT: ["V", "F"]
|
|
1937
2021
|
};
|
|
1938
2022
|
/** The list ops of a standalone module. */
|
|
1939
2023
|
const LIST_OPS = /* @__PURE__ */ new Set([
|
|
@@ -1973,6 +2057,8 @@ function namesOf(e, out = /* @__PURE__ */ new Set()) {
|
|
|
1973
2057
|
* constant of the run count as one node each. A part with less work costs less than the read of its column.
|
|
1974
2058
|
*/
|
|
1975
2059
|
const COLUMN_WORK = 10;
|
|
2060
|
+
/** This function gives the head of a loop over targets: from `lo` up to `hi`, or from `hi` down to `lo` in the reverse order. */
|
|
2061
|
+
const forHead = (j, lo, hi, reverse) => reverse ? `for (let ${j} = ${hi} - 1; ${j} >= ${lo}; ${j}--) {` : `for (let ${j} = ${lo}; ${j} < ${hi}; ${j}++) {`;
|
|
1976
2062
|
/** This function tells if an expression has a loop of names: a fold or an iteration. */
|
|
1977
2063
|
function hasLoop(e) {
|
|
1978
2064
|
let found = LOOPS.get(e);
|
|
@@ -2192,6 +2278,9 @@ var Printer = class {
|
|
|
2192
2278
|
/** The parts of the helper S that the nodes of the memo that keep their items need (helperS). */
|
|
2193
2279
|
features = /* @__PURE__ */ new Set();
|
|
2194
2280
|
#names = 0;
|
|
2281
|
+
/** The number of the next bound function, and of the next printed function. */
|
|
2282
|
+
#fnIds = 0;
|
|
2283
|
+
#printed = 0;
|
|
2195
2284
|
/** The level of the names of the next loop. It is far above the levels of the axes, so the two do not meet. */
|
|
2196
2285
|
#loops = 1e6;
|
|
2197
2286
|
/** The node of the memo whose function the printer prints, so it prints the node itself. */
|
|
@@ -2210,6 +2299,10 @@ var Printer = class {
|
|
|
2210
2299
|
#columns = /* @__PURE__ */ new Map();
|
|
2211
2300
|
/** The expressions whose code is a constant of the run. */
|
|
2212
2301
|
#constant = /* @__PURE__ */ new WeakSet();
|
|
2302
|
+
/** The expressions at a place that takes a list: a binding, the head of a list op and the list of an axis. */
|
|
2303
|
+
#wantList = /* @__PURE__ */ new WeakSet();
|
|
2304
|
+
/** The facts of the list of each function of the memo whose value is a list. */
|
|
2305
|
+
#lists = /* @__PURE__ */ new Map();
|
|
2213
2306
|
constructor(expr, options) {
|
|
2214
2307
|
this.#domains = options.domains ?? [];
|
|
2215
2308
|
this.#facts = factsOf({ domains: this.#domains });
|
|
@@ -2293,7 +2386,8 @@ var Printer = class {
|
|
|
2293
2386
|
const base = {
|
|
2294
2387
|
kind: name.kind,
|
|
2295
2388
|
deps: name.deps,
|
|
2296
|
-
plain: true
|
|
2389
|
+
plain: true,
|
|
2390
|
+
list: name.list
|
|
2297
2391
|
};
|
|
2298
2392
|
return name.lazy ? {
|
|
2299
2393
|
...base,
|
|
@@ -2312,7 +2406,8 @@ var Printer = class {
|
|
|
2312
2406
|
code: v.held ?? v.code,
|
|
2313
2407
|
kind: v.kind,
|
|
2314
2408
|
lazy: v.held !== void 0,
|
|
2315
|
-
deps: NO_DEPS
|
|
2409
|
+
deps: NO_DEPS,
|
|
2410
|
+
list: v.list
|
|
2316
2411
|
};
|
|
2317
2412
|
}
|
|
2318
2413
|
/**
|
|
@@ -2345,16 +2440,19 @@ var Printer = class {
|
|
|
2345
2440
|
const name = this.fresh("c");
|
|
2346
2441
|
if (lazy) this.run.push(`const ${name} = ${this.use("L")}(() => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
|
|
2347
2442
|
else this.run.push(...this.#store(name, c, true));
|
|
2443
|
+
const { list } = c.v;
|
|
2348
2444
|
const read = bare(this.#read(lazy ? {
|
|
2349
2445
|
code: `${name}()`,
|
|
2350
2446
|
kind: c.v.kind,
|
|
2351
2447
|
lazy: true,
|
|
2352
|
-
deps: NO_DEPS
|
|
2448
|
+
deps: NO_DEPS,
|
|
2449
|
+
list
|
|
2353
2450
|
} : {
|
|
2354
2451
|
code: name,
|
|
2355
2452
|
kind: c.v.kind,
|
|
2356
2453
|
lazy: !c.v.safe,
|
|
2357
|
-
deps: NO_DEPS
|
|
2454
|
+
deps: NO_DEPS,
|
|
2455
|
+
list
|
|
2358
2456
|
}));
|
|
2359
2457
|
if (key !== void 0) this.#constants.set(key, read);
|
|
2360
2458
|
return read;
|
|
@@ -2375,17 +2473,36 @@ var Printer = class {
|
|
|
2375
2473
|
this.#keyOf.set(c, this.#constKey(e, ctx));
|
|
2376
2474
|
this.#constant.add(e);
|
|
2377
2475
|
}
|
|
2476
|
+
if (c.v.list !== void 0 && !this.#wantList.has(e)) throw new NotStandalone("a list is a value of the program");
|
|
2378
2477
|
return this.#columnar(e, c, ctx) ? this.#column(e, c, ctx) : c;
|
|
2379
2478
|
}
|
|
2479
|
+
/** This method gives the code of a list at a place that takes a list (LIST.VALUE): an each, a name, sortBy or take. */
|
|
2480
|
+
#listOf(e, ctx) {
|
|
2481
|
+
this.#wantList.add(e);
|
|
2482
|
+
const c = this.expr(e, ctx);
|
|
2483
|
+
if (c.v.list === void 0) throw new NotStandalone("a list op on a value that is not a list");
|
|
2484
|
+
return c;
|
|
2485
|
+
}
|
|
2486
|
+
/** This method tells if an expression gives a list at a place that takes one: an each, sortBy, take or a name of a list. */
|
|
2487
|
+
#isList(e, ctx) {
|
|
2488
|
+
if (e.tag === "each") return true;
|
|
2489
|
+
if (e.tag === "app") return e.op === "sortBy" || e.op === "take";
|
|
2490
|
+
return e.tag === "var" && ctx.env.get(e.name)?.list !== void 0;
|
|
2491
|
+
}
|
|
2380
2492
|
/**
|
|
2381
2493
|
* This method tells if a part of a loop over the targets is a column of the run. Its value depends only on the
|
|
2382
2494
|
* target and on the constants of the run, so each run computes it once for each record. Each name that it reads
|
|
2383
2495
|
* from outside is a constant of the run, so its code can be outside the loop.
|
|
2384
2496
|
*/
|
|
2385
2497
|
#columnar(e, c, ctx) {
|
|
2386
|
-
if (ctx.level < 2 ||
|
|
2498
|
+
if (ctx.level < 2 || c.v.deps.size !== 1 || !c.v.deps.has(ctx.level) || this.#work(e) < COLUMN_WORK) return false;
|
|
2387
2499
|
if (this.#keyed(e)) return false;
|
|
2388
|
-
return [...namesOf(e)].every((name) => (ctx.env.get(name)?.deps.size ?? 0) === 0);
|
|
2500
|
+
return [...namesOf(e)].every((name) => (ctx.env.get(name)?.deps.size ?? 0) === 0) && !this.#callsLocal(e, ctx);
|
|
2501
|
+
}
|
|
2502
|
+
/** This method tells if an expression calls a function that is at the place of its binding, not at the start of a run. */
|
|
2503
|
+
#callsLocal(e, ctx) {
|
|
2504
|
+
if (e.tag === "call" && ctx.fns.get(e.fn)?.local === true) return true;
|
|
2505
|
+
return children(e).some((x) => this.#callsLocal(x, ctx));
|
|
2389
2506
|
}
|
|
2390
2507
|
/** This method tells if an expression has a node of the memo with keys in it. */
|
|
2391
2508
|
#keyed(e) {
|
|
@@ -2409,7 +2526,8 @@ var Printer = class {
|
|
|
2409
2526
|
code: `${this.use("G")}(${name}(${ctx.focus}))`,
|
|
2410
2527
|
kind: c.v.kind,
|
|
2411
2528
|
safe: false,
|
|
2412
|
-
deps: c.v.deps
|
|
2529
|
+
deps: c.v.deps,
|
|
2530
|
+
list: c.v.list
|
|
2413
2531
|
});
|
|
2414
2532
|
}
|
|
2415
2533
|
#node(e, ctx) {
|
|
@@ -2418,6 +2536,9 @@ var Printer = class {
|
|
|
2418
2536
|
case "ref": return this.#ref(e, ctx);
|
|
2419
2537
|
case "var": return bare(this.#var(e.name, ctx));
|
|
2420
2538
|
case "let": return this.#let(e, ctx);
|
|
2539
|
+
/* v8 ignore next -- @preserve */
|
|
2540
|
+
case "fn": throw new NotStandalone("a function outside the bindings of a let");
|
|
2541
|
+
case "call": return this.#call(e, ctx);
|
|
2421
2542
|
case "rec": {
|
|
2422
2543
|
const names = Object.keys(e.fields);
|
|
2423
2544
|
const raw = Object.values(e.fields).map((x) => this.expr(x, ctx));
|
|
@@ -2435,7 +2556,7 @@ var Printer = class {
|
|
|
2435
2556
|
};
|
|
2436
2557
|
}
|
|
2437
2558
|
case "app": return this.#app(e, ctx);
|
|
2438
|
-
case "each":
|
|
2559
|
+
case "each": return this.#eachList(e, ctx);
|
|
2439
2560
|
case "fold": return this.#fold(e, ctx);
|
|
2440
2561
|
case "iterate": return this.#iterate(e, ctx);
|
|
2441
2562
|
case "ext": throw new NotStandalone(`the extension "${e.kind}"`);
|
|
@@ -2521,7 +2642,7 @@ var Printer = class {
|
|
|
2521
2642
|
for (const m of at) {
|
|
2522
2643
|
if (fails) for (const d of deps) kept.add(d);
|
|
2523
2644
|
if (moveExpr(m) !== void 0) deps = values[next++].v.deps;
|
|
2524
|
-
else if (m.t === "origin") deps = /* @__PURE__ */ new Set([
|
|
2645
|
+
else if (m.t === "origin") deps = /* @__PURE__ */ new Set([ctx.originLevel]);
|
|
2525
2646
|
else if (m.t === "key" || m.t === "index" || m.t === "parent") deps = NO_DEPS;
|
|
2526
2647
|
fails = m.t !== "origin";
|
|
2527
2648
|
}
|
|
@@ -2530,6 +2651,7 @@ var Printer = class {
|
|
|
2530
2651
|
#var(name, ctx) {
|
|
2531
2652
|
const bound = ctx.env.get(name);
|
|
2532
2653
|
if (bound !== void 0) return this.#read(bound);
|
|
2654
|
+
if (ctx.fns.has(name)) throw new NotStandalone(`the name "${name}" is a function, not a value`);
|
|
2533
2655
|
const kind = this.#schema.vars?.[name];
|
|
2534
2656
|
if (kind === void 0) throw new NotStandalone(`a variable that is not in the schema: "${name}"`);
|
|
2535
2657
|
let code = this.vars.get(name);
|
|
@@ -2545,9 +2667,26 @@ var Printer = class {
|
|
|
2545
2667
|
*/
|
|
2546
2668
|
#let(e, ctx) {
|
|
2547
2669
|
const env = new Map(ctx.env);
|
|
2670
|
+
const fns = new Map(ctx.fns);
|
|
2548
2671
|
const s = [];
|
|
2549
2672
|
let deps = /* @__PURE__ */ new Set();
|
|
2673
|
+
const bound = [];
|
|
2550
2674
|
for (const [name, x] of Object.entries(e.bind)) {
|
|
2675
|
+
if (x.tag === "fn") {
|
|
2676
|
+
const b = {
|
|
2677
|
+
id: this.#fnIds++,
|
|
2678
|
+
fn: x,
|
|
2679
|
+
ctx,
|
|
2680
|
+
specs: /* @__PURE__ */ new Map(),
|
|
2681
|
+
decls: [],
|
|
2682
|
+
local: false
|
|
2683
|
+
};
|
|
2684
|
+
bound.push(b);
|
|
2685
|
+
fns.set(name, b);
|
|
2686
|
+
env.delete(name);
|
|
2687
|
+
continue;
|
|
2688
|
+
}
|
|
2689
|
+
this.#wantList.add(x);
|
|
2551
2690
|
const c = this.#hoist(this.expr(x, ctx), hasLoop(x));
|
|
2552
2691
|
if (c.v.deps.size === 0) {
|
|
2553
2692
|
env.set(name, this.#nameOf(c.v));
|
|
@@ -2559,22 +2698,133 @@ var Printer = class {
|
|
|
2559
2698
|
code: b,
|
|
2560
2699
|
kind: c.v.kind,
|
|
2561
2700
|
lazy: !c.v.safe,
|
|
2562
|
-
deps: c.v.deps
|
|
2701
|
+
deps: c.v.deps,
|
|
2702
|
+
list: c.v.list
|
|
2563
2703
|
});
|
|
2564
2704
|
deps = /* @__PURE__ */ new Set([...deps, ...c.v.deps]);
|
|
2565
2705
|
}
|
|
2566
2706
|
const body = this.expr(e.body, {
|
|
2567
2707
|
...ctx,
|
|
2568
|
-
env
|
|
2708
|
+
env,
|
|
2709
|
+
fns
|
|
2569
2710
|
});
|
|
2570
2711
|
return {
|
|
2571
|
-
s: [
|
|
2712
|
+
s: [
|
|
2713
|
+
...s,
|
|
2714
|
+
...bound.flatMap((b) => b.decls),
|
|
2715
|
+
...body.s
|
|
2716
|
+
],
|
|
2572
2717
|
v: {
|
|
2573
2718
|
...body.v,
|
|
2574
2719
|
deps: /* @__PURE__ */ new Set([...deps, ...body.v.deps])
|
|
2575
2720
|
}
|
|
2576
2721
|
};
|
|
2577
2722
|
}
|
|
2723
|
+
/**
|
|
2724
|
+
* This method prints a call (FN.CALL). Each argument evaluates at the place of the call, as a binding of a let. The
|
|
2725
|
+
* call goes to the printed function of the signature of its arguments, with the focus and the origin if it reads them.
|
|
2726
|
+
*/
|
|
2727
|
+
#call(e, ctx) {
|
|
2728
|
+
const bound = ctx.env.has(e.fn) ? void 0 : ctx.fns.get(e.fn);
|
|
2729
|
+
if (bound === void 0) throw new NotStandalone(`a call of "${e.fn}", which is not a function`);
|
|
2730
|
+
if (bound.fn.params.length !== e.args.length) throw new NotStandalone(`a call of "${e.fn}" with ${String(e.args.length)} arguments, not ${String(bound.fn.params.length)}`);
|
|
2731
|
+
const s = [];
|
|
2732
|
+
const args = e.args.map((x) => {
|
|
2733
|
+
const c = this.#hoist(this.expr(x, ctx), hasLoop(x));
|
|
2734
|
+
if (c.v.safe) {
|
|
2735
|
+
s.push(...c.s);
|
|
2736
|
+
return {
|
|
2737
|
+
code: c.v.code,
|
|
2738
|
+
kind: c.v.kind,
|
|
2739
|
+
deps: c.v.deps
|
|
2740
|
+
};
|
|
2741
|
+
}
|
|
2742
|
+
if (c.s.length === 0 && c.v.held !== void 0) return {
|
|
2743
|
+
code: c.v.held,
|
|
2744
|
+
kind: c.v.kind,
|
|
2745
|
+
deps: c.v.deps
|
|
2746
|
+
};
|
|
2747
|
+
const b = this.fresh("b");
|
|
2748
|
+
s.push(...this.#store(b, c));
|
|
2749
|
+
return {
|
|
2750
|
+
code: b,
|
|
2751
|
+
kind: c.v.kind,
|
|
2752
|
+
deps: c.v.deps
|
|
2753
|
+
};
|
|
2754
|
+
});
|
|
2755
|
+
const sig = JSON.stringify(args.map((a) => a.kind ?? null));
|
|
2756
|
+
const spec = bound.specs.get(sig) ?? this.#printFn(bound, args, sig);
|
|
2757
|
+
const levels = (d) => d === spec.origin ? [ctx.originLevel] : d === spec.focus ? [ctx.level] : d === spec.param ? [] : [d];
|
|
2758
|
+
const deps = /* @__PURE__ */ new Set([...args.flatMap((a) => [...a.deps]), ...[...spec.deps].flatMap(levels)]);
|
|
2759
|
+
const position = spec.deps.has(spec.origin) ? [ctx.focus, ctx.origin] : spec.deps.has(spec.focus) ? [ctx.focus] : [];
|
|
2760
|
+
return {
|
|
2761
|
+
s,
|
|
2762
|
+
v: {
|
|
2763
|
+
code: `${spec.name}(${[...args.map((a) => a.code), ...position].join(", ")})`,
|
|
2764
|
+
kind: spec.kind,
|
|
2765
|
+
safe: spec.safe,
|
|
2766
|
+
deps
|
|
2767
|
+
}
|
|
2768
|
+
};
|
|
2769
|
+
}
|
|
2770
|
+
/**
|
|
2771
|
+
* This method prints a bound function for one signature of its arguments, as one JavaScript function. Its parameters
|
|
2772
|
+
* are the arguments, then the focus and the origin, if the body reads them. A function that reads a name of the place
|
|
2773
|
+
* of its binding is at that place. Each other function is at the start of a run, so a constant of the run can call it.
|
|
2774
|
+
*/
|
|
2775
|
+
#printFn(bound, args, sig) {
|
|
2776
|
+
const origin = 2e6 + 1e3 * this.#printed++;
|
|
2777
|
+
const focus = origin + 1;
|
|
2778
|
+
const param = -origin;
|
|
2779
|
+
const names = args.map(() => this.fresh("p"));
|
|
2780
|
+
const k = this.fresh("k");
|
|
2781
|
+
const o = this.fresh("o");
|
|
2782
|
+
const env = new Map(bound.ctx.env);
|
|
2783
|
+
bound.fn.params.forEach((p, i) => env.set(p, {
|
|
2784
|
+
code: names[i],
|
|
2785
|
+
kind: args[i].kind,
|
|
2786
|
+
lazy: true,
|
|
2787
|
+
deps: /* @__PURE__ */ new Set([param])
|
|
2788
|
+
}));
|
|
2789
|
+
const body = this.expr(bound.fn.body, {
|
|
2790
|
+
origin: o,
|
|
2791
|
+
focus: k,
|
|
2792
|
+
level: focus,
|
|
2793
|
+
originLevel: origin,
|
|
2794
|
+
env,
|
|
2795
|
+
fns: bound.ctx.fns,
|
|
2796
|
+
keys: /* @__PURE__ */ new Map()
|
|
2797
|
+
});
|
|
2798
|
+
const markers = /* @__PURE__ */ new Set([
|
|
2799
|
+
origin,
|
|
2800
|
+
focus,
|
|
2801
|
+
param
|
|
2802
|
+
]);
|
|
2803
|
+
const global = [...body.v.deps].every((d) => markers.has(d));
|
|
2804
|
+
const name = this.fresh("fn");
|
|
2805
|
+
const position = body.v.deps.has(origin) ? [k, o] : body.v.deps.has(focus) ? [k] : [];
|
|
2806
|
+
const head = `const ${name} = (${[...names, ...position].join(", ")}) =>`;
|
|
2807
|
+
const decl = body.s.length === 0 ? [`${head} ${body.v.code};`] : [
|
|
2808
|
+
`${head} {`,
|
|
2809
|
+
...ind(body.s),
|
|
2810
|
+
` return ${body.v.code};`,
|
|
2811
|
+
"};"
|
|
2812
|
+
];
|
|
2813
|
+
(global ? this.run : bound.decls).push(...decl);
|
|
2814
|
+
if (!global) bound.local = true;
|
|
2815
|
+
const spec = {
|
|
2816
|
+
name,
|
|
2817
|
+
kind: body.v.kind,
|
|
2818
|
+
safe: body.v.safe,
|
|
2819
|
+
deps: body.v.deps,
|
|
2820
|
+
origin,
|
|
2821
|
+
focus,
|
|
2822
|
+
param,
|
|
2823
|
+
global
|
|
2824
|
+
};
|
|
2825
|
+
bound.specs.set(sig, spec);
|
|
2826
|
+
return spec;
|
|
2827
|
+
}
|
|
2578
2828
|
#boolean(v) {
|
|
2579
2829
|
return v.kind === "boolean" ? v.code : `${this.use("B")}(${v.code})`;
|
|
2580
2830
|
}
|
|
@@ -2589,6 +2839,8 @@ var Printer = class {
|
|
|
2589
2839
|
if (op === "ifError") return this.#ifError(args, ctx);
|
|
2590
2840
|
const [head] = args;
|
|
2591
2841
|
if (LIST_OPS.has(op) && head?.tag === "each") return this.#list(op, head, args.slice(1), ctx);
|
|
2842
|
+
if (head !== void 0 && (op === "sortBy" || op === "take")) return this.#order(op, head, args.slice(1), ctx);
|
|
2843
|
+
if (head !== void 0 && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, args.slice(1), ctx);
|
|
2592
2844
|
const raw = args.map((x) => this.expr(x, ctx));
|
|
2593
2845
|
const deps = depsOf(...raw);
|
|
2594
2846
|
const values = this.#settle(raw, deps, args);
|
|
@@ -2774,13 +3026,37 @@ var Printer = class {
|
|
|
2774
3026
|
* window have a loop.
|
|
2775
3027
|
*/
|
|
2776
3028
|
#targetLoop(axis, at, ctx) {
|
|
2777
|
-
const { base, tests } = testsOf(axis);
|
|
3029
|
+
const { base: outer, tests } = testsOf(axis);
|
|
3030
|
+
const reverse = outer.t === "reverse";
|
|
3031
|
+
const base = outer.t === "reverse" ? outer.axis : outer;
|
|
2778
3032
|
const j = this.fresh("j");
|
|
2779
3033
|
const inner = {
|
|
2780
3034
|
...ctx,
|
|
2781
3035
|
focus: j,
|
|
2782
3036
|
level: ctx.level + 1
|
|
2783
3037
|
};
|
|
3038
|
+
if (base.t === "list") {
|
|
3039
|
+
if (at.length > 0) throw new NotStandalone("the axis \"list\" with an address");
|
|
3040
|
+
const list = this.#hoist(this.#listOf(base.list, ctx), hasLoop(base.list));
|
|
3041
|
+
const a = this.fresh("A");
|
|
3042
|
+
const q = this.fresh("q");
|
|
3043
|
+
const head = reverse ? `for (let ${q} = ${a}.length - 1; ${q} >= 0; ${q}--) {` : `for (let ${q} = 0; ${q} < ${a}.length; ${q}++) {`;
|
|
3044
|
+
const key = [`const ${j} = ${a}[${q}][0];`];
|
|
3045
|
+
const check = [`if (${a}[${q}][1] === E) ${this.use("F")}();`];
|
|
3046
|
+
return {
|
|
3047
|
+
s: [...list.s, `const ${a} = ${list.v.code};`],
|
|
3048
|
+
j,
|
|
3049
|
+
head,
|
|
3050
|
+
key,
|
|
3051
|
+
check,
|
|
3052
|
+
skip: void 0,
|
|
3053
|
+
fixed: false,
|
|
3054
|
+
start: list.v.deps,
|
|
3055
|
+
startSafe: list.v.safe,
|
|
3056
|
+
tests,
|
|
3057
|
+
inner
|
|
3058
|
+
};
|
|
3059
|
+
}
|
|
2784
3060
|
const s = [];
|
|
2785
3061
|
let lo = "0";
|
|
2786
3062
|
let hi = "n";
|
|
@@ -2801,14 +3077,17 @@ var Printer = class {
|
|
|
2801
3077
|
s.push(`const ${h} = Math.min(n, ${f} + Math.min(${base.to}, n) + 1);`);
|
|
2802
3078
|
hi = h;
|
|
2803
3079
|
} else if (base.t !== "all") throw new NotStandalone(`the axis "${base.t}"`);
|
|
3080
|
+
const startSafe = at.every((m) => m.t === "origin");
|
|
2804
3081
|
return {
|
|
2805
3082
|
s,
|
|
2806
3083
|
j,
|
|
2807
|
-
lo,
|
|
2808
|
-
|
|
3084
|
+
head: forHead(j, lo, hi, reverse),
|
|
3085
|
+
key: [],
|
|
3086
|
+
check: [],
|
|
2809
3087
|
skip,
|
|
2810
3088
|
fixed: f === void 0,
|
|
2811
3089
|
start,
|
|
3090
|
+
startSafe,
|
|
2812
3091
|
tests,
|
|
2813
3092
|
inner
|
|
2814
3093
|
};
|
|
@@ -2816,18 +3095,57 @@ var Printer = class {
|
|
|
2816
3095
|
#list(op, target, rest, ctx) {
|
|
2817
3096
|
const strict = this.#strict(op, rest);
|
|
2818
3097
|
const at = target.at ?? [];
|
|
2819
|
-
const { s, j,
|
|
3098
|
+
const { s, j, head, key, check, skip, fixed, start, startSafe, tests, inner } = this.#targetLoop(target.axis, at, ctx);
|
|
2820
3099
|
const { level } = inner;
|
|
2821
3100
|
const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
|
|
2822
3101
|
const body = this.#hoist(this.expr(target.body, inner), hasLoop(target.body));
|
|
2823
3102
|
if (op === "count" && fixed && tests.length === 0 && body.v.safe) return bare(plainValue("n", "number"));
|
|
2824
3103
|
const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== level));
|
|
3104
|
+
const r = this.#reduction(op, body.v.kind);
|
|
3105
|
+
const { v } = r;
|
|
3106
|
+
s.push(r.init);
|
|
3107
|
+
const item = [
|
|
3108
|
+
...check,
|
|
3109
|
+
...testCodes.flatMap((t) => [...t.s, `if (!${this.#boolean(t.v)}) continue;`]),
|
|
3110
|
+
...body.s
|
|
3111
|
+
];
|
|
3112
|
+
const safeItem = check.length === 0 && body.v.safe && testCodes.every((t) => this.#safeBoolean(t.v));
|
|
3113
|
+
const loop = [
|
|
3114
|
+
...key,
|
|
3115
|
+
...skip === void 0 ? [] : [`if (${j} === ${skip}) continue;`],
|
|
3116
|
+
...safeItem ? [...item, `const ${v} = ${body.v.code};`] : [
|
|
3117
|
+
`let ${v};`,
|
|
3118
|
+
"try {",
|
|
3119
|
+
...ind(item),
|
|
3120
|
+
` ${v} = ${body.v.code};`,
|
|
3121
|
+
"} catch {",
|
|
3122
|
+
strict ? ` ${this.use("F")}();` : " continue;",
|
|
3123
|
+
"}"
|
|
3124
|
+
],
|
|
3125
|
+
r.step
|
|
3126
|
+
];
|
|
3127
|
+
s.push(head, ...ind(loop), "}");
|
|
3128
|
+
const safe = startSafe && !strict && (op === "count" || BOOLEAN_LIST_OPS.has(op) && body.v.kind === "boolean");
|
|
3129
|
+
return {
|
|
3130
|
+
s,
|
|
3131
|
+
v: {
|
|
3132
|
+
code: r.result,
|
|
3133
|
+
kind: listKind(op, body.v.kind),
|
|
3134
|
+
safe,
|
|
3135
|
+
deps
|
|
3136
|
+
}
|
|
3137
|
+
};
|
|
3138
|
+
}
|
|
3139
|
+
/**
|
|
3140
|
+
* This method gives the parts of a list op that reduces the values of its items. The parts are the variable `v` of
|
|
3141
|
+
* the value of an item, the first statement, the step and the result. A value of the wrong kind is an error of the op.
|
|
3142
|
+
*/
|
|
3143
|
+
#reduction(op, kind) {
|
|
2825
3144
|
const a = this.fresh("a");
|
|
2826
3145
|
const n = this.fresh("n");
|
|
2827
3146
|
const v = this.fresh("x");
|
|
2828
|
-
const num =
|
|
2829
|
-
const bool =
|
|
2830
|
-
const F = this.use("F");
|
|
3147
|
+
const num = kind === "number" ? v : `${this.use("U")}(${v})`;
|
|
3148
|
+
const bool = kind === "boolean" ? v : `${this.use("B")}(${v})`;
|
|
2831
3149
|
const inits = {
|
|
2832
3150
|
count: `let ${n} = 0;`,
|
|
2833
3151
|
sum: `let ${a} = 0;`,
|
|
@@ -2836,7 +3154,6 @@ var Printer = class {
|
|
|
2836
3154
|
none: `let ${a} = false;`,
|
|
2837
3155
|
all: `let ${a} = true;`
|
|
2838
3156
|
};
|
|
2839
|
-
s.push(inits[op] ?? `let ${a}, ${n} = 0;`);
|
|
2840
3157
|
const steps = {
|
|
2841
3158
|
count: `${n}++;`,
|
|
2842
3159
|
sum: `${a} += ${num};`,
|
|
@@ -2848,37 +3165,128 @@ var Printer = class {
|
|
|
2848
3165
|
all: `if (!${bool}) ${a} = false;`,
|
|
2849
3166
|
first: `if (${n}++ === 0) ${a} = ${v};`
|
|
2850
3167
|
};
|
|
2851
|
-
const
|
|
2852
|
-
const
|
|
3168
|
+
const F = this.use("F");
|
|
3169
|
+
const results = {
|
|
3170
|
+
count: n,
|
|
3171
|
+
sum: this.#checked("number", a),
|
|
3172
|
+
mean: `(${n} === 0 ? ${F}() : ${this.#checked("number", `${a} / ${n}`)})`,
|
|
3173
|
+
any: a,
|
|
3174
|
+
all: a,
|
|
3175
|
+
none: `!${a}`
|
|
3176
|
+
};
|
|
3177
|
+
return {
|
|
3178
|
+
v,
|
|
3179
|
+
init: inits[op] ?? `let ${a}, ${n} = 0;`,
|
|
3180
|
+
step: steps[op],
|
|
3181
|
+
result: results[op] ?? `(${n} === 0 ? ${F}() : ${a})`
|
|
3182
|
+
};
|
|
3183
|
+
}
|
|
3184
|
+
/**
|
|
3185
|
+
* This method prints a list as an array (LIST.VALUE): a pair of the key and the value of each item. A test that
|
|
3186
|
+
* fails, a body that fails and an error item of the axis list give the error sentinel as the value.
|
|
3187
|
+
*/
|
|
3188
|
+
#eachList(e, ctx) {
|
|
3189
|
+
const at = e.at ?? [];
|
|
3190
|
+
const { s, j, head, key, check, skip, start, startSafe, tests, inner } = this.#targetLoop(e.axis, at, ctx);
|
|
3191
|
+
const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
|
|
3192
|
+
const body = this.#hoist(this.expr(e.body, inner), hasLoop(e.body));
|
|
3193
|
+
const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== inner.level));
|
|
3194
|
+
const list = this.fresh("L");
|
|
3195
|
+
const v = this.fresh("x");
|
|
3196
|
+
const item = [
|
|
3197
|
+
...check,
|
|
3198
|
+
...testCodes.flatMap((t) => [...t.s, `if (!${this.#boolean(t.v)}) continue;`]),
|
|
3199
|
+
...body.s
|
|
3200
|
+
];
|
|
3201
|
+
const safeItem = check.length === 0 && body.v.safe && testCodes.every((t) => this.#safeBoolean(t.v));
|
|
2853
3202
|
const loop = [
|
|
3203
|
+
...key,
|
|
2854
3204
|
...skip === void 0 ? [] : [`if (${j} === ${skip}) continue;`],
|
|
2855
|
-
...safeItem ? [...item,
|
|
3205
|
+
...safeItem ? [...item, `${list}.push([${j}, ${body.v.code}]);`] : [
|
|
2856
3206
|
`let ${v};`,
|
|
2857
3207
|
"try {",
|
|
2858
3208
|
...ind(item),
|
|
2859
3209
|
` ${v} = ${body.v.code};`,
|
|
2860
3210
|
"} catch {",
|
|
2861
|
-
|
|
3211
|
+
` ${v} = E;`,
|
|
3212
|
+
"}",
|
|
3213
|
+
`${list}.push([${j}, ${v}]);`
|
|
3214
|
+
]
|
|
3215
|
+
];
|
|
3216
|
+
return {
|
|
3217
|
+
s: [
|
|
3218
|
+
...s,
|
|
3219
|
+
`const ${list} = [];`,
|
|
3220
|
+
head,
|
|
3221
|
+
...ind(loop),
|
|
2862
3222
|
"}"
|
|
2863
3223
|
],
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
3224
|
+
v: {
|
|
3225
|
+
code: list,
|
|
3226
|
+
kind: void 0,
|
|
3227
|
+
safe: startSafe,
|
|
3228
|
+
deps,
|
|
3229
|
+
list: { item: body.v.kind }
|
|
3230
|
+
}
|
|
3231
|
+
};
|
|
3232
|
+
}
|
|
3233
|
+
/**
|
|
3234
|
+
* This method prints sortBy and take (LIST.SORT, LIST.TAKE): a new array from the array of a list. An error item is
|
|
3235
|
+
* not in the new list, and with strict it is the error of the op.
|
|
3236
|
+
*/
|
|
3237
|
+
#order(op, head, rest, ctx) {
|
|
3238
|
+
const list = this.#listOf(head, ctx);
|
|
3239
|
+
const exprs = [head, ...op === "take" ? [rest[0] ?? lit(null)] : []];
|
|
3240
|
+
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
3241
|
+
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
3242
|
+
const deps = depsOf(...raw);
|
|
3243
|
+
const parts = this.#settle(raw, deps, exprs);
|
|
3244
|
+
const item = list.v.list.item;
|
|
2876
3245
|
return {
|
|
2877
|
-
s,
|
|
3246
|
+
s: stmtsOf(...parts),
|
|
2878
3247
|
v: {
|
|
2879
|
-
code,
|
|
2880
|
-
kind:
|
|
2881
|
-
safe,
|
|
3248
|
+
code: `${this.use(op === "sortBy" ? "SB" : "TK")}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
|
|
3249
|
+
kind: void 0,
|
|
3250
|
+
safe: false,
|
|
3251
|
+
deps,
|
|
3252
|
+
list: { item }
|
|
3253
|
+
}
|
|
3254
|
+
};
|
|
3255
|
+
}
|
|
3256
|
+
/** This method prints a list op over a list that is not an each: a loop over its array, and nth (LIST.NTH). */
|
|
3257
|
+
#overList(op, head, rest, ctx) {
|
|
3258
|
+
const list = this.#listOf(head, ctx);
|
|
3259
|
+
const exprs = [head, ...op === "nth" ? [rest[0] ?? lit(null)] : []];
|
|
3260
|
+
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
3261
|
+
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
3262
|
+
const deps = depsOf(...raw);
|
|
3263
|
+
const parts = this.#settle(raw, deps, exprs);
|
|
3264
|
+
const [l] = parts;
|
|
3265
|
+
const item = list.v.list.item;
|
|
3266
|
+
if (op === "nth") return {
|
|
3267
|
+
s: stmtsOf(...parts),
|
|
3268
|
+
v: {
|
|
3269
|
+
code: `${this.use("NT")}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
|
|
3270
|
+
kind: item,
|
|
3271
|
+
safe: false,
|
|
3272
|
+
deps
|
|
3273
|
+
}
|
|
3274
|
+
};
|
|
3275
|
+
const r = this.#reduction(op, item);
|
|
3276
|
+
const { v } = r;
|
|
3277
|
+
return {
|
|
3278
|
+
s: [
|
|
3279
|
+
...l.s,
|
|
3280
|
+
r.init,
|
|
3281
|
+
`for (const [, ${v}] of ${l.v.code}) {`,
|
|
3282
|
+
` if (${v} === E) ${strict ? `${this.use("F")}();` : "continue;"}`,
|
|
3283
|
+
` ${r.step}`,
|
|
3284
|
+
"}"
|
|
3285
|
+
],
|
|
3286
|
+
v: {
|
|
3287
|
+
code: r.result,
|
|
3288
|
+
kind: listKind(op, item),
|
|
3289
|
+
safe: false,
|
|
2882
3290
|
deps
|
|
2883
3291
|
}
|
|
2884
3292
|
};
|
|
@@ -2994,7 +3402,9 @@ var Printer = class {
|
|
|
2994
3402
|
env: names.env
|
|
2995
3403
|
});
|
|
2996
3404
|
const body = [
|
|
3405
|
+
...t.key,
|
|
2997
3406
|
...t.skip === void 0 ? [] : [`if (${t.j} === ${t.skip}) continue;`],
|
|
3407
|
+
...t.check,
|
|
2998
3408
|
...tests.flatMap((c) => [...c.s, `if (!${this.#boolean(c.v)}) continue;`]),
|
|
2999
3409
|
...step.s
|
|
3000
3410
|
];
|
|
@@ -3005,12 +3415,11 @@ var Printer = class {
|
|
|
3005
3415
|
...inside,
|
|
3006
3416
|
...[...result.v.deps].filter((d) => d !== loop)
|
|
3007
3417
|
]);
|
|
3008
|
-
const head = `for (let ${t.j} = ${t.lo}; ${t.j} < ${t.hi}; ${t.j}++) {`;
|
|
3009
3418
|
return {
|
|
3010
3419
|
s: [
|
|
3011
3420
|
...names.s,
|
|
3012
3421
|
...t.s,
|
|
3013
|
-
head,
|
|
3422
|
+
t.head,
|
|
3014
3423
|
...ind(body),
|
|
3015
3424
|
"}",
|
|
3016
3425
|
...result.s
|
|
@@ -3048,7 +3457,9 @@ var Printer = class {
|
|
|
3048
3457
|
origin: void 0,
|
|
3049
3458
|
focus: void 0,
|
|
3050
3459
|
level: 1,
|
|
3460
|
+
originLevel: 1,
|
|
3051
3461
|
env: /* @__PURE__ */ new Map(),
|
|
3462
|
+
fns: /* @__PURE__ */ new Map(),
|
|
3052
3463
|
keys: names
|
|
3053
3464
|
};
|
|
3054
3465
|
if (varying.length === 0) {
|
|
@@ -3073,6 +3484,7 @@ var Printer = class {
|
|
|
3073
3484
|
} else {
|
|
3074
3485
|
const body = this.expr(e, bodyCtx);
|
|
3075
3486
|
this.#kinds.set(fn, body.v.kind);
|
|
3487
|
+
this.#lists.set(fn, body.v.list);
|
|
3076
3488
|
this.run.push(`const ${fn} = ${this.use(params.length === 1 ? "M1" : "M")}((${params.join(", ")}) => {`, ...ind(body.s), ` return ${body.v.code};`, "});");
|
|
3077
3489
|
}
|
|
3078
3490
|
}
|
|
@@ -3092,7 +3504,8 @@ var Printer = class {
|
|
|
3092
3504
|
code: `${fn}(${args.join(", ")})`,
|
|
3093
3505
|
kind: this.#kinds.get(fn),
|
|
3094
3506
|
safe: false,
|
|
3095
|
-
deps: depsOf(...values)
|
|
3507
|
+
deps: depsOf(...values),
|
|
3508
|
+
list: this.#lists.get(fn)
|
|
3096
3509
|
}
|
|
3097
3510
|
};
|
|
3098
3511
|
}
|
|
@@ -3205,7 +3618,9 @@ var Printer = class {
|
|
|
3205
3618
|
origin: "o",
|
|
3206
3619
|
focus: "o",
|
|
3207
3620
|
level: 1,
|
|
3621
|
+
originLevel: 1,
|
|
3208
3622
|
env: /* @__PURE__ */ new Map(),
|
|
3623
|
+
fns: /* @__PURE__ */ new Map(),
|
|
3209
3624
|
keys: /* @__PURE__ */ new Map()
|
|
3210
3625
|
}));
|
|
3211
3626
|
for (const [name, form] of this.#forms) if (form !== void 0 && !this.#called.has(name)) this.top.delete(name);
|
|
@@ -3219,7 +3634,13 @@ var Printer = class {
|
|
|
3219
3634
|
return canonical(e, (x) => {
|
|
3220
3635
|
const key = ctx.keys.get(x);
|
|
3221
3636
|
if (key !== void 0) return { "@": key.code };
|
|
3222
|
-
|
|
3637
|
+
const node = x;
|
|
3638
|
+
if (node?.tag === "var") return { $: ctx.env.get(node.name)?.code ?? node.name };
|
|
3639
|
+
return node?.tag === "call" ? {
|
|
3640
|
+
"()": node.fn,
|
|
3641
|
+
of: ctx.fns.get(node.fn)?.id ?? null,
|
|
3642
|
+
args: node.args
|
|
3643
|
+
} : void 0;
|
|
3223
3644
|
});
|
|
3224
3645
|
}
|
|
3225
3646
|
};
|