@mark1russell7/vex 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +93 -12
- package/build/index.d.ts.map +1 -1
- package/build/index.js +315 -74
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/builder.ts +56 -3
- package/src/errors.ts +4 -0
- package/src/evaluate.ts +24 -6
- package/src/ir.ts +26 -1
- package/src/sheet.ts +142 -59
- package/src/space.ts +131 -1
package/build/index.js
CHANGED
|
@@ -38,6 +38,8 @@ const CODE_OF = {
|
|
|
38
38
|
"no-offset": "#REF!",
|
|
39
39
|
"no-grid": "#REF!",
|
|
40
40
|
"no-tree": "#REF!",
|
|
41
|
+
"no-table": "#REF!",
|
|
42
|
+
"no-graph": "#REF!",
|
|
41
43
|
"missing-field": "#N/A",
|
|
42
44
|
empty: "#N/A",
|
|
43
45
|
"not-instance": "#VALUE!",
|
|
@@ -188,6 +190,29 @@ const axes = {
|
|
|
188
190
|
descendants: Object.freeze({ t: "descendants" }),
|
|
189
191
|
/** The other children of the parent of the focus. For a root, the other roots. */
|
|
190
192
|
siblings: Object.freeze({ t: "siblings" }),
|
|
193
|
+
/**
|
|
194
|
+
* The keys at the offsets from `from` to `to` of the focus, in order: a window of rows. The offset 0 is the focus. In
|
|
195
|
+
* a tree space, the offsets go among the siblings. An offset outside the space has no target.
|
|
196
|
+
*/
|
|
197
|
+
window: (from, to) => ({
|
|
198
|
+
t: "window",
|
|
199
|
+
from,
|
|
200
|
+
to
|
|
201
|
+
}),
|
|
202
|
+
/**
|
|
203
|
+
* Each key of the table `name` of the evaluation, in key order. The body evaluates in the space of that table, and
|
|
204
|
+
* there the origin is the target.
|
|
205
|
+
*/
|
|
206
|
+
table: (name) => ({
|
|
207
|
+
t: "table",
|
|
208
|
+
name
|
|
209
|
+
}),
|
|
210
|
+
/** The keys at the ends of the edges from the focus, in a graph space. In a tree space, the children. */
|
|
211
|
+
successors: Object.freeze({ t: "successors" }),
|
|
212
|
+
/** The keys with an edge to the focus, in key order. In a tree space, the parent. */
|
|
213
|
+
predecessors: Object.freeze({ t: "predecessors" }),
|
|
214
|
+
/** Each key that a path of edges reaches from the focus, in breadth-first order, without the focus. */
|
|
215
|
+
reachable: Object.freeze({ t: "reachable" }),
|
|
191
216
|
/** The targets of `axis` where `test` gives `true`. */
|
|
192
217
|
where: (axis, test) => ({
|
|
193
218
|
t: "where",
|
|
@@ -243,6 +268,10 @@ function validTree(todo) {
|
|
|
243
268
|
const t = u["t"];
|
|
244
269
|
if (t === "neighbors") {
|
|
245
270
|
if (u["n"] !== 4 && u["n"] !== 8) return false;
|
|
271
|
+
} else if (t === "window") {
|
|
272
|
+
if (!Number.isSafeInteger(u["from"]) || !Number.isSafeInteger(u["to"])) return false;
|
|
273
|
+
} else if (t === "table") {
|
|
274
|
+
if (typeof u["name"] !== "string") return false;
|
|
246
275
|
} else if (t === "where") {
|
|
247
276
|
push(true, [u["axis"]]);
|
|
248
277
|
push(false, [u["test"]]);
|
|
@@ -253,7 +282,10 @@ function validTree(todo) {
|
|
|
253
282
|
"children",
|
|
254
283
|
"ancestors",
|
|
255
284
|
"descendants",
|
|
256
|
-
"siblings"
|
|
285
|
+
"siblings",
|
|
286
|
+
"successors",
|
|
287
|
+
"predecessors",
|
|
288
|
+
"reachable"
|
|
257
289
|
].includes(t)) return false;
|
|
258
290
|
continue;
|
|
259
291
|
}
|
|
@@ -425,6 +457,13 @@ var TreeSpace = class {
|
|
|
425
457
|
}
|
|
426
458
|
coords() {}
|
|
427
459
|
keyAt() {}
|
|
460
|
+
successorsOf(k) {
|
|
461
|
+
return this.childrenOf(k);
|
|
462
|
+
}
|
|
463
|
+
predecessorsOf(k) {
|
|
464
|
+
const p = this.#parents.get(k);
|
|
465
|
+
return p === void 0 ? [] : [p];
|
|
466
|
+
}
|
|
428
467
|
parentOf(k) {
|
|
429
468
|
return this.#parents.get(k);
|
|
430
469
|
}
|
|
@@ -433,6 +472,49 @@ var TreeSpace = class {
|
|
|
433
472
|
}
|
|
434
473
|
};
|
|
435
474
|
/** The constructors of spaces. */
|
|
475
|
+
/** The empty list of keys. */
|
|
476
|
+
const NO_KEYS = Object.freeze([]);
|
|
477
|
+
var GraphSpace = class {
|
|
478
|
+
kind = "graph";
|
|
479
|
+
keys;
|
|
480
|
+
#records;
|
|
481
|
+
#out;
|
|
482
|
+
#in;
|
|
483
|
+
constructor(records, edges) {
|
|
484
|
+
const entries = Object.entries(records);
|
|
485
|
+
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
486
|
+
this.#records = new Map(entries);
|
|
487
|
+
const out = /* @__PURE__ */ new Map();
|
|
488
|
+
for (const [k, to] of Object.entries(edges)) {
|
|
489
|
+
if (!this.#records.has(k)) throw new TypeError(`the graph has no key "${k}", but the edges name it`);
|
|
490
|
+
const ends = [...new Set(to ?? NO_KEYS)];
|
|
491
|
+
for (const t of ends) if (!this.#records.has(t)) throw new TypeError(`the edge from "${k}" goes to "${t}", which is not a key of the graph`);
|
|
492
|
+
out.set(k, Object.freeze(ends));
|
|
493
|
+
}
|
|
494
|
+
this.#out = out;
|
|
495
|
+
const into = /* @__PURE__ */ new Map();
|
|
496
|
+
for (const k of this.keys) for (const t of out.get(k) ?? NO_KEYS) {
|
|
497
|
+
const from = into.get(t);
|
|
498
|
+
if (from === void 0) into.set(t, [k]);
|
|
499
|
+
else from.push(k);
|
|
500
|
+
}
|
|
501
|
+
this.#in = new Map([...into].map(([k, from]) => [k, Object.freeze(from)]));
|
|
502
|
+
}
|
|
503
|
+
has(k) {
|
|
504
|
+
return this.#records.has(k);
|
|
505
|
+
}
|
|
506
|
+
get(k) {
|
|
507
|
+
return this.#records.get(k);
|
|
508
|
+
}
|
|
509
|
+
coords() {}
|
|
510
|
+
keyAt() {}
|
|
511
|
+
successorsOf(k) {
|
|
512
|
+
return this.#out.get(k) ?? NO_KEYS;
|
|
513
|
+
}
|
|
514
|
+
predecessorsOf(k) {
|
|
515
|
+
return this.#in.get(k) ?? NO_KEYS;
|
|
516
|
+
}
|
|
517
|
+
};
|
|
436
518
|
const space = {
|
|
437
519
|
/** This function makes a space from the entries of an object. The keys are the property names. */
|
|
438
520
|
record(records) {
|
|
@@ -452,6 +534,13 @@ const space = {
|
|
|
452
534
|
*/
|
|
453
535
|
tree(records, parents) {
|
|
454
536
|
return new TreeSpace(records, parents);
|
|
537
|
+
},
|
|
538
|
+
/**
|
|
539
|
+
* This function makes a graph space: the records by key, and the edges from each key, in order. An edge to a key
|
|
540
|
+
* that is not in the graph throws a `TypeError`. A graph can have cycles, and an edge can go from a key to itself.
|
|
541
|
+
*/
|
|
542
|
+
graph(records, edges) {
|
|
543
|
+
return new GraphSpace(records, edges);
|
|
455
544
|
}
|
|
456
545
|
};
|
|
457
546
|
/** This function applies one move to the focus. It gives the new focus, or a `#REF!` error. */
|
|
@@ -580,8 +669,49 @@ function axisTargets(s, at, a) {
|
|
|
580
669
|
case "ancestors":
|
|
581
670
|
case "descendants":
|
|
582
671
|
case "siblings": return treeTargets(s, at, a.t);
|
|
672
|
+
case "window": return windowTargets(s, at, a.from, a.to);
|
|
673
|
+
case "successors":
|
|
674
|
+
case "predecessors":
|
|
675
|
+
case "reachable": return graphTargets(s, at, a.t);
|
|
676
|
+
case "table": return fail(vexError("no-table", `the axis "table" needs the tables of the evaluation, and "${a.name}" is not one of them`, at));
|
|
583
677
|
}
|
|
584
678
|
}
|
|
679
|
+
/**
|
|
680
|
+
* This function gives the targets of an axis of a graph. `reachable` has each key that a path of one or more edges
|
|
681
|
+
* reaches from the focus, once, in the order of a breadth-first search. The focus is not a target of `reachable`.
|
|
682
|
+
*/
|
|
683
|
+
function graphTargets(s, at, t) {
|
|
684
|
+
const successorsOf = s.successorsOf?.bind(s);
|
|
685
|
+
const predecessorsOf = s.predecessorsOf?.bind(s);
|
|
686
|
+
if (successorsOf === void 0 || predecessorsOf === void 0) return fail(vexError("no-graph", `"${t}" needs a graph space or a tree space, but this space is a ${s.kind}`, at));
|
|
687
|
+
if (t === "successors") return ok(successorsOf(at.focus));
|
|
688
|
+
if (t === "predecessors") return ok(predecessorsOf(at.focus));
|
|
689
|
+
const seen = /* @__PURE__ */ new Set([at.focus]);
|
|
690
|
+
const queue = [at.focus];
|
|
691
|
+
for (const k of queue) for (const next of successorsOf(k)) {
|
|
692
|
+
if (seen.has(next)) continue;
|
|
693
|
+
seen.add(next);
|
|
694
|
+
queue.push(next);
|
|
695
|
+
}
|
|
696
|
+
return ok(queue.slice(1));
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* This function gives the targets of the axis `window(from, to)`: the keys of the moves `offset(d)` for each `d`
|
|
700
|
+
* from `from` to `to`, in order. An offset outside the space has no target. The axis needs an array space or a tree
|
|
701
|
+
* space, where an offset has one number.
|
|
702
|
+
*/
|
|
703
|
+
function windowTargets(s, at, from, to) {
|
|
704
|
+
if (!(s.parentOf !== void 0 && s.childrenOf !== void 0) && s.coords(at.focus)?.length !== 1) return fail(vexError("no-offset", `"window" needs an array space or a tree space, but this space is a ${s.kind}`, at));
|
|
705
|
+
const out = [];
|
|
706
|
+
for (let d = Math.max(from, -s.keys.length); d <= Math.min(to, s.keys.length); d++) {
|
|
707
|
+
const r = applyMove(s, at, {
|
|
708
|
+
t: "offset",
|
|
709
|
+
d: [d]
|
|
710
|
+
});
|
|
711
|
+
if (r.ok) out.push(r.value);
|
|
712
|
+
}
|
|
713
|
+
return ok(out);
|
|
714
|
+
}
|
|
585
715
|
//#endregion
|
|
586
716
|
//#region src/domain.ts
|
|
587
717
|
/** This function makes a domain. The result is frozen. */
|
|
@@ -722,7 +852,8 @@ function compile(expr, opts = {}) {
|
|
|
722
852
|
env
|
|
723
853
|
}, {
|
|
724
854
|
space,
|
|
725
|
-
trace: r.trace
|
|
855
|
+
trace: r.trace,
|
|
856
|
+
tables: r.tables
|
|
726
857
|
});
|
|
727
858
|
} catch (thrown) {
|
|
728
859
|
if (isStackOverflow(thrown)) return fail(vexError("depth", DEPTH_MESSAGE, {
|
|
@@ -774,6 +905,8 @@ function evaluate(expr, opts) {
|
|
|
774
905
|
function explain(expr, opts) {
|
|
775
906
|
return compiled(expr, opts).explain(opts);
|
|
776
907
|
}
|
|
908
|
+
/** This function gives the axis inside the `where` axes of an axis. */
|
|
909
|
+
const baseAxis = (a) => a.t === "where" ? baseAxis(a.axis) : a;
|
|
777
910
|
var Compiler = class {
|
|
778
911
|
#domains;
|
|
779
912
|
#fns;
|
|
@@ -974,7 +1107,17 @@ var Compiler = class {
|
|
|
974
1107
|
env: f.env
|
|
975
1108
|
};
|
|
976
1109
|
}
|
|
977
|
-
const
|
|
1110
|
+
const base = baseAxis(e.axis);
|
|
1111
|
+
let inner = c;
|
|
1112
|
+
if (base.t === "table") {
|
|
1113
|
+
const space = c.tables !== void 0 && Object.hasOwn(c.tables, base.name) ? c.tables[base.name] : void 0;
|
|
1114
|
+
if (space === void 0) return this.#emit(c, info, f, this.#err("no-table", `the evaluation has no table "${base.name}"`, f, path));
|
|
1115
|
+
inner = {
|
|
1116
|
+
...c,
|
|
1117
|
+
space
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
const ts = targets(from, inner);
|
|
978
1121
|
if (!ts.ok) return this.#emit(c, info, f, ts);
|
|
979
1122
|
const items = ts.value.map(({ key, test }) => {
|
|
980
1123
|
if (test !== void 0 && !test.ok) return {
|
|
@@ -985,11 +1128,11 @@ var Compiler = class {
|
|
|
985
1128
|
key,
|
|
986
1129
|
result: body({
|
|
987
1130
|
pos: {
|
|
988
|
-
origin: f.pos.origin,
|
|
1131
|
+
origin: base.t === "table" ? key : f.pos.origin,
|
|
989
1132
|
focus: key
|
|
990
1133
|
},
|
|
991
1134
|
env: f.env
|
|
992
|
-
},
|
|
1135
|
+
}, inner)
|
|
993
1136
|
};
|
|
994
1137
|
});
|
|
995
1138
|
return this.#emit(c, info, f, ok(vexList(items)));
|
|
@@ -997,6 +1140,7 @@ var Compiler = class {
|
|
|
997
1140
|
}
|
|
998
1141
|
/** This method compiles the targets of an axis. For `where`, a failed test keeps the target with its error. */
|
|
999
1142
|
#targets(a, path, counter) {
|
|
1143
|
+
if (a.t === "table") return (_f, c) => ok(c.space.keys.map((key) => ({ key })));
|
|
1000
1144
|
if (a.t !== "where") return (f, c) => {
|
|
1001
1145
|
const keys = axisTargets(c.space, f.pos, a);
|
|
1002
1146
|
return keys.ok ? ok(keys.value.map((key) => ({ key }))) : fail({
|
|
@@ -1007,6 +1151,7 @@ var Compiler = class {
|
|
|
1007
1151
|
const inner = this.#targets(a.axis, path, counter);
|
|
1008
1152
|
const testPath = [...path, counter.next++];
|
|
1009
1153
|
const test = this.node(a.test, testPath);
|
|
1154
|
+
const sameOrigin = baseAxis(a).t === "table";
|
|
1010
1155
|
return (f, c) => {
|
|
1011
1156
|
const ts = inner(f, c);
|
|
1012
1157
|
if (!ts.ok) return ts;
|
|
@@ -1018,7 +1163,7 @@ var Compiler = class {
|
|
|
1018
1163
|
}
|
|
1019
1164
|
const at = {
|
|
1020
1165
|
pos: {
|
|
1021
|
-
origin: f.pos.origin,
|
|
1166
|
+
origin: sameOrigin ? t.key : f.pos.origin,
|
|
1022
1167
|
focus: t.key
|
|
1023
1168
|
},
|
|
1024
1169
|
env: f.env
|
|
@@ -1620,6 +1765,9 @@ function traversal(items, env, isStrict = false) {
|
|
|
1620
1765
|
*
|
|
1621
1766
|
* A cell reference is the extension node `ext("vex.cell", { column, at })`. Thus the IR has no new kind, and the
|
|
1622
1767
|
* JSON form, `deps` and the traces work without a change.
|
|
1768
|
+
*
|
|
1769
|
+
* A cell can have its own formula. That formula replaces the formula of the column at the key of the cell. A
|
|
1770
|
+
* spreadsheet has the same thing: a cell with a formula that is different from the other formulas of its column.
|
|
1623
1771
|
*/
|
|
1624
1772
|
/** The extension kind of a cell reference. */
|
|
1625
1773
|
const CELL_KIND = "vex.cell";
|
|
@@ -1634,7 +1782,17 @@ const cycleError = (column, key) => fail(vexError("cycle", `the cell "${column}"
|
|
|
1634
1782
|
focus: key
|
|
1635
1783
|
}));
|
|
1636
1784
|
/** The id of a cell. */
|
|
1637
|
-
const idOf = (column, key) => `${column}\u0000${key}`;
|
|
1785
|
+
const idOf = (t, column, key) => `${t.tag}\u0001${column}\u0000${key}`;
|
|
1786
|
+
/** This function gives the state of a table at the start of a run. */
|
|
1787
|
+
const tableState = (where, i, t) => ({
|
|
1788
|
+
where,
|
|
1789
|
+
tag: String(i),
|
|
1790
|
+
space: t.space,
|
|
1791
|
+
columns: t.columns,
|
|
1792
|
+
cells: t.cells ?? /* @__PURE__ */ new Map(),
|
|
1793
|
+
warmed: /* @__PURE__ */ new Map(),
|
|
1794
|
+
index: void 0
|
|
1795
|
+
});
|
|
1638
1796
|
/**
|
|
1639
1797
|
* The budget of the stack for the recursion of a run, in estimated frames. Each engine has more stack than this, so
|
|
1640
1798
|
* the recursion of a run cannot fill the stack (SHEET.DEEP).
|
|
@@ -1654,42 +1812,89 @@ const STACK_BUDGET = 3e3;
|
|
|
1654
1812
|
* cell, and the cell that reads it pauses. A paused evaluation reads no other cell and changes no state of the
|
|
1655
1813
|
* search. The run then evaluates the started cell from its own list, and it evaluates the paused cell again after
|
|
1656
1814
|
* that cell. A formula is pure (EVAL.PURE), so the second evaluation gives the result of one evaluation.
|
|
1815
|
+
*
|
|
1816
|
+
* A run can have more tables (SHEET.TABLES). Each table has its own space, and one search covers the cells of all
|
|
1817
|
+
* the tables. Thus a cycle across tables gives `#CYCLE!` too.
|
|
1657
1818
|
*/
|
|
1658
1819
|
var SheetRun = class {
|
|
1659
|
-
#
|
|
1820
|
+
#main;
|
|
1821
|
+
#byName = /* @__PURE__ */ new Map();
|
|
1822
|
+
#bySpace = /* @__PURE__ */ new Map();
|
|
1660
1823
|
#opts;
|
|
1661
1824
|
#done = /* @__PURE__ */ new Map();
|
|
1662
|
-
#warmed = /* @__PURE__ */ new Map();
|
|
1663
|
-
#index;
|
|
1664
1825
|
#visits = /* @__PURE__ */ new Map();
|
|
1665
1826
|
#stack = [];
|
|
1666
|
-
#top
|
|
1667
|
-
id: "",
|
|
1668
|
-
column: "",
|
|
1669
|
-
key: "",
|
|
1670
|
-
index: -1,
|
|
1671
|
-
low: -1,
|
|
1672
|
-
reread: false,
|
|
1673
|
-
result: cycleError("", ""),
|
|
1674
|
-
paused: false,
|
|
1675
|
-
need: void 0
|
|
1676
|
-
};
|
|
1827
|
+
#top;
|
|
1677
1828
|
#next = 0;
|
|
1678
1829
|
#used = 0;
|
|
1679
1830
|
#cost = /* @__PURE__ */ new Map();
|
|
1680
1831
|
#budget;
|
|
1681
1832
|
/** `budget` is the budget of the stack in estimated frames. A test can make it small, so the run pauses often. */
|
|
1682
1833
|
constructor(columns, opts, budget = STACK_BUDGET) {
|
|
1683
|
-
|
|
1834
|
+
const { cells, name, tables, space, ...rest } = opts;
|
|
1835
|
+
this.#main = {
|
|
1836
|
+
where: "the sheet",
|
|
1837
|
+
tag: "0",
|
|
1838
|
+
space,
|
|
1839
|
+
columns,
|
|
1840
|
+
cells: cells ?? /* @__PURE__ */ new Map(),
|
|
1841
|
+
warmed: /* @__PURE__ */ new Map(),
|
|
1842
|
+
index: void 0
|
|
1843
|
+
};
|
|
1844
|
+
this.#bySpace.set(space, this.#main);
|
|
1845
|
+
if (name !== void 0) this.#byName.set(name, this.#main);
|
|
1846
|
+
if (tables !== void 0) {
|
|
1847
|
+
let i = 0;
|
|
1848
|
+
for (const [n, t] of tables) {
|
|
1849
|
+
if (this.#byName.has(n)) continue;
|
|
1850
|
+
const other = tableState(`the table "${n}"`, ++i, t);
|
|
1851
|
+
this.#byName.set(n, other);
|
|
1852
|
+
if (!this.#bySpace.has(t.space)) this.#bySpace.set(t.space, other);
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
this.#top = {
|
|
1856
|
+
id: "",
|
|
1857
|
+
table: this.#main,
|
|
1858
|
+
column: "",
|
|
1859
|
+
key: "",
|
|
1860
|
+
index: -1,
|
|
1861
|
+
low: -1,
|
|
1862
|
+
reread: false,
|
|
1863
|
+
result: cycleError("", ""),
|
|
1864
|
+
paused: false,
|
|
1865
|
+
need: void 0
|
|
1866
|
+
};
|
|
1684
1867
|
this.#budget = budget;
|
|
1868
|
+
const spaces = Object.fromEntries([...this.#byName].map(([n, t]) => [n, t.space]));
|
|
1685
1869
|
this.#opts = {
|
|
1686
|
-
...
|
|
1870
|
+
...rest,
|
|
1871
|
+
tables: spaces,
|
|
1687
1872
|
extensions: {
|
|
1688
|
-
...
|
|
1873
|
+
...rest.extensions,
|
|
1689
1874
|
[CELL_KIND]: this.#handler
|
|
1690
1875
|
}
|
|
1691
1876
|
};
|
|
1692
1877
|
}
|
|
1878
|
+
/** This method gives the program of a cell: its own formula, or the formula of its column (SHEET.CELL.FORMULA). */
|
|
1879
|
+
#programOf(t, column, key) {
|
|
1880
|
+
return (t.cells.size === 0 ? void 0 : t.cells.get(column)?.get(key)) ?? t.columns.get(column);
|
|
1881
|
+
}
|
|
1882
|
+
/** This method gives the error of a cell without a program. */
|
|
1883
|
+
#unbound(t, column, key) {
|
|
1884
|
+
const message = t.cells.has(column) ? `the cell "${column}" at "${key}" has no formula` : `${t.where} has no column "${column}"`;
|
|
1885
|
+
return fail(vexError("unbound", message, {
|
|
1886
|
+
origin: key,
|
|
1887
|
+
focus: key
|
|
1888
|
+
}));
|
|
1889
|
+
}
|
|
1890
|
+
/** This method gives the table of a name for a read from a host. Without a name, it is the main table. */
|
|
1891
|
+
#named(table, key) {
|
|
1892
|
+
if (table === void 0) return this.#main;
|
|
1893
|
+
return this.#byName.get(table) ?? fail(vexError("no-table", `the run has no table "${table}"`, {
|
|
1894
|
+
origin: key,
|
|
1895
|
+
focus: key
|
|
1896
|
+
}));
|
|
1897
|
+
}
|
|
1693
1898
|
#handler = (data, ctx) => {
|
|
1694
1899
|
if (!isCellRef(data)) return fail(vexError("bad-expression", "a cell reference needs a column name and an address", { path: ctx.path }));
|
|
1695
1900
|
const where = resolveAddr(ctx.space, ctx.position, data.at);
|
|
@@ -1697,41 +1902,46 @@ var SheetRun = class {
|
|
|
1697
1902
|
...where.error,
|
|
1698
1903
|
path: ctx.path
|
|
1699
1904
|
});
|
|
1700
|
-
|
|
1905
|
+
/* v8 ignore next -- @preserve: each space of an evaluation of the run is the space of a table */
|
|
1906
|
+
return this.#read(this.#bySpace.get(ctx.space) ?? this.#main, data.column, where.value);
|
|
1701
1907
|
};
|
|
1702
1908
|
/**
|
|
1703
|
-
* This method gives the result of one cell: the column `column` at the key `key
|
|
1704
|
-
* the column at the keys before `key`, in key order. Thus a recurrence
|
|
1909
|
+
* This method gives the result of one cell: the column `column` at the key `key` of the main table, or of the table
|
|
1910
|
+
* `table`. A read from a host first evaluates the column at the keys before `key`, in key order. Thus a recurrence
|
|
1911
|
+
* over the keys reads finished cells.
|
|
1705
1912
|
*/
|
|
1706
|
-
cell(column, key) {
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
return this.#
|
|
1913
|
+
cell(column, key, table) {
|
|
1914
|
+
const t = this.#named(table, key);
|
|
1915
|
+
if (!("space" in t)) return t;
|
|
1916
|
+
if (this.#top.index >= 0) return this.#read(t, column, key);
|
|
1917
|
+
this.#warmTo(t, column, key);
|
|
1918
|
+
return this.#drive(t, column, key);
|
|
1710
1919
|
}
|
|
1711
1920
|
/** This method evaluates the column `column` at each key before `key` that it did not evaluate before, in key order. */
|
|
1712
|
-
#warmTo(column, key) {
|
|
1713
|
-
if (!
|
|
1714
|
-
const keys =
|
|
1715
|
-
|
|
1716
|
-
const end =
|
|
1717
|
-
const start =
|
|
1718
|
-
for (let i = start; i < end; i++) this.#drive(column, keys[i] ?? key);
|
|
1719
|
-
if (end > start)
|
|
1720
|
-
}
|
|
1721
|
-
/** This method gives the estimated frames of the stack that one evaluation of
|
|
1722
|
-
#costOf(
|
|
1723
|
-
let cost = this.#cost.get(
|
|
1921
|
+
#warmTo(t, column, key) {
|
|
1922
|
+
if (!t.columns.has(column)) return;
|
|
1923
|
+
const keys = t.space.keys;
|
|
1924
|
+
t.index ??= new Map(keys.map((k, i) => [k, i]));
|
|
1925
|
+
const end = t.index.get(key) ?? 0;
|
|
1926
|
+
const start = t.warmed.get(column) ?? 0;
|
|
1927
|
+
for (let i = start; i < end; i++) this.#drive(t, column, keys[i] ?? key);
|
|
1928
|
+
if (end > start) t.warmed.set(column, end);
|
|
1929
|
+
}
|
|
1930
|
+
/** This method gives the estimated frames of the stack that one evaluation of the program `program` uses. */
|
|
1931
|
+
#costOf(program) {
|
|
1932
|
+
let cost = this.#cost.get(program);
|
|
1724
1933
|
if (cost === void 0) {
|
|
1725
1934
|
cost = 3 * nestingOf(program) + 16;
|
|
1726
|
-
this.#cost.set(
|
|
1935
|
+
this.#cost.set(program, cost);
|
|
1727
1936
|
}
|
|
1728
1937
|
return cost;
|
|
1729
1938
|
}
|
|
1730
1939
|
/** This method puts a new cell on the stack of the search. */
|
|
1731
|
-
#start(column, key) {
|
|
1940
|
+
#start(t, column, key) {
|
|
1732
1941
|
const n = this.#next++;
|
|
1733
1942
|
const visit = {
|
|
1734
|
-
id: idOf(column, key),
|
|
1943
|
+
id: idOf(t, column, key),
|
|
1944
|
+
table: t,
|
|
1735
1945
|
column,
|
|
1736
1946
|
key,
|
|
1737
1947
|
index: n,
|
|
@@ -1748,13 +1958,14 @@ var SheetRun = class {
|
|
|
1748
1958
|
/** This method evaluates the formula of a cell once. When the evaluation pauses, the result of the cell does not change. */
|
|
1749
1959
|
#evaluate(visit, program) {
|
|
1750
1960
|
const caller = this.#top;
|
|
1751
|
-
const cost = this.#costOf(
|
|
1961
|
+
const cost = this.#costOf(program);
|
|
1752
1962
|
visit.paused = false;
|
|
1753
1963
|
visit.need = void 0;
|
|
1754
1964
|
this.#top = visit;
|
|
1755
1965
|
this.#used += cost;
|
|
1756
1966
|
const result = evaluate(program, {
|
|
1757
1967
|
...this.#opts,
|
|
1968
|
+
space: visit.table.space,
|
|
1758
1969
|
origin: visit.key
|
|
1759
1970
|
});
|
|
1760
1971
|
this.#used -= cost;
|
|
@@ -1767,8 +1978,8 @@ var SheetRun = class {
|
|
|
1767
1978
|
else if (caller !== void 0) caller.low = Math.min(caller.low, visit.low);
|
|
1768
1979
|
}
|
|
1769
1980
|
/** This method reads a cell from the formula of the cell at the top. */
|
|
1770
|
-
#read(column, key) {
|
|
1771
|
-
const id = idOf(column, key);
|
|
1981
|
+
#read(t, column, key) {
|
|
1982
|
+
const id = idOf(t, column, key);
|
|
1772
1983
|
const done = this.#done.get(id);
|
|
1773
1984
|
if (done !== void 0) return done;
|
|
1774
1985
|
const caller = this.#top;
|
|
@@ -1779,13 +1990,10 @@ var SheetRun = class {
|
|
|
1779
1990
|
seen.reread = true;
|
|
1780
1991
|
return cycleError(column, key);
|
|
1781
1992
|
}
|
|
1782
|
-
const program = this.#
|
|
1783
|
-
if (program === void 0) return
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
}));
|
|
1787
|
-
const visit = this.#start(column, key);
|
|
1788
|
-
if (this.#used + this.#costOf(column, program) > this.#budget) {
|
|
1993
|
+
const program = this.#programOf(t, column, key);
|
|
1994
|
+
if (program === void 0) return this.#unbound(t, column, key);
|
|
1995
|
+
const visit = this.#start(t, column, key);
|
|
1996
|
+
if (this.#used + this.#costOf(program) > this.#budget) {
|
|
1789
1997
|
caller.paused = true;
|
|
1790
1998
|
caller.need = visit;
|
|
1791
1999
|
return cycleError(column, key);
|
|
@@ -1800,18 +2008,15 @@ var SheetRun = class {
|
|
|
1800
2008
|
return this.#done.get(id) ?? cycleError(column, key);
|
|
1801
2009
|
}
|
|
1802
2010
|
/** This method evaluates a cell from the top level. The cells that pause go on a list, not on the call stack. */
|
|
1803
|
-
#drive(column, key) {
|
|
1804
|
-
const id = idOf(column, key);
|
|
2011
|
+
#drive(t, column, key) {
|
|
2012
|
+
const id = idOf(t, column, key);
|
|
1805
2013
|
const done = this.#done.get(id);
|
|
1806
2014
|
if (done !== void 0) return done;
|
|
1807
|
-
if (
|
|
1808
|
-
|
|
1809
|
-
focus: key
|
|
1810
|
-
}));
|
|
1811
|
-
const work = [this.#start(column, key)];
|
|
2015
|
+
if (this.#programOf(t, column, key) === void 0) return this.#unbound(t, column, key);
|
|
2016
|
+
const work = [this.#start(t, column, key)];
|
|
1812
2017
|
for (let visit = work.at(-1); visit !== void 0; visit = work.at(-1)) {
|
|
1813
|
-
const program = this.#
|
|
1814
|
-
/* v8 ignore next -- @preserve: a visit exists only for a
|
|
2018
|
+
const program = this.#programOf(visit.table, visit.column, visit.key);
|
|
2019
|
+
/* v8 ignore next -- @preserve: a visit exists only for a cell with a program */
|
|
1815
2020
|
if (program === void 0) break;
|
|
1816
2021
|
this.#evaluate(visit, program);
|
|
1817
2022
|
if (visit.paused) {
|
|
@@ -1832,17 +2037,19 @@ var SheetRun = class {
|
|
|
1832
2037
|
this.#done.set(m.id, cyclic ? cycleError(m.column, m.key) : m.result);
|
|
1833
2038
|
}
|
|
1834
2039
|
}
|
|
1835
|
-
/** This method evaluates one cell with a trace of the nodes of its
|
|
1836
|
-
explain(column, key) {
|
|
1837
|
-
const result = this.cell(column, key);
|
|
1838
|
-
const
|
|
1839
|
-
|
|
2040
|
+
/** This method evaluates one cell with a trace of the nodes of its program. The result is the result of the cell. */
|
|
2041
|
+
explain(column, key, table) {
|
|
2042
|
+
const result = this.cell(column, key, table);
|
|
2043
|
+
const t = this.#named(table, key);
|
|
2044
|
+
const program = "space" in t ? this.#programOf(t, column, key) : void 0;
|
|
2045
|
+
if (program === void 0 || !("space" in t)) return {
|
|
1840
2046
|
events: [],
|
|
1841
2047
|
result
|
|
1842
2048
|
};
|
|
1843
2049
|
return {
|
|
1844
2050
|
events: explain(program, {
|
|
1845
2051
|
...this.#opts,
|
|
2052
|
+
space: t.space,
|
|
1846
2053
|
origin: key
|
|
1847
2054
|
}).events,
|
|
1848
2055
|
result
|
|
@@ -1852,8 +2059,9 @@ var SheetRun = class {
|
|
|
1852
2059
|
* This method evaluates the column `column` at each key before `key`, in key order. A recurrence over the keys,
|
|
1853
2060
|
* for example a running total, then reads finished cells.
|
|
1854
2061
|
*/
|
|
1855
|
-
warm(column, key) {
|
|
1856
|
-
this.#
|
|
2062
|
+
warm(column, key, table) {
|
|
2063
|
+
const t = this.#named(table, key);
|
|
2064
|
+
if ("space" in t) this.#warmTo(t, column, key);
|
|
1857
2065
|
}
|
|
1858
2066
|
};
|
|
1859
2067
|
//#endregion
|
|
@@ -2059,6 +2267,28 @@ var ChainImpl = class ChainImpl {
|
|
|
2059
2267
|
siblings(body, opts) {
|
|
2060
2268
|
return this.#axis({ t: "siblings" }, body, opts);
|
|
2061
2269
|
}
|
|
2270
|
+
window(from, to, body, opts) {
|
|
2271
|
+
return this.#axis({
|
|
2272
|
+
t: "window",
|
|
2273
|
+
from,
|
|
2274
|
+
to
|
|
2275
|
+
}, body, opts);
|
|
2276
|
+
}
|
|
2277
|
+
table(name, body, opts) {
|
|
2278
|
+
return this.#axis({
|
|
2279
|
+
t: "table",
|
|
2280
|
+
name
|
|
2281
|
+
}, body, opts);
|
|
2282
|
+
}
|
|
2283
|
+
successors(body, opts) {
|
|
2284
|
+
return this.#axis({ t: "successors" }, body, opts);
|
|
2285
|
+
}
|
|
2286
|
+
predecessors(body, opts) {
|
|
2287
|
+
return this.#axis({ t: "predecessors" }, body, opts);
|
|
2288
|
+
}
|
|
2289
|
+
reachable(body, opts) {
|
|
2290
|
+
return this.#axis({ t: "reachable" }, body, opts);
|
|
2291
|
+
}
|
|
2062
2292
|
};
|
|
2063
2293
|
/** This function gives the options of an evaluation of a chain or a list chain at the origin `k`. */
|
|
2064
2294
|
function evalOptions(s, k) {
|
|
@@ -2194,25 +2424,36 @@ var SheetRootImpl = class extends RootImpl {
|
|
|
2194
2424
|
var SheetImpl = class SheetImpl {
|
|
2195
2425
|
#env;
|
|
2196
2426
|
#columns;
|
|
2197
|
-
|
|
2427
|
+
#cells;
|
|
2428
|
+
constructor(env, columns, cells = /* @__PURE__ */ new Map()) {
|
|
2198
2429
|
this.#env = env;
|
|
2199
2430
|
this.#columns = columns;
|
|
2431
|
+
this.#cells = cells;
|
|
2200
2432
|
}
|
|
2201
2433
|
get columns() {
|
|
2202
2434
|
return Object.freeze(Object.fromEntries(this.#columns));
|
|
2203
2435
|
}
|
|
2436
|
+
get cellFormulas() {
|
|
2437
|
+
return Object.freeze(Object.fromEntries([...this.#cells].map(([c, keys]) => [c, Object.freeze(Object.fromEntries(keys))])));
|
|
2438
|
+
}
|
|
2204
2439
|
declare() {
|
|
2205
2440
|
return this;
|
|
2206
2441
|
}
|
|
2207
2442
|
column(name, formula) {
|
|
2208
2443
|
const program = formula(new SheetRootImpl(this.#env)).program;
|
|
2209
|
-
return new SheetImpl(this.#env, new Map([...this.#columns, [name, program]]));
|
|
2444
|
+
return new SheetImpl(this.#env, new Map([...this.#columns, [name, program]]), this.#cells);
|
|
2445
|
+
}
|
|
2446
|
+
cellFormula(k, column, formula) {
|
|
2447
|
+
const program = formula(new SheetRootImpl(this.#env)).program;
|
|
2448
|
+
const keys = new Map([...this.#cells.get(column) ?? [], [k, program]]);
|
|
2449
|
+
return new SheetImpl(this.#env, this.#columns, new Map([...this.#cells, [column, keys]]));
|
|
2210
2450
|
}
|
|
2211
2451
|
#run() {
|
|
2212
2452
|
const { space: s, domains, options } = this.#env;
|
|
2213
2453
|
return new SheetRun(this.#columns, {
|
|
2214
2454
|
space: s,
|
|
2215
2455
|
domains,
|
|
2456
|
+
cells: this.#cells,
|
|
2216
2457
|
...options.fns === void 0 ? {} : { fns: options.fns },
|
|
2217
2458
|
...options.extensions === void 0 ? {} : { extensions: options.extensions }
|
|
2218
2459
|
});
|