@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/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/vex",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"description": "Vex: typed spreadsheet formulas over domain objects. The kernel and the typed builder.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"typescript",
|
package/src/builder.ts
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
import type { AnyDomain, Domain, OpSpec } from "./domain.ts";
|
|
6
6
|
import type { VexError } from "./errors.ts";
|
|
7
7
|
import { evaluate, explain, type EvalOptions, type ExtHandler, type FreeFn } from "./evaluate.ts";
|
|
8
|
-
import { app, each, ext, index as indexMove, key as keyMove, let_, lit, offset as offsetMove, other as otherMove, origin as originMove, parent as parentMove, rec, ref, toPath, v, type
|
|
9
|
-
import { cell as cellRef, SheetRun } from "./sheet.ts";
|
|
8
|
+
import { app, each, ext, fold as foldExpr, index as indexMove, iterate as iterateExpr, key as keyMove, let_, lit, offset as offsetMove, other as otherMove, origin as originMove, parent as parentMove, rec, ref, toPath, v, type Axis, type Expr, type ExprOf, type FixedMove, type Move } from "./ir.ts";
|
|
9
|
+
import { cell as cellRef, SheetRun, type CellFormulas } from "./sheet.ts";
|
|
10
10
|
import { toOptional, type Optional, type Result } from "./result.ts";
|
|
11
11
|
import type { Space } from "./space.ts";
|
|
12
12
|
import type { Trace } from "./trace.ts";
|
|
@@ -178,10 +178,16 @@ export interface ChainBase<Ctx extends ChainContext, T> {
|
|
|
178
178
|
all(): Traversal<Ctx["keys"], T>;
|
|
179
179
|
/** This method gives a chain whose value is a field of the record at the current address. */
|
|
180
180
|
from<P extends keyof Ctx["record"] & string>(field: P): ChainOf<Ctx, NonNullable<Ctx["record"][P]>>;
|
|
181
|
-
/**
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Navigation: later field references read at the key `k`. A chain of a text gives the key, and it evaluates at the
|
|
183
|
+
* focus before the moves (NAV.COMPUTED).
|
|
184
|
+
*/
|
|
185
|
+
to(k: Ctx["keys"] | ChainBase<Ctx, string>): ChainOf<Ctx, T>;
|
|
186
|
+
/**
|
|
187
|
+
* Navigation: later field references read at the key with this position in the key order. A chain of a number gives
|
|
188
|
+
* the position, and it evaluates at the focus before the moves (NAV.COMPUTED).
|
|
189
|
+
*/
|
|
190
|
+
index(i: number | ChainBase<Ctx, number>): ChainOf<Ctx, T>;
|
|
185
191
|
/** Navigation: later field references read at a relative position in an array or a grid. */
|
|
186
192
|
offset(...d: readonly number[]): ChainOf<Ctx, T>;
|
|
187
193
|
/** Navigation: later field references read at the origin again. */
|
|
@@ -202,6 +208,17 @@ export interface ChainBase<Ctx extends ChainContext, T> {
|
|
|
202
208
|
call<N extends CallableFns<Ctx, T>>(name: N, ...args: CallArgs<Ctx, N>): ChainOf<Ctx, CallReturn<Ctx, N>>;
|
|
203
209
|
/** This method evaluates each branch with the current value, and gives a record of the results. */
|
|
204
210
|
fork<const B extends Readonly<Record<string, (c: ChainOf<Ctx, T>) => ChainBase<Ctx, unknown>>>>(branches: B): ChainOf<Ctx, { readonly [N in keyof B]: ValueOf<ReturnType<B[N]>> }>;
|
|
211
|
+
/**
|
|
212
|
+
* This method makes up to `limit` steps (ITERATE.LIMIT). `init` gives the names and their first values, and `step`
|
|
213
|
+
* gives the next values. A true `until` stops the steps (ITERATE.UNTIL). `result` gives the value of the chain.
|
|
214
|
+
*/
|
|
215
|
+
iterate<const I extends Readonly<Record<string, unknown>>, R>(
|
|
216
|
+
limit: number,
|
|
217
|
+
init: I,
|
|
218
|
+
step: (vars: LoopVars<Ctx, I>) => NoInfer<LoopStep<Ctx, I>>,
|
|
219
|
+
result: (vars: LoopVars<Ctx, I>) => ArgInput<R, Ctx, false>,
|
|
220
|
+
opts?: { readonly until?: (vars: LoopVars<Ctx, I>) => ArgInput<boolean, Ctx, false> },
|
|
221
|
+
): ChainOf<Ctx, R>;
|
|
205
222
|
/** This method binds names for the body. The body gets a variable token for each name. */
|
|
206
223
|
with<const B extends Readonly<Record<string, unknown>>, U>(
|
|
207
224
|
binds: B,
|
|
@@ -221,6 +238,22 @@ export interface ChainBase<Ctx extends ChainContext, T> {
|
|
|
221
238
|
descendants<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
222
239
|
/** The siblings axis of a tree space: the other children of the parent, or the other roots for a root. */
|
|
223
240
|
siblings<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
241
|
+
/**
|
|
242
|
+
* The window axis of an array space or a tree space: the keys at the offsets from `from` to `to` of the focus, in
|
|
243
|
+
* order. The offset 0 is the focus. An offset outside the space has no target.
|
|
244
|
+
*/
|
|
245
|
+
window<U>(from: number, to: number, body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
246
|
+
/**
|
|
247
|
+
* The table axis: the body evaluates at each key of the table `name` of the evaluation, in the space of that table.
|
|
248
|
+
* The body reads the fields of that table, but the types of the chain are the types of this space.
|
|
249
|
+
*/
|
|
250
|
+
table<U>(name: string, body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
251
|
+
/** The successors axis of a graph space: the ends of the edges from the focus. In a tree space, the children. */
|
|
252
|
+
successors<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
253
|
+
/** The predecessors axis of a graph space: the keys with an edge to the focus. In a tree space, the parent. */
|
|
254
|
+
predecessors<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
255
|
+
/** The reachable axis of a graph space: each key that a path of edges reaches, in breadth-first order. */
|
|
256
|
+
reachable<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
224
257
|
}
|
|
225
258
|
|
|
226
259
|
/** The options of an axis. */
|
|
@@ -259,10 +292,28 @@ export type ArgValue<Ctx extends ChainContext, A> = A extends Arg<infer T>
|
|
|
259
292
|
: A;
|
|
260
293
|
|
|
261
294
|
/** A chain whose value is a list: the result of an axis. */
|
|
295
|
+
/** The type of a value of a name of a loop: a literal of `init` gives its primitive type. */
|
|
296
|
+
type LoopValue<Ctx extends ChainContext, A> = ArgValue<Ctx, A> extends infer T ? (T extends number ? number : T extends string ? string : T extends boolean ? boolean : T) : never;
|
|
297
|
+
|
|
298
|
+
/** The names of a loop of the builder: a chain of the value of each name of `init` (LOOP.NAMES). */
|
|
299
|
+
export type LoopVars<Ctx extends ChainContext, I> = { readonly [N in keyof I]: ChainOf<Ctx, LoopValue<Ctx, I[N]>> };
|
|
300
|
+
|
|
301
|
+
/** The next values of a step of a loop: a value for some of the names of `init`. */
|
|
302
|
+
export type LoopStep<Ctx extends ChainContext, I> = { readonly [N in keyof I]?: ArgInput<LoopValue<Ctx, I[N]>, Ctx, false> };
|
|
303
|
+
|
|
262
304
|
export interface ListChain<Ctx extends ChainContext, U> {
|
|
263
305
|
readonly kind: "vex.list-chain";
|
|
264
306
|
/** The expression of the chain, as plain data. */
|
|
265
307
|
readonly program: Expr;
|
|
308
|
+
/**
|
|
309
|
+
* This method folds the items in the order of the axis (FOLD.ORDER). `init` gives the names and their first values.
|
|
310
|
+
* `step` gives the next values from the names and the item, a chain at the target. `result` gives the value.
|
|
311
|
+
*/
|
|
312
|
+
fold<const I extends Readonly<Record<string, unknown>>, R>(
|
|
313
|
+
init: I,
|
|
314
|
+
step: (vars: LoopVars<Ctx, I>, item: ChainOf<Ctx, U>) => NoInfer<LoopStep<Ctx, I>>,
|
|
315
|
+
result: (vars: LoopVars<Ctx, I>) => ArgInput<R, Ctx, false>,
|
|
316
|
+
): ChainOf<Ctx, R>;
|
|
266
317
|
count(opts?: ListOpts): ChainOf<Ctx, number>;
|
|
267
318
|
sum(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
268
319
|
mean(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
@@ -328,11 +379,12 @@ export interface Root<Ctx extends ChainContext> {
|
|
|
328
379
|
export interface SheetRoot<Ctx extends ChainContext, Cols> extends Root<Ctx> {
|
|
329
380
|
/**
|
|
330
381
|
* This method makes a cell reference: the column `column` at the key `at`, or at the address `at` from the focus.
|
|
331
|
-
* Without `at`, it reads at the focus. The type is the type of a column before
|
|
382
|
+
* The address has only fixed moves. Without `at`, it reads at the focus. The type is the type of a column before
|
|
383
|
+
* this one.
|
|
332
384
|
*/
|
|
333
|
-
cell<N extends keyof Cols & string>(column: N, at?: Ctx["keys"] |
|
|
385
|
+
cell<N extends keyof Cols & string>(column: N, at?: Ctx["keys"] | readonly FixedMove[]): Arg<Cols[N]>;
|
|
334
386
|
/** This method makes a cell reference to any column, also this column or a later one. The caller gives the type. */
|
|
335
|
-
cell<T>(column: string, at?: Ctx["keys"] |
|
|
387
|
+
cell<T>(column: string, at?: Ctx["keys"] | readonly FixedMove[]): Arg<T>;
|
|
336
388
|
}
|
|
337
389
|
|
|
338
390
|
/** One row of a sheet: the key and the result of each column. */
|
|
@@ -360,8 +412,19 @@ export interface Sheet<Ctx extends ChainContext, Cols, Decl = object> {
|
|
|
360
412
|
* `cell` and its type: the column itself (a recurrence) or a later column. It has no effect at run time.
|
|
361
413
|
*/
|
|
362
414
|
declare<D extends Readonly<Record<string, unknown>>>(): Sheet<Ctx, Cols, Decl & D>;
|
|
415
|
+
/**
|
|
416
|
+
* This method gives a new sheet in which the cell of the column `column` at the key `k` has its own formula. That
|
|
417
|
+
* formula replaces the formula of the column at the key `k`. It must give a value of the type of the column.
|
|
418
|
+
*/
|
|
419
|
+
cellFormula<const N extends keyof (Cols & Decl) & string>(
|
|
420
|
+
k: Ctx["keys"],
|
|
421
|
+
column: N,
|
|
422
|
+
formula: (r: SheetRoot<Ctx, Cols & Decl>) => ChainBase<Ctx, (Cols & Decl)[N]>,
|
|
423
|
+
): Sheet<Ctx, Cols, Decl>;
|
|
363
424
|
/** The program of each column. */
|
|
364
425
|
readonly columns: Readonly<Record<string, Expr>>;
|
|
426
|
+
/** The program of each cell with its own formula, by column and then by key. */
|
|
427
|
+
readonly cellFormulas: Readonly<Record<string, Readonly<Record<string, Expr>>>>;
|
|
365
428
|
/** This method evaluates one cell, and gives the error if there is one. */
|
|
366
429
|
result<N extends keyof Cols & string>(k: Ctx["keys"], column: N): Result<Cols[N]>;
|
|
367
430
|
/** This method evaluates one cell. An error gives `none`. */
|
|
@@ -435,6 +498,17 @@ function toExpr(a: unknown, addr: readonly Move[]): Expr {
|
|
|
435
498
|
return lit(a);
|
|
436
499
|
}
|
|
437
500
|
|
|
501
|
+
/** This function gives the expressions of a step of a loop, with the unique names of the loop. */
|
|
502
|
+
function stepExprs(step: Readonly<Record<string, unknown>>, unique: ReadonlyMap<string, string>, addr: readonly Move[]): Readonly<Record<string, Expr>> {
|
|
503
|
+
const out: Record<string, Expr> = {};
|
|
504
|
+
for (const [n, a] of Object.entries(step)) {
|
|
505
|
+
const name = unique.get(n);
|
|
506
|
+
if (name === undefined) throw new TypeError(`the name "${n}" of the step is not a name of init`);
|
|
507
|
+
out[name] = toExpr(a, addr);
|
|
508
|
+
}
|
|
509
|
+
return out;
|
|
510
|
+
}
|
|
511
|
+
|
|
438
512
|
const PROXY_RESERVED = new Set(["then", "catch", "finally", "toJSON", "valueOf", "toString", "constructor", "inspect", "asymmetricMatch", "$$typeof", "nodeType"]);
|
|
439
513
|
|
|
440
514
|
class ChainImpl {
|
|
@@ -488,11 +562,11 @@ class ChainImpl {
|
|
|
488
562
|
#move(m: Move): ChainImpl {
|
|
489
563
|
return new ChainImpl({ ...this.#s, addr: [...this.#s.addr, m] });
|
|
490
564
|
}
|
|
491
|
-
to(k: string): ChainImpl {
|
|
492
|
-
return this.#move(keyMove(k));
|
|
565
|
+
to(k: string | { readonly program: Expr }): ChainImpl {
|
|
566
|
+
return this.#move(keyMove(typeof k === "string" ? k : k.program));
|
|
493
567
|
}
|
|
494
|
-
index(i: number): ChainImpl {
|
|
495
|
-
return this.#move(indexMove(i));
|
|
568
|
+
index(i: number | { readonly program: Expr }): ChainImpl {
|
|
569
|
+
return this.#move(indexMove(typeof i === "number" ? i : i.program));
|
|
496
570
|
}
|
|
497
571
|
offset(...d: readonly number[]): ChainImpl {
|
|
498
572
|
return this.#move(offsetMove(...d));
|
|
@@ -543,6 +617,37 @@ class ChainImpl {
|
|
|
543
617
|
return new ChainImpl({ ...this.#s, expr: let_(bindExprs, inner.program) });
|
|
544
618
|
}
|
|
545
619
|
|
|
620
|
+
/**
|
|
621
|
+
* This method gives the names of a loop. Each name of `init` gets a name that is unique at the depth of the chain.
|
|
622
|
+
* It also gets its first value at the address of the chain, and a chain of its value.
|
|
623
|
+
*/
|
|
624
|
+
loopNames(init: Readonly<Record<string, unknown>>): { readonly unique: ReadonlyMap<string, string>; readonly first: Readonly<Record<string, Expr>>; readonly vars: Readonly<Record<string, ChainImpl>> } {
|
|
625
|
+
const depth = this.#s.depth;
|
|
626
|
+
const unique = new Map<string, string>();
|
|
627
|
+
const first: Record<string, Expr> = {};
|
|
628
|
+
const vars: Record<string, ChainImpl> = {};
|
|
629
|
+
for (const [n, a] of Object.entries(init)) {
|
|
630
|
+
const name = `${n}$${depth}`;
|
|
631
|
+
unique.set(n, name);
|
|
632
|
+
first[name] = toExpr(a, this.#s.addr);
|
|
633
|
+
vars[n] = new ChainImpl({ ...this.#s, expr: v(name), addr: [], depth: depth + 1 });
|
|
634
|
+
}
|
|
635
|
+
return { unique, first, vars };
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
iterate(
|
|
639
|
+
limit: number,
|
|
640
|
+
init: Readonly<Record<string, unknown>>,
|
|
641
|
+
step: (vars: Readonly<Record<string, ChainImpl>>) => Readonly<Record<string, unknown>>,
|
|
642
|
+
result: (vars: Readonly<Record<string, ChainImpl>>) => unknown,
|
|
643
|
+
opts?: { readonly until?: (vars: Readonly<Record<string, ChainImpl>>) => unknown },
|
|
644
|
+
): ChainImpl {
|
|
645
|
+
const { unique, first, vars } = this.loopNames(init);
|
|
646
|
+
const next = stepExprs(step(vars), unique, this.#s.addr);
|
|
647
|
+
const until = opts?.until === undefined ? undefined : toExpr(opts.until(vars), this.#s.addr);
|
|
648
|
+
return new ChainImpl({ ...this.#s, expr: iterateExpr(limit, first, next, toExpr(result(vars), this.#s.addr), until) });
|
|
649
|
+
}
|
|
650
|
+
|
|
546
651
|
#axis(a: Axis, body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
547
652
|
const { name, start } = this.#bound();
|
|
548
653
|
// Inside the axis, the focus is the target, so the address of the body starts empty.
|
|
@@ -573,6 +678,21 @@ class ChainImpl {
|
|
|
573
678
|
siblings(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
574
679
|
return this.#axis({ t: "siblings" }, body, opts);
|
|
575
680
|
}
|
|
681
|
+
window(from: number, to: number, body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
682
|
+
return this.#axis({ t: "window", from, to }, body, opts);
|
|
683
|
+
}
|
|
684
|
+
table(name: string, body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
685
|
+
return this.#axis({ t: "table", name }, body, opts);
|
|
686
|
+
}
|
|
687
|
+
successors(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
688
|
+
return this.#axis({ t: "successors" }, body, opts);
|
|
689
|
+
}
|
|
690
|
+
predecessors(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
691
|
+
return this.#axis({ t: "predecessors" }, body, opts);
|
|
692
|
+
}
|
|
693
|
+
reachable(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
694
|
+
return this.#axis({ t: "reachable" }, body, opts);
|
|
695
|
+
}
|
|
576
696
|
}
|
|
577
697
|
|
|
578
698
|
/** This function gives the options of an evaluation of a chain or a list chain at the origin `k`. */
|
|
@@ -598,6 +718,20 @@ class ListChainImpl {
|
|
|
598
718
|
get program(): Expr {
|
|
599
719
|
return this.#s.expr;
|
|
600
720
|
}
|
|
721
|
+
fold(
|
|
722
|
+
init: Readonly<Record<string, unknown>>,
|
|
723
|
+
step: (vars: Readonly<Record<string, ChainImpl>>, item: ChainImpl) => Readonly<Record<string, unknown>>,
|
|
724
|
+
result: (vars: Readonly<Record<string, ChainImpl>>) => unknown,
|
|
725
|
+
): ChainImpl {
|
|
726
|
+
// The list of an axis is a let of the start value around the axis. The fold keeps the let, and the body of the
|
|
727
|
+
// axis is the item of each step.
|
|
728
|
+
const { bind, body: loop } = this.#s.expr as ExprOf<"let"> & { readonly body: ExprOf<"each"> };
|
|
729
|
+
const names = new ChainImpl({ ...this.#s, expr: lit(null) }).loopNames(init);
|
|
730
|
+
const item = new ChainImpl({ ...this.#s, expr: loop.body, addr: [] });
|
|
731
|
+
const next = stepExprs(step(names.vars, item), names.unique, []);
|
|
732
|
+
return new ChainImpl({ ...this.#s, expr: let_(bind, foldExpr(loop.axis, names.first, next, toExpr(result(names.vars), this.#s.addr), loop.at)) });
|
|
733
|
+
}
|
|
734
|
+
|
|
601
735
|
#op(op: string, extra: readonly Expr[], opts?: ListOpts & { readonly domain?: string }): ChainImpl {
|
|
602
736
|
const options = { ...(opts?.strict === true ? { strict: true } : {}), ...(opts?.domain === undefined ? {} : { domain: opts.domain }) };
|
|
603
737
|
const args = Object.keys(options).length > 0 ? [...extra, lit(options)] : extra;
|
|
@@ -696,7 +830,7 @@ class RootImpl {
|
|
|
696
830
|
}
|
|
697
831
|
|
|
698
832
|
class SheetRootImpl extends RootImpl {
|
|
699
|
-
cell(column: string, at?: string |
|
|
833
|
+
cell(column: string, at?: string | readonly FixedMove[]): Arg<unknown> {
|
|
700
834
|
return argToken(cellRef(column, typeof at === "string" ? [keyMove(at)] : at));
|
|
701
835
|
}
|
|
702
836
|
}
|
|
@@ -704,25 +838,36 @@ class SheetRootImpl extends RootImpl {
|
|
|
704
838
|
class SheetImpl {
|
|
705
839
|
readonly #env: Env;
|
|
706
840
|
readonly #columns: ReadonlyMap<string, Expr>;
|
|
707
|
-
|
|
841
|
+
readonly #cells: CellFormulas;
|
|
842
|
+
constructor(env: Env, columns: ReadonlyMap<string, Expr>, cells: CellFormulas = new Map()) {
|
|
708
843
|
this.#env = env;
|
|
709
844
|
this.#columns = columns;
|
|
845
|
+
this.#cells = cells;
|
|
710
846
|
}
|
|
711
847
|
get columns(): Readonly<Record<string, Expr>> {
|
|
712
848
|
return Object.freeze(Object.fromEntries(this.#columns));
|
|
713
849
|
}
|
|
850
|
+
get cellFormulas(): Readonly<Record<string, Readonly<Record<string, Expr>>>> {
|
|
851
|
+
return Object.freeze(Object.fromEntries([...this.#cells].map(([c, keys]) => [c, Object.freeze(Object.fromEntries(keys))])));
|
|
852
|
+
}
|
|
714
853
|
declare(): SheetImpl {
|
|
715
854
|
return this;
|
|
716
855
|
}
|
|
717
856
|
column(name: string, formula: (r: SheetRootImpl) => { readonly program: Expr }): SheetImpl {
|
|
718
857
|
const program = formula(new SheetRootImpl(this.#env)).program;
|
|
719
|
-
return new SheetImpl(this.#env, new Map([...this.#columns, [name, program]]));
|
|
858
|
+
return new SheetImpl(this.#env, new Map([...this.#columns, [name, program]]), this.#cells);
|
|
859
|
+
}
|
|
860
|
+
cellFormula(k: string, column: string, formula: (r: SheetRootImpl) => { readonly program: Expr }): SheetImpl {
|
|
861
|
+
const program = formula(new SheetRootImpl(this.#env)).program;
|
|
862
|
+
const keys = new Map([...(this.#cells.get(column) ?? []), [k, program]]);
|
|
863
|
+
return new SheetImpl(this.#env, this.#columns, new Map([...this.#cells, [column, keys]]));
|
|
720
864
|
}
|
|
721
865
|
#run(): SheetRun {
|
|
722
866
|
const { space: s, domains, options } = this.#env;
|
|
723
867
|
return new SheetRun(this.#columns, {
|
|
724
868
|
space: s,
|
|
725
869
|
domains,
|
|
870
|
+
cells: this.#cells,
|
|
726
871
|
// Stryker disable next-line ConditionalExpression: an undefined option and an absent option are the same
|
|
727
872
|
...(options.fns === undefined ? {} : { fns: options.fns }),
|
|
728
873
|
// Stryker disable next-line ConditionalExpression: an undefined option and an absent option are the same
|
|
@@ -744,6 +889,8 @@ class SheetImpl {
|
|
|
744
889
|
}
|
|
745
890
|
table(): readonly SheetRow<string, Record<string, unknown>>[] {
|
|
746
891
|
const run = this.#run();
|
|
892
|
+
// The table reads each cell, so a fill evaluates them in the order of their reads first (SHEET.FILL).
|
|
893
|
+
run.fill();
|
|
747
894
|
const names = [...this.#columns.keys()];
|
|
748
895
|
return this.#env.space.keys.map((key) => ({ key, cells: Object.fromEntries(names.map((c) => [c, run.cell(c, key)])) }));
|
|
749
896
|
}
|
package/src/deps.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { children, type Addr, type Axis, type Expr } from "./ir.ts";
|
|
1
|
+
import { addrExprs, children, type Addr, type Axis, type Expr } from "./ir.ts";
|
|
2
2
|
|
|
3
3
|
/** One static read of a program: a field path at an address, inside zero or more axes. */
|
|
4
4
|
export interface Dependency {
|
|
@@ -14,7 +14,8 @@ export interface Dependency {
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* This function gives the static reads of an expression. A host can use them to recompute a program only
|
|
17
|
-
* when a field that it reads changes.
|
|
17
|
+
* when a field that it reads changes. A computed move reads a key that only a run knows. Its address keeps the
|
|
18
|
+
* expression, and the reads of that expression are in the list too (NAV.COMPUTED).
|
|
18
19
|
*/
|
|
19
20
|
export function deps(e: Expr): readonly Dependency[] {
|
|
20
21
|
const out: Dependency[] = [];
|
|
@@ -23,13 +24,16 @@ export function deps(e: Expr): readonly Dependency[] {
|
|
|
23
24
|
const todo: { readonly x: Expr; readonly axes: readonly Axis["t"][]; readonly axisAt: readonly Addr[] }[] = [{ x: e, axes: [], axisAt: [] }];
|
|
24
25
|
for (let item = todo.pop(); item !== undefined; item = todo.pop()) {
|
|
25
26
|
const { x } = item;
|
|
26
|
-
if (x.tag === "ref") {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
27
|
+
if (x.tag === "ref") out.push({ at: x.at ?? [], path: x.path, axes: item.axes, axisAt: item.axisAt });
|
|
28
|
+
const all = children(x);
|
|
29
|
+
const axial = x.tag === "each" || x.tag === "fold";
|
|
30
|
+
// Inside the axis: the body or the values of the step, and the tests of where. The values of init, the result of a
|
|
31
|
+
// fold and the expressions of an address evaluate at the node, outside the axis.
|
|
32
|
+
const first = x.tag === "fold" ? Object.keys(x.init).length : 0;
|
|
33
|
+
const last = axial ? all.length - addrExprs(x.at).length - (x.tag === "fold" ? 1 : 0) : 0;
|
|
34
|
+
const axes = axial ? [...item.axes, x.axis.t] : item.axes;
|
|
35
|
+
const axisAt = axial ? [...item.axisAt, x.at ?? []] : item.axisAt;
|
|
36
|
+
for (let i = all.length - 1; i >= 0; i--) todo.push(i >= first && i < last ? { x: all[i] as Expr, axes, axisAt } : { ...item, x: all[i] as Expr });
|
|
33
37
|
}
|
|
34
38
|
return out;
|
|
35
39
|
}
|
package/src/domain.ts
CHANGED
|
@@ -9,6 +9,9 @@ export type Law = "commutative" | "associative" | "idempotent";
|
|
|
9
9
|
/** The kind of a parameter. The interpreter checks the arguments of an op that declares its parameters. */
|
|
10
10
|
export type ParamKind = "domain" | "number" | "boolean" | "string" | "any";
|
|
11
11
|
|
|
12
|
+
/** The kind of the result of an op: a kind of a parameter, or `domain:Name` for a value of the domain `Name`. */
|
|
13
|
+
export type ReturnKind = ParamKind | `domain:${string}`;
|
|
14
|
+
|
|
12
15
|
/** The description of one op of a domain. */
|
|
13
16
|
export interface OpSpec<D = never> {
|
|
14
17
|
/**
|
|
@@ -24,8 +27,17 @@ export interface OpSpec<D = never> {
|
|
|
24
27
|
readonly liftScalar?: boolean | readonly boolean[];
|
|
25
28
|
/** The kinds of the parameters after the receiver. When set, the interpreter checks the arguments. */
|
|
26
29
|
readonly params?: readonly ParamKind[];
|
|
30
|
+
/**
|
|
31
|
+
* The kind of the result: `domain` for a value of this domain, and `domain:Name` for a value of the domain `Name`. A
|
|
32
|
+
* compiler reads it to know the kinds of the values of a program (KIND.RETURNS). The interpreter does not check it,
|
|
33
|
+
* and the law checker of the test kit does.
|
|
34
|
+
*/
|
|
35
|
+
readonly returns?: ReturnKind;
|
|
27
36
|
}
|
|
28
37
|
|
|
38
|
+
/** A JavaScript type of primitive values that a domain can accept. */
|
|
39
|
+
export type PrimitiveKind = "number" | "string" | "boolean";
|
|
40
|
+
|
|
29
41
|
/** The type of an op that lifts number arguments. Use it to type the ops of a domain under `isolatedDeclarations`. */
|
|
30
42
|
export type LiftedOp<D> = OpSpec<D> & { readonly liftScalar: true };
|
|
31
43
|
|
|
@@ -46,6 +58,11 @@ export interface DomainSpec<Name extends string, D, Ops extends Readonly<Record<
|
|
|
46
58
|
readonly name: Name;
|
|
47
59
|
/** The runtime test for the values of the domain. It must not give false positives. */
|
|
48
60
|
readonly is: (u: unknown) => u is D;
|
|
61
|
+
/**
|
|
62
|
+
* The JavaScript type of the values of the domain, if `is` accepts each value of that type and no other value
|
|
63
|
+
* (KIND.PRIMITIVE). A compiler then finds the op of such a value before a run.
|
|
64
|
+
*/
|
|
65
|
+
readonly primitive?: PrimitiveKind;
|
|
49
66
|
/** The ops of the domain, with their metadata. */
|
|
50
67
|
readonly ops: Ops;
|
|
51
68
|
/** With `"all"`, each method of a value is an op, also without an entry in `ops`. The default is `"declared"`. */
|
|
@@ -60,6 +77,11 @@ export interface DomainSpec<Name extends string, D, Ops extends Readonly<Record<
|
|
|
60
77
|
readonly encode?: (d: D) => unknown;
|
|
61
78
|
/** The value of a JSON form that `encode` made. */
|
|
62
79
|
readonly decode?: (json: unknown) => D;
|
|
80
|
+
/**
|
|
81
|
+
* Some values of the domain. A compiler gives them to an op and to its copy in a module: the two must give the same
|
|
82
|
+
* results (KIND.DOMAIN).
|
|
83
|
+
*/
|
|
84
|
+
readonly samples?: () => readonly D[];
|
|
63
85
|
}
|
|
64
86
|
|
|
65
87
|
/** A domain. Make one with `defineDomain`. */
|
package/src/errors.ts
CHANGED
|
@@ -12,6 +12,8 @@ export type ErrorKind =
|
|
|
12
12
|
| "no-offset"
|
|
13
13
|
| "no-grid"
|
|
14
14
|
| "no-tree"
|
|
15
|
+
| "no-table"
|
|
16
|
+
| "no-graph"
|
|
15
17
|
| "missing-field"
|
|
16
18
|
| "empty"
|
|
17
19
|
| "not-instance"
|
|
@@ -25,7 +27,8 @@ export type ErrorKind =
|
|
|
25
27
|
| "args"
|
|
26
28
|
| "cycle"
|
|
27
29
|
| "depth"
|
|
28
|
-
| "bad-expression"
|
|
30
|
+
| "bad-expression"
|
|
31
|
+
| "limit";
|
|
29
32
|
|
|
30
33
|
const CODE_OF: Readonly<Record<ErrorKind, ErrorCode>> = {
|
|
31
34
|
"unknown-key": "#REF!",
|
|
@@ -34,6 +37,8 @@ const CODE_OF: Readonly<Record<ErrorKind, ErrorCode>> = {
|
|
|
34
37
|
"no-offset": "#REF!",
|
|
35
38
|
"no-grid": "#REF!",
|
|
36
39
|
"no-tree": "#REF!",
|
|
40
|
+
"no-table": "#REF!",
|
|
41
|
+
"no-graph": "#REF!",
|
|
37
42
|
"missing-field": "#N/A",
|
|
38
43
|
empty: "#N/A",
|
|
39
44
|
"not-instance": "#VALUE!",
|
|
@@ -43,6 +48,7 @@ const CODE_OF: Readonly<Record<ErrorKind, ErrorCode>> = {
|
|
|
43
48
|
unbound: "#NAME?",
|
|
44
49
|
"not-finite": "#NUM!",
|
|
45
50
|
"invalid-value": "#NUM!",
|
|
51
|
+
limit: "#NUM!",
|
|
46
52
|
threw: "#CALC!",
|
|
47
53
|
"undefined-result": "#CALC!",
|
|
48
54
|
args: "#ARGS",
|