@mark1russell7/vex-compiler 0.11.1 → 0.12.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/README.md +2 -0
- package/build/index.d.ts +37 -5
- package/build/index.d.ts.map +1 -1
- package/build/index.js +792 -88
- package/build/index.js.map +1 -1
- package/package.json +4 -4
- package/src/facts.ts +2 -2
- package/src/index.ts +1 -1
- package/src/js.ts +4 -2
- package/src/lower.ts +1 -0
- package/src/module.ts +639 -69
- package/src/runtime.ts +2 -7
- package/src/sheet-order.ts +123 -0
package/build/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MAX_NESTING, addrExprs, axisTargets, children, compile, findMethod, fixAddr, fromDocument, isDocument, isFieldName, isListOp, isOpName, isStackOverflow, isVexList, liftsAt, listOf, lit, matchesKind, moveExpr, nestingOf, ok, resolveAddr, safeIs, shapeError, vexList } from "@mark1russell7/vex";
|
|
1
|
+
import { CELL_KIND, MAX_NESTING, addrExprs, axisTargets, children, compile, findMethod, fixAddr, fromDocument, isDocument, isFieldName, isListOp, isMove, isOpName, isStackOverflow, isVexList, liftsAt, listOf, lit, matchesKind, moveExpr, nestingOf, ok, rec, resolveAddr, safeIs, shapeError, vexList } from "@mark1russell7/vex";
|
|
2
2
|
//#region src/facts.ts
|
|
3
3
|
/**
|
|
4
4
|
* The facts of the options that the printer uses (docs/COMPILER.md §13.2). For each op site, they give the domain
|
|
@@ -12,7 +12,7 @@ import { MAX_NESTING, addrExprs, axisTargets, children, compile, findMethod, fix
|
|
|
12
12
|
const MAX_CALLS = 1e4;
|
|
13
13
|
/**
|
|
14
14
|
* The list ops that give a list, or read a position or a key of a list (LIST.SORT, LIST.TAKE, LIST.NTH, LIST.BUILD,
|
|
15
|
-
* LIST.GET).
|
|
15
|
+
* LIST.GET, LIST.INDEXOF, LIST.FILL).
|
|
16
16
|
*/
|
|
17
17
|
const ORDER_OPS = /* @__PURE__ */ new Set([
|
|
18
18
|
"sortBy",
|
|
@@ -22,7 +22,9 @@ const ORDER_OPS = /* @__PURE__ */ new Set([
|
|
|
22
22
|
"append",
|
|
23
23
|
"flat",
|
|
24
24
|
"without",
|
|
25
|
-
"get"
|
|
25
|
+
"get",
|
|
26
|
+
"indexOf",
|
|
27
|
+
"fill"
|
|
26
28
|
]);
|
|
27
29
|
/**
|
|
28
30
|
* This function gives the reason that tier 2 cannot take a program with lists as values. The reason is the axis `list`,
|
|
@@ -321,7 +323,7 @@ function printJs(p, facts) {
|
|
|
321
323
|
const f = `f${x.name}_${st.domain}`;
|
|
322
324
|
fns.set(f, `${domain(st.domain)}.ops[${expr(constant(x.name))}].fn`);
|
|
323
325
|
attempt(`${f}(${values.join(", ")})`, indent);
|
|
324
|
-
const kind = st.returns;
|
|
326
|
+
const kind = primitiveOf(st.returns);
|
|
325
327
|
if (kind === void 0) {
|
|
326
328
|
resultChecks(out, facts.checks, indent);
|
|
327
329
|
return;
|
|
@@ -1339,6 +1341,7 @@ var Lowerer = class {
|
|
|
1339
1341
|
case "flat":
|
|
1340
1342
|
case "without":
|
|
1341
1343
|
case "get":
|
|
1344
|
+
case "indexOf":
|
|
1342
1345
|
/* v8 ignore next -- @preserve */
|
|
1343
1346
|
throw new Unsupported(`the list op "${op}"`);
|
|
1344
1347
|
}
|
|
@@ -1733,13 +1736,7 @@ function createRuntime(options) {
|
|
|
1733
1736
|
}
|
|
1734
1737
|
},
|
|
1735
1738
|
opts: (value) => value?.strict,
|
|
1736
|
-
rec(names, values)
|
|
1737
|
-
const out = {};
|
|
1738
|
-
names.forEach((n, i) => {
|
|
1739
|
-
out[n] = values[i];
|
|
1740
|
-
});
|
|
1741
|
-
return Object.freeze(out);
|
|
1742
|
-
},
|
|
1739
|
+
rec: (names, values) => Object.freeze(Object.fromEntries(names.map((n, i) => [n, values[i]]))),
|
|
1743
1740
|
list: (keys, values) => vexList(keys.map((key, i) => ({
|
|
1744
1741
|
key,
|
|
1745
1742
|
result: {
|
|
@@ -1843,6 +1840,107 @@ function tiered(source, options = {}) {
|
|
|
1843
1840
|
};
|
|
1844
1841
|
}
|
|
1845
1842
|
//#endregion
|
|
1843
|
+
//#region src/sheet-order.ts
|
|
1844
|
+
/**
|
|
1845
|
+
* The static order of the cells of a sheet in a module (COMPILE.MODULE.SHEET). It is the order of `SheetRun.fill` in
|
|
1846
|
+
* `packages/core/src/sheet.ts` (SHEET.FILL). Each column comes after the columns that it reads. The keys of a column
|
|
1847
|
+
* follow its reads from itself. The order changes only the speed, not a result (SHEET.ORDER).
|
|
1848
|
+
*/
|
|
1849
|
+
/** This function tells if the data of an extension is a cell reference: a column, and an address of fixed moves. */
|
|
1850
|
+
const isCellRef = (u) => typeof u === "object" && u !== null && typeof u.column === "string" && Array.isArray(u.at) && u.at.every((m) => isMove(m) && moveExpr(m) === void 0);
|
|
1851
|
+
/** This function adds the direction of one move or one axis of a read of a column from itself. */
|
|
1852
|
+
function addDirection(d, m) {
|
|
1853
|
+
if (m.t === "reverse") {
|
|
1854
|
+
addDirection(d, m.axis);
|
|
1855
|
+
return;
|
|
1856
|
+
}
|
|
1857
|
+
if (m.t === "offset" || m.t === "window") {
|
|
1858
|
+
const steps = m.t === "offset" ? [m.d.find((x) => x !== 0) ?? 0] : [m.from, m.to];
|
|
1859
|
+
if (steps.some((x) => x < 0)) d.add("before");
|
|
1860
|
+
if (steps.some((x) => x > 0)) d.add("after");
|
|
1861
|
+
} else if (m.t === "children" || m.t === "descendants") d.add("down");
|
|
1862
|
+
}
|
|
1863
|
+
/** This function gives the base axis of an axis, and the tests of its `where` axes. */
|
|
1864
|
+
function partsOf$1(axis) {
|
|
1865
|
+
const tests = [];
|
|
1866
|
+
let base = axis;
|
|
1867
|
+
while (base.t === "where") {
|
|
1868
|
+
tests.push(base.test);
|
|
1869
|
+
base = base.axis;
|
|
1870
|
+
}
|
|
1871
|
+
return {
|
|
1872
|
+
base,
|
|
1873
|
+
tests
|
|
1874
|
+
};
|
|
1875
|
+
}
|
|
1876
|
+
/**
|
|
1877
|
+
* This function gives the columns that the formulas of the column `self` read with a cell reference: its formula and
|
|
1878
|
+
* the formulas of its cells. It also gives the directions of their reads of the column itself. A read in the body or
|
|
1879
|
+
* in a test of an axis also has the direction of that axis.
|
|
1880
|
+
*/
|
|
1881
|
+
function readsOf(columns, cells, self) {
|
|
1882
|
+
const read = /* @__PURE__ */ new Set();
|
|
1883
|
+
const directions = /* @__PURE__ */ new Set();
|
|
1884
|
+
const formula = columns.get(self);
|
|
1885
|
+
const todo = [...formula === void 0 ? [] : [formula], ...cells.get(self)?.values() ?? []].filter((e) => shapeError(e) === void 0).map((e) => [e, void 0]);
|
|
1886
|
+
for (let item = todo.pop(); item !== void 0; item = todo.pop()) {
|
|
1887
|
+
const [e, axis] = item;
|
|
1888
|
+
if (e.tag === "each" || e.tag === "fold") {
|
|
1889
|
+
const { base, tests } = partsOf$1(e.axis);
|
|
1890
|
+
for (const x of [...e.tag === "each" ? [e.body] : Object.values(e.step), ...tests]) todo.push([x, base]);
|
|
1891
|
+
const list = listOf(e.axis);
|
|
1892
|
+
for (const x of [
|
|
1893
|
+
...e.tag === "fold" ? [...Object.values(e.init), e.result] : [],
|
|
1894
|
+
...addrExprs(e.at),
|
|
1895
|
+
...list === void 0 ? [] : [list]
|
|
1896
|
+
]) todo.push([x, axis]);
|
|
1897
|
+
} else if (e.tag !== "ext") for (const child of children(e)) todo.push([child, axis]);
|
|
1898
|
+
else if (e.kind === CELL_KIND && isCellRef(e.data)) {
|
|
1899
|
+
read.add(e.data.column);
|
|
1900
|
+
if (e.data.column === self) for (const m of [...axis === void 0 ? [] : [axis], ...e.data.at]) addDirection(directions, m);
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
return {
|
|
1904
|
+
columns: read,
|
|
1905
|
+
directions
|
|
1906
|
+
};
|
|
1907
|
+
}
|
|
1908
|
+
/**
|
|
1909
|
+
* This function gives the columns of a sheet in an order where each column comes after the columns that it reads. The
|
|
1910
|
+
* columns of a cycle keep their order. The columns with formulas of cells only come last.
|
|
1911
|
+
*/
|
|
1912
|
+
function columnOrder(columns, cells) {
|
|
1913
|
+
const names = /* @__PURE__ */ new Set([...columns.keys(), ...cells.keys()]);
|
|
1914
|
+
let left = [...names].map((c) => ({
|
|
1915
|
+
c,
|
|
1916
|
+
needs: [...readsOf(columns, cells, c).columns].filter((x) => x !== c && names.has(x))
|
|
1917
|
+
}));
|
|
1918
|
+
const order = [];
|
|
1919
|
+
const placed = /* @__PURE__ */ new Set();
|
|
1920
|
+
while (left.length > 0) {
|
|
1921
|
+
const ready = left.filter((x) => x.needs.every((n) => placed.has(n)));
|
|
1922
|
+
for (const x of ready.length > 0 ? ready : left.slice(0, 1)) {
|
|
1923
|
+
order.push(x.c);
|
|
1924
|
+
placed.add(x.c);
|
|
1925
|
+
}
|
|
1926
|
+
left = left.filter((x) => !placed.has(x.c));
|
|
1927
|
+
}
|
|
1928
|
+
return order;
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* This function gives the order of the keys of a column. A read of a later key reverses the order. In a tree, a read of
|
|
1932
|
+
* the children puts the children before their parent, as `keyOrder` of `SheetRun` does.
|
|
1933
|
+
*/
|
|
1934
|
+
function keyOrderOf(d) {
|
|
1935
|
+
const reverse = d.has("after") && !d.has("before");
|
|
1936
|
+
const post = d.has("down");
|
|
1937
|
+
return {
|
|
1938
|
+
reverse,
|
|
1939
|
+
post,
|
|
1940
|
+
back: post !== reverse
|
|
1941
|
+
};
|
|
1942
|
+
}
|
|
1943
|
+
//#endregion
|
|
1846
1944
|
//#region src/module.ts
|
|
1847
1945
|
/**
|
|
1848
1946
|
* Standalone modules (docs/COMPILER.md §4.4 and §6). `emitModule` prints a program as an ES module that has no import of
|
|
@@ -1861,14 +1959,28 @@ function tiered(source, options = {}) {
|
|
|
1861
1959
|
* 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
|
|
1862
1960
|
* nodes with the same expression share the function.
|
|
1863
1961
|
*
|
|
1864
|
-
* The printer takes a part of the language. It takes an array space of records, the fields and
|
|
1865
|
-
* schema
|
|
1866
|
-
*
|
|
1962
|
+
* The printer takes a part of the language. It takes an array space or a tree space of records, and the fields and
|
|
1963
|
+
* the variables of the schema. It takes the axes `all`, `others` and `window`. A tree space also has the move `parent` and the
|
|
1964
|
+
* axes of a tree (COMPILE.MODULE.TREE). The kinds of each op site must decide its op. Another construct gives a
|
|
1965
|
+
* reason, not code.
|
|
1966
|
+
*
|
|
1967
|
+
* `emitSheetModule` prints the formula columns of a sheet as one module (COMPILE.MODULE.SHEET). Each column is an array
|
|
1968
|
+
* of results, and the run fills the arrays in the static order of `SheetRun.fill`. A read of a cell that is not finished
|
|
1969
|
+
* starts the search of `SheetRun`. Thus each cell on a cycle gives the error of Vex.
|
|
1867
1970
|
*/
|
|
1868
1971
|
/** The error of a construct that a standalone module does not have. */
|
|
1869
1972
|
var NotStandalone = class extends Error {};
|
|
1870
1973
|
/** The levels of a constant of the run. */
|
|
1871
1974
|
const NO_DEPS = /* @__PURE__ */ new Set();
|
|
1975
|
+
/**
|
|
1976
|
+
* The level of a read of a cell (COMPILE.MODULE.SHEET). The results of the cells change in a run. Thus a value that reads
|
|
1977
|
+
* a cell is not a constant or a column of the run. The level is far from the other levels.
|
|
1978
|
+
*/
|
|
1979
|
+
const CELLS = -1;
|
|
1980
|
+
/** The name of the function of a run of a sheet that reads a cell: its column and its position. */
|
|
1981
|
+
const READ_CELL = "RC";
|
|
1982
|
+
/** This pattern finds a read of a cell in the code of a value: the read, or a printed function that can read a cell. */
|
|
1983
|
+
const READS_IN_CODE = /\b(RC|fn\d+)\(/;
|
|
1872
1984
|
/** This function gives the levels of some codes together. */
|
|
1873
1985
|
const depsOf = (...xs) => new Set(xs.flatMap((x) => [...x.v.deps]));
|
|
1874
1986
|
/** This function gives the statements of some codes, in order. */
|
|
@@ -1899,6 +2011,183 @@ const HELPERS = {
|
|
|
1899
2011
|
].join("\n"),
|
|
1900
2012
|
TK: "function TK(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(0, n) : F(); }",
|
|
1901
2013
|
NT: "function NT(l, i, s) { const o = V(l, s); return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F(); }",
|
|
2014
|
+
PT: [
|
|
2015
|
+
"function PT(p, n) {",
|
|
2016
|
+
" if (!Array.isArray(p) || p.length !== n) C(\"the parents are not an array with one parent for each record\");",
|
|
2017
|
+
" const P = new Int32Array(n), s = new Uint8Array(n);",
|
|
2018
|
+
" for (let i = 0; i < n; i++) {",
|
|
2019
|
+
" const x = p[i];",
|
|
2020
|
+
" if (x !== -1 && !(Number.isInteger(x) && x >= 0 && x < n)) C(`the parent of the record ${i} is not -1 or the position of a record`);",
|
|
2021
|
+
" P[i] = x;",
|
|
2022
|
+
" }",
|
|
2023
|
+
" for (let i = 0; i < n; i++) {",
|
|
2024
|
+
" let k = i;",
|
|
2025
|
+
" while (k >= 0 && s[k] === 0) {",
|
|
2026
|
+
" s[k] = 1;",
|
|
2027
|
+
" k = P[k];",
|
|
2028
|
+
" }",
|
|
2029
|
+
" if (k >= 0 && s[k] === 1) C(`the parents of the record ${i} make a cycle`);",
|
|
2030
|
+
" for (k = i; k >= 0 && s[k] === 1; k = P[k]) s[k] = 2;",
|
|
2031
|
+
" }",
|
|
2032
|
+
" return P;",
|
|
2033
|
+
"}"
|
|
2034
|
+
].join("\n"),
|
|
2035
|
+
CH: [
|
|
2036
|
+
"function CH(P, n) {",
|
|
2037
|
+
" const h = [], r = [];",
|
|
2038
|
+
" for (let i = 0; i < n; i++) h.push([]);",
|
|
2039
|
+
" for (let i = 0; i < n; i++) (P[i] < 0 ? r : h[P[i]]).push(i);",
|
|
2040
|
+
" return [h, r];",
|
|
2041
|
+
"}"
|
|
2042
|
+
].join("\n"),
|
|
2043
|
+
SG: [
|
|
2044
|
+
"function SG(P, h, r, n) {",
|
|
2045
|
+
" const g = [], s = new Int32Array(n);",
|
|
2046
|
+
" for (let i = 0; i < n; i++) g.push(P[i] < 0 ? r : h[P[i]]);",
|
|
2047
|
+
" for (const x of [r, ...h]) for (let q = 0; q < x.length; q++) s[x[q]] = q;",
|
|
2048
|
+
" return [g, s];",
|
|
2049
|
+
"}"
|
|
2050
|
+
].join("\n"),
|
|
2051
|
+
PR: [
|
|
2052
|
+
"function PR(h, r, n) {",
|
|
2053
|
+
" const o = [], at = new Int32Array(n), z = new Int32Array(n), st = [];",
|
|
2054
|
+
" for (let q = r.length - 1; q >= 0; q--) st.push(r[q]);",
|
|
2055
|
+
" while (st.length > 0) {",
|
|
2056
|
+
" const k = st.pop();",
|
|
2057
|
+
" at[k] = o.length;",
|
|
2058
|
+
" o.push(k);",
|
|
2059
|
+
" for (let q = h[k].length - 1; q >= 0; q--) st.push(h[k][q]);",
|
|
2060
|
+
" }",
|
|
2061
|
+
" for (let q = n - 1; q >= 0; q--) {",
|
|
2062
|
+
" const k = o[q];",
|
|
2063
|
+
" z[k] = 1;",
|
|
2064
|
+
" for (const c of h[k]) z[k] += z[c];",
|
|
2065
|
+
" }",
|
|
2066
|
+
" return [o, at, z];",
|
|
2067
|
+
"}"
|
|
2068
|
+
].join("\n"),
|
|
2069
|
+
UP: "function UP(P, k) { const p = P[k]; return p < 0 ? F() : p; }",
|
|
2070
|
+
SO: "function SO(g, s, k, d) { const t = g[k][s[k] + d]; return t === undefined ? F() : t; }",
|
|
2071
|
+
AN: "function AN(P, k) { const o = []; for (let p = P[k]; p >= 0; p = P[p]) o.push(p); return o; }",
|
|
2072
|
+
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; }",
|
|
2073
|
+
SH: [
|
|
2074
|
+
"function SH(n, cols, own, budget) {",
|
|
2075
|
+
" const pg = (c, k) => {",
|
|
2076
|
+
" const o = own[c];",
|
|
2077
|
+
" const g = o === undefined ? undefined : o[k];",
|
|
2078
|
+
" return g === undefined ? cols[c] : g;",
|
|
2079
|
+
" };",
|
|
2080
|
+
" const D = [];",
|
|
2081
|
+
" for (let c = 0; c < cols.length; c++) {",
|
|
2082
|
+
" const d = new Array(n);",
|
|
2083
|
+
" for (let k = 0; k < n; k++) if (pg(c, k) === undefined) d[k] = E;",
|
|
2084
|
+
" D.push(d);",
|
|
2085
|
+
" }",
|
|
2086
|
+
" const seen = new Map(), stack = [], FILL = {};",
|
|
2087
|
+
" let top = FILL, missed = false, next = 0, used = 0;",
|
|
2088
|
+
" const start = (c, k) => {",
|
|
2089
|
+
" const i = next++;",
|
|
2090
|
+
" const v = { id: c * n + k, c, k, i, low: i, re: false, r: E, p: false, need: undefined };",
|
|
2091
|
+
" seen.set(v.id, v);",
|
|
2092
|
+
" stack.push(v);",
|
|
2093
|
+
" return v;",
|
|
2094
|
+
" };",
|
|
2095
|
+
" const ev = (v, g) => {",
|
|
2096
|
+
" const caller = top;",
|
|
2097
|
+
" v.p = false;",
|
|
2098
|
+
" v.need = undefined;",
|
|
2099
|
+
" top = v;",
|
|
2100
|
+
" used += g[1];",
|
|
2101
|
+
" const x = g[0](v.k);",
|
|
2102
|
+
" used -= g[1];",
|
|
2103
|
+
" top = caller;",
|
|
2104
|
+
" if (!v.p) v.r = x;",
|
|
2105
|
+
" };",
|
|
2106
|
+
" const end = (v, caller) => {",
|
|
2107
|
+
" if (v.low === v.i) {",
|
|
2108
|
+
" const ms = stack.splice(stack.lastIndexOf(v));",
|
|
2109
|
+
" const cycle = ms.length > 1 || v.re;",
|
|
2110
|
+
" for (const m of ms) {",
|
|
2111
|
+
" seen.delete(m.id);",
|
|
2112
|
+
" D[m.c][m.k] = cycle ? E : m.r;",
|
|
2113
|
+
" }",
|
|
2114
|
+
" } else if (caller !== undefined) caller.low = Math.min(caller.low, v.low);",
|
|
2115
|
+
" };",
|
|
2116
|
+
" const r = (c, k) => {",
|
|
2117
|
+
" const d = D[c][k];",
|
|
2118
|
+
" if (d !== undefined) return d;",
|
|
2119
|
+
" const caller = top;",
|
|
2120
|
+
" if (caller === FILL) {",
|
|
2121
|
+
" missed = true;",
|
|
2122
|
+
" return E;",
|
|
2123
|
+
" }",
|
|
2124
|
+
" if (caller.p) return E;",
|
|
2125
|
+
" const s = seen.get(c * n + k);",
|
|
2126
|
+
" if (s !== undefined) {",
|
|
2127
|
+
" caller.low = Math.min(caller.low, s.i);",
|
|
2128
|
+
" s.re = true;",
|
|
2129
|
+
" return E;",
|
|
2130
|
+
" }",
|
|
2131
|
+
" const g = pg(c, k);",
|
|
2132
|
+
" const v = start(c, k);",
|
|
2133
|
+
" if (used + g[1] > budget) {",
|
|
2134
|
+
" caller.p = true;",
|
|
2135
|
+
" caller.need = v;",
|
|
2136
|
+
" return E;",
|
|
2137
|
+
" }",
|
|
2138
|
+
" ev(v, g);",
|
|
2139
|
+
" if (v.p) {",
|
|
2140
|
+
" caller.p = true;",
|
|
2141
|
+
" caller.need = v;",
|
|
2142
|
+
" return E;",
|
|
2143
|
+
" }",
|
|
2144
|
+
" end(v, caller);",
|
|
2145
|
+
" const x = D[c][k];",
|
|
2146
|
+
" return x === undefined ? E : x;",
|
|
2147
|
+
" };",
|
|
2148
|
+
" const drive = (c, k) => {",
|
|
2149
|
+
" const work = [start(c, k)];",
|
|
2150
|
+
" for (let v = work[0]; v !== undefined; v = work[work.length - 1]) {",
|
|
2151
|
+
" ev(v, pg(v.c, v.k));",
|
|
2152
|
+
" if (v.p) {",
|
|
2153
|
+
" for (let q = v.need; q !== undefined; q = q.need) work.push(q);",
|
|
2154
|
+
" continue;",
|
|
2155
|
+
" }",
|
|
2156
|
+
" work.pop();",
|
|
2157
|
+
" end(v, work[work.length - 1]);",
|
|
2158
|
+
" }",
|
|
2159
|
+
" };",
|
|
2160
|
+
" const f = (c, k) => {",
|
|
2161
|
+
" if (D[c][k] !== undefined) return;",
|
|
2162
|
+
" missed = false;",
|
|
2163
|
+
" const x = pg(c, k)[0](k);",
|
|
2164
|
+
" if (missed) drive(c, k);",
|
|
2165
|
+
" else D[c][k] = x;",
|
|
2166
|
+
" };",
|
|
2167
|
+
" const o = (c) => {",
|
|
2168
|
+
" const d = D[c], out = [];",
|
|
2169
|
+
" for (let k = 0; k < n; k++) out.push(d[k] === E ? VEX_ERROR : d[k]);",
|
|
2170
|
+
" return out;",
|
|
2171
|
+
" };",
|
|
2172
|
+
" return { r, f, o };",
|
|
2173
|
+
"}"
|
|
2174
|
+
].join("\n"),
|
|
2175
|
+
KO: [
|
|
2176
|
+
"function KO(h, r, post, back) {",
|
|
2177
|
+
" const o = [], st = [];",
|
|
2178
|
+
" const push = (x) => {",
|
|
2179
|
+
" if (back) for (let q = 0; q < x.length; q++) st.push(x[q]);",
|
|
2180
|
+
" else for (let q = x.length - 1; q >= 0; q--) st.push(x[q]);",
|
|
2181
|
+
" };",
|
|
2182
|
+
" push(r);",
|
|
2183
|
+
" while (st.length > 0) {",
|
|
2184
|
+
" const k = st.pop();",
|
|
2185
|
+
" o.push(k);",
|
|
2186
|
+
" push(h[k]);",
|
|
2187
|
+
" }",
|
|
2188
|
+
" return post ? o.reverse() : o;",
|
|
2189
|
+
"}"
|
|
2190
|
+
].join("\n"),
|
|
1902
2191
|
M1: [
|
|
1903
2192
|
"function M1(f) {",
|
|
1904
2193
|
" const m = new Map();",
|
|
@@ -1937,6 +2226,11 @@ const HELPERS = {
|
|
|
1937
2226
|
].join("\n")
|
|
1938
2227
|
};
|
|
1939
2228
|
/**
|
|
2229
|
+
* This list has the globals of JavaScript that the code of a module reads. This code is the helpers, the run and the
|
|
2230
|
+
* copies of the ops of the test domains. A test reads the text of the modules of the golden files and checks this list (V-060).
|
|
2231
|
+
*/
|
|
2232
|
+
const MODULE_GLOBALS = new Set("Array Error Int32Array Map Math Number Object Uint8Array undefined".split(" "));
|
|
2233
|
+
/**
|
|
1940
2234
|
* This function gives the code of the helper S, a node of the memo that keeps its items (Printer.#cached). The helper
|
|
1941
2235
|
* has only the parts that the nodes of the module need:
|
|
1942
2236
|
*
|
|
@@ -2039,8 +2333,36 @@ const NEEDS = {
|
|
|
2039
2333
|
V: ["F"],
|
|
2040
2334
|
SB: ["V", "F"],
|
|
2041
2335
|
TK: ["V", "F"],
|
|
2042
|
-
NT: ["V", "F"]
|
|
2336
|
+
NT: ["V", "F"],
|
|
2337
|
+
PT: ["C"],
|
|
2338
|
+
UP: ["F"],
|
|
2339
|
+
SO: ["F"],
|
|
2340
|
+
CH: [],
|
|
2341
|
+
SG: [],
|
|
2342
|
+
PR: [],
|
|
2343
|
+
AN: [],
|
|
2344
|
+
BF: [],
|
|
2345
|
+
SH: [],
|
|
2346
|
+
KO: []
|
|
2043
2347
|
};
|
|
2348
|
+
/** The statement of each part of a tree in a run, with the helper that computes it. */
|
|
2349
|
+
const TREE_PARTS = {
|
|
2350
|
+
children: ["CH", "const [H, RT] = CH(P, n);"],
|
|
2351
|
+
siblings: ["SG", "const [GP, SI] = SG(P, H, RT, n);"],
|
|
2352
|
+
order: ["PR", "const [PO, PI, SZ] = PR(H, RT, n);"]
|
|
2353
|
+
};
|
|
2354
|
+
/** The axes of a tree space in a module, and the axes that a module of an array space does not have. */
|
|
2355
|
+
const TREE_AXES = /* @__PURE__ */ new Set([
|
|
2356
|
+
"children",
|
|
2357
|
+
"ancestors",
|
|
2358
|
+
"descendants",
|
|
2359
|
+
"siblings",
|
|
2360
|
+
"successors",
|
|
2361
|
+
"predecessors",
|
|
2362
|
+
"reachable"
|
|
2363
|
+
]);
|
|
2364
|
+
/** This function tells if an axis has its targets in a range of an array of positions in a tree space. */
|
|
2365
|
+
const isTreeAxis = (a) => a.t === "window" || TREE_AXES.has(a.t);
|
|
2044
2366
|
/** The list ops of a standalone module. */
|
|
2045
2367
|
const LIST_OPS = /* @__PURE__ */ new Set([
|
|
2046
2368
|
"count",
|
|
@@ -2065,13 +2387,23 @@ const BOOLEAN_LIST_OPS = /* @__PURE__ */ new Set([
|
|
|
2065
2387
|
"all",
|
|
2066
2388
|
"none"
|
|
2067
2389
|
]);
|
|
2068
|
-
/** The list ops that build a list from lists (LIST.BUILD),
|
|
2390
|
+
/** The list ops that build a list from lists (LIST.BUILD), `get` (LIST.GET) and `indexOf` (LIST.INDEXOF). A module gives a reason for them. */
|
|
2069
2391
|
const BUILD_OPS = /* @__PURE__ */ new Set([
|
|
2070
2392
|
"drop",
|
|
2071
2393
|
"append",
|
|
2072
2394
|
"flat",
|
|
2073
2395
|
"without",
|
|
2074
|
-
"get"
|
|
2396
|
+
"get",
|
|
2397
|
+
"indexOf"
|
|
2398
|
+
]);
|
|
2399
|
+
/** This function tells if the step of a list op throws for a value of the kind `kind`, which is the wrong kind. */
|
|
2400
|
+
const stepThrows = (op, kind) => NUMBER_STEPS.has(op) && kind !== "number" || BOOLEAN_LIST_OPS.has(op) && kind !== "boolean";
|
|
2401
|
+
/** The list ops whose step takes a number. */
|
|
2402
|
+
const NUMBER_STEPS = /* @__PURE__ */ new Set([
|
|
2403
|
+
"sum",
|
|
2404
|
+
"mean",
|
|
2405
|
+
"min",
|
|
2406
|
+
"max"
|
|
2075
2407
|
]);
|
|
2076
2408
|
/** This function gives the kind of the result of a list op: first gives the kind of its items. */
|
|
2077
2409
|
const listKind = (op, item) => NUMBER_LIST_OPS.has(op) ? "number" : BOOLEAN_LIST_OPS.has(op) ? "boolean" : item;
|
|
@@ -2311,6 +2643,18 @@ function inlineForm(src) {
|
|
|
2311
2643
|
}
|
|
2312
2644
|
/** This function gives the text of an inline form without spaces, with `#i` for the parameter `at(i)`. */
|
|
2313
2645
|
const patternOf = (form, at) => form.parts.map((p) => typeof p === "number" ? `#${at(p)}` : p.replaceAll(/\s/g, "")).join("");
|
|
2646
|
+
/** The reserved words of the strict code of a module, and the two names that strict code does not bind. */
|
|
2647
|
+
const RESERVED = new Set("await break case catch class const continue debugger default delete do else enum export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield arguments eval".split(" "));
|
|
2648
|
+
/**
|
|
2649
|
+
* This function gives the reason that a name cannot be the name of the run, or `undefined` for a good name (V-060). A
|
|
2650
|
+
* reserved word does not parse, and a second declaration of a name of the module does not parse. The function of the
|
|
2651
|
+
* run with the name of a global of `MODULE_GLOBALS` hides that global from the code of the module.
|
|
2652
|
+
*/
|
|
2653
|
+
function nameReason(name, declared) {
|
|
2654
|
+
if (RESERVED.has(name)) return `the name "${name}" is a reserved word of JavaScript`;
|
|
2655
|
+
if (MODULE_GLOBALS.has(name)) return `the name "${name}" is a global that a module reads`;
|
|
2656
|
+
return declared.has(name) ? `the name "${name}" is a name of the module` : void 0;
|
|
2657
|
+
}
|
|
2314
2658
|
var Printer = class {
|
|
2315
2659
|
#facts;
|
|
2316
2660
|
#domains;
|
|
@@ -2327,6 +2671,11 @@ var Printer = class {
|
|
|
2327
2671
|
memos = false;
|
|
2328
2672
|
/** The parts of the helper S that the nodes of the memo that keep their items need (helperS). */
|
|
2329
2673
|
features = /* @__PURE__ */ new Set();
|
|
2674
|
+
/** True for a tree space, and the parts of the tree that the program reads (COMPILE.MODULE.TREE). */
|
|
2675
|
+
#tree;
|
|
2676
|
+
treeParts = /* @__PURE__ */ new Set();
|
|
2677
|
+
/** The columns of a sheet, for a module of a sheet (COMPILE.MODULE.SHEET). */
|
|
2678
|
+
#sheet;
|
|
2330
2679
|
#names = 0;
|
|
2331
2680
|
/** The number of the next bound function, and of the next printed function. */
|
|
2332
2681
|
#fnIds = 0;
|
|
@@ -2361,26 +2710,34 @@ var Printer = class {
|
|
|
2361
2710
|
#wantList = /* @__PURE__ */ new WeakSet();
|
|
2362
2711
|
/** The facts of the list of each function of the memo whose value is a list. */
|
|
2363
2712
|
#lists = /* @__PURE__ */ new Map();
|
|
2364
|
-
constructor(expr, options) {
|
|
2713
|
+
constructor(expr, options, sheet) {
|
|
2365
2714
|
this.#domains = options.domains ?? [];
|
|
2366
2715
|
this.#facts = factsOf({ domains: this.#domains });
|
|
2367
2716
|
this.#schema = options.schema;
|
|
2368
2717
|
this.#memo = memoOf(expr);
|
|
2718
|
+
this.#tree = options.space === "tree";
|
|
2719
|
+
this.#sheet = sheet;
|
|
2369
2720
|
}
|
|
2370
2721
|
use(name) {
|
|
2371
2722
|
this.helpers.add(name);
|
|
2372
2723
|
return name;
|
|
2373
2724
|
}
|
|
2725
|
+
/** This method gives the name of a part of the tree, for example `H` for the children, and marks the part as read. */
|
|
2726
|
+
#part(part, name) {
|
|
2727
|
+
this.treeParts.add(part);
|
|
2728
|
+
return name;
|
|
2729
|
+
}
|
|
2374
2730
|
/**
|
|
2375
2731
|
* This method gives the check of a result of a kind, or undefined for a result that needs no check. A number must be
|
|
2376
2732
|
* finite, and the domain of the kind can check the value with valid. The law checker of the test kit checks the
|
|
2377
2733
|
* declaration `returns` of each op (KIND.RETURNS), so the module trusts the kind itself.
|
|
2378
2734
|
*/
|
|
2379
2735
|
check(kind) {
|
|
2380
|
-
const
|
|
2736
|
+
const d = this.#facts.checkOf(kind);
|
|
2737
|
+
if (d === void 0) throw new NotStandalone(`a domain without a primitive type comes before the domain of a ${kind}`);
|
|
2738
|
+
const plain = `k_${kind.replace(":", "_")}`;
|
|
2739
|
+
const name = /^[\w$]+$/.test(plain) ? plain : `k_d${String(d)}`;
|
|
2381
2740
|
if (!this.top.has(name)) {
|
|
2382
|
-
const d = this.#facts.checkOf(kind);
|
|
2383
|
-
if (d === void 0) throw new NotStandalone(`a domain without a primitive type comes before the domain of a ${kind}`);
|
|
2384
2741
|
const domain = this.#domains[d];
|
|
2385
2742
|
const valid = domain?.valid === void 0 ? void 0 : this.#valid(d, domain.name, kind, domain.valid);
|
|
2386
2743
|
const conds = [...kind === "number" ? ["Number.isFinite(x)"] : [], ...valid === void 0 ? [] : [`${valid}(x)`]];
|
|
@@ -2561,7 +2918,7 @@ var Printer = class {
|
|
|
2561
2918
|
* from outside is a constant of the run, so its code can be outside the loop.
|
|
2562
2919
|
*/
|
|
2563
2920
|
#columnar(e, c, ctx) {
|
|
2564
|
-
if (ctx.level < 2 || !c.v.deps.has(ctx.level) || this.#work(e, ctx.fns) < COLUMN_WORK) return false;
|
|
2921
|
+
if (ctx.level < 2 || c.v.deps.has(CELLS) || !c.v.deps.has(ctx.level) || this.#work(e, ctx.fns) < COLUMN_WORK) return false;
|
|
2565
2922
|
const scope = this.#callScope(c, ctx);
|
|
2566
2923
|
if (c.v.deps.size !== 1 && scope === void 0) return false;
|
|
2567
2924
|
if (this.#keyed(e)) return false;
|
|
@@ -2646,8 +3003,8 @@ var Printer = class {
|
|
|
2646
3003
|
const names = Object.keys(e.fields);
|
|
2647
3004
|
const raw = Object.values(e.fields).map((x) => this.expr(x, ctx));
|
|
2648
3005
|
const deps = depsOf(...raw);
|
|
2649
|
-
const fields = this.#settle(raw, deps, Object.values(e.fields));
|
|
2650
|
-
const code = `({ ${fields.map((x, i) => `${JSON.stringify(names[i])}: ${x.v.code}`).join(", ")} })`;
|
|
3006
|
+
const fields = this.#inOrder(this.#settle(raw, deps, Object.values(e.fields)), true);
|
|
3007
|
+
const code = `({ ${fields.map((x, i) => `${names[i] === "__proto__" ? "[\"__proto__\"]" : JSON.stringify(names[i])}: ${x.v.code}`).join(", ")} })`;
|
|
2651
3008
|
return {
|
|
2652
3009
|
s: stmtsOf(...fields),
|
|
2653
3010
|
v: {
|
|
@@ -2662,10 +3019,87 @@ var Printer = class {
|
|
|
2662
3019
|
case "each": return this.#eachList(e, ctx);
|
|
2663
3020
|
case "fold": return this.#fold(e, ctx);
|
|
2664
3021
|
case "iterate": return this.#iterate(e, ctx);
|
|
2665
|
-
case "ext":
|
|
3022
|
+
case "ext": return this.#cell(e, ctx);
|
|
2666
3023
|
}
|
|
2667
3024
|
}
|
|
2668
3025
|
/**
|
|
3026
|
+
* This method gives the code of a read of a cell in a module of a sheet (COMPILE.MODULE.SHEET). The value is the result
|
|
3027
|
+
* of the cell, which can be the error sentinel, so the read throws its error. A read that is not a cell reference, or
|
|
3028
|
+
* of a column that the sheet does not have, gives an error, as in `SheetRun`.
|
|
3029
|
+
*/
|
|
3030
|
+
#cell(e, ctx) {
|
|
3031
|
+
const sheet = this.#sheet;
|
|
3032
|
+
if (sheet === void 0 || e.kind !== CELL_KIND) throw new NotStandalone(`the extension "${e.kind}"`);
|
|
3033
|
+
const F = this.use("F");
|
|
3034
|
+
if (!isCellRef(e.data)) return bare({
|
|
3035
|
+
code: `${F}()`,
|
|
3036
|
+
kind: void 0,
|
|
3037
|
+
safe: false,
|
|
3038
|
+
deps: /* @__PURE__ */ new Set([CELLS])
|
|
3039
|
+
});
|
|
3040
|
+
const { column, at } = e.data;
|
|
3041
|
+
const deps = /* @__PURE__ */ new Set([...this.#addressDeps(at, ctx, []), CELLS]);
|
|
3042
|
+
const index = sheet.columns.get(column);
|
|
3043
|
+
if (index === void 0) return bare({
|
|
3044
|
+
code: `${F}()`,
|
|
3045
|
+
kind: sheet.kinds.get(column),
|
|
3046
|
+
safe: false,
|
|
3047
|
+
deps
|
|
3048
|
+
});
|
|
3049
|
+
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`);
|
|
3050
|
+
const read = `${READ_CELL}(${String(index)}, ${this.#address(at, ctx, [])})`;
|
|
3051
|
+
const v = {
|
|
3052
|
+
code: `${this.use("G")}(${read})`,
|
|
3053
|
+
kind: sheet.kinds.get(column),
|
|
3054
|
+
safe: false,
|
|
3055
|
+
deps
|
|
3056
|
+
};
|
|
3057
|
+
return bare(at.every((m) => m.t === "origin") ? {
|
|
3058
|
+
...v,
|
|
3059
|
+
held: read
|
|
3060
|
+
} : v);
|
|
3061
|
+
}
|
|
3062
|
+
/**
|
|
3063
|
+
* This method gives the parts of a node in the order of the interpreter, when a part reads a cell (COMPILE.MODULE.SHEET).
|
|
3064
|
+
* A read of a cell can start the search of the run, and the search decides the cycles. So the module must read each
|
|
3065
|
+
* cell that the interpreter reads. With `all`, the interpreter evaluates each part also after an error, so each part
|
|
3066
|
+
* goes into a variable that can hold the error. Else the first error stops the parts, so each part goes into a
|
|
3067
|
+
* variable before the next part starts.
|
|
3068
|
+
*
|
|
3069
|
+
* The statements of the parts come before the code of their values. The last part that reads a cell keeps its place
|
|
3070
|
+
* only if no statement after it can throw. Also, with `all`, a value before it must not throw, or the code of its value
|
|
3071
|
+
* must read no cell.
|
|
3072
|
+
*/
|
|
3073
|
+
#inOrder(parts, all) {
|
|
3074
|
+
const last = parts.findLastIndex((x) => x.v.deps.has(CELLS));
|
|
3075
|
+
const after = parts.slice(last + 1).every((x) => x.s.length === 0 || x.v.safe);
|
|
3076
|
+
const before = !all || parts.slice(0, Math.max(last, 0)).every((x) => x.v.safe) || !READS_IN_CODE.test(parts[last].v.code);
|
|
3077
|
+
return parts.map((x, i) => {
|
|
3078
|
+
if (i > last || x.v.safe && !x.v.deps.has(CELLS) || i === last && after && before) return x;
|
|
3079
|
+
const b = this.fresh("b");
|
|
3080
|
+
if (all && !x.v.safe) return {
|
|
3081
|
+
s: this.#store(b, x),
|
|
3082
|
+
v: {
|
|
3083
|
+
...x.v,
|
|
3084
|
+
code: `${this.use("G")}(${b})`,
|
|
3085
|
+
held: b,
|
|
3086
|
+
plain: false
|
|
3087
|
+
}
|
|
3088
|
+
};
|
|
3089
|
+
return {
|
|
3090
|
+
s: [...x.s, `const ${b} = ${x.v.code};`],
|
|
3091
|
+
v: {
|
|
3092
|
+
kind: x.v.kind,
|
|
3093
|
+
deps: x.v.deps,
|
|
3094
|
+
list: x.v.list,
|
|
3095
|
+
code: b,
|
|
3096
|
+
safe: true,
|
|
3097
|
+
plain: true
|
|
3098
|
+
}
|
|
3099
|
+
};
|
|
3100
|
+
});
|
|
3101
|
+
}
|
|
3102
|
+
/**
|
|
2669
3103
|
* This method gives the code of the index of an address from the focus. `values` has the code of the value of each
|
|
2670
3104
|
* computed move, in order (NAV.COMPUTED).
|
|
2671
3105
|
*/
|
|
@@ -2675,7 +3109,7 @@ var Printer = class {
|
|
|
2675
3109
|
let fails = false;
|
|
2676
3110
|
const checks = [];
|
|
2677
3111
|
for (const m of at) {
|
|
2678
|
-
if (fails && index !== void 0 && m.t !== "offset" && m.t !== "other") checks.push(index);
|
|
3112
|
+
if (fails && index !== void 0 && m.t !== "offset" && m.t !== "other" && (m.t !== "parent" || !this.#tree)) checks.push(index);
|
|
2679
3113
|
index = moveExpr(m) === void 0 ? this.#move(m, index, ctx.origin) : this.#computed(m, values[next++]);
|
|
2680
3114
|
fails = m.t !== "origin";
|
|
2681
3115
|
}
|
|
@@ -2701,13 +3135,18 @@ var Printer = class {
|
|
|
2701
3135
|
/* v8 ignore next -- @preserve */
|
|
2702
3136
|
if (focus === void 0) throw new NotStandalone("a node of the memo reads its focus");
|
|
2703
3137
|
if (d === void 0 || more.length > 0 || !Number.isInteger(d)) return `${F}()`;
|
|
3138
|
+
if (this.#tree) return `${this.use("SO")}(${this.#part("siblings", "GP")}, SI, ${focus}, ${d})`;
|
|
2704
3139
|
return `${this.use("X")}(${focus} + ${d}, n)`;
|
|
2705
3140
|
}
|
|
2706
3141
|
case "other":
|
|
2707
3142
|
/* v8 ignore next -- @preserve */
|
|
2708
3143
|
if (focus === void 0) throw new NotStandalone("a node of the memo reads its focus");
|
|
2709
3144
|
return `(n === 2 ? 1 - ${focus} : ${F}())`;
|
|
2710
|
-
case "parent":
|
|
3145
|
+
case "parent":
|
|
3146
|
+
if (!this.#tree) return `${F}()`;
|
|
3147
|
+
/* v8 ignore next -- @preserve */
|
|
3148
|
+
if (focus === void 0) throw new NotStandalone("a node of the memo reads its focus");
|
|
3149
|
+
return `${this.use("UP")}(P, ${focus})`;
|
|
2711
3150
|
}
|
|
2712
3151
|
}
|
|
2713
3152
|
#ref(e, ctx) {
|
|
@@ -2718,7 +3157,7 @@ var Printer = class {
|
|
|
2718
3157
|
const safe = at.every((m) => m.t === "origin");
|
|
2719
3158
|
const values = this.#addressValues(at, ctx);
|
|
2720
3159
|
const deps = this.#addressDeps(at, ctx, values);
|
|
2721
|
-
const parts = this.#settle(values, deps);
|
|
3160
|
+
const parts = this.#inOrder(this.#settle(values, deps), false);
|
|
2722
3161
|
return {
|
|
2723
3162
|
s: stmtsOf(...parts),
|
|
2724
3163
|
v: {
|
|
@@ -2746,7 +3185,7 @@ var Printer = class {
|
|
|
2746
3185
|
if (fails) for (const d of deps) kept.add(d);
|
|
2747
3186
|
if (moveExpr(m) !== void 0) deps = values[next++].v.deps;
|
|
2748
3187
|
else if (m.t === "origin") deps = /* @__PURE__ */ new Set([ctx.originLevel]);
|
|
2749
|
-
else if (m.t === "key" || m.t === "index" || m.t === "parent") deps = NO_DEPS;
|
|
3188
|
+
else if (m.t === "key" || m.t === "index" || m.t === "parent" && !this.#tree) deps = NO_DEPS;
|
|
2750
3189
|
fails = m.t !== "origin";
|
|
2751
3190
|
}
|
|
2752
3191
|
return /* @__PURE__ */ new Set([...deps, ...kept]);
|
|
@@ -2963,7 +3402,7 @@ var Printer = class {
|
|
|
2963
3402
|
if (head !== void 0 && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, args.slice(1), ctx);
|
|
2964
3403
|
const raw = args.map((x) => this.expr(x, ctx));
|
|
2965
3404
|
const deps = depsOf(...raw);
|
|
2966
|
-
const values = this.#settle(raw, deps, args);
|
|
3405
|
+
const values = this.#inOrder(this.#settle(raw, deps, args), true);
|
|
2967
3406
|
const site = this.#facts.staticSite(op, values.map((x) => x.v.kind));
|
|
2968
3407
|
if (site?.t !== "fn") throw new NotStandalone(`the kinds of the values of "${op}" do not decide its op`);
|
|
2969
3408
|
if (site.bad) return {
|
|
@@ -3142,8 +3581,8 @@ var Printer = class {
|
|
|
3142
3581
|
*/
|
|
3143
3582
|
/**
|
|
3144
3583
|
* This method gives the loop over the targets of an axis: the statements before it, its bounds and the key that it
|
|
3145
|
-
* skips. `start` has the levels of the address, and `inner` is the position at a target. The axes all, others
|
|
3146
|
-
* window have a loop.
|
|
3584
|
+
* skips. `start` has the levels of the address, and `inner` is the position at a target. The axes all, others,
|
|
3585
|
+
* window and list have a loop. In a tree space, the axes of a tree have a loop too.
|
|
3147
3586
|
*/
|
|
3148
3587
|
#targetLoop(axis, at, ctx) {
|
|
3149
3588
|
const { base: outer, tests } = testsOf(axis);
|
|
@@ -3186,18 +3625,35 @@ var Printer = class {
|
|
|
3186
3625
|
if (f !== void 0) {
|
|
3187
3626
|
const values = this.#addressValues(at, ctx);
|
|
3188
3627
|
start = this.#addressDeps(at, ctx, values);
|
|
3189
|
-
const parts = this.#settle(values, start);
|
|
3628
|
+
const parts = this.#inOrder(this.#settle(values, start), false);
|
|
3190
3629
|
s.push(...stmtsOf(...parts), `const ${f} = ${this.#address(at, ctx, parts)};`);
|
|
3191
3630
|
}
|
|
3631
|
+
const startSafe = at.every((m) => m.t === "origin");
|
|
3632
|
+
if (base.t === "window" && (!Number.isInteger(base.from) || !Number.isInteger(base.to))) throw new NotStandalone("the axis \"window\" with a bound that is not an integer");
|
|
3633
|
+
if (this.#tree && isTreeAxis(base)) {
|
|
3634
|
+
const range = this.#treeRange(base, f);
|
|
3635
|
+
const q = this.fresh("q");
|
|
3636
|
+
return {
|
|
3637
|
+
s: [...s, ...range.s],
|
|
3638
|
+
j,
|
|
3639
|
+
head: forHead(q, range.lo, range.hi, reverse),
|
|
3640
|
+
key: [`const ${j} = ${range.a}[${q}];`],
|
|
3641
|
+
check: [],
|
|
3642
|
+
skip: range.skip,
|
|
3643
|
+
fixed: false,
|
|
3644
|
+
start,
|
|
3645
|
+
startSafe,
|
|
3646
|
+
tests,
|
|
3647
|
+
inner
|
|
3648
|
+
};
|
|
3649
|
+
}
|
|
3192
3650
|
if (base.t === "others") skip = f;
|
|
3193
3651
|
else if (base.t === "window") {
|
|
3194
|
-
if (!Number.isInteger(base.from) || !Number.isInteger(base.to)) throw new NotStandalone("the axis \"window\" with a bound that is not an integer");
|
|
3195
3652
|
const h = this.fresh("h");
|
|
3196
3653
|
lo = `Math.max(0, ${f} + Math.max(${base.from}, -n))`;
|
|
3197
3654
|
s.push(`const ${h} = Math.min(n, ${f} + Math.min(${base.to}, n) + 1);`);
|
|
3198
3655
|
hi = h;
|
|
3199
3656
|
} else if (base.t !== "all") throw new NotStandalone(`the axis "${base.t}"`);
|
|
3200
|
-
const startSafe = at.every((m) => m.t === "origin");
|
|
3201
3657
|
return {
|
|
3202
3658
|
s,
|
|
3203
3659
|
j,
|
|
@@ -3212,6 +3668,54 @@ var Printer = class {
|
|
|
3212
3668
|
inner
|
|
3213
3669
|
};
|
|
3214
3670
|
}
|
|
3671
|
+
/**
|
|
3672
|
+
* This method gives the targets of an axis of a tree space at the position `f`. They are the positions `a[q]` for `q`
|
|
3673
|
+
* from `lo` up to `hi`, without the key that the loop skips. The axes come from AXIS.TREE, AXIS.GRAPH and AXIS.WINDOW.
|
|
3674
|
+
*/
|
|
3675
|
+
#treeRange(base, f) {
|
|
3676
|
+
const a = this.fresh("A");
|
|
3677
|
+
const all = (code, lo = "0", skip) => ({
|
|
3678
|
+
s: [`const ${a} = ${code};`],
|
|
3679
|
+
a,
|
|
3680
|
+
lo,
|
|
3681
|
+
hi: `${a}.length`,
|
|
3682
|
+
skip
|
|
3683
|
+
});
|
|
3684
|
+
switch (base.t) {
|
|
3685
|
+
case "children":
|
|
3686
|
+
case "successors": return all(`${this.#part("children", "H")}[${f}]`);
|
|
3687
|
+
case "siblings": return all(`${this.#part("siblings", "GP")}[${f}]`, "0", f);
|
|
3688
|
+
case "ancestors": return all(`${this.use("AN")}(P, ${f})`);
|
|
3689
|
+
case "predecessors": return all(`(P[${f}] < 0 ? [] : [P[${f}]])`);
|
|
3690
|
+
case "reachable": return all(`${this.use("BF")}(${this.#part("children", "H")}, ${f})`, "1");
|
|
3691
|
+
case "descendants": {
|
|
3692
|
+
const h = this.fresh("h");
|
|
3693
|
+
return {
|
|
3694
|
+
s: [`const ${a} = ${this.#part("order", "PI")}[${f}] + 1;`, `const ${h} = ${a} + SZ[${f}] - 1;`],
|
|
3695
|
+
a: "PO",
|
|
3696
|
+
lo: a,
|
|
3697
|
+
hi: h,
|
|
3698
|
+
skip: void 0
|
|
3699
|
+
};
|
|
3700
|
+
}
|
|
3701
|
+
case "window": {
|
|
3702
|
+
const { from, to } = base;
|
|
3703
|
+
const l = this.fresh("h");
|
|
3704
|
+
const h = this.fresh("h");
|
|
3705
|
+
return {
|
|
3706
|
+
s: [
|
|
3707
|
+
`const ${a} = ${this.#part("siblings", "GP")}[${f}];`,
|
|
3708
|
+
`const ${l} = Math.max(0, SI[${f}] + ${String(from)});`,
|
|
3709
|
+
`const ${h} = Math.min(${a}.length, SI[${f}] + ${String(to)} + 1);`
|
|
3710
|
+
],
|
|
3711
|
+
a,
|
|
3712
|
+
lo: l,
|
|
3713
|
+
hi: h,
|
|
3714
|
+
skip: void 0
|
|
3715
|
+
};
|
|
3716
|
+
}
|
|
3717
|
+
}
|
|
3718
|
+
}
|
|
3215
3719
|
#list(op, target, rest, ctx) {
|
|
3216
3720
|
const strict = this.#strict(op, rest);
|
|
3217
3721
|
const at = target.at ?? [];
|
|
@@ -3230,6 +3734,44 @@ var Printer = class {
|
|
|
3230
3734
|
...body.s
|
|
3231
3735
|
];
|
|
3232
3736
|
const safeItem = check.length === 0 && body.v.safe && testCodes.every((t) => this.#safeBoolean(t.v));
|
|
3737
|
+
if ([body, ...testCodes].some((x) => x.v.deps.has(CELLS)) && (strict || stepThrows(op, body.v.kind))) {
|
|
3738
|
+
const list = this.fresh("L");
|
|
3739
|
+
const x = this.fresh("x");
|
|
3740
|
+
const all = [
|
|
3741
|
+
...key,
|
|
3742
|
+
...skip === void 0 ? [] : [`if (${j} === ${skip}) continue;`],
|
|
3743
|
+
`let ${x};`,
|
|
3744
|
+
"try {",
|
|
3745
|
+
...ind(item),
|
|
3746
|
+
` ${x} = ${body.v.code};`,
|
|
3747
|
+
"} catch {",
|
|
3748
|
+
` ${x} = E;`,
|
|
3749
|
+
"}",
|
|
3750
|
+
`${list}.push(${x});`
|
|
3751
|
+
];
|
|
3752
|
+
const reduce = [
|
|
3753
|
+
`for (const ${v} of ${list}) {`,
|
|
3754
|
+
` if (${v} === E) ${strict ? `${this.use("F")}();` : "continue;"}`,
|
|
3755
|
+
` ${r.step}`,
|
|
3756
|
+
"}"
|
|
3757
|
+
];
|
|
3758
|
+
return {
|
|
3759
|
+
s: [
|
|
3760
|
+
...s,
|
|
3761
|
+
`const ${list} = [];`,
|
|
3762
|
+
head,
|
|
3763
|
+
...ind(all),
|
|
3764
|
+
"}",
|
|
3765
|
+
...reduce
|
|
3766
|
+
],
|
|
3767
|
+
v: {
|
|
3768
|
+
code: r.result,
|
|
3769
|
+
kind: listKind(op, body.v.kind),
|
|
3770
|
+
safe: false,
|
|
3771
|
+
deps
|
|
3772
|
+
}
|
|
3773
|
+
};
|
|
3774
|
+
}
|
|
3233
3775
|
const loop = [
|
|
3234
3776
|
...key,
|
|
3235
3777
|
...skip === void 0 ? [] : [`if (${j} === ${skip}) continue;`],
|
|
@@ -3360,7 +3902,7 @@ var Printer = class {
|
|
|
3360
3902
|
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
3361
3903
|
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
3362
3904
|
const deps = depsOf(...raw);
|
|
3363
|
-
const parts = this.#settle(raw, deps, exprs);
|
|
3905
|
+
const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
|
|
3364
3906
|
const item = list.v.list.item;
|
|
3365
3907
|
return {
|
|
3366
3908
|
s: stmtsOf(...parts),
|
|
@@ -3380,7 +3922,7 @@ var Printer = class {
|
|
|
3380
3922
|
const strict = this.#strict(op, rest.slice(exprs.length - 1));
|
|
3381
3923
|
const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
|
|
3382
3924
|
const deps = depsOf(...raw);
|
|
3383
|
-
const parts = this.#settle(raw, deps, exprs);
|
|
3925
|
+
const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
|
|
3384
3926
|
const [l] = parts;
|
|
3385
3927
|
const item = list.v.list.item;
|
|
3386
3928
|
if (op === "nth") return {
|
|
@@ -3513,6 +4055,7 @@ var Printer = class {
|
|
|
3513
4055
|
const names = this.#loopNames(e.init, ctx, loop);
|
|
3514
4056
|
const t = this.#targetLoop(e.axis, e.at ?? [], ctx);
|
|
3515
4057
|
const tests = t.tests.map((x) => this.#hoist(this.expr(x, t.inner), hasLoop(x)));
|
|
4058
|
+
if (tests.some((x) => x.v.deps.has(CELLS))) throw new NotStandalone("a test of the axis of a fold reads a cell");
|
|
3516
4059
|
const step = this.#loopStep(e.step, {
|
|
3517
4060
|
...t.inner,
|
|
3518
4061
|
env: names.env
|
|
@@ -3733,8 +4276,9 @@ var Printer = class {
|
|
|
3733
4276
|
};
|
|
3734
4277
|
}
|
|
3735
4278
|
/** This method gives the code of the program at the key of the run. A constant of the run evaluates once. */
|
|
4279
|
+
/** This method gives the code of a program at the key `o` of the run. A sheet has one program for each formula. */
|
|
3736
4280
|
root(expr) {
|
|
3737
|
-
|
|
4281
|
+
return this.#hoist(this.expr(expr, {
|
|
3738
4282
|
origin: "o",
|
|
3739
4283
|
focus: "o",
|
|
3740
4284
|
level: 1,
|
|
@@ -3743,8 +4287,10 @@ var Printer = class {
|
|
|
3743
4287
|
fns: /* @__PURE__ */ new Map(),
|
|
3744
4288
|
keys: /* @__PURE__ */ new Map()
|
|
3745
4289
|
}));
|
|
4290
|
+
}
|
|
4291
|
+
/** This method removes the declarations that no code reads, after the last program. */
|
|
4292
|
+
finish() {
|
|
3746
4293
|
for (const [name, form] of this.#forms) if (form !== void 0 && !this.#called.has(name)) this.top.delete(name);
|
|
3747
|
-
return c;
|
|
3748
4294
|
}
|
|
3749
4295
|
/**
|
|
3750
4296
|
* This method gives a text that is the same for two expressions with the same structure and the same reads. A name
|
|
@@ -3764,42 +4310,30 @@ var Printer = class {
|
|
|
3764
4310
|
});
|
|
3765
4311
|
}
|
|
3766
4312
|
};
|
|
3767
|
-
/**
|
|
3768
|
-
|
|
3769
|
-
* function that takes the records and the variables and gives the value at each key. It is pure, so a build can use it.
|
|
3770
|
-
* The program can be an expression or a document (DOC.ENTRY).
|
|
3771
|
-
*/
|
|
3772
|
-
function emitModule(source, options) {
|
|
4313
|
+
/** This function gives the name of the function of a module, and its space, or the reason that the options are not valid. */
|
|
4314
|
+
function runOf(options) {
|
|
3773
4315
|
const name = options.name ?? "run";
|
|
3774
|
-
if (!/^[A-Za-z_$][\w$]*$/.test(name)) return {
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
value: source
|
|
3781
|
-
};
|
|
3782
|
-
if (!read.ok) return {
|
|
3783
|
-
ok: false,
|
|
3784
|
-
reason: read.error.message
|
|
4316
|
+
if (!/^[A-Za-z_$][\w$]*$/.test(name)) return { reason: `the name "${name}" is not a JavaScript name` };
|
|
4317
|
+
const tree = options.space === "tree";
|
|
4318
|
+
if (!tree && options.space !== void 0 && options.space !== "array") return { reason: `the space "${String(options.space)}" is not "array" or "tree"` };
|
|
4319
|
+
return {
|
|
4320
|
+
name,
|
|
4321
|
+
tree
|
|
3785
4322
|
};
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
4323
|
+
}
|
|
4324
|
+
/** This function gives the reason of an error of the printer. Another exception is a defect, so it goes to the caller. */
|
|
4325
|
+
function reasonOf(thrown) {
|
|
4326
|
+
if (!(thrown instanceof NotStandalone)) throw thrown;
|
|
4327
|
+
return {
|
|
3789
4328
|
ok: false,
|
|
3790
|
-
reason:
|
|
4329
|
+
reason: thrown.message
|
|
3791
4330
|
};
|
|
3792
|
-
|
|
3793
|
-
|
|
3794
|
-
|
|
3795
|
-
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
return {
|
|
3799
|
-
ok: false,
|
|
3800
|
-
reason: thrown.message
|
|
3801
|
-
};
|
|
3802
|
-
}
|
|
4331
|
+
}
|
|
4332
|
+
/**
|
|
4333
|
+
* This function gives the text of a module: the declarations, the helpers, and the function of the run. The run checks
|
|
4334
|
+
* its input, computes the parts of the tree and the constants of the run, and then gives the statements `body`.
|
|
4335
|
+
*/
|
|
4336
|
+
function printModule(p, options, run, body) {
|
|
3803
4337
|
p.use("C");
|
|
3804
4338
|
const helpers = /* @__PURE__ */ new Set();
|
|
3805
4339
|
const add = (h) => {
|
|
@@ -3814,13 +4348,31 @@ function emitModule(source, options) {
|
|
|
3814
4348
|
fieldChecks = fields.map(([f, k]) => ` || ${p.against(k, `r[${JSON.stringify(f)}]`)}`).join("");
|
|
3815
4349
|
varChecks = vars.map(([v, k]) => ` || ${p.against(k, `vars[${JSON.stringify(v)}]`)}`).join("");
|
|
3816
4350
|
} catch (thrown) {
|
|
3817
|
-
|
|
3818
|
-
return {
|
|
3819
|
-
ok: false,
|
|
3820
|
-
reason: thrown.message
|
|
3821
|
-
};
|
|
4351
|
+
return reasonOf(thrown);
|
|
3822
4352
|
}
|
|
4353
|
+
const { name, tree } = run;
|
|
4354
|
+
if (tree) p.use("PT");
|
|
4355
|
+
if (p.treeParts.size > 0) p.treeParts.add("children");
|
|
4356
|
+
const parts = [
|
|
4357
|
+
"children",
|
|
4358
|
+
"siblings",
|
|
4359
|
+
"order"
|
|
4360
|
+
].filter((x) => p.treeParts.has(x));
|
|
4361
|
+
for (const x of parts) p.use(TREE_PARTS[x][0]);
|
|
3823
4362
|
[...p.helpers].toSorted().forEach(add);
|
|
4363
|
+
const bad = nameReason(name, /* @__PURE__ */ new Set([
|
|
4364
|
+
"VexContractError",
|
|
4365
|
+
"VEX_ERROR",
|
|
4366
|
+
"E",
|
|
4367
|
+
...p.memos ? ["Z"] : [],
|
|
4368
|
+
...helpers.has("S") ? ["N", "W"] : [],
|
|
4369
|
+
...helpers,
|
|
4370
|
+
...p.top.keys()
|
|
4371
|
+
]));
|
|
4372
|
+
if (bad !== void 0) return {
|
|
4373
|
+
ok: false,
|
|
4374
|
+
reason: bad
|
|
4375
|
+
};
|
|
3824
4376
|
return {
|
|
3825
4377
|
ok: true,
|
|
3826
4378
|
source: [
|
|
@@ -3834,7 +4386,7 @@ function emitModule(source, options) {
|
|
|
3834
4386
|
...helpers.has("S") ? ["const N = {}, W = {};"] : [],
|
|
3835
4387
|
...[...helpers].map((h) => h === "S" ? helperS(p.features) : HELPERS[h]),
|
|
3836
4388
|
...p.top.values(),
|
|
3837
|
-
`export function ${name}(records, vars) {`,
|
|
4389
|
+
`export function ${name}(records, vars${tree ? ", parents" : ""}) {`,
|
|
3838
4390
|
" if (!Array.isArray(records)) C(\"the records are not an array\");",
|
|
3839
4391
|
" const R = records;",
|
|
3840
4392
|
" const n = R.length;",
|
|
@@ -3843,24 +4395,176 @@ function emitModule(source, options) {
|
|
|
3843
4395
|
` if (typeof r !== "object" || r === null${fieldChecks}) C(\`the record \${i} does not keep the schema\`);`,
|
|
3844
4396
|
" }",
|
|
3845
4397
|
` if (typeof vars !== "object" || vars === null${varChecks}) C("the variables do not keep the schema");`,
|
|
4398
|
+
...tree ? [" const P = PT(parents, n);", ...parts.map((x) => ` ${TREE_PARTS[x][1]}`)] : [],
|
|
3846
4399
|
...[...p.vars].map(([v, code]) => ` const ${code} = vars[${JSON.stringify(v)}];`),
|
|
3847
4400
|
...ind(p.run),
|
|
3848
|
-
|
|
3849
|
-
" for (let o = 0; o < n; o++) {",
|
|
3850
|
-
" try {",
|
|
3851
|
-
...ind(ind(ind(value.s))),
|
|
3852
|
-
` out.push(${value.v.code});`,
|
|
3853
|
-
" } catch {",
|
|
3854
|
-
" out.push(VEX_ERROR);",
|
|
3855
|
-
" }",
|
|
3856
|
-
" }",
|
|
3857
|
-
" return out;",
|
|
4401
|
+
...ind(body),
|
|
3858
4402
|
"}",
|
|
3859
4403
|
""
|
|
3860
4404
|
].join("\n")
|
|
3861
4405
|
};
|
|
3862
4406
|
}
|
|
4407
|
+
/**
|
|
4408
|
+
* This function prints a program as a standalone ES module. The module exports `VexContractError`, `VEX_ERROR` and a
|
|
4409
|
+
* function that takes the records and the variables and gives the value at each key. A module of a tree space takes
|
|
4410
|
+
* the parents too. The function is pure, so a build can use it. The program can be an expression or a document
|
|
4411
|
+
* (DOC.ENTRY).
|
|
4412
|
+
*/
|
|
4413
|
+
function emitModule(source, options) {
|
|
4414
|
+
const run = runOf(options);
|
|
4415
|
+
if ("reason" in run) return {
|
|
4416
|
+
ok: false,
|
|
4417
|
+
reason: run.reason
|
|
4418
|
+
};
|
|
4419
|
+
const read = isDocument(source) ? fromDocument(source, options.domains) : {
|
|
4420
|
+
ok: true,
|
|
4421
|
+
value: source
|
|
4422
|
+
};
|
|
4423
|
+
if (!read.ok) return {
|
|
4424
|
+
ok: false,
|
|
4425
|
+
reason: read.error.message
|
|
4426
|
+
};
|
|
4427
|
+
const expr = read.value;
|
|
4428
|
+
const shape = shapeError(expr);
|
|
4429
|
+
if (shape !== void 0) return {
|
|
4430
|
+
ok: false,
|
|
4431
|
+
reason: shape
|
|
4432
|
+
};
|
|
4433
|
+
const p = new Printer(expr, options);
|
|
4434
|
+
let value;
|
|
4435
|
+
try {
|
|
4436
|
+
value = p.root(expr);
|
|
4437
|
+
p.finish();
|
|
4438
|
+
} catch (thrown) {
|
|
4439
|
+
return reasonOf(thrown);
|
|
4440
|
+
}
|
|
4441
|
+
return printModule(p, options, run, [
|
|
4442
|
+
"const out = [];",
|
|
4443
|
+
"for (let o = 0; o < n; o++) {",
|
|
4444
|
+
" try {",
|
|
4445
|
+
...ind(ind(value.s)),
|
|
4446
|
+
` out.push(${value.v.code});`,
|
|
4447
|
+
" } catch {",
|
|
4448
|
+
" out.push(VEX_ERROR);",
|
|
4449
|
+
" }",
|
|
4450
|
+
"}",
|
|
4451
|
+
"return out;"
|
|
4452
|
+
]);
|
|
4453
|
+
}
|
|
4454
|
+
/** The budget of the stack of the search of a run of a sheet, as `STACK_BUDGET` of `SheetRun`. */
|
|
4455
|
+
const SHEET_BUDGET = 3e3;
|
|
4456
|
+
/** This function gives the words for a formula in a reason: the formula of a column, or of one cell. */
|
|
4457
|
+
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)}"`;
|
|
4458
|
+
/** This function gives the words for a kind in a reason. */
|
|
4459
|
+
const kindText = (k) => k ?? "that the printer does not know";
|
|
4460
|
+
/**
|
|
4461
|
+
* This function prints the formula columns of a sheet as a standalone ES module (COMPILE.MODULE.SHEET). The function of
|
|
4462
|
+
* the module takes the records and the variables, and the parents for a tree. It gives an object with an array for
|
|
4463
|
+
* each column: the result of the column at each key, or `VEX_ERROR`. The results are the results of `SheetRun`.
|
|
4464
|
+
*
|
|
4465
|
+
* The run fills the columns in the order of `SheetRun.fill`. A read of a finished cell is the read of an array. A read
|
|
4466
|
+
* of a cell that is not finished starts the search of `SheetRun`, with the same budget of the stack. Each cell on a
|
|
4467
|
+
* cycle of reads gives `VEX_ERROR`.
|
|
4468
|
+
*/
|
|
4469
|
+
function emitSheetModule(columns, options) {
|
|
4470
|
+
const run = runOf(options);
|
|
4471
|
+
if ("reason" in run) return {
|
|
4472
|
+
ok: false,
|
|
4473
|
+
reason: run.reason
|
|
4474
|
+
};
|
|
4475
|
+
const budget = options.budget ?? SHEET_BUDGET;
|
|
4476
|
+
if (!Number.isSafeInteger(budget) || budget < 0) return {
|
|
4477
|
+
ok: false,
|
|
4478
|
+
reason: `the budget ${String(budget)} is not a whole number of 0 or more`
|
|
4479
|
+
};
|
|
4480
|
+
const cells = options.cells ?? /* @__PURE__ */ new Map();
|
|
4481
|
+
const names = [.../* @__PURE__ */ new Set([...columns.keys(), ...cells.keys()])];
|
|
4482
|
+
const order = columnOrder(columns, cells);
|
|
4483
|
+
const formulas = order.flatMap((column) => {
|
|
4484
|
+
const own = [...cells.get(column) ?? []].filter(([k]) => /^(0|[1-9]\d*)$/.test(k) && Number.isSafeInteger(Number(k)));
|
|
4485
|
+
const formula = columns.get(column);
|
|
4486
|
+
return [...formula === void 0 ? [] : [{
|
|
4487
|
+
column,
|
|
4488
|
+
key: void 0,
|
|
4489
|
+
expr: formula
|
|
4490
|
+
}], ...own.map(([k, expr]) => ({
|
|
4491
|
+
column,
|
|
4492
|
+
key: Number(k),
|
|
4493
|
+
expr
|
|
4494
|
+
}))];
|
|
4495
|
+
});
|
|
4496
|
+
for (const f of formulas) {
|
|
4497
|
+
const shape = shapeError(f.expr);
|
|
4498
|
+
if (shape !== void 0) return {
|
|
4499
|
+
ok: false,
|
|
4500
|
+
reason: `${formulaText(f)}: ${shape}`
|
|
4501
|
+
};
|
|
4502
|
+
}
|
|
4503
|
+
const kinds = new Map(Object.entries(options.kinds ?? {}));
|
|
4504
|
+
const index = new Map(names.map((c, i) => [c, i]));
|
|
4505
|
+
const p = new Printer(rec(Object.fromEntries(formulas.map((f, i) => [`f${String(i)}`, f.expr]))), options, {
|
|
4506
|
+
columns: index,
|
|
4507
|
+
kinds
|
|
4508
|
+
});
|
|
4509
|
+
const fns = [];
|
|
4510
|
+
const decls = [];
|
|
4511
|
+
try {
|
|
4512
|
+
for (const f of formulas) {
|
|
4513
|
+
const code = p.root(f.expr);
|
|
4514
|
+
const kind = code.v.kind;
|
|
4515
|
+
if (!kinds.has(f.column)) kinds.set(f.column, kind);
|
|
4516
|
+
else if (kinds.get(f.column) !== kind) throw new NotStandalone(`${formulaText(f)} gives a value of the kind ${kindText(kind)}, not of the kind ${kindText(kinds.get(f.column))} of its column`);
|
|
4517
|
+
const fn = p.fresh("cf");
|
|
4518
|
+
fns.push(fn);
|
|
4519
|
+
decls.push(...code.v.safe && code.s.length === 0 ? [`const ${fn} = (o) => ${code.v.code};`] : [
|
|
4520
|
+
`const ${fn} = (o) => {`,
|
|
4521
|
+
" try {",
|
|
4522
|
+
...ind(ind(code.s)),
|
|
4523
|
+
` return ${code.v.code};`,
|
|
4524
|
+
" } catch {",
|
|
4525
|
+
" return E;",
|
|
4526
|
+
" }",
|
|
4527
|
+
"};"
|
|
4528
|
+
]);
|
|
4529
|
+
}
|
|
4530
|
+
p.finish();
|
|
4531
|
+
} catch (thrown) {
|
|
4532
|
+
return reasonOf(thrown);
|
|
4533
|
+
}
|
|
4534
|
+
const entry = (i) => `[${fns[i]}, ${String(3 * nestingOf(formulas[i].expr) + 16)}]`;
|
|
4535
|
+
const own = names.map((c) => {
|
|
4536
|
+
const at = formulas.flatMap((f, i) => f.column === c && f.key !== void 0 ? [`${String(f.key)}: ${entry(i)}`] : []);
|
|
4537
|
+
return at.length === 0 ? "undefined" : `{ ${at.join(", ")} }`;
|
|
4538
|
+
});
|
|
4539
|
+
const cols = names.map((c) => {
|
|
4540
|
+
const i = formulas.findIndex((f) => f.column === c && f.key === void 0);
|
|
4541
|
+
return i < 0 ? "undefined" : entry(i);
|
|
4542
|
+
});
|
|
4543
|
+
p.use("SH");
|
|
4544
|
+
const orders = /* @__PURE__ */ new Map();
|
|
4545
|
+
const fill = order.map((c) => {
|
|
4546
|
+
const { reverse, post, back } = keyOrderOf(readsOf(columns, cells, c).directions);
|
|
4547
|
+
const at = String(index.get(c));
|
|
4548
|
+
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);`;
|
|
4549
|
+
const args = `${p.use("KO")}(H, RT, ${String(post)}, ${String(back)})`;
|
|
4550
|
+
let keys = orders.get(args);
|
|
4551
|
+
if (keys === void 0) {
|
|
4552
|
+
keys = p.fresh("K");
|
|
4553
|
+
orders.set(args, keys);
|
|
4554
|
+
}
|
|
4555
|
+
return `for (const o of ${keys}) SS.f(${at}, o);`;
|
|
4556
|
+
});
|
|
4557
|
+
if (run.tree) p.treeParts.add("children");
|
|
4558
|
+
return printModule(p, options, run, [
|
|
4559
|
+
...decls,
|
|
4560
|
+
`const SS = SH(n, [${cols.join(", ")}], [${own.join(", ")}], ${String(budget)});`,
|
|
4561
|
+
`const ${READ_CELL} = SS.r;`,
|
|
4562
|
+
...[...orders].map(([args, keys]) => `const ${keys} = ${args};`),
|
|
4563
|
+
...fill,
|
|
4564
|
+
`return { ${names.map((c, i) => `${JSON.stringify(c)}: SS.o(${String(i)})`).join(", ")} };`
|
|
4565
|
+
]);
|
|
4566
|
+
}
|
|
3863
4567
|
//#endregion
|
|
3864
|
-
export { FALLBACK, createRuntime, emitModule, generate, load, tiered };
|
|
4568
|
+
export { FALLBACK, createRuntime, emitModule, emitSheetModule, generate, load, tiered };
|
|
3865
4569
|
|
|
3866
4570
|
//# sourceMappingURL=index.js.map
|