@mark1russell7/vex-compiler 0.7.0 → 0.8.1
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 +564 -69
- 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 +438 -53
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,13 @@ 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}++) {`;
|
|
2062
|
+
/** This function tells if an expression has a loop in a loop: an axis, a fold or an iteration in another one. */
|
|
2063
|
+
function nestsLoops(e, inLoop = false) {
|
|
2064
|
+
const loop = e.tag === "each" || e.tag === "fold" || e.tag === "iterate";
|
|
2065
|
+
return loop && inLoop || children(e).some((x) => nestsLoops(x, inLoop || loop));
|
|
2066
|
+
}
|
|
1976
2067
|
/** This function tells if an expression has a loop of names: a fold or an iteration. */
|
|
1977
2068
|
function hasLoop(e) {
|
|
1978
2069
|
let found = LOOPS.get(e);
|
|
@@ -2192,6 +2283,9 @@ var Printer = class {
|
|
|
2192
2283
|
/** The parts of the helper S that the nodes of the memo that keep their items need (helperS). */
|
|
2193
2284
|
features = /* @__PURE__ */ new Set();
|
|
2194
2285
|
#names = 0;
|
|
2286
|
+
/** The number of the next bound function, and of the next printed function. */
|
|
2287
|
+
#fnIds = 0;
|
|
2288
|
+
#printed = 0;
|
|
2195
2289
|
/** 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
2290
|
#loops = 1e6;
|
|
2197
2291
|
/** The node of the memo whose function the printer prints, so it prints the node itself. */
|
|
@@ -2208,8 +2302,20 @@ var Printer = class {
|
|
|
2208
2302
|
#constants = /* @__PURE__ */ new Map();
|
|
2209
2303
|
/** The columns of the run, by the structure of their expression. */
|
|
2210
2304
|
#columns = /* @__PURE__ */ new Map();
|
|
2305
|
+
/** The expressions whose code is the read of a column of the run. */
|
|
2306
|
+
#columned = /* @__PURE__ */ new WeakSet();
|
|
2307
|
+
/** The lazy constants of the run, and the printed functions that have a loop: code that reads one waits for its read. */
|
|
2308
|
+
#deferred = /* @__PURE__ */ new Set();
|
|
2309
|
+
/** The functions that the printer prints at this time, the innermost last. */
|
|
2310
|
+
#scopes = [];
|
|
2311
|
+
/** The work of the body of each bound function, by the number of its binding. */
|
|
2312
|
+
#fnWork = /* @__PURE__ */ new Map();
|
|
2211
2313
|
/** The expressions whose code is a constant of the run. */
|
|
2212
2314
|
#constant = /* @__PURE__ */ new WeakSet();
|
|
2315
|
+
/** The expressions at a place that takes a list: a binding, the head of a list op and the list of an axis. */
|
|
2316
|
+
#wantList = /* @__PURE__ */ new WeakSet();
|
|
2317
|
+
/** The facts of the list of each function of the memo whose value is a list. */
|
|
2318
|
+
#lists = /* @__PURE__ */ new Map();
|
|
2213
2319
|
constructor(expr, options) {
|
|
2214
2320
|
this.#domains = options.domains ?? [];
|
|
2215
2321
|
this.#facts = factsOf({ domains: this.#domains });
|
|
@@ -2288,12 +2394,17 @@ var Printer = class {
|
|
|
2288
2394
|
fresh(prefix) {
|
|
2289
2395
|
return `${prefix}${this.#names++}`;
|
|
2290
2396
|
}
|
|
2397
|
+
/** This method tells if code reads a lazy constant of the run, or calls a printed function that has a loop. */
|
|
2398
|
+
#defers(c) {
|
|
2399
|
+
return [c.v.code, ...c.s].some((line) => [...line.matchAll(/\b(c\d+|fn\d+)\(/g)].some((m) => this.#deferred.has(m[1])));
|
|
2400
|
+
}
|
|
2291
2401
|
/** This method gives the code of a name: a read that throws the error of its value, if it can hold an error. */
|
|
2292
2402
|
#read(name) {
|
|
2293
2403
|
const base = {
|
|
2294
2404
|
kind: name.kind,
|
|
2295
2405
|
deps: name.deps,
|
|
2296
|
-
plain: true
|
|
2406
|
+
plain: true,
|
|
2407
|
+
list: name.list
|
|
2297
2408
|
};
|
|
2298
2409
|
return name.lazy ? {
|
|
2299
2410
|
...base,
|
|
@@ -2312,7 +2423,8 @@ var Printer = class {
|
|
|
2312
2423
|
code: v.held ?? v.code,
|
|
2313
2424
|
kind: v.kind,
|
|
2314
2425
|
lazy: v.held !== void 0,
|
|
2315
|
-
deps: NO_DEPS
|
|
2426
|
+
deps: NO_DEPS,
|
|
2427
|
+
list: v.list
|
|
2316
2428
|
};
|
|
2317
2429
|
}
|
|
2318
2430
|
/**
|
|
@@ -2335,7 +2447,8 @@ var Printer = class {
|
|
|
2335
2447
|
}
|
|
2336
2448
|
/**
|
|
2337
2449
|
* This method evaluates a constant of the run once, before the loop over the keys, and gives the read of its value. A
|
|
2338
|
-
* constant with a loop (`lazy`) evaluates on its first read instead, as a branch of the interpreter does.
|
|
2450
|
+
* constant with a loop (`lazy`) evaluates on its first read instead, as a branch of the interpreter does. So does a
|
|
2451
|
+
* constant that reads a lazy constant or calls a function with a loop. Else it starts that loop before the run.
|
|
2339
2452
|
*/
|
|
2340
2453
|
#hoist(c, lazy = false) {
|
|
2341
2454
|
if (c.v.deps.size > 0 || c.v.plain === true && c.s.length === 0) return c;
|
|
@@ -2343,18 +2456,24 @@ var Printer = class {
|
|
|
2343
2456
|
const hit = key === void 0 ? void 0 : this.#constants.get(key);
|
|
2344
2457
|
if (hit !== void 0) return hit;
|
|
2345
2458
|
const name = this.fresh("c");
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2459
|
+
const deferred = lazy || this.#defers(c);
|
|
2460
|
+
if (deferred) {
|
|
2461
|
+
this.#deferred.add(name);
|
|
2462
|
+
this.run.push(`const ${name} = ${this.use("L")}(() => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
|
|
2463
|
+
} else this.run.push(...this.#store(name, c, true));
|
|
2464
|
+
const { list } = c.v;
|
|
2465
|
+
const read = bare(this.#read(deferred ? {
|
|
2349
2466
|
code: `${name}()`,
|
|
2350
2467
|
kind: c.v.kind,
|
|
2351
2468
|
lazy: true,
|
|
2352
|
-
deps: NO_DEPS
|
|
2469
|
+
deps: NO_DEPS,
|
|
2470
|
+
list
|
|
2353
2471
|
} : {
|
|
2354
2472
|
code: name,
|
|
2355
2473
|
kind: c.v.kind,
|
|
2356
2474
|
lazy: !c.v.safe,
|
|
2357
|
-
deps: NO_DEPS
|
|
2475
|
+
deps: NO_DEPS,
|
|
2476
|
+
list
|
|
2358
2477
|
}));
|
|
2359
2478
|
if (key !== void 0) this.#constants.set(key, read);
|
|
2360
2479
|
return read;
|
|
@@ -2375,26 +2494,81 @@ var Printer = class {
|
|
|
2375
2494
|
this.#keyOf.set(c, this.#constKey(e, ctx));
|
|
2376
2495
|
this.#constant.add(e);
|
|
2377
2496
|
}
|
|
2497
|
+
if (c.v.list !== void 0 && !this.#wantList.has(e)) throw new NotStandalone("a list is a value of the program");
|
|
2378
2498
|
return this.#columnar(e, c, ctx) ? this.#column(e, c, ctx) : c;
|
|
2379
2499
|
}
|
|
2500
|
+
/** This method gives the code of a list at a place that takes a list (LIST.VALUE): an each, a name, sortBy or take. */
|
|
2501
|
+
#listOf(e, ctx) {
|
|
2502
|
+
this.#wantList.add(e);
|
|
2503
|
+
const c = this.expr(e, ctx);
|
|
2504
|
+
if (c.v.list === void 0) throw new NotStandalone("a list op on a value that is not a list");
|
|
2505
|
+
return c;
|
|
2506
|
+
}
|
|
2507
|
+
/** 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. */
|
|
2508
|
+
#isList(e, ctx) {
|
|
2509
|
+
if (e.tag === "each") return true;
|
|
2510
|
+
if (e.tag === "app") return e.op === "sortBy" || e.op === "take";
|
|
2511
|
+
return e.tag === "var" && ctx.env.get(e.name)?.list !== void 0;
|
|
2512
|
+
}
|
|
2380
2513
|
/**
|
|
2381
2514
|
* 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
2515
|
* target and on the constants of the run, so each run computes it once for each record. Each name that it reads
|
|
2383
2516
|
* from outside is a constant of the run, so its code can be outside the loop.
|
|
2384
2517
|
*/
|
|
2385
2518
|
#columnar(e, c, ctx) {
|
|
2386
|
-
if (ctx.level < 2 ||
|
|
2519
|
+
if (ctx.level < 2 || !c.v.deps.has(ctx.level) || this.#work(e, ctx.fns) < COLUMN_WORK) return false;
|
|
2520
|
+
const scope = this.#callScope(c, ctx);
|
|
2521
|
+
if (c.v.deps.size !== 1 && scope === void 0) return false;
|
|
2387
2522
|
if (this.#keyed(e)) return false;
|
|
2388
|
-
|
|
2523
|
+
const visible = (name) => {
|
|
2524
|
+
const bound = ctx.env.get(name);
|
|
2525
|
+
return (bound?.deps.size ?? 0) === 0 || (scope?.names.has(bound.code) ?? false);
|
|
2526
|
+
};
|
|
2527
|
+
return [...namesOf(e)].every(visible) && !this.#callsLocal(e, ctx);
|
|
2528
|
+
}
|
|
2529
|
+
/**
|
|
2530
|
+
* This method gives the function whose call can hold a column of a part. The part is in a loop of the function, and it
|
|
2531
|
+
* reads a parameter. The body of the function has a loop in a loop, so a call can read the part more than once. Each
|
|
2532
|
+
* name that the part reads is a parameter, a binding at the root of the body or a constant (`#columnar`). So the
|
|
2533
|
+
* part depends only on its target and on values that one call does not change. These are the parameters, the focus
|
|
2534
|
+
* and the origin. The levels in a node of the memo start at 1, far below the focus of a function. So a node of the
|
|
2535
|
+
* memo holds no column of a call.
|
|
2536
|
+
*/
|
|
2537
|
+
#callScope(c, ctx) {
|
|
2538
|
+
const scope = this.#scopes.at(-1);
|
|
2539
|
+
return scope !== void 0 && scope.repeats && ctx.level > scope.focus && c.v.deps.has(scope.param) ? scope : void 0;
|
|
2540
|
+
}
|
|
2541
|
+
/** This method tells if an expression calls a function that is at the place of its binding, not at the start of a run. */
|
|
2542
|
+
#callsLocal(e, ctx) {
|
|
2543
|
+
if (e.tag === "call" && ctx.fns.get(e.fn)?.local === true) return true;
|
|
2544
|
+
return children(e).some((x) => this.#callsLocal(x, ctx));
|
|
2389
2545
|
}
|
|
2390
2546
|
/** This method tells if an expression has a node of the memo with keys in it. */
|
|
2391
2547
|
#keyed(e) {
|
|
2392
2548
|
return (this.#memo.get(e)?.length ?? 0) > 0 || children(e).some((x) => this.#keyed(x));
|
|
2393
2549
|
}
|
|
2394
|
-
/**
|
|
2395
|
-
|
|
2550
|
+
/**
|
|
2551
|
+
* This method gives the work of an expression: a node of the memo and a constant of the run are one read each. A call
|
|
2552
|
+
* adds the work of the body of its function, because the body evaluates at each call.
|
|
2553
|
+
*/
|
|
2554
|
+
#work(e, fns) {
|
|
2396
2555
|
if (this.#memo.has(e) || this.#constant.has(e)) return 1;
|
|
2397
|
-
|
|
2556
|
+
const body = e.tag === "call" ? this.#bodyWork(fns.get(e.fn)) : 0;
|
|
2557
|
+
return children(e).reduce((sum, x) => sum + this.#work(x, fns), 1 + body);
|
|
2558
|
+
}
|
|
2559
|
+
/**
|
|
2560
|
+
* This method gives the work of the body of a function, once for each function. Thus a chain of calls costs no more
|
|
2561
|
+
* than its functions. A body that is a column of the run is one read. A body sees the functions of the place of its
|
|
2562
|
+
* binding, and no function calls itself (FN.TOTAL).
|
|
2563
|
+
*/
|
|
2564
|
+
#bodyWork(bound) {
|
|
2565
|
+
if (bound === void 0) return 0;
|
|
2566
|
+
let work = this.#fnWork.get(bound.id);
|
|
2567
|
+
if (work === void 0) {
|
|
2568
|
+
work = this.#columned.has(bound.fn.body) ? 1 : this.#work(bound.fn.body, bound.ctx.fns);
|
|
2569
|
+
this.#fnWork.set(bound.id, work);
|
|
2570
|
+
}
|
|
2571
|
+
return work;
|
|
2398
2572
|
}
|
|
2399
2573
|
/** This method gives the read of a column of the run. Two parts with the same structure share one column. */
|
|
2400
2574
|
#column(e, c, ctx) {
|
|
@@ -2402,14 +2576,16 @@ var Printer = class {
|
|
|
2402
2576
|
let name = this.#columns.get(key);
|
|
2403
2577
|
if (name === void 0) {
|
|
2404
2578
|
name = this.fresh("col");
|
|
2405
|
-
this.run.push(`const ${name} = ${this.use("Q")}((${ctx.focus}) => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
|
|
2579
|
+
(this.#callScope(c, ctx)?.decls ?? this.run).push(`const ${name} = ${this.use("Q")}((${ctx.focus}) => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
|
|
2406
2580
|
this.#columns.set(key, name);
|
|
2407
2581
|
}
|
|
2582
|
+
this.#columned.add(e);
|
|
2408
2583
|
return bare({
|
|
2409
2584
|
code: `${this.use("G")}(${name}(${ctx.focus}))`,
|
|
2410
2585
|
kind: c.v.kind,
|
|
2411
2586
|
safe: false,
|
|
2412
|
-
deps: c.v.deps
|
|
2587
|
+
deps: c.v.deps,
|
|
2588
|
+
list: c.v.list
|
|
2413
2589
|
});
|
|
2414
2590
|
}
|
|
2415
2591
|
#node(e, ctx) {
|
|
@@ -2418,6 +2594,9 @@ var Printer = class {
|
|
|
2418
2594
|
case "ref": return this.#ref(e, ctx);
|
|
2419
2595
|
case "var": return bare(this.#var(e.name, ctx));
|
|
2420
2596
|
case "let": return this.#let(e, ctx);
|
|
2597
|
+
/* v8 ignore next -- @preserve */
|
|
2598
|
+
case "fn": throw new NotStandalone("a function outside the bindings of a let");
|
|
2599
|
+
case "call": return this.#call(e, ctx);
|
|
2421
2600
|
case "rec": {
|
|
2422
2601
|
const names = Object.keys(e.fields);
|
|
2423
2602
|
const raw = Object.values(e.fields).map((x) => this.expr(x, ctx));
|
|
@@ -2435,7 +2614,7 @@ var Printer = class {
|
|
|
2435
2614
|
};
|
|
2436
2615
|
}
|
|
2437
2616
|
case "app": return this.#app(e, ctx);
|
|
2438
|
-
case "each":
|
|
2617
|
+
case "each": return this.#eachList(e, ctx);
|
|
2439
2618
|
case "fold": return this.#fold(e, ctx);
|
|
2440
2619
|
case "iterate": return this.#iterate(e, ctx);
|
|
2441
2620
|
case "ext": throw new NotStandalone(`the extension "${e.kind}"`);
|
|
@@ -2521,7 +2700,7 @@ var Printer = class {
|
|
|
2521
2700
|
for (const m of at) {
|
|
2522
2701
|
if (fails) for (const d of deps) kept.add(d);
|
|
2523
2702
|
if (moveExpr(m) !== void 0) deps = values[next++].v.deps;
|
|
2524
|
-
else if (m.t === "origin") deps = /* @__PURE__ */ new Set([
|
|
2703
|
+
else if (m.t === "origin") deps = /* @__PURE__ */ new Set([ctx.originLevel]);
|
|
2525
2704
|
else if (m.t === "key" || m.t === "index" || m.t === "parent") deps = NO_DEPS;
|
|
2526
2705
|
fails = m.t !== "origin";
|
|
2527
2706
|
}
|
|
@@ -2530,6 +2709,7 @@ var Printer = class {
|
|
|
2530
2709
|
#var(name, ctx) {
|
|
2531
2710
|
const bound = ctx.env.get(name);
|
|
2532
2711
|
if (bound !== void 0) return this.#read(bound);
|
|
2712
|
+
if (ctx.fns.has(name)) throw new NotStandalone(`the name "${name}" is a function, not a value`);
|
|
2533
2713
|
const kind = this.#schema.vars?.[name];
|
|
2534
2714
|
if (kind === void 0) throw new NotStandalone(`a variable that is not in the schema: "${name}"`);
|
|
2535
2715
|
let code = this.vars.get(name);
|
|
@@ -2545,9 +2725,28 @@ var Printer = class {
|
|
|
2545
2725
|
*/
|
|
2546
2726
|
#let(e, ctx) {
|
|
2547
2727
|
const env = new Map(ctx.env);
|
|
2728
|
+
const fns = new Map(ctx.fns);
|
|
2548
2729
|
const s = [];
|
|
2549
2730
|
let deps = /* @__PURE__ */ new Set();
|
|
2731
|
+
const bound = [];
|
|
2732
|
+
const scope = this.#scopes.at(-1);
|
|
2733
|
+
const root = scope !== void 0 && scope.root === e ? scope : void 0;
|
|
2550
2734
|
for (const [name, x] of Object.entries(e.bind)) {
|
|
2735
|
+
if (x.tag === "fn") {
|
|
2736
|
+
const b = {
|
|
2737
|
+
id: this.#fnIds++,
|
|
2738
|
+
fn: x,
|
|
2739
|
+
ctx,
|
|
2740
|
+
specs: /* @__PURE__ */ new Map(),
|
|
2741
|
+
decls: [],
|
|
2742
|
+
local: false
|
|
2743
|
+
};
|
|
2744
|
+
bound.push(b);
|
|
2745
|
+
fns.set(name, b);
|
|
2746
|
+
env.delete(name);
|
|
2747
|
+
continue;
|
|
2748
|
+
}
|
|
2749
|
+
this.#wantList.add(x);
|
|
2551
2750
|
const c = this.#hoist(this.expr(x, ctx), hasLoop(x));
|
|
2552
2751
|
if (c.v.deps.size === 0) {
|
|
2553
2752
|
env.set(name, this.#nameOf(c.v));
|
|
@@ -2559,22 +2758,147 @@ var Printer = class {
|
|
|
2559
2758
|
code: b,
|
|
2560
2759
|
kind: c.v.kind,
|
|
2561
2760
|
lazy: !c.v.safe,
|
|
2562
|
-
deps: c.v.deps
|
|
2761
|
+
deps: c.v.deps,
|
|
2762
|
+
list: c.v.list
|
|
2563
2763
|
});
|
|
2764
|
+
root?.names.add(b);
|
|
2564
2765
|
deps = /* @__PURE__ */ new Set([...deps, ...c.v.deps]);
|
|
2565
2766
|
}
|
|
2767
|
+
if (root !== void 0) root.root = e.body;
|
|
2566
2768
|
const body = this.expr(e.body, {
|
|
2567
2769
|
...ctx,
|
|
2568
|
-
env
|
|
2770
|
+
env,
|
|
2771
|
+
fns
|
|
2569
2772
|
});
|
|
2570
2773
|
return {
|
|
2571
|
-
s: [
|
|
2774
|
+
s: [
|
|
2775
|
+
...s,
|
|
2776
|
+
...bound.flatMap((b) => b.decls),
|
|
2777
|
+
...body.s
|
|
2778
|
+
],
|
|
2572
2779
|
v: {
|
|
2573
2780
|
...body.v,
|
|
2574
2781
|
deps: /* @__PURE__ */ new Set([...deps, ...body.v.deps])
|
|
2575
2782
|
}
|
|
2576
2783
|
};
|
|
2577
2784
|
}
|
|
2785
|
+
/**
|
|
2786
|
+
* This method prints a call (FN.CALL). Each argument evaluates at the place of the call, as a binding of a let. The
|
|
2787
|
+
* call goes to the printed function of the signature of its arguments, with the focus and the origin if it reads them.
|
|
2788
|
+
*/
|
|
2789
|
+
#call(e, ctx) {
|
|
2790
|
+
const bound = ctx.env.has(e.fn) ? void 0 : ctx.fns.get(e.fn);
|
|
2791
|
+
if (bound === void 0) throw new NotStandalone(`a call of "${e.fn}", which is not a function`);
|
|
2792
|
+
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)}`);
|
|
2793
|
+
const s = [];
|
|
2794
|
+
const args = e.args.map((x) => {
|
|
2795
|
+
const c = this.#hoist(this.expr(x, ctx), hasLoop(x));
|
|
2796
|
+
if (c.v.safe) {
|
|
2797
|
+
s.push(...c.s);
|
|
2798
|
+
return {
|
|
2799
|
+
code: c.v.code,
|
|
2800
|
+
kind: c.v.kind,
|
|
2801
|
+
deps: c.v.deps
|
|
2802
|
+
};
|
|
2803
|
+
}
|
|
2804
|
+
if (c.s.length === 0 && c.v.held !== void 0) return {
|
|
2805
|
+
code: c.v.held,
|
|
2806
|
+
kind: c.v.kind,
|
|
2807
|
+
deps: c.v.deps
|
|
2808
|
+
};
|
|
2809
|
+
const b = this.fresh("b");
|
|
2810
|
+
s.push(...this.#store(b, c));
|
|
2811
|
+
return {
|
|
2812
|
+
code: b,
|
|
2813
|
+
kind: c.v.kind,
|
|
2814
|
+
deps: c.v.deps
|
|
2815
|
+
};
|
|
2816
|
+
});
|
|
2817
|
+
const sig = JSON.stringify(args.map((a) => a.kind ?? null));
|
|
2818
|
+
const spec = bound.specs.get(sig) ?? this.#printFn(bound, args, sig);
|
|
2819
|
+
const levels = (d) => d === spec.origin ? [ctx.originLevel] : d === spec.focus ? [ctx.level] : d === spec.param ? [] : [d];
|
|
2820
|
+
const deps = /* @__PURE__ */ new Set([...args.flatMap((a) => [...a.deps]), ...[...spec.deps].flatMap(levels)]);
|
|
2821
|
+
const position = spec.deps.has(spec.origin) ? [ctx.focus, ctx.origin] : spec.deps.has(spec.focus) ? [ctx.focus] : [];
|
|
2822
|
+
return {
|
|
2823
|
+
s,
|
|
2824
|
+
v: {
|
|
2825
|
+
code: `${spec.name}(${[...args.map((a) => a.code), ...position].join(", ")})`,
|
|
2826
|
+
kind: spec.kind,
|
|
2827
|
+
safe: spec.safe,
|
|
2828
|
+
deps
|
|
2829
|
+
}
|
|
2830
|
+
};
|
|
2831
|
+
}
|
|
2832
|
+
/**
|
|
2833
|
+
* This method prints a bound function for one signature of its arguments, as one JavaScript function. Its parameters
|
|
2834
|
+
* are the arguments, then the focus and the origin, if the body reads them. A function that reads a name of the place
|
|
2835
|
+
* 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.
|
|
2836
|
+
*/
|
|
2837
|
+
#printFn(bound, args, sig) {
|
|
2838
|
+
const origin = 2e6 + 1e3 * this.#printed++;
|
|
2839
|
+
const focus = origin + 1;
|
|
2840
|
+
const param = -origin;
|
|
2841
|
+
const names = args.map(() => this.fresh("p"));
|
|
2842
|
+
const k = this.fresh("k");
|
|
2843
|
+
const o = this.fresh("o");
|
|
2844
|
+
const env = new Map(bound.ctx.env);
|
|
2845
|
+
bound.fn.params.forEach((p, i) => env.set(p, {
|
|
2846
|
+
code: names[i],
|
|
2847
|
+
kind: args[i].kind,
|
|
2848
|
+
lazy: true,
|
|
2849
|
+
deps: /* @__PURE__ */ new Set([param])
|
|
2850
|
+
}));
|
|
2851
|
+
const scope = {
|
|
2852
|
+
param,
|
|
2853
|
+
focus,
|
|
2854
|
+
repeats: nestsLoops(bound.fn.body),
|
|
2855
|
+
names: new Set(names),
|
|
2856
|
+
decls: [],
|
|
2857
|
+
root: bound.fn.body
|
|
2858
|
+
};
|
|
2859
|
+
this.#scopes.push(scope);
|
|
2860
|
+
const body = this.expr(bound.fn.body, {
|
|
2861
|
+
origin: o,
|
|
2862
|
+
focus: k,
|
|
2863
|
+
level: focus,
|
|
2864
|
+
originLevel: origin,
|
|
2865
|
+
env,
|
|
2866
|
+
fns: bound.ctx.fns,
|
|
2867
|
+
keys: /* @__PURE__ */ new Map()
|
|
2868
|
+
});
|
|
2869
|
+
this.#scopes.pop();
|
|
2870
|
+
const markers = /* @__PURE__ */ new Set([
|
|
2871
|
+
origin,
|
|
2872
|
+
focus,
|
|
2873
|
+
param
|
|
2874
|
+
]);
|
|
2875
|
+
const global = [...body.v.deps].every((d) => markers.has(d));
|
|
2876
|
+
const name = this.fresh("fn");
|
|
2877
|
+
if (hasLoop(bound.fn.body) || this.#defers(body)) this.#deferred.add(name);
|
|
2878
|
+
const position = body.v.deps.has(origin) ? [k, o] : body.v.deps.has(focus) ? [k] : [];
|
|
2879
|
+
const head = `const ${name} = (${[...names, ...position].join(", ")}) =>`;
|
|
2880
|
+
const s = [...scope.decls, ...body.s];
|
|
2881
|
+
const decl = s.length === 0 ? [`${head} ${body.v.code};`] : [
|
|
2882
|
+
`${head} {`,
|
|
2883
|
+
...ind(s),
|
|
2884
|
+
` return ${body.v.code};`,
|
|
2885
|
+
"};"
|
|
2886
|
+
];
|
|
2887
|
+
(global ? this.run : bound.decls).push(...decl);
|
|
2888
|
+
if (!global) bound.local = true;
|
|
2889
|
+
const spec = {
|
|
2890
|
+
name,
|
|
2891
|
+
kind: body.v.kind,
|
|
2892
|
+
safe: body.v.safe,
|
|
2893
|
+
deps: body.v.deps,
|
|
2894
|
+
origin,
|
|
2895
|
+
focus,
|
|
2896
|
+
param,
|
|
2897
|
+
global
|
|
2898
|
+
};
|
|
2899
|
+
bound.specs.set(sig, spec);
|
|
2900
|
+
return spec;
|
|
2901
|
+
}
|
|
2578
2902
|
#boolean(v) {
|
|
2579
2903
|
return v.kind === "boolean" ? v.code : `${this.use("B")}(${v.code})`;
|
|
2580
2904
|
}
|
|
@@ -2589,6 +2913,8 @@ var Printer = class {
|
|
|
2589
2913
|
if (op === "ifError") return this.#ifError(args, ctx);
|
|
2590
2914
|
const [head] = args;
|
|
2591
2915
|
if (LIST_OPS.has(op) && head?.tag === "each") return this.#list(op, head, args.slice(1), ctx);
|
|
2916
|
+
if (head !== void 0 && (op === "sortBy" || op === "take")) return this.#order(op, head, args.slice(1), ctx);
|
|
2917
|
+
if (head !== void 0 && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, args.slice(1), ctx);
|
|
2592
2918
|
const raw = args.map((x) => this.expr(x, ctx));
|
|
2593
2919
|
const deps = depsOf(...raw);
|
|
2594
2920
|
const values = this.#settle(raw, deps, args);
|
|
@@ -2774,13 +3100,37 @@ var Printer = class {
|
|
|
2774
3100
|
* window have a loop.
|
|
2775
3101
|
*/
|
|
2776
3102
|
#targetLoop(axis, at, ctx) {
|
|
2777
|
-
const { base, tests } = testsOf(axis);
|
|
3103
|
+
const { base: outer, tests } = testsOf(axis);
|
|
3104
|
+
const reverse = outer.t === "reverse";
|
|
3105
|
+
const base = outer.t === "reverse" ? outer.axis : outer;
|
|
2778
3106
|
const j = this.fresh("j");
|
|
2779
3107
|
const inner = {
|
|
2780
3108
|
...ctx,
|
|
2781
3109
|
focus: j,
|
|
2782
3110
|
level: ctx.level + 1
|
|
2783
3111
|
};
|
|
3112
|
+
if (base.t === "list") {
|
|
3113
|
+
if (at.length > 0) throw new NotStandalone("the axis \"list\" with an address");
|
|
3114
|
+
const list = this.#hoist(this.#listOf(base.list, ctx), hasLoop(base.list));
|
|
3115
|
+
const a = this.fresh("A");
|
|
3116
|
+
const q = this.fresh("q");
|
|
3117
|
+
const head = reverse ? `for (let ${q} = ${a}.length - 1; ${q} >= 0; ${q}--) {` : `for (let ${q} = 0; ${q} < ${a}.length; ${q}++) {`;
|
|
3118
|
+
const key = [`const ${j} = ${a}[${q}][0];`];
|
|
3119
|
+
const check = [`if (${a}[${q}][1] === E) ${this.use("F")}();`];
|
|
3120
|
+
return {
|
|
3121
|
+
s: [...list.s, `const ${a} = ${list.v.code};`],
|
|
3122
|
+
j,
|
|
3123
|
+
head,
|
|
3124
|
+
key,
|
|
3125
|
+
check,
|
|
3126
|
+
skip: void 0,
|
|
3127
|
+
fixed: false,
|
|
3128
|
+
start: list.v.deps,
|
|
3129
|
+
startSafe: list.v.safe,
|
|
3130
|
+
tests,
|
|
3131
|
+
inner
|
|
3132
|
+
};
|
|
3133
|
+
}
|
|
2784
3134
|
const s = [];
|
|
2785
3135
|
let lo = "0";
|
|
2786
3136
|
let hi = "n";
|
|
@@ -2801,14 +3151,17 @@ var Printer = class {
|
|
|
2801
3151
|
s.push(`const ${h} = Math.min(n, ${f} + Math.min(${base.to}, n) + 1);`);
|
|
2802
3152
|
hi = h;
|
|
2803
3153
|
} else if (base.t !== "all") throw new NotStandalone(`the axis "${base.t}"`);
|
|
3154
|
+
const startSafe = at.every((m) => m.t === "origin");
|
|
2804
3155
|
return {
|
|
2805
3156
|
s,
|
|
2806
3157
|
j,
|
|
2807
|
-
lo,
|
|
2808
|
-
|
|
3158
|
+
head: forHead(j, lo, hi, reverse),
|
|
3159
|
+
key: [],
|
|
3160
|
+
check: [],
|
|
2809
3161
|
skip,
|
|
2810
3162
|
fixed: f === void 0,
|
|
2811
3163
|
start,
|
|
3164
|
+
startSafe,
|
|
2812
3165
|
tests,
|
|
2813
3166
|
inner
|
|
2814
3167
|
};
|
|
@@ -2816,18 +3169,57 @@ var Printer = class {
|
|
|
2816
3169
|
#list(op, target, rest, ctx) {
|
|
2817
3170
|
const strict = this.#strict(op, rest);
|
|
2818
3171
|
const at = target.at ?? [];
|
|
2819
|
-
const { s, j,
|
|
3172
|
+
const { s, j, head, key, check, skip, fixed, start, startSafe, tests, inner } = this.#targetLoop(target.axis, at, ctx);
|
|
2820
3173
|
const { level } = inner;
|
|
2821
3174
|
const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
|
|
2822
3175
|
const body = this.#hoist(this.expr(target.body, inner), hasLoop(target.body));
|
|
2823
3176
|
if (op === "count" && fixed && tests.length === 0 && body.v.safe) return bare(plainValue("n", "number"));
|
|
2824
3177
|
const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== level));
|
|
3178
|
+
const r = this.#reduction(op, body.v.kind);
|
|
3179
|
+
const { v } = r;
|
|
3180
|
+
s.push(r.init);
|
|
3181
|
+
const item = [
|
|
3182
|
+
...check,
|
|
3183
|
+
...testCodes.flatMap((t) => [...t.s, `if (!${this.#boolean(t.v)}) continue;`]),
|
|
3184
|
+
...body.s
|
|
3185
|
+
];
|
|
3186
|
+
const safeItem = check.length === 0 && body.v.safe && testCodes.every((t) => this.#safeBoolean(t.v));
|
|
3187
|
+
const loop = [
|
|
3188
|
+
...key,
|
|
3189
|
+
...skip === void 0 ? [] : [`if (${j} === ${skip}) continue;`],
|
|
3190
|
+
...safeItem ? [...item, `const ${v} = ${body.v.code};`] : [
|
|
3191
|
+
`let ${v};`,
|
|
3192
|
+
"try {",
|
|
3193
|
+
...ind(item),
|
|
3194
|
+
` ${v} = ${body.v.code};`,
|
|
3195
|
+
"} catch {",
|
|
3196
|
+
strict ? ` ${this.use("F")}();` : " continue;",
|
|
3197
|
+
"}"
|
|
3198
|
+
],
|
|
3199
|
+
r.step
|
|
3200
|
+
];
|
|
3201
|
+
s.push(head, ...ind(loop), "}");
|
|
3202
|
+
const safe = startSafe && !strict && (op === "count" || BOOLEAN_LIST_OPS.has(op) && body.v.kind === "boolean");
|
|
3203
|
+
return {
|
|
3204
|
+
s,
|
|
3205
|
+
v: {
|
|
3206
|
+
code: r.result,
|
|
3207
|
+
kind: listKind(op, body.v.kind),
|
|
3208
|
+
safe,
|
|
3209
|
+
deps
|
|
3210
|
+
}
|
|
3211
|
+
};
|
|
3212
|
+
}
|
|
3213
|
+
/**
|
|
3214
|
+
* This method gives the parts of a list op that reduces the values of its items. The parts are the variable `v` of
|
|
3215
|
+
* 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.
|
|
3216
|
+
*/
|
|
3217
|
+
#reduction(op, kind) {
|
|
2825
3218
|
const a = this.fresh("a");
|
|
2826
3219
|
const n = this.fresh("n");
|
|
2827
3220
|
const v = this.fresh("x");
|
|
2828
|
-
const num =
|
|
2829
|
-
const bool =
|
|
2830
|
-
const F = this.use("F");
|
|
3221
|
+
const num = kind === "number" ? v : `${this.use("U")}(${v})`;
|
|
3222
|
+
const bool = kind === "boolean" ? v : `${this.use("B")}(${v})`;
|
|
2831
3223
|
const inits = {
|
|
2832
3224
|
count: `let ${n} = 0;`,
|
|
2833
3225
|
sum: `let ${a} = 0;`,
|
|
@@ -2836,7 +3228,6 @@ var Printer = class {
|
|
|
2836
3228
|
none: `let ${a} = false;`,
|
|
2837
3229
|
all: `let ${a} = true;`
|
|
2838
3230
|
};
|
|
2839
|
-
s.push(inits[op] ?? `let ${a}, ${n} = 0;`);
|
|
2840
3231
|
const steps = {
|
|
2841
3232
|
count: `${n}++;`,
|
|
2842
3233
|
sum: `${a} += ${num};`,
|
|
@@ -2848,37 +3239,128 @@ var Printer = class {
|
|
|
2848
3239
|
all: `if (!${bool}) ${a} = false;`,
|
|
2849
3240
|
first: `if (${n}++ === 0) ${a} = ${v};`
|
|
2850
3241
|
};
|
|
2851
|
-
const
|
|
2852
|
-
const
|
|
3242
|
+
const F = this.use("F");
|
|
3243
|
+
const results = {
|
|
3244
|
+
count: n,
|
|
3245
|
+
sum: this.#checked("number", a),
|
|
3246
|
+
mean: `(${n} === 0 ? ${F}() : ${this.#checked("number", `${a} / ${n}`)})`,
|
|
3247
|
+
any: a,
|
|
3248
|
+
all: a,
|
|
3249
|
+
none: `!${a}`
|
|
3250
|
+
};
|
|
3251
|
+
return {
|
|
3252
|
+
v,
|
|
3253
|
+
init: inits[op] ?? `let ${a}, ${n} = 0;`,
|
|
3254
|
+
step: steps[op],
|
|
3255
|
+
result: results[op] ?? `(${n} === 0 ? ${F}() : ${a})`
|
|
3256
|
+
};
|
|
3257
|
+
}
|
|
3258
|
+
/**
|
|
3259
|
+
* This method prints a list as an array (LIST.VALUE): a pair of the key and the value of each item. A test that
|
|
3260
|
+
* fails, a body that fails and an error item of the axis list give the error sentinel as the value.
|
|
3261
|
+
*/
|
|
3262
|
+
#eachList(e, ctx) {
|
|
3263
|
+
const at = e.at ?? [];
|
|
3264
|
+
const { s, j, head, key, check, skip, start, startSafe, tests, inner } = this.#targetLoop(e.axis, at, ctx);
|
|
3265
|
+
const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
|
|
3266
|
+
const body = this.#hoist(this.expr(e.body, inner), hasLoop(e.body));
|
|
3267
|
+
const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== inner.level));
|
|
3268
|
+
const list = this.fresh("L");
|
|
3269
|
+
const v = this.fresh("x");
|
|
3270
|
+
const item = [
|
|
3271
|
+
...check,
|
|
3272
|
+
...testCodes.flatMap((t) => [...t.s, `if (!${this.#boolean(t.v)}) continue;`]),
|
|
3273
|
+
...body.s
|
|
3274
|
+
];
|
|
3275
|
+
const safeItem = check.length === 0 && body.v.safe && testCodes.every((t) => this.#safeBoolean(t.v));
|
|
2853
3276
|
const loop = [
|
|
3277
|
+
...key,
|
|
2854
3278
|
...skip === void 0 ? [] : [`if (${j} === ${skip}) continue;`],
|
|
2855
|
-
...safeItem ? [...item,
|
|
3279
|
+
...safeItem ? [...item, `${list}.push([${j}, ${body.v.code}]);`] : [
|
|
2856
3280
|
`let ${v};`,
|
|
2857
3281
|
"try {",
|
|
2858
3282
|
...ind(item),
|
|
2859
3283
|
` ${v} = ${body.v.code};`,
|
|
2860
3284
|
"} catch {",
|
|
2861
|
-
|
|
3285
|
+
` ${v} = E;`,
|
|
3286
|
+
"}",
|
|
3287
|
+
`${list}.push([${j}, ${v}]);`
|
|
3288
|
+
]
|
|
3289
|
+
];
|
|
3290
|
+
return {
|
|
3291
|
+
s: [
|
|
3292
|
+
...s,
|
|
3293
|
+
`const ${list} = [];`,
|
|
3294
|
+
head,
|
|
3295
|
+
...ind(loop),
|
|
2862
3296
|
"}"
|
|
2863
3297
|
],
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
3298
|
+
v: {
|
|
3299
|
+
code: list,
|
|
3300
|
+
kind: void 0,
|
|
3301
|
+
safe: startSafe,
|
|
3302
|
+
deps,
|
|
3303
|
+
list: { item: body.v.kind }
|
|
3304
|
+
}
|
|
3305
|
+
};
|
|
3306
|
+
}
|
|
3307
|
+
/**
|
|
3308
|
+
* This method prints sortBy and take (LIST.SORT, LIST.TAKE): a new array from the array of a list. An error item is
|
|
3309
|
+
* not in the new list, and with strict it is the error of the op.
|
|
3310
|
+
*/
|
|
3311
|
+
#order(op, head, rest, ctx) {
|
|
3312
|
+
const list = this.#listOf(head, ctx);
|
|
3313
|
+
const exprs = [head, ...op === "take" ? [rest[0] ?? lit(null)] : []];
|
|
3314
|
+
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
3315
|
+
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
3316
|
+
const deps = depsOf(...raw);
|
|
3317
|
+
const parts = this.#settle(raw, deps, exprs);
|
|
3318
|
+
const item = list.v.list.item;
|
|
2876
3319
|
return {
|
|
2877
|
-
s,
|
|
3320
|
+
s: stmtsOf(...parts),
|
|
2878
3321
|
v: {
|
|
2879
|
-
code,
|
|
2880
|
-
kind:
|
|
2881
|
-
safe,
|
|
3322
|
+
code: `${this.use(op === "sortBy" ? "SB" : "TK")}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
|
|
3323
|
+
kind: void 0,
|
|
3324
|
+
safe: false,
|
|
3325
|
+
deps,
|
|
3326
|
+
list: { item }
|
|
3327
|
+
}
|
|
3328
|
+
};
|
|
3329
|
+
}
|
|
3330
|
+
/** This method prints a list op over a list that is not an each: a loop over its array, and nth (LIST.NTH). */
|
|
3331
|
+
#overList(op, head, rest, ctx) {
|
|
3332
|
+
const list = this.#listOf(head, ctx);
|
|
3333
|
+
const exprs = [head, ...op === "nth" ? [rest[0] ?? lit(null)] : []];
|
|
3334
|
+
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
3335
|
+
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
3336
|
+
const deps = depsOf(...raw);
|
|
3337
|
+
const parts = this.#settle(raw, deps, exprs);
|
|
3338
|
+
const [l] = parts;
|
|
3339
|
+
const item = list.v.list.item;
|
|
3340
|
+
if (op === "nth") return {
|
|
3341
|
+
s: stmtsOf(...parts),
|
|
3342
|
+
v: {
|
|
3343
|
+
code: `${this.use("NT")}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
|
|
3344
|
+
kind: item,
|
|
3345
|
+
safe: false,
|
|
3346
|
+
deps
|
|
3347
|
+
}
|
|
3348
|
+
};
|
|
3349
|
+
const r = this.#reduction(op, item);
|
|
3350
|
+
const { v } = r;
|
|
3351
|
+
return {
|
|
3352
|
+
s: [
|
|
3353
|
+
...l.s,
|
|
3354
|
+
r.init,
|
|
3355
|
+
`for (const [, ${v}] of ${l.v.code}) {`,
|
|
3356
|
+
` if (${v} === E) ${strict ? `${this.use("F")}();` : "continue;"}`,
|
|
3357
|
+
` ${r.step}`,
|
|
3358
|
+
"}"
|
|
3359
|
+
],
|
|
3360
|
+
v: {
|
|
3361
|
+
code: r.result,
|
|
3362
|
+
kind: listKind(op, item),
|
|
3363
|
+
safe: false,
|
|
2882
3364
|
deps
|
|
2883
3365
|
}
|
|
2884
3366
|
};
|
|
@@ -2994,7 +3476,9 @@ var Printer = class {
|
|
|
2994
3476
|
env: names.env
|
|
2995
3477
|
});
|
|
2996
3478
|
const body = [
|
|
3479
|
+
...t.key,
|
|
2997
3480
|
...t.skip === void 0 ? [] : [`if (${t.j} === ${t.skip}) continue;`],
|
|
3481
|
+
...t.check,
|
|
2998
3482
|
...tests.flatMap((c) => [...c.s, `if (!${this.#boolean(c.v)}) continue;`]),
|
|
2999
3483
|
...step.s
|
|
3000
3484
|
];
|
|
@@ -3005,12 +3489,11 @@ var Printer = class {
|
|
|
3005
3489
|
...inside,
|
|
3006
3490
|
...[...result.v.deps].filter((d) => d !== loop)
|
|
3007
3491
|
]);
|
|
3008
|
-
const head = `for (let ${t.j} = ${t.lo}; ${t.j} < ${t.hi}; ${t.j}++) {`;
|
|
3009
3492
|
return {
|
|
3010
3493
|
s: [
|
|
3011
3494
|
...names.s,
|
|
3012
3495
|
...t.s,
|
|
3013
|
-
head,
|
|
3496
|
+
t.head,
|
|
3014
3497
|
...ind(body),
|
|
3015
3498
|
"}",
|
|
3016
3499
|
...result.s
|
|
@@ -3048,7 +3531,9 @@ var Printer = class {
|
|
|
3048
3531
|
origin: void 0,
|
|
3049
3532
|
focus: void 0,
|
|
3050
3533
|
level: 1,
|
|
3534
|
+
originLevel: 1,
|
|
3051
3535
|
env: /* @__PURE__ */ new Map(),
|
|
3536
|
+
fns: /* @__PURE__ */ new Map(),
|
|
3052
3537
|
keys: names
|
|
3053
3538
|
};
|
|
3054
3539
|
if (varying.length === 0) {
|
|
@@ -3073,6 +3558,7 @@ var Printer = class {
|
|
|
3073
3558
|
} else {
|
|
3074
3559
|
const body = this.expr(e, bodyCtx);
|
|
3075
3560
|
this.#kinds.set(fn, body.v.kind);
|
|
3561
|
+
this.#lists.set(fn, body.v.list);
|
|
3076
3562
|
this.run.push(`const ${fn} = ${this.use(params.length === 1 ? "M1" : "M")}((${params.join(", ")}) => {`, ...ind(body.s), ` return ${body.v.code};`, "});");
|
|
3077
3563
|
}
|
|
3078
3564
|
}
|
|
@@ -3092,7 +3578,8 @@ var Printer = class {
|
|
|
3092
3578
|
code: `${fn}(${args.join(", ")})`,
|
|
3093
3579
|
kind: this.#kinds.get(fn),
|
|
3094
3580
|
safe: false,
|
|
3095
|
-
deps: depsOf(...values)
|
|
3581
|
+
deps: depsOf(...values),
|
|
3582
|
+
list: this.#lists.get(fn)
|
|
3096
3583
|
}
|
|
3097
3584
|
};
|
|
3098
3585
|
}
|
|
@@ -3205,7 +3692,9 @@ var Printer = class {
|
|
|
3205
3692
|
origin: "o",
|
|
3206
3693
|
focus: "o",
|
|
3207
3694
|
level: 1,
|
|
3695
|
+
originLevel: 1,
|
|
3208
3696
|
env: /* @__PURE__ */ new Map(),
|
|
3697
|
+
fns: /* @__PURE__ */ new Map(),
|
|
3209
3698
|
keys: /* @__PURE__ */ new Map()
|
|
3210
3699
|
}));
|
|
3211
3700
|
for (const [name, form] of this.#forms) if (form !== void 0 && !this.#called.has(name)) this.top.delete(name);
|
|
@@ -3219,7 +3708,13 @@ var Printer = class {
|
|
|
3219
3708
|
return canonical(e, (x) => {
|
|
3220
3709
|
const key = ctx.keys.get(x);
|
|
3221
3710
|
if (key !== void 0) return { "@": key.code };
|
|
3222
|
-
|
|
3711
|
+
const node = x;
|
|
3712
|
+
if (node?.tag === "var") return { $: ctx.env.get(node.name)?.code ?? node.name };
|
|
3713
|
+
return node?.tag === "call" ? {
|
|
3714
|
+
"()": node.fn,
|
|
3715
|
+
of: ctx.fns.get(node.fn)?.id ?? null,
|
|
3716
|
+
args: node.args
|
|
3717
|
+
} : void 0;
|
|
3223
3718
|
});
|
|
3224
3719
|
}
|
|
3225
3720
|
};
|