@mark1russell7/vex 0.5.0 → 0.7.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 +1 -0
- package/build/index.d.ts +257 -38
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1061 -175
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/builder.ts +163 -16
- package/src/deps.ts +13 -9
- package/src/domain.ts +22 -0
- package/src/errors.ts +7 -1
- package/src/evaluate.ts +188 -24
- package/src/ir.ts +203 -44
- package/src/json.ts +29 -1
- package/src/sheet.ts +388 -74
- package/src/space.ts +175 -12
package/src/sheet.ts
CHANGED
|
@@ -5,45 +5,228 @@
|
|
|
5
5
|
*
|
|
6
6
|
* A cell reference is the extension node `ext("vex.cell", { column, at })`. Thus the IR has no new kind, and the
|
|
7
7
|
* JSON form, `deps` and the traces work without a change.
|
|
8
|
+
*
|
|
9
|
+
* A cell can have its own formula. That formula replaces the formula of the column at the key of the cell. A
|
|
10
|
+
* spreadsheet has the same thing: a cell with a formula that is different from the other formulas of its column.
|
|
8
11
|
*/
|
|
9
12
|
import { vexError } from "./errors.ts";
|
|
10
|
-
import { evaluate, explain, nestingOf, type EvalOptions, type ExtHandler } from "./evaluate.ts";
|
|
11
|
-
import { ext, isMove, type
|
|
13
|
+
import { evaluate, explain, nestingOf, type CompileOptions, type EvalOptions, type ExtHandler, type Program } from "./evaluate.ts";
|
|
14
|
+
import { addrExprs, children, ext, isMove, moveExpr, shapeError, type Axis, type Expr, type FixedMove, type Move } from "./ir.ts";
|
|
12
15
|
import { fail, type Result } from "./result.ts";
|
|
13
|
-
import { resolveAddr } from "./space.ts";
|
|
16
|
+
import { resolveAddr, type Space } from "./space.ts";
|
|
14
17
|
import type { Trace } from "./trace.ts";
|
|
15
18
|
|
|
16
19
|
/** The extension kind of a cell reference. */
|
|
17
20
|
export const CELL_KIND = "vex.cell";
|
|
18
21
|
|
|
19
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* The data of a cell reference: the column, and the address from the focus. The data of an extension is not an
|
|
24
|
+
* expression, so the address has only fixed moves.
|
|
25
|
+
*/
|
|
20
26
|
export interface CellRef {
|
|
21
27
|
readonly column: string;
|
|
22
|
-
readonly at:
|
|
28
|
+
readonly at: readonly FixedMove[];
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
/** This function makes a cell reference: the column `column` at the address `at` from the focus. */
|
|
26
|
-
export const cell = (column: string, at:
|
|
32
|
+
export const cell = (column: string, at: readonly FixedMove[] = []): Expr => ext(CELL_KIND, { column, at } satisfies CellRef);
|
|
27
33
|
|
|
28
34
|
const isCellRef = (u: unknown): u is CellRef =>
|
|
29
35
|
typeof u === "object" &&
|
|
30
36
|
u !== null &&
|
|
31
37
|
typeof (u as { column?: unknown }).column === "string" &&
|
|
32
38
|
Array.isArray((u as { at?: unknown }).at) &&
|
|
33
|
-
(u as { at: readonly unknown[] }).at.every(isMove);
|
|
39
|
+
(u as { at: readonly unknown[] }).at.every((m) => isMove(m) && moveExpr(m) === undefined);
|
|
40
|
+
|
|
41
|
+
/** The formulas of single cells: for each column, the formula of each key that has its own formula. */
|
|
42
|
+
export type CellFormulas = ReadonlyMap<string, ReadonlyMap<string, Expr>>;
|
|
43
|
+
|
|
44
|
+
/** A table of a run: its space, the formulas of its columns, and the formulas of its cells. */
|
|
45
|
+
export interface SheetTable {
|
|
46
|
+
readonly space: Space;
|
|
47
|
+
readonly columns: ReadonlyMap<string, Expr>;
|
|
48
|
+
readonly cells?: CellFormulas;
|
|
49
|
+
}
|
|
34
50
|
|
|
35
|
-
/** The options of a sheet run: the evaluation options without the origin and the trace. */
|
|
36
|
-
export type SheetOptions = Omit<EvalOptions, "origin" | "trace"
|
|
51
|
+
/** The options of a sheet run: the evaluation options without the origin and the trace, and the tables. */
|
|
52
|
+
export type SheetOptions = Omit<EvalOptions, "origin" | "trace" | "tables"> & {
|
|
53
|
+
/** The cells with their own formula. The formula of a cell replaces the formula of its column at its key. */
|
|
54
|
+
readonly cells?: CellFormulas;
|
|
55
|
+
/** The name of this table, so the axis `table(name)` in a formula of another table can read it. */
|
|
56
|
+
readonly name?: string;
|
|
57
|
+
/** The other tables, by name (SHEET.TABLES). A cell reference reads the table of the space where it evaluates. */
|
|
58
|
+
readonly tables?: ReadonlyMap<string, SheetTable>;
|
|
59
|
+
/**
|
|
60
|
+
* The compiler of the formulas of the columns (SHEET.COMPILE), for example `tiered` of `@mark1russell7/vex-compiler`.
|
|
61
|
+
* Without a compiler, the interpreter evaluates each formula. The compiled program must give the results of the
|
|
62
|
+
* interpreter.
|
|
63
|
+
*/
|
|
64
|
+
readonly compile?: CompileFn;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** A compiler of the formulas of the columns of a run. */
|
|
68
|
+
export type CompileFn = (expr: Expr, options: CompileOptions) => Program;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The compiled programs of the formulas of the columns: one entry for each formula, for the last compiler and options.
|
|
72
|
+
* A compiled program reads the cells of the active run, so each run with the same compiler and options can use it.
|
|
73
|
+
*/
|
|
74
|
+
const PROGRAMS = new WeakMap<Expr, { readonly compile: CompileFn; readonly domains: unknown; readonly fns: unknown; readonly extensions: unknown; readonly program: Program }>();
|
|
75
|
+
|
|
76
|
+
/** The key of the options without extensions, for the extensions of the compiled programs. */
|
|
77
|
+
const NO_EXTENSIONS = {};
|
|
78
|
+
|
|
79
|
+
/** The state of one table in a run. */
|
|
80
|
+
interface Table {
|
|
81
|
+
/** The words for the table in a message: "the sheet" for the main table, or the table and its name. */
|
|
82
|
+
readonly where: string;
|
|
83
|
+
/** A short text for the ids of the cells of the table. */
|
|
84
|
+
readonly tag: string;
|
|
85
|
+
readonly space: Space;
|
|
86
|
+
readonly columns: ReadonlyMap<string, Expr>;
|
|
87
|
+
readonly cells: CellFormulas;
|
|
88
|
+
// For each column, the number of keys at the start of the key order that a warm-up evaluated.
|
|
89
|
+
readonly warmed: Map<string, number>;
|
|
90
|
+
// The position of each key in the key order, made at the first warm-up.
|
|
91
|
+
index: ReadonlyMap<string, number> | undefined;
|
|
92
|
+
/** The result of each finished cell: for each column, the result at each key. */
|
|
93
|
+
readonly done: Map<string, Map<string, Result<unknown>>>;
|
|
94
|
+
}
|
|
37
95
|
|
|
38
96
|
const cycleError = (column: string, key: string): Result<never> =>
|
|
39
97
|
fail(vexError("cycle", `the cell "${column}" at "${key}" is on a cycle of cell references`, { origin: key, focus: key }));
|
|
40
98
|
|
|
41
99
|
/** The id of a cell. */
|
|
42
|
-
const idOf = (column: string, key: string): string => `${column}\u0000${key}`;
|
|
100
|
+
const idOf = (t: Table, column: string, key: string): string => `${t.tag}\u0001${column}\u0000${key}`;
|
|
101
|
+
|
|
102
|
+
/** This function gives the state of a table at the start of a run. */
|
|
103
|
+
const tableState = (where: string, i: number, t: SheetTable): Table => ({ where, tag: String(i), space: t.space, columns: t.columns, cells: t.cells ?? new Map(), warmed: new Map(), index: undefined, done: new Map() });
|
|
104
|
+
|
|
105
|
+
/** This function gives the result of a finished cell, or `undefined`. */
|
|
106
|
+
const doneOf = (t: Table, column: string, key: string): Result<unknown> | undefined => t.done.get(column)?.get(key);
|
|
107
|
+
|
|
108
|
+
/** This function keeps the result of a finished cell. */
|
|
109
|
+
function setDone(t: Table, column: string, key: string, result: Result<unknown>): void {
|
|
110
|
+
let results = t.done.get(column);
|
|
111
|
+
if (results === undefined) {
|
|
112
|
+
results = new Map();
|
|
113
|
+
t.done.set(column, results);
|
|
114
|
+
}
|
|
115
|
+
results.set(key, result);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** A direction of the reads of a column from itself: to an earlier key, to a later key, or down a tree to the children. */
|
|
119
|
+
type Direction = "before" | "after" | "down";
|
|
120
|
+
|
|
121
|
+
/** This function adds the direction of one move or one axis of a read of a column from itself. */
|
|
122
|
+
function addDirection(d: Set<Direction>, m: Move | Axis): void {
|
|
123
|
+
if (m.t === "offset" || m.t === "window") {
|
|
124
|
+
// An offset moves by its first step that is not 0. A window reads the keys from `from` to `to`.
|
|
125
|
+
const steps = m.t === "offset" ? [m.d.find((x) => x !== 0) ?? 0] : [m.from, m.to];
|
|
126
|
+
// A step of 0 reads the cell itself, which is a cycle in each order.
|
|
127
|
+
// Stryker disable next-line EqualityOperator: see the comment above
|
|
128
|
+
if (steps.some((x) => x < 0)) d.add("before");
|
|
129
|
+
// Stryker disable next-line EqualityOperator: see the comment above
|
|
130
|
+
if (steps.some((x) => x > 0)) d.add("after");
|
|
131
|
+
} else if (m.t === "children" || m.t === "descendants") d.add("down");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** This function gives the base axis of an axis, and the tests of its `where` axes. */
|
|
135
|
+
function partsOf(axis: Axis): { readonly base: Axis; readonly tests: readonly Expr[] } {
|
|
136
|
+
const tests: Expr[] = [];
|
|
137
|
+
let base = axis;
|
|
138
|
+
while (base.t === "where") {
|
|
139
|
+
tests.push(base.test);
|
|
140
|
+
base = base.axis;
|
|
141
|
+
}
|
|
142
|
+
return { base, tests };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* This function gives the columns that the formulas of the column `self` read with a cell reference: its formula and
|
|
147
|
+
* the formulas of its cells. It also gives the directions of their reads of the column itself. A read in the body or
|
|
148
|
+
* in a test of an axis also has the direction of that axis. It walks each expression with a stack, not with recursion.
|
|
149
|
+
*/
|
|
150
|
+
function readsOf(t: Table, self: string): { readonly columns: ReadonlySet<string>; readonly directions: ReadonlySet<Direction> } {
|
|
151
|
+
const columns = new Set<string>();
|
|
152
|
+
const directions = new Set<Direction>();
|
|
153
|
+
const formula = t.columns.get(self);
|
|
154
|
+
// An expression that is not valid reads no column. Its evaluation gives the error (EVAL.SHAPE).
|
|
155
|
+
const todo: [Expr, Axis | undefined][] = [...(formula === undefined ? [] : [formula]), ...(t.cells.get(self)?.values() ?? [])].filter((e) => shapeError(e) === undefined).map((e) => [e, undefined]);
|
|
156
|
+
for (let item = todo.pop(); item !== undefined; item = todo.pop()) {
|
|
157
|
+
const [e, axis] = item;
|
|
158
|
+
if (e.tag === "each" || e.tag === "fold") {
|
|
159
|
+
const { base, tests } = partsOf(e.axis);
|
|
160
|
+
for (const x of [...(e.tag === "each" ? [e.body] : Object.values(e.step)), ...tests]) todo.push([x, base]);
|
|
161
|
+
// The values of init, the result of a fold and the expressions of the address evaluate at the node, so they keep
|
|
162
|
+
// the outer direction.
|
|
163
|
+
for (const x of [...(e.tag === "fold" ? [...Object.values(e.init), e.result] : []), ...addrExprs(e.at)]) todo.push([x, axis]);
|
|
164
|
+
} else if (e.tag !== "ext") {
|
|
165
|
+
for (const child of children(e)) todo.push([child, axis]);
|
|
166
|
+
// The data of an extension of another kind can look like a cell reference. It only changes the order of a fill.
|
|
167
|
+
// Stryker disable next-line ConditionalExpression,LogicalOperator: see the comment above
|
|
168
|
+
} else if (e.kind === CELL_KIND && isCellRef(e.data)) {
|
|
169
|
+
columns.add(e.data.column);
|
|
170
|
+
// The directions of the reads of another column only change the order of a fill, because that column comes first.
|
|
171
|
+
// Stryker disable next-line ConditionalExpression: see the comment above
|
|
172
|
+
if (e.data.column === self) for (const m of [...(axis === undefined ? [] : [axis]), ...e.data.at]) addDirection(directions, m);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return { columns, directions };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* This function gives the columns of a table in an order where each column comes after the columns that it reads.
|
|
180
|
+
* The columns of a cycle keep their order. The columns with formulas of cells only come last.
|
|
181
|
+
*/
|
|
182
|
+
function columnOrder(t: Table): readonly string[] {
|
|
183
|
+
const names = new Set([...t.columns.keys(), ...t.cells.keys()]);
|
|
184
|
+
let left = [...names].map((c) => ({ c, needs: [...readsOf(t, c).columns].filter((x) => x !== c && names.has(x)) }));
|
|
185
|
+
const order: string[] = [];
|
|
186
|
+
const placed = new Set<string>();
|
|
187
|
+
// Each pass places each column whose reads are placed. A pass that places no column leaves a cycle, so it places
|
|
188
|
+
// the first column that is left.
|
|
189
|
+
while (left.length > 0) {
|
|
190
|
+
const ready = left.filter((x) => x.needs.every((n) => placed.has(n)));
|
|
191
|
+
for (const x of ready.length > 0 ? ready : left.slice(0, 1)) {
|
|
192
|
+
order.push(x.c);
|
|
193
|
+
placed.add(x.c);
|
|
194
|
+
}
|
|
195
|
+
left = left.filter((x) => !placed.has(x.c));
|
|
196
|
+
}
|
|
197
|
+
return order;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* This function gives the keys of a space in the order of the reads of a column from itself. A read of a later key
|
|
202
|
+
* reverses the order. In a tree, a read of the children puts the children before their parent.
|
|
203
|
+
*/
|
|
204
|
+
function keyOrder(s: Space, d: ReadonlySet<Direction>): readonly string[] {
|
|
205
|
+
const reverse = d.has("after") && !d.has("before");
|
|
206
|
+
const childrenOf = s.childrenOf?.bind(s);
|
|
207
|
+
if (childrenOf === undefined) return reverse ? s.keys.toReversed() : s.keys;
|
|
208
|
+
const post = d.has("down");
|
|
209
|
+
// The order with each parent before its children, and the order after the children is that order from the end, with
|
|
210
|
+
// the siblings in the other order. `push` puts the next key to visit at the end of the stack.
|
|
211
|
+
const backwards = post !== reverse;
|
|
212
|
+
const push = (stack: string[], keys: readonly string[]): void => {
|
|
213
|
+
for (const k of backwards ? keys : keys.toReversed()) stack.push(k);
|
|
214
|
+
};
|
|
215
|
+
const isChild = new Set(s.keys.flatMap((k) => childrenOf(k)));
|
|
216
|
+
const stack: string[] = [];
|
|
217
|
+
push(stack, s.keys.filter((k) => !isChild.has(k)));
|
|
218
|
+
const out: string[] = [];
|
|
219
|
+
for (let k = stack.pop(); k !== undefined; k = stack.pop()) {
|
|
220
|
+
out.push(k);
|
|
221
|
+
push(stack, childrenOf(k));
|
|
222
|
+
}
|
|
223
|
+
return post ? out.toReversed() : out;
|
|
224
|
+
}
|
|
43
225
|
|
|
44
226
|
/** The state of one cell in the search. */
|
|
45
227
|
interface Visit {
|
|
46
228
|
readonly id: string;
|
|
229
|
+
readonly table: Table;
|
|
47
230
|
readonly column: string;
|
|
48
231
|
readonly key: string;
|
|
49
232
|
readonly index: number;
|
|
@@ -76,78 +259,149 @@ const STACK_BUDGET = 3000;
|
|
|
76
259
|
* cell, and the cell that reads it pauses. A paused evaluation reads no other cell and changes no state of the
|
|
77
260
|
* search. The run then evaluates the started cell from its own list, and it evaluates the paused cell again after
|
|
78
261
|
* that cell. A formula is pure (EVAL.PURE), so the second evaluation gives the result of one evaluation.
|
|
262
|
+
*
|
|
263
|
+
* A run can have more tables (SHEET.TABLES). Each table has its own space, and one search covers the cells of all
|
|
264
|
+
* the tables. Thus a cycle across tables gives `#CYCLE!` too.
|
|
79
265
|
*/
|
|
80
266
|
export class SheetRun {
|
|
81
|
-
readonly #
|
|
82
|
-
|
|
83
|
-
readonly #
|
|
84
|
-
|
|
85
|
-
readonly #
|
|
86
|
-
// The position of each key in the key order, made at the first warm-up.
|
|
87
|
-
#index: ReadonlyMap<string, number> | undefined;
|
|
267
|
+
readonly #main: Table;
|
|
268
|
+
// The tables by name and by space. A cell reference reads the table of the space where it evaluates.
|
|
269
|
+
readonly #byName = new Map<string, Table>();
|
|
270
|
+
readonly #bySpace = new Map<Space, Table>();
|
|
271
|
+
readonly #opts: Omit<EvalOptions, "origin" | "trace" | "space">;
|
|
88
272
|
// The cells on the stack of the search: started, but their component is not finished.
|
|
89
273
|
readonly #visits = new Map<string, Visit>();
|
|
90
274
|
// Stryker disable next-line ArrayDeclaration: the search never reads below the root of a component
|
|
91
275
|
readonly #stack: Visit[] = [];
|
|
92
276
|
// The cell whose formula is in evaluation. At the top level, it is a placeholder that is not a cell.
|
|
93
|
-
|
|
94
|
-
|
|
277
|
+
#top: Visit;
|
|
278
|
+
// The placeholder at the top in a fill, and true when the evaluation of a fill reads a cell that is not finished.
|
|
279
|
+
readonly #filling: Visit;
|
|
280
|
+
#missed = false;
|
|
95
281
|
#next = 0;
|
|
96
|
-
// The estimated frames of the stack that the evaluations in progress use, and the cost of
|
|
282
|
+
// The estimated frames of the stack that the evaluations in progress use, and the cost of each program.
|
|
97
283
|
#used = 0;
|
|
98
|
-
readonly #cost = new Map<
|
|
284
|
+
readonly #cost = new Map<Expr, number>();
|
|
99
285
|
readonly #budget: number;
|
|
286
|
+
readonly #compile: CompileFn | undefined;
|
|
287
|
+
readonly #compileOptions: CompileOptions;
|
|
288
|
+
// The run that evaluates a formula with a compiled program. Each run is synchronous, so a run inside the evaluation
|
|
289
|
+
// of another run gives the active run back when it ends.
|
|
290
|
+
static #active: SheetRun | undefined;
|
|
291
|
+
// The extensions of the compiled programs, for each object of extensions of the options. Their cell handler reads
|
|
292
|
+
// the cells of the active run, so a compiled program does not depend on one run.
|
|
293
|
+
static readonly #shared = new WeakMap<object, Readonly<Record<string, ExtHandler>>>();
|
|
100
294
|
|
|
101
295
|
/** `budget` is the budget of the stack in estimated frames. A test can make it small, so the run pauses often. */
|
|
102
296
|
constructor(columns: ReadonlyMap<string, Expr>, opts: SheetOptions, budget: number = STACK_BUDGET) {
|
|
103
|
-
|
|
297
|
+
const { cells, name, tables, space, compile, ...rest } = opts;
|
|
298
|
+
this.#main = { where: "the sheet", tag: "0", space, columns, cells: cells ?? new Map(), warmed: new Map(), index: undefined, done: new Map() };
|
|
299
|
+
this.#bySpace.set(space, this.#main);
|
|
300
|
+
if (name !== undefined) this.#byName.set(name, this.#main);
|
|
301
|
+
if (tables !== undefined) {
|
|
302
|
+
let i = 0;
|
|
303
|
+
for (const [n, t] of tables) {
|
|
304
|
+
if (this.#byName.has(n)) continue;
|
|
305
|
+
const other = tableState(`the table "${n}"`, ++i, t);
|
|
306
|
+
this.#byName.set(n, other);
|
|
307
|
+
// A table with the space of an earlier table does not take its cells: the first table keeps the space.
|
|
308
|
+
if (!this.#bySpace.has(t.space)) this.#bySpace.set(t.space, other);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// Stryker disable next-line all: the placeholder is not a cell, so the search reads none of its fields
|
|
312
|
+
this.#top = { id: "", table: this.#main, column: "", key: "", index: -1, low: -1, reread: false, result: cycleError("", ""), paused: false, need: undefined };
|
|
313
|
+
// Stryker disable next-line all: the placeholder is not a cell, so the search reads none of its fields
|
|
314
|
+
this.#filling = { id: "", table: this.#main, column: "", key: "", index: 0, low: 0, reread: false, result: cycleError("", ""), paused: false, need: undefined };
|
|
104
315
|
this.#budget = budget;
|
|
105
|
-
this.#
|
|
316
|
+
this.#compile = compile;
|
|
317
|
+
const spaces = Object.fromEntries([...this.#byName].map(([n, t]) => [n, t.space]));
|
|
318
|
+
this.#opts = { ...rest, tables: spaces, extensions: { ...rest.extensions, [CELL_KIND]: this.#handler } };
|
|
319
|
+
const outer = rest.extensions ?? NO_EXTENSIONS;
|
|
320
|
+
let shared = SheetRun.#shared.get(outer);
|
|
321
|
+
if (shared === undefined) {
|
|
322
|
+
shared = { ...rest.extensions, [CELL_KIND]: SheetRun.#cellOfActive };
|
|
323
|
+
SheetRun.#shared.set(outer, shared);
|
|
324
|
+
}
|
|
325
|
+
this.#compileOptions = { ...this.#opts, extensions: shared };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** The cell handler of the compiled programs: it reads the cells of the active run. */
|
|
329
|
+
static readonly #cellOfActive: ExtHandler = (data, ctx) => {
|
|
330
|
+
const run = SheetRun.#active;
|
|
331
|
+
return run === undefined ? fail(vexError("unbound", "a compiled formula reads cells only in the evaluation of a sheet run")) : run.#handler(data, ctx);
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/** This method gives the program of a cell: its own formula, or the formula of its column (SHEET.CELL.FORMULA). */
|
|
335
|
+
#programOf(t: Table, column: string, key: string): Expr | undefined {
|
|
336
|
+
// Stryker disable next-line ConditionalExpression: without formulas of cells, the lookup gives undefined anyway
|
|
337
|
+
const own = t.cells.size === 0 ? undefined : t.cells.get(column)?.get(key);
|
|
338
|
+
return own ?? t.columns.get(column);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** This method gives the error of a cell without a program. */
|
|
342
|
+
#unbound(t: Table, column: string, key: string): Result<never> {
|
|
343
|
+
const message = t.cells.has(column) ? `the cell "${column}" at "${key}" has no formula` : `${t.where} has no column "${column}"`;
|
|
344
|
+
return fail(vexError("unbound", message, { origin: key, focus: key }));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** This method gives the table of a name for a read from a host. Without a name, it is the main table. */
|
|
348
|
+
#named(table: string | undefined, key: string): Table | Result<never> {
|
|
349
|
+
if (table === undefined) return this.#main;
|
|
350
|
+
return this.#byName.get(table) ?? fail(vexError("no-table", `the run has no table "${table}"`, { origin: key, focus: key }));
|
|
106
351
|
}
|
|
107
352
|
|
|
108
353
|
readonly #handler: ExtHandler = (data, ctx) => {
|
|
109
|
-
|
|
354
|
+
// The interpreter gives an error without an origin the path and the position of its node.
|
|
355
|
+
if (!isCellRef(data)) return fail(vexError("bad-expression", "a cell reference needs a column name and an address of fixed moves"));
|
|
110
356
|
const where = resolveAddr(ctx.space, ctx.position, data.at);
|
|
111
357
|
if (!where.ok) return fail({ ...where.error, path: ctx.path });
|
|
112
|
-
|
|
358
|
+
// The evaluation of a cell has the space of its table, and the body of the axis table has the space of that table.
|
|
359
|
+
/* v8 ignore next -- @preserve: each space of an evaluation of the run is the space of a table */
|
|
360
|
+
return this.#read(this.#bySpace.get(ctx.space) ?? this.#main, data.column, where.value);
|
|
113
361
|
};
|
|
114
362
|
|
|
115
363
|
/**
|
|
116
|
-
* This method gives the result of one cell: the column `column` at the key `key
|
|
117
|
-
* the column at the keys before `key`, in key order. Thus a recurrence
|
|
364
|
+
* This method gives the result of one cell: the column `column` at the key `key` of the main table, or of the table
|
|
365
|
+
* `table`. A read from a host first evaluates the column at the keys before `key`, in key order. Thus a recurrence
|
|
366
|
+
* over the keys reads finished cells.
|
|
118
367
|
*/
|
|
119
|
-
cell(column: string, key: string): Result<unknown> {
|
|
368
|
+
cell(column: string, key: string, table?: string): Result<unknown> {
|
|
369
|
+
const t = this.#named(table, key);
|
|
370
|
+
if (!("space" in t)) return t;
|
|
120
371
|
// The placeholder at the top has the index -1. A read inside a formula has a cell at the top, so it does not warm.
|
|
121
|
-
if (this.#top.index >= 0) return this.#read(column, key);
|
|
122
|
-
|
|
123
|
-
|
|
372
|
+
if (this.#top.index >= 0) return this.#read(t, column, key);
|
|
373
|
+
// A finished cell needs no warm-up.
|
|
374
|
+
const done = doneOf(t, column, key);
|
|
375
|
+
if (done !== undefined) return done;
|
|
376
|
+
this.#warmTo(t, column, key);
|
|
377
|
+
return this.#drive(t, column, key);
|
|
124
378
|
}
|
|
125
379
|
|
|
126
380
|
/** This method evaluates the column `column` at each key before `key` that it did not evaluate before, in key order. */
|
|
127
|
-
#warmTo(column: string, key: string): void {
|
|
128
|
-
if (!
|
|
129
|
-
const keys =
|
|
130
|
-
|
|
131
|
-
const end =
|
|
132
|
-
const start =
|
|
133
|
-
for (let i = start; i < end; i++) this.#drive(column, keys[i] ?? key);
|
|
134
|
-
if (end > start)
|
|
381
|
+
#warmTo(t: Table, column: string, key: string): void {
|
|
382
|
+
if (!t.columns.has(column)) return;
|
|
383
|
+
const keys = t.space.keys;
|
|
384
|
+
t.index ??= new Map(keys.map((k, i) => [k, i]));
|
|
385
|
+
const end = t.index.get(key) ?? 0;
|
|
386
|
+
const start = t.warmed.get(column) ?? 0;
|
|
387
|
+
for (let i = start; i < end; i++) this.#drive(t, column, keys[i] ?? key);
|
|
388
|
+
if (end > start) t.warmed.set(column, end);
|
|
135
389
|
}
|
|
136
390
|
|
|
137
|
-
/** This method gives the estimated frames of the stack that one evaluation of
|
|
138
|
-
#costOf(
|
|
139
|
-
let cost = this.#cost.get(
|
|
391
|
+
/** This method gives the estimated frames of the stack that one evaluation of the program `program` uses. */
|
|
392
|
+
#costOf(program: Expr): number {
|
|
393
|
+
let cost = this.#cost.get(program);
|
|
140
394
|
if (cost === undefined) {
|
|
141
|
-
cost = 3 * nestingOf(program) + 16;
|
|
142
|
-
this.#cost.set(
|
|
395
|
+
cost = 3 * (shapeError(program) === undefined ? nestingOf(program) : 1) + 16;
|
|
396
|
+
this.#cost.set(program, cost);
|
|
143
397
|
}
|
|
144
398
|
return cost;
|
|
145
399
|
}
|
|
146
400
|
|
|
147
401
|
/** This method puts a new cell on the stack of the search. */
|
|
148
|
-
#start(column: string, key: string): Visit {
|
|
402
|
+
#start(t: Table, column: string, key: string): Visit {
|
|
149
403
|
const n = this.#next++;
|
|
150
|
-
const visit: Visit = { id: idOf(column, key), column, key, index: n, low: n, reread: false, result: cycleError(column, key), paused: false, need: undefined };
|
|
404
|
+
const visit: Visit = { id: idOf(t, column, key), table: t, column, key, index: n, low: n, reread: false, result: cycleError(column, key), paused: false, need: undefined };
|
|
151
405
|
this.#visits.set(visit.id, visit);
|
|
152
406
|
this.#stack.push(visit);
|
|
153
407
|
return visit;
|
|
@@ -156,17 +410,45 @@ export class SheetRun {
|
|
|
156
410
|
/** This method evaluates the formula of a cell once. When the evaluation pauses, the result of the cell does not change. */
|
|
157
411
|
#evaluate(visit: Visit, program: Expr): void {
|
|
158
412
|
const caller = this.#top;
|
|
159
|
-
const cost = this.#costOf(
|
|
413
|
+
const cost = this.#costOf(program);
|
|
160
414
|
visit.paused = false;
|
|
161
415
|
visit.need = undefined;
|
|
162
416
|
this.#top = visit;
|
|
163
417
|
this.#used += cost;
|
|
164
|
-
const result =
|
|
418
|
+
const result = this.#run(visit.table, visit.column, visit.key, program);
|
|
165
419
|
this.#used -= cost;
|
|
166
420
|
this.#top = caller;
|
|
167
421
|
if (!visit.paused) visit.result = result;
|
|
168
422
|
}
|
|
169
423
|
|
|
424
|
+
/** This method evaluates the formula `program` of a cell, with its compiled program or with the interpreter. */
|
|
425
|
+
#run(t: Table, column: string, key: string, program: Expr): Result<unknown> {
|
|
426
|
+
const options = { ...this.#opts, space: t.space, origin: key };
|
|
427
|
+
const compiled = this.#compiled(t, column, program);
|
|
428
|
+
if (compiled === undefined) return evaluate(program, options);
|
|
429
|
+
const active = SheetRun.#active;
|
|
430
|
+
SheetRun.#active = this;
|
|
431
|
+
const result = compiled.run(options);
|
|
432
|
+
SheetRun.#active = active;
|
|
433
|
+
return result;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* This method gives the compiled program of the formula of a column, or `undefined` for the interpreter. A formula
|
|
438
|
+
* compiles once for each compiler and options, also across runs.
|
|
439
|
+
*/
|
|
440
|
+
#compiled(t: Table, column: string, program: Expr): Program | undefined {
|
|
441
|
+
const compile = this.#compile;
|
|
442
|
+
// The formula of a cell evaluates once in a run, so the run compiles only the formulas of the columns.
|
|
443
|
+
if (compile === undefined || t.columns.get(column) !== program) return undefined;
|
|
444
|
+
const { domains, fns, extensions } = this.#compileOptions;
|
|
445
|
+
const hit = PROGRAMS.get(program);
|
|
446
|
+
if (hit !== undefined && hit.compile === compile && hit.domains === domains && hit.fns === fns && hit.extensions === extensions) return hit.program;
|
|
447
|
+
const compiled = compile(program, this.#compileOptions);
|
|
448
|
+
PROGRAMS.set(program, { compile, domains, fns, extensions, program: compiled });
|
|
449
|
+
return compiled;
|
|
450
|
+
}
|
|
451
|
+
|
|
170
452
|
/** This method ends a cell whose evaluation did not pause: it finishes its component, or it gives its low to the caller. */
|
|
171
453
|
#end(visit: Visit, caller: Visit | undefined): void {
|
|
172
454
|
if (visit.low === visit.index) this.#finish(visit);
|
|
@@ -174,13 +456,18 @@ export class SheetRun {
|
|
|
174
456
|
}
|
|
175
457
|
|
|
176
458
|
/** This method reads a cell from the formula of the cell at the top. */
|
|
177
|
-
#read(column: string, key: string): Result<unknown> {
|
|
178
|
-
const
|
|
179
|
-
const done = this.#done.get(id);
|
|
459
|
+
#read(t: Table, column: string, key: string): Result<unknown> {
|
|
460
|
+
const done = doneOf(t, column, key);
|
|
180
461
|
if (done !== undefined) return done;
|
|
181
462
|
const caller = this.#top;
|
|
182
463
|
// A paused evaluation reads no other cell, so it does not follow a path that its final evaluation does not follow.
|
|
464
|
+
// In a fill, the cell goes to the search after the evaluation (SHEET.FILL).
|
|
183
465
|
if (caller.paused) return cycleError(column, key);
|
|
466
|
+
if (caller === this.#filling) {
|
|
467
|
+
this.#missed = true;
|
|
468
|
+
return cycleError(column, key);
|
|
469
|
+
}
|
|
470
|
+
const id = idOf(t, column, key);
|
|
184
471
|
const seen = this.#visits.get(id);
|
|
185
472
|
if (seen !== undefined) {
|
|
186
473
|
// A read of a cell that is not finished: the caller and this cell are on one cycle. The cell is on the
|
|
@@ -190,10 +477,10 @@ export class SheetRun {
|
|
|
190
477
|
seen.reread = true;
|
|
191
478
|
return cycleError(column, key);
|
|
192
479
|
}
|
|
193
|
-
const program = this.#
|
|
194
|
-
if (program === undefined) return
|
|
195
|
-
const visit = this.#start(column, key);
|
|
196
|
-
if (this.#used + this.#costOf(
|
|
480
|
+
const program = this.#programOf(t, column, key);
|
|
481
|
+
if (program === undefined) return this.#unbound(t, column, key);
|
|
482
|
+
const visit = this.#start(t, column, key);
|
|
483
|
+
if (this.#used + this.#costOf(program) > this.#budget) {
|
|
197
484
|
// Over the budget: the caller pauses, and the run evaluates the new cell from its own list.
|
|
198
485
|
caller.paused = true;
|
|
199
486
|
caller.need = visit;
|
|
@@ -206,19 +493,18 @@ export class SheetRun {
|
|
|
206
493
|
return cycleError(column, key);
|
|
207
494
|
}
|
|
208
495
|
this.#end(visit, caller);
|
|
209
|
-
return
|
|
496
|
+
return doneOf(t, column, key) ?? cycleError(column, key);
|
|
210
497
|
}
|
|
211
498
|
|
|
212
499
|
/** This method evaluates a cell from the top level. The cells that pause go on a list, not on the call stack. */
|
|
213
|
-
#drive(column: string, key: string): Result<unknown> {
|
|
214
|
-
const
|
|
215
|
-
const done = this.#done.get(id);
|
|
500
|
+
#drive(t: Table, column: string, key: string): Result<unknown> {
|
|
501
|
+
const done = doneOf(t, column, key);
|
|
216
502
|
if (done !== undefined) return done;
|
|
217
|
-
if (
|
|
218
|
-
const work: Visit[] = [this.#start(column, key)];
|
|
503
|
+
if (this.#programOf(t, column, key) === undefined) return this.#unbound(t, column, key);
|
|
504
|
+
const work: Visit[] = [this.#start(t, column, key)];
|
|
219
505
|
for (let visit = work.at(-1); visit !== undefined; visit = work.at(-1)) {
|
|
220
|
-
const program = this.#
|
|
221
|
-
/* v8 ignore next -- @preserve: a visit exists only for a
|
|
506
|
+
const program = this.#programOf(visit.table, visit.column, visit.key);
|
|
507
|
+
/* v8 ignore next -- @preserve: a visit exists only for a cell with a program */
|
|
222
508
|
if (program === undefined) break;
|
|
223
509
|
this.#evaluate(visit, program);
|
|
224
510
|
if (visit.paused) {
|
|
@@ -229,7 +515,7 @@ export class SheetRun {
|
|
|
229
515
|
work.pop();
|
|
230
516
|
this.#end(visit, work.at(-1));
|
|
231
517
|
}
|
|
232
|
-
return
|
|
518
|
+
return doneOf(t, column, key) ?? cycleError(column, key);
|
|
233
519
|
}
|
|
234
520
|
|
|
235
521
|
/** This method takes the component of `root` from the stack, and sets the final result of each of its cells. */
|
|
@@ -240,25 +526,53 @@ export class SheetRun {
|
|
|
240
526
|
// Stryker disable next-line ConditionalExpression,LogicalOperator: members.length > 1 implies root.reread
|
|
241
527
|
const cyclic = members.length > 1 || root.reread;
|
|
242
528
|
for (const m of members) {
|
|
243
|
-
// Stryker disable next-line CallExpression: a finished cell
|
|
529
|
+
// Stryker disable next-line CallExpression: a finished cell has a result, and a read looks for the result first
|
|
244
530
|
this.#visits.delete(m.id);
|
|
245
|
-
|
|
531
|
+
setDone(m.table, m.column, m.key, cyclic ? cycleError(m.column, m.key) : m.result);
|
|
246
532
|
}
|
|
247
533
|
}
|
|
248
534
|
|
|
249
|
-
/** This method evaluates one cell with a trace of the nodes of its
|
|
250
|
-
explain(column: string, key: string): Trace {
|
|
251
|
-
const result = this.cell(column, key);
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
535
|
+
/** This method evaluates one cell with a trace of the nodes of its program. The result is the result of the cell. */
|
|
536
|
+
explain(column: string, key: string, table?: string): Trace {
|
|
537
|
+
const result = this.cell(column, key, table);
|
|
538
|
+
const t = this.#named(table, key);
|
|
539
|
+
const program = "space" in t ? this.#programOf(t, column, key) : undefined;
|
|
540
|
+
if (program === undefined || !("space" in t)) return { events: [], result };
|
|
541
|
+
return { events: explain(program, { ...this.#opts, space: t.space, origin: key }).events, result };
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* This method evaluates each cell of the table `table`, or of the main table (SHEET.FILL). Each column comes after
|
|
546
|
+
* the columns that it reads, and the keys of a column come in the order of its reads from itself. Thus most reads
|
|
547
|
+
* find a finished cell. An evaluation that reads a cell that is not finished has no effect, and the search then
|
|
548
|
+
* evaluates its cell. A read of a cell gives the same result after a fill.
|
|
549
|
+
*/
|
|
550
|
+
fill(table?: string): void {
|
|
551
|
+
const t = table === undefined ? this.#main : this.#byName.get(table);
|
|
552
|
+
// A read inside a formula has a cell at the top. A fill there has no effect.
|
|
553
|
+
if (t === undefined || this.#top.index >= 0) return;
|
|
554
|
+
const host = this.#top;
|
|
555
|
+
for (const column of columnOrder(t)) {
|
|
556
|
+
for (const key of keyOrder(t.space, readsOf(t, column).directions)) {
|
|
557
|
+
const program = this.#programOf(t, column, key);
|
|
558
|
+
if (program === undefined || doneOf(t, column, key) !== undefined) continue;
|
|
559
|
+
this.#top = this.#filling;
|
|
560
|
+
this.#missed = false;
|
|
561
|
+
const result = this.#run(t, column, key, program);
|
|
562
|
+
this.#top = host;
|
|
563
|
+
if (this.#missed) this.#drive(t, column, key);
|
|
564
|
+
else setDone(t, column, key, result);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
255
567
|
}
|
|
256
568
|
|
|
257
569
|
/**
|
|
258
570
|
* This method evaluates the column `column` at each key before `key`, in key order. A recurrence over the keys,
|
|
259
571
|
* for example a running total, then reads finished cells.
|
|
260
572
|
*/
|
|
261
|
-
warm(column: string, key: string): void {
|
|
262
|
-
this.#
|
|
573
|
+
warm(column: string, key: string, table?: string): void {
|
|
574
|
+
const t = this.#named(table, key);
|
|
575
|
+
// Stryker disable next-line all: a warm-up changes no result, only the depth of the recursion (SHEET.ORDER)
|
|
576
|
+
if ("space" in t) this.#warmTo(t, column, key);
|
|
263
577
|
}
|
|
264
578
|
}
|