@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/src/module.ts
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* schema, and the axes `all`, `others` and `window`. The kinds of each op site must decide its op. Another construct
|
|
20
20
|
* gives a reason, not code.
|
|
21
21
|
*/
|
|
22
|
-
import { addrExprs, children, isFieldName, moveExpr, shapeError, type AnyDomain, type Axis, type Expr, type ExprOf, type Move, type Names, type PrimitiveKind } from "@mark1russell7/vex";
|
|
22
|
+
import { addrExprs, children, isFieldName, lit, moveExpr, shapeError, type AnyDomain, type Axis, type Expr, type ExprOf, type Move, type Names, type PrimitiveKind } from "@mark1russell7/vex";
|
|
23
23
|
import { factsOf, kindOfDomain, primitiveOf, type Facts, type Kind, type Schema } from "./facts.ts";
|
|
24
24
|
import { memoNodes } from "./memo.ts";
|
|
25
25
|
|
|
@@ -55,6 +55,13 @@ interface Value {
|
|
|
55
55
|
readonly deps: ReadonlySet<number>;
|
|
56
56
|
/** True for a literal and for the read of a name: a constant of the run with such code needs no variable of its own. */
|
|
57
57
|
readonly plain?: boolean;
|
|
58
|
+
/** For a list (LIST.VALUE): the kind of its items. The code is an array of pairs of a key and a value or the sentinel. */
|
|
59
|
+
readonly list?: ListInfo | undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The facts of a list of the code: the kind of the values of its items, if the printer knows it. */
|
|
63
|
+
interface ListInfo {
|
|
64
|
+
readonly item: Kind | undefined;
|
|
58
65
|
}
|
|
59
66
|
|
|
60
67
|
/** The code of a node: the statements before its value, and its value. `safe` covers the statements too. */
|
|
@@ -69,6 +76,14 @@ interface Name {
|
|
|
69
76
|
readonly kind: Kind | undefined;
|
|
70
77
|
readonly lazy: boolean;
|
|
71
78
|
readonly deps: ReadonlySet<number>;
|
|
79
|
+
readonly list?: ListInfo | undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** An argument of a call: its code, its kind and its levels. */
|
|
83
|
+
interface Arg {
|
|
84
|
+
readonly code: string;
|
|
85
|
+
readonly kind: Kind | undefined;
|
|
86
|
+
readonly deps: ReadonlySet<number>;
|
|
72
87
|
}
|
|
73
88
|
|
|
74
89
|
/** The levels of a constant of the run. */
|
|
@@ -92,12 +107,49 @@ interface Ctx {
|
|
|
92
107
|
readonly focus: string | undefined;
|
|
93
108
|
/** The level of the focus: 1 at the key of the run, and one more in each axis. */
|
|
94
109
|
readonly level: number;
|
|
110
|
+
/** The level of the origin: 1 at the key of the run, and the marker of the origin in the body of a function. */
|
|
111
|
+
readonly originLevel: number;
|
|
95
112
|
readonly env: ReadonlyMap<string, Name>;
|
|
96
113
|
readonly keys: ReadonlyMap<Expr, Name>;
|
|
114
|
+
/** The bound functions (FN.BIND), with the context of their binding. */
|
|
115
|
+
readonly fns: ReadonlyMap<string, BoundFn>;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A bound function of the printer (FN.BIND): its node and the context of its binding. It also has one printed
|
|
120
|
+
* JavaScript function for each signature of the arguments of its calls.
|
|
121
|
+
*/
|
|
122
|
+
interface BoundFn {
|
|
123
|
+
/** The number of the binding. Two functions with one name are two functions, so a key of a constant needs it. */
|
|
124
|
+
readonly id: number;
|
|
125
|
+
readonly fn: ExprOf<"fn">;
|
|
126
|
+
readonly ctx: Ctx;
|
|
127
|
+
/** The printed function for each signature: the kind of each argument. */
|
|
128
|
+
readonly specs: Map<string, Spec>;
|
|
129
|
+
/** The declarations of the printed functions that read names of the place of the binding. The let prints them. */
|
|
130
|
+
readonly decls: string[];
|
|
131
|
+
/** True when a printed function of the binding is at the place of the binding, not at the start of a run. */
|
|
132
|
+
local: boolean;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* A printed function. The levels of the value of its body have a marker for the origin, for the focus and for each
|
|
137
|
+
* parameter. A call replaces each marker with the levels at the call.
|
|
138
|
+
*/
|
|
139
|
+
interface Spec {
|
|
140
|
+
readonly name: string;
|
|
141
|
+
readonly kind: Kind | undefined;
|
|
142
|
+
readonly safe: boolean;
|
|
143
|
+
readonly deps: ReadonlySet<number>;
|
|
144
|
+
readonly origin: number;
|
|
145
|
+
readonly focus: number;
|
|
146
|
+
readonly param: number;
|
|
147
|
+
/** True when the body reads only constants of the run, its parameters and its position, so it is at the start of a run. */
|
|
148
|
+
readonly global: boolean;
|
|
97
149
|
}
|
|
98
150
|
|
|
99
151
|
/** The name of a helper of the generated code. */
|
|
100
|
-
type Helper = "F" | "C" | "T" | "L" | "Q" | "G" | "B" | "U" | "X" | "I" | "K" | "M" | "M1" | "S";
|
|
152
|
+
type Helper = "F" | "C" | "T" | "L" | "Q" | "G" | "B" | "U" | "X" | "I" | "K" | "M" | "M1" | "S" | "V" | "SB" | "TK" | "NT";
|
|
101
153
|
|
|
102
154
|
/** The helpers of the generated code. A module has only the helpers that it uses. */
|
|
103
155
|
const HELPERS: Readonly<Record<Exclude<Helper, "S">, string>> = {
|
|
@@ -117,6 +169,19 @@ const HELPERS: Readonly<Record<Exclude<Helper, "S">, string>> = {
|
|
|
117
169
|
// The index of a computed position, and of a computed key: a key of an array space is the text of its index.
|
|
118
170
|
I: "function I(t, n) { return Number.isInteger(t) && t >= 0 && t < n ? t : F(); }",
|
|
119
171
|
K: 'function K(k, n) { return typeof k === "string" && /^(0|[1-9]\\d*)$/.test(k) && +k < n ? +k : F(); }',
|
|
172
|
+
// The ops on the order of a list (LIST.SORT, LIST.TAKE, LIST.NTH) work on the items with values. With strict, an
|
|
173
|
+
// error item is the error of the op. A sort is stable, and the values are numbers without NaN or texts of one kind.
|
|
174
|
+
V: "function V(l, s) { if (s && l.some((x) => x[1] === E)) F(); return l.filter((x) => x[1] !== E); }",
|
|
175
|
+
SB: [
|
|
176
|
+
"function SB(l, s) {",
|
|
177
|
+
" const o = V(l, s);",
|
|
178
|
+
" const t = o.length > 0 ? typeof o[0][1] : \"number\";",
|
|
179
|
+
" for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
|
|
180
|
+
" return o.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
|
|
181
|
+
"}",
|
|
182
|
+
].join("\n"),
|
|
183
|
+
TK: "function TK(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(0, n) : F(); }",
|
|
184
|
+
NT: "function NT(l, i, s) { const o = V(l, s); return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F(); }",
|
|
120
185
|
// The memo of a function of one key, and of more keys. -0 has its own entry, because an op can tell it from 0.
|
|
121
186
|
M1: [
|
|
122
187
|
"function M1(f) {",
|
|
@@ -273,7 +338,13 @@ const STEPS: Readonly<Record<string, readonly [string, string]>> = {
|
|
|
273
338
|
};
|
|
274
339
|
|
|
275
340
|
/** The helpers that each helper uses. */
|
|
276
|
-
const NEEDS: Readonly<Record<Helper, readonly Helper[]>> = {
|
|
341
|
+
const NEEDS: Readonly<Record<Helper, readonly Helper[]>> = {
|
|
342
|
+
F: [], C: [], T: [], L: [], Q: [], G: ["F"], B: ["F"], U: ["F"], X: ["F"], I: ["F"], K: ["F"], M: ["G"], M1: ["G"], S: ["M1", "F"],
|
|
343
|
+
// Only SB, TK and NT use V, and they need F too, so the need of V only completes the list.
|
|
344
|
+
// Stryker disable next-line ArrayDeclaration: see the comment above
|
|
345
|
+
V: ["F"],
|
|
346
|
+
SB: ["V", "F"], TK: ["V", "F"], NT: ["V", "F"],
|
|
347
|
+
};
|
|
277
348
|
|
|
278
349
|
/** The list ops of a standalone module. */
|
|
279
350
|
const LIST_OPS: ReadonlySet<string> = new Set(["count", "sum", "mean", "min", "max", "any", "all", "none", "first"]);
|
|
@@ -298,6 +369,9 @@ function namesOf(e: Expr, out = new Set<string>()): Set<string> {
|
|
|
298
369
|
*/
|
|
299
370
|
const COLUMN_WORK = 10;
|
|
300
371
|
|
|
372
|
+
/** 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. */
|
|
373
|
+
const forHead = (j: string, lo: string, hi: string, reverse: boolean): string => (reverse ? `for (let ${j} = ${hi} - 1; ${j} >= ${lo}; ${j}--) {` : `for (let ${j} = ${lo}; ${j} < ${hi}; ${j}++) {`);
|
|
374
|
+
|
|
301
375
|
/** This function tells if an expression has a loop of names: a fold or an iteration. */
|
|
302
376
|
function hasLoop(e: Expr): boolean {
|
|
303
377
|
let found = LOOPS.get(e);
|
|
@@ -522,6 +596,9 @@ class Printer {
|
|
|
522
596
|
/** The parts of the helper S that the nodes of the memo that keep their items need (helperS). */
|
|
523
597
|
readonly features = new Set<Feature>();
|
|
524
598
|
#names = 0;
|
|
599
|
+
/** The number of the next bound function, and of the next printed function. */
|
|
600
|
+
#fnIds = 0;
|
|
601
|
+
#printed = 0;
|
|
525
602
|
/** The level of the names of the next loop. It is far above the levels of the axes, so the two do not meet. */
|
|
526
603
|
#loops = 1_000_000;
|
|
527
604
|
/** The node of the memo whose function the printer prints, so it prints the node itself. */
|
|
@@ -540,6 +617,10 @@ class Printer {
|
|
|
540
617
|
readonly #columns = new Map<string, string>();
|
|
541
618
|
/** The expressions whose code is a constant of the run. */
|
|
542
619
|
readonly #constant = new WeakSet<Expr>();
|
|
620
|
+
/** The expressions at a place that takes a list: a binding, the head of a list op and the list of an axis. */
|
|
621
|
+
readonly #wantList = new WeakSet<Expr>();
|
|
622
|
+
/** The facts of the list of each function of the memo whose value is a list. */
|
|
623
|
+
readonly #lists = new Map<string, ListInfo | undefined>();
|
|
543
624
|
|
|
544
625
|
constructor(expr: Expr, options: ModuleOptions) {
|
|
545
626
|
this.#domains = options.domains ?? [];
|
|
@@ -624,13 +705,13 @@ class Printer {
|
|
|
624
705
|
|
|
625
706
|
/** This method gives the code of a name: a read that throws the error of its value, if it can hold an error. */
|
|
626
707
|
#read(name: Name): Value {
|
|
627
|
-
const base = { kind: name.kind, deps: name.deps, plain: true };
|
|
708
|
+
const base = { kind: name.kind, deps: name.deps, plain: true, list: name.list };
|
|
628
709
|
return name.lazy ? { ...base, code: `${this.use("G")}(${name.code})`, safe: false, held: name.code } : { ...base, code: name.code, safe: true };
|
|
629
710
|
}
|
|
630
711
|
|
|
631
712
|
/** This method gives the name of a constant of the run, from the read of its value. */
|
|
632
713
|
#nameOf(v: Value): Name {
|
|
633
|
-
return { code: v.held ?? v.code, kind: v.kind, lazy: v.held !== undefined, deps: NO_DEPS };
|
|
714
|
+
return { code: v.held ?? v.code, kind: v.kind, lazy: v.held !== undefined, deps: NO_DEPS, list: v.list };
|
|
634
715
|
}
|
|
635
716
|
|
|
636
717
|
/**
|
|
@@ -656,7 +737,8 @@ class Printer {
|
|
|
656
737
|
const name = this.fresh("c");
|
|
657
738
|
if (lazy) this.run.push(`const ${name} = ${this.use("L")}(() => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
|
|
658
739
|
else this.run.push(...this.#store(name, c, true));
|
|
659
|
-
const
|
|
740
|
+
const { list } = c.v;
|
|
741
|
+
const read = bare(this.#read(lazy ? { code: `${name}()`, kind: c.v.kind, lazy: true, deps: NO_DEPS, list } : { code: name, kind: c.v.kind, lazy: !c.v.safe, deps: NO_DEPS, list }));
|
|
660
742
|
if (key !== undefined) this.#constants.set(key, read);
|
|
661
743
|
return read;
|
|
662
744
|
}
|
|
@@ -679,19 +761,44 @@ class Printer {
|
|
|
679
761
|
this.#keyOf.set(c, this.#constKey(e, ctx));
|
|
680
762
|
this.#constant.add(e);
|
|
681
763
|
}
|
|
764
|
+
// A list is a value only at a place that takes a list. A result, an argument and a field take no list.
|
|
765
|
+
if (c.v.list !== undefined && !this.#wantList.has(e)) throw new NotStandalone("a list is a value of the program");
|
|
682
766
|
return this.#columnar(e, c, ctx) ? this.#column(e, c, ctx) : c;
|
|
683
767
|
}
|
|
684
768
|
|
|
769
|
+
/** This method gives the code of a list at a place that takes a list (LIST.VALUE): an each, a name, sortBy or take. */
|
|
770
|
+
#listOf(e: Expr, ctx: Ctx): Code {
|
|
771
|
+
this.#wantList.add(e);
|
|
772
|
+
const c = this.expr(e, ctx);
|
|
773
|
+
if (c.v.list === undefined) throw new NotStandalone("a list op on a value that is not a list");
|
|
774
|
+
return c;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/** 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. */
|
|
778
|
+
#isList(e: Expr, ctx: Ctx): boolean {
|
|
779
|
+
if (e.tag === "each") return true;
|
|
780
|
+
if (e.tag === "app") return e.op === "sortBy" || e.op === "take";
|
|
781
|
+
return e.tag === "var" && ctx.env.get(e.name)?.list !== undefined;
|
|
782
|
+
}
|
|
783
|
+
|
|
685
784
|
/**
|
|
686
785
|
* This method tells if a part of a loop over the targets is a column of the run. Its value depends only on the
|
|
687
786
|
* target and on the constants of the run, so each run computes it once for each record. Each name that it reads
|
|
688
787
|
* from outside is a constant of the run, so its code can be outside the loop.
|
|
689
788
|
*/
|
|
690
789
|
#columnar(e: Expr, c: Code, ctx: Ctx): boolean {
|
|
691
|
-
|
|
790
|
+
// A node of the memo has the level 1, so a column has a focus.
|
|
791
|
+
if (ctx.level < 2 || c.v.deps.size !== 1 || !c.v.deps.has(ctx.level) || this.#work(e) < COLUMN_WORK) return false;
|
|
692
792
|
// A node of the memo with keys keeps its costly part for each value of its keys already.
|
|
693
793
|
if (this.#keyed(e)) return false;
|
|
694
|
-
return [...namesOf(e)].every((name) => (ctx.env.get(name)?.deps.size ?? 0) === 0);
|
|
794
|
+
return [...namesOf(e)].every((name) => (ctx.env.get(name)?.deps.size ?? 0) === 0) && !this.#callsLocal(e, ctx);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/** This method tells if an expression calls a function that is at the place of its binding, not at the start of a run. */
|
|
798
|
+
#callsLocal(e: Expr, ctx: Ctx): boolean {
|
|
799
|
+
// Stryker disable next-line ConditionalExpression: a node that is not a call has no field fn, so the map gives undefined
|
|
800
|
+
if (e.tag === "call" && ctx.fns.get(e.fn)?.local === true) return true;
|
|
801
|
+
return children(e).some((x) => this.#callsLocal(x, ctx));
|
|
695
802
|
}
|
|
696
803
|
|
|
697
804
|
/** This method tells if an expression has a node of the memo with keys in it. */
|
|
@@ -714,7 +821,7 @@ class Printer {
|
|
|
714
821
|
this.run.push(`const ${name} = ${this.use("Q")}((${ctx.focus as string}) => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
|
|
715
822
|
this.#columns.set(key, name);
|
|
716
823
|
}
|
|
717
|
-
return bare({ code: `${this.use("G")}(${name}(${ctx.focus as string}))`, kind: c.v.kind, safe: false, deps: c.v.deps });
|
|
824
|
+
return bare({ code: `${this.use("G")}(${name}(${ctx.focus as string}))`, kind: c.v.kind, safe: false, deps: c.v.deps, list: c.v.list });
|
|
718
825
|
}
|
|
719
826
|
|
|
720
827
|
#node(e: Expr, ctx: Ctx): Code {
|
|
@@ -727,6 +834,12 @@ class Printer {
|
|
|
727
834
|
return bare(this.#var(e.name, ctx));
|
|
728
835
|
case "let":
|
|
729
836
|
return this.#let(e, ctx);
|
|
837
|
+
// A function is only a binding of let, so the check of the shape stops this node first.
|
|
838
|
+
// Stryker disable next-line all: see the comment above
|
|
839
|
+
/* v8 ignore next -- @preserve */
|
|
840
|
+
case "fn": throw new NotStandalone("a function outside the bindings of a let");
|
|
841
|
+
case "call":
|
|
842
|
+
return this.#call(e, ctx);
|
|
730
843
|
case "rec": {
|
|
731
844
|
const names = Object.keys(e.fields);
|
|
732
845
|
const raw = Object.values(e.fields).map((x) => this.expr(x, ctx));
|
|
@@ -739,7 +852,7 @@ class Printer {
|
|
|
739
852
|
case "app":
|
|
740
853
|
return this.#app(e, ctx);
|
|
741
854
|
case "each":
|
|
742
|
-
|
|
855
|
+
return this.#eachList(e, ctx);
|
|
743
856
|
case "fold":
|
|
744
857
|
return this.#fold(e, ctx);
|
|
745
858
|
case "iterate":
|
|
@@ -840,7 +953,7 @@ class Printer {
|
|
|
840
953
|
for (const m of at) {
|
|
841
954
|
if (fails) for (const d of deps) kept.add(d);
|
|
842
955
|
if (moveExpr(m) !== undefined) deps = (values[next++] as Code).v.deps;
|
|
843
|
-
else if (m.t === "origin") deps = new Set([
|
|
956
|
+
else if (m.t === "origin") deps = new Set([ctx.originLevel]);
|
|
844
957
|
else if (m.t === "key" || m.t === "index" || m.t === "parent") deps = NO_DEPS;
|
|
845
958
|
fails = m.t !== "origin";
|
|
846
959
|
}
|
|
@@ -850,6 +963,8 @@ class Printer {
|
|
|
850
963
|
#var(name: string, ctx: Ctx): Value {
|
|
851
964
|
const bound = ctx.env.get(name);
|
|
852
965
|
if (bound !== undefined) return this.#read(bound);
|
|
966
|
+
// A name of a loop hides a function with its name, so this check comes after the names of the scope.
|
|
967
|
+
if (ctx.fns.has(name)) throw new NotStandalone(`the name "${name}" is a function, not a value`);
|
|
853
968
|
const kind = this.#schema.vars?.[name];
|
|
854
969
|
if (kind === undefined) throw new NotStandalone(`a variable that is not in the schema: "${name}"`);
|
|
855
970
|
let code = this.vars.get(name);
|
|
@@ -866,9 +981,22 @@ class Printer {
|
|
|
866
981
|
*/
|
|
867
982
|
#let(e: ExprOf<"let">, ctx: Ctx): Code {
|
|
868
983
|
const env = new Map(ctx.env);
|
|
984
|
+
const fns = new Map(ctx.fns);
|
|
869
985
|
const s: string[] = [];
|
|
870
986
|
let deps = new Set<number>();
|
|
987
|
+
const bound: BoundFn[] = [];
|
|
871
988
|
for (const [name, x] of Object.entries(e.bind)) {
|
|
989
|
+
// A binding sees the context of the let, so a function cannot call itself.
|
|
990
|
+
if (x.tag === "fn") {
|
|
991
|
+
// Stryker disable next-line UpdateOperator: the numbers of the bindings only need to be distinct
|
|
992
|
+
const b: BoundFn = { id: this.#fnIds++, fn: x, ctx, specs: new Map(), decls: [], local: false };
|
|
993
|
+
bound.push(b);
|
|
994
|
+
fns.set(name, b);
|
|
995
|
+
env.delete(name);
|
|
996
|
+
continue;
|
|
997
|
+
}
|
|
998
|
+
// A binding takes a list (LIST.VALUE). A value hides a function of its name, because a read looks at the values first.
|
|
999
|
+
this.#wantList.add(x);
|
|
872
1000
|
const c = this.#hoist(this.expr(x, ctx), hasLoop(x));
|
|
873
1001
|
if (c.v.deps.size === 0) {
|
|
874
1002
|
env.set(name, this.#nameOf(c.v));
|
|
@@ -876,12 +1004,78 @@ class Printer {
|
|
|
876
1004
|
}
|
|
877
1005
|
const b = this.fresh("b");
|
|
878
1006
|
s.push(...this.#store(b, c));
|
|
879
|
-
env.set(name, { code: b, kind: c.v.kind, lazy: !c.v.safe, deps: c.v.deps });
|
|
1007
|
+
env.set(name, { code: b, kind: c.v.kind, lazy: !c.v.safe, deps: c.v.deps, list: c.v.list });
|
|
880
1008
|
// The statements of the binding read its keys, so the let is not a constant of the run.
|
|
881
1009
|
deps = new Set([...deps, ...c.v.deps]);
|
|
882
1010
|
}
|
|
883
|
-
const body = this.expr(e.body, { ...ctx, env });
|
|
884
|
-
|
|
1011
|
+
const body = this.expr(e.body, { ...ctx, env, fns });
|
|
1012
|
+
// A function that reads names of the place of the binding comes after them, before the body.
|
|
1013
|
+
return { s: [...s, ...bound.flatMap((b) => b.decls), ...body.s], v: { ...body.v, deps: new Set([...deps, ...body.v.deps]) } };
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
/**
|
|
1017
|
+
* This method prints a call (FN.CALL). Each argument evaluates at the place of the call, as a binding of a let. The
|
|
1018
|
+
* call goes to the printed function of the signature of its arguments, with the focus and the origin if it reads them.
|
|
1019
|
+
*/
|
|
1020
|
+
#call(e: ExprOf<"call">, ctx: Ctx): Code {
|
|
1021
|
+
const bound = ctx.env.has(e.fn) ? undefined : ctx.fns.get(e.fn);
|
|
1022
|
+
if (bound === undefined) throw new NotStandalone(`a call of "${e.fn}", which is not a function`);
|
|
1023
|
+
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)}`);
|
|
1024
|
+
const s: string[] = [];
|
|
1025
|
+
const args = e.args.map((x): Arg => {
|
|
1026
|
+
const c = this.#hoist(this.expr(x, ctx), hasLoop(x));
|
|
1027
|
+
// A value that cannot throw is the argument itself. A read of a name that can hold the error passes the name.
|
|
1028
|
+
if (c.v.safe) {
|
|
1029
|
+
s.push(...c.s);
|
|
1030
|
+
return { code: c.v.code, kind: c.v.kind, deps: c.v.deps };
|
|
1031
|
+
}
|
|
1032
|
+
// Stryker disable next-line ConditionalExpression: the statements with a read of a name bind other names and do not throw, so the read alone has the same value
|
|
1033
|
+
if (c.s.length === 0 && c.v.held !== undefined) return { code: c.v.held, kind: c.v.kind, deps: c.v.deps };
|
|
1034
|
+
const b = this.fresh("b");
|
|
1035
|
+
s.push(...this.#store(b, c));
|
|
1036
|
+
return { code: b, kind: c.v.kind, deps: c.v.deps };
|
|
1037
|
+
});
|
|
1038
|
+
// Each parameter can hold the error of its argument, so one function serves the arguments of one kind.
|
|
1039
|
+
const sig = JSON.stringify(args.map((a) => a.kind ?? null));
|
|
1040
|
+
const spec = bound.specs.get(sig) ?? this.#printFn(bound, args, sig);
|
|
1041
|
+
// Each marker becomes the levels at the call. The levels of the arguments count for each call, because their
|
|
1042
|
+
// statements read their keys, so the marker of the parameters gives no level.
|
|
1043
|
+
const levels = (d: number): readonly number[] => (d === spec.origin ? [ctx.originLevel] : d === spec.focus ? [ctx.level] : d === spec.param ? [] : [d]);
|
|
1044
|
+
const deps = new Set([...args.flatMap((a) => [...a.deps]), ...[...spec.deps].flatMap(levels)]);
|
|
1045
|
+
const position = spec.deps.has(spec.origin) ? [ctx.focus, ctx.origin] : spec.deps.has(spec.focus) ? [ctx.focus] : [];
|
|
1046
|
+
return { s, v: { code: `${spec.name}(${[...args.map((a) => a.code), ...position].join(", ")})`, kind: spec.kind, safe: spec.safe, deps } };
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* This method prints a bound function for one signature of its arguments, as one JavaScript function. Its parameters
|
|
1051
|
+
* are the arguments, then the focus and the origin, if the body reads them. A function that reads a name of the place
|
|
1052
|
+
* 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.
|
|
1053
|
+
*/
|
|
1054
|
+
#printFn(bound: BoundFn, args: readonly Arg[], sig: string): Spec {
|
|
1055
|
+
// The markers are far from the levels of the axes and of the loops, so they do not meet them. They only need to be
|
|
1056
|
+
// distinct.
|
|
1057
|
+
// Stryker disable next-line ArithmeticOperator,UpdateOperator: see the comment above
|
|
1058
|
+
const origin = 2_000_000 + 1000 * this.#printed++;
|
|
1059
|
+
const focus = origin + 1;
|
|
1060
|
+
const param = -origin;
|
|
1061
|
+
const names = args.map(() => this.fresh("p"));
|
|
1062
|
+
const k = this.fresh("k");
|
|
1063
|
+
const o = this.fresh("o");
|
|
1064
|
+
const env = new Map(bound.ctx.env);
|
|
1065
|
+
// A parameter hides a function of its name, because a read looks at the values first.
|
|
1066
|
+
bound.fn.params.forEach((p, i) => env.set(p, { code: names[i] as string, kind: (args[i] as Arg).kind, lazy: true, deps: new Set([param]) }));
|
|
1067
|
+
const body = this.expr(bound.fn.body, { origin: o, focus: k, level: focus, originLevel: origin, env, fns: bound.ctx.fns, keys: new Map() });
|
|
1068
|
+
const markers = new Set([origin, focus, param]);
|
|
1069
|
+
const global = [...body.v.deps].every((d) => markers.has(d));
|
|
1070
|
+
const name = this.fresh("fn");
|
|
1071
|
+
const position = body.v.deps.has(origin) ? [k, o] : body.v.deps.has(focus) ? [k] : [];
|
|
1072
|
+
const head = `const ${name} = (${[...names, ...position].join(", ")}) =>`;
|
|
1073
|
+
const decl = body.s.length === 0 ? [`${head} ${body.v.code};`] : [`${head} {`, ...ind(body.s), ` return ${body.v.code};`, "};"];
|
|
1074
|
+
(global ? this.run : bound.decls).push(...decl);
|
|
1075
|
+
if (!global) bound.local = true;
|
|
1076
|
+
const spec: Spec = { name, kind: body.v.kind, safe: body.v.safe, deps: body.v.deps, origin, focus, param, global };
|
|
1077
|
+
bound.specs.set(sig, spec);
|
|
1078
|
+
return spec;
|
|
885
1079
|
}
|
|
886
1080
|
|
|
887
1081
|
#boolean(v: Value): string {
|
|
@@ -900,6 +1094,8 @@ class Printer {
|
|
|
900
1094
|
if (op === "ifError") return this.#ifError(args, ctx);
|
|
901
1095
|
const [head] = args;
|
|
902
1096
|
if (LIST_OPS.has(op) && head?.tag === "each") return this.#list(op, head, args.slice(1), ctx);
|
|
1097
|
+
if (head !== undefined && (op === "sortBy" || op === "take")) return this.#order(op, head, args.slice(1), ctx);
|
|
1098
|
+
if (head !== undefined && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, args.slice(1), ctx);
|
|
903
1099
|
const raw = args.map((x) => this.expr(x, ctx));
|
|
904
1100
|
const deps = depsOf(...raw);
|
|
905
1101
|
const values = this.#settle(raw, deps, args);
|
|
@@ -1010,17 +1206,36 @@ class Printer {
|
|
|
1010
1206
|
#targetLoop(axis: Axis, at: readonly Move[], ctx: Ctx): {
|
|
1011
1207
|
readonly s: string[];
|
|
1012
1208
|
readonly j: string;
|
|
1013
|
-
|
|
1014
|
-
readonly
|
|
1209
|
+
/** The head of the loop, and the statements at the start of each item before the try and inside it. */
|
|
1210
|
+
readonly head: string;
|
|
1211
|
+
readonly key: readonly string[];
|
|
1212
|
+
readonly check: readonly string[];
|
|
1015
1213
|
readonly skip: string | undefined;
|
|
1016
1214
|
readonly fixed: boolean;
|
|
1017
1215
|
readonly start: ReadonlySet<number>;
|
|
1216
|
+
/** True when the statements before the loop cannot throw. */
|
|
1217
|
+
readonly startSafe: boolean;
|
|
1018
1218
|
readonly tests: readonly Expr[];
|
|
1019
1219
|
readonly inner: Ctx;
|
|
1020
1220
|
} {
|
|
1021
|
-
const { base, tests } = testsOf(axis);
|
|
1221
|
+
const { base: outer, tests } = testsOf(axis);
|
|
1222
|
+
// The reverse order of an axis is a loop from its end (AXIS.REVERSE). The validation keeps a base axis inside.
|
|
1223
|
+
const reverse = outer.t === "reverse";
|
|
1224
|
+
const base = outer.t === "reverse" ? outer.axis : outer;
|
|
1022
1225
|
const j = this.fresh("j");
|
|
1023
1226
|
const inner: Ctx = { ...ctx, focus: j, level: ctx.level + 1 };
|
|
1227
|
+
// The axis list goes over the array of its list (AXIS.LIST). The list evaluates at the node, before the loop, and an
|
|
1228
|
+
// error item is an item that fails.
|
|
1229
|
+
if (base.t === "list") {
|
|
1230
|
+
if (at.length > 0) throw new NotStandalone('the axis "list" with an address');
|
|
1231
|
+
const list = this.#hoist(this.#listOf(base.list, ctx), hasLoop(base.list));
|
|
1232
|
+
const a = this.fresh("A");
|
|
1233
|
+
const q = this.fresh("q");
|
|
1234
|
+
const head = reverse ? `for (let ${q} = ${a}.length - 1; ${q} >= 0; ${q}--) {` : `for (let ${q} = 0; ${q} < ${a}.length; ${q}++) {`;
|
|
1235
|
+
const key = [`const ${j} = ${a}[${q}][0];`];
|
|
1236
|
+
const check = [`if (${a}[${q}][1] === E) ${this.use("F")}();`];
|
|
1237
|
+
return { s: [...list.s, `const ${a} = ${list.v.code};`], j, head, key, check, skip: undefined, fixed: false, start: list.v.deps, startSafe: list.v.safe, tests, inner };
|
|
1238
|
+
}
|
|
1024
1239
|
const s: string[] = [];
|
|
1025
1240
|
let lo = "0";
|
|
1026
1241
|
let hi = "n";
|
|
@@ -1043,13 +1258,14 @@ class Printer {
|
|
|
1043
1258
|
s.push(`const ${h} = Math.min(n, ${f as string} + Math.min(${base.to}, n) + 1);`);
|
|
1044
1259
|
hi = h;
|
|
1045
1260
|
} else if (base.t !== "all") throw new NotStandalone(`the axis "${base.t}"`);
|
|
1046
|
-
|
|
1261
|
+
const startSafe = at.every((m) => m.t === "origin");
|
|
1262
|
+
return { s, j, head: forHead(j, lo, hi, reverse), key: [], check: [], skip, fixed: f === undefined, start, startSafe, tests, inner };
|
|
1047
1263
|
}
|
|
1048
1264
|
|
|
1049
1265
|
#list(op: string, target: ExprOf<"each">, rest: readonly Expr[], ctx: Ctx): Code {
|
|
1050
1266
|
const strict = this.#strict(op, rest);
|
|
1051
1267
|
const at = target.at ?? [];
|
|
1052
|
-
const { s, j,
|
|
1268
|
+
const { s, j, head, key, check, skip, fixed, start, startSafe, tests, inner } = this.#targetLoop(target.axis, at, ctx);
|
|
1053
1269
|
const { level } = inner;
|
|
1054
1270
|
// Inside the loop, a constant of the run evaluates once, before the loop over the keys of the run.
|
|
1055
1271
|
const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
|
|
@@ -1057,12 +1273,37 @@ class Printer {
|
|
|
1057
1273
|
// A count of all the items, whose body cannot fail, is the count of the records. A safe body covers its statements.
|
|
1058
1274
|
if (op === "count" && fixed && tests.length === 0 && body.v.safe) return bare(plainValue("n", "number"));
|
|
1059
1275
|
const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== level));
|
|
1276
|
+
const r = this.#reduction(op, body.v.kind);
|
|
1277
|
+
const { v } = r;
|
|
1278
|
+
s.push(r.init);
|
|
1279
|
+
const item = [...check, ...testCodes.flatMap((t) => [...t.s, `if (!${this.#boolean(t.v)}) continue;`]), ...body.s];
|
|
1280
|
+
// An item whose tests and body cannot fail needs no try.
|
|
1281
|
+
const safeItem = check.length === 0 && body.v.safe && testCodes.every((t) => this.#safeBoolean(t.v));
|
|
1282
|
+
const loop = [
|
|
1283
|
+
...key,
|
|
1284
|
+
...(skip === undefined ? [] : [`if (${j} === ${skip}) continue;`]),
|
|
1285
|
+
...(safeItem
|
|
1286
|
+
? [...item, `const ${v} = ${body.v.code};`]
|
|
1287
|
+
: [`let ${v};`, "try {", ...ind(item), ` ${v} = ${body.v.code};`, "} catch {", strict ? ` ${this.use("F")}();` : " continue;", "}"]),
|
|
1288
|
+
r.step,
|
|
1289
|
+
];
|
|
1290
|
+
s.push(head, ...ind(loop), "}");
|
|
1291
|
+
// A count without strict, and any, all and none over booleans without strict, cannot fail. The address of the axis
|
|
1292
|
+
// can fail, except at the focus or at the origin.
|
|
1293
|
+
const safe = startSafe && !strict && (op === "count" || (BOOLEAN_LIST_OPS.has(op) && body.v.kind === "boolean"));
|
|
1294
|
+
return { s, v: { code: r.result, kind: listKind(op, body.v.kind), safe, deps } };
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
/**
|
|
1298
|
+
* This method gives the parts of a list op that reduces the values of its items. The parts are the variable `v` of
|
|
1299
|
+
* 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.
|
|
1300
|
+
*/
|
|
1301
|
+
#reduction(op: string, kind: Kind | undefined): { readonly v: string; readonly init: string; readonly step: string; readonly result: string } {
|
|
1060
1302
|
const a = this.fresh("a");
|
|
1061
1303
|
const n = this.fresh("n");
|
|
1062
1304
|
const v = this.fresh("x");
|
|
1063
|
-
const num =
|
|
1064
|
-
const bool =
|
|
1065
|
-
const F = this.use("F");
|
|
1305
|
+
const num = kind === "number" ? v : `${this.use("U")}(${v})`;
|
|
1306
|
+
const bool = kind === "boolean" ? v : `${this.use("B")}(${v})`;
|
|
1066
1307
|
const inits: Readonly<Record<string, string>> = {
|
|
1067
1308
|
count: `let ${n} = 0;`,
|
|
1068
1309
|
sum: `let ${a} = 0;`,
|
|
@@ -1071,7 +1312,6 @@ class Printer {
|
|
|
1071
1312
|
none: `let ${a} = false;`,
|
|
1072
1313
|
all: `let ${a} = true;`,
|
|
1073
1314
|
};
|
|
1074
|
-
s.push(inits[op] ?? `let ${a}, ${n} = 0;`);
|
|
1075
1315
|
const steps: Readonly<Record<string, string>> = {
|
|
1076
1316
|
count: `${n}++;`,
|
|
1077
1317
|
sum: `${a} += ${num};`,
|
|
@@ -1083,17 +1323,7 @@ class Printer {
|
|
|
1083
1323
|
all: `if (!${bool}) ${a} = false;`,
|
|
1084
1324
|
first: `if (${n}++ === 0) ${a} = ${v};`,
|
|
1085
1325
|
};
|
|
1086
|
-
const
|
|
1087
|
-
// An item whose tests and body cannot fail needs no try.
|
|
1088
|
-
const safeItem = body.v.safe && testCodes.every((t) => this.#safeBoolean(t.v));
|
|
1089
|
-
const loop = [
|
|
1090
|
-
...(skip === undefined ? [] : [`if (${j} === ${skip}) continue;`]),
|
|
1091
|
-
...(safeItem
|
|
1092
|
-
? [...item, `const ${v} = ${body.v.code};`]
|
|
1093
|
-
: [`let ${v};`, "try {", ...ind(item), ` ${v} = ${body.v.code};`, "} catch {", strict ? ` ${F}();` : " continue;", "}"]),
|
|
1094
|
-
steps[op] as string,
|
|
1095
|
-
];
|
|
1096
|
-
s.push(`for (let ${j} = ${lo}; ${j} < ${hi}; ${j}++) {`, ...ind(loop), "}");
|
|
1326
|
+
const F = this.use("F");
|
|
1097
1327
|
const results: Readonly<Record<string, string>> = {
|
|
1098
1328
|
count: n,
|
|
1099
1329
|
sum: this.#checked("number", a),
|
|
@@ -1102,12 +1332,63 @@ class Printer {
|
|
|
1102
1332
|
all: a,
|
|
1103
1333
|
none: `!${a}`,
|
|
1104
1334
|
};
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1335
|
+
return { v, init: inits[op] ?? `let ${a}, ${n} = 0;`, step: steps[op] as string, result: results[op] ?? `(${n} === 0 ? ${F}() : ${a})` };
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
/**
|
|
1339
|
+
* This method prints a list as an array (LIST.VALUE): a pair of the key and the value of each item. A test that
|
|
1340
|
+
* fails, a body that fails and an error item of the axis list give the error sentinel as the value.
|
|
1341
|
+
*/
|
|
1342
|
+
#eachList(e: ExprOf<"each">, ctx: Ctx): Code {
|
|
1343
|
+
const at = e.at ?? [];
|
|
1344
|
+
const { s, j, head, key, check, skip, start, startSafe, tests, inner } = this.#targetLoop(e.axis, at, ctx);
|
|
1345
|
+
const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
|
|
1346
|
+
const body = this.#hoist(this.expr(e.body, inner), hasLoop(e.body));
|
|
1347
|
+
const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== inner.level));
|
|
1348
|
+
const list = this.fresh("L");
|
|
1349
|
+
const v = this.fresh("x");
|
|
1350
|
+
const item = [...check, ...testCodes.flatMap((t) => [...t.s, `if (!${this.#boolean(t.v)}) continue;`]), ...body.s];
|
|
1351
|
+
const safeItem = check.length === 0 && body.v.safe && testCodes.every((t) => this.#safeBoolean(t.v));
|
|
1352
|
+
const loop = [
|
|
1353
|
+
...key,
|
|
1354
|
+
...(skip === undefined ? [] : [`if (${j} === ${skip}) continue;`]),
|
|
1355
|
+
...(safeItem ? [...item, `${list}.push([${j}, ${body.v.code}]);`] : [`let ${v};`, "try {", ...ind(item), ` ${v} = ${body.v.code};`, "} catch {", ` ${v} = E;`, "}", `${list}.push([${j}, ${v}]);`]),
|
|
1356
|
+
];
|
|
1357
|
+
return { s: [...s, `const ${list} = [];`, head, ...ind(loop), "}"], v: { code: list, kind: undefined, safe: startSafe, deps, list: { item: body.v.kind } } };
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
/**
|
|
1361
|
+
* This method prints sortBy and take (LIST.SORT, LIST.TAKE): a new array from the array of a list. An error item is
|
|
1362
|
+
* not in the new list, and with strict it is the error of the op.
|
|
1363
|
+
*/
|
|
1364
|
+
#order(op: "sortBy" | "take", head: Expr, rest: readonly Expr[], ctx: Ctx): Code {
|
|
1365
|
+
const list = this.#listOf(head, ctx);
|
|
1366
|
+
// A missing count is the literal null, which is not a whole number, so take gives its error.
|
|
1367
|
+
const exprs = [head, ...(op === "take" ? [rest[0] ?? lit(null)] : [])];
|
|
1368
|
+
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
1369
|
+
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
1370
|
+
const deps = depsOf(...raw);
|
|
1371
|
+
const parts = this.#settle(raw, deps, exprs);
|
|
1372
|
+
const item = (list.v.list as ListInfo).item;
|
|
1373
|
+
return { s: stmtsOf(...parts), v: { code: `${this.use(op === "sortBy" ? "SB" : "TK")}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`, kind: undefined, safe: false, deps, list: { item } } };
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
/** This method prints a list op over a list that is not an each: a loop over its array, and nth (LIST.NTH). */
|
|
1377
|
+
#overList(op: string, head: Expr, rest: readonly Expr[], ctx: Ctx): Code {
|
|
1378
|
+
const list = this.#listOf(head, ctx);
|
|
1379
|
+
// A missing position is the literal null, which is not a whole number, so nth gives its error.
|
|
1380
|
+
const exprs = [head, ...(op === "nth" ? [rest[0] ?? lit(null)] : [])];
|
|
1381
|
+
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
1382
|
+
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
1383
|
+
const deps = depsOf(...raw);
|
|
1384
|
+
const parts = this.#settle(raw, deps, exprs);
|
|
1385
|
+
const [l] = parts as [Code];
|
|
1386
|
+
const item = (list.v.list as ListInfo).item;
|
|
1387
|
+
if (op === "nth") return { s: stmtsOf(...parts), v: { code: `${this.use("NT")}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`, kind: item, safe: false, deps } };
|
|
1388
|
+
const r = this.#reduction(op, item);
|
|
1389
|
+
const { v } = r;
|
|
1390
|
+
const s = [...l.s, r.init, `for (const [, ${v}] of ${l.v.code}) {`, ` if (${v} === E) ${strict ? `${this.use("F")}();` : "continue;"}`, ` ${r.step}`, "}"];
|
|
1391
|
+
return { s, v: { code: r.result, kind: listKind(op, item), safe: false, deps } };
|
|
1111
1392
|
}
|
|
1112
1393
|
|
|
1113
1394
|
/**
|
|
@@ -1186,14 +1467,15 @@ class Printer {
|
|
|
1186
1467
|
const step = this.#loopStep(e.step, { ...t.inner, env: names.env }, names.vars);
|
|
1187
1468
|
const result = this.expr(e.result, { ...ctx, env: names.env });
|
|
1188
1469
|
const body = [
|
|
1470
|
+
...t.key,
|
|
1189
1471
|
...(t.skip === undefined ? [] : [`if (${t.j} === ${t.skip}) continue;`]),
|
|
1472
|
+
...t.check,
|
|
1190
1473
|
...tests.flatMap((c) => [...c.s, `if (!${this.#boolean(c.v)}) continue;`]),
|
|
1191
1474
|
...step.s,
|
|
1192
1475
|
];
|
|
1193
1476
|
const inside = [...tests.flatMap((c) => [...c.v.deps]), ...step.deps].filter((d) => d !== loop && d !== t.inner.level);
|
|
1194
1477
|
const deps = new Set([...names.deps, ...t.start, ...inside, ...[...result.v.deps].filter((d) => d !== loop)]);
|
|
1195
|
-
|
|
1196
|
-
return { s: [...names.s, ...t.s, head, ...ind(body), "}", ...result.s], v: { code: result.v.code, kind: result.v.kind, safe: false, deps } };
|
|
1478
|
+
return { s: [...names.s, ...t.s, t.head, ...ind(body), "}", ...result.s], v: { code: result.v.code, kind: result.v.kind, safe: false, deps } };
|
|
1197
1479
|
}
|
|
1198
1480
|
|
|
1199
1481
|
/**
|
|
@@ -1214,7 +1496,7 @@ class Printer {
|
|
|
1214
1496
|
);
|
|
1215
1497
|
const outer = this.#inside;
|
|
1216
1498
|
this.#inside = e;
|
|
1217
|
-
const bodyCtx: Ctx = { origin: undefined, focus: undefined, level: 1, env: new Map(), keys: names };
|
|
1499
|
+
const bodyCtx: Ctx = { origin: undefined, focus: undefined, level: 1, originLevel: 1, env: new Map(), fns: new Map(), keys: names };
|
|
1218
1500
|
if (varying.length === 0) {
|
|
1219
1501
|
const body = this.#hoist(this.expr(e, bodyCtx));
|
|
1220
1502
|
this.#inside = outer;
|
|
@@ -1237,6 +1519,7 @@ class Printer {
|
|
|
1237
1519
|
// The loop of the node gives statements.
|
|
1238
1520
|
const body = this.expr(e, bodyCtx);
|
|
1239
1521
|
this.#kinds.set(fn, body.v.kind);
|
|
1522
|
+
this.#lists.set(fn, body.v.list);
|
|
1240
1523
|
this.run.push(`const ${fn} = ${this.use(params.length === 1 ? "M1" : "M")}((${params.join(", ")}) => {`, ...ind(body.s), ` return ${body.v.code};`, "});");
|
|
1241
1524
|
}
|
|
1242
1525
|
}
|
|
@@ -1251,7 +1534,7 @@ class Printer {
|
|
|
1251
1534
|
s.push(...this.#store(t, c));
|
|
1252
1535
|
return t;
|
|
1253
1536
|
});
|
|
1254
|
-
return { s, v: { code: `${fn}(${args.join(", ")})`, kind: this.#kinds.get(fn), safe: false, deps: depsOf(...values) } };
|
|
1537
|
+
return { s, v: { code: `${fn}(${args.join(", ")})`, kind: this.#kinds.get(fn), safe: false, deps: depsOf(...values), list: this.#lists.get(fn) } };
|
|
1255
1538
|
}
|
|
1256
1539
|
|
|
1257
1540
|
/**
|
|
@@ -1338,7 +1621,7 @@ class Printer {
|
|
|
1338
1621
|
|
|
1339
1622
|
/** This method gives the code of the program at the key of the run. A constant of the run evaluates once. */
|
|
1340
1623
|
root(expr: Expr): Code {
|
|
1341
|
-
const c = this.#hoist(this.expr(expr, { origin: "o", focus: "o", level: 1, env: new Map(), keys: new Map() }));
|
|
1624
|
+
const c = this.#hoist(this.expr(expr, { origin: "o", focus: "o", level: 1, originLevel: 1, env: new Map(), fns: new Map(), keys: new Map() }));
|
|
1342
1625
|
// A function of an op with an inline form at each of its sites needs no declaration.
|
|
1343
1626
|
for (const [name, form] of this.#forms) if (form !== undefined && !this.#called.has(name)) this.top.delete(name);
|
|
1344
1627
|
return c;
|
|
@@ -1354,7 +1637,10 @@ class Printer {
|
|
|
1354
1637
|
return canonical(e, (x) => {
|
|
1355
1638
|
const key = ctx.keys.get(x as Expr);
|
|
1356
1639
|
if (key !== undefined) return { "@": key.code };
|
|
1357
|
-
|
|
1640
|
+
const node = x as Expr | undefined;
|
|
1641
|
+
if (node?.tag === "var") return { $: ctx.env.get(node.name)?.code ?? node.name };
|
|
1642
|
+
// A call gives the number of the binding of its function, so two functions with one name give two keys.
|
|
1643
|
+
return node?.tag === "call" ? { "()": node.fn, of: ctx.fns.get(node.fn)?.id ?? null, args: node.args } : undefined;
|
|
1358
1644
|
});
|
|
1359
1645
|
}
|
|
1360
1646
|
}
|