@mark1russell7/vex 0.6.0 → 0.8.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 +244 -33
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1093 -122
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/builder.ts +222 -28
- package/src/deps.ts +57 -8
- package/src/domain.ts +22 -0
- package/src/errors.ts +3 -1
- package/src/evaluate.ts +275 -20
- package/src/ir.ts +238 -51
- package/src/json.ts +44 -2
- package/src/list.ts +3 -3
- package/src/sheet.ts +259 -21
- package/src/space.ts +52 -11
package/src/ir.ts
CHANGED
|
@@ -3,15 +3,21 @@
|
|
|
3
3
|
* shape as the expressions of `render`.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* One move of an address. The moves of an address apply in order, from the focus of the evaluation. The key of `key`
|
|
8
|
+
* and the position of `index` can be an expression, which evaluates before the moves (NAV.COMPUTED).
|
|
9
|
+
*/
|
|
7
10
|
export type Move =
|
|
8
|
-
| { readonly t: "key"; readonly key: string }
|
|
9
|
-
| { readonly t: "index"; readonly i: number }
|
|
11
|
+
| { readonly t: "key"; readonly key: string | Expr }
|
|
12
|
+
| { readonly t: "index"; readonly i: number | Expr }
|
|
10
13
|
| { readonly t: "other" }
|
|
11
14
|
| { readonly t: "offset"; readonly d: readonly number[] }
|
|
12
15
|
| { readonly t: "origin" }
|
|
13
16
|
| { readonly t: "parent" };
|
|
14
17
|
|
|
18
|
+
/** A move with a fixed key or position: a move with no expression. */
|
|
19
|
+
export type FixedMove = Exclude<Move, { readonly t: "key" | "index" }> | { readonly t: "key"; readonly key: string } | { readonly t: "index"; readonly i: number };
|
|
20
|
+
|
|
15
21
|
/** An address: a list of moves. The empty list is the focus. */
|
|
16
22
|
export type Addr = readonly Move[];
|
|
17
23
|
|
|
@@ -30,6 +36,8 @@ export type Axis =
|
|
|
30
36
|
| { readonly t: "successors" }
|
|
31
37
|
| { readonly t: "predecessors" }
|
|
32
38
|
| { readonly t: "reachable" }
|
|
39
|
+
| { readonly t: "list"; readonly list: Expr }
|
|
40
|
+
| { readonly t: "reverse"; readonly axis: Axis }
|
|
33
41
|
| { readonly t: "where"; readonly axis: Axis; readonly test: Expr };
|
|
34
42
|
|
|
35
43
|
/** A Vex expression. */
|
|
@@ -41,8 +49,18 @@ export type Expr =
|
|
|
41
49
|
| { readonly tag: "var"; readonly name: string }
|
|
42
50
|
| { readonly tag: "rec"; readonly fields: Readonly<Record<string, Expr>> }
|
|
43
51
|
| { readonly tag: "each"; readonly axis: Axis; readonly body: Expr; readonly at?: Addr }
|
|
52
|
+
| { readonly tag: "fold"; readonly axis: Axis; readonly init: Names; readonly step: Names; readonly result: Expr; readonly at?: Addr }
|
|
53
|
+
| { readonly tag: "iterate"; readonly limit: number; readonly init: Names; readonly step: Names; readonly until?: Expr; readonly result: Expr }
|
|
54
|
+
| { readonly tag: "fn"; readonly params: readonly string[]; readonly body: Expr }
|
|
55
|
+
| { readonly tag: "call"; readonly fn: string; readonly args: readonly Expr[] }
|
|
44
56
|
| { readonly tag: "ext"; readonly kind: string; readonly data: unknown };
|
|
45
57
|
|
|
58
|
+
/** The names of a loop and an expression for each (LOOP.NAMES). */
|
|
59
|
+
export type Names = Readonly<Record<string, Expr>>;
|
|
60
|
+
|
|
61
|
+
/** The largest number of steps of `iterate` (ITERATE.LIMIT). */
|
|
62
|
+
export const MAX_ITERATIONS = 100_000;
|
|
63
|
+
|
|
46
64
|
/** The tag of an expression kind. */
|
|
47
65
|
export type ExprTag = Expr["tag"];
|
|
48
66
|
|
|
@@ -84,23 +102,68 @@ export function each(axis: Axis, body: Expr, at?: Addr): ExprOf<"each"> {
|
|
|
84
102
|
return at === undefined || at.length === 0 ? { tag: "each", axis, body } : { tag: "each", axis, body, at };
|
|
85
103
|
}
|
|
86
104
|
|
|
105
|
+
/**
|
|
106
|
+
* This function makes a fold: a step at each target of `axis`, in order. `init` gives the names and their first
|
|
107
|
+
* values, and `step` gives the next values. `result` gives the value from the last values (LOOP.NAMES, FOLD.ORDER).
|
|
108
|
+
*/
|
|
109
|
+
export function fold(axis: Axis, init: Names, step: Names, result: Expr, at?: Addr): ExprOf<"fold"> {
|
|
110
|
+
return at === undefined || at.length === 0 ? { tag: "fold", axis, init, step, result } : { tag: "fold", axis, init, step, result, at };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* This function makes an iteration: up to `limit` steps. `init` gives the names and their first values, and `step`
|
|
115
|
+
* gives the next values. A true `until` stops it, and `result` gives the value (ITERATE.LIMIT, ITERATE.UNTIL).
|
|
116
|
+
*/
|
|
117
|
+
export function iterate(limit: number, init: Names, step: Names, result: Expr, until?: Expr): ExprOf<"iterate"> {
|
|
118
|
+
return until === undefined ? { tag: "iterate", limit, init, step, result } : { tag: "iterate", limit, init, step, until, result };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* This function makes a function (FN.BIND): the names of its parameters and its body. A function is only the value of
|
|
123
|
+
* a binding of `let`. The body sees the names of the place of that `let`, so a function cannot call itself.
|
|
124
|
+
*/
|
|
125
|
+
export const fn = (params: readonly string[], body: Expr): ExprOf<"fn"> => ({ tag: "fn", params, body });
|
|
126
|
+
|
|
127
|
+
/** This function makes a call of a bound function (FN.CALL). The body evaluates at the position of the call. */
|
|
128
|
+
export const call = (name: string, ...args: Expr[]): ExprOf<"call"> => ({ tag: "call", fn: name, args });
|
|
129
|
+
|
|
87
130
|
/** This function makes an expression of an extension kind. */
|
|
88
131
|
export const ext = (kind: string, data: unknown): ExprOf<"ext"> => ({ tag: "ext", kind, data });
|
|
89
132
|
|
|
90
133
|
// ---------------------------------------------------------------- moves and axes
|
|
91
134
|
|
|
92
|
-
/** The move to an absolute key. */
|
|
93
|
-
export
|
|
94
|
-
|
|
95
|
-
export
|
|
135
|
+
/** The move to an absolute key. The key can be an expression that gives a text. */
|
|
136
|
+
export function key(k: string): FixedMove;
|
|
137
|
+
export function key(k: string | Expr): Move;
|
|
138
|
+
export function key(k: string | Expr): Move {
|
|
139
|
+
return { t: "key", key: k };
|
|
140
|
+
}
|
|
141
|
+
/** The move to a position in the key order. The position can be an expression that gives a number. */
|
|
142
|
+
export function index(i: number): FixedMove;
|
|
143
|
+
export function index(i: number | Expr): Move;
|
|
144
|
+
export function index(i: number | Expr): Move {
|
|
145
|
+
return { t: "index", i };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** This function gives the expression of a move with a computed key or position, or `undefined`. */
|
|
149
|
+
export function moveExpr(m: Move): Expr | undefined {
|
|
150
|
+
if (m.t === "key") return typeof m.key === "string" ? undefined : m.key;
|
|
151
|
+
return m.t === "index" && typeof m.i !== "number" ? m.i : undefined;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** This function gives the expressions of the computed moves of an address, in the order of the moves. */
|
|
155
|
+
export const addrExprs = (at: Addr | undefined): readonly Expr[] => (at ?? []).flatMap((m) => moveExpr(m) ?? []);
|
|
156
|
+
|
|
157
|
+
/** This function tells if each move of an address is fixed. */
|
|
158
|
+
export const isFixedAddr = (at: Addr | undefined): at is readonly FixedMove[] => addrExprs(at).length === 0;
|
|
96
159
|
/** The move to the other key of a space with two keys. */
|
|
97
|
-
export const other:
|
|
160
|
+
export const other: FixedMove = Object.freeze({ t: "other" });
|
|
98
161
|
/** The relative move in an array (one number) or a grid (two numbers). */
|
|
99
|
-
export const offset = (...d: readonly number[]):
|
|
162
|
+
export const offset = (...d: readonly number[]): FixedMove => ({ t: "offset", d });
|
|
100
163
|
/** The move back to the origin of the evaluation. */
|
|
101
|
-
export const origin:
|
|
164
|
+
export const origin: FixedMove = Object.freeze({ t: "origin" });
|
|
102
165
|
/** The move to the parent of the focus in a tree space. */
|
|
103
|
-
export const parent:
|
|
166
|
+
export const parent: FixedMove = Object.freeze({ t: "parent" });
|
|
104
167
|
|
|
105
168
|
/** The axes. */
|
|
106
169
|
export const axes = {
|
|
@@ -136,6 +199,16 @@ export const axes = {
|
|
|
136
199
|
predecessors: Object.freeze({ t: "predecessors" }) as Axis,
|
|
137
200
|
/** Each key that a path of edges reaches from the focus, in breadth-first order, without the focus. */
|
|
138
201
|
reachable: Object.freeze({ t: "reachable" }) as Axis,
|
|
202
|
+
/**
|
|
203
|
+
* The targets of `axis` in the reverse order (AXIS.REVERSE). The inner axis is a base axis: not `where`, `reverse` or
|
|
204
|
+
* `table`. A test goes outside: `where(reverse(axis), test)`.
|
|
205
|
+
*/
|
|
206
|
+
reverse: (axis: Axis): Axis => ({ t: "reverse", axis }),
|
|
207
|
+
/**
|
|
208
|
+
* The keys of the items of the list that `list` gives, in the order of the list (AXIS.LIST). The expression evaluates
|
|
209
|
+
* before the targets. An error item gives an error item for its target.
|
|
210
|
+
*/
|
|
211
|
+
list: (list: Expr): Axis => ({ t: "list", list }),
|
|
139
212
|
/** The targets of `axis` where `test` gives `true`. */
|
|
140
213
|
where: (axis: Axis, test: Expr): Axis => ({ t: "where", axis, test }),
|
|
141
214
|
} as const;
|
|
@@ -146,123 +219,237 @@ const isObject = (u: unknown): u is Readonly<Record<string, unknown>> => typeof
|
|
|
146
219
|
|
|
147
220
|
const isStringArray = (u: unknown): u is readonly string[] => Array.isArray(u) && u.every((s) => typeof s === "string");
|
|
148
221
|
|
|
149
|
-
/** This function tells if a value is a valid move. */
|
|
222
|
+
/** This function tells if a value is a valid move. The expression of a computed move must be valid too. */
|
|
150
223
|
export function isMove(u: unknown): u is Move {
|
|
151
|
-
|
|
224
|
+
const exprs = moveParts(u);
|
|
225
|
+
return exprs !== undefined && exprs.every(isExpr);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* A test of the numbers of a move or of a window. The JSON form needs whole numbers. An expression in code can have any
|
|
230
|
+
* number, and a number that is not whole has a meaning at run time (EVAL.SHAPE).
|
|
231
|
+
*/
|
|
232
|
+
type NumberTest = (x: unknown) => boolean;
|
|
233
|
+
|
|
234
|
+
const anyNumber: NumberTest = (x) => typeof x === "number";
|
|
235
|
+
|
|
236
|
+
/** This function checks the fixed parts of a move. It gives the expressions of the move, or `undefined` for a bad move. */
|
|
237
|
+
function moveParts(u: unknown, isNumber: NumberTest = Number.isInteger): readonly unknown[] | undefined {
|
|
238
|
+
if (!isObject(u)) return undefined;
|
|
152
239
|
switch (u["t"]) {
|
|
153
240
|
case "key":
|
|
154
|
-
return typeof u["key"] === "string";
|
|
241
|
+
return typeof u["key"] === "string" ? [] : isObject(u["key"]) ? [u["key"]] : undefined;
|
|
155
242
|
case "index":
|
|
156
|
-
return
|
|
243
|
+
return isNumber(u["i"]) ? [] : isObject(u["i"]) ? [u["i"]] : undefined;
|
|
157
244
|
case "offset":
|
|
158
|
-
return Array.isArray(u["d"]) && u["d"].every((n) =>
|
|
245
|
+
return Array.isArray(u["d"]) && u["d"].every((n) => isNumber(n)) ? [] : undefined;
|
|
159
246
|
case "other":
|
|
160
247
|
case "origin":
|
|
161
248
|
case "parent":
|
|
162
|
-
return
|
|
249
|
+
return [];
|
|
163
250
|
default:
|
|
164
|
-
return
|
|
251
|
+
return undefined;
|
|
165
252
|
}
|
|
166
253
|
}
|
|
167
254
|
|
|
168
255
|
/** This function tells if a value is a valid axis. It checks the tests of `where` too. */
|
|
169
256
|
export function isAxis(u: unknown): u is Axis {
|
|
170
|
-
return
|
|
257
|
+
return badPart([{ axis: true, u }], true) === undefined;
|
|
171
258
|
}
|
|
172
259
|
|
|
173
260
|
/** This function tells if a value is a valid expression. It checks the whole tree. */
|
|
174
261
|
export function isExpr(u: unknown): u is Expr {
|
|
175
|
-
return
|
|
262
|
+
return badPart([{ axis: false, u }], true) === undefined;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The tags of the expressions of this version of Vex. */
|
|
266
|
+
const TAGS: ReadonlySet<unknown> = new Set(["lit", "ref", "app", "let", "var", "rec", "each", "fold", "iterate", "fn", "call", "ext"]);
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* This function gives the reason that a value is not a valid expression, or `undefined` for a valid expression
|
|
270
|
+
* (EVAL.SHAPE). An expression from a newer version of Vex can have a tag that this version does not know. Unlike
|
|
271
|
+
* `isExpr`, it accepts a number that is not whole in a move or in a window, because it has a meaning at run time.
|
|
272
|
+
*/
|
|
273
|
+
export function shapeError(u: unknown): string | undefined {
|
|
274
|
+
const bad = badPart([{ axis: false, u }], false);
|
|
275
|
+
if (bad === undefined) return undefined;
|
|
276
|
+
const tag = isObject(bad.u) ? bad.u["tag"] : undefined;
|
|
277
|
+
if (bad.axis) return "the expression has an axis that is not valid";
|
|
278
|
+
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`;
|
|
279
|
+
if (tag === "fn") return "the expression has a function outside the bindings of a let, or a function with parameters that are not distinct names";
|
|
280
|
+
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";
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* This function checks the names of a loop: `init` and `step` are records, and each name of `step` is a name of `init`.
|
|
285
|
+
* It gives the expressions of `init`, of `step` and `result`, or `undefined` (LOOP.NAMES).
|
|
286
|
+
*/
|
|
287
|
+
function loopParts(u: Readonly<Record<string, unknown>>): readonly unknown[] | undefined {
|
|
288
|
+
const { init, step } = u;
|
|
289
|
+
if (!isObject(init) || Array.isArray(init) || !isObject(step) || Array.isArray(step)) return undefined;
|
|
290
|
+
if (!Object.keys(step).every((n) => Object.hasOwn(init, n))) return undefined;
|
|
291
|
+
return [...Object.values(init), ...Object.values(step), u["result"]];
|
|
176
292
|
}
|
|
177
293
|
|
|
178
|
-
|
|
294
|
+
/** This function checks the fixed parts of an address. It gives the expressions of its moves, or `undefined`. */
|
|
295
|
+
function addrParts(u: unknown, isNumber: NumberTest): readonly unknown[] | undefined {
|
|
296
|
+
if (u === undefined) return [];
|
|
297
|
+
if (!Array.isArray(u)) return undefined;
|
|
298
|
+
const out: unknown[] = [];
|
|
299
|
+
for (const m of u) {
|
|
300
|
+
const parts = moveParts(m, isNumber);
|
|
301
|
+
if (parts === undefined) return undefined;
|
|
302
|
+
out.push(...parts);
|
|
303
|
+
}
|
|
304
|
+
return out;
|
|
305
|
+
}
|
|
179
306
|
|
|
180
307
|
/**
|
|
181
|
-
* This function checks a tree of expressions and axes
|
|
182
|
-
* example, a deep JSON file from a stranger does not need a deep call
|
|
308
|
+
* This function checks a tree of expressions and axes, and it gives the first part that is not valid. It uses a stack,
|
|
309
|
+
* not recursion, so the input can be deep. For example, a deep JSON file from a stranger does not need a deep call
|
|
310
|
+
* stack (EVAL.NESTING). With `whole`, the numbers of the moves and of the windows must be whole, as the JSON form needs.
|
|
183
311
|
*/
|
|
184
|
-
function
|
|
312
|
+
function badPart(todo: { readonly axis: boolean; readonly u: unknown }[], whole: boolean): { readonly axis: boolean; readonly u: unknown } | undefined {
|
|
313
|
+
const moveNumber = whole ? Number.isInteger : anyNumber;
|
|
314
|
+
const windowNumber = whole ? Number.isSafeInteger : anyNumber;
|
|
185
315
|
const push = (axis: boolean, values: readonly unknown[]): void => {
|
|
186
316
|
for (const u of values) todo.push({ axis, u });
|
|
187
317
|
};
|
|
188
318
|
for (let item = todo.pop(); item !== undefined; item = todo.pop()) {
|
|
189
319
|
const { u } = item;
|
|
190
|
-
if (!isObject(u)) return
|
|
320
|
+
if (!isObject(u)) return item;
|
|
191
321
|
if (item.axis) {
|
|
192
322
|
const t = u["t"];
|
|
193
323
|
if (t === "neighbors") {
|
|
194
|
-
if (u["n"] !== 4 && u["n"] !== 8) return
|
|
324
|
+
if (u["n"] !== 4 && u["n"] !== 8) return item;
|
|
195
325
|
} else if (t === "window") {
|
|
196
|
-
if (!
|
|
326
|
+
if (!windowNumber(u["from"]) || !windowNumber(u["to"])) return item;
|
|
197
327
|
} else if (t === "table") {
|
|
198
|
-
if (typeof u["name"] !== "string") return
|
|
328
|
+
if (typeof u["name"] !== "string") return item;
|
|
199
329
|
} else if (t === "where") {
|
|
200
330
|
push(true, [u["axis"]]);
|
|
201
331
|
push(false, [u["test"]]);
|
|
332
|
+
} else if (t === "list") {
|
|
333
|
+
push(false, [u["list"]]);
|
|
334
|
+
} else if (t === "reverse") {
|
|
335
|
+
// The inner axis of reverse is a base axis.
|
|
336
|
+
const inner = u["axis"];
|
|
337
|
+
if (!isObject(inner) || ["where", "reverse", "table"].includes(inner["t"] as string)) return item;
|
|
338
|
+
push(true, [inner]);
|
|
202
339
|
} else if (!["all", "others", "other", "children", "ancestors", "descendants", "siblings", "successors", "predecessors", "reachable"].includes(t as string)) {
|
|
203
|
-
return
|
|
340
|
+
return item;
|
|
204
341
|
}
|
|
205
342
|
continue;
|
|
206
343
|
}
|
|
207
344
|
switch (u["tag"]) {
|
|
208
345
|
case "lit":
|
|
209
|
-
if (!("value" in u)) return
|
|
346
|
+
if (!("value" in u)) return item;
|
|
210
347
|
break;
|
|
211
|
-
case "ref":
|
|
212
|
-
|
|
348
|
+
case "ref": {
|
|
349
|
+
const at = addrParts(u["at"], moveNumber);
|
|
350
|
+
if (!isStringArray(u["path"]) || at === undefined) return item;
|
|
351
|
+
push(false, at);
|
|
213
352
|
break;
|
|
353
|
+
}
|
|
214
354
|
case "app":
|
|
215
|
-
if (typeof u["op"] !== "string" || !Array.isArray(u["args"])) return
|
|
355
|
+
if (typeof u["op"] !== "string" || !Array.isArray(u["args"])) return item;
|
|
216
356
|
push(false, u["args"]);
|
|
217
357
|
break;
|
|
218
|
-
case "let":
|
|
219
|
-
if (!isObject(u["bind"]) || Array.isArray(u["bind"])) return
|
|
220
|
-
|
|
358
|
+
case "let": {
|
|
359
|
+
if (!isObject(u["bind"]) || Array.isArray(u["bind"])) return item;
|
|
360
|
+
// A binding can be a function (FN.BIND): its parameters are distinct names, and its body is an expression.
|
|
361
|
+
const values = Object.values(u["bind"]).map((b) => (isObject(b) && b["tag"] === "fn" && isStringArray(b["params"]) && new Set(b["params"]).size === b["params"].length ? b["body"] : b));
|
|
362
|
+
push(false, [...values, u["body"]]);
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
case "call":
|
|
366
|
+
if (typeof u["fn"] !== "string" || !Array.isArray(u["args"])) return item;
|
|
367
|
+
push(false, u["args"]);
|
|
221
368
|
break;
|
|
222
369
|
case "var":
|
|
223
|
-
if (typeof u["name"] !== "string") return
|
|
370
|
+
if (typeof u["name"] !== "string") return item;
|
|
224
371
|
break;
|
|
225
372
|
case "rec":
|
|
226
|
-
if (!isObject(u["fields"]) || Array.isArray(u["fields"])) return
|
|
373
|
+
if (!isObject(u["fields"]) || Array.isArray(u["fields"])) return item;
|
|
227
374
|
push(false, Object.values(u["fields"]));
|
|
228
375
|
break;
|
|
229
|
-
case "each":
|
|
230
|
-
|
|
376
|
+
case "each": {
|
|
377
|
+
const at = addrParts(u["at"], moveNumber);
|
|
378
|
+
if (at === undefined) return item;
|
|
379
|
+
push(true, [u["axis"]]);
|
|
380
|
+
push(false, [u["body"], ...at]);
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
case "fold": {
|
|
384
|
+
const at = addrParts(u["at"], moveNumber);
|
|
385
|
+
const names = loopParts(u);
|
|
386
|
+
if (at === undefined || names === undefined) return item;
|
|
231
387
|
push(true, [u["axis"]]);
|
|
232
|
-
push(false, [
|
|
388
|
+
push(false, [...names, ...at]);
|
|
233
389
|
break;
|
|
390
|
+
}
|
|
391
|
+
case "iterate": {
|
|
392
|
+
const limit = u["limit"];
|
|
393
|
+
const names = loopParts(u);
|
|
394
|
+
if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 0 || limit > MAX_ITERATIONS || names === undefined) return item;
|
|
395
|
+
push(false, [...names, ...(u["until"] === undefined ? [] : [u["until"]])]);
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
234
398
|
case "ext":
|
|
235
|
-
if (typeof u["kind"] !== "string" || !("data" in u)) return
|
|
399
|
+
if (typeof u["kind"] !== "string" || !("data" in u)) return item;
|
|
236
400
|
break;
|
|
237
401
|
default:
|
|
238
|
-
return
|
|
402
|
+
return item;
|
|
239
403
|
}
|
|
240
404
|
}
|
|
241
|
-
return
|
|
405
|
+
return undefined;
|
|
242
406
|
}
|
|
243
407
|
|
|
244
|
-
/**
|
|
408
|
+
/**
|
|
409
|
+
* This function gives the child expressions of an expression, in the order of their child index. The expressions of
|
|
410
|
+
* the computed moves of an address come last. They evaluate at the position of their node, also for an axis.
|
|
411
|
+
*/
|
|
245
412
|
export function children(e: Expr): readonly Expr[] {
|
|
246
413
|
switch (e.tag) {
|
|
247
414
|
case "app":
|
|
415
|
+
case "call":
|
|
248
416
|
return e.args;
|
|
417
|
+
case "fn":
|
|
418
|
+
return [e.body];
|
|
249
419
|
case "let":
|
|
250
420
|
return [...Object.values(e.bind), e.body];
|
|
251
421
|
case "rec":
|
|
252
422
|
return Object.values(e.fields);
|
|
253
423
|
case "each":
|
|
254
|
-
return [e.body, ...
|
|
255
|
-
case "
|
|
424
|
+
return [e.body, ...axisExprs(e.axis), ...addrExprs(e.at)];
|
|
425
|
+
case "fold":
|
|
426
|
+
return [...Object.values(e.init), ...Object.values(e.step), ...axisExprs(e.axis), ...addrExprs(e.at), e.result];
|
|
427
|
+
case "iterate":
|
|
428
|
+
return [...Object.values(e.init), ...Object.values(e.step), ...(e.until === undefined ? [] : [e.until]), e.result];
|
|
256
429
|
case "ref":
|
|
430
|
+
return addrExprs(e.at);
|
|
431
|
+
case "lit":
|
|
257
432
|
case "var":
|
|
258
433
|
case "ext":
|
|
259
434
|
return [];
|
|
260
435
|
}
|
|
261
436
|
}
|
|
262
437
|
|
|
263
|
-
/**
|
|
264
|
-
function
|
|
438
|
+
/**
|
|
439
|
+
* This function gives the expressions of an axis, in the order of their child indexes. The expression of the axis
|
|
440
|
+
* `list` comes first, and then the tests of the `where` axes from the inside out. It uses a loop, not recursion.
|
|
441
|
+
*/
|
|
442
|
+
export function axisExprs(a: Axis): readonly Expr[] {
|
|
265
443
|
const out: Expr[] = [];
|
|
266
|
-
for (let x
|
|
267
|
-
|
|
444
|
+
for (let x = a; x.t === "where"; x = x.axis) out.unshift(x.test);
|
|
445
|
+
const list = listOf(a);
|
|
446
|
+
return list === undefined ? out : [list, ...out];
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** This function gives the expression of the axis `list` inside an axis, through `where` and `reverse`, if it has one. */
|
|
450
|
+
export function listOf(a: Axis): Expr | undefined {
|
|
451
|
+
let x = a;
|
|
452
|
+
while (x.t === "where") x = x.axis;
|
|
453
|
+
const base = x.t === "reverse" ? x.axis : x;
|
|
454
|
+
return base.t === "list" ? base.list : undefined;
|
|
268
455
|
}
|
package/src/json.ts
CHANGED
|
@@ -128,8 +128,36 @@ function mapValues(u: unknown, f: (value: unknown) => Result<unknown>): Result<u
|
|
|
128
128
|
}
|
|
129
129
|
return undefined;
|
|
130
130
|
};
|
|
131
|
+
// The expression of a computed move is a tree of the expression too (NAV.COMPUTED).
|
|
132
|
+
const address = (): Result<unknown> | undefined => {
|
|
133
|
+
const at = u["at"];
|
|
134
|
+
if (!Array.isArray(at)) return undefined;
|
|
135
|
+
const moves: unknown[] = [];
|
|
136
|
+
for (const m of at) {
|
|
137
|
+
const field = isRecord(m) ? (m["t"] === "key" ? "key" : m["t"] === "index" ? "i" : undefined) : undefined;
|
|
138
|
+
if (field === undefined || !isRecord(m) || !isRecord(m[field])) {
|
|
139
|
+
moves.push(m);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const r = mapValues(m[field], f);
|
|
143
|
+
if (!r.ok) return r;
|
|
144
|
+
moves.push({ ...m, [field]: r.value });
|
|
145
|
+
}
|
|
146
|
+
out["at"] = moves;
|
|
147
|
+
return undefined;
|
|
148
|
+
};
|
|
149
|
+
// The test of where and the list of the axis list are expressions, also inside reverse.
|
|
131
150
|
const axis = (a: unknown): Result<unknown> => {
|
|
132
|
-
if (!isRecord(a)
|
|
151
|
+
if (!isRecord(a)) return ok(a);
|
|
152
|
+
if (a["t"] === "list") {
|
|
153
|
+
const list = mapValues(a["list"], f);
|
|
154
|
+
return list.ok ? ok({ ...a, list: list.value }) : list;
|
|
155
|
+
}
|
|
156
|
+
if (a["t"] === "reverse") {
|
|
157
|
+
const inner = axis(a["axis"]);
|
|
158
|
+
return inner.ok ? ok({ ...a, axis: inner.value }) : inner;
|
|
159
|
+
}
|
|
160
|
+
if (a["t"] !== "where") return ok(a);
|
|
133
161
|
const inner = axis(a["axis"]);
|
|
134
162
|
if (!inner.ok) return inner;
|
|
135
163
|
const test = mapValues(a["test"], f);
|
|
@@ -153,8 +181,22 @@ function mapValues(u: unknown, f: (value: unknown) => Result<unknown>): Result<u
|
|
|
153
181
|
const a = axis(u["axis"]);
|
|
154
182
|
if (!a.ok) return a;
|
|
155
183
|
out["axis"] = a.value;
|
|
156
|
-
return child("body") ?? ok(out);
|
|
184
|
+
return child("body") ?? address() ?? ok(out);
|
|
157
185
|
}
|
|
186
|
+
case "fold": {
|
|
187
|
+
const a = axis(u["axis"]);
|
|
188
|
+
if (!a.ok) return a;
|
|
189
|
+
out["axis"] = a.value;
|
|
190
|
+
return each("init") ?? each("step") ?? child("result") ?? address() ?? ok(out);
|
|
191
|
+
}
|
|
192
|
+
case "iterate":
|
|
193
|
+
return each("init") ?? each("step") ?? (u["until"] === undefined ? undefined : child("until")) ?? child("result") ?? ok(out);
|
|
194
|
+
case "fn":
|
|
195
|
+
return child("body") ?? ok(out);
|
|
196
|
+
case "call":
|
|
197
|
+
return each("args") ?? ok(out);
|
|
198
|
+
case "ref":
|
|
199
|
+
return address() ?? ok(out);
|
|
158
200
|
default:
|
|
159
201
|
return ok(out);
|
|
160
202
|
}
|
package/src/list.ts
CHANGED
|
@@ -19,11 +19,11 @@ export const vexList = <T>(items: readonly ListItem<T>[]): VexList<T> => Object.
|
|
|
19
19
|
export const isVexList = (u: unknown): u is VexList =>
|
|
20
20
|
(u as { kind?: unknown } | null | undefined)?.kind === "vex.list" && Array.isArray((u as { items?: unknown }).items);
|
|
21
21
|
|
|
22
|
-
/** The name of a core list op. */
|
|
23
|
-
export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors";
|
|
22
|
+
/** The name of a core list op. `sortBy` and `take` give a list, and the other ops give a value. */
|
|
23
|
+
export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors" | "sortBy" | "take" | "nth";
|
|
24
24
|
|
|
25
25
|
/** The names of the core list ops. They apply when the first argument is a list. */
|
|
26
|
-
export const LIST_OPS: ReadonlySet<string> = new Set<ListOp>(["count", "sum", "min", "max", "mean", "any", "all", "none", "values", "keys", "reduce", "first", "errors"]);
|
|
26
|
+
export const LIST_OPS: ReadonlySet<string> = new Set<ListOp>(["count", "sum", "min", "max", "mean", "any", "all", "none", "values", "keys", "reduce", "first", "errors", "sortBy", "take", "nth"]);
|
|
27
27
|
|
|
28
28
|
/** This function tells if a name is the name of a core list op. */
|
|
29
29
|
export const isListOp = (op: string): op is ListOp => LIST_OPS.has(op);
|