@mark1russell7/vex-compiler 0.13.0 → 0.13.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 +225 -38
- package/build/index.js.map +1 -1
- package/package.json +4 -4
- package/src/module.ts +226 -40
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/vex-compiler",
|
|
4
|
-
"version": "0.13.
|
|
4
|
+
"version": "0.13.1",
|
|
5
5
|
"description": "The Vex compiler: generated JavaScript for Vex programs, at run time and at build time, with the results of the interpreter.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"vex",
|
|
@@ -43,15 +43,15 @@
|
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@mark1russell7/cue": "github:mark1russell7/cue",
|
|
46
|
-
"@mark1russell7/vex-domains": "0.13.
|
|
47
|
-
"@mark1russell7/vex-testkit": "0.13.
|
|
46
|
+
"@mark1russell7/vex-domains": "0.13.1",
|
|
47
|
+
"@mark1russell7/vex-testkit": "0.13.1",
|
|
48
48
|
"@types/node": "^26.6.4",
|
|
49
49
|
"fast-check": "^4.10.2",
|
|
50
50
|
"typescript": "^7.0.2",
|
|
51
51
|
"vitest": "^5.0.3"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@mark1russell7/vex": "0.13.
|
|
54
|
+
"@mark1russell7/vex": "0.13.1"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "tsdown",
|
package/src/module.ts
CHANGED
|
@@ -17,14 +17,15 @@
|
|
|
17
17
|
*
|
|
18
18
|
* The printer takes a part of the language. It takes an array space or a tree space of records, and the fields and
|
|
19
19
|
* the variables of the schema. It takes the axes `all`, `others` and `window`. A tree space also has the move `parent` and the
|
|
20
|
-
* axes of a tree (COMPILE.MODULE.TREE).
|
|
21
|
-
*
|
|
20
|
+
* axes of a tree (COMPILE.MODULE.TREE). A list is an array of pairs of a key and a value. The list ops, list literals
|
|
21
|
+
* and lists as names of loops are in a module (COMPILE.MODULE.LIST). The kinds of each op site must decide its op.
|
|
22
|
+
* Another construct gives a reason, not code.
|
|
22
23
|
*
|
|
23
24
|
* `emitSheetModule` prints the formula columns of a sheet as one module (COMPILE.MODULE.SHEET). Each column is an array
|
|
24
25
|
* of results, and the run fills the arrays in the static order of `SheetRun.fill`. A read of a cell that is not finished
|
|
25
26
|
* starts the search of `SheetRun`. Thus each cell on a cycle gives the error of Vex.
|
|
26
27
|
*/
|
|
27
|
-
import { addrExprs, CELL_KIND, children, fromDocument, isDocument, isFieldName, lit, moveExpr, nestingOf, rec, shapeError, type AnyDomain, type Axis, type CellFormulas, type Envelope, type Expr, type ExprOf, type Move, type Names, type PrimitiveKind } from "@mark1russell7/vex";
|
|
28
|
+
import { addrExprs, app, CELL_KIND, children, fromDocument, isDocument, isFieldName, isVexList, lit, moveExpr, nestingOf, rec, shapeError, type AnyDomain, type Axis, type CellFormulas, type Envelope, type Expr, type ExprOf, type Move, type Names, type PrimitiveKind, type VexList } from "@mark1russell7/vex";
|
|
28
29
|
import { factsOf, kindOfDomain, primitiveOf, type Facts, type Kind, type Schema } from "./facts.ts";
|
|
29
30
|
import { memoNodes } from "./memo.ts";
|
|
30
31
|
import { columnOrder, isCellRef, keyOrderOf, readsOf } from "./sheet-order.ts";
|
|
@@ -87,9 +88,18 @@ interface Value {
|
|
|
87
88
|
readonly list?: ListInfo | undefined;
|
|
88
89
|
}
|
|
89
90
|
|
|
90
|
-
/**
|
|
91
|
+
/**
|
|
92
|
+
* The facts of a list of the code: the kind of the values of its items, if the printer knows it. The key of an item is
|
|
93
|
+
* the position of its record, or the text of the key of a list literal (LIST.LITERAL).
|
|
94
|
+
*/
|
|
91
95
|
interface ListInfo {
|
|
92
96
|
readonly item: Kind | undefined;
|
|
97
|
+
/** For a list of lists (LIST.FLAT): the facts of each list that is the value of an item. Each item of such a list is an array. */
|
|
98
|
+
readonly inner?: ListInfo | undefined;
|
|
99
|
+
/** True when a key can be the text of a key of a literal. The focus of the axis list must be the position of a record. */
|
|
100
|
+
readonly texts?: boolean;
|
|
101
|
+
/** True for a list without items: the empty literal, and a list from it. Such a list fits each list. */
|
|
102
|
+
readonly empty?: boolean;
|
|
93
103
|
}
|
|
94
104
|
|
|
95
105
|
/** The code of a node: the statements before its value, and its value. `safe` covers the statements too. */
|
|
@@ -216,7 +226,10 @@ interface Spec {
|
|
|
216
226
|
}
|
|
217
227
|
|
|
218
228
|
/** The name of a helper of the generated code. */
|
|
219
|
-
type Helper = "F" | "C" | "T" | "L" | "Q" | "G" | "B" | "U" | "X" | "I" | "K" | "M" | "M1" | "S" | "V" | "SB" | "TK" | "NT" | TreeHelper | SheetHelper;
|
|
229
|
+
type Helper = "F" | "C" | "T" | "L" | "Q" | "G" | "B" | "U" | "X" | "I" | "K" | "M" | "M1" | "S" | "V" | "SB" | "TK" | "NT" | BuildHelper | TreeHelper | SheetHelper;
|
|
230
|
+
|
|
231
|
+
/** The helpers of the list ops that build a list (LIST.BUILD), of `get` (LIST.GET) and of `indexOf` (LIST.INDEXOF). */
|
|
232
|
+
type BuildHelper = "DR" | "AP" | "WO" | "FL" | "GT" | "IX";
|
|
220
233
|
|
|
221
234
|
/** The helpers of a tree space (COMPILE.MODULE.TREE). */
|
|
222
235
|
type TreeHelper = "PT" | "CH" | "SG" | "PR" | "UP" | "SO" | "AN" | "BF";
|
|
@@ -255,6 +268,50 @@ const HELPERS: Readonly<Record<Exclude<Helper, "S">, string>> = {
|
|
|
255
268
|
].join("\n"),
|
|
256
269
|
TK: "function TK(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(0, n) : F(); }",
|
|
257
270
|
NT: "function NT(l, i, s) { const o = V(l, s); return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F(); }",
|
|
271
|
+
// The ops that build a list (LIST.BUILD) keep the key and the value of each item. A key is a position or a text, so
|
|
272
|
+
// `without` and `get` compare the text of each key. A list is an array that does not change after the op that makes it.
|
|
273
|
+
DR: "function DR(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(n) : F(); }",
|
|
274
|
+
AP: "function AP(l, o, s) { return V(l, s).concat(V(o, s)); }",
|
|
275
|
+
WO: [
|
|
276
|
+
"function WO(l, o, s) {",
|
|
277
|
+
" const a = V(l, s), g = new Map();",
|
|
278
|
+
" for (const [k] of V(o, s)) g.set(`${k}`, 1);",
|
|
279
|
+
" return a.filter((x) => !g.has(`${x[0]}`));",
|
|
280
|
+
"}",
|
|
281
|
+
].join("\n"),
|
|
282
|
+
FL: [
|
|
283
|
+
"function FL(l, s) {",
|
|
284
|
+
" const o = [];",
|
|
285
|
+
" for (const [, v] of V(l, s)) for (const x of V(v, s)) o.push(x);",
|
|
286
|
+
" return o;",
|
|
287
|
+
"}",
|
|
288
|
+
].join("\n"),
|
|
289
|
+
// The index of the keys of a list (LIST.GET): the value of the first item of each key. The array keeps its index, so a
|
|
290
|
+
// step that reads each key of a list of n items costs O(n). The key E marks an error item, for strict.
|
|
291
|
+
GT: [
|
|
292
|
+
"function GT(l, k, s) {",
|
|
293
|
+
" let x = l.ix;",
|
|
294
|
+
" if (x === undefined) {",
|
|
295
|
+
" x = new Map();",
|
|
296
|
+
" for (const [q, v] of l) {",
|
|
297
|
+
" const t = `${q}`;",
|
|
298
|
+
" if (v === E) x.set(E, E);",
|
|
299
|
+
" else if (!x.has(t)) x.set(t, v);",
|
|
300
|
+
" }",
|
|
301
|
+
" l.ix = x;",
|
|
302
|
+
" }",
|
|
303
|
+
' return (s && x.has(E)) || typeof k !== "string" || !x.has(k) ? F() : x.get(k);',
|
|
304
|
+
"}",
|
|
305
|
+
].join("\n"),
|
|
306
|
+
// The position of the first value among the items with a value (LIST.INDEXOF). `===` compares the values.
|
|
307
|
+
IX: [
|
|
308
|
+
"function IX(l, x, s) {",
|
|
309
|
+
" const o = V(l, s);",
|
|
310
|
+
' if (typeof x !== "number" && typeof x !== "string" && typeof x !== "boolean") F();',
|
|
311
|
+
" for (let i = 0; i < o.length; i++) if (o[i][1] === x) return i;",
|
|
312
|
+
" return F();",
|
|
313
|
+
"}",
|
|
314
|
+
].join("\n"),
|
|
258
315
|
// The parents of a tree space: the position of the parent of each record, or -1 for a root. A walk up from each record
|
|
259
316
|
// marks its keys, so a walk that meets a key of the same walk finds a cycle. Each key is in one walk, so the check is linear.
|
|
260
317
|
PT: [
|
|
@@ -608,10 +665,11 @@ const STEPS: Readonly<Record<string, readonly [string, string]>> = {
|
|
|
608
665
|
/** The helpers that each helper uses. */
|
|
609
666
|
const NEEDS: Readonly<Record<Helper, readonly Helper[]>> = {
|
|
610
667
|
F: [], C: [], T: [], L: [], Q: [], G: ["F"], B: ["F"], U: ["F"], X: ["F"], I: ["F"], K: ["F"], M: ["G"], M1: ["G"], S: ["M1", "F"],
|
|
611
|
-
// Only
|
|
668
|
+
// Only the helpers of the list ops use V, and each of them needs F too, so the need of V only completes the list.
|
|
612
669
|
// Stryker disable next-line ArrayDeclaration: see the comment above
|
|
613
670
|
V: ["F"],
|
|
614
671
|
SB: ["V", "F"], TK: ["V", "F"], NT: ["V", "F"],
|
|
672
|
+
DR: ["V", "F"], AP: ["V"], WO: ["V"], FL: ["V"], GT: ["F"], IX: ["V", "F"],
|
|
615
673
|
// Each module has C, and each move uses F (#move), so these needs only complete the list.
|
|
616
674
|
// Stryker disable next-line ArrayDeclaration: see the comment above
|
|
617
675
|
PT: ["C"], UP: ["F"], SO: ["F"],
|
|
@@ -654,8 +712,73 @@ interface TreeRange {
|
|
|
654
712
|
const LIST_OPS: ReadonlySet<string> = new Set(["count", "sum", "mean", "min", "max", "any", "all", "none", "first"]);
|
|
655
713
|
const NUMBER_LIST_OPS: ReadonlySet<string> = new Set(["count", "sum", "mean", "min", "max"]);
|
|
656
714
|
const BOOLEAN_LIST_OPS: ReadonlySet<string> = new Set(["any", "all", "none"]);
|
|
657
|
-
/** The list ops
|
|
658
|
-
const BUILD_OPS: ReadonlySet<string> = new Set(["
|
|
715
|
+
/** The list ops over lists of `#build`: append, without and flat (LIST.BUILD), get (LIST.GET) and indexOf (LIST.INDEXOF). */
|
|
716
|
+
const BUILD_OPS: ReadonlySet<string> = new Set(["append", "without", "flat", "get", "indexOf"]);
|
|
717
|
+
|
|
718
|
+
/** The helper of each op of `#build`. */
|
|
719
|
+
const BUILD_HELPERS: Readonly<Record<string, BuildHelper>> = { append: "AP", without: "WO", flat: "FL", get: "GT", indexOf: "IX" };
|
|
720
|
+
|
|
721
|
+
/** The list ops whose value is a list (LIST.SORT, LIST.TAKE and LIST.BUILD). */
|
|
722
|
+
const LIST_RESULTS: ReadonlySet<string> = new Set(["sortBy", "take", "drop", "append", "without", "flat"]);
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* This function tells if an expression gives a list. An each, a list literal and a list op of `LIST_RESULTS` give a
|
|
726
|
+
* list. A name of a list, and a let or a loop whose result is a list, give a list too. `isName` tells if a name outside the expression holds a list.
|
|
727
|
+
*/
|
|
728
|
+
function listy(e: Expr, isName: (name: string) => boolean): boolean {
|
|
729
|
+
switch (e.tag) {
|
|
730
|
+
case "each":
|
|
731
|
+
return true;
|
|
732
|
+
case "lit":
|
|
733
|
+
return isVexList(e.value);
|
|
734
|
+
case "app":
|
|
735
|
+
return LIST_RESULTS.has(e.op);
|
|
736
|
+
case "var":
|
|
737
|
+
return isName(e.name);
|
|
738
|
+
case "let":
|
|
739
|
+
return listy(e.body, namesIn(e.bind, isName));
|
|
740
|
+
case "fold":
|
|
741
|
+
case "iterate":
|
|
742
|
+
return listy(e.result, namesIn(e.init, isName));
|
|
743
|
+
case "call":
|
|
744
|
+
case "ext":
|
|
745
|
+
case "fn":
|
|
746
|
+
case "rec":
|
|
747
|
+
case "ref":
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/** This function tells if a name holds a list in the scope of some bindings. A binding hides a name outside, and a function is not a list. */
|
|
753
|
+
function namesIn(bind: Readonly<Record<string, Expr>>, isName: (name: string) => boolean): (name: string) => boolean {
|
|
754
|
+
const lists = new Map(Object.entries(bind).map(([name, x]) => [name, x.tag !== "fn" && listy(x, isName)]));
|
|
755
|
+
return (name) => lists.get(name) ?? isName(name);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/** This function tells if an expression has a list literal with an item, so a key of the list can be a text (LIST.LITERAL). */
|
|
759
|
+
function hasKeyedLiteral(e: Expr): boolean {
|
|
760
|
+
return (e.tag === "lit" && isVexList(e.value) && e.value.items.length > 0) || children(e).some(hasKeyedLiteral);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/** This function gives the facts of the list of `append` from the facts of its two lists. An empty list fits each list. */
|
|
764
|
+
function joinLists(a: ListInfo, b: ListInfo): ListInfo {
|
|
765
|
+
if (a.empty === true) return b;
|
|
766
|
+
if (b.empty === true) return a;
|
|
767
|
+
if ((a.inner === undefined) !== (b.inner === undefined)) throw new NotStandalone('"append" of a list of lists and a list of values');
|
|
768
|
+
const texts = a.texts === true || b.texts === true;
|
|
769
|
+
return { item: join(a.item, b.item), inner: a.inner === undefined ? undefined : joinLists(a.inner, b.inner as ListInfo), texts };
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* This function tells if a list of a step fits the list of a name of a loop (LOOP.LIST). The items of the step have the
|
|
774
|
+
* kind of the items of the name, if the name has one. The name also has the text keys and the lists of lists of the step.
|
|
775
|
+
*/
|
|
776
|
+
function fits(name: ListInfo, step: ListInfo): boolean {
|
|
777
|
+
if (step.empty === true) return true;
|
|
778
|
+
if (name.item !== undefined && step.item !== name.item) return false;
|
|
779
|
+
if (step.texts === true && name.texts !== true) return false;
|
|
780
|
+
return name.inner === undefined ? step.inner === undefined : step.inner !== undefined && fits(name.inner, step.inner);
|
|
781
|
+
}
|
|
659
782
|
|
|
660
783
|
/** This function tells if the step of a list op throws for a value of the kind `kind`, which is the wrong kind. */
|
|
661
784
|
const stepThrows = (op: string, kind: Kind | undefined): boolean => (NUMBER_STEPS.has(op) && kind !== "number") || (BOOLEAN_LIST_OPS.has(op) && kind !== "boolean");
|
|
@@ -1159,11 +1282,18 @@ class Printer {
|
|
|
1159
1282
|
return c;
|
|
1160
1283
|
}
|
|
1161
1284
|
|
|
1162
|
-
/** This method tells if an expression gives a list at a place that takes one
|
|
1285
|
+
/** This method tells if an expression gives a list at a place that takes one (`listy`). */
|
|
1163
1286
|
#isList(e: Expr, ctx: Ctx): boolean {
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1287
|
+
return listy(e, (name) => ctx.env.get(name)?.list !== undefined);
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
/** This method prints a list literal (LIST.LITERAL) as an array of pairs of a key and a value. An error item holds the sentinel. */
|
|
1291
|
+
#listLiteral(l: VexList): Code {
|
|
1292
|
+
const values = l.items.map((it) => (it.result.ok ? literal(it.result.value) : undefined));
|
|
1293
|
+
const kinds = values.flatMap((x) => (x === undefined ? [] : [x.kind]));
|
|
1294
|
+
const code = `[${l.items.map((it, i) => `[${JSON.stringify(it.key)}, ${values[i]?.code ?? "E"}]`).join(", ")}]`;
|
|
1295
|
+
const has = l.items.length > 0;
|
|
1296
|
+
return bare({ code, kind: undefined, safe: true, deps: NO_DEPS, list: { item: kinds.reduce(join, kinds[0]), texts: has, empty: !has } });
|
|
1167
1297
|
}
|
|
1168
1298
|
|
|
1169
1299
|
/**
|
|
@@ -1256,7 +1386,7 @@ class Printer {
|
|
|
1256
1386
|
#node(e: Expr, ctx: Ctx): Code {
|
|
1257
1387
|
switch (e.tag) {
|
|
1258
1388
|
case "lit":
|
|
1259
|
-
return bare(literal(e.value));
|
|
1389
|
+
return isVexList(e.value) ? this.#listLiteral(e.value) : bare(literal(e.value));
|
|
1260
1390
|
case "ref":
|
|
1261
1391
|
return this.#ref(e, ctx);
|
|
1262
1392
|
case "var":
|
|
@@ -1466,7 +1596,7 @@ class Printer {
|
|
|
1466
1596
|
* A binding is a variable. A binding with an error is an error only where the body reads its name, so the variable
|
|
1467
1597
|
* can hold the sentinel. A binding that is a constant of the run is the variable of that constant.
|
|
1468
1598
|
*/
|
|
1469
|
-
#let(e: ExprOf<"let">, ctx: Ctx): Code {
|
|
1599
|
+
#let(e: ExprOf<"let">, ctx: Ctx, wrap?: (body: Expr) => Expr): Code {
|
|
1470
1600
|
const env = new Map(ctx.env);
|
|
1471
1601
|
const fns = new Map(ctx.fns);
|
|
1472
1602
|
const s: string[] = [];
|
|
@@ -1501,8 +1631,12 @@ class Printer {
|
|
|
1501
1631
|
// The statements of the binding read its keys, so the let is not a constant of the run.
|
|
1502
1632
|
deps = new Set([...deps, ...c.v.deps]);
|
|
1503
1633
|
}
|
|
1504
|
-
|
|
1505
|
-
const
|
|
1634
|
+
// A list op over a let is the list op over its body, in the scope of its bindings (`#app`).
|
|
1635
|
+
const inner = wrap === undefined ? e.body : wrap(e.body);
|
|
1636
|
+
// A let at a place that takes a list gives its body there (LIST.VALUE).
|
|
1637
|
+
if (this.#wantList.has(e)) this.#wantList.add(inner);
|
|
1638
|
+
if (root !== undefined) root.root = inner;
|
|
1639
|
+
const body = this.expr(inner, { ...ctx, env, fns });
|
|
1506
1640
|
// A function that reads names of the place of the binding comes after them, before the body.
|
|
1507
1641
|
return { s: [...s, ...bound.flatMap((b) => b.decls), ...body.s], v: { ...body.v, deps: new Set([...deps, ...body.v.deps]) } };
|
|
1508
1642
|
}
|
|
@@ -1593,11 +1727,16 @@ class Printer {
|
|
|
1593
1727
|
if (op === "and" || op === "or") return this.#andOr(op === "and", args, ctx);
|
|
1594
1728
|
if (op === "ifError") return this.#ifError(args, ctx);
|
|
1595
1729
|
const [head] = args;
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1730
|
+
const rest = args.slice(1);
|
|
1731
|
+
// An each whose body is a list gives a list of lists (LIST.FLAT), so a list op over it goes over its array.
|
|
1732
|
+
if (LIST_OPS.has(op) && head?.tag === "each" && !this.#isList(head.body, ctx)) return this.#list(op, head, rest, ctx);
|
|
1733
|
+
// A list op over a let with only literal options is the list op over the body of the let, so an each in the body
|
|
1734
|
+
// is one loop with a running value, not an array. A builder sheet prints such a let, for example `sum(let($0 = 0, each(…)))`.
|
|
1735
|
+
if (LIST_OPS.has(op) && head?.tag === "let" && rest.every((x) => x.tag === "lit")) return this.#let(head, ctx, (body) => app(op, body, ...rest));
|
|
1736
|
+
// sortBy and take are list ops for each receiver. Each other list op needs a list, and a domain can have its name.
|
|
1737
|
+
if (head !== undefined && (op === "sortBy" || op === "take" || (op === "drop" && this.#isList(head, ctx)))) return this.#order(op, head, rest, ctx);
|
|
1738
|
+
if (head !== undefined && BUILD_OPS.has(op) && this.#isList(head, ctx)) return this.#build(op, head, rest, ctx);
|
|
1739
|
+
if (head !== undefined && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, rest, ctx);
|
|
1601
1740
|
const raw = args.map((x) => this.expr(x, ctx));
|
|
1602
1741
|
const deps = depsOf(...raw);
|
|
1603
1742
|
// The interpreter evaluates each argument, also after an error.
|
|
@@ -1732,6 +1871,8 @@ class Printer {
|
|
|
1732
1871
|
if (base.t === "list") {
|
|
1733
1872
|
if (at.length > 0) throw new NotStandalone('the axis "list" with an address');
|
|
1734
1873
|
const list = this.#hoist(this.#listOf(base.list, ctx), hasLoop(base.list));
|
|
1874
|
+
// The focus of a target is the position of a record, so a list with the keys of a literal has no loop of a module.
|
|
1875
|
+
if ((list.v.list as ListInfo).texts === true) throw new NotStandalone('the axis "list" over a list with the keys of a literal');
|
|
1735
1876
|
const a = this.fresh("A");
|
|
1736
1877
|
const q = this.fresh("q");
|
|
1737
1878
|
const head = reverse ? `for (let ${q} = ${a}.length - 1; ${q} >= 0; ${q}--) {` : `for (let ${q} = 0; ${q} < ${a}.length; ${q}++) {`;
|
|
@@ -1900,6 +2041,8 @@ class Printer {
|
|
|
1900
2041
|
const at = e.at ?? [];
|
|
1901
2042
|
const { s, j, head, key, check, skip, start, startSafe, tests, inner } = this.#targetLoop(e.axis, at, ctx);
|
|
1902
2043
|
const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
|
|
2044
|
+
// The value of an item can be a list, for flat (LIST.FLAT). The facts of the list tell it.
|
|
2045
|
+
if (this.#wantList.has(e)) this.#wantList.add(e.body);
|
|
1903
2046
|
const body = this.#hoist(this.expr(e.body, inner), hasLoop(e.body));
|
|
1904
2047
|
const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== inner.level));
|
|
1905
2048
|
const list = this.fresh("L");
|
|
@@ -1911,23 +2054,54 @@ class Printer {
|
|
|
1911
2054
|
...(skip === undefined ? [] : [`if (${j} === ${skip}) continue;`]),
|
|
1912
2055
|
...(safeItem ? [...item, `${list}.push([${j}, ${body.v.code}]);`] : [`let ${v};`, "try {", ...ind(item), ` ${v} = ${body.v.code};`, "} catch {", ` ${v} = E;`, "}", `${list}.push([${j}, ${v}]);`]),
|
|
1913
2056
|
];
|
|
1914
|
-
return { s: [...s, `const ${list} = [];`, head, ...ind(loop), "}"], v: { code: list, kind: undefined, safe: startSafe, deps, list: { item: body.v.kind } } };
|
|
2057
|
+
return { s: [...s, `const ${list} = [];`, head, ...ind(loop), "}"], v: { code: list, kind: undefined, safe: startSafe, deps, list: { item: body.v.kind, inner: body.v.list } } };
|
|
1915
2058
|
}
|
|
1916
2059
|
|
|
1917
2060
|
/**
|
|
1918
|
-
* This method prints sortBy and
|
|
1919
|
-
* not in the new list, and with strict it is the error of the op.
|
|
2061
|
+
* This method prints sortBy, take and drop (LIST.SORT, LIST.TAKE, LIST.DROP): a new array from the array of a list. An
|
|
2062
|
+
* error item is not in the new list, and with strict it is the error of the op.
|
|
1920
2063
|
*/
|
|
1921
|
-
#order(op: "sortBy" | "take", head: Expr, rest: readonly Expr[], ctx: Ctx): Code {
|
|
2064
|
+
#order(op: "sortBy" | "take" | "drop", head: Expr, rest: readonly Expr[], ctx: Ctx): Code {
|
|
1922
2065
|
const list = this.#listOf(head, ctx);
|
|
1923
|
-
// A missing count is the literal null, which is not a whole number, so take
|
|
1924
|
-
const exprs = [head, ...(op === "
|
|
2066
|
+
// A missing count is the literal null, which is not a whole number, so take and drop give their error.
|
|
2067
|
+
const exprs = [head, ...(op === "sortBy" ? [] : [rest[0] ?? lit(null)])];
|
|
1925
2068
|
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
1926
2069
|
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
1927
2070
|
const deps = depsOf(...raw);
|
|
1928
2071
|
const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
|
|
1929
|
-
const
|
|
1930
|
-
|
|
2072
|
+
const helper = op === "sortBy" ? "SB" : op === "take" ? "TK" : "DR";
|
|
2073
|
+
// The new list has some items of the list, so it keeps the facts of the list.
|
|
2074
|
+
return { s: stmtsOf(...parts), v: { code: `${this.use(helper)}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`, kind: undefined, safe: false, deps, list: list.v.list } };
|
|
2075
|
+
}
|
|
2076
|
+
|
|
2077
|
+
/**
|
|
2078
|
+
* This method prints append, without and flat (LIST.BUILD), get (LIST.GET) and indexOf (LIST.INDEXOF) over the array
|
|
2079
|
+
* of a list. Each list that the op reads follows LIST.LENIENT, as in the interpreter.
|
|
2080
|
+
*/
|
|
2081
|
+
#build(op: string, head: Expr, rest: readonly Expr[], ctx: Ctx): Code {
|
|
2082
|
+
const list = this.#listOf(head, ctx);
|
|
2083
|
+
const info = list.v.list as ListInfo;
|
|
2084
|
+
const two = op === "append" || op === "without";
|
|
2085
|
+
// A missing key of get, or value of indexOf, is the literal null. It is not a text or a value of indexOf, so the op
|
|
2086
|
+
// gives its error. A missing second list of append and without is not a list, so it gives a reason.
|
|
2087
|
+
const exprs = [head, ...(op === "flat" ? [] : [rest[0] ?? lit(null)])];
|
|
2088
|
+
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
2089
|
+
const raw = [list, ...exprs.slice(1).map((x) => (two ? this.#listOf(x, ctx) : this.expr(x, ctx)))];
|
|
2090
|
+
const deps = depsOf(...raw);
|
|
2091
|
+
const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
|
|
2092
|
+
const helper = BUILD_HELPERS[op] as BuildHelper;
|
|
2093
|
+
const value = (kind: Kind | undefined, facts: ListInfo | undefined): Code => ({
|
|
2094
|
+
s: stmtsOf(...parts),
|
|
2095
|
+
v: { code: `${this.use(helper)}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`, kind, safe: false, deps, list: facts },
|
|
2096
|
+
});
|
|
2097
|
+
if (op === "get") return value(info.item, info.inner);
|
|
2098
|
+
if (op === "indexOf") return value("number", undefined);
|
|
2099
|
+
if (op === "without") return value(undefined, info);
|
|
2100
|
+
if (op === "append") return value(undefined, joinLists(info, (parts[1] as Code).v.list as ListInfo));
|
|
2101
|
+
// flat: the value of each item must be a list, so a list of values gives a reason. An empty list gives an empty list.
|
|
2102
|
+
if (info.empty === true) return value(undefined, info);
|
|
2103
|
+
if (info.inner === undefined) throw new NotStandalone('"flat" over a list whose items are not lists');
|
|
2104
|
+
return value(undefined, info.inner);
|
|
1931
2105
|
}
|
|
1932
2106
|
|
|
1933
2107
|
/** This method prints a list op over a list that is not an each: a loop over its array, and nth (LIST.NTH). */
|
|
@@ -1940,12 +2114,13 @@ class Printer {
|
|
|
1940
2114
|
const deps = depsOf(...raw);
|
|
1941
2115
|
const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
|
|
1942
2116
|
const [l] = parts as [Code];
|
|
1943
|
-
const item =
|
|
1944
|
-
|
|
2117
|
+
const { item, inner } = list.v.list as ListInfo;
|
|
2118
|
+
// The value of an item of a list of lists is a list, so nth and first give a list (LIST.FLAT).
|
|
2119
|
+
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, list: inner } };
|
|
1945
2120
|
const r = this.#reduction(op, item);
|
|
1946
2121
|
const { v } = r;
|
|
1947
2122
|
const s = [...l.s, r.init, `for (const [, ${v}] of ${l.v.code}) {`, ` if (${v} === E) ${strict ? `${this.use("F")}();` : "continue;"}`, ` ${r.step}`, "}"];
|
|
1948
|
-
return { s, v: { code: r.result, kind: listKind(op, item), safe: false, deps } };
|
|
2123
|
+
return { s, v: { code: r.result, kind: listKind(op, item), safe: false, deps, list: op === "first" ? inner : undefined } };
|
|
1949
2124
|
}
|
|
1950
2125
|
|
|
1951
2126
|
/**
|
|
@@ -1953,17 +2128,23 @@ class Printer {
|
|
|
1953
2128
|
* node. Inside the loop, each name has the level `loop`. Thus a part that reads a name stays in the loop. A loop that
|
|
1954
2129
|
* reads no key of the run is a constant of the run.
|
|
1955
2130
|
*/
|
|
1956
|
-
#loopNames(init: Names, ctx: Ctx, loop: number): { readonly s: string[]; readonly env: Map<string, Name>; readonly vars: ReadonlyMap<string, Name>; readonly deps: Set<number> } {
|
|
2131
|
+
#loopNames(init: Names, step: Names, ctx: Ctx, loop: number): { readonly s: string[]; readonly env: Map<string, Name>; readonly vars: ReadonlyMap<string, Name>; readonly deps: Set<number> } {
|
|
1957
2132
|
const s: string[] = [];
|
|
1958
2133
|
const env = new Map(ctx.env);
|
|
1959
2134
|
const vars = new Map<string, Name>();
|
|
1960
2135
|
const deps = new Set<number>();
|
|
2136
|
+
// A step with a list literal can give a name the keys of the literal (LOOP.LIST).
|
|
2137
|
+
const keyed = Object.values(step).some(hasKeyedLiteral);
|
|
1961
2138
|
for (const [name, x] of Object.entries(init)) {
|
|
2139
|
+
// A name of a loop can hold a list (LOOP.LIST). Its first list fixes the kind of the items, if it has one.
|
|
2140
|
+
this.#wantList.add(x);
|
|
1962
2141
|
const c = this.expr(x, ctx);
|
|
1963
|
-
|
|
2142
|
+
const facts = c.v.list;
|
|
2143
|
+
if (c.v.kind === undefined && facts === undefined) throw new NotStandalone(`the first value of the name "${name}" of a loop has no kind`);
|
|
1964
2144
|
const v = this.fresh("l");
|
|
1965
2145
|
s.push(...c.s, `let ${v} = ${c.v.code};`);
|
|
1966
|
-
const
|
|
2146
|
+
const list = facts === undefined ? undefined : { item: facts.item, inner: facts.inner, texts: facts.texts === true || keyed };
|
|
2147
|
+
const read: Name = { code: v, kind: c.v.kind, lazy: false, deps: new Set([loop]), list };
|
|
1967
2148
|
env.set(name, read);
|
|
1968
2149
|
vars.set(name, read);
|
|
1969
2150
|
for (const d of c.v.deps) deps.add(d);
|
|
@@ -1977,9 +2158,11 @@ class Printer {
|
|
|
1977
2158
|
const sets: string[] = [];
|
|
1978
2159
|
const deps = new Set<number>();
|
|
1979
2160
|
for (const [name, x] of Object.entries(step)) {
|
|
2161
|
+
this.#wantList.add(x);
|
|
1980
2162
|
const c = this.#hoist(this.expr(x, inner), hasLoop(x));
|
|
1981
2163
|
const v = vars.get(name) as Name;
|
|
1982
|
-
if (c.v.kind !== v.kind) throw new NotStandalone(`a step of a loop changes the kind of the name "${name}"`);
|
|
2164
|
+
if (c.v.kind !== v.kind || (c.v.list === undefined) !== (v.list === undefined)) throw new NotStandalone(`a step of a loop changes the kind of the name "${name}"`);
|
|
2165
|
+
if (v.list !== undefined && !fits(v.list, c.v.list as ListInfo)) throw new NotStandalone(`a step of a loop gives the name "${name}" a list that does not fit its first list`);
|
|
1983
2166
|
const t = this.fresh("t");
|
|
1984
2167
|
s.push(...c.s, `const ${t} = ${c.v.code};`);
|
|
1985
2168
|
sets.push(`${v.code} = ${t};`);
|
|
@@ -1995,8 +2178,10 @@ class Printer {
|
|
|
1995
2178
|
#iterate(e: ExprOf<"iterate">, ctx: Ctx): Code {
|
|
1996
2179
|
// Stryker disable next-line UpdateOperator: a level of a loop is a name, so a count down gives other names, and the same code
|
|
1997
2180
|
const loop = this.#loops++;
|
|
1998
|
-
const names = this.#loopNames(e.init, ctx, loop);
|
|
2181
|
+
const names = this.#loopNames(e.init, e.step, ctx, loop);
|
|
1999
2182
|
const inner: Ctx = { ...ctx, env: names.env };
|
|
2183
|
+
// A loop at a place that takes a list gives its result there (LOOP.LIST).
|
|
2184
|
+
if (this.#wantList.has(e)) this.#wantList.add(e.result);
|
|
2000
2185
|
const until = e.until === undefined ? undefined : this.expr(e.until, inner);
|
|
2001
2186
|
const step = this.#loopStep(e.step, inner, names.vars);
|
|
2002
2187
|
const result = this.expr(e.result, inner);
|
|
@@ -2008,7 +2193,7 @@ class Printer {
|
|
|
2008
2193
|
];
|
|
2009
2194
|
const inside = [...(until?.v.deps ?? []), ...step.deps, ...result.v.deps].filter((d) => d !== loop);
|
|
2010
2195
|
const deps = new Set([...names.deps, ...inside]);
|
|
2011
|
-
return { s: [...names.s, `for (let ${k} = 0; ; ${k}++) {`, ...ind(body), "}", ...result.s], v: { code: result.v.code, kind: result.v.kind, safe: false, deps } };
|
|
2196
|
+
return { s: [...names.s, `for (let ${k} = 0; ; ${k}++) {`, ...ind(body), "}", ...result.s], v: { code: result.v.code, kind: result.v.kind, safe: false, deps, list: result.v.list } };
|
|
2012
2197
|
}
|
|
2013
2198
|
|
|
2014
2199
|
/**
|
|
@@ -2018,7 +2203,8 @@ class Printer {
|
|
|
2018
2203
|
#fold(e: ExprOf<"fold">, ctx: Ctx): Code {
|
|
2019
2204
|
// Stryker disable next-line UpdateOperator: a level of a loop is a name, so a count down gives other names, and the same code
|
|
2020
2205
|
const loop = this.#loops++;
|
|
2021
|
-
const names = this.#loopNames(e.init, ctx, loop);
|
|
2206
|
+
const names = this.#loopNames(e.init, e.step, ctx, loop);
|
|
2207
|
+
if (this.#wantList.has(e)) this.#wantList.add(e.result);
|
|
2022
2208
|
const t = this.#targetLoop(e.axis, e.at ?? [], ctx);
|
|
2023
2209
|
const tests = t.tests.map((x) => this.#hoist(this.expr(x, t.inner), hasLoop(x)));
|
|
2024
2210
|
// The interpreter evaluates the tests at each target before the first step, so a module of a sheet does not have them.
|
|
@@ -2034,7 +2220,7 @@ class Printer {
|
|
|
2034
2220
|
];
|
|
2035
2221
|
const inside = [...tests.flatMap((c) => [...c.v.deps]), ...step.deps].filter((d) => d !== loop && d !== t.inner.level);
|
|
2036
2222
|
const deps = new Set([...names.deps, ...t.start, ...inside, ...[...result.v.deps].filter((d) => d !== loop)]);
|
|
2037
|
-
return { s: [...names.s, ...t.s, t.head, ...ind(body), "}", ...result.s], v: { code: result.v.code, kind: result.v.kind, safe: false, deps } };
|
|
2223
|
+
return { s: [...names.s, ...t.s, t.head, ...ind(body), "}", ...result.s], v: { code: result.v.code, kind: result.v.kind, safe: false, deps, list: result.v.list } };
|
|
2038
2224
|
}
|
|
2039
2225
|
|
|
2040
2226
|
/**
|