@mark1russell7/vex 0.4.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 +125 -18
- package/build/index.d.ts.map +1 -1
- package/build/index.js +570 -130
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/builder.ts +56 -3
- package/src/deps.ts +11 -12
- package/src/errors.ts +20 -2
- package/src/evaluate.ts +66 -13
- package/src/ir.ts +93 -42
- package/src/json.ts +11 -4
- package/src/sheet.ts +240 -64
- package/src/space.ts +131 -1
package/src/sheet.ts
CHANGED
|
@@ -5,12 +5,15 @@
|
|
|
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, type EvalOptions, type ExtHandler } from "./evaluate.ts";
|
|
13
|
+
import { evaluate, explain, nestingOf, type EvalOptions, type ExtHandler } from "./evaluate.ts";
|
|
11
14
|
import { ext, isMove, type Addr, type Expr } 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. */
|
|
@@ -32,89 +35,232 @@ const isCellRef = (u: unknown): u is CellRef =>
|
|
|
32
35
|
Array.isArray((u as { at?: unknown }).at) &&
|
|
33
36
|
(u as { at: readonly unknown[] }).at.every(isMove);
|
|
34
37
|
|
|
35
|
-
/** The
|
|
36
|
-
export type
|
|
38
|
+
/** The formulas of single cells: for each column, the formula of each key that has its own formula. */
|
|
39
|
+
export type CellFormulas = ReadonlyMap<string, ReadonlyMap<string, Expr>>;
|
|
40
|
+
|
|
41
|
+
/** A table of a run: its space, the formulas of its columns, and the formulas of its cells. */
|
|
42
|
+
export interface SheetTable {
|
|
43
|
+
readonly space: Space;
|
|
44
|
+
readonly columns: ReadonlyMap<string, Expr>;
|
|
45
|
+
readonly cells?: CellFormulas;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The options of a sheet run: the evaluation options without the origin and the trace, and the tables. */
|
|
49
|
+
export type SheetOptions = Omit<EvalOptions, "origin" | "trace" | "tables"> & {
|
|
50
|
+
/** The cells with their own formula. The formula of a cell replaces the formula of its column at its key. */
|
|
51
|
+
readonly cells?: CellFormulas;
|
|
52
|
+
/** The name of this table, so the axis `table(name)` in a formula of another table can read it. */
|
|
53
|
+
readonly name?: string;
|
|
54
|
+
/** The other tables, by name (SHEET.TABLES). A cell reference reads the table of the space where it evaluates. */
|
|
55
|
+
readonly tables?: ReadonlyMap<string, SheetTable>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/** The state of one table in a run. */
|
|
59
|
+
interface Table {
|
|
60
|
+
/** The words for the table in a message: "the sheet" for the main table, or the table and its name. */
|
|
61
|
+
readonly where: string;
|
|
62
|
+
/** A short text for the ids of the cells of the table. */
|
|
63
|
+
readonly tag: string;
|
|
64
|
+
readonly space: Space;
|
|
65
|
+
readonly columns: ReadonlyMap<string, Expr>;
|
|
66
|
+
readonly cells: CellFormulas;
|
|
67
|
+
// For each column, the number of keys at the start of the key order that a warm-up evaluated.
|
|
68
|
+
readonly warmed: Map<string, number>;
|
|
69
|
+
// The position of each key in the key order, made at the first warm-up.
|
|
70
|
+
index: ReadonlyMap<string, number> | undefined;
|
|
71
|
+
}
|
|
37
72
|
|
|
38
73
|
const cycleError = (column: string, key: string): Result<never> =>
|
|
39
74
|
fail(vexError("cycle", `the cell "${column}" at "${key}" is on a cycle of cell references`, { origin: key, focus: key }));
|
|
40
75
|
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
* gives `#CYCLE!`. A read of a cell that is not finished gives `#CYCLE!` too. Thus the order of the evaluations
|
|
48
|
-
* does not change a result, also when `ifError` catches the error of a cycle.
|
|
49
|
-
*/
|
|
76
|
+
/** The id of a cell. */
|
|
77
|
+
const idOf = (t: Table, column: string, key: string): string => `${t.tag}\u0001${column}\u0000${key}`;
|
|
78
|
+
|
|
79
|
+
/** This function gives the state of a table at the start of a run. */
|
|
80
|
+
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 });
|
|
81
|
+
|
|
50
82
|
/** The state of one cell in the search. */
|
|
51
83
|
interface Visit {
|
|
52
84
|
readonly id: string;
|
|
85
|
+
readonly table: Table;
|
|
53
86
|
readonly column: string;
|
|
54
87
|
readonly key: string;
|
|
55
88
|
readonly index: number;
|
|
56
89
|
low: number;
|
|
57
90
|
reread: boolean;
|
|
58
91
|
result: Result<unknown>;
|
|
92
|
+
/** True when the last evaluation paused, because a cell that it reads is not finished. */
|
|
93
|
+
paused: boolean;
|
|
94
|
+
/** The cell that the evaluation waits for: a cell that the run started, or a cell that paused too. */
|
|
95
|
+
need: Visit | undefined;
|
|
59
96
|
}
|
|
60
97
|
|
|
98
|
+
/**
|
|
99
|
+
* The budget of the stack for the recursion of a run, in estimated frames. Each engine has more stack than this, so
|
|
100
|
+
* the recursion of a run cannot fill the stack (SHEET.DEEP).
|
|
101
|
+
*/
|
|
102
|
+
const STACK_BUDGET = 3000;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* One run of a sheet. It keeps the result of each cell that it evaluated. The space does not change, so a cell has
|
|
106
|
+
* one result in a run.
|
|
107
|
+
*
|
|
108
|
+
* The run is a depth-first search with the algorithm of Tarjan for strongly connected components. A cell finishes
|
|
109
|
+
* when its component finishes. Each cell of a component with two or more cells, or with a reference to itself,
|
|
110
|
+
* gives `#CYCLE!`. A read of a cell that is not finished gives `#CYCLE!` too. Thus the order of the evaluations
|
|
111
|
+
* does not change a result, also when `ifError` catches the error of a cycle.
|
|
112
|
+
*
|
|
113
|
+
* The search does not depend on the call stack of the engine (SHEET.DEEP). A read of a cell evaluates that cell
|
|
114
|
+
* with recursion while the stack of the run stays in a budget. When a read is over the budget, the run starts the
|
|
115
|
+
* cell, and the cell that reads it pauses. A paused evaluation reads no other cell and changes no state of the
|
|
116
|
+
* search. The run then evaluates the started cell from its own list, and it evaluates the paused cell again after
|
|
117
|
+
* that cell. A formula is pure (EVAL.PURE), so the second evaluation gives the result of one evaluation.
|
|
118
|
+
*
|
|
119
|
+
* A run can have more tables (SHEET.TABLES). Each table has its own space, and one search covers the cells of all
|
|
120
|
+
* the tables. Thus a cycle across tables gives `#CYCLE!` too.
|
|
121
|
+
*/
|
|
61
122
|
export class SheetRun {
|
|
62
|
-
readonly #
|
|
63
|
-
|
|
123
|
+
readonly #main: Table;
|
|
124
|
+
// The tables by name and by space. A cell reference reads the table of the space where it evaluates.
|
|
125
|
+
readonly #byName = new Map<string, Table>();
|
|
126
|
+
readonly #bySpace = new Map<Space, Table>();
|
|
127
|
+
readonly #opts: Omit<EvalOptions, "origin" | "trace" | "space">;
|
|
64
128
|
readonly #done = new Map<string, Result<unknown>>();
|
|
65
|
-
// For each column, the number of keys at the start of the key order that a warm-up evaluated.
|
|
66
|
-
readonly #warmed = new Map<string, number>();
|
|
67
|
-
// The position of each key in the key order, made at the first warm-up.
|
|
68
|
-
#index: ReadonlyMap<string, number> | undefined;
|
|
69
129
|
// The cells on the stack of the search: started, but their component is not finished.
|
|
70
130
|
readonly #visits = new Map<string, Visit>();
|
|
71
131
|
// Stryker disable next-line ArrayDeclaration: the search never reads below the root of a component
|
|
72
132
|
readonly #stack: Visit[] = [];
|
|
73
133
|
// The cell whose formula is in evaluation. At the top level, it is a placeholder that is not a cell.
|
|
74
|
-
|
|
75
|
-
#top: Visit = { id: "", column: "", key: "", index: -1, low: -1, reread: false, result: cycleError("", "") };
|
|
134
|
+
#top: Visit;
|
|
76
135
|
#next = 0;
|
|
136
|
+
// The estimated frames of the stack that the evaluations in progress use, and the cost of each program.
|
|
137
|
+
#used = 0;
|
|
138
|
+
readonly #cost = new Map<Expr, number>();
|
|
139
|
+
readonly #budget: number;
|
|
140
|
+
|
|
141
|
+
/** `budget` is the budget of the stack in estimated frames. A test can make it small, so the run pauses often. */
|
|
142
|
+
constructor(columns: ReadonlyMap<string, Expr>, opts: SheetOptions, budget: number = STACK_BUDGET) {
|
|
143
|
+
const { cells, name, tables, space, ...rest } = opts;
|
|
144
|
+
this.#main = { where: "the sheet", tag: "0", space, columns, cells: cells ?? new Map(), warmed: new Map(), index: undefined };
|
|
145
|
+
this.#bySpace.set(space, this.#main);
|
|
146
|
+
if (name !== undefined) this.#byName.set(name, this.#main);
|
|
147
|
+
if (tables !== undefined) {
|
|
148
|
+
let i = 0;
|
|
149
|
+
for (const [n, t] of tables) {
|
|
150
|
+
if (this.#byName.has(n)) continue;
|
|
151
|
+
const other = tableState(`the table "${n}"`, ++i, t);
|
|
152
|
+
this.#byName.set(n, other);
|
|
153
|
+
// A table with the space of an earlier table does not take its cells: the first table keeps the space.
|
|
154
|
+
if (!this.#bySpace.has(t.space)) this.#bySpace.set(t.space, other);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// Stryker disable next-line all: the placeholder is not a cell, so the search reads none of its fields
|
|
158
|
+
this.#top = { id: "", table: this.#main, column: "", key: "", index: -1, low: -1, reread: false, result: cycleError("", ""), paused: false, need: undefined };
|
|
159
|
+
this.#budget = budget;
|
|
160
|
+
const spaces = Object.fromEntries([...this.#byName].map(([n, t]) => [n, t.space]));
|
|
161
|
+
this.#opts = { ...rest, tables: spaces, extensions: { ...rest.extensions, [CELL_KIND]: this.#handler } };
|
|
162
|
+
}
|
|
77
163
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
164
|
+
/** This method gives the program of a cell: its own formula, or the formula of its column (SHEET.CELL.FORMULA). */
|
|
165
|
+
#programOf(t: Table, column: string, key: string): Expr | undefined {
|
|
166
|
+
// Stryker disable next-line ConditionalExpression: without formulas of cells, the lookup gives undefined anyway
|
|
167
|
+
const own = t.cells.size === 0 ? undefined : t.cells.get(column)?.get(key);
|
|
168
|
+
return own ?? t.columns.get(column);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** This method gives the error of a cell without a program. */
|
|
172
|
+
#unbound(t: Table, column: string, key: string): Result<never> {
|
|
173
|
+
const message = t.cells.has(column) ? `the cell "${column}" at "${key}" has no formula` : `${t.where} has no column "${column}"`;
|
|
174
|
+
return fail(vexError("unbound", message, { origin: key, focus: key }));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** This method gives the table of a name for a read from a host. Without a name, it is the main table. */
|
|
178
|
+
#named(table: string | undefined, key: string): Table | Result<never> {
|
|
179
|
+
if (table === undefined) return this.#main;
|
|
180
|
+
return this.#byName.get(table) ?? fail(vexError("no-table", `the run has no table "${table}"`, { origin: key, focus: key }));
|
|
81
181
|
}
|
|
82
182
|
|
|
83
183
|
readonly #handler: ExtHandler = (data, ctx) => {
|
|
84
184
|
if (!isCellRef(data)) return fail(vexError("bad-expression", "a cell reference needs a column name and an address", { path: ctx.path }));
|
|
85
185
|
const where = resolveAddr(ctx.space, ctx.position, data.at);
|
|
86
186
|
if (!where.ok) return fail({ ...where.error, path: ctx.path });
|
|
87
|
-
|
|
187
|
+
// The evaluation of a cell has the space of its table, and the body of the axis table has the space of that table.
|
|
188
|
+
/* v8 ignore next -- @preserve: each space of an evaluation of the run is the space of a table */
|
|
189
|
+
return this.#read(this.#bySpace.get(ctx.space) ?? this.#main, data.column, where.value);
|
|
88
190
|
};
|
|
89
191
|
|
|
90
192
|
/**
|
|
91
|
-
* This method gives the result of one cell: the column `column` at the key `key
|
|
92
|
-
* the column at the keys before `key`, in key order. Thus a recurrence
|
|
93
|
-
*
|
|
193
|
+
* This method gives the result of one cell: the column `column` at the key `key` of the main table, or of the table
|
|
194
|
+
* `table`. A read from a host first evaluates the column at the keys before `key`, in key order. Thus a recurrence
|
|
195
|
+
* over the keys reads finished cells.
|
|
94
196
|
*/
|
|
95
|
-
cell(column: string, key: string): Result<unknown> {
|
|
197
|
+
cell(column: string, key: string, table?: string): Result<unknown> {
|
|
198
|
+
const t = this.#named(table, key);
|
|
199
|
+
if (!("space" in t)) return t;
|
|
96
200
|
// 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.
|
|
97
|
-
if (this.#top.index
|
|
98
|
-
|
|
201
|
+
if (this.#top.index >= 0) return this.#read(t, column, key);
|
|
202
|
+
this.#warmTo(t, column, key);
|
|
203
|
+
return this.#drive(t, column, key);
|
|
99
204
|
}
|
|
100
205
|
|
|
101
206
|
/** This method evaluates the column `column` at each key before `key` that it did not evaluate before, in key order. */
|
|
102
|
-
#warmTo(column: string, key: string): void {
|
|
103
|
-
if (!
|
|
104
|
-
const keys =
|
|
105
|
-
|
|
106
|
-
const end =
|
|
107
|
-
const start =
|
|
108
|
-
for (let i = start; i < end; i++) this.#
|
|
109
|
-
if (end > start)
|
|
207
|
+
#warmTo(t: Table, column: string, key: string): void {
|
|
208
|
+
if (!t.columns.has(column)) return;
|
|
209
|
+
const keys = t.space.keys;
|
|
210
|
+
t.index ??= new Map(keys.map((k, i) => [k, i]));
|
|
211
|
+
const end = t.index.get(key) ?? 0;
|
|
212
|
+
const start = t.warmed.get(column) ?? 0;
|
|
213
|
+
for (let i = start; i < end; i++) this.#drive(t, column, keys[i] ?? key);
|
|
214
|
+
if (end > start) t.warmed.set(column, end);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** This method gives the estimated frames of the stack that one evaluation of the program `program` uses. */
|
|
218
|
+
#costOf(program: Expr): number {
|
|
219
|
+
let cost = this.#cost.get(program);
|
|
220
|
+
if (cost === undefined) {
|
|
221
|
+
cost = 3 * nestingOf(program) + 16;
|
|
222
|
+
this.#cost.set(program, cost);
|
|
223
|
+
}
|
|
224
|
+
return cost;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** This method puts a new cell on the stack of the search. */
|
|
228
|
+
#start(t: Table, column: string, key: string): Visit {
|
|
229
|
+
const n = this.#next++;
|
|
230
|
+
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 };
|
|
231
|
+
this.#visits.set(visit.id, visit);
|
|
232
|
+
this.#stack.push(visit);
|
|
233
|
+
return visit;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** This method evaluates the formula of a cell once. When the evaluation pauses, the result of the cell does not change. */
|
|
237
|
+
#evaluate(visit: Visit, program: Expr): void {
|
|
238
|
+
const caller = this.#top;
|
|
239
|
+
const cost = this.#costOf(program);
|
|
240
|
+
visit.paused = false;
|
|
241
|
+
visit.need = undefined;
|
|
242
|
+
this.#top = visit;
|
|
243
|
+
this.#used += cost;
|
|
244
|
+
const result = evaluate(program, { ...this.#opts, space: visit.table.space, origin: visit.key });
|
|
245
|
+
this.#used -= cost;
|
|
246
|
+
this.#top = caller;
|
|
247
|
+
if (!visit.paused) visit.result = result;
|
|
110
248
|
}
|
|
111
249
|
|
|
112
|
-
/** This method
|
|
113
|
-
#
|
|
114
|
-
|
|
250
|
+
/** This method ends a cell whose evaluation did not pause: it finishes its component, or it gives its low to the caller. */
|
|
251
|
+
#end(visit: Visit, caller: Visit | undefined): void {
|
|
252
|
+
if (visit.low === visit.index) this.#finish(visit);
|
|
253
|
+
else if (caller !== undefined) caller.low = Math.min(caller.low, visit.low);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** This method reads a cell from the formula of the cell at the top. */
|
|
257
|
+
#read(t: Table, column: string, key: string): Result<unknown> {
|
|
258
|
+
const id = idOf(t, column, key);
|
|
115
259
|
const done = this.#done.get(id);
|
|
116
260
|
if (done !== undefined) return done;
|
|
117
261
|
const caller = this.#top;
|
|
262
|
+
// A paused evaluation reads no other cell, so it does not follow a path that its final evaluation does not follow.
|
|
263
|
+
if (caller.paused) return cycleError(column, key);
|
|
118
264
|
const seen = this.#visits.get(id);
|
|
119
265
|
if (seen !== undefined) {
|
|
120
266
|
// A read of a cell that is not finished: the caller and this cell are on one cycle. The cell is on the
|
|
@@ -124,25 +270,52 @@ export class SheetRun {
|
|
|
124
270
|
seen.reread = true;
|
|
125
271
|
return cycleError(column, key);
|
|
126
272
|
}
|
|
127
|
-
const program = this.#
|
|
128
|
-
if (program === undefined) return
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
this.#
|
|
273
|
+
const program = this.#programOf(t, column, key);
|
|
274
|
+
if (program === undefined) return this.#unbound(t, column, key);
|
|
275
|
+
const visit = this.#start(t, column, key);
|
|
276
|
+
if (this.#used + this.#costOf(program) > this.#budget) {
|
|
277
|
+
// Over the budget: the caller pauses, and the run evaluates the new cell from its own list.
|
|
278
|
+
caller.paused = true;
|
|
279
|
+
caller.need = visit;
|
|
280
|
+
return cycleError(column, key);
|
|
281
|
+
}
|
|
282
|
+
this.#evaluate(visit, program);
|
|
283
|
+
if (visit.paused) {
|
|
284
|
+
caller.paused = true;
|
|
285
|
+
caller.need = visit;
|
|
286
|
+
return cycleError(column, key);
|
|
287
|
+
}
|
|
288
|
+
this.#end(visit, caller);
|
|
289
|
+
return this.#done.get(id) ?? cycleError(column, key);
|
|
290
|
+
}
|
|
137
291
|
|
|
138
|
-
|
|
139
|
-
|
|
292
|
+
/** This method evaluates a cell from the top level. The cells that pause go on a list, not on the call stack. */
|
|
293
|
+
#drive(t: Table, column: string, key: string): Result<unknown> {
|
|
294
|
+
const id = idOf(t, column, key);
|
|
295
|
+
const done = this.#done.get(id);
|
|
296
|
+
if (done !== undefined) return done;
|
|
297
|
+
if (this.#programOf(t, column, key) === undefined) return this.#unbound(t, column, key);
|
|
298
|
+
const work: Visit[] = [this.#start(t, column, key)];
|
|
299
|
+
for (let visit = work.at(-1); visit !== undefined; visit = work.at(-1)) {
|
|
300
|
+
const program = this.#programOf(visit.table, visit.column, visit.key);
|
|
301
|
+
/* v8 ignore next -- @preserve: a visit exists only for a cell with a program */
|
|
302
|
+
if (program === undefined) break;
|
|
303
|
+
this.#evaluate(visit, program);
|
|
304
|
+
if (visit.paused) {
|
|
305
|
+
// The cells that the evaluation waits for go on the list, the last one on the top.
|
|
306
|
+
for (let need = visit.need; need !== undefined; need = need.need) work.push(need);
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
work.pop();
|
|
310
|
+
this.#end(visit, work.at(-1));
|
|
311
|
+
}
|
|
140
312
|
return this.#done.get(id) ?? cycleError(column, key);
|
|
141
313
|
}
|
|
142
314
|
|
|
143
315
|
/** This method takes the component of `root` from the stack, and sets the final result of each of its cells. */
|
|
144
316
|
#finish(root: Visit): void {
|
|
145
|
-
|
|
317
|
+
// The root is near the top of the stack, so the search starts at the top: a deep chain needs no long scan.
|
|
318
|
+
const members = this.#stack.splice(this.#stack.lastIndexOf(root));
|
|
146
319
|
// A component of two or more cells reads its root again, so root.reread alone decides. The length test is a guard.
|
|
147
320
|
// Stryker disable next-line ConditionalExpression,LogicalOperator: members.length > 1 implies root.reread
|
|
148
321
|
const cyclic = members.length > 1 || root.reread;
|
|
@@ -153,19 +326,22 @@ export class SheetRun {
|
|
|
153
326
|
}
|
|
154
327
|
}
|
|
155
328
|
|
|
156
|
-
/** This method evaluates one cell with a trace of the nodes of its
|
|
157
|
-
explain(column: string, key: string): Trace {
|
|
158
|
-
const result = this.cell(column, key);
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
329
|
+
/** This method evaluates one cell with a trace of the nodes of its program. The result is the result of the cell. */
|
|
330
|
+
explain(column: string, key: string, table?: string): Trace {
|
|
331
|
+
const result = this.cell(column, key, table);
|
|
332
|
+
const t = this.#named(table, key);
|
|
333
|
+
const program = "space" in t ? this.#programOf(t, column, key) : undefined;
|
|
334
|
+
if (program === undefined || !("space" in t)) return { events: [], result };
|
|
335
|
+
return { events: explain(program, { ...this.#opts, space: t.space, origin: key }).events, result };
|
|
162
336
|
}
|
|
163
337
|
|
|
164
338
|
/**
|
|
165
339
|
* This method evaluates the column `column` at each key before `key`, in key order. A recurrence over the keys,
|
|
166
|
-
* for example a running total, then reads finished cells
|
|
340
|
+
* for example a running total, then reads finished cells.
|
|
167
341
|
*/
|
|
168
|
-
warm(column: string, key: string): void {
|
|
169
|
-
this.#
|
|
342
|
+
warm(column: string, key: string, table?: string): void {
|
|
343
|
+
const t = this.#named(table, key);
|
|
344
|
+
// Stryker disable next-line all: a warm-up changes no result, only the depth of the recursion (SHEET.ORDER)
|
|
345
|
+
if ("space" in t) this.#warmTo(t, column, key);
|
|
170
346
|
}
|
|
171
347
|
}
|
package/src/space.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { Addr, Axis, Move } from "./ir.ts";
|
|
|
3
3
|
import { fail, ok, type Result } from "./result.ts";
|
|
4
4
|
|
|
5
5
|
/** The kind of a space. The kind sets which moves and axes are valid. */
|
|
6
|
-
export type SpaceKind = "record" | "array" | "grid" | "tree";
|
|
6
|
+
export type SpaceKind = "record" | "array" | "grid" | "tree" | "graph";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* A space: an immutable collection of records with keys. A Vex program evaluates at one key of a space.
|
|
@@ -25,6 +25,10 @@ export interface Space<K extends string = string, O = unknown> {
|
|
|
25
25
|
parentOf?(k: string): K | undefined;
|
|
26
26
|
/** The children of the key `k` in a tree space, in key order. Other spaces do not have it. */
|
|
27
27
|
childrenOf?(k: string): readonly K[];
|
|
28
|
+
/** The keys at the ends of the edges from `k`, in a graph space or a tree space. Other spaces do not have it. */
|
|
29
|
+
successorsOf?(k: string): readonly K[];
|
|
30
|
+
/** The keys with an edge to `k`, in key order, in a graph space or a tree space. Other spaces do not have it. */
|
|
31
|
+
predecessorsOf?(k: string): readonly K[];
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
/** The key type of a grid space. */
|
|
@@ -169,6 +173,14 @@ class TreeSpace<K extends string, O> implements Space<K, O> {
|
|
|
169
173
|
keyAt(): K | undefined {
|
|
170
174
|
return undefined;
|
|
171
175
|
}
|
|
176
|
+
// In a tree, the edges go from a parent to its children.
|
|
177
|
+
successorsOf(k: string): readonly K[] {
|
|
178
|
+
return this.childrenOf(k);
|
|
179
|
+
}
|
|
180
|
+
predecessorsOf(k: string): readonly K[] {
|
|
181
|
+
const p = this.#parents.get(k);
|
|
182
|
+
return p === undefined ? [] : [p];
|
|
183
|
+
}
|
|
172
184
|
parentOf(k: string): K | undefined {
|
|
173
185
|
return this.#parents.get(k);
|
|
174
186
|
}
|
|
@@ -178,6 +190,63 @@ class TreeSpace<K extends string, O> implements Space<K, O> {
|
|
|
178
190
|
}
|
|
179
191
|
|
|
180
192
|
/** The constructors of spaces. */
|
|
193
|
+
/** The empty list of keys. */
|
|
194
|
+
const NO_KEYS: readonly never[] = Object.freeze([]);
|
|
195
|
+
|
|
196
|
+
class GraphSpace<K extends string, O> implements Space<K, O> {
|
|
197
|
+
readonly kind: SpaceKind = "graph";
|
|
198
|
+
readonly keys: readonly K[];
|
|
199
|
+
readonly #records: ReadonlyMap<string, O>;
|
|
200
|
+
readonly #out: ReadonlyMap<string, readonly K[]>;
|
|
201
|
+
readonly #in: ReadonlyMap<string, readonly K[]>;
|
|
202
|
+
|
|
203
|
+
constructor(records: Readonly<Record<K, O>>, edges: Readonly<Partial<Record<K, readonly K[]>>>) {
|
|
204
|
+
const entries = Object.entries(records) as [K, O][];
|
|
205
|
+
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
206
|
+
this.#records = new Map(entries);
|
|
207
|
+
const out = new Map<string, readonly K[]>();
|
|
208
|
+
for (const [k, to] of Object.entries(edges) as [K, readonly K[] | undefined][]) {
|
|
209
|
+
if (!this.#records.has(k)) throw new TypeError(`the graph has no key "${k}", but the edges name it`);
|
|
210
|
+
// A second edge to the same key is the same edge.
|
|
211
|
+
const ends = [...new Set(to ?? NO_KEYS)];
|
|
212
|
+
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`);
|
|
213
|
+
out.set(k, Object.freeze(ends));
|
|
214
|
+
}
|
|
215
|
+
this.#out = out;
|
|
216
|
+
// The predecessors in key order: the keys in key order, and the ends of the edges of each key.
|
|
217
|
+
const into = new Map<string, K[]>();
|
|
218
|
+
for (const k of this.keys) {
|
|
219
|
+
for (const t of out.get(k) ?? NO_KEYS) {
|
|
220
|
+
const from = into.get(t);
|
|
221
|
+
if (from === undefined) into.set(t, [k]);
|
|
222
|
+
else from.push(k);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
this.#in = new Map([...into].map(([k, from]) => [k, Object.freeze(from)]));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
has(k: string): k is K {
|
|
229
|
+
return this.#records.has(k);
|
|
230
|
+
}
|
|
231
|
+
get(k: string): O | undefined {
|
|
232
|
+
return this.#records.get(k);
|
|
233
|
+
}
|
|
234
|
+
// Stryker disable next-line BlockStatement: the body gives undefined, and an empty body gives undefined too
|
|
235
|
+
coords(): readonly number[] | undefined {
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
// Stryker disable next-line BlockStatement: the body gives undefined, and an empty body gives undefined too
|
|
239
|
+
keyAt(): K | undefined {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
successorsOf(k: string): readonly K[] {
|
|
243
|
+
return this.#out.get(k) ?? NO_KEYS;
|
|
244
|
+
}
|
|
245
|
+
predecessorsOf(k: string): readonly K[] {
|
|
246
|
+
return this.#in.get(k) ?? NO_KEYS;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
181
250
|
export const space = {
|
|
182
251
|
/** This function makes a space from the entries of an object. The keys are the property names. */
|
|
183
252
|
record<const R extends Readonly<Record<string, unknown>>>(records: R): Space<keyof R & string, R[keyof R]> {
|
|
@@ -198,6 +267,13 @@ export const space = {
|
|
|
198
267
|
tree<const R extends Readonly<Record<string, unknown>>>(records: R, parents: Readonly<Partial<Record<keyof R & string, (keyof R & string) | null>>>): Space<keyof R & string, R[keyof R]> {
|
|
199
268
|
return new TreeSpace<keyof R & string, R[keyof R]>(records, parents);
|
|
200
269
|
},
|
|
270
|
+
/**
|
|
271
|
+
* This function makes a graph space: the records by key, and the edges from each key, in order. An edge to a key
|
|
272
|
+
* that is not in the graph throws a `TypeError`. A graph can have cycles, and an edge can go from a key to itself.
|
|
273
|
+
*/
|
|
274
|
+
graph<const R extends Readonly<Record<string, unknown>>>(records: R, edges: Readonly<Partial<Record<keyof R & string, readonly (keyof R & string)[]>>>): Space<keyof R & string, R[keyof R]> {
|
|
275
|
+
return new GraphSpace<keyof R & string, R[keyof R]>(records, edges);
|
|
276
|
+
},
|
|
201
277
|
} as const;
|
|
202
278
|
|
|
203
279
|
/** The position of an evaluation: the key where it started, and the key where relative references read. */
|
|
@@ -335,5 +411,59 @@ export function axisTargets(s: Space, at: Position, a: Exclude<Axis, { readonly
|
|
|
335
411
|
case "descendants":
|
|
336
412
|
case "siblings":
|
|
337
413
|
return treeTargets(s, at, a.t);
|
|
414
|
+
case "window":
|
|
415
|
+
return windowTargets(s, at, a.from, a.to);
|
|
416
|
+
case "successors":
|
|
417
|
+
case "predecessors":
|
|
418
|
+
case "reachable":
|
|
419
|
+
return graphTargets(s, at, a.t);
|
|
420
|
+
case "table":
|
|
421
|
+
// The evaluator gives the axis table the space of its table. Here, without the tables, it has no targets.
|
|
422
|
+
return fail(vexError("no-table", `the axis "table" needs the tables of the evaluation, and "${a.name}" is not one of them`, at));
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* This function gives the targets of an axis of a graph. `reachable` has each key that a path of one or more edges
|
|
428
|
+
* reaches from the focus, once, in the order of a breadth-first search. The focus is not a target of `reachable`.
|
|
429
|
+
*/
|
|
430
|
+
function graphTargets(s: Space, at: Position, t: "successors" | "predecessors" | "reachable"): Result<readonly string[]> {
|
|
431
|
+
const successorsOf = s.successorsOf?.bind(s);
|
|
432
|
+
const predecessorsOf = s.predecessorsOf?.bind(s);
|
|
433
|
+
if (successorsOf === undefined || predecessorsOf === undefined) {
|
|
434
|
+
return fail(vexError("no-graph", `"${t}" needs a graph space or a tree space, but this space is a ${s.kind}`, at));
|
|
435
|
+
}
|
|
436
|
+
if (t === "successors") return ok(successorsOf(at.focus));
|
|
437
|
+
if (t === "predecessors") return ok(predecessorsOf(at.focus));
|
|
438
|
+
// A queue, not recursion, so a long path does not need a deep call stack. A loop over an array also reads the
|
|
439
|
+
// keys that the loop adds to it.
|
|
440
|
+
const seen = new Set<string>([at.focus]);
|
|
441
|
+
const queue: string[] = [at.focus];
|
|
442
|
+
for (const k of queue) {
|
|
443
|
+
for (const next of successorsOf(k)) {
|
|
444
|
+
if (seen.has(next)) continue;
|
|
445
|
+
seen.add(next);
|
|
446
|
+
queue.push(next);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return ok(queue.slice(1));
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* This function gives the targets of the axis `window(from, to)`: the keys of the moves `offset(d)` for each `d`
|
|
454
|
+
* from `from` to `to`, in order. An offset outside the space has no target. The axis needs an array space or a tree
|
|
455
|
+
* space, where an offset has one number.
|
|
456
|
+
*/
|
|
457
|
+
function windowTargets(s: Space, at: Position, from: number, to: number): Result<readonly string[]> {
|
|
458
|
+
const tree = s.parentOf !== undefined && s.childrenOf !== undefined;
|
|
459
|
+
if (!tree && s.coords(at.focus)?.length !== 1) {
|
|
460
|
+
return fail(vexError("no-offset", `"window" needs an array space or a tree space, but this space is a ${s.kind}`, at));
|
|
461
|
+
}
|
|
462
|
+
// An offset larger than the number of keys has no target, so the loop stays in the size of the space.
|
|
463
|
+
const out: string[] = [];
|
|
464
|
+
for (let d = Math.max(from, -s.keys.length); d <= Math.min(to, s.keys.length); d++) {
|
|
465
|
+
const r = applyMove(s, at, { t: "offset", d: [d] });
|
|
466
|
+
if (r.ok) out.push(r.value);
|
|
338
467
|
}
|
|
468
|
+
return ok(out);
|
|
339
469
|
}
|