@mark1russell7/vex 0.9.0 → 0.10.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 +36 -1
- package/build/index.d.ts +155 -28
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2124 -227
- package/build/index.js.map +1 -1
- package/package.json +5 -1
- package/src/builder.ts +54 -13
- package/src/codec.ts +104 -0
- package/src/deps.ts +174 -60
- package/src/document.ts +301 -0
- package/src/errors.ts +7 -3
- package/src/evaluate.ts +82 -28
- package/src/index.ts +5 -1
- package/src/json.ts +11 -96
- package/src/kernel.ts +897 -0
- package/src/list.ts +6 -3
- package/src/plan.ts +88 -0
- package/src/same.ts +31 -0
- package/src/sheet.ts +2 -1
- package/src/write.ts +257 -0
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.10.0",
|
|
5
5
|
"description": "Vex: typed spreadsheet formulas over domain objects. The kernel and the typed builder.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"typescript",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"directory": "packages/core"
|
|
28
28
|
},
|
|
29
29
|
"type": "module",
|
|
30
|
+
"sideEffects": false,
|
|
30
31
|
"main": "./build/index.js",
|
|
31
32
|
"types": "./build/index.d.ts",
|
|
32
33
|
"exports": {
|
|
@@ -47,6 +48,9 @@
|
|
|
47
48
|
"publishConfig": {
|
|
48
49
|
"access": "public"
|
|
49
50
|
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@mark1russell7/coeval": "^0.6.0"
|
|
53
|
+
},
|
|
50
54
|
"devDependencies": {
|
|
51
55
|
"@mark1russell7/cue": "github:mark1russell7/cue",
|
|
52
56
|
"@mark1russell7/optional": "github:mark1russell7/optional",
|
package/src/builder.ts
CHANGED
|
@@ -231,35 +231,35 @@ export interface ChainBase<Ctx extends ChainContext, T> {
|
|
|
231
231
|
*/
|
|
232
232
|
functions<const F extends Readonly<Record<string, FnDef<readonly FnParamKind[], unknown>>>, U>(fns: F, body: (c: ChainOf<Ctx, T>, call: FnCalls<Ctx, F>) => ChainBase<Ctx, U>): ChainOf<Ctx, U>;
|
|
233
233
|
/** The others axis: the body evaluates at each key except the focus. Its start value is the current value. */
|
|
234
|
-
others<U>(body: (c: ChainOf<Ctx, T>) =>
|
|
234
|
+
others<U>(body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
235
235
|
/** The neighbors axis of a grid. */
|
|
236
|
-
neighbors<U>(n: 4 | 8, body: (c: ChainOf<Ctx, T>) =>
|
|
236
|
+
neighbors<U>(n: 4 | 8, body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
237
237
|
/** The all axis inside the expression: the body evaluates at each key, with the current value as start value. */
|
|
238
|
-
each<U>(body: (c: ChainOf<Ctx, T>) =>
|
|
238
|
+
each<U>(body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
239
239
|
/** The children axis of a tree space: the body evaluates at each child of the focus. */
|
|
240
|
-
children<U>(body: (c: ChainOf<Ctx, T>) =>
|
|
240
|
+
children<U>(body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
241
241
|
/** The ancestors axis of a tree space: the parent first, the root last. */
|
|
242
|
-
ancestors<U>(body: (c: ChainOf<Ctx, T>) =>
|
|
242
|
+
ancestors<U>(body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
243
243
|
/** The descendants axis of a tree space: depth first, each parent before its children. */
|
|
244
|
-
descendants<U>(body: (c: ChainOf<Ctx, T>) =>
|
|
244
|
+
descendants<U>(body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
245
245
|
/** The siblings axis of a tree space: the other children of the parent, or the other roots for a root. */
|
|
246
|
-
siblings<U>(body: (c: ChainOf<Ctx, T>) =>
|
|
246
|
+
siblings<U>(body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
247
247
|
/**
|
|
248
248
|
* The window axis of an array space or a tree space: the keys at the offsets from `from` to `to` of the focus, in
|
|
249
249
|
* order. The offset 0 is the focus. An offset outside the space has no target.
|
|
250
250
|
*/
|
|
251
|
-
window<U>(from: number, to: number, body: (c: ChainOf<Ctx, T>) =>
|
|
251
|
+
window<U>(from: number, to: number, body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
252
252
|
/**
|
|
253
253
|
* The table axis: the body evaluates at each key of the table `name` of the evaluation, in the space of that table.
|
|
254
254
|
* The body reads the fields of that table, but the types of the chain are the types of this space.
|
|
255
255
|
*/
|
|
256
|
-
table<U>(name: string, body: (c: ChainOf<Ctx, T>) =>
|
|
256
|
+
table<U>(name: string, body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
257
257
|
/** The successors axis of a graph space: the ends of the edges from the focus. In a tree space, the children. */
|
|
258
|
-
successors<U>(body: (c: ChainOf<Ctx, T>) =>
|
|
258
|
+
successors<U>(body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
259
259
|
/** The predecessors axis of a graph space: the keys with an edge to the focus. In a tree space, the parent. */
|
|
260
|
-
predecessors<U>(body: (c: ChainOf<Ctx, T>) =>
|
|
260
|
+
predecessors<U>(body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
261
261
|
/** The reachable axis of a graph space: each key that a path of edges reaches, in breadth-first order. */
|
|
262
|
-
reachable<U>(body: (c: ChainOf<Ctx, T>) =>
|
|
262
|
+
reachable<U>(body: (c: ChainOf<Ctx, T>) => AxisBody<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
263
263
|
}
|
|
264
264
|
|
|
265
265
|
/** The options of an axis. */
|
|
@@ -341,8 +341,23 @@ export type FnCalls<Ctx extends ChainContext, F> = {
|
|
|
341
341
|
readonly [N in keyof F]: F[N] extends FnDef<infer P, infer R> ? (...args: { readonly [I in keyof P]: ArgInput<KindValue<P[I]>, Ctx, false> }) => ChainOf<Ctx, R> : never;
|
|
342
342
|
};
|
|
343
343
|
|
|
344
|
+
/**
|
|
345
|
+
* The value of the body of an axis: a chain, or a list chain (BUILD.LIST.BUILD). For a list chain, the value of each
|
|
346
|
+
* item of the axis is a list, so `flat` joins the lists.
|
|
347
|
+
*/
|
|
348
|
+
export type AxisBody<Ctx extends ChainContext, U> = ChainBase<Ctx, U> | ListBody<U>;
|
|
349
|
+
|
|
350
|
+
/** A list chain as the body of an axis. Its phantom member carries the list. */
|
|
351
|
+
export interface ListBody<U> {
|
|
352
|
+
readonly kind: "vex.list-chain";
|
|
353
|
+
readonly [CHAIN_VALUE]?: U;
|
|
354
|
+
readonly program: Expr;
|
|
355
|
+
}
|
|
356
|
+
|
|
344
357
|
export interface ListChain<Ctx extends ChainContext, U> {
|
|
345
358
|
readonly kind: "vex.list-chain";
|
|
359
|
+
/** This phantom member carries the type of the list. It is not present at run time. */
|
|
360
|
+
readonly [CHAIN_VALUE]?: VexList<U>;
|
|
346
361
|
/** The expression of the chain, as plain data. */
|
|
347
362
|
readonly program: Expr;
|
|
348
363
|
/**
|
|
@@ -375,11 +390,19 @@ export interface ListChain<Ctx extends ChainContext, U> {
|
|
|
375
390
|
take(n: number, opts?: ListOpts): ListChain<Ctx, U>;
|
|
376
391
|
/** The value of the item at the position `i`, from 0 (LIST.NTH). */
|
|
377
392
|
nth(i: number, opts?: ListOpts): ChainOf<Ctx, U>;
|
|
393
|
+
/** The items after the first `n` items (LIST.DROP). Thus `drop(1)` removes the first item of a stack. */
|
|
394
|
+
drop(n: number, opts?: ListOpts): ListChain<Ctx, U>;
|
|
395
|
+
/** The items of this list, and then the items of `other` (LIST.APPEND). Two items can have the same key. */
|
|
396
|
+
append<V>(other: ListChain<Ctx, V>, opts?: ListOpts): ListChain<Ctx, U | V>;
|
|
397
|
+
/** The items of this list whose key is not the key of an item of `other` (LIST.WITHOUT). */
|
|
398
|
+
without<V>(other: ListChain<Ctx, V>, opts?: ListOpts): ListChain<Ctx, U>;
|
|
399
|
+
/** The items of the lists that are the values of the items, in order (LIST.FLAT). The values must be lists. */
|
|
400
|
+
flat<V>(this: ListChain<Ctx, VexList<V>>, opts?: ListOpts): ListChain<Ctx, V>;
|
|
378
401
|
/**
|
|
379
402
|
* The axis list (AXIS.LIST): the body evaluates at the key of each item, in the order of the list. The body reads the
|
|
380
403
|
* fields of the record of the item. The chain of the body has no value of its own.
|
|
381
404
|
*/
|
|
382
|
-
each<V>(body: (c: ChainOf<Ctx, null>) =>
|
|
405
|
+
each<V>(body: (c: ChainOf<Ctx, null>) => AxisBody<Ctx, V>, opts?: AxisOpts<Ctx, null>): ListChain<Ctx, V>;
|
|
383
406
|
/** The list at the origin `k`: one item for each target of the axis, with its key and its result. */
|
|
384
407
|
result(k: Ctx["keys"]): Result<VexList<U>>;
|
|
385
408
|
/** The list at the origin `k`, as an `Optional`. */
|
|
@@ -546,6 +569,12 @@ const kindOf = (u: unknown): unknown => (u as { kind?: unknown } | null | undefi
|
|
|
546
569
|
const isArg = (u: unknown): u is Arg<unknown> => kindOf(u) === "vex.arg";
|
|
547
570
|
const isChainLike = (u: unknown): u is { readonly program: Expr } => kindOf(u) === "vex.chain" || kindOf(u) === "vex.list-chain";
|
|
548
571
|
|
|
572
|
+
/** This function gives the expression of the list of `append` and `without`. The list is a chain, not a literal. */
|
|
573
|
+
function listProgram(op: string, other: unknown): Expr {
|
|
574
|
+
if (!isChainLike(other)) throw new TypeError(`${op} needs a list chain`);
|
|
575
|
+
return other.program;
|
|
576
|
+
}
|
|
577
|
+
|
|
549
578
|
/** This function turns a builder argument into an expression. A bare string is a field reference at `addr`. */
|
|
550
579
|
function toExpr(a: unknown, addr: readonly Move[]): Expr {
|
|
551
580
|
if (typeof a === "string") return ref(toPath(a), addr);
|
|
@@ -865,6 +894,18 @@ class ListChainImpl {
|
|
|
865
894
|
nth(i: number, opts?: ListOpts): ChainImpl {
|
|
866
895
|
return this.#op("nth", [lit(i)], opts);
|
|
867
896
|
}
|
|
897
|
+
drop(n: number, opts?: ListOpts): ListChainImpl {
|
|
898
|
+
return this.#list("drop", [lit(n)], opts);
|
|
899
|
+
}
|
|
900
|
+
append(other: unknown, opts?: ListOpts): ListChainImpl {
|
|
901
|
+
return this.#list("append", [listProgram("append", other)], opts);
|
|
902
|
+
}
|
|
903
|
+
without(other: unknown, opts?: ListOpts): ListChainImpl {
|
|
904
|
+
return this.#list("without", [listProgram("without", other)], opts);
|
|
905
|
+
}
|
|
906
|
+
flat(opts?: ListOpts): ListChainImpl {
|
|
907
|
+
return this.#list("flat", [], opts);
|
|
908
|
+
}
|
|
868
909
|
each(body: (c: ChainImpl) => { readonly program: Expr }, opts?: AxisOptsImpl): ListChainImpl {
|
|
869
910
|
// The chain of the body is at the key of the item, and it has no value of its own.
|
|
870
911
|
const start = new ChainImpl({ ...this.#s, expr: lit(null), addr: [], depth: this.#s.depth + 1 });
|
package/src/codec.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The JSON form of the values of a literal. `serialize`, `parse`, `toDocument` and `fromDocument` share it. The package
|
|
3
|
+
* index does not export this module.
|
|
4
|
+
*/
|
|
5
|
+
import type { AnyDomain } from "./domain.ts";
|
|
6
|
+
import { vexError } from "./errors.ts";
|
|
7
|
+
import { fail, ok, type Result } from "./result.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The JSON form of the values in a literal. JSON data stays as it is, with three exceptions. Each exception is a record
|
|
11
|
+
* with the key `$vex`:
|
|
12
|
+
*
|
|
13
|
+
* - `{ "$vex": "domain", "domain": name, "value": v }`: a value of a domain, with the `encode` of the domain.
|
|
14
|
+
* - `{ "$vex": "number", "value": "-0" }`: a number that JSON has no text for: `-0`, `NaN`, `Infinity` or `-Infinity`.
|
|
15
|
+
* - `{ "$vex": "escape", "value": record }`: a record of the data that has the key `$vex` itself (IR.JSON.ESCAPE).
|
|
16
|
+
*
|
|
17
|
+
* A record with the key `$vex` and another form stays data, so a file from an older version reads as before.
|
|
18
|
+
*
|
|
19
|
+
* The functions walk the tree of the expression, so only the value of a literal gets this form. A field of a literal
|
|
20
|
+
* that looks like an expression stays data. The data of an extension is plain JSON, as before. A document has the same
|
|
21
|
+
* form for the value of a literal (DOC.LITERAL).
|
|
22
|
+
*/
|
|
23
|
+
type Encoded =
|
|
24
|
+
| { readonly $vex: "domain"; readonly domain: string; readonly value: unknown }
|
|
25
|
+
| { readonly $vex: "number"; readonly value: "-0" | "NaN" | "Infinity" | "-Infinity" }
|
|
26
|
+
| { readonly $vex: "escape"; readonly value: Readonly<Record<string, unknown>> };
|
|
27
|
+
|
|
28
|
+
/** This function tells if a value is a record: an object that is not an array. */
|
|
29
|
+
export const isRecord = (u: unknown): u is Readonly<Record<string, unknown>> => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
30
|
+
|
|
31
|
+
const isPlainRecord = (u: unknown): u is Readonly<Record<string, unknown>> => {
|
|
32
|
+
if (!isRecord(u)) return false;
|
|
33
|
+
const proto: unknown = Object.getPrototypeOf(u);
|
|
34
|
+
return proto === Object.prototype || proto === null;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** This function gives the JSON form of a value, or the reason why the value has no JSON form. */
|
|
38
|
+
export function encodeValue(u: unknown, domains: readonly AnyDomain[]): Result<unknown> {
|
|
39
|
+
if (u === null || typeof u === "string" || typeof u === "boolean") return ok(u);
|
|
40
|
+
if (typeof u === "number") {
|
|
41
|
+
if (Object.is(u, -0)) return ok({ $vex: "number", value: "-0" } satisfies Encoded);
|
|
42
|
+
// Stryker disable next-line EqualityOperator: here u is an infinity, so u > 0 and u >= 0 are the same test
|
|
43
|
+
return Number.isFinite(u) ? ok(u) : ok({ $vex: "number", value: Number.isNaN(u) ? "NaN" : u > 0 ? "Infinity" : "-Infinity" } satisfies Encoded);
|
|
44
|
+
}
|
|
45
|
+
if (Array.isArray(u)) {
|
|
46
|
+
const out: unknown[] = [];
|
|
47
|
+
for (const x of u) {
|
|
48
|
+
const r = encodeValue(x, domains);
|
|
49
|
+
if (!r.ok) return r;
|
|
50
|
+
out.push(r.value);
|
|
51
|
+
}
|
|
52
|
+
return ok(out);
|
|
53
|
+
}
|
|
54
|
+
if (isPlainRecord(u)) {
|
|
55
|
+
const out: Record<string, unknown> = {};
|
|
56
|
+
for (const [k, x] of Object.entries(u)) {
|
|
57
|
+
const r = encodeValue(x, domains);
|
|
58
|
+
if (!r.ok) return r;
|
|
59
|
+
out[k] = r.value;
|
|
60
|
+
}
|
|
61
|
+
return ok(Object.hasOwn(u, "$vex") ? ({ $vex: "escape", value: out } satisfies Encoded) : out);
|
|
62
|
+
}
|
|
63
|
+
// A plain array or record stays data. Another object is a value of a domain, or it has no JSON form.
|
|
64
|
+
const d = domains.find((dom) => dom.encode !== undefined && dom.is(u));
|
|
65
|
+
if (d?.encode !== undefined) return ok({ $vex: "domain", domain: d.name, value: d.encode(u) } satisfies Encoded);
|
|
66
|
+
return fail(vexError("bad-expression", "a literal value is not JSON data and no domain encodes it"));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const NUMBERS: Readonly<Record<string, number>> = { "-0": -0, NaN: Number.NaN, Infinity: Number.POSITIVE_INFINITY, "-Infinity": Number.NEGATIVE_INFINITY };
|
|
70
|
+
|
|
71
|
+
/** This function reads a value from its JSON form. */
|
|
72
|
+
export function decodeValue(u: unknown, domains: readonly AnyDomain[]): Result<unknown> {
|
|
73
|
+
if (Array.isArray(u)) {
|
|
74
|
+
const out: unknown[] = [];
|
|
75
|
+
for (const x of u) {
|
|
76
|
+
const r = decodeValue(x, domains);
|
|
77
|
+
if (!r.ok) return r;
|
|
78
|
+
out.push(r.value);
|
|
79
|
+
}
|
|
80
|
+
return ok(out);
|
|
81
|
+
}
|
|
82
|
+
if (!isRecord(u)) return ok(u);
|
|
83
|
+
const fields = (record: Readonly<Record<string, unknown>>): Result<unknown> => {
|
|
84
|
+
const out: Record<string, unknown> = {};
|
|
85
|
+
for (const [k, x] of Object.entries(record)) {
|
|
86
|
+
const r = decodeValue(x, domains);
|
|
87
|
+
if (!r.ok) return r;
|
|
88
|
+
out[k] = r.value;
|
|
89
|
+
}
|
|
90
|
+
return ok(out);
|
|
91
|
+
};
|
|
92
|
+
// A record without the key $vex matches none of the forms below, so it stays data too.
|
|
93
|
+
const kind = u["$vex"];
|
|
94
|
+
if (kind === "escape" && isRecord(u["value"])) return fields(u["value"]);
|
|
95
|
+
if (kind === "number" && typeof u["value"] === "string" && Object.hasOwn(NUMBERS, u["value"])) return ok(NUMBERS[u["value"]]);
|
|
96
|
+
if (kind === "domain" && typeof u["domain"] === "string") {
|
|
97
|
+
const name = u["domain"];
|
|
98
|
+
const d = domains.find((dom) => dom.name === name);
|
|
99
|
+
if (d?.decode === undefined) return fail(vexError("bad-expression", `no domain "${name}" decodes a literal`));
|
|
100
|
+
return ok(d.decode(u["value"]));
|
|
101
|
+
}
|
|
102
|
+
// A form that this version does not know stays data, so a file from an older version reads as before.
|
|
103
|
+
return fields(u);
|
|
104
|
+
}
|
package/src/deps.ts
CHANGED
|
@@ -1,27 +1,12 @@
|
|
|
1
|
-
import { addrExprs, children, listOf, type Addr, type Axis, type Expr, type ExprOf } from "./ir.ts";
|
|
2
|
-
|
|
3
|
-
/** A bound function: its node, and the functions of the place of its binding. */
|
|
4
|
-
interface Bound {
|
|
5
|
-
readonly fn: ExprOf<"fn">;
|
|
6
|
-
readonly fns: ReadonlyMap<string, Bound>;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/** This function gives the functions of a scope without some names: a parameter or a binding hides a function. */
|
|
10
|
-
const without = (fns: ReadonlyMap<string, Bound>, names: readonly string[]): ReadonlyMap<string, Bound> => new Map([...fns].filter(([n]) => !names.includes(n)));
|
|
11
|
-
|
|
12
1
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
2
|
+
* The static reads of a program (DEPS.READS). The static kit of coeval finds the references of the document of the
|
|
3
|
+
* program, by the contracts of Vex (`refsOf`). Vex adds only what is its own: the axes around a read, and the reads of
|
|
4
|
+
* a function at each of its calls (FN.CALL).
|
|
16
5
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const steps = first + Object.keys(x.step).length;
|
|
22
|
-
// The children of a fold have the tests of where and the address between the steps and the result.
|
|
23
|
-
return (i) => (i >= first && (i < steps || i === count - 1 || x.tag === "iterate") ? hidden : fns);
|
|
24
|
-
}
|
|
6
|
+
import { pathMatches, refsOf, type DocVisit, type Key, type RefUse } from "@mark1russell7/coeval";
|
|
7
|
+
import type { Addr, Axis, Expr, ExprOf } from "./ir.ts";
|
|
8
|
+
import { VEX_WORLD } from "./kernel.ts";
|
|
9
|
+
import { writeDocument } from "./write.ts";
|
|
25
10
|
|
|
26
11
|
/** One static read of a program: a field path at an address, inside zero or more axes. */
|
|
27
12
|
export interface Dependency {
|
|
@@ -35,50 +20,179 @@ export interface Dependency {
|
|
|
35
20
|
readonly axisAt: readonly Addr[];
|
|
36
21
|
}
|
|
37
22
|
|
|
23
|
+
/** An axis around a read: the kind of the axis of a loop, and the address of the loop. */
|
|
24
|
+
interface Around {
|
|
25
|
+
readonly t: Axis["t"];
|
|
26
|
+
readonly at: Addr;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A chain of items, the innermost first. The chains of two documents share the items of the documents around both. */
|
|
30
|
+
interface Chain<T> {
|
|
31
|
+
readonly item: T;
|
|
32
|
+
readonly up: Chain<T> | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The context of a document for its reads. */
|
|
36
|
+
interface Context {
|
|
37
|
+
/** The axes around the document, the innermost first. */
|
|
38
|
+
readonly axes: Chain<Around> | undefined;
|
|
39
|
+
/** Inside the axis document of a loop: the axis of that loop. A test of `where` there is inside the axis. */
|
|
40
|
+
readonly axis: Around | undefined;
|
|
41
|
+
/** The nearest function around the document: a binding of a let. Its reads happen at its calls, not here. */
|
|
42
|
+
readonly fn: DocVisit | undefined;
|
|
43
|
+
/** The calls around the document whose function is a binding of a let, the innermost first. */
|
|
44
|
+
readonly calls: Chain<DocVisit> | undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const ROOT: Context = { axes: undefined, axis: undefined, fn: undefined, calls: undefined };
|
|
48
|
+
|
|
49
|
+
/** This function gives the items of a chain until the chain `end`, from the outside to the inside. */
|
|
50
|
+
function itemsUntil<T>(chain: Chain<T> | undefined, end: Chain<T> | undefined): T[] {
|
|
51
|
+
const out: T[] = [];
|
|
52
|
+
for (let c = chain; c !== undefined && c !== end; c = c.up) out.push(c.item);
|
|
53
|
+
return out.toReversed();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** This function gives the kind of a visit, as its keys joined with dots, for example `each` or `axes.where`. */
|
|
57
|
+
// Stryker disable next-line ArrayDeclaration: each document of the writer has a type of the world, so each visit has a type path
|
|
58
|
+
const kindOf = (v: DocVisit): string => (v.typePath ?? []).join(".");
|
|
59
|
+
|
|
38
60
|
/**
|
|
39
|
-
* This function gives the static reads of an expression. A host can use them to recompute a program only
|
|
40
|
-
*
|
|
41
|
-
*
|
|
61
|
+
* This function gives the static reads of an expression. A host can use them to recompute a program only when a field
|
|
62
|
+
* that it reads changes. A computed move reads a key that only a run knows. Its address keeps the expression, and the
|
|
63
|
+
* reads of that expression are in the list too (DEPS.COMPUTED). The kernel walks the document with a stack, so an
|
|
64
|
+
* expression of any depth works (EVAL.NESTING).
|
|
42
65
|
*/
|
|
43
66
|
export function deps(e: Expr): readonly Dependency[] {
|
|
44
|
-
const
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
67
|
+
const nodes = new Map<object, Expr>();
|
|
68
|
+
// The order "reads" puts the body or the steps of a loop before its axis, as the reads of an evaluation come.
|
|
69
|
+
const doc = writeDocument(e, {
|
|
70
|
+
// Stryker disable next-line ArrayDeclaration: the reads come from the references, not from the form of a literal
|
|
71
|
+
domains: [],
|
|
72
|
+
// Stryker disable next-line StringLiteral: the writer tests only the mode "json", and each other mode keeps the value
|
|
73
|
+
literals: "host",
|
|
74
|
+
order: "reads",
|
|
75
|
+
nodes,
|
|
76
|
+
});
|
|
77
|
+
const uses = refsOf(VEX_WORLD, doc);
|
|
78
|
+
const memo = new WeakMap<DocVisit, Context>();
|
|
79
|
+
const functions = new WeakMap<DocVisit, DocVisit | null>();
|
|
80
|
+
const fnVisits = new Map<unknown, DocVisit>();
|
|
81
|
+
|
|
82
|
+
/** This function gives the function of a call: the visit of the binding of a let, or `undefined` (FN.CALL). */
|
|
83
|
+
const functionOf = (call: DocVisit): DocVisit | undefined => {
|
|
84
|
+
const known = functions.get(call);
|
|
85
|
+
if (known !== undefined) return known ?? undefined;
|
|
86
|
+
const name = (call.doc.value as { readonly fn: { readonly value: readonly [string, string] } }).fn.value[1];
|
|
87
|
+
let found: DocVisit | undefined;
|
|
88
|
+
// The innermost kind that binds the name decides. A parameter, a binding or a name of a loop hides a function.
|
|
89
|
+
search: for (let child: DocVisit = call, around = call.parent; around !== undefined; child = around, around = around.parent) {
|
|
90
|
+
// Stryker disable next-line OptionalChaining: each document around a call is a term or a function, so it has a kind
|
|
91
|
+
for (const rule of around.kind?.scope ?? []) {
|
|
92
|
+
if (!rule.in.some((pattern) => pathMatches(child.slot, pattern))) continue;
|
|
93
|
+
const source = rule.names as { readonly keysAt?: readonly Key[]; readonly listAt?: readonly Key[] };
|
|
94
|
+
// Stryker disable next-line ArrayDeclaration: each rule of the scopes of Vex gives its names with keysAt or listAt
|
|
95
|
+
const at = (source.keysAt ?? source.listAt ?? []).reduce<unknown>((x, k) => (x as Readonly<Record<string, unknown>>)[String(k)], around.doc.value);
|
|
96
|
+
const names = source.keysAt !== undefined ? Object.keys(at as object) : (at as readonly string[]);
|
|
97
|
+
if (!names.includes(name)) continue;
|
|
98
|
+
// Only a binding of a let is a function (FN.BIND), and the map has only the documents of the functions.
|
|
99
|
+
found = fnVisits.get((at as Readonly<Record<string, unknown>>)[name]);
|
|
100
|
+
break search;
|
|
101
|
+
}
|
|
60
102
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
103
|
+
functions.set(call, found ?? null);
|
|
104
|
+
return found;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** This function gives the context of a child from the context of its parent. */
|
|
108
|
+
const step = (outer: Context, parent: DocVisit, child: DocVisit): Context => {
|
|
109
|
+
const kind = kindOf(parent);
|
|
110
|
+
const head = child.slot[0];
|
|
111
|
+
let { axes, fn, calls } = outer;
|
|
112
|
+
let axis: Around | undefined;
|
|
113
|
+
if (outer.axis !== undefined) {
|
|
114
|
+
// In the axis document of a loop: a test of where is inside the axis, and the list of the axis list is outside.
|
|
115
|
+
// Only the axis where has a test.
|
|
116
|
+
if (head === "test") axes = { item: outer.axis, up: axes };
|
|
117
|
+
else if (head === "axis") axis = outer.axis;
|
|
118
|
+
} else if (kind === "each" || kind === "fold") {
|
|
119
|
+
const node = nodes.get(parent.doc) as ExprOf<"each" | "fold">;
|
|
120
|
+
const around: Around = { t: node.axis.t, at: node.at ?? [] };
|
|
121
|
+
if (head === "body" || head === "step") axes = { item: around, up: axes };
|
|
122
|
+
else if (head === "axis") axis = around;
|
|
66
123
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
// Inside the axis: the body or the values of the step, and the tests of where. The values of init, the result of a
|
|
71
|
-
// fold, the list of the axis list and the expressions of an address evaluate at the node, outside the axis.
|
|
72
|
-
const first = x.tag === "fold" ? Object.keys(x.init).length : 0;
|
|
73
|
-
const last = axial ? all.length - addrExprs(x.at).length - (x.tag === "fold" ? 1 : 0) : 0;
|
|
74
|
-
// The list of the axis list comes after the body or the values of the step, before the tests of where.
|
|
75
|
-
const listAt = axial && listOf(x.axis) !== undefined ? (x.tag === "fold" ? first + Object.keys(x.step).length : 1) : -1;
|
|
76
|
-
const axes = axial ? [...item.axes, x.axis.t] : item.axes;
|
|
77
|
-
const axisAt = axial ? [...item.axisAt, x.at ?? []] : item.axisAt;
|
|
78
|
-
for (let i = all.length - 1; i >= 0; i--) {
|
|
79
|
-
const inside = i >= first && i < last && i !== listAt;
|
|
80
|
-
todo.push({ x: all[i] as Expr, axes: inside ? axes : item.axes, axisAt: inside ? axisAt : item.axisAt, fns: fnsOf(i) });
|
|
124
|
+
if (kind === "fn") {
|
|
125
|
+
fn = parent;
|
|
126
|
+
fnVisits.set(parent.doc, parent);
|
|
81
127
|
}
|
|
128
|
+
// Each call goes in the chain, also a call of a name that is not a function: inCall looks for one call of a function.
|
|
129
|
+
if (kind === "call") calls = { item: parent, up: calls };
|
|
130
|
+
return { axes, axis, fn, calls };
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* This function gives the context of a visit. It walks up to a known context with a loop, not with recursion. The
|
|
135
|
+
* context of no visit is the context of the root.
|
|
136
|
+
*/
|
|
137
|
+
const contextOf = (v: DocVisit | undefined): Context => {
|
|
138
|
+
const todo: DocVisit[] = [];
|
|
139
|
+
let x: DocVisit | undefined = v;
|
|
140
|
+
while (x !== undefined && !memo.has(x)) {
|
|
141
|
+
todo.push(x);
|
|
142
|
+
x = x.parent;
|
|
143
|
+
}
|
|
144
|
+
let ctx = x === undefined ? ROOT : (memo.get(x) as Context);
|
|
145
|
+
for (let i = todo.length - 1; i >= 0; i--) {
|
|
146
|
+
const child = todo[i] as DocVisit;
|
|
147
|
+
ctx = child.parent === undefined ? ROOT : step(ctx, child.parent, child);
|
|
148
|
+
memo.set(child, ctx);
|
|
149
|
+
}
|
|
150
|
+
return ctx;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// The references of each function, apart from the others: they read at each call of the function.
|
|
154
|
+
const groups = new Map<DocVisit | undefined, RefUse[]>();
|
|
155
|
+
for (const use of uses) {
|
|
156
|
+
const fn = contextOf(use.visit).fn;
|
|
157
|
+
const group = groups.get(fn) ?? [];
|
|
158
|
+
group.push(use);
|
|
159
|
+
groups.set(fn, group);
|
|
82
160
|
}
|
|
161
|
+
|
|
162
|
+
const out: Dependency[] = [];
|
|
163
|
+
/** This function gives the reads of the references of a function at a call, inside the axes of the call. */
|
|
164
|
+
const emit = (group: readonly RefUse[], base: DocVisit | undefined, outside: readonly Around[]): void => {
|
|
165
|
+
const baseAxes = contextOf(base).axes;
|
|
166
|
+
const inside = (v: DocVisit): readonly Around[] => [...outside, ...itemsUntil(contextOf(v).axes, baseAxes)];
|
|
167
|
+
// The reads of a call come after the reads of its arguments, so a call waits until the walk leaves it.
|
|
168
|
+
const pending: { readonly call: DocVisit; readonly fn: DocVisit; readonly axes: readonly Around[] }[] = [];
|
|
169
|
+
const inCall = (v: DocVisit | undefined, call: DocVisit): boolean => {
|
|
170
|
+
for (let c = contextOf(v).calls; c !== undefined && c.item.depth >= call.depth; c = c.up) if (c.item === call) return true;
|
|
171
|
+
return false;
|
|
172
|
+
};
|
|
173
|
+
const flush = (until?: DocVisit): void => {
|
|
174
|
+
for (;;) {
|
|
175
|
+
const top = pending.at(-1);
|
|
176
|
+
if (top === undefined || inCall(until, top.call)) return;
|
|
177
|
+
pending.pop();
|
|
178
|
+
emit(groups.get(top.fn) ?? [], top.fn, top.axes);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
for (const use of group) {
|
|
182
|
+
flush(use.visit);
|
|
183
|
+
if (use.root === "self" && !use.address) {
|
|
184
|
+
const node = nodes.get(use.visit.doc) as ExprOf<"ref">;
|
|
185
|
+
const axes = inside(use.visit);
|
|
186
|
+
out.push({ at: node.at ?? [], path: node.path, axes: axes.map((a) => a.t), axisAt: axes.map((a) => a.at) });
|
|
187
|
+
}
|
|
188
|
+
const call = use.visit.parent;
|
|
189
|
+
if (call !== undefined && kindOf(call) === "call" && use.visit.slot[0] === "fn") {
|
|
190
|
+
const fn = functionOf(call);
|
|
191
|
+
if (fn !== undefined) pending.push({ call, fn, axes: inside(call) });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
flush();
|
|
195
|
+
};
|
|
196
|
+
emit(groups.get(undefined) ?? [], undefined, []);
|
|
83
197
|
return out;
|
|
84
198
|
}
|