@mark1russell7/vex 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +93 -12
- package/build/index.d.ts.map +1 -1
- package/build/index.js +315 -74
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/builder.ts +56 -3
- package/src/errors.ts +4 -0
- package/src/evaluate.ts +24 -6
- package/src/ir.ts +26 -1
- package/src/sheet.ts +142 -59
- package/src/space.ts +131 -1
package/src/space.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { Addr, Axis, Move } from "./ir.ts";
|
|
|
3
3
|
import { fail, ok, type Result } from "./result.ts";
|
|
4
4
|
|
|
5
5
|
/** The kind of a space. The kind sets which moves and axes are valid. */
|
|
6
|
-
export type SpaceKind = "record" | "array" | "grid" | "tree";
|
|
6
|
+
export type SpaceKind = "record" | "array" | "grid" | "tree" | "graph";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* A space: an immutable collection of records with keys. A Vex program evaluates at one key of a space.
|
|
@@ -25,6 +25,10 @@ export interface Space<K extends string = string, O = unknown> {
|
|
|
25
25
|
parentOf?(k: string): K | undefined;
|
|
26
26
|
/** The children of the key `k` in a tree space, in key order. Other spaces do not have it. */
|
|
27
27
|
childrenOf?(k: string): readonly K[];
|
|
28
|
+
/** The keys at the ends of the edges from `k`, in a graph space or a tree space. Other spaces do not have it. */
|
|
29
|
+
successorsOf?(k: string): readonly K[];
|
|
30
|
+
/** The keys with an edge to `k`, in key order, in a graph space or a tree space. Other spaces do not have it. */
|
|
31
|
+
predecessorsOf?(k: string): readonly K[];
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
/** The key type of a grid space. */
|
|
@@ -169,6 +173,14 @@ class TreeSpace<K extends string, O> implements Space<K, O> {
|
|
|
169
173
|
keyAt(): K | undefined {
|
|
170
174
|
return undefined;
|
|
171
175
|
}
|
|
176
|
+
// In a tree, the edges go from a parent to its children.
|
|
177
|
+
successorsOf(k: string): readonly K[] {
|
|
178
|
+
return this.childrenOf(k);
|
|
179
|
+
}
|
|
180
|
+
predecessorsOf(k: string): readonly K[] {
|
|
181
|
+
const p = this.#parents.get(k);
|
|
182
|
+
return p === undefined ? [] : [p];
|
|
183
|
+
}
|
|
172
184
|
parentOf(k: string): K | undefined {
|
|
173
185
|
return this.#parents.get(k);
|
|
174
186
|
}
|
|
@@ -178,6 +190,63 @@ class TreeSpace<K extends string, O> implements Space<K, O> {
|
|
|
178
190
|
}
|
|
179
191
|
|
|
180
192
|
/** The constructors of spaces. */
|
|
193
|
+
/** The empty list of keys. */
|
|
194
|
+
const NO_KEYS: readonly never[] = Object.freeze([]);
|
|
195
|
+
|
|
196
|
+
class GraphSpace<K extends string, O> implements Space<K, O> {
|
|
197
|
+
readonly kind: SpaceKind = "graph";
|
|
198
|
+
readonly keys: readonly K[];
|
|
199
|
+
readonly #records: ReadonlyMap<string, O>;
|
|
200
|
+
readonly #out: ReadonlyMap<string, readonly K[]>;
|
|
201
|
+
readonly #in: ReadonlyMap<string, readonly K[]>;
|
|
202
|
+
|
|
203
|
+
constructor(records: Readonly<Record<K, O>>, edges: Readonly<Partial<Record<K, readonly K[]>>>) {
|
|
204
|
+
const entries = Object.entries(records) as [K, O][];
|
|
205
|
+
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
206
|
+
this.#records = new Map(entries);
|
|
207
|
+
const out = new Map<string, readonly K[]>();
|
|
208
|
+
for (const [k, to] of Object.entries(edges) as [K, readonly K[] | undefined][]) {
|
|
209
|
+
if (!this.#records.has(k)) throw new TypeError(`the graph has no key "${k}", but the edges name it`);
|
|
210
|
+
// A second edge to the same key is the same edge.
|
|
211
|
+
const ends = [...new Set(to ?? NO_KEYS)];
|
|
212
|
+
for (const t of ends) if (!this.#records.has(t)) throw new TypeError(`the edge from "${k}" goes to "${t}", which is not a key of the graph`);
|
|
213
|
+
out.set(k, Object.freeze(ends));
|
|
214
|
+
}
|
|
215
|
+
this.#out = out;
|
|
216
|
+
// The predecessors in key order: the keys in key order, and the ends of the edges of each key.
|
|
217
|
+
const into = new Map<string, K[]>();
|
|
218
|
+
for (const k of this.keys) {
|
|
219
|
+
for (const t of out.get(k) ?? NO_KEYS) {
|
|
220
|
+
const from = into.get(t);
|
|
221
|
+
if (from === undefined) into.set(t, [k]);
|
|
222
|
+
else from.push(k);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
this.#in = new Map([...into].map(([k, from]) => [k, Object.freeze(from)]));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
has(k: string): k is K {
|
|
229
|
+
return this.#records.has(k);
|
|
230
|
+
}
|
|
231
|
+
get(k: string): O | undefined {
|
|
232
|
+
return this.#records.get(k);
|
|
233
|
+
}
|
|
234
|
+
// Stryker disable next-line BlockStatement: the body gives undefined, and an empty body gives undefined too
|
|
235
|
+
coords(): readonly number[] | undefined {
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
// Stryker disable next-line BlockStatement: the body gives undefined, and an empty body gives undefined too
|
|
239
|
+
keyAt(): K | undefined {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
successorsOf(k: string): readonly K[] {
|
|
243
|
+
return this.#out.get(k) ?? NO_KEYS;
|
|
244
|
+
}
|
|
245
|
+
predecessorsOf(k: string): readonly K[] {
|
|
246
|
+
return this.#in.get(k) ?? NO_KEYS;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
181
250
|
export const space = {
|
|
182
251
|
/** This function makes a space from the entries of an object. The keys are the property names. */
|
|
183
252
|
record<const R extends Readonly<Record<string, unknown>>>(records: R): Space<keyof R & string, R[keyof R]> {
|
|
@@ -198,6 +267,13 @@ export const space = {
|
|
|
198
267
|
tree<const R extends Readonly<Record<string, unknown>>>(records: R, parents: Readonly<Partial<Record<keyof R & string, (keyof R & string) | null>>>): Space<keyof R & string, R[keyof R]> {
|
|
199
268
|
return new TreeSpace<keyof R & string, R[keyof R]>(records, parents);
|
|
200
269
|
},
|
|
270
|
+
/**
|
|
271
|
+
* This function makes a graph space: the records by key, and the edges from each key, in order. An edge to a key
|
|
272
|
+
* that is not in the graph throws a `TypeError`. A graph can have cycles, and an edge can go from a key to itself.
|
|
273
|
+
*/
|
|
274
|
+
graph<const R extends Readonly<Record<string, unknown>>>(records: R, edges: Readonly<Partial<Record<keyof R & string, readonly (keyof R & string)[]>>>): Space<keyof R & string, R[keyof R]> {
|
|
275
|
+
return new GraphSpace<keyof R & string, R[keyof R]>(records, edges);
|
|
276
|
+
},
|
|
201
277
|
} as const;
|
|
202
278
|
|
|
203
279
|
/** The position of an evaluation: the key where it started, and the key where relative references read. */
|
|
@@ -335,5 +411,59 @@ export function axisTargets(s: Space, at: Position, a: Exclude<Axis, { readonly
|
|
|
335
411
|
case "descendants":
|
|
336
412
|
case "siblings":
|
|
337
413
|
return treeTargets(s, at, a.t);
|
|
414
|
+
case "window":
|
|
415
|
+
return windowTargets(s, at, a.from, a.to);
|
|
416
|
+
case "successors":
|
|
417
|
+
case "predecessors":
|
|
418
|
+
case "reachable":
|
|
419
|
+
return graphTargets(s, at, a.t);
|
|
420
|
+
case "table":
|
|
421
|
+
// The evaluator gives the axis table the space of its table. Here, without the tables, it has no targets.
|
|
422
|
+
return fail(vexError("no-table", `the axis "table" needs the tables of the evaluation, and "${a.name}" is not one of them`, at));
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* This function gives the targets of an axis of a graph. `reachable` has each key that a path of one or more edges
|
|
428
|
+
* reaches from the focus, once, in the order of a breadth-first search. The focus is not a target of `reachable`.
|
|
429
|
+
*/
|
|
430
|
+
function graphTargets(s: Space, at: Position, t: "successors" | "predecessors" | "reachable"): Result<readonly string[]> {
|
|
431
|
+
const successorsOf = s.successorsOf?.bind(s);
|
|
432
|
+
const predecessorsOf = s.predecessorsOf?.bind(s);
|
|
433
|
+
if (successorsOf === undefined || predecessorsOf === undefined) {
|
|
434
|
+
return fail(vexError("no-graph", `"${t}" needs a graph space or a tree space, but this space is a ${s.kind}`, at));
|
|
435
|
+
}
|
|
436
|
+
if (t === "successors") return ok(successorsOf(at.focus));
|
|
437
|
+
if (t === "predecessors") return ok(predecessorsOf(at.focus));
|
|
438
|
+
// A queue, not recursion, so a long path does not need a deep call stack. A loop over an array also reads the
|
|
439
|
+
// keys that the loop adds to it.
|
|
440
|
+
const seen = new Set<string>([at.focus]);
|
|
441
|
+
const queue: string[] = [at.focus];
|
|
442
|
+
for (const k of queue) {
|
|
443
|
+
for (const next of successorsOf(k)) {
|
|
444
|
+
if (seen.has(next)) continue;
|
|
445
|
+
seen.add(next);
|
|
446
|
+
queue.push(next);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return ok(queue.slice(1));
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* This function gives the targets of the axis `window(from, to)`: the keys of the moves `offset(d)` for each `d`
|
|
454
|
+
* from `from` to `to`, in order. An offset outside the space has no target. The axis needs an array space or a tree
|
|
455
|
+
* space, where an offset has one number.
|
|
456
|
+
*/
|
|
457
|
+
function windowTargets(s: Space, at: Position, from: number, to: number): Result<readonly string[]> {
|
|
458
|
+
const tree = s.parentOf !== undefined && s.childrenOf !== undefined;
|
|
459
|
+
if (!tree && s.coords(at.focus)?.length !== 1) {
|
|
460
|
+
return fail(vexError("no-offset", `"window" needs an array space or a tree space, but this space is a ${s.kind}`, at));
|
|
461
|
+
}
|
|
462
|
+
// An offset larger than the number of keys has no target, so the loop stays in the size of the space.
|
|
463
|
+
const out: string[] = [];
|
|
464
|
+
for (let d = Math.max(from, -s.keys.length); d <= Math.min(to, s.keys.length); d++) {
|
|
465
|
+
const r = applyMove(s, at, { t: "offset", d: [d] });
|
|
466
|
+
if (r.ok) out.push(r.value);
|
|
338
467
|
}
|
|
468
|
+
return ok(out);
|
|
339
469
|
}
|