@mark1russell7/vex-compiler 0.13.1 → 0.13.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +15 -5
- package/build/index.d.ts.map +1 -1
- package/build/index.js +376 -72
- package/build/index.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +1 -1
- package/src/module.ts +326 -84
- package/src/runtime.ts +4 -0
- package/src/sheet-order.ts +63 -0
package/build/index.js
CHANGED
|
@@ -1672,6 +1672,7 @@ function createRuntime(options) {
|
|
|
1672
1672
|
});
|
|
1673
1673
|
if (r !== void 0) return r;
|
|
1674
1674
|
}
|
|
1675
|
+
if (listOp && fn === void 0 && Array.isArray(values[0])) throw new Error("tier 1 gives a list op on an array");
|
|
1675
1676
|
try {
|
|
1676
1677
|
return fn === void 0 ? F : checked(fn(...values));
|
|
1677
1678
|
} catch (thrown) {
|
|
@@ -1940,6 +1941,57 @@ function keyOrderOf(d) {
|
|
|
1940
1941
|
back: post !== reverse
|
|
1941
1942
|
};
|
|
1942
1943
|
}
|
|
1944
|
+
/** This function gives the axis under the tests of `where` and under `reverse`. Both read the targets of that axis. */
|
|
1945
|
+
function baseOf(axis) {
|
|
1946
|
+
let base = axis;
|
|
1947
|
+
while (base.t === "where" || base.t === "reverse") base = base.axis;
|
|
1948
|
+
return base;
|
|
1949
|
+
}
|
|
1950
|
+
/**
|
|
1951
|
+
* This function tells if each read of a cell of the column `self` reads a finished cell in the static order of the
|
|
1952
|
+
* fill (COMPILE.MODULE.SHEET.DIRECT). A read of a column before `self` in `order` reads a finished cell. A read of a
|
|
1953
|
+
* column that the sheet does not have also reads a finished cell. A read of `self` reads a finished cell only in
|
|
1954
|
+
* these three forms:
|
|
1955
|
+
* - in a tree in pre-order, at the move `parent` from the key,
|
|
1956
|
+
* - in an array in key order, at an offset of one negative step from the key,
|
|
1957
|
+
* - in a tree in post-order, at the key of a target of the axis `children` of the key.
|
|
1958
|
+
*
|
|
1959
|
+
* A read in the body of a function can evaluate at each place of a call, so it is not one of these forms.
|
|
1960
|
+
*
|
|
1961
|
+
* The columns of `fused` come before `self` in one loop over the keys (COMPILE.MODULE.ONCE). The loop evaluates each of
|
|
1962
|
+
* them at a key before `self` at that key. Thus a read of one of them reads a finished cell only at the key itself, or
|
|
1963
|
+
* in one of the three forms.
|
|
1964
|
+
*/
|
|
1965
|
+
function readsFinished(columns, order, self, tree, fused = /* @__PURE__ */ new Set()) {
|
|
1966
|
+
const formula = columns.get(self);
|
|
1967
|
+
if (formula === void 0 || shapeError(formula) !== void 0) return false;
|
|
1968
|
+
const { reverse, post } = keyOrderOf(readsOf(columns, /* @__PURE__ */ new Map(), self).directions);
|
|
1969
|
+
const position = order.indexOf(self);
|
|
1970
|
+
const todo = [[formula, "key"]];
|
|
1971
|
+
for (let item = todo.pop(); item !== void 0; item = todo.pop()) {
|
|
1972
|
+
const [e, place] = item;
|
|
1973
|
+
if (e.tag === "each" || e.tag === "fold") {
|
|
1974
|
+
const inner = place === "key" && (e.at === void 0 || e.at.length === 0) && baseOf(e.axis).t === "children" ? "children" : "unknown";
|
|
1975
|
+
const { tests } = partsOf$1(e.axis);
|
|
1976
|
+
for (const x of [...e.tag === "each" ? [e.body] : Object.values(e.step), ...tests]) todo.push([x, inner]);
|
|
1977
|
+
const list = listOf(e.axis);
|
|
1978
|
+
for (const x of [
|
|
1979
|
+
...e.tag === "fold" ? [...Object.values(e.init), e.result] : [],
|
|
1980
|
+
...addrExprs(e.at),
|
|
1981
|
+
...list === void 0 ? [] : [list]
|
|
1982
|
+
]) todo.push([x, place]);
|
|
1983
|
+
} else if (e.tag === "fn") todo.push([e.body, "unknown"]);
|
|
1984
|
+
else if (e.tag !== "ext") for (const child of children(e)) todo.push([child, place]);
|
|
1985
|
+
else if (e.kind === CELL_KIND && isCellRef(e.data)) {
|
|
1986
|
+
const { column, at } = e.data;
|
|
1987
|
+
if (order.indexOf(column) > position) return false;
|
|
1988
|
+
if (column !== self && !fused.has(column)) continue;
|
|
1989
|
+
const [move, ...rest] = at;
|
|
1990
|
+
if (!(column !== self && place === "key" && at.every((m) => m.t === "origin") || (place === "key" ? move !== void 0 && rest.length === 0 && (tree && !post && move.t === "parent" || !tree && !reverse && move.t === "offset" && move.d.length === 1 && (move.d[0] ?? 0) < 0) : place === "children" && tree && post && at.length === 0))) return false;
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
return true;
|
|
1994
|
+
}
|
|
1943
1995
|
//#endregion
|
|
1944
1996
|
//#region src/module.ts
|
|
1945
1997
|
/**
|
|
@@ -1959,15 +2011,18 @@ function keyOrderOf(d) {
|
|
|
1959
2011
|
* before the loop over the keys. A node of the memo is one function of its other keys, with a map of its values. The
|
|
1960
2012
|
* nodes with the same expression share the function.
|
|
1961
2013
|
*
|
|
1962
|
-
* The printer takes a part of the language. It takes an array space or a
|
|
1963
|
-
* the variables of the schema. It takes the axes `all`, `others` and `window`. A tree space also has the
|
|
1964
|
-
* axes of a tree (COMPILE.MODULE.TREE). A
|
|
1965
|
-
*
|
|
2014
|
+
* The printer takes a part of the language. It takes an array space, a tree space or a graph space of records, and
|
|
2015
|
+
* the fields and the variables of the schema. It takes the axes `all`, `others` and `window`. A tree space also has the
|
|
2016
|
+
* move `parent` and the axes of a tree (COMPILE.MODULE.TREE). A graph space has the axes of a graph
|
|
2017
|
+
* (COMPILE.MODULE.GRAPH). A list is an array of pairs of a key and a value. The list ops, `reduce`, list literals and
|
|
2018
|
+
* lists as names of loops are in a module (COMPILE.MODULE.LIST, COMPILE.MODULE.REDUCE). The value of a program can be a
|
|
2019
|
+
* list, and the run gives it as a `vex.list` (COMPILE.MODULE.LIST.RESULT). The kinds of each op site must decide its op.
|
|
1966
2020
|
* Another construct gives a reason, not code.
|
|
1967
2021
|
*
|
|
1968
2022
|
* `emitSheetModule` prints the formula columns of a sheet as one module (COMPILE.MODULE.SHEET). Each column is an array
|
|
1969
2023
|
* of results, and the run fills the arrays in the static order of `SheetRun.fill`. A read of a cell that is not finished
|
|
1970
|
-
* starts the search of `SheetRun`. Thus each cell on a cycle gives the error of Vex.
|
|
2024
|
+
* starts the search of `SheetRun`. Thus each cell on a cycle gives the error of Vex. A cell can hold a list, and a
|
|
2025
|
+
* formula reads it with the list ops (COMPILE.MODULE.SHEET.LIST).
|
|
1971
2026
|
*/
|
|
1972
2027
|
/** The error of a construct that a standalone module does not have. */
|
|
1973
2028
|
var NotStandalone = class extends Error {};
|
|
@@ -2110,6 +2165,31 @@ const HELPERS = {
|
|
|
2110
2165
|
SO: "function SO(g, s, k, d) { const t = g[k][s[k] + d]; return t === undefined ? F() : t; }",
|
|
2111
2166
|
AN: "function AN(P, k) { const o = []; for (let p = P[k]; p >= 0; p = P[p]) o.push(p); return o; }",
|
|
2112
2167
|
BF: "function BF(h, k) { const o = [k]; for (let i = 0; i < o.length; i++) for (const c of h[o[i]]) o.push(c); return o; }",
|
|
2168
|
+
GR: [
|
|
2169
|
+
"function GR(e, n) {",
|
|
2170
|
+
" if (!Array.isArray(e) || e.length !== n) C(\"the edges are not an array with the ends of the edges of each record\");",
|
|
2171
|
+
" const O = [], s = new Int32Array(n);",
|
|
2172
|
+
" for (let i = 0; i < n; i++) {",
|
|
2173
|
+
" const x = e[i];",
|
|
2174
|
+
" if (!Array.isArray(x)) C(`the edges of the record ${i} are not an array`);",
|
|
2175
|
+
" const o = [];",
|
|
2176
|
+
" for (const t of x) {",
|
|
2177
|
+
" if (!(Number.isInteger(t) && t >= 0 && t < n)) C(`an edge of the record ${i} does not go to the position of a record`);",
|
|
2178
|
+
" if (s[t] !== i + 1) {",
|
|
2179
|
+
" s[t] = i + 1;",
|
|
2180
|
+
" o.push(t);",
|
|
2181
|
+
" }",
|
|
2182
|
+
" }",
|
|
2183
|
+
" O.push(o);",
|
|
2184
|
+
" }",
|
|
2185
|
+
" return O;",
|
|
2186
|
+
"}"
|
|
2187
|
+
].join("\n"),
|
|
2188
|
+
GI: "function GI(O, n) { const I = []; for (let i = 0; i < n; i++) I.push([]); for (let i = 0; i < n; i++) for (const t of O[i]) I[t].push(i); return I; }",
|
|
2189
|
+
RB: "function RB(O, k, n) { const s = new Uint8Array(n), o = [k]; s[k] = 1; for (let i = 0; i < o.length; i++) for (const c of O[o[i]]) if (s[c] === 0) { s[c] = 1; o.push(c); } return o; }",
|
|
2190
|
+
DO: "function DO(d) { for (let k = 0; k < d.length; k++) if (d[k] === E) d[k] = VEX_ERROR; return d; }",
|
|
2191
|
+
LO: "function LO(l, h) { return { kind: \"vex.list\", items: l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LO(v, h - 1) : v } })) }; }",
|
|
2192
|
+
LC: "function LC(d, h) { for (let k = 0; k < d.length; k++) if (d[k] !== VEX_ERROR) d[k] = LO(d[k], h); return d; }",
|
|
2113
2193
|
SH: [
|
|
2114
2194
|
"function SH(n, cols, own, budget) {",
|
|
2115
2195
|
" const pg = (c, k) => {",
|
|
@@ -2381,6 +2461,7 @@ const NEEDS = {
|
|
|
2381
2461
|
GT: ["F"],
|
|
2382
2462
|
IX: ["V", "F"],
|
|
2383
2463
|
PT: ["C"],
|
|
2464
|
+
GR: ["C"],
|
|
2384
2465
|
UP: ["F"],
|
|
2385
2466
|
SO: ["F"],
|
|
2386
2467
|
CH: [],
|
|
@@ -2388,8 +2469,13 @@ const NEEDS = {
|
|
|
2388
2469
|
PR: [],
|
|
2389
2470
|
AN: [],
|
|
2390
2471
|
BF: [],
|
|
2472
|
+
GI: [],
|
|
2473
|
+
RB: [],
|
|
2391
2474
|
SH: [],
|
|
2392
|
-
KO: []
|
|
2475
|
+
KO: [],
|
|
2476
|
+
DO: [],
|
|
2477
|
+
LO: [],
|
|
2478
|
+
LC: ["LO"]
|
|
2393
2479
|
};
|
|
2394
2480
|
/** The statement of each part of a tree in a run, with the helper that computes it. */
|
|
2395
2481
|
const TREE_PARTS = {
|
|
@@ -2409,6 +2495,12 @@ const TREE_AXES = /* @__PURE__ */ new Set([
|
|
|
2409
2495
|
]);
|
|
2410
2496
|
/** This function tells if an axis has its targets in a range of an array of positions in a tree space. */
|
|
2411
2497
|
const isTreeAxis = (a) => a.t === "window" || TREE_AXES.has(a.t);
|
|
2498
|
+
/** The axes of a graph space in a module (COMPILE.MODULE.GRAPH), other than the axes of each space. */
|
|
2499
|
+
const GRAPH_AXES = /* @__PURE__ */ new Set([
|
|
2500
|
+
"successors",
|
|
2501
|
+
"predecessors",
|
|
2502
|
+
"reachable"
|
|
2503
|
+
]);
|
|
2412
2504
|
/** The list ops of a standalone module. */
|
|
2413
2505
|
const LIST_OPS = /* @__PURE__ */ new Set([
|
|
2414
2506
|
"count",
|
|
@@ -2421,6 +2513,8 @@ const LIST_OPS = /* @__PURE__ */ new Set([
|
|
|
2421
2513
|
"none",
|
|
2422
2514
|
"first"
|
|
2423
2515
|
]);
|
|
2516
|
+
/** The list ops with a running value in a module: the list ops above, and `reduce` with the op of a domain (COMPILE.MODULE.REDUCE). */
|
|
2517
|
+
const FOLD_OPS = /* @__PURE__ */ new Set([...LIST_OPS, "reduce"]);
|
|
2424
2518
|
const NUMBER_LIST_OPS = /* @__PURE__ */ new Set([
|
|
2425
2519
|
"count",
|
|
2426
2520
|
"sum",
|
|
@@ -2462,43 +2556,52 @@ const LIST_RESULTS = /* @__PURE__ */ new Set([
|
|
|
2462
2556
|
* This function tells if an expression gives a list. An each, a list literal and a list op of `LIST_RESULTS` give a
|
|
2463
2557
|
* 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.
|
|
2464
2558
|
*/
|
|
2465
|
-
function listy(e, isName) {
|
|
2559
|
+
function listy(e, isName, isCell) {
|
|
2466
2560
|
switch (e.tag) {
|
|
2467
2561
|
case "each": return true;
|
|
2468
2562
|
case "lit": return isVexList(e.value);
|
|
2469
|
-
case "app": return LIST_RESULTS.has(e.op);
|
|
2563
|
+
case "app": return LIST_RESULTS.has(e.op) || (e.op === "if" || e.op === "ifError") && e.args.slice(e.op === "if" ? 1 : 0).some((x) => listy(x, isName, isCell));
|
|
2470
2564
|
case "var": return isName(e.name);
|
|
2471
|
-
case "let": return listy(e.body, namesIn(e.bind, isName));
|
|
2565
|
+
case "let": return listy(e.body, namesIn(e.bind, isName, isCell), isCell);
|
|
2472
2566
|
case "fold":
|
|
2473
|
-
case "iterate": return listy(e.result, namesIn(e.init, isName));
|
|
2567
|
+
case "iterate": return listy(e.result, namesIn(e.init, isName, isCell), isCell);
|
|
2568
|
+
case "ext": return isCell(e);
|
|
2474
2569
|
case "call":
|
|
2475
|
-
case "ext":
|
|
2476
2570
|
case "fn":
|
|
2477
2571
|
case "rec":
|
|
2478
2572
|
case "ref": return false;
|
|
2479
2573
|
}
|
|
2480
2574
|
}
|
|
2481
2575
|
/** 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. */
|
|
2482
|
-
function namesIn(bind, isName) {
|
|
2483
|
-
const lists = new Map(Object.entries(bind).map(([name, x]) => [name, x.tag !== "fn" && listy(x, isName)]));
|
|
2576
|
+
function namesIn(bind, isName, isCell) {
|
|
2577
|
+
const lists = new Map(Object.entries(bind).map(([name, x]) => [name, x.tag !== "fn" && listy(x, isName, isCell)]));
|
|
2484
2578
|
return (name) => lists.get(name) ?? isName(name);
|
|
2485
2579
|
}
|
|
2486
2580
|
/** 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). */
|
|
2487
2581
|
function hasKeyedLiteral(e) {
|
|
2488
2582
|
return e.tag === "lit" && isVexList(e.value) && e.value.items.length > 0 || children(e).some(hasKeyedLiteral);
|
|
2489
2583
|
}
|
|
2490
|
-
/**
|
|
2491
|
-
function
|
|
2584
|
+
/**
|
|
2585
|
+
* This function gives the facts of the list of `append` from the facts of its two lists. It also gives the list of
|
|
2586
|
+
* `if` from its two branches. An empty list fits each list. The op `op` names the op in the reason.
|
|
2587
|
+
*/
|
|
2588
|
+
function joinLists(a, b, op) {
|
|
2492
2589
|
if (a.empty === true) return b;
|
|
2493
2590
|
if (b.empty === true) return a;
|
|
2494
|
-
if (a.inner === void 0 !== (b.inner === void 0)) throw new NotStandalone("
|
|
2591
|
+
if (a.inner === void 0 !== (b.inner === void 0)) throw new NotStandalone(`"${op}" of a list of lists and a list of values`);
|
|
2495
2592
|
const texts = a.texts === true || b.texts === true;
|
|
2496
2593
|
return {
|
|
2497
2594
|
item: join(a.item, b.item),
|
|
2498
|
-
inner: a.inner === void 0 ? void 0 : joinLists(a.inner, b.inner),
|
|
2595
|
+
inner: a.inner === void 0 ? void 0 : joinLists(a.inner, b.inner, op),
|
|
2499
2596
|
texts
|
|
2500
2597
|
};
|
|
2501
2598
|
}
|
|
2599
|
+
/** This function gives the facts of the list of `if` or `ifError` from its two branches. Two values that are not lists give undefined. */
|
|
2600
|
+
function branchLists(op, a, b) {
|
|
2601
|
+
if (a === void 0 && b === void 0) return void 0;
|
|
2602
|
+
if (a === void 0 || b === void 0) throw new NotStandalone(`"${op}" of a list and a value that is not a list`);
|
|
2603
|
+
return joinLists(a, b, op);
|
|
2604
|
+
}
|
|
2502
2605
|
/**
|
|
2503
2606
|
* 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
|
|
2504
2607
|
* 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.
|
|
@@ -2510,7 +2613,7 @@ function fits(name, step) {
|
|
|
2510
2613
|
return name.inner === void 0 ? step.inner === void 0 : step.inner !== void 0 && fits(name.inner, step.inner);
|
|
2511
2614
|
}
|
|
2512
2615
|
/** This function tells if the step of a list op throws for a value of the kind `kind`, which is the wrong kind. */
|
|
2513
|
-
const stepThrows = (op, kind) => NUMBER_STEPS.has(op) && kind !== "number" || BOOLEAN_LIST_OPS.has(op) && kind !== "boolean";
|
|
2616
|
+
const stepThrows = (op, kind) => op === "reduce" || NUMBER_STEPS.has(op) && kind !== "number" || BOOLEAN_LIST_OPS.has(op) && kind !== "boolean";
|
|
2514
2617
|
/** The list ops whose step takes a number. */
|
|
2515
2618
|
const NUMBER_STEPS = /* @__PURE__ */ new Set([
|
|
2516
2619
|
"sum",
|
|
@@ -2787,6 +2890,9 @@ var Printer = class {
|
|
|
2787
2890
|
/** True for a tree space, and the parts of the tree that the program reads (COMPILE.MODULE.TREE). */
|
|
2788
2891
|
#tree;
|
|
2789
2892
|
treeParts = /* @__PURE__ */ new Set();
|
|
2893
|
+
/** True for a graph space, and true when the program reads the predecessors of a graph (COMPILE.MODULE.GRAPH). */
|
|
2894
|
+
#graph;
|
|
2895
|
+
into = false;
|
|
2790
2896
|
/** The columns of a sheet, for a module of a sheet (COMPILE.MODULE.SHEET). */
|
|
2791
2897
|
#sheet;
|
|
2792
2898
|
#names = 0;
|
|
@@ -2829,6 +2935,7 @@ var Printer = class {
|
|
|
2829
2935
|
this.#schema = options.schema;
|
|
2830
2936
|
this.#memo = memoOf(expr);
|
|
2831
2937
|
this.#tree = options.space === "tree";
|
|
2938
|
+
this.#graph = options.space === "graph";
|
|
2832
2939
|
this.#sheet = sheet;
|
|
2833
2940
|
}
|
|
2834
2941
|
use(name) {
|
|
@@ -3021,7 +3128,11 @@ var Printer = class {
|
|
|
3021
3128
|
}
|
|
3022
3129
|
/** This method tells if an expression gives a list at a place that takes one (`listy`). */
|
|
3023
3130
|
#isList(e, ctx) {
|
|
3024
|
-
return listy(e, (name) => ctx.env.get(name)?.list !== void 0);
|
|
3131
|
+
return listy(e, (name) => ctx.env.get(name)?.list !== void 0, (x) => this.#cellList(x) !== void 0);
|
|
3132
|
+
}
|
|
3133
|
+
/** This method gives the facts of the list of a read of a cell. A read of a column that holds no list gives undefined. */
|
|
3134
|
+
#cellList(e) {
|
|
3135
|
+
return e.kind === CELL_KIND && isCellRef(e.data) ? this.#sheet?.lists.get(e.data.column) : void 0;
|
|
3025
3136
|
}
|
|
3026
3137
|
/** 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. */
|
|
3027
3138
|
#listLiteral(l) {
|
|
@@ -3169,19 +3280,23 @@ var Printer = class {
|
|
|
3169
3280
|
const { column, at } = e.data;
|
|
3170
3281
|
const deps = /* @__PURE__ */ new Set([...this.#addressDeps(at, ctx, []), CELLS]);
|
|
3171
3282
|
const index = sheet.columns.get(column);
|
|
3283
|
+
const list = sheet.lists.get(column);
|
|
3172
3284
|
if (index === void 0) return bare({
|
|
3173
3285
|
code: `${F}()`,
|
|
3174
3286
|
kind: sheet.kinds.get(column),
|
|
3175
3287
|
safe: false,
|
|
3176
|
-
deps
|
|
3288
|
+
deps,
|
|
3289
|
+
list
|
|
3177
3290
|
});
|
|
3178
3291
|
if (!sheet.kinds.has(column)) throw new NotStandalone(`a formula reads the column "${column}" before its formula, so the column needs its kind in the option kinds`);
|
|
3179
|
-
const
|
|
3292
|
+
const address = this.#address(at, ctx, []);
|
|
3293
|
+
const read = sheet.direct ? `D[${String(index)}][${address}]` : `${READ_CELL}(${String(index)}, ${address})`;
|
|
3180
3294
|
const v = {
|
|
3181
3295
|
code: `${this.use("G")}(${read})`,
|
|
3182
3296
|
kind: sheet.kinds.get(column),
|
|
3183
3297
|
safe: false,
|
|
3184
|
-
deps
|
|
3298
|
+
deps,
|
|
3299
|
+
list
|
|
3185
3300
|
};
|
|
3186
3301
|
return bare(at.every((m) => m.t === "origin") ? {
|
|
3187
3302
|
...v,
|
|
@@ -3200,6 +3315,7 @@ var Printer = class {
|
|
|
3200
3315
|
* must read no cell.
|
|
3201
3316
|
*/
|
|
3202
3317
|
#inOrder(parts, all) {
|
|
3318
|
+
if (this.#sheet?.direct === true) return parts;
|
|
3203
3319
|
const last = parts.findLastIndex((x) => x.v.deps.has(CELLS));
|
|
3204
3320
|
const after = parts.slice(last + 1).every((x) => x.s.length === 0 || x.v.safe);
|
|
3205
3321
|
const before = !all || parts.slice(0, Math.max(last, 0)).every((x) => x.v.safe) || !READS_IN_CODE.test(parts[last].v.code);
|
|
@@ -3263,7 +3379,7 @@ var Printer = class {
|
|
|
3263
3379
|
const [d, ...more] = m.d;
|
|
3264
3380
|
/* v8 ignore next -- @preserve */
|
|
3265
3381
|
if (focus === void 0) throw new NotStandalone("a node of the memo reads its focus");
|
|
3266
|
-
if (d === void 0 || more.length > 0 || !Number.isInteger(d)) return `${F}()`;
|
|
3382
|
+
if (this.#graph || d === void 0 || more.length > 0 || !Number.isInteger(d)) return `${F}()`;
|
|
3267
3383
|
if (this.#tree) return `${this.use("SO")}(${this.#part("siblings", "GP")}, SI, ${focus}, ${d})`;
|
|
3268
3384
|
return `${this.use("X")}(${focus} + ${d}, n)`;
|
|
3269
3385
|
}
|
|
@@ -3523,19 +3639,24 @@ var Printer = class {
|
|
|
3523
3639
|
}
|
|
3524
3640
|
#app(e, ctx) {
|
|
3525
3641
|
const { op, args } = e;
|
|
3642
|
+
if ((op === "if" || op === "ifError") && this.#wantList.has(e)) for (const x of args.slice(op === "if" ? 1 : 0)) this.#wantList.add(x);
|
|
3526
3643
|
if (op === "if") return this.#if(args, ctx);
|
|
3527
3644
|
if (op === "and" || op === "or") return this.#andOr(op === "and", args, ctx);
|
|
3528
3645
|
if (op === "ifError") return this.#ifError(args, ctx);
|
|
3529
3646
|
const [head] = args;
|
|
3530
3647
|
const rest = args.slice(1);
|
|
3531
|
-
if (
|
|
3532
|
-
if (
|
|
3648
|
+
if (FOLD_OPS.has(op) && head?.tag === "each" && !this.#isList(head.body, ctx)) return this.#list(op, head, rest, ctx);
|
|
3649
|
+
if (FOLD_OPS.has(op) && head?.tag === "let" && rest.every((x) => x.tag === "lit")) return this.#let(head, ctx, (body) => app(op, body, ...rest));
|
|
3533
3650
|
if (head !== void 0 && (op === "sortBy" || op === "take" || op === "drop" && this.#isList(head, ctx))) return this.#order(op, head, rest, ctx);
|
|
3534
3651
|
if (head !== void 0 && BUILD_OPS.has(op) && this.#isList(head, ctx)) return this.#build(op, head, rest, ctx);
|
|
3535
|
-
if (head !== void 0 && (
|
|
3652
|
+
if (head !== void 0 && (FOLD_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, rest, ctx);
|
|
3536
3653
|
const raw = args.map((x) => this.expr(x, ctx));
|
|
3537
3654
|
const deps = depsOf(...raw);
|
|
3538
3655
|
const values = this.#inOrder(this.#settle(raw, deps, args), true);
|
|
3656
|
+
return this.#opCall(op, values, deps);
|
|
3657
|
+
}
|
|
3658
|
+
/** This method prints the call of the op of a domain on some values, from the static site of their kinds. */
|
|
3659
|
+
#opCall(op, values, deps) {
|
|
3539
3660
|
const site = this.#facts.staticSite(op, values.map((x) => x.v.kind));
|
|
3540
3661
|
if (site?.t !== "fn") throw new NotStandalone(`the kinds of the values of "${op}" do not decide its op`);
|
|
3541
3662
|
if (site.bad) return {
|
|
@@ -3602,7 +3723,8 @@ var Printer = class {
|
|
|
3602
3723
|
const v = {
|
|
3603
3724
|
kind: join(yes.v.kind, no.v.kind),
|
|
3604
3725
|
safe: this.#safeBoolean(cond.v) && yes.v.safe && no.v.safe,
|
|
3605
|
-
deps
|
|
3726
|
+
deps,
|
|
3727
|
+
list: branchLists("if", yes.v.list, no.v.list)
|
|
3606
3728
|
};
|
|
3607
3729
|
if (yes.s.length === 0 && no.s.length === 0) return {
|
|
3608
3730
|
s: cond.s,
|
|
@@ -3684,7 +3806,8 @@ var Printer = class {
|
|
|
3684
3806
|
code: t,
|
|
3685
3807
|
kind: join(first.v.kind, second.v.kind),
|
|
3686
3808
|
safe: second.v.safe,
|
|
3687
|
-
deps
|
|
3809
|
+
deps,
|
|
3810
|
+
list: branchLists("ifError", first.v.list, second.v.list)
|
|
3688
3811
|
}
|
|
3689
3812
|
};
|
|
3690
3813
|
}
|
|
@@ -3700,15 +3823,35 @@ var Printer = class {
|
|
|
3700
3823
|
* This method gives the option strict of a list op. The options must be one literal with only strict. Only a literal
|
|
3701
3824
|
* has a value, so another expression gives the reason too.
|
|
3702
3825
|
*/
|
|
3703
|
-
#strict(op, rest) {
|
|
3826
|
+
#strict(op, rest, names = ["strict"]) {
|
|
3704
3827
|
const [options, ...more] = rest;
|
|
3705
3828
|
if (more.length > 0) throw new NotStandalone(`"${op}" with more than one option`);
|
|
3706
3829
|
if (options === void 0) return false;
|
|
3707
3830
|
const opts = options.value;
|
|
3708
|
-
if (typeof opts !== "object" || opts === null || Object.keys(opts).some((k) => k
|
|
3831
|
+
if (typeof opts !== "object" || opts === null || Object.keys(opts).some((k) => !names.includes(k))) throw new NotStandalone(`the options of "${op}" are not a literal with ${names.join(" or ")}`);
|
|
3709
3832
|
return opts.strict === true;
|
|
3710
3833
|
}
|
|
3711
3834
|
/**
|
|
3835
|
+
* This method gives the option strict of a list op. For `reduce`, it also gives the op of each step: the first
|
|
3836
|
+
* argument, a literal text, and the value of the option `domain` (COMPILE.MODULE.REDUCE).
|
|
3837
|
+
*/
|
|
3838
|
+
#foldOptions(op, rest) {
|
|
3839
|
+
if (op !== "reduce") return {
|
|
3840
|
+
strict: this.#strict(op, rest),
|
|
3841
|
+
step: void 0
|
|
3842
|
+
};
|
|
3843
|
+
const [name, ...options] = rest;
|
|
3844
|
+
const value = name?.value;
|
|
3845
|
+
if (name?.tag !== "lit" || typeof value !== "string") throw new NotStandalone("\"reduce\" without a literal text as its op");
|
|
3846
|
+
return {
|
|
3847
|
+
strict: this.#strict(op, options, ["strict", "domain"]),
|
|
3848
|
+
step: {
|
|
3849
|
+
op: value,
|
|
3850
|
+
domain: options[0]?.value?.domain
|
|
3851
|
+
}
|
|
3852
|
+
};
|
|
3853
|
+
}
|
|
3854
|
+
/**
|
|
3712
3855
|
* A list op over an axis: one `for` loop with a running value. An item with an error is skipped, and with `strict` it
|
|
3713
3856
|
* is an error of the list op. A value of the wrong kind is an error of the list op, as in the interpreter.
|
|
3714
3857
|
*/
|
|
@@ -3781,6 +3924,24 @@ var Printer = class {
|
|
|
3781
3924
|
inner
|
|
3782
3925
|
};
|
|
3783
3926
|
}
|
|
3927
|
+
if (this.#graph && GRAPH_AXES.has(base.t)) {
|
|
3928
|
+
const range = this.#graphRange(base.t, f);
|
|
3929
|
+
const q = this.fresh("q");
|
|
3930
|
+
return {
|
|
3931
|
+
s: [...s, ...range.s],
|
|
3932
|
+
j,
|
|
3933
|
+
head: forHead(q, range.lo, range.hi, reverse),
|
|
3934
|
+
key: [`const ${j} = ${range.a}[${q}];`],
|
|
3935
|
+
check: [],
|
|
3936
|
+
skip: void 0,
|
|
3937
|
+
fixed: false,
|
|
3938
|
+
start,
|
|
3939
|
+
startSafe,
|
|
3940
|
+
tests,
|
|
3941
|
+
inner
|
|
3942
|
+
};
|
|
3943
|
+
}
|
|
3944
|
+
if (this.#graph && base.t === "window") throw new NotStandalone("the axis \"window\" in a graph space");
|
|
3784
3945
|
if (base.t === "others") skip = f;
|
|
3785
3946
|
else if (base.t === "window") {
|
|
3786
3947
|
const h = this.fresh("h");
|
|
@@ -3803,6 +3964,26 @@ var Printer = class {
|
|
|
3803
3964
|
};
|
|
3804
3965
|
}
|
|
3805
3966
|
/**
|
|
3967
|
+
* This method gives the targets of an axis of a graph space at the position `f` (AXIS.GRAPH). They are the positions
|
|
3968
|
+
* `a[q]` for `q` from `lo` up to `hi`. The first key of the search of `reachable` is the focus, which is not a target.
|
|
3969
|
+
*/
|
|
3970
|
+
#graphRange(t, f) {
|
|
3971
|
+
const a = this.fresh("A");
|
|
3972
|
+
const range = (code, lo = "0") => ({
|
|
3973
|
+
s: [`const ${a} = ${code};`],
|
|
3974
|
+
a,
|
|
3975
|
+
lo,
|
|
3976
|
+
hi: `${a}.length`,
|
|
3977
|
+
skip: void 0
|
|
3978
|
+
});
|
|
3979
|
+
if (t === "successors") return range(`GO[${f}]`);
|
|
3980
|
+
if (t === "predecessors") {
|
|
3981
|
+
this.into = true;
|
|
3982
|
+
return range(`GN[${f}]`);
|
|
3983
|
+
}
|
|
3984
|
+
return range(`${this.use("RB")}(GO, ${f}, n)`, "1");
|
|
3985
|
+
}
|
|
3986
|
+
/**
|
|
3806
3987
|
* This method gives the targets of an axis of a tree space at the position `f`. They are the positions `a[q]` for `q`
|
|
3807
3988
|
* from `lo` up to `hi`, without the key that the loop skips. The axes come from AXIS.TREE, AXIS.GRAPH and AXIS.WINDOW.
|
|
3808
3989
|
*/
|
|
@@ -3851,7 +4032,7 @@ var Printer = class {
|
|
|
3851
4032
|
}
|
|
3852
4033
|
}
|
|
3853
4034
|
#list(op, target, rest, ctx) {
|
|
3854
|
-
const strict = this.#
|
|
4035
|
+
const { strict, step } = this.#foldOptions(op, rest);
|
|
3855
4036
|
const at = target.at ?? [];
|
|
3856
4037
|
const { s, j, head, key, check, skip, fixed, start, startSafe, tests, inner } = this.#targetLoop(target.axis, at, ctx);
|
|
3857
4038
|
const { level } = inner;
|
|
@@ -3859,7 +4040,7 @@ var Printer = class {
|
|
|
3859
4040
|
const body = this.#hoist(this.expr(target.body, inner), hasLoop(target.body));
|
|
3860
4041
|
if (op === "count" && fixed && tests.length === 0 && body.v.safe && !body.v.deps.has(CELLS)) return bare(plainValue("n", "number"));
|
|
3861
4042
|
const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== level));
|
|
3862
|
-
const r = this.#reduction(op, body.v.kind);
|
|
4043
|
+
const r = this.#reduction(op, body.v.kind, step);
|
|
3863
4044
|
const { v } = r;
|
|
3864
4045
|
s.push(r.init);
|
|
3865
4046
|
const item = [
|
|
@@ -3900,7 +4081,7 @@ var Printer = class {
|
|
|
3900
4081
|
],
|
|
3901
4082
|
v: {
|
|
3902
4083
|
code: r.result,
|
|
3903
|
-
kind:
|
|
4084
|
+
kind: r.kind,
|
|
3904
4085
|
safe: false,
|
|
3905
4086
|
deps
|
|
3906
4087
|
}
|
|
@@ -3926,7 +4107,7 @@ var Printer = class {
|
|
|
3926
4107
|
s,
|
|
3927
4108
|
v: {
|
|
3928
4109
|
code: r.result,
|
|
3929
|
-
kind:
|
|
4110
|
+
kind: r.kind,
|
|
3930
4111
|
safe,
|
|
3931
4112
|
deps
|
|
3932
4113
|
}
|
|
@@ -3936,10 +4117,15 @@ var Printer = class {
|
|
|
3936
4117
|
* This method gives the parts of a list op that reduces the values of its items. The parts are the variable `v` of
|
|
3937
4118
|
* 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.
|
|
3938
4119
|
*/
|
|
3939
|
-
#reduction(op, kind) {
|
|
4120
|
+
#reduction(op, kind, step) {
|
|
3940
4121
|
const a = this.fresh("a");
|
|
3941
4122
|
const n = this.fresh("n");
|
|
3942
4123
|
const v = this.fresh("x");
|
|
4124
|
+
if (step !== void 0) return {
|
|
4125
|
+
v,
|
|
4126
|
+
init: `let ${a}, ${n} = 0;`,
|
|
4127
|
+
...this.#reduceStep(step, kind, a, n, v)
|
|
4128
|
+
};
|
|
3943
4129
|
const num = kind === "number" ? v : `${this.use("U")}(${v})`;
|
|
3944
4130
|
const bool = kind === "boolean" ? v : `${this.use("B")}(${v})`;
|
|
3945
4131
|
const inits = {
|
|
@@ -3974,7 +4160,51 @@ var Printer = class {
|
|
|
3974
4160
|
v,
|
|
3975
4161
|
init: inits[op] ?? `let ${a}, ${n} = 0;`,
|
|
3976
4162
|
step: steps[op],
|
|
3977
|
-
result: results[op] ?? `(${n} === 0 ? ${F}() : ${a})
|
|
4163
|
+
result: results[op] ?? `(${n} === 0 ? ${F}() : ${a})`,
|
|
4164
|
+
kind: listKind(op, kind)
|
|
4165
|
+
};
|
|
4166
|
+
}
|
|
4167
|
+
/**
|
|
4168
|
+
* This method gives the step and the result of `reduce` (COMPILE.MODULE.REDUCE), as in the interpreter. The first item
|
|
4169
|
+
* is the first running value. Each later item goes into the op of the domain of the running value. The kinds of the
|
|
4170
|
+
* items decide that op, and the op must give a value of the same kind. No items give the identity of the op.
|
|
4171
|
+
*/
|
|
4172
|
+
#reduceStep(step, kind, a, n, v) {
|
|
4173
|
+
const arg = (code) => bare({
|
|
4174
|
+
code,
|
|
4175
|
+
kind,
|
|
4176
|
+
safe: true,
|
|
4177
|
+
deps: NO_DEPS,
|
|
4178
|
+
plain: true
|
|
4179
|
+
});
|
|
4180
|
+
const opCode = this.#opCall(step.op, [arg(a), arg(v)], NO_DEPS);
|
|
4181
|
+
if (opCode.v.kind !== kind) throw new NotStandalone(`the op "${step.op}" of "reduce" gives a value of the kind ${kindText(opCode.v.kind)}, not of the kind ${kindText(kind)} of the items`);
|
|
4182
|
+
const empty = this.#identity(step);
|
|
4183
|
+
return {
|
|
4184
|
+
step: `${a} = ${n}++ === 0 ? ${v} : ${opCode.v.code};`,
|
|
4185
|
+
result: `(${n} === 0 ? ${empty.code} : ${a})`,
|
|
4186
|
+
kind: empty.kind === void 0 || empty.kind === kind ? kind : void 0
|
|
4187
|
+
};
|
|
4188
|
+
}
|
|
4189
|
+
/**
|
|
4190
|
+
* This method gives the code of `reduce` over no items, as in the interpreter. The code is the identity of the op in
|
|
4191
|
+
* the one domain that has one, or in the domain of the option `domain`. Another case is an error. The kind is the kind
|
|
4192
|
+
* of the domain.
|
|
4193
|
+
*/
|
|
4194
|
+
#identity(step) {
|
|
4195
|
+
const { op, domain } = step;
|
|
4196
|
+
const fail = {
|
|
4197
|
+
code: `${this.use("F")}()`,
|
|
4198
|
+
kind: void 0
|
|
4199
|
+
};
|
|
4200
|
+
const [only, second] = [...this.#domains.entries()].filter(([, d]) => Object.hasOwn(d.ops, op) && d.ops[op].identity !== void 0 && (domain === void 0 || d.name === domain));
|
|
4201
|
+
if (only === void 0 || second !== void 0) return fail;
|
|
4202
|
+
const [i, d] = only;
|
|
4203
|
+
const name = `id_${String(i)}_${String(this.#opIndex(op))}`;
|
|
4204
|
+
if (!this.top.has(name)) this.top.set(name, `const ${name} = ${copied(d.ops[op].identity, [], `the identity of the op "${op}" of the domain ${d.name}`)};`);
|
|
4205
|
+
return {
|
|
4206
|
+
code: `${name}()`,
|
|
4207
|
+
kind: kindOfDomain(d)
|
|
3978
4208
|
};
|
|
3979
4209
|
}
|
|
3980
4210
|
/**
|
|
@@ -4080,7 +4310,7 @@ var Printer = class {
|
|
|
4080
4310
|
if (op === "get") return value(info.item, info.inner);
|
|
4081
4311
|
if (op === "indexOf") return value("number", void 0);
|
|
4082
4312
|
if (op === "without") return value(void 0, info);
|
|
4083
|
-
if (op === "append") return value(void 0, joinLists(info, parts[1].v.list));
|
|
4313
|
+
if (op === "append") return value(void 0, joinLists(info, parts[1].v.list, op));
|
|
4084
4314
|
if (info.empty === true) return value(void 0, info);
|
|
4085
4315
|
if (info.inner === void 0) throw new NotStandalone("\"flat\" over a list whose items are not lists");
|
|
4086
4316
|
return value(void 0, info.inner);
|
|
@@ -4089,7 +4319,7 @@ var Printer = class {
|
|
|
4089
4319
|
#overList(op, head, rest, ctx) {
|
|
4090
4320
|
const list = this.#listOf(head, ctx);
|
|
4091
4321
|
const exprs = [head, ...op === "nth" ? [rest[0] ?? lit(null)] : []];
|
|
4092
|
-
const strict = this.#
|
|
4322
|
+
const { strict, step } = this.#foldOptions(op, rest.slice(exprs.length - 1));
|
|
4093
4323
|
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
4094
4324
|
const deps = depsOf(...raw);
|
|
4095
4325
|
const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
|
|
@@ -4105,7 +4335,7 @@ var Printer = class {
|
|
|
4105
4335
|
list: inner
|
|
4106
4336
|
}
|
|
4107
4337
|
};
|
|
4108
|
-
const r = this.#reduction(op, item);
|
|
4338
|
+
const r = this.#reduction(op, item, step);
|
|
4109
4339
|
const { v } = r;
|
|
4110
4340
|
return {
|
|
4111
4341
|
s: [
|
|
@@ -4118,7 +4348,7 @@ var Printer = class {
|
|
|
4118
4348
|
],
|
|
4119
4349
|
v: {
|
|
4120
4350
|
code: r.result,
|
|
4121
|
-
kind:
|
|
4351
|
+
kind: r.kind,
|
|
4122
4352
|
safe: false,
|
|
4123
4353
|
deps,
|
|
4124
4354
|
list: op === "first" ? inner : void 0
|
|
@@ -4465,6 +4695,7 @@ var Printer = class {
|
|
|
4465
4695
|
/** This method gives the code of the program at the key of the run. A constant of the run evaluates once. */
|
|
4466
4696
|
/** This method gives the code of a program at the key `o` of the run. A sheet has one program for each formula. */
|
|
4467
4697
|
root(expr) {
|
|
4698
|
+
this.#wantList.add(expr);
|
|
4468
4699
|
return this.#hoist(this.expr(expr, {
|
|
4469
4700
|
origin: "o",
|
|
4470
4701
|
focus: "o",
|
|
@@ -4501,11 +4732,12 @@ var Printer = class {
|
|
|
4501
4732
|
function runOf(options) {
|
|
4502
4733
|
const name = options.name ?? "run";
|
|
4503
4734
|
if (!/^[A-Za-z_$][\w$]*$/.test(name)) return { reason: `the name "${name}" is not a JavaScript name` };
|
|
4504
|
-
const
|
|
4505
|
-
if (
|
|
4735
|
+
const space = options.space ?? "array";
|
|
4736
|
+
if (space !== "array" && space !== "tree" && space !== "graph") return { reason: `the space "${String(space)}" is not "array", "tree" or "graph"` };
|
|
4506
4737
|
return {
|
|
4507
4738
|
name,
|
|
4508
|
-
tree
|
|
4739
|
+
tree: space === "tree",
|
|
4740
|
+
graph: space === "graph"
|
|
4509
4741
|
};
|
|
4510
4742
|
}
|
|
4511
4743
|
/** This function gives the reason of an error of the printer. Another exception is a defect, so it goes to the caller. */
|
|
@@ -4537,7 +4769,7 @@ function printModule(p, options, run, body) {
|
|
|
4537
4769
|
} catch (thrown) {
|
|
4538
4770
|
return reasonOf(thrown);
|
|
4539
4771
|
}
|
|
4540
|
-
const { name, tree } = run;
|
|
4772
|
+
const { name, tree, graph } = run;
|
|
4541
4773
|
if (tree) p.use("PT");
|
|
4542
4774
|
if (p.treeParts.size > 0) p.treeParts.add("children");
|
|
4543
4775
|
const parts = [
|
|
@@ -4546,6 +4778,8 @@ function printModule(p, options, run, body) {
|
|
|
4546
4778
|
"order"
|
|
4547
4779
|
].filter((x) => p.treeParts.has(x));
|
|
4548
4780
|
for (const x of parts) p.use(TREE_PARTS[x][0]);
|
|
4781
|
+
if (graph) p.use("GR");
|
|
4782
|
+
if (p.into) p.use("GI");
|
|
4549
4783
|
[...p.helpers].toSorted().forEach(add);
|
|
4550
4784
|
const bad = nameReason(name, /* @__PURE__ */ new Set([
|
|
4551
4785
|
"VexContractError",
|
|
@@ -4573,7 +4807,7 @@ function printModule(p, options, run, body) {
|
|
|
4573
4807
|
...helpers.has("S") ? ["const N = {}, W = {};"] : [],
|
|
4574
4808
|
...[...helpers].map((h) => h === "S" ? helperS(p.features) : HELPERS[h]),
|
|
4575
4809
|
...p.top.values(),
|
|
4576
|
-
`export function ${name}(records, vars${tree ? ", parents" : ""}) {`,
|
|
4810
|
+
`export function ${name}(records, vars${tree ? ", parents" : graph ? ", edges" : ""}) {`,
|
|
4577
4811
|
" if (!Array.isArray(records)) C(\"the records are not an array\");",
|
|
4578
4812
|
" const R = records;",
|
|
4579
4813
|
" const n = R.length;",
|
|
@@ -4583,6 +4817,7 @@ function printModule(p, options, run, body) {
|
|
|
4583
4817
|
" }",
|
|
4584
4818
|
` if (typeof vars !== "object" || vars === null${varChecks}) C("the variables do not keep the schema");`,
|
|
4585
4819
|
...tree ? [" const P = PT(parents, n);", ...parts.map((x) => ` ${TREE_PARTS[x][1]}`)] : [],
|
|
4820
|
+
...graph ? [" const GO = GR(edges, n);", ...p.into ? [" const GN = GI(GO, n);"] : []] : [],
|
|
4586
4821
|
...[...p.vars].map(([v, code]) => ` const ${code} = vars[${JSON.stringify(v)}];`),
|
|
4587
4822
|
...ind(p.run),
|
|
4588
4823
|
...ind(body),
|
|
@@ -4625,12 +4860,13 @@ function emitModule(source, options) {
|
|
|
4625
4860
|
} catch (thrown) {
|
|
4626
4861
|
return reasonOf(thrown);
|
|
4627
4862
|
}
|
|
4863
|
+
const result = value.v.list === void 0 ? value.v.code : `${p.use("LO")}(${value.v.code}, ${String(depthOf(value.v.list))})`;
|
|
4628
4864
|
return printModule(p, options, run, [
|
|
4629
4865
|
"const out = [];",
|
|
4630
4866
|
"for (let o = 0; o < n; o++) {",
|
|
4631
4867
|
" try {",
|
|
4632
4868
|
...ind(ind(value.s)),
|
|
4633
|
-
` out.push(${
|
|
4869
|
+
` out.push(${result});`,
|
|
4634
4870
|
" } catch {",
|
|
4635
4871
|
" out.push(VEX_ERROR);",
|
|
4636
4872
|
" }",
|
|
@@ -4642,8 +4878,31 @@ function emitModule(source, options) {
|
|
|
4642
4878
|
const SHEET_BUDGET = 3e3;
|
|
4643
4879
|
/** This function gives the words for a formula in a reason: the formula of a column, or of one cell. */
|
|
4644
4880
|
const formulaText = (f) => f.key === void 0 ? `the formula of the column "${f.column}"` : `the formula of the cell "${f.column}" at "${String(f.key)}"`;
|
|
4645
|
-
/** This function gives the words for a kind in a reason. */
|
|
4646
|
-
const kindText = (k) => k ?? "that the printer does not know"
|
|
4881
|
+
/** This function gives the words for a kind in a reason. A list gives the kind of its items. */
|
|
4882
|
+
const kindText = (k, l) => l === void 0 ? k ?? "that the printer does not know" : `list of ${l.inner === void 0 ? kindText(l.item) : kindText(void 0, l.inner)}`;
|
|
4883
|
+
/** This function gives the number of the levels of lists in the items of a list: 0 for a list of values. */
|
|
4884
|
+
const depthOf = (l) => l.inner === void 0 ? 0 : 1 + depthOf(l.inner);
|
|
4885
|
+
/** This function gives the facts of the lists of a column from the kind of the option `kinds` (COMPILE.MODULE.SHEET.LIST). */
|
|
4886
|
+
const listOfKind = (k) => {
|
|
4887
|
+
const texts = k.texts === true;
|
|
4888
|
+
return typeof k.list === "object" ? {
|
|
4889
|
+
item: void 0,
|
|
4890
|
+
inner: listOfKind(k.list),
|
|
4891
|
+
texts
|
|
4892
|
+
} : {
|
|
4893
|
+
item: k.list,
|
|
4894
|
+
texts
|
|
4895
|
+
};
|
|
4896
|
+
};
|
|
4897
|
+
/**
|
|
4898
|
+
* This function gives the facts of the lists of a column from the list of its first formula. The other cells of the
|
|
4899
|
+
* column can have other formulas. Thus a list without items does not tell that each list of the column is empty.
|
|
4900
|
+
*/
|
|
4901
|
+
const columnList = (l) => ({
|
|
4902
|
+
item: l.item,
|
|
4903
|
+
inner: l.inner === void 0 ? void 0 : columnList(l.inner),
|
|
4904
|
+
texts: l.texts === true
|
|
4905
|
+
});
|
|
4647
4906
|
/**
|
|
4648
4907
|
* This function prints the formula columns of a sheet as a standalone ES module (COMPILE.MODULE.SHEET). The function of
|
|
4649
4908
|
* the module takes the records and the variables, and the parents for a tree. It gives an object with an array for
|
|
@@ -4687,20 +4946,46 @@ function emitSheetModule(columns, options) {
|
|
|
4687
4946
|
reason: `${formulaText(f)}: ${shape}`
|
|
4688
4947
|
};
|
|
4689
4948
|
}
|
|
4690
|
-
const kinds = new Map(
|
|
4949
|
+
const kinds = /* @__PURE__ */ new Map();
|
|
4950
|
+
const lists = /* @__PURE__ */ new Map();
|
|
4951
|
+
for (const [c, k] of Object.entries(options.kinds ?? {})) {
|
|
4952
|
+
kinds.set(c, typeof k === "object" ? void 0 : k);
|
|
4953
|
+
if (typeof k === "object") lists.set(c, listOfKind(k));
|
|
4954
|
+
}
|
|
4691
4955
|
const index = new Map(names.map((c, i) => [c, i]));
|
|
4956
|
+
const direct = formulas.every((f) => f.key === void 0) && order.every((c) => readsFinished(columns, order, c, run.tree));
|
|
4692
4957
|
const p = new Printer(rec(Object.fromEntries(formulas.map((f, i) => [`f${String(i)}`, f.expr]))), options, {
|
|
4693
4958
|
columns: index,
|
|
4694
|
-
kinds
|
|
4959
|
+
kinds,
|
|
4960
|
+
lists,
|
|
4961
|
+
direct
|
|
4695
4962
|
});
|
|
4696
4963
|
const fns = [];
|
|
4697
4964
|
const decls = [];
|
|
4965
|
+
const blocks = /* @__PURE__ */ new Map();
|
|
4698
4966
|
try {
|
|
4699
4967
|
for (const f of formulas) {
|
|
4700
4968
|
const code = p.root(f.expr);
|
|
4701
|
-
const kind = code.v
|
|
4702
|
-
if (!kinds.has(f.column))
|
|
4703
|
-
|
|
4969
|
+
const { kind, list } = code.v;
|
|
4970
|
+
if (!kinds.has(f.column)) {
|
|
4971
|
+
kinds.set(f.column, kind);
|
|
4972
|
+
if (list !== void 0) lists.set(f.column, columnList(list));
|
|
4973
|
+
} else {
|
|
4974
|
+
const known = lists.get(f.column);
|
|
4975
|
+
if (kinds.get(f.column) !== kind || known === void 0 !== (list === void 0) || known !== void 0 && !fits(known, list)) throw new NotStandalone(`${formulaText(f)} gives a value of the kind ${kindText(kind, list)}, not of the kind ${kindText(kinds.get(f.column), known)} of its column`);
|
|
4976
|
+
}
|
|
4977
|
+
if (direct) {
|
|
4978
|
+
const at = `D[${String(index.get(f.column))}][o]`;
|
|
4979
|
+
blocks.set(f.column, code.v.safe && code.s.length === 0 ? [`${at} = ${code.v.code};`] : [
|
|
4980
|
+
"try {",
|
|
4981
|
+
...ind(code.s),
|
|
4982
|
+
` ${at} = ${code.v.code};`,
|
|
4983
|
+
"} catch {",
|
|
4984
|
+
` ${at} = E;`,
|
|
4985
|
+
"}"
|
|
4986
|
+
]);
|
|
4987
|
+
continue;
|
|
4988
|
+
}
|
|
4704
4989
|
const fn = p.fresh("cf");
|
|
4705
4990
|
fns.push(fn);
|
|
4706
4991
|
decls.push(...code.v.safe && code.s.length === 0 ? [`const ${fn} = (o) => ${code.v.code};`] : [
|
|
@@ -4718,37 +5003,56 @@ function emitSheetModule(columns, options) {
|
|
|
4718
5003
|
} catch (thrown) {
|
|
4719
5004
|
return reasonOf(thrown);
|
|
4720
5005
|
}
|
|
4721
|
-
|
|
4722
|
-
const own = names.map((c) => {
|
|
4723
|
-
const at = formulas.flatMap((f, i) => f.column === c && f.key !== void 0 ? [`${String(f.key)}: ${entry(i)}`] : []);
|
|
4724
|
-
return at.length === 0 ? "undefined" : `{ ${at.join(", ")} }`;
|
|
4725
|
-
});
|
|
4726
|
-
const cols = names.map((c) => {
|
|
4727
|
-
const i = formulas.findIndex((f) => f.column === c && f.key === void 0);
|
|
4728
|
-
return i < 0 ? "undefined" : entry(i);
|
|
4729
|
-
});
|
|
4730
|
-
p.use("SH");
|
|
5006
|
+
p.use(direct ? "DO" : "SH");
|
|
4731
5007
|
const orders = /* @__PURE__ */ new Map();
|
|
4732
|
-
const
|
|
5008
|
+
const headOf = (c) => {
|
|
4733
5009
|
const { reverse, post, back } = keyOrderOf(readsOf(columns, cells, c).directions);
|
|
4734
|
-
|
|
4735
|
-
if (!run.tree) return reverse ? `for (let o = n - 1; o >= 0; o--) SS.f(${at}, o);` : `for (let o = 0; o < n; o++) SS.f(${at}, o);`;
|
|
5010
|
+
if (!run.tree) return reverse ? "for (let o = n - 1; o >= 0; o--)" : "for (let o = 0; o < n; o++)";
|
|
4736
5011
|
const args = `${p.use("KO")}(H, RT, ${String(post)}, ${String(back)})`;
|
|
4737
5012
|
let keys = orders.get(args);
|
|
4738
5013
|
if (keys === void 0) {
|
|
4739
5014
|
keys = p.fresh("K");
|
|
4740
5015
|
orders.set(args, keys);
|
|
4741
5016
|
}
|
|
4742
|
-
return `for (const o of ${keys})
|
|
4743
|
-
}
|
|
5017
|
+
return `for (const o of ${keys})`;
|
|
5018
|
+
};
|
|
5019
|
+
let fill;
|
|
5020
|
+
if (direct) {
|
|
5021
|
+
const loops = [];
|
|
5022
|
+
for (const c of order) {
|
|
5023
|
+
const head = headOf(c);
|
|
5024
|
+
const last = loops.at(-1);
|
|
5025
|
+
if (last?.head === head && readsFinished(columns, order, c, run.tree, new Set(last.columns))) last.columns.push(c);
|
|
5026
|
+
else loops.push({
|
|
5027
|
+
head,
|
|
5028
|
+
columns: [c]
|
|
5029
|
+
});
|
|
5030
|
+
}
|
|
5031
|
+
fill = [];
|
|
5032
|
+
for (const { head, columns: cs } of loops) fill.push(`${head} {`, ...cs.flatMap((c) => ind(blocks.get(c))), "}");
|
|
5033
|
+
} else {
|
|
5034
|
+
const entry = (i) => `[${fns[i]}, ${String(3 * nestingOf(formulas[i].expr) + 16)}]`;
|
|
5035
|
+
const own = names.map((c) => {
|
|
5036
|
+
const at = formulas.flatMap((f, i) => f.column === c && f.key !== void 0 ? [`${String(f.key)}: ${entry(i)}`] : []);
|
|
5037
|
+
return at.length === 0 ? "undefined" : `{ ${at.join(", ")} }`;
|
|
5038
|
+
});
|
|
5039
|
+
const cols = names.map((c) => {
|
|
5040
|
+
const i = formulas.findIndex((f) => f.column === c && f.key === void 0);
|
|
5041
|
+
return i < 0 ? "undefined" : entry(i);
|
|
5042
|
+
});
|
|
5043
|
+
decls.push(`const SS = SH(n, [${cols.join(", ")}], [${own.join(", ")}], ${String(budget)});`, `const ${READ_CELL} = SS.r;`);
|
|
5044
|
+
fill = order.map((c) => `${headOf(c)} SS.f(${String(index.get(c))}, o);`);
|
|
5045
|
+
}
|
|
4744
5046
|
if (run.tree) p.treeParts.add("children");
|
|
5047
|
+
const resultOf = (c, read) => {
|
|
5048
|
+
const l = lists.get(c);
|
|
5049
|
+
return l === void 0 ? read : `${p.use("LC")}(${read}, ${String(depthOf(l))})`;
|
|
5050
|
+
};
|
|
4745
5051
|
return printModule(p, options, run, [
|
|
4746
|
-
...decls,
|
|
4747
|
-
`const SS = SH(n, [${cols.join(", ")}], [${own.join(", ")}], ${String(budget)});`,
|
|
4748
|
-
`const ${READ_CELL} = SS.r;`,
|
|
5052
|
+
...direct ? [`const D = [${names.map(() => "new Array(n)").join(", ")}];`] : decls,
|
|
4749
5053
|
...[...orders].map(([args, keys]) => `const ${keys} = ${args};`),
|
|
4750
5054
|
...fill,
|
|
4751
|
-
`return { ${names.map((c, i) => `${JSON.stringify(c)}: SS.o(${String(i)})`).join(", ")} };`
|
|
5055
|
+
`return { ${names.map((c, i) => `${JSON.stringify(c)}: ${resultOf(c, direct ? `DO(D[${String(i)}])` : `SS.o(${String(i)})`)}`).join(", ")} };`
|
|
4752
5056
|
]);
|
|
4753
5057
|
}
|
|
4754
5058
|
//#endregion
|