@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/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!",
|
|
@@ -47,6 +49,7 @@ const CODE_OF = {
|
|
|
47
49
|
unbound: "#NAME?",
|
|
48
50
|
"not-finite": "#NUM!",
|
|
49
51
|
"invalid-value": "#NUM!",
|
|
52
|
+
limit: "#NUM!",
|
|
50
53
|
threw: "#CALC!",
|
|
51
54
|
"undefined-result": "#CALC!",
|
|
52
55
|
args: "#ARGS",
|
|
@@ -83,6 +86,8 @@ function vexError(kind, message, details = {}) {
|
|
|
83
86
|
const formatError = (e) => `${e.code} ${e.kind}: ${e.message}`;
|
|
84
87
|
//#endregion
|
|
85
88
|
//#region src/ir.ts
|
|
89
|
+
/** The largest number of steps of `iterate` (ITERATE.LIMIT). */
|
|
90
|
+
const MAX_ITERATIONS = 1e5;
|
|
86
91
|
/** This function makes a literal. */
|
|
87
92
|
const lit = (value) => ({
|
|
88
93
|
tag: "lit",
|
|
@@ -140,22 +145,73 @@ function each(axis, body, at) {
|
|
|
140
145
|
at
|
|
141
146
|
};
|
|
142
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* This function makes a fold: a step at each target of `axis`, in order. `init` gives the names and their first
|
|
150
|
+
* values, and `step` gives the next values. `result` gives the value from the last values (LOOP.NAMES, FOLD.ORDER).
|
|
151
|
+
*/
|
|
152
|
+
function fold(axis, init, step, result, at) {
|
|
153
|
+
return at === void 0 || at.length === 0 ? {
|
|
154
|
+
tag: "fold",
|
|
155
|
+
axis,
|
|
156
|
+
init,
|
|
157
|
+
step,
|
|
158
|
+
result
|
|
159
|
+
} : {
|
|
160
|
+
tag: "fold",
|
|
161
|
+
axis,
|
|
162
|
+
init,
|
|
163
|
+
step,
|
|
164
|
+
result,
|
|
165
|
+
at
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* This function makes an iteration: up to `limit` steps. `init` gives the names and their first values, and `step`
|
|
170
|
+
* gives the next values. A true `until` stops it, and `result` gives the value (ITERATE.LIMIT, ITERATE.UNTIL).
|
|
171
|
+
*/
|
|
172
|
+
function iterate(limit, init, step, result, until) {
|
|
173
|
+
return until === void 0 ? {
|
|
174
|
+
tag: "iterate",
|
|
175
|
+
limit,
|
|
176
|
+
init,
|
|
177
|
+
step,
|
|
178
|
+
result
|
|
179
|
+
} : {
|
|
180
|
+
tag: "iterate",
|
|
181
|
+
limit,
|
|
182
|
+
init,
|
|
183
|
+
step,
|
|
184
|
+
until,
|
|
185
|
+
result
|
|
186
|
+
};
|
|
187
|
+
}
|
|
143
188
|
/** This function makes an expression of an extension kind. */
|
|
144
189
|
const ext = (kind, data) => ({
|
|
145
190
|
tag: "ext",
|
|
146
191
|
kind,
|
|
147
192
|
data
|
|
148
193
|
});
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
194
|
+
function key(k) {
|
|
195
|
+
return {
|
|
196
|
+
t: "key",
|
|
197
|
+
key: k
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function index(i) {
|
|
201
|
+
return {
|
|
202
|
+
t: "index",
|
|
203
|
+
i
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
/** This function gives the expression of a move with a computed key or position, or `undefined`. */
|
|
207
|
+
function moveExpr(m) {
|
|
208
|
+
if (m.t === "key") return typeof m.key === "string" ? void 0 : m.key;
|
|
209
|
+
return m.t === "index" && typeof m.i !== "number" ? m.i : void 0;
|
|
210
|
+
}
|
|
211
|
+
/** This function gives the expressions of the computed moves of an address, in the order of the moves. */
|
|
212
|
+
const addrExprs = (at) => (at ?? []).flatMap((m) => moveExpr(m) ?? []);
|
|
213
|
+
/** This function tells if each move of an address is fixed. */
|
|
214
|
+
const isFixedAddr = (at) => addrExprs(at).length === 0;
|
|
159
215
|
/** The move to the other key of a space with two keys. */
|
|
160
216
|
const other = Object.freeze({ t: "other" });
|
|
161
217
|
/** The relative move in an array (one number) or a grid (two numbers). */
|
|
@@ -188,6 +244,29 @@ const axes = {
|
|
|
188
244
|
descendants: Object.freeze({ t: "descendants" }),
|
|
189
245
|
/** The other children of the parent of the focus. For a root, the other roots. */
|
|
190
246
|
siblings: Object.freeze({ t: "siblings" }),
|
|
247
|
+
/**
|
|
248
|
+
* 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
|
|
249
|
+
* a tree space, the offsets go among the siblings. An offset outside the space has no target.
|
|
250
|
+
*/
|
|
251
|
+
window: (from, to) => ({
|
|
252
|
+
t: "window",
|
|
253
|
+
from,
|
|
254
|
+
to
|
|
255
|
+
}),
|
|
256
|
+
/**
|
|
257
|
+
* Each key of the table `name` of the evaluation, in key order. The body evaluates in the space of that table, and
|
|
258
|
+
* there the origin is the target.
|
|
259
|
+
*/
|
|
260
|
+
table: (name) => ({
|
|
261
|
+
t: "table",
|
|
262
|
+
name
|
|
263
|
+
}),
|
|
264
|
+
/** The keys at the ends of the edges from the focus, in a graph space. In a tree space, the children. */
|
|
265
|
+
successors: Object.freeze({ t: "successors" }),
|
|
266
|
+
/** The keys with an edge to the focus, in key order. In a tree space, the parent. */
|
|
267
|
+
predecessors: Object.freeze({ t: "predecessors" }),
|
|
268
|
+
/** Each key that a path of edges reaches from the focus, in breadth-first order, without the focus. */
|
|
269
|
+
reachable: Object.freeze({ t: "reachable" }),
|
|
191
270
|
/** The targets of `axis` where `test` gives `true`. */
|
|
192
271
|
where: (axis, test) => ({
|
|
193
272
|
t: "where",
|
|
@@ -197,39 +276,102 @@ const axes = {
|
|
|
197
276
|
};
|
|
198
277
|
const isObject = (u) => typeof u === "object" && u !== null;
|
|
199
278
|
const isStringArray = (u) => Array.isArray(u) && u.every((s) => typeof s === "string");
|
|
200
|
-
/** This function tells if a value is a valid move. */
|
|
279
|
+
/** This function tells if a value is a valid move. The expression of a computed move must be valid too. */
|
|
201
280
|
function isMove(u) {
|
|
202
|
-
|
|
281
|
+
const exprs = moveParts(u);
|
|
282
|
+
return exprs !== void 0 && exprs.every(isExpr);
|
|
283
|
+
}
|
|
284
|
+
const anyNumber = (x) => typeof x === "number";
|
|
285
|
+
/** This function checks the fixed parts of a move. It gives the expressions of the move, or `undefined` for a bad move. */
|
|
286
|
+
function moveParts(u, isNumber = Number.isInteger) {
|
|
287
|
+
if (!isObject(u)) return void 0;
|
|
203
288
|
switch (u["t"]) {
|
|
204
|
-
case "key": return typeof u["key"] === "string";
|
|
205
|
-
case "index": return
|
|
206
|
-
case "offset": return Array.isArray(u["d"]) && u["d"].every((n) =>
|
|
289
|
+
case "key": return typeof u["key"] === "string" ? [] : isObject(u["key"]) ? [u["key"]] : void 0;
|
|
290
|
+
case "index": return isNumber(u["i"]) ? [] : isObject(u["i"]) ? [u["i"]] : void 0;
|
|
291
|
+
case "offset": return Array.isArray(u["d"]) && u["d"].every((n) => isNumber(n)) ? [] : void 0;
|
|
207
292
|
case "other":
|
|
208
293
|
case "origin":
|
|
209
|
-
case "parent": return
|
|
210
|
-
default: return
|
|
294
|
+
case "parent": return [];
|
|
295
|
+
default: return;
|
|
211
296
|
}
|
|
212
297
|
}
|
|
213
298
|
/** This function tells if a value is a valid axis. It checks the tests of `where` too. */
|
|
214
299
|
function isAxis(u) {
|
|
215
|
-
return
|
|
300
|
+
return badPart([{
|
|
216
301
|
axis: true,
|
|
217
302
|
u
|
|
218
|
-
}]);
|
|
303
|
+
}], true) === void 0;
|
|
219
304
|
}
|
|
220
305
|
/** This function tells if a value is a valid expression. It checks the whole tree. */
|
|
221
306
|
function isExpr(u) {
|
|
222
|
-
return
|
|
307
|
+
return badPart([{
|
|
223
308
|
axis: false,
|
|
224
309
|
u
|
|
225
|
-
}]);
|
|
310
|
+
}], true) === void 0;
|
|
311
|
+
}
|
|
312
|
+
/** The tags of the expressions of this version of Vex. */
|
|
313
|
+
const TAGS = /* @__PURE__ */ new Set([
|
|
314
|
+
"lit",
|
|
315
|
+
"ref",
|
|
316
|
+
"app",
|
|
317
|
+
"let",
|
|
318
|
+
"var",
|
|
319
|
+
"rec",
|
|
320
|
+
"each",
|
|
321
|
+
"fold",
|
|
322
|
+
"iterate",
|
|
323
|
+
"ext"
|
|
324
|
+
]);
|
|
325
|
+
/**
|
|
326
|
+
* This function gives the reason that a value is not a valid expression, or `undefined` for a valid expression
|
|
327
|
+
* (EVAL.SHAPE). An expression from a newer version of Vex can have a tag that this version does not know. Unlike
|
|
328
|
+
* `isExpr`, it accepts a number that is not whole in a move or in a window, because it has a meaning at run time.
|
|
329
|
+
*/
|
|
330
|
+
function shapeError(u) {
|
|
331
|
+
const bad = badPart([{
|
|
332
|
+
axis: false,
|
|
333
|
+
u
|
|
334
|
+
}], false);
|
|
335
|
+
if (bad === void 0) return void 0;
|
|
336
|
+
const tag = isObject(bad.u) ? bad.u["tag"] : void 0;
|
|
337
|
+
if (bad.axis) return "the expression has an axis that is not valid";
|
|
338
|
+
if (typeof tag === "string" && !TAGS.has(tag)) return `the expression has a node with the tag "${tag}", and this version of Vex does not know it`;
|
|
339
|
+
return typeof tag === "string" ? `the expression has a node "${tag}" with parts that are not valid` : "the expression has a part that is not a node";
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* This function checks the names of a loop: `init` and `step` are records, and each name of `step` is a name of `init`.
|
|
343
|
+
* It gives the expressions of `init`, of `step` and `result`, or `undefined` (LOOP.NAMES).
|
|
344
|
+
*/
|
|
345
|
+
function loopParts(u) {
|
|
346
|
+
const { init, step } = u;
|
|
347
|
+
if (!isObject(init) || Array.isArray(init) || !isObject(step) || Array.isArray(step)) return void 0;
|
|
348
|
+
if (!Object.keys(step).every((n) => Object.hasOwn(init, n))) return void 0;
|
|
349
|
+
return [
|
|
350
|
+
...Object.values(init),
|
|
351
|
+
...Object.values(step),
|
|
352
|
+
u["result"]
|
|
353
|
+
];
|
|
354
|
+
}
|
|
355
|
+
/** This function checks the fixed parts of an address. It gives the expressions of its moves, or `undefined`. */
|
|
356
|
+
function addrParts(u, isNumber) {
|
|
357
|
+
if (u === void 0) return [];
|
|
358
|
+
if (!Array.isArray(u)) return void 0;
|
|
359
|
+
const out = [];
|
|
360
|
+
for (const m of u) {
|
|
361
|
+
const parts = moveParts(m, isNumber);
|
|
362
|
+
if (parts === void 0) return void 0;
|
|
363
|
+
out.push(...parts);
|
|
364
|
+
}
|
|
365
|
+
return out;
|
|
226
366
|
}
|
|
227
|
-
const isAddr = (u) => u === void 0 || Array.isArray(u) && u.every(isMove);
|
|
228
367
|
/**
|
|
229
|
-
* This function checks a tree of expressions and axes
|
|
230
|
-
* example, a deep JSON file from a stranger does not need a deep call
|
|
368
|
+
* This function checks a tree of expressions and axes, and it gives the first part that is not valid. It uses a stack,
|
|
369
|
+
* not recursion, so the input can be deep. For example, a deep JSON file from a stranger does not need a deep call
|
|
370
|
+
* stack (EVAL.NESTING). With `whole`, the numbers of the moves and of the windows must be whole, as the JSON form needs.
|
|
231
371
|
*/
|
|
232
|
-
function
|
|
372
|
+
function badPart(todo, whole) {
|
|
373
|
+
const moveNumber = whole ? Number.isInteger : anyNumber;
|
|
374
|
+
const windowNumber = whole ? Number.isSafeInteger : anyNumber;
|
|
233
375
|
const push = (axis, values) => {
|
|
234
376
|
for (const u of values) todo.push({
|
|
235
377
|
axis,
|
|
@@ -238,11 +380,15 @@ function validTree(todo) {
|
|
|
238
380
|
};
|
|
239
381
|
for (let item = todo.pop(); item !== void 0; item = todo.pop()) {
|
|
240
382
|
const { u } = item;
|
|
241
|
-
if (!isObject(u)) return
|
|
383
|
+
if (!isObject(u)) return item;
|
|
242
384
|
if (item.axis) {
|
|
243
385
|
const t = u["t"];
|
|
244
386
|
if (t === "neighbors") {
|
|
245
|
-
if (u["n"] !== 4 && u["n"] !== 8) return
|
|
387
|
+
if (u["n"] !== 4 && u["n"] !== 8) return item;
|
|
388
|
+
} else if (t === "window") {
|
|
389
|
+
if (!windowNumber(u["from"]) || !windowNumber(u["to"])) return item;
|
|
390
|
+
} else if (t === "table") {
|
|
391
|
+
if (typeof u["name"] !== "string") return item;
|
|
246
392
|
} else if (t === "where") {
|
|
247
393
|
push(true, [u["axis"]]);
|
|
248
394
|
push(false, [u["test"]]);
|
|
@@ -253,54 +399,96 @@ function validTree(todo) {
|
|
|
253
399
|
"children",
|
|
254
400
|
"ancestors",
|
|
255
401
|
"descendants",
|
|
256
|
-
"siblings"
|
|
257
|
-
|
|
402
|
+
"siblings",
|
|
403
|
+
"successors",
|
|
404
|
+
"predecessors",
|
|
405
|
+
"reachable"
|
|
406
|
+
].includes(t)) return item;
|
|
258
407
|
continue;
|
|
259
408
|
}
|
|
260
409
|
switch (u["tag"]) {
|
|
261
410
|
case "lit":
|
|
262
|
-
if (!("value" in u)) return
|
|
411
|
+
if (!("value" in u)) return item;
|
|
263
412
|
break;
|
|
264
|
-
case "ref":
|
|
265
|
-
|
|
413
|
+
case "ref": {
|
|
414
|
+
const at = addrParts(u["at"], moveNumber);
|
|
415
|
+
if (!isStringArray(u["path"]) || at === void 0) return item;
|
|
416
|
+
push(false, at);
|
|
266
417
|
break;
|
|
418
|
+
}
|
|
267
419
|
case "app":
|
|
268
|
-
if (typeof u["op"] !== "string" || !Array.isArray(u["args"])) return
|
|
420
|
+
if (typeof u["op"] !== "string" || !Array.isArray(u["args"])) return item;
|
|
269
421
|
push(false, u["args"]);
|
|
270
422
|
break;
|
|
271
423
|
case "let":
|
|
272
|
-
if (!isObject(u["bind"]) || Array.isArray(u["bind"])) return
|
|
424
|
+
if (!isObject(u["bind"]) || Array.isArray(u["bind"])) return item;
|
|
273
425
|
push(false, [...Object.values(u["bind"]), u["body"]]);
|
|
274
426
|
break;
|
|
275
427
|
case "var":
|
|
276
|
-
if (typeof u["name"] !== "string") return
|
|
428
|
+
if (typeof u["name"] !== "string") return item;
|
|
277
429
|
break;
|
|
278
430
|
case "rec":
|
|
279
|
-
if (!isObject(u["fields"]) || Array.isArray(u["fields"])) return
|
|
431
|
+
if (!isObject(u["fields"]) || Array.isArray(u["fields"])) return item;
|
|
280
432
|
push(false, Object.values(u["fields"]));
|
|
281
433
|
break;
|
|
282
|
-
case "each":
|
|
283
|
-
|
|
434
|
+
case "each": {
|
|
435
|
+
const at = addrParts(u["at"], moveNumber);
|
|
436
|
+
if (at === void 0) return item;
|
|
284
437
|
push(true, [u["axis"]]);
|
|
285
|
-
push(false, [u["body"]]);
|
|
438
|
+
push(false, [u["body"], ...at]);
|
|
286
439
|
break;
|
|
440
|
+
}
|
|
441
|
+
case "fold": {
|
|
442
|
+
const at = addrParts(u["at"], moveNumber);
|
|
443
|
+
const names = loopParts(u);
|
|
444
|
+
if (at === void 0 || names === void 0) return item;
|
|
445
|
+
push(true, [u["axis"]]);
|
|
446
|
+
push(false, [...names, ...at]);
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
case "iterate": {
|
|
450
|
+
const limit = u["limit"];
|
|
451
|
+
const names = loopParts(u);
|
|
452
|
+
if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 0 || limit > 1e5 || names === void 0) return item;
|
|
453
|
+
push(false, [...names, ...u["until"] === void 0 ? [] : [u["until"]]]);
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
287
456
|
case "ext":
|
|
288
|
-
if (typeof u["kind"] !== "string" || !("data" in u)) return
|
|
457
|
+
if (typeof u["kind"] !== "string" || !("data" in u)) return item;
|
|
289
458
|
break;
|
|
290
|
-
default: return
|
|
459
|
+
default: return item;
|
|
291
460
|
}
|
|
292
461
|
}
|
|
293
|
-
return true;
|
|
294
462
|
}
|
|
295
|
-
/**
|
|
463
|
+
/**
|
|
464
|
+
* This function gives the child expressions of an expression, in the order of their child index. The expressions of
|
|
465
|
+
* the computed moves of an address come last. They evaluate at the position of their node, also for an axis.
|
|
466
|
+
*/
|
|
296
467
|
function children(e) {
|
|
297
468
|
switch (e.tag) {
|
|
298
469
|
case "app": return e.args;
|
|
299
470
|
case "let": return [...Object.values(e.bind), e.body];
|
|
300
471
|
case "rec": return Object.values(e.fields);
|
|
301
|
-
case "each": return [
|
|
472
|
+
case "each": return [
|
|
473
|
+
e.body,
|
|
474
|
+
...whereTests(e.axis),
|
|
475
|
+
...addrExprs(e.at)
|
|
476
|
+
];
|
|
477
|
+
case "fold": return [
|
|
478
|
+
...Object.values(e.init),
|
|
479
|
+
...Object.values(e.step),
|
|
480
|
+
...whereTests(e.axis),
|
|
481
|
+
...addrExprs(e.at),
|
|
482
|
+
e.result
|
|
483
|
+
];
|
|
484
|
+
case "iterate": return [
|
|
485
|
+
...Object.values(e.init),
|
|
486
|
+
...Object.values(e.step),
|
|
487
|
+
...e.until === void 0 ? [] : [e.until],
|
|
488
|
+
e.result
|
|
489
|
+
];
|
|
490
|
+
case "ref": return addrExprs(e.at);
|
|
302
491
|
case "lit":
|
|
303
|
-
case "ref":
|
|
304
492
|
case "var":
|
|
305
493
|
case "ext": return [];
|
|
306
494
|
}
|
|
@@ -316,10 +504,12 @@ function whereTests(a) {
|
|
|
316
504
|
var RecordSpace = class {
|
|
317
505
|
kind = "record";
|
|
318
506
|
keys;
|
|
507
|
+
records;
|
|
319
508
|
#records;
|
|
320
509
|
constructor(records) {
|
|
321
510
|
const entries = Object.entries(records);
|
|
322
511
|
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
512
|
+
this.records = Object.freeze(entries.map(([, o]) => o));
|
|
323
513
|
this.#records = new Map(entries);
|
|
324
514
|
}
|
|
325
515
|
has(k) {
|
|
@@ -334,29 +524,28 @@ var RecordSpace = class {
|
|
|
334
524
|
var ArraySpace = class {
|
|
335
525
|
kind = "array";
|
|
336
526
|
keys;
|
|
337
|
-
|
|
527
|
+
records;
|
|
338
528
|
constructor(items) {
|
|
339
|
-
|
|
340
|
-
this.
|
|
341
|
-
this.#items = new Map(entries);
|
|
529
|
+
this.keys = Object.freeze(Array.from(items, (_item, i) => `${i}`));
|
|
530
|
+
this.records = Object.freeze(Array.from(items));
|
|
342
531
|
}
|
|
343
532
|
has(k) {
|
|
344
|
-
return this
|
|
533
|
+
return this.keys[Number(k)] === k;
|
|
345
534
|
}
|
|
346
535
|
get(k) {
|
|
347
|
-
return this
|
|
536
|
+
return this.has(k) ? this.records[Number(k)] : void 0;
|
|
348
537
|
}
|
|
349
538
|
coords(k) {
|
|
350
539
|
return this.has(k) ? [Number(k)] : void 0;
|
|
351
540
|
}
|
|
352
541
|
keyAt(coords) {
|
|
353
|
-
|
|
354
|
-
return coords.length === 1 && this.has(k) ? k : void 0;
|
|
542
|
+
return coords.length === 1 ? this.keys[Number(coords[0])] : void 0;
|
|
355
543
|
}
|
|
356
544
|
};
|
|
357
545
|
var GridSpace = class {
|
|
358
546
|
kind = "grid";
|
|
359
547
|
keys;
|
|
548
|
+
records;
|
|
360
549
|
#cells;
|
|
361
550
|
#coords;
|
|
362
551
|
constructor(rows) {
|
|
@@ -366,6 +555,7 @@ var GridSpace = class {
|
|
|
366
555
|
Object.freeze([i, j])
|
|
367
556
|
]));
|
|
368
557
|
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
558
|
+
this.records = Object.freeze(entries.map(([, o]) => o));
|
|
369
559
|
this.#cells = new Map(entries.map(([k, cell]) => [k, cell]));
|
|
370
560
|
this.#coords = new Map(entries.map(([k, , c]) => [k, c]));
|
|
371
561
|
}
|
|
@@ -386,12 +576,14 @@ var GridSpace = class {
|
|
|
386
576
|
var TreeSpace = class {
|
|
387
577
|
kind = "tree";
|
|
388
578
|
keys;
|
|
579
|
+
records;
|
|
389
580
|
#records;
|
|
390
581
|
#parents;
|
|
391
582
|
#children;
|
|
392
583
|
constructor(records, parents) {
|
|
393
584
|
const entries = Object.entries(records);
|
|
394
585
|
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
586
|
+
this.records = Object.freeze(entries.map(([, o]) => o));
|
|
395
587
|
this.#records = new Map(entries);
|
|
396
588
|
const parentOf = /* @__PURE__ */ new Map();
|
|
397
589
|
for (const [k, p] of Object.entries(parents)) {
|
|
@@ -425,6 +617,13 @@ var TreeSpace = class {
|
|
|
425
617
|
}
|
|
426
618
|
coords() {}
|
|
427
619
|
keyAt() {}
|
|
620
|
+
successorsOf(k) {
|
|
621
|
+
return this.childrenOf(k);
|
|
622
|
+
}
|
|
623
|
+
predecessorsOf(k) {
|
|
624
|
+
const p = this.#parents.get(k);
|
|
625
|
+
return p === void 0 ? [] : [p];
|
|
626
|
+
}
|
|
428
627
|
parentOf(k) {
|
|
429
628
|
return this.#parents.get(k);
|
|
430
629
|
}
|
|
@@ -433,6 +632,51 @@ var TreeSpace = class {
|
|
|
433
632
|
}
|
|
434
633
|
};
|
|
435
634
|
/** The constructors of spaces. */
|
|
635
|
+
/** The empty list of keys. */
|
|
636
|
+
const NO_KEYS = Object.freeze([]);
|
|
637
|
+
var GraphSpace = class {
|
|
638
|
+
kind = "graph";
|
|
639
|
+
keys;
|
|
640
|
+
records;
|
|
641
|
+
#records;
|
|
642
|
+
#out;
|
|
643
|
+
#in;
|
|
644
|
+
constructor(records, edges) {
|
|
645
|
+
const entries = Object.entries(records);
|
|
646
|
+
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
647
|
+
this.records = Object.freeze(entries.map(([, o]) => o));
|
|
648
|
+
this.#records = new Map(entries);
|
|
649
|
+
const out = /* @__PURE__ */ new Map();
|
|
650
|
+
for (const [k, to] of Object.entries(edges)) {
|
|
651
|
+
if (!this.#records.has(k)) throw new TypeError(`the graph has no key "${k}", but the edges name it`);
|
|
652
|
+
const ends = [...new Set(to ?? NO_KEYS)];
|
|
653
|
+
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`);
|
|
654
|
+
out.set(k, Object.freeze(ends));
|
|
655
|
+
}
|
|
656
|
+
this.#out = out;
|
|
657
|
+
const into = /* @__PURE__ */ new Map();
|
|
658
|
+
for (const k of this.keys) for (const t of out.get(k) ?? NO_KEYS) {
|
|
659
|
+
const from = into.get(t);
|
|
660
|
+
if (from === void 0) into.set(t, [k]);
|
|
661
|
+
else from.push(k);
|
|
662
|
+
}
|
|
663
|
+
this.#in = new Map([...into].map(([k, from]) => [k, Object.freeze(from)]));
|
|
664
|
+
}
|
|
665
|
+
has(k) {
|
|
666
|
+
return this.#records.has(k);
|
|
667
|
+
}
|
|
668
|
+
get(k) {
|
|
669
|
+
return this.#records.get(k);
|
|
670
|
+
}
|
|
671
|
+
coords() {}
|
|
672
|
+
keyAt() {}
|
|
673
|
+
successorsOf(k) {
|
|
674
|
+
return this.#out.get(k) ?? NO_KEYS;
|
|
675
|
+
}
|
|
676
|
+
predecessorsOf(k) {
|
|
677
|
+
return this.#in.get(k) ?? NO_KEYS;
|
|
678
|
+
}
|
|
679
|
+
};
|
|
436
680
|
const space = {
|
|
437
681
|
/** This function makes a space from the entries of an object. The keys are the property names. */
|
|
438
682
|
record(records) {
|
|
@@ -452,6 +696,13 @@ const space = {
|
|
|
452
696
|
*/
|
|
453
697
|
tree(records, parents) {
|
|
454
698
|
return new TreeSpace(records, parents);
|
|
699
|
+
},
|
|
700
|
+
/**
|
|
701
|
+
* This function makes a graph space: the records by key, and the edges from each key, in order. An edge to a key
|
|
702
|
+
* that is not in the graph throws a `TypeError`. A graph can have cycles, and an edge can go from a key to itself.
|
|
703
|
+
*/
|
|
704
|
+
graph(records, edges) {
|
|
705
|
+
return new GraphSpace(records, edges);
|
|
455
706
|
}
|
|
456
707
|
};
|
|
457
708
|
/** This function applies one move to the focus. It gives the new focus, or a `#REF!` error. */
|
|
@@ -497,6 +748,31 @@ function applyMove(s, at, m) {
|
|
|
497
748
|
}
|
|
498
749
|
}
|
|
499
750
|
}
|
|
751
|
+
/**
|
|
752
|
+
* This function gives the fixed moves of an address. `values` has the value of the expression of each computed move,
|
|
753
|
+
* in order (NAV.COMPUTED). A key that is not a text, or a position that is not a number, gives `#VALUE!`.
|
|
754
|
+
*/
|
|
755
|
+
function fixAddr(addr, values, at) {
|
|
756
|
+
const out = [];
|
|
757
|
+
let next = 0;
|
|
758
|
+
for (const m of addr) {
|
|
759
|
+
if (moveExpr(m) === void 0) {
|
|
760
|
+
out.push(m);
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
const u = values[next++];
|
|
764
|
+
if (m.t === "key" && typeof u === "string") out.push({
|
|
765
|
+
t: "key",
|
|
766
|
+
key: u
|
|
767
|
+
});
|
|
768
|
+
else if (m.t === "index" && typeof u === "number") out.push({
|
|
769
|
+
t: "index",
|
|
770
|
+
i: u
|
|
771
|
+
});
|
|
772
|
+
else return fail(vexError("kind-mismatch", m.t === "key" ? `the key of a move is a ${typeof u}, not a text` : `the position of a move is a ${typeof u}, not a number`, at));
|
|
773
|
+
}
|
|
774
|
+
return ok(out);
|
|
775
|
+
}
|
|
500
776
|
/** This function applies the moves of an address in order. It gives the key where the address points. */
|
|
501
777
|
function resolveAddr(s, at, addr) {
|
|
502
778
|
let focus = at.focus;
|
|
@@ -580,8 +856,49 @@ function axisTargets(s, at, a) {
|
|
|
580
856
|
case "ancestors":
|
|
581
857
|
case "descendants":
|
|
582
858
|
case "siblings": return treeTargets(s, at, a.t);
|
|
859
|
+
case "window": return windowTargets(s, at, a.from, a.to);
|
|
860
|
+
case "successors":
|
|
861
|
+
case "predecessors":
|
|
862
|
+
case "reachable": return graphTargets(s, at, a.t);
|
|
863
|
+
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
864
|
}
|
|
584
865
|
}
|
|
866
|
+
/**
|
|
867
|
+
* This function gives the targets of an axis of a graph. `reachable` has each key that a path of one or more edges
|
|
868
|
+
* reaches from the focus, once, in the order of a breadth-first search. The focus is not a target of `reachable`.
|
|
869
|
+
*/
|
|
870
|
+
function graphTargets(s, at, t) {
|
|
871
|
+
const successorsOf = s.successorsOf?.bind(s);
|
|
872
|
+
const predecessorsOf = s.predecessorsOf?.bind(s);
|
|
873
|
+
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));
|
|
874
|
+
if (t === "successors") return ok(successorsOf(at.focus));
|
|
875
|
+
if (t === "predecessors") return ok(predecessorsOf(at.focus));
|
|
876
|
+
const seen = /* @__PURE__ */ new Set([at.focus]);
|
|
877
|
+
const queue = [at.focus];
|
|
878
|
+
for (const k of queue) for (const next of successorsOf(k)) {
|
|
879
|
+
if (seen.has(next)) continue;
|
|
880
|
+
seen.add(next);
|
|
881
|
+
queue.push(next);
|
|
882
|
+
}
|
|
883
|
+
return ok(queue.slice(1));
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* This function gives the targets of the axis `window(from, to)`: the keys of the moves `offset(d)` for each `d`
|
|
887
|
+
* from `from` to `to`, in order. An offset outside the space has no target. The axis needs an array space or a tree
|
|
888
|
+
* space, where an offset has one number.
|
|
889
|
+
*/
|
|
890
|
+
function windowTargets(s, at, from, to) {
|
|
891
|
+
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));
|
|
892
|
+
const out = [];
|
|
893
|
+
for (let d = Math.max(from, -s.keys.length); d <= Math.min(to, s.keys.length); d++) {
|
|
894
|
+
const r = applyMove(s, at, {
|
|
895
|
+
t: "offset",
|
|
896
|
+
d: [d]
|
|
897
|
+
});
|
|
898
|
+
if (r.ok) out.push(r.value);
|
|
899
|
+
}
|
|
900
|
+
return ok(out);
|
|
901
|
+
}
|
|
585
902
|
//#endregion
|
|
586
903
|
//#region src/domain.ts
|
|
587
904
|
/** This function makes a domain. The result is frozen. */
|
|
@@ -700,12 +1017,14 @@ function nestingOf(e) {
|
|
|
700
1017
|
return deepest;
|
|
701
1018
|
}
|
|
702
1019
|
function compile(expr, opts = {}) {
|
|
703
|
-
const
|
|
704
|
-
const
|
|
1020
|
+
const shape = shapeError(expr);
|
|
1021
|
+
const nesting = shape === void 0 ? nestingOf(expr) : 0;
|
|
1022
|
+
const bad = shape ?? (nesting > 500 ? `the expression is nested ${nesting} levels deep, and the limit is 500` : void 0);
|
|
1023
|
+
const root = bad === void 0 ? new Compiler(opts).node(expr, []) : (f) => fail(vexError("bad-expression", bad, {
|
|
705
1024
|
path: [],
|
|
706
1025
|
origin: f.pos.origin,
|
|
707
1026
|
focus: f.pos.focus
|
|
708
|
-
}))
|
|
1027
|
+
}));
|
|
709
1028
|
const run = (r) => {
|
|
710
1029
|
const { space, origin } = r;
|
|
711
1030
|
if (!space.has(origin)) return fail(vexError("unknown-key", `the space has no key "${origin}"`, {
|
|
@@ -722,7 +1041,8 @@ function compile(expr, opts = {}) {
|
|
|
722
1041
|
env
|
|
723
1042
|
}, {
|
|
724
1043
|
space,
|
|
725
|
-
trace: r.trace
|
|
1044
|
+
trace: r.trace,
|
|
1045
|
+
tables: r.tables
|
|
726
1046
|
});
|
|
727
1047
|
} catch (thrown) {
|
|
728
1048
|
if (isStackOverflow(thrown)) return fail(vexError("depth", DEPTH_MESSAGE, {
|
|
@@ -774,6 +1094,8 @@ function evaluate(expr, opts) {
|
|
|
774
1094
|
function explain(expr, opts) {
|
|
775
1095
|
return compiled(expr, opts).explain(opts);
|
|
776
1096
|
}
|
|
1097
|
+
/** This function gives the axis inside the `where` axes of an axis. */
|
|
1098
|
+
const baseAxis = (a) => a.t === "where" ? baseAxis(a.axis) : a;
|
|
777
1099
|
var Compiler = class {
|
|
778
1100
|
#domains;
|
|
779
1101
|
#fns;
|
|
@@ -850,6 +1172,8 @@ var Compiler = class {
|
|
|
850
1172
|
}
|
|
851
1173
|
case "app": return isSpecialForm(e.op) ? this.#special(e.op, this.#nodes(e.args, path), info) : this.#app(e.op, this.#nodes(e.args, path), info);
|
|
852
1174
|
case "each": return this.#each(e, info);
|
|
1175
|
+
case "fold": return this.#fold(e, info);
|
|
1176
|
+
case "iterate": return this.#iterate(e, info);
|
|
853
1177
|
case "ext": {
|
|
854
1178
|
const extensions = this.#extensions ?? {};
|
|
855
1179
|
const handler = Object.hasOwn(extensions, e.kind) ? extensions[e.kind] : void 0;
|
|
@@ -893,10 +1217,34 @@ var Compiler = class {
|
|
|
893
1217
|
thrown
|
|
894
1218
|
});
|
|
895
1219
|
}
|
|
1220
|
+
/**
|
|
1221
|
+
* This method compiles an address. Its function gives the key where the address points. The expressions of the
|
|
1222
|
+
* computed moves evaluate first, at the position of the node, and their child indexes start at `first`. An error of
|
|
1223
|
+
* an expression keeps its path, and an error of a move gets the path of the node.
|
|
1224
|
+
*/
|
|
1225
|
+
#address(at, path, first) {
|
|
1226
|
+
const nodes = addrExprs(at).map((x, i) => this.node(x, [...path, first + i]));
|
|
1227
|
+
const moved = (r) => r.ok ? r : fail({
|
|
1228
|
+
...r.error,
|
|
1229
|
+
path
|
|
1230
|
+
});
|
|
1231
|
+
if (nodes.length === 0) return (f, c) => moved(resolveAddr(c.space, f.pos, at));
|
|
1232
|
+
return (f, c) => {
|
|
1233
|
+
const values = [];
|
|
1234
|
+
for (const n of nodes) {
|
|
1235
|
+
const r = n(f, c);
|
|
1236
|
+
if (!r.ok) return r;
|
|
1237
|
+
values.push(r.value);
|
|
1238
|
+
}
|
|
1239
|
+
const fixed = fixAddr(at, values, f.pos);
|
|
1240
|
+
return fixed.ok ? moved(resolveAddr(c.space, f.pos, fixed.value)) : moved(fixed);
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
896
1243
|
#ref(e, info) {
|
|
897
1244
|
const { path } = info;
|
|
898
1245
|
const segs = e.path;
|
|
899
1246
|
const at = e.at ?? [];
|
|
1247
|
+
const address = this.#address(at, path, 0);
|
|
900
1248
|
const label = segs.join(".");
|
|
901
1249
|
const steps = segs.map((seg, i) => ({
|
|
902
1250
|
seg,
|
|
@@ -906,11 +1254,8 @@ var Compiler = class {
|
|
|
906
1254
|
return (f, c) => {
|
|
907
1255
|
let key = f.pos.focus;
|
|
908
1256
|
if (at.length > 0) {
|
|
909
|
-
const where =
|
|
910
|
-
if (!where.ok) return this.#emit(c, info, f,
|
|
911
|
-
...where.error,
|
|
912
|
-
path
|
|
913
|
-
}));
|
|
1257
|
+
const where = address(f, c);
|
|
1258
|
+
if (!where.ok) return this.#emit(c, info, f, where);
|
|
914
1259
|
key = where.value;
|
|
915
1260
|
}
|
|
916
1261
|
const failed = (r) => this.#emit(c, info, f, r, [{
|
|
@@ -956,16 +1301,15 @@ var Compiler = class {
|
|
|
956
1301
|
#each(e, info) {
|
|
957
1302
|
const { path } = info;
|
|
958
1303
|
const at = e.at ?? [];
|
|
959
|
-
const
|
|
1304
|
+
const counter = { next: 1 };
|
|
1305
|
+
const targets = this.#targets(e.axis, path, counter);
|
|
960
1306
|
const body = this.node(e.body, [...path, 0]);
|
|
1307
|
+
const address = this.#address(at, path, counter.next);
|
|
961
1308
|
return (f, c) => {
|
|
962
1309
|
let from = f;
|
|
963
1310
|
if (at.length > 0) {
|
|
964
|
-
const where =
|
|
965
|
-
if (!where.ok) return this.#emit(c, info, f,
|
|
966
|
-
...where.error,
|
|
967
|
-
path
|
|
968
|
-
}));
|
|
1311
|
+
const where = address(f, c);
|
|
1312
|
+
if (!where.ok) return this.#emit(c, info, f, where);
|
|
969
1313
|
from = {
|
|
970
1314
|
pos: {
|
|
971
1315
|
origin: f.pos.origin,
|
|
@@ -974,7 +1318,17 @@ var Compiler = class {
|
|
|
974
1318
|
env: f.env
|
|
975
1319
|
};
|
|
976
1320
|
}
|
|
977
|
-
const
|
|
1321
|
+
const base = baseAxis(e.axis);
|
|
1322
|
+
let inner = c;
|
|
1323
|
+
if (base.t === "table") {
|
|
1324
|
+
const space = c.tables !== void 0 && Object.hasOwn(c.tables, base.name) ? c.tables[base.name] : void 0;
|
|
1325
|
+
if (space === void 0) return this.#emit(c, info, f, this.#err("no-table", `the evaluation has no table "${base.name}"`, f, path));
|
|
1326
|
+
inner = {
|
|
1327
|
+
...c,
|
|
1328
|
+
space
|
|
1329
|
+
};
|
|
1330
|
+
}
|
|
1331
|
+
const ts = targets(from, inner);
|
|
978
1332
|
if (!ts.ok) return this.#emit(c, info, f, ts);
|
|
979
1333
|
const items = ts.value.map(({ key, test }) => {
|
|
980
1334
|
if (test !== void 0 && !test.ok) return {
|
|
@@ -985,18 +1339,148 @@ var Compiler = class {
|
|
|
985
1339
|
key,
|
|
986
1340
|
result: body({
|
|
987
1341
|
pos: {
|
|
988
|
-
origin: f.pos.origin,
|
|
1342
|
+
origin: base.t === "table" ? key : f.pos.origin,
|
|
989
1343
|
focus: key
|
|
990
1344
|
},
|
|
991
1345
|
env: f.env
|
|
992
|
-
},
|
|
1346
|
+
}, inner)
|
|
993
1347
|
};
|
|
994
1348
|
});
|
|
995
1349
|
return this.#emit(c, info, f, ok(vexList(items)));
|
|
996
1350
|
};
|
|
997
1351
|
}
|
|
1352
|
+
/**
|
|
1353
|
+
* This method compiles the names of a loop: `init` at the indexes from 0, and `step` after them (LOOP.NAMES). Its
|
|
1354
|
+
* functions give the first values, and the values after one step. The first error stops each of them.
|
|
1355
|
+
*/
|
|
1356
|
+
#names(e, path) {
|
|
1357
|
+
const inits = Object.entries(e.init).map(([name, x], i) => [name, this.node(x, [...path, i])]);
|
|
1358
|
+
const steps = Object.entries(e.step).map(([name, x], i) => [name, this.node(x, [...path, inits.length + i])]);
|
|
1359
|
+
return {
|
|
1360
|
+
first: (f, c) => {
|
|
1361
|
+
const env = new Map(f.env);
|
|
1362
|
+
for (const [name, n] of inits) {
|
|
1363
|
+
const r = n(f, c);
|
|
1364
|
+
if (!r.ok) return r;
|
|
1365
|
+
env.set(name, r);
|
|
1366
|
+
}
|
|
1367
|
+
return ok(env);
|
|
1368
|
+
},
|
|
1369
|
+
next: (f, c) => {
|
|
1370
|
+
const values = [];
|
|
1371
|
+
for (const [name, n] of steps) {
|
|
1372
|
+
const r = n(f, c);
|
|
1373
|
+
if (!r.ok) return r;
|
|
1374
|
+
values.push([name, r]);
|
|
1375
|
+
}
|
|
1376
|
+
const env = new Map(f.env);
|
|
1377
|
+
for (const [name, r] of values) env.set(name, r);
|
|
1378
|
+
return ok(env);
|
|
1379
|
+
}
|
|
1380
|
+
};
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* This method compiles a fold (FOLD.ORDER, FOLD.ERROR). `init` evaluates at the node, then the address and the
|
|
1384
|
+
* targets. A step evaluates at each target in order. `result` evaluates at the node with the last values.
|
|
1385
|
+
*/
|
|
1386
|
+
#fold(e, info) {
|
|
1387
|
+
const { path } = info;
|
|
1388
|
+
const at = e.at ?? [];
|
|
1389
|
+
const names = this.#names(e, path);
|
|
1390
|
+
const counter = { next: Object.keys(e.init).length + Object.keys(e.step).length };
|
|
1391
|
+
const targets = this.#targets(e.axis, path, counter);
|
|
1392
|
+
const address = this.#address(at, path, counter.next);
|
|
1393
|
+
const result = this.node(e.result, [...path, counter.next + addrExprs(at).length]);
|
|
1394
|
+
const base = baseAxis(e.axis);
|
|
1395
|
+
return (f, c) => {
|
|
1396
|
+
const first = names.first(f, c);
|
|
1397
|
+
if (!first.ok) return this.#emit(c, info, f, first);
|
|
1398
|
+
let env = first.value;
|
|
1399
|
+
let from = f;
|
|
1400
|
+
if (at.length > 0) {
|
|
1401
|
+
const where = address(f, c);
|
|
1402
|
+
if (!where.ok) return this.#emit(c, info, f, where);
|
|
1403
|
+
from = {
|
|
1404
|
+
pos: {
|
|
1405
|
+
origin: f.pos.origin,
|
|
1406
|
+
focus: where.value
|
|
1407
|
+
},
|
|
1408
|
+
env: f.env
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
let inner = c;
|
|
1412
|
+
if (base.t === "table") {
|
|
1413
|
+
const space = c.tables !== void 0 && Object.hasOwn(c.tables, base.name) ? c.tables[base.name] : void 0;
|
|
1414
|
+
if (space === void 0) return this.#emit(c, info, f, this.#err("no-table", `the evaluation has no table "${base.name}"`, f, path));
|
|
1415
|
+
inner = {
|
|
1416
|
+
...c,
|
|
1417
|
+
space
|
|
1418
|
+
};
|
|
1419
|
+
}
|
|
1420
|
+
const ts = targets(from, inner);
|
|
1421
|
+
if (!ts.ok) return this.#emit(c, info, f, ts);
|
|
1422
|
+
for (const { key, test } of ts.value) {
|
|
1423
|
+
if (test !== void 0 && !test.ok) return this.#emit(c, info, f, test);
|
|
1424
|
+
const after = names.next({
|
|
1425
|
+
pos: {
|
|
1426
|
+
origin: base.t === "table" ? key : f.pos.origin,
|
|
1427
|
+
focus: key
|
|
1428
|
+
},
|
|
1429
|
+
env
|
|
1430
|
+
}, inner);
|
|
1431
|
+
if (!after.ok) return this.#emit(c, info, f, after);
|
|
1432
|
+
env = after.value;
|
|
1433
|
+
}
|
|
1434
|
+
return this.#emit(c, info, f, result({
|
|
1435
|
+
pos: f.pos,
|
|
1436
|
+
env
|
|
1437
|
+
}, c));
|
|
1438
|
+
};
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* This method compiles an iteration (ITERATE.LIMIT, ITERATE.UNTIL, ITERATE.ERROR). The test `until` evaluates before
|
|
1442
|
+
* each step and after the last step. `result` evaluates with the last values.
|
|
1443
|
+
*/
|
|
1444
|
+
#iterate(e, info) {
|
|
1445
|
+
const { path } = info;
|
|
1446
|
+
const names = this.#names(e, path);
|
|
1447
|
+
const untilAt = Object.keys(e.init).length + Object.keys(e.step).length;
|
|
1448
|
+
const untilPath = [...path, untilAt];
|
|
1449
|
+
const until = e.until === void 0 ? void 0 : this.node(e.until, untilPath);
|
|
1450
|
+
const result = this.node(e.result, [...path, untilAt + (until === void 0 ? 0 : 1)]);
|
|
1451
|
+
const { limit } = e;
|
|
1452
|
+
return (f, c) => {
|
|
1453
|
+
const first = names.first(f, c);
|
|
1454
|
+
if (!first.ok) return this.#emit(c, info, f, first);
|
|
1455
|
+
let env = first.value;
|
|
1456
|
+
for (let k = 0;; k++) {
|
|
1457
|
+
const frame = {
|
|
1458
|
+
pos: f.pos,
|
|
1459
|
+
env
|
|
1460
|
+
};
|
|
1461
|
+
if (until !== void 0) {
|
|
1462
|
+
const t = until(frame, c);
|
|
1463
|
+
if (!t.ok) return this.#emit(c, info, f, t);
|
|
1464
|
+
if (t.value === true) break;
|
|
1465
|
+
if (t.value !== false) return this.#emit(c, info, f, this.#err("kind-mismatch", `the test "until" of "iterate" gave a ${typeof t.value}, not a boolean`, f, untilPath));
|
|
1466
|
+
}
|
|
1467
|
+
if (k === limit) {
|
|
1468
|
+
if (until !== void 0) return this.#emit(c, info, f, this.#err("limit", `"iterate" made its ${String(limit)} steps, and its test "until" is not true`, f, path));
|
|
1469
|
+
break;
|
|
1470
|
+
}
|
|
1471
|
+
const after = names.next(frame, c);
|
|
1472
|
+
if (!after.ok) return this.#emit(c, info, f, after);
|
|
1473
|
+
env = after.value;
|
|
1474
|
+
}
|
|
1475
|
+
return this.#emit(c, info, f, result({
|
|
1476
|
+
pos: f.pos,
|
|
1477
|
+
env
|
|
1478
|
+
}, c));
|
|
1479
|
+
};
|
|
1480
|
+
}
|
|
998
1481
|
/** This method compiles the targets of an axis. For `where`, a failed test keeps the target with its error. */
|
|
999
1482
|
#targets(a, path, counter) {
|
|
1483
|
+
if (a.t === "table") return (_f, c) => ok(c.space.keys.map((key) => ({ key })));
|
|
1000
1484
|
if (a.t !== "where") return (f, c) => {
|
|
1001
1485
|
const keys = axisTargets(c.space, f.pos, a);
|
|
1002
1486
|
return keys.ok ? ok(keys.value.map((key) => ({ key }))) : fail({
|
|
@@ -1007,6 +1491,7 @@ var Compiler = class {
|
|
|
1007
1491
|
const inner = this.#targets(a.axis, path, counter);
|
|
1008
1492
|
const testPath = [...path, counter.next++];
|
|
1009
1493
|
const test = this.node(a.test, testPath);
|
|
1494
|
+
const sameOrigin = baseAxis(a).t === "table";
|
|
1010
1495
|
return (f, c) => {
|
|
1011
1496
|
const ts = inner(f, c);
|
|
1012
1497
|
if (!ts.ok) return ts;
|
|
@@ -1018,7 +1503,7 @@ var Compiler = class {
|
|
|
1018
1503
|
}
|
|
1019
1504
|
const at = {
|
|
1020
1505
|
pos: {
|
|
1021
|
-
origin: f.pos.origin,
|
|
1506
|
+
origin: sameOrigin ? t.key : f.pos.origin,
|
|
1022
1507
|
focus: t.key
|
|
1023
1508
|
},
|
|
1024
1509
|
env: f.env
|
|
@@ -1226,6 +1711,7 @@ const FORBIDDEN_FIELDS = /* @__PURE__ */ new Set([
|
|
|
1226
1711
|
"constructor",
|
|
1227
1712
|
"prototype"
|
|
1228
1713
|
]);
|
|
1714
|
+
/** This function tells if a segment of a path can name a field. A compiler uses the same rule as the interpreter. */
|
|
1229
1715
|
const isFieldName = (seg) => seg.length > 0 && !FORBIDDEN_FIELDS.has(seg);
|
|
1230
1716
|
/** This function runs the `is` test of a domain. A test that throws counts as `false`. */
|
|
1231
1717
|
function safeIs(d, value) {
|
|
@@ -1235,6 +1721,7 @@ function safeIs(d, value) {
|
|
|
1235
1721
|
return false;
|
|
1236
1722
|
}
|
|
1237
1723
|
}
|
|
1724
|
+
/** This function tells if an argument has the kind of a parameter of an op of the domain `domain`. */
|
|
1238
1725
|
function matchesKind(kind, value, domain) {
|
|
1239
1726
|
switch (kind) {
|
|
1240
1727
|
case "domain": return safeIs(domain, value);
|
|
@@ -1264,15 +1751,17 @@ function labelOf(e) {
|
|
|
1264
1751
|
case "let": return `let ${Object.keys(e.bind).join(", ")}`;
|
|
1265
1752
|
case "var": return e.name;
|
|
1266
1753
|
case "rec": return `{ ${Object.keys(e.fields).join(", ")} }`;
|
|
1267
|
-
case "each":
|
|
1754
|
+
case "each":
|
|
1755
|
+
case "fold": return e.at === void 0 ? `${e.tag} ${e.axis.t}` : `${e.tag} ${e.axis.t} from ${e.at.map(moveText).join(".")}`;
|
|
1756
|
+
case "iterate": return `iterate ${String(e.limit)}`;
|
|
1268
1757
|
case "ext": return e.kind;
|
|
1269
1758
|
}
|
|
1270
1759
|
}
|
|
1271
1760
|
/** This function gives a short text for a move, for the label of an axis with an address. */
|
|
1272
1761
|
function moveText(m) {
|
|
1273
1762
|
switch (m.t) {
|
|
1274
|
-
case "key": return `key(${m.key})`;
|
|
1275
|
-
case "index": return `index(${m.i})`;
|
|
1763
|
+
case "key": return `key(${typeof m.key === "string" ? m.key : labelOf(m.key)})`;
|
|
1764
|
+
case "index": return `index(${typeof m.i === "number" ? String(m.i) : labelOf(m.i)})`;
|
|
1276
1765
|
case "offset": return `offset(${m.d.join(", ")})`;
|
|
1277
1766
|
case "other":
|
|
1278
1767
|
case "origin":
|
|
@@ -1320,7 +1809,8 @@ const sizeOf = (e) => 1 + children(e).reduce((s, c) => s + sizeOf(c), 0);
|
|
|
1320
1809
|
//#region src/deps.ts
|
|
1321
1810
|
/**
|
|
1322
1811
|
* This function gives the static reads of an expression. A host can use them to recompute a program only
|
|
1323
|
-
* when a field that it reads changes.
|
|
1812
|
+
* when a field that it reads changes. A computed move reads a key that only a run knows. Its address keeps the
|
|
1813
|
+
* expression, and the reads of that expression are in the list too (NAV.COMPUTED).
|
|
1324
1814
|
*/
|
|
1325
1815
|
function deps(e) {
|
|
1326
1816
|
const out = [];
|
|
@@ -1331,21 +1821,25 @@ function deps(e) {
|
|
|
1331
1821
|
}];
|
|
1332
1822
|
for (let item = todo.pop(); item !== void 0; item = todo.pop()) {
|
|
1333
1823
|
const { x } = item;
|
|
1334
|
-
if (x.tag === "ref") {
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
const
|
|
1344
|
-
const
|
|
1345
|
-
|
|
1346
|
-
|
|
1824
|
+
if (x.tag === "ref") out.push({
|
|
1825
|
+
at: x.at ?? [],
|
|
1826
|
+
path: x.path,
|
|
1827
|
+
axes: item.axes,
|
|
1828
|
+
axisAt: item.axisAt
|
|
1829
|
+
});
|
|
1830
|
+
const all = children(x);
|
|
1831
|
+
const axial = x.tag === "each" || x.tag === "fold";
|
|
1832
|
+
const first = x.tag === "fold" ? Object.keys(x.init).length : 0;
|
|
1833
|
+
const last = axial ? all.length - addrExprs(x.at).length - (x.tag === "fold" ? 1 : 0) : 0;
|
|
1834
|
+
const axes = axial ? [...item.axes, x.axis.t] : item.axes;
|
|
1835
|
+
const axisAt = axial ? [...item.axisAt, x.at ?? []] : item.axisAt;
|
|
1836
|
+
for (let i = all.length - 1; i >= 0; i--) todo.push(i >= first && i < last ? {
|
|
1837
|
+
x: all[i],
|
|
1347
1838
|
axes,
|
|
1348
1839
|
axisAt
|
|
1840
|
+
} : {
|
|
1841
|
+
...item,
|
|
1842
|
+
x: all[i]
|
|
1349
1843
|
});
|
|
1350
1844
|
}
|
|
1351
1845
|
return out;
|
|
@@ -1467,6 +1961,25 @@ function mapValues(u, f) {
|
|
|
1467
1961
|
out[key] = rec;
|
|
1468
1962
|
}
|
|
1469
1963
|
};
|
|
1964
|
+
const address = () => {
|
|
1965
|
+
const at = u["at"];
|
|
1966
|
+
if (!Array.isArray(at)) return void 0;
|
|
1967
|
+
const moves = [];
|
|
1968
|
+
for (const m of at) {
|
|
1969
|
+
const field = isRecord(m) ? m["t"] === "key" ? "key" : m["t"] === "index" ? "i" : void 0 : void 0;
|
|
1970
|
+
if (field === void 0 || !isRecord(m) || !isRecord(m[field])) {
|
|
1971
|
+
moves.push(m);
|
|
1972
|
+
continue;
|
|
1973
|
+
}
|
|
1974
|
+
const r = mapValues(m[field], f);
|
|
1975
|
+
if (!r.ok) return r;
|
|
1976
|
+
moves.push({
|
|
1977
|
+
...m,
|
|
1978
|
+
[field]: r.value
|
|
1979
|
+
});
|
|
1980
|
+
}
|
|
1981
|
+
out["at"] = moves;
|
|
1982
|
+
};
|
|
1470
1983
|
const axis = (a) => {
|
|
1471
1984
|
if (!isRecord(a) || a["t"] !== "where") return ok(a);
|
|
1472
1985
|
const inner = axis(a["axis"]);
|
|
@@ -1493,8 +2006,16 @@ function mapValues(u, f) {
|
|
|
1493
2006
|
const a = axis(u["axis"]);
|
|
1494
2007
|
if (!a.ok) return a;
|
|
1495
2008
|
out["axis"] = a.value;
|
|
1496
|
-
return child("body") ?? ok(out);
|
|
2009
|
+
return child("body") ?? address() ?? ok(out);
|
|
2010
|
+
}
|
|
2011
|
+
case "fold": {
|
|
2012
|
+
const a = axis(u["axis"]);
|
|
2013
|
+
if (!a.ok) return a;
|
|
2014
|
+
out["axis"] = a.value;
|
|
2015
|
+
return each("init") ?? each("step") ?? child("result") ?? address() ?? ok(out);
|
|
1497
2016
|
}
|
|
2017
|
+
case "iterate": return each("init") ?? each("step") ?? (u["until"] === void 0 ? void 0 : child("until")) ?? child("result") ?? ok(out);
|
|
2018
|
+
case "ref": return address() ?? ok(out);
|
|
1498
2019
|
default: return ok(out);
|
|
1499
2020
|
}
|
|
1500
2021
|
}
|
|
@@ -1620,6 +2141,9 @@ function traversal(items, env, isStrict = false) {
|
|
|
1620
2141
|
*
|
|
1621
2142
|
* A cell reference is the extension node `ext("vex.cell", { column, at })`. Thus the IR has no new kind, and the
|
|
1622
2143
|
* JSON form, `deps` and the traces work without a change.
|
|
2144
|
+
*
|
|
2145
|
+
* A cell can have its own formula. That formula replaces the formula of the column at the key of the cell. A
|
|
2146
|
+
* spreadsheet has the same thing: a cell with a formula that is different from the other formulas of its column.
|
|
1623
2147
|
*/
|
|
1624
2148
|
/** The extension kind of a cell reference. */
|
|
1625
2149
|
const CELL_KIND = "vex.cell";
|
|
@@ -1628,13 +2152,135 @@ const cell = (column, at = []) => ext(CELL_KIND, {
|
|
|
1628
2152
|
column,
|
|
1629
2153
|
at
|
|
1630
2154
|
});
|
|
1631
|
-
const isCellRef = (u) => typeof u === "object" && u !== null && typeof u.column === "string" && Array.isArray(u.at) && u.at.every(isMove);
|
|
2155
|
+
const isCellRef = (u) => typeof u === "object" && u !== null && typeof u.column === "string" && Array.isArray(u.at) && u.at.every((m) => isMove(m) && moveExpr(m) === void 0);
|
|
2156
|
+
/**
|
|
2157
|
+
* The compiled programs of the formulas of the columns: one entry for each formula, for the last compiler and options.
|
|
2158
|
+
* A compiled program reads the cells of the active run, so each run with the same compiler and options can use it.
|
|
2159
|
+
*/
|
|
2160
|
+
const PROGRAMS = /* @__PURE__ */ new WeakMap();
|
|
2161
|
+
/** The key of the options without extensions, for the extensions of the compiled programs. */
|
|
2162
|
+
const NO_EXTENSIONS = {};
|
|
1632
2163
|
const cycleError = (column, key) => fail(vexError("cycle", `the cell "${column}" at "${key}" is on a cycle of cell references`, {
|
|
1633
2164
|
origin: key,
|
|
1634
2165
|
focus: key
|
|
1635
2166
|
}));
|
|
1636
2167
|
/** The id of a cell. */
|
|
1637
|
-
const idOf = (column, key) => `${column}\u0000${key}`;
|
|
2168
|
+
const idOf = (t, column, key) => `${t.tag}\u0001${column}\u0000${key}`;
|
|
2169
|
+
/** This function gives the state of a table at the start of a run. */
|
|
2170
|
+
const tableState = (where, i, t) => ({
|
|
2171
|
+
where,
|
|
2172
|
+
tag: String(i),
|
|
2173
|
+
space: t.space,
|
|
2174
|
+
columns: t.columns,
|
|
2175
|
+
cells: t.cells ?? /* @__PURE__ */ new Map(),
|
|
2176
|
+
warmed: /* @__PURE__ */ new Map(),
|
|
2177
|
+
index: void 0,
|
|
2178
|
+
done: /* @__PURE__ */ new Map()
|
|
2179
|
+
});
|
|
2180
|
+
/** This function gives the result of a finished cell, or `undefined`. */
|
|
2181
|
+
const doneOf = (t, column, key) => t.done.get(column)?.get(key);
|
|
2182
|
+
/** This function keeps the result of a finished cell. */
|
|
2183
|
+
function setDone(t, column, key, result) {
|
|
2184
|
+
let results = t.done.get(column);
|
|
2185
|
+
if (results === void 0) {
|
|
2186
|
+
results = /* @__PURE__ */ new Map();
|
|
2187
|
+
t.done.set(column, results);
|
|
2188
|
+
}
|
|
2189
|
+
results.set(key, result);
|
|
2190
|
+
}
|
|
2191
|
+
/** This function adds the direction of one move or one axis of a read of a column from itself. */
|
|
2192
|
+
function addDirection(d, m) {
|
|
2193
|
+
if (m.t === "offset" || m.t === "window") {
|
|
2194
|
+
const steps = m.t === "offset" ? [m.d.find((x) => x !== 0) ?? 0] : [m.from, m.to];
|
|
2195
|
+
if (steps.some((x) => x < 0)) d.add("before");
|
|
2196
|
+
if (steps.some((x) => x > 0)) d.add("after");
|
|
2197
|
+
} else if (m.t === "children" || m.t === "descendants") d.add("down");
|
|
2198
|
+
}
|
|
2199
|
+
/** This function gives the base axis of an axis, and the tests of its `where` axes. */
|
|
2200
|
+
function partsOf(axis) {
|
|
2201
|
+
const tests = [];
|
|
2202
|
+
let base = axis;
|
|
2203
|
+
while (base.t === "where") {
|
|
2204
|
+
tests.push(base.test);
|
|
2205
|
+
base = base.axis;
|
|
2206
|
+
}
|
|
2207
|
+
return {
|
|
2208
|
+
base,
|
|
2209
|
+
tests
|
|
2210
|
+
};
|
|
2211
|
+
}
|
|
2212
|
+
/**
|
|
2213
|
+
* This function gives the columns that the formulas of the column `self` read with a cell reference: its formula and
|
|
2214
|
+
* the formulas of its cells. It also gives the directions of their reads of the column itself. A read in the body or
|
|
2215
|
+
* in a test of an axis also has the direction of that axis. It walks each expression with a stack, not with recursion.
|
|
2216
|
+
*/
|
|
2217
|
+
function readsOf(t, self) {
|
|
2218
|
+
const columns = /* @__PURE__ */ new Set();
|
|
2219
|
+
const directions = /* @__PURE__ */ new Set();
|
|
2220
|
+
const formula = t.columns.get(self);
|
|
2221
|
+
const todo = [...formula === void 0 ? [] : [formula], ...t.cells.get(self)?.values() ?? []].filter((e) => shapeError(e) === void 0).map((e) => [e, void 0]);
|
|
2222
|
+
for (let item = todo.pop(); item !== void 0; item = todo.pop()) {
|
|
2223
|
+
const [e, axis] = item;
|
|
2224
|
+
if (e.tag === "each" || e.tag === "fold") {
|
|
2225
|
+
const { base, tests } = partsOf(e.axis);
|
|
2226
|
+
for (const x of [...e.tag === "each" ? [e.body] : Object.values(e.step), ...tests]) todo.push([x, base]);
|
|
2227
|
+
for (const x of [...e.tag === "fold" ? [...Object.values(e.init), e.result] : [], ...addrExprs(e.at)]) todo.push([x, axis]);
|
|
2228
|
+
} else if (e.tag !== "ext") for (const child of children(e)) todo.push([child, axis]);
|
|
2229
|
+
else if (e.kind === "vex.cell" && isCellRef(e.data)) {
|
|
2230
|
+
columns.add(e.data.column);
|
|
2231
|
+
if (e.data.column === self) for (const m of [...axis === void 0 ? [] : [axis], ...e.data.at]) addDirection(directions, m);
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
return {
|
|
2235
|
+
columns,
|
|
2236
|
+
directions
|
|
2237
|
+
};
|
|
2238
|
+
}
|
|
2239
|
+
/**
|
|
2240
|
+
* This function gives the columns of a table in an order where each column comes after the columns that it reads.
|
|
2241
|
+
* The columns of a cycle keep their order. The columns with formulas of cells only come last.
|
|
2242
|
+
*/
|
|
2243
|
+
function columnOrder(t) {
|
|
2244
|
+
const names = /* @__PURE__ */ new Set([...t.columns.keys(), ...t.cells.keys()]);
|
|
2245
|
+
let left = [...names].map((c) => ({
|
|
2246
|
+
c,
|
|
2247
|
+
needs: [...readsOf(t, c).columns].filter((x) => x !== c && names.has(x))
|
|
2248
|
+
}));
|
|
2249
|
+
const order = [];
|
|
2250
|
+
const placed = /* @__PURE__ */ new Set();
|
|
2251
|
+
while (left.length > 0) {
|
|
2252
|
+
const ready = left.filter((x) => x.needs.every((n) => placed.has(n)));
|
|
2253
|
+
for (const x of ready.length > 0 ? ready : left.slice(0, 1)) {
|
|
2254
|
+
order.push(x.c);
|
|
2255
|
+
placed.add(x.c);
|
|
2256
|
+
}
|
|
2257
|
+
left = left.filter((x) => !placed.has(x.c));
|
|
2258
|
+
}
|
|
2259
|
+
return order;
|
|
2260
|
+
}
|
|
2261
|
+
/**
|
|
2262
|
+
* 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
|
|
2263
|
+
* reverses the order. In a tree, a read of the children puts the children before their parent.
|
|
2264
|
+
*/
|
|
2265
|
+
function keyOrder(s, d) {
|
|
2266
|
+
const reverse = d.has("after") && !d.has("before");
|
|
2267
|
+
const childrenOf = s.childrenOf?.bind(s);
|
|
2268
|
+
if (childrenOf === void 0) return reverse ? s.keys.toReversed() : s.keys;
|
|
2269
|
+
const post = d.has("down");
|
|
2270
|
+
const backwards = post !== reverse;
|
|
2271
|
+
const push = (stack, keys) => {
|
|
2272
|
+
for (const k of backwards ? keys : keys.toReversed()) stack.push(k);
|
|
2273
|
+
};
|
|
2274
|
+
const isChild = new Set(s.keys.flatMap((k) => childrenOf(k)));
|
|
2275
|
+
const stack = [];
|
|
2276
|
+
push(stack, s.keys.filter((k) => !isChild.has(k)));
|
|
2277
|
+
const out = [];
|
|
2278
|
+
for (let k = stack.pop(); k !== void 0; k = stack.pop()) {
|
|
2279
|
+
out.push(k);
|
|
2280
|
+
push(stack, childrenOf(k));
|
|
2281
|
+
}
|
|
2282
|
+
return post ? out.toReversed() : out;
|
|
2283
|
+
}
|
|
1638
2284
|
/**
|
|
1639
2285
|
* The budget of the stack for the recursion of a run, in estimated frames. Each engine has more stack than this, so
|
|
1640
2286
|
* the recursion of a run cannot fill the stack (SHEET.DEEP).
|
|
@@ -1654,84 +2300,175 @@ const STACK_BUDGET = 3e3;
|
|
|
1654
2300
|
* cell, and the cell that reads it pauses. A paused evaluation reads no other cell and changes no state of the
|
|
1655
2301
|
* search. The run then evaluates the started cell from its own list, and it evaluates the paused cell again after
|
|
1656
2302
|
* that cell. A formula is pure (EVAL.PURE), so the second evaluation gives the result of one evaluation.
|
|
2303
|
+
*
|
|
2304
|
+
* A run can have more tables (SHEET.TABLES). Each table has its own space, and one search covers the cells of all
|
|
2305
|
+
* the tables. Thus a cycle across tables gives `#CYCLE!` too.
|
|
1657
2306
|
*/
|
|
1658
|
-
var SheetRun = class {
|
|
1659
|
-
#
|
|
2307
|
+
var SheetRun = class SheetRun {
|
|
2308
|
+
#main;
|
|
2309
|
+
#byName = /* @__PURE__ */ new Map();
|
|
2310
|
+
#bySpace = /* @__PURE__ */ new Map();
|
|
1660
2311
|
#opts;
|
|
1661
|
-
#done = /* @__PURE__ */ new Map();
|
|
1662
|
-
#warmed = /* @__PURE__ */ new Map();
|
|
1663
|
-
#index;
|
|
1664
2312
|
#visits = /* @__PURE__ */ new Map();
|
|
1665
2313
|
#stack = [];
|
|
1666
|
-
#top
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
key: "",
|
|
1670
|
-
index: -1,
|
|
1671
|
-
low: -1,
|
|
1672
|
-
reread: false,
|
|
1673
|
-
result: cycleError("", ""),
|
|
1674
|
-
paused: false,
|
|
1675
|
-
need: void 0
|
|
1676
|
-
};
|
|
2314
|
+
#top;
|
|
2315
|
+
#filling;
|
|
2316
|
+
#missed = false;
|
|
1677
2317
|
#next = 0;
|
|
1678
2318
|
#used = 0;
|
|
1679
2319
|
#cost = /* @__PURE__ */ new Map();
|
|
1680
2320
|
#budget;
|
|
2321
|
+
#compile;
|
|
2322
|
+
#compileOptions;
|
|
2323
|
+
static #active;
|
|
2324
|
+
static #shared = /* @__PURE__ */ new WeakMap();
|
|
1681
2325
|
/** `budget` is the budget of the stack in estimated frames. A test can make it small, so the run pauses often. */
|
|
1682
2326
|
constructor(columns, opts, budget = STACK_BUDGET) {
|
|
1683
|
-
|
|
2327
|
+
const { cells, name, tables, space, compile, ...rest } = opts;
|
|
2328
|
+
this.#main = {
|
|
2329
|
+
where: "the sheet",
|
|
2330
|
+
tag: "0",
|
|
2331
|
+
space,
|
|
2332
|
+
columns,
|
|
2333
|
+
cells: cells ?? /* @__PURE__ */ new Map(),
|
|
2334
|
+
warmed: /* @__PURE__ */ new Map(),
|
|
2335
|
+
index: void 0,
|
|
2336
|
+
done: /* @__PURE__ */ new Map()
|
|
2337
|
+
};
|
|
2338
|
+
this.#bySpace.set(space, this.#main);
|
|
2339
|
+
if (name !== void 0) this.#byName.set(name, this.#main);
|
|
2340
|
+
if (tables !== void 0) {
|
|
2341
|
+
let i = 0;
|
|
2342
|
+
for (const [n, t] of tables) {
|
|
2343
|
+
if (this.#byName.has(n)) continue;
|
|
2344
|
+
const other = tableState(`the table "${n}"`, ++i, t);
|
|
2345
|
+
this.#byName.set(n, other);
|
|
2346
|
+
if (!this.#bySpace.has(t.space)) this.#bySpace.set(t.space, other);
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
this.#top = {
|
|
2350
|
+
id: "",
|
|
2351
|
+
table: this.#main,
|
|
2352
|
+
column: "",
|
|
2353
|
+
key: "",
|
|
2354
|
+
index: -1,
|
|
2355
|
+
low: -1,
|
|
2356
|
+
reread: false,
|
|
2357
|
+
result: cycleError("", ""),
|
|
2358
|
+
paused: false,
|
|
2359
|
+
need: void 0
|
|
2360
|
+
};
|
|
2361
|
+
this.#filling = {
|
|
2362
|
+
id: "",
|
|
2363
|
+
table: this.#main,
|
|
2364
|
+
column: "",
|
|
2365
|
+
key: "",
|
|
2366
|
+
index: 0,
|
|
2367
|
+
low: 0,
|
|
2368
|
+
reread: false,
|
|
2369
|
+
result: cycleError("", ""),
|
|
2370
|
+
paused: false,
|
|
2371
|
+
need: void 0
|
|
2372
|
+
};
|
|
1684
2373
|
this.#budget = budget;
|
|
2374
|
+
this.#compile = compile;
|
|
2375
|
+
const spaces = Object.fromEntries([...this.#byName].map(([n, t]) => [n, t.space]));
|
|
1685
2376
|
this.#opts = {
|
|
1686
|
-
...
|
|
2377
|
+
...rest,
|
|
2378
|
+
tables: spaces,
|
|
1687
2379
|
extensions: {
|
|
1688
|
-
...
|
|
2380
|
+
...rest.extensions,
|
|
1689
2381
|
[CELL_KIND]: this.#handler
|
|
1690
2382
|
}
|
|
1691
2383
|
};
|
|
2384
|
+
const outer = rest.extensions ?? NO_EXTENSIONS;
|
|
2385
|
+
let shared = SheetRun.#shared.get(outer);
|
|
2386
|
+
if (shared === void 0) {
|
|
2387
|
+
shared = {
|
|
2388
|
+
...rest.extensions,
|
|
2389
|
+
[CELL_KIND]: SheetRun.#cellOfActive
|
|
2390
|
+
};
|
|
2391
|
+
SheetRun.#shared.set(outer, shared);
|
|
2392
|
+
}
|
|
2393
|
+
this.#compileOptions = {
|
|
2394
|
+
...this.#opts,
|
|
2395
|
+
extensions: shared
|
|
2396
|
+
};
|
|
2397
|
+
}
|
|
2398
|
+
/** The cell handler of the compiled programs: it reads the cells of the active run. */
|
|
2399
|
+
static #cellOfActive = (data, ctx) => {
|
|
2400
|
+
const run = SheetRun.#active;
|
|
2401
|
+
return run === void 0 ? fail(vexError("unbound", "a compiled formula reads cells only in the evaluation of a sheet run")) : run.#handler(data, ctx);
|
|
2402
|
+
};
|
|
2403
|
+
/** This method gives the program of a cell: its own formula, or the formula of its column (SHEET.CELL.FORMULA). */
|
|
2404
|
+
#programOf(t, column, key) {
|
|
2405
|
+
return (t.cells.size === 0 ? void 0 : t.cells.get(column)?.get(key)) ?? t.columns.get(column);
|
|
2406
|
+
}
|
|
2407
|
+
/** This method gives the error of a cell without a program. */
|
|
2408
|
+
#unbound(t, column, key) {
|
|
2409
|
+
const message = t.cells.has(column) ? `the cell "${column}" at "${key}" has no formula` : `${t.where} has no column "${column}"`;
|
|
2410
|
+
return fail(vexError("unbound", message, {
|
|
2411
|
+
origin: key,
|
|
2412
|
+
focus: key
|
|
2413
|
+
}));
|
|
2414
|
+
}
|
|
2415
|
+
/** This method gives the table of a name for a read from a host. Without a name, it is the main table. */
|
|
2416
|
+
#named(table, key) {
|
|
2417
|
+
if (table === void 0) return this.#main;
|
|
2418
|
+
return this.#byName.get(table) ?? fail(vexError("no-table", `the run has no table "${table}"`, {
|
|
2419
|
+
origin: key,
|
|
2420
|
+
focus: key
|
|
2421
|
+
}));
|
|
1692
2422
|
}
|
|
1693
2423
|
#handler = (data, ctx) => {
|
|
1694
|
-
if (!isCellRef(data)) return fail(vexError("bad-expression", "a cell reference needs a column name and an address
|
|
2424
|
+
if (!isCellRef(data)) return fail(vexError("bad-expression", "a cell reference needs a column name and an address of fixed moves"));
|
|
1695
2425
|
const where = resolveAddr(ctx.space, ctx.position, data.at);
|
|
1696
2426
|
if (!where.ok) return fail({
|
|
1697
2427
|
...where.error,
|
|
1698
2428
|
path: ctx.path
|
|
1699
2429
|
});
|
|
1700
|
-
|
|
2430
|
+
/* v8 ignore next -- @preserve: each space of an evaluation of the run is the space of a table */
|
|
2431
|
+
return this.#read(this.#bySpace.get(ctx.space) ?? this.#main, data.column, where.value);
|
|
1701
2432
|
};
|
|
1702
2433
|
/**
|
|
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
|
|
2434
|
+
* This method gives the result of one cell: the column `column` at the key `key` of the main table, or of the table
|
|
2435
|
+
* `table`. A read from a host first evaluates the column at the keys before `key`, in key order. Thus a recurrence
|
|
2436
|
+
* over the keys reads finished cells.
|
|
1705
2437
|
*/
|
|
1706
|
-
cell(column, key) {
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
return this.#
|
|
2438
|
+
cell(column, key, table) {
|
|
2439
|
+
const t = this.#named(table, key);
|
|
2440
|
+
if (!("space" in t)) return t;
|
|
2441
|
+
if (this.#top.index >= 0) return this.#read(t, column, key);
|
|
2442
|
+
const done = doneOf(t, column, key);
|
|
2443
|
+
if (done !== void 0) return done;
|
|
2444
|
+
this.#warmTo(t, column, key);
|
|
2445
|
+
return this.#drive(t, column, key);
|
|
1710
2446
|
}
|
|
1711
2447
|
/** 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(
|
|
2448
|
+
#warmTo(t, column, key) {
|
|
2449
|
+
if (!t.columns.has(column)) return;
|
|
2450
|
+
const keys = t.space.keys;
|
|
2451
|
+
t.index ??= new Map(keys.map((k, i) => [k, i]));
|
|
2452
|
+
const end = t.index.get(key) ?? 0;
|
|
2453
|
+
const start = t.warmed.get(column) ?? 0;
|
|
2454
|
+
for (let i = start; i < end; i++) this.#drive(t, column, keys[i] ?? key);
|
|
2455
|
+
if (end > start) t.warmed.set(column, end);
|
|
2456
|
+
}
|
|
2457
|
+
/** This method gives the estimated frames of the stack that one evaluation of the program `program` uses. */
|
|
2458
|
+
#costOf(program) {
|
|
2459
|
+
let cost = this.#cost.get(program);
|
|
1724
2460
|
if (cost === void 0) {
|
|
1725
|
-
cost = 3 * nestingOf(program) + 16;
|
|
1726
|
-
this.#cost.set(
|
|
2461
|
+
cost = 3 * (shapeError(program) === void 0 ? nestingOf(program) : 1) + 16;
|
|
2462
|
+
this.#cost.set(program, cost);
|
|
1727
2463
|
}
|
|
1728
2464
|
return cost;
|
|
1729
2465
|
}
|
|
1730
2466
|
/** This method puts a new cell on the stack of the search. */
|
|
1731
|
-
#start(column, key) {
|
|
2467
|
+
#start(t, column, key) {
|
|
1732
2468
|
const n = this.#next++;
|
|
1733
2469
|
const visit = {
|
|
1734
|
-
id: idOf(column, key),
|
|
2470
|
+
id: idOf(t, column, key),
|
|
2471
|
+
table: t,
|
|
1735
2472
|
column,
|
|
1736
2473
|
key,
|
|
1737
2474
|
index: n,
|
|
@@ -1748,44 +2485,77 @@ var SheetRun = class {
|
|
|
1748
2485
|
/** This method evaluates the formula of a cell once. When the evaluation pauses, the result of the cell does not change. */
|
|
1749
2486
|
#evaluate(visit, program) {
|
|
1750
2487
|
const caller = this.#top;
|
|
1751
|
-
const cost = this.#costOf(
|
|
2488
|
+
const cost = this.#costOf(program);
|
|
1752
2489
|
visit.paused = false;
|
|
1753
2490
|
visit.need = void 0;
|
|
1754
2491
|
this.#top = visit;
|
|
1755
2492
|
this.#used += cost;
|
|
1756
|
-
const result =
|
|
1757
|
-
...this.#opts,
|
|
1758
|
-
origin: visit.key
|
|
1759
|
-
});
|
|
2493
|
+
const result = this.#run(visit.table, visit.column, visit.key, program);
|
|
1760
2494
|
this.#used -= cost;
|
|
1761
2495
|
this.#top = caller;
|
|
1762
2496
|
if (!visit.paused) visit.result = result;
|
|
1763
2497
|
}
|
|
2498
|
+
/** This method evaluates the formula `program` of a cell, with its compiled program or with the interpreter. */
|
|
2499
|
+
#run(t, column, key, program) {
|
|
2500
|
+
const options = {
|
|
2501
|
+
...this.#opts,
|
|
2502
|
+
space: t.space,
|
|
2503
|
+
origin: key
|
|
2504
|
+
};
|
|
2505
|
+
const compiled = this.#compiled(t, column, program);
|
|
2506
|
+
if (compiled === void 0) return evaluate(program, options);
|
|
2507
|
+
const active = SheetRun.#active;
|
|
2508
|
+
SheetRun.#active = this;
|
|
2509
|
+
const result = compiled.run(options);
|
|
2510
|
+
SheetRun.#active = active;
|
|
2511
|
+
return result;
|
|
2512
|
+
}
|
|
2513
|
+
/**
|
|
2514
|
+
* This method gives the compiled program of the formula of a column, or `undefined` for the interpreter. A formula
|
|
2515
|
+
* compiles once for each compiler and options, also across runs.
|
|
2516
|
+
*/
|
|
2517
|
+
#compiled(t, column, program) {
|
|
2518
|
+
const compile = this.#compile;
|
|
2519
|
+
if (compile === void 0 || t.columns.get(column) !== program) return void 0;
|
|
2520
|
+
const { domains, fns, extensions } = this.#compileOptions;
|
|
2521
|
+
const hit = PROGRAMS.get(program);
|
|
2522
|
+
if (hit !== void 0 && hit.compile === compile && hit.domains === domains && hit.fns === fns && hit.extensions === extensions) return hit.program;
|
|
2523
|
+
const compiled = compile(program, this.#compileOptions);
|
|
2524
|
+
PROGRAMS.set(program, {
|
|
2525
|
+
compile,
|
|
2526
|
+
domains,
|
|
2527
|
+
fns,
|
|
2528
|
+
extensions,
|
|
2529
|
+
program: compiled
|
|
2530
|
+
});
|
|
2531
|
+
return compiled;
|
|
2532
|
+
}
|
|
1764
2533
|
/** This method ends a cell whose evaluation did not pause: it finishes its component, or it gives its low to the caller. */
|
|
1765
2534
|
#end(visit, caller) {
|
|
1766
2535
|
if (visit.low === visit.index) this.#finish(visit);
|
|
1767
2536
|
else if (caller !== void 0) caller.low = Math.min(caller.low, visit.low);
|
|
1768
2537
|
}
|
|
1769
2538
|
/** This method reads a cell from the formula of the cell at the top. */
|
|
1770
|
-
#read(column, key) {
|
|
1771
|
-
const
|
|
1772
|
-
const done = this.#done.get(id);
|
|
2539
|
+
#read(t, column, key) {
|
|
2540
|
+
const done = doneOf(t, column, key);
|
|
1773
2541
|
if (done !== void 0) return done;
|
|
1774
2542
|
const caller = this.#top;
|
|
1775
2543
|
if (caller.paused) return cycleError(column, key);
|
|
2544
|
+
if (caller === this.#filling) {
|
|
2545
|
+
this.#missed = true;
|
|
2546
|
+
return cycleError(column, key);
|
|
2547
|
+
}
|
|
2548
|
+
const id = idOf(t, column, key);
|
|
1776
2549
|
const seen = this.#visits.get(id);
|
|
1777
2550
|
if (seen !== void 0) {
|
|
1778
2551
|
caller.low = Math.min(caller.low, seen.index);
|
|
1779
2552
|
seen.reread = true;
|
|
1780
2553
|
return cycleError(column, key);
|
|
1781
2554
|
}
|
|
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) {
|
|
2555
|
+
const program = this.#programOf(t, column, key);
|
|
2556
|
+
if (program === void 0) return this.#unbound(t, column, key);
|
|
2557
|
+
const visit = this.#start(t, column, key);
|
|
2558
|
+
if (this.#used + this.#costOf(program) > this.#budget) {
|
|
1789
2559
|
caller.paused = true;
|
|
1790
2560
|
caller.need = visit;
|
|
1791
2561
|
return cycleError(column, key);
|
|
@@ -1797,21 +2567,17 @@ var SheetRun = class {
|
|
|
1797
2567
|
return cycleError(column, key);
|
|
1798
2568
|
}
|
|
1799
2569
|
this.#end(visit, caller);
|
|
1800
|
-
return
|
|
2570
|
+
return doneOf(t, column, key) ?? cycleError(column, key);
|
|
1801
2571
|
}
|
|
1802
2572
|
/** 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
|
|
1805
|
-
const done = this.#done.get(id);
|
|
2573
|
+
#drive(t, column, key) {
|
|
2574
|
+
const done = doneOf(t, column, key);
|
|
1806
2575
|
if (done !== void 0) return done;
|
|
1807
|
-
if (
|
|
1808
|
-
|
|
1809
|
-
focus: key
|
|
1810
|
-
}));
|
|
1811
|
-
const work = [this.#start(column, key)];
|
|
2576
|
+
if (this.#programOf(t, column, key) === void 0) return this.#unbound(t, column, key);
|
|
2577
|
+
const work = [this.#start(t, column, key)];
|
|
1812
2578
|
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
|
|
2579
|
+
const program = this.#programOf(visit.table, visit.column, visit.key);
|
|
2580
|
+
/* v8 ignore next -- @preserve: a visit exists only for a cell with a program */
|
|
1815
2581
|
if (program === void 0) break;
|
|
1816
2582
|
this.#evaluate(visit, program);
|
|
1817
2583
|
if (visit.paused) {
|
|
@@ -1821,7 +2587,7 @@ var SheetRun = class {
|
|
|
1821
2587
|
work.pop();
|
|
1822
2588
|
this.#end(visit, work.at(-1));
|
|
1823
2589
|
}
|
|
1824
|
-
return
|
|
2590
|
+
return doneOf(t, column, key) ?? cycleError(column, key);
|
|
1825
2591
|
}
|
|
1826
2592
|
/** This method takes the component of `root` from the stack, and sets the final result of each of its cells. */
|
|
1827
2593
|
#finish(root) {
|
|
@@ -1829,31 +2595,55 @@ var SheetRun = class {
|
|
|
1829
2595
|
const cyclic = members.length > 1 || root.reread;
|
|
1830
2596
|
for (const m of members) {
|
|
1831
2597
|
this.#visits.delete(m.id);
|
|
1832
|
-
|
|
2598
|
+
setDone(m.table, m.column, m.key, cyclic ? cycleError(m.column, m.key) : m.result);
|
|
1833
2599
|
}
|
|
1834
2600
|
}
|
|
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
|
-
|
|
2601
|
+
/** This method evaluates one cell with a trace of the nodes of its program. The result is the result of the cell. */
|
|
2602
|
+
explain(column, key, table) {
|
|
2603
|
+
const result = this.cell(column, key, table);
|
|
2604
|
+
const t = this.#named(table, key);
|
|
2605
|
+
const program = "space" in t ? this.#programOf(t, column, key) : void 0;
|
|
2606
|
+
if (program === void 0 || !("space" in t)) return {
|
|
1840
2607
|
events: [],
|
|
1841
2608
|
result
|
|
1842
2609
|
};
|
|
1843
2610
|
return {
|
|
1844
2611
|
events: explain(program, {
|
|
1845
2612
|
...this.#opts,
|
|
2613
|
+
space: t.space,
|
|
1846
2614
|
origin: key
|
|
1847
2615
|
}).events,
|
|
1848
2616
|
result
|
|
1849
2617
|
};
|
|
1850
2618
|
}
|
|
1851
2619
|
/**
|
|
2620
|
+
* This method evaluates each cell of the table `table`, or of the main table (SHEET.FILL). Each column comes after
|
|
2621
|
+
* the columns that it reads, and the keys of a column come in the order of its reads from itself. Thus most reads
|
|
2622
|
+
* find a finished cell. An evaluation that reads a cell that is not finished has no effect, and the search then
|
|
2623
|
+
* evaluates its cell. A read of a cell gives the same result after a fill.
|
|
2624
|
+
*/
|
|
2625
|
+
fill(table) {
|
|
2626
|
+
const t = table === void 0 ? this.#main : this.#byName.get(table);
|
|
2627
|
+
if (t === void 0 || this.#top.index >= 0) return;
|
|
2628
|
+
const host = this.#top;
|
|
2629
|
+
for (const column of columnOrder(t)) for (const key of keyOrder(t.space, readsOf(t, column).directions)) {
|
|
2630
|
+
const program = this.#programOf(t, column, key);
|
|
2631
|
+
if (program === void 0 || doneOf(t, column, key) !== void 0) continue;
|
|
2632
|
+
this.#top = this.#filling;
|
|
2633
|
+
this.#missed = false;
|
|
2634
|
+
const result = this.#run(t, column, key, program);
|
|
2635
|
+
this.#top = host;
|
|
2636
|
+
if (this.#missed) this.#drive(t, column, key);
|
|
2637
|
+
else setDone(t, column, key, result);
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
/**
|
|
1852
2641
|
* This method evaluates the column `column` at each key before `key`, in key order. A recurrence over the keys,
|
|
1853
2642
|
* for example a running total, then reads finished cells.
|
|
1854
2643
|
*/
|
|
1855
|
-
warm(column, key) {
|
|
1856
|
-
this.#
|
|
2644
|
+
warm(column, key, table) {
|
|
2645
|
+
const t = this.#named(table, key);
|
|
2646
|
+
if ("space" in t) this.#warmTo(t, column, key);
|
|
1857
2647
|
}
|
|
1858
2648
|
};
|
|
1859
2649
|
//#endregion
|
|
@@ -1877,6 +2667,16 @@ function toExpr(a, addr) {
|
|
|
1877
2667
|
if (isChainLike(a)) return a.program;
|
|
1878
2668
|
return lit(a);
|
|
1879
2669
|
}
|
|
2670
|
+
/** This function gives the expressions of a step of a loop, with the unique names of the loop. */
|
|
2671
|
+
function stepExprs(step, unique, addr) {
|
|
2672
|
+
const out = {};
|
|
2673
|
+
for (const [n, a] of Object.entries(step)) {
|
|
2674
|
+
const name = unique.get(n);
|
|
2675
|
+
if (name === void 0) throw new TypeError(`the name "${n}" of the step is not a name of init`);
|
|
2676
|
+
out[name] = toExpr(a, addr);
|
|
2677
|
+
}
|
|
2678
|
+
return out;
|
|
2679
|
+
}
|
|
1880
2680
|
const PROXY_RESERVED = /* @__PURE__ */ new Set([
|
|
1881
2681
|
"then",
|
|
1882
2682
|
"catch",
|
|
@@ -1944,10 +2744,10 @@ var ChainImpl = class ChainImpl {
|
|
|
1944
2744
|
});
|
|
1945
2745
|
}
|
|
1946
2746
|
to(k) {
|
|
1947
|
-
return this.#move(key(k));
|
|
2747
|
+
return this.#move(key(typeof k === "string" ? k : k.program));
|
|
1948
2748
|
}
|
|
1949
2749
|
index(i) {
|
|
1950
|
-
return this.#move(index(i));
|
|
2750
|
+
return this.#move(index(typeof i === "number" ? i : i.program));
|
|
1951
2751
|
}
|
|
1952
2752
|
offset(...d) {
|
|
1953
2753
|
return this.#move(offset(...d));
|
|
@@ -2018,6 +2818,41 @@ var ChainImpl = class ChainImpl {
|
|
|
2018
2818
|
expr: let_(bindExprs, inner.program)
|
|
2019
2819
|
});
|
|
2020
2820
|
}
|
|
2821
|
+
/**
|
|
2822
|
+
* This method gives the names of a loop. Each name of `init` gets a name that is unique at the depth of the chain.
|
|
2823
|
+
* It also gets its first value at the address of the chain, and a chain of its value.
|
|
2824
|
+
*/
|
|
2825
|
+
loopNames(init) {
|
|
2826
|
+
const depth = this.#s.depth;
|
|
2827
|
+
const unique = /* @__PURE__ */ new Map();
|
|
2828
|
+
const first = {};
|
|
2829
|
+
const vars = {};
|
|
2830
|
+
for (const [n, a] of Object.entries(init)) {
|
|
2831
|
+
const name = `${n}$${depth}`;
|
|
2832
|
+
unique.set(n, name);
|
|
2833
|
+
first[name] = toExpr(a, this.#s.addr);
|
|
2834
|
+
vars[n] = new ChainImpl({
|
|
2835
|
+
...this.#s,
|
|
2836
|
+
expr: v(name),
|
|
2837
|
+
addr: [],
|
|
2838
|
+
depth: depth + 1
|
|
2839
|
+
});
|
|
2840
|
+
}
|
|
2841
|
+
return {
|
|
2842
|
+
unique,
|
|
2843
|
+
first,
|
|
2844
|
+
vars
|
|
2845
|
+
};
|
|
2846
|
+
}
|
|
2847
|
+
iterate(limit, init, step, result, opts) {
|
|
2848
|
+
const { unique, first, vars } = this.loopNames(init);
|
|
2849
|
+
const next = stepExprs(step(vars), unique, this.#s.addr);
|
|
2850
|
+
const until = opts?.until === void 0 ? void 0 : toExpr(opts.until(vars), this.#s.addr);
|
|
2851
|
+
return new ChainImpl({
|
|
2852
|
+
...this.#s,
|
|
2853
|
+
expr: iterate(limit, first, next, toExpr(result(vars), this.#s.addr), until)
|
|
2854
|
+
});
|
|
2855
|
+
}
|
|
2021
2856
|
#axis(a, body, opts) {
|
|
2022
2857
|
const { name, start } = this.#bound();
|
|
2023
2858
|
const inTarget = new ChainImpl({
|
|
@@ -2059,6 +2894,28 @@ var ChainImpl = class ChainImpl {
|
|
|
2059
2894
|
siblings(body, opts) {
|
|
2060
2895
|
return this.#axis({ t: "siblings" }, body, opts);
|
|
2061
2896
|
}
|
|
2897
|
+
window(from, to, body, opts) {
|
|
2898
|
+
return this.#axis({
|
|
2899
|
+
t: "window",
|
|
2900
|
+
from,
|
|
2901
|
+
to
|
|
2902
|
+
}, body, opts);
|
|
2903
|
+
}
|
|
2904
|
+
table(name, body, opts) {
|
|
2905
|
+
return this.#axis({
|
|
2906
|
+
t: "table",
|
|
2907
|
+
name
|
|
2908
|
+
}, body, opts);
|
|
2909
|
+
}
|
|
2910
|
+
successors(body, opts) {
|
|
2911
|
+
return this.#axis({ t: "successors" }, body, opts);
|
|
2912
|
+
}
|
|
2913
|
+
predecessors(body, opts) {
|
|
2914
|
+
return this.#axis({ t: "predecessors" }, body, opts);
|
|
2915
|
+
}
|
|
2916
|
+
reachable(body, opts) {
|
|
2917
|
+
return this.#axis({ t: "reachable" }, body, opts);
|
|
2918
|
+
}
|
|
2062
2919
|
};
|
|
2063
2920
|
/** This function gives the options of an evaluation of a chain or a list chain at the origin `k`. */
|
|
2064
2921
|
function evalOptions(s, k) {
|
|
@@ -2080,6 +2937,23 @@ var ListChainImpl = class {
|
|
|
2080
2937
|
get program() {
|
|
2081
2938
|
return this.#s.expr;
|
|
2082
2939
|
}
|
|
2940
|
+
fold(init, step, result) {
|
|
2941
|
+
const { bind, body: loop } = this.#s.expr;
|
|
2942
|
+
const names = new ChainImpl({
|
|
2943
|
+
...this.#s,
|
|
2944
|
+
expr: lit(null)
|
|
2945
|
+
}).loopNames(init);
|
|
2946
|
+
const item = new ChainImpl({
|
|
2947
|
+
...this.#s,
|
|
2948
|
+
expr: loop.body,
|
|
2949
|
+
addr: []
|
|
2950
|
+
});
|
|
2951
|
+
const next = stepExprs(step(names.vars, item), names.unique, []);
|
|
2952
|
+
return new ChainImpl({
|
|
2953
|
+
...this.#s,
|
|
2954
|
+
expr: let_(bind, fold(loop.axis, names.first, next, toExpr(result(names.vars), this.#s.addr), loop.at))
|
|
2955
|
+
});
|
|
2956
|
+
}
|
|
2083
2957
|
#op(op, extra, opts) {
|
|
2084
2958
|
const options = {
|
|
2085
2959
|
...opts?.strict === true ? { strict: true } : {},
|
|
@@ -2194,25 +3068,36 @@ var SheetRootImpl = class extends RootImpl {
|
|
|
2194
3068
|
var SheetImpl = class SheetImpl {
|
|
2195
3069
|
#env;
|
|
2196
3070
|
#columns;
|
|
2197
|
-
|
|
3071
|
+
#cells;
|
|
3072
|
+
constructor(env, columns, cells = /* @__PURE__ */ new Map()) {
|
|
2198
3073
|
this.#env = env;
|
|
2199
3074
|
this.#columns = columns;
|
|
3075
|
+
this.#cells = cells;
|
|
2200
3076
|
}
|
|
2201
3077
|
get columns() {
|
|
2202
3078
|
return Object.freeze(Object.fromEntries(this.#columns));
|
|
2203
3079
|
}
|
|
3080
|
+
get cellFormulas() {
|
|
3081
|
+
return Object.freeze(Object.fromEntries([...this.#cells].map(([c, keys]) => [c, Object.freeze(Object.fromEntries(keys))])));
|
|
3082
|
+
}
|
|
2204
3083
|
declare() {
|
|
2205
3084
|
return this;
|
|
2206
3085
|
}
|
|
2207
3086
|
column(name, formula) {
|
|
2208
3087
|
const program = formula(new SheetRootImpl(this.#env)).program;
|
|
2209
|
-
return new SheetImpl(this.#env, new Map([...this.#columns, [name, program]]));
|
|
3088
|
+
return new SheetImpl(this.#env, new Map([...this.#columns, [name, program]]), this.#cells);
|
|
3089
|
+
}
|
|
3090
|
+
cellFormula(k, column, formula) {
|
|
3091
|
+
const program = formula(new SheetRootImpl(this.#env)).program;
|
|
3092
|
+
const keys = new Map([...this.#cells.get(column) ?? [], [k, program]]);
|
|
3093
|
+
return new SheetImpl(this.#env, this.#columns, new Map([...this.#cells, [column, keys]]));
|
|
2210
3094
|
}
|
|
2211
3095
|
#run() {
|
|
2212
3096
|
const { space: s, domains, options } = this.#env;
|
|
2213
3097
|
return new SheetRun(this.#columns, {
|
|
2214
3098
|
space: s,
|
|
2215
3099
|
domains,
|
|
3100
|
+
cells: this.#cells,
|
|
2216
3101
|
...options.fns === void 0 ? {} : { fns: options.fns },
|
|
2217
3102
|
...options.extensions === void 0 ? {} : { extensions: options.extensions }
|
|
2218
3103
|
});
|
|
@@ -2232,6 +3117,7 @@ var SheetImpl = class SheetImpl {
|
|
|
2232
3117
|
}
|
|
2233
3118
|
table() {
|
|
2234
3119
|
const run = this.#run();
|
|
3120
|
+
run.fill();
|
|
2235
3121
|
const names = [...this.#columns.keys()];
|
|
2236
3122
|
return this.#env.space.keys.map((key) => ({
|
|
2237
3123
|
key,
|
|
@@ -2258,6 +3144,6 @@ function vex(...domains) {
|
|
|
2258
3144
|
};
|
|
2259
3145
|
}
|
|
2260
3146
|
//#endregion
|
|
2261
|
-
export { CELL_KIND, LIST_OPS, MAX_NESTING, SPECIAL_FORMS, SheetRun, app, applyMove, axes, axisTargets, cell, chainResult, children, codeOf, compile, defineDomain, deps, describeType, domainOf, each, evaluate, explain, ext, fail, findMethod, formatError, formatTrace, getOr, index, isAxis, isDepthError, isExpr, isListOp, isMove, isNone, isOpName, isSome, isStackOverflow, isVexList, key, labelOf, let_, liftsAt, lit, mapResult, monoids, nestingOf, none, offset, ok, origin, other, parent, parse, previewValue, rec, ref, resolveAddr, resolveOp, serialize, sizeOf, some, space, toOptional, toPath, traversal, v, vex, vexError, vexList };
|
|
3147
|
+
export { CELL_KIND, LIST_OPS, MAX_ITERATIONS, MAX_NESTING, SPECIAL_FORMS, SheetRun, addrExprs, app, applyMove, axes, axisTargets, cell, chainResult, children, codeOf, compile, defineDomain, deps, describeType, domainOf, each, evaluate, explain, ext, fail, findMethod, fixAddr, fold, formatError, formatTrace, getOr, index, isAxis, isDepthError, isExpr, isFieldName, isFixedAddr, isListOp, isMove, isNone, isOpName, isSome, isStackOverflow, isVexList, iterate, key, labelOf, let_, liftsAt, lit, mapResult, matchesKind, monoids, moveExpr, nestingOf, none, offset, ok, origin, other, parent, parse, previewValue, rec, ref, resolveAddr, resolveOp, safeIs, serialize, shapeError, sizeOf, some, space, toOptional, toPath, traversal, v, vex, vexError, vexList };
|
|
2262
3148
|
|
|
2263
3149
|
//# sourceMappingURL=index.js.map
|