@mark1russell7/vex 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/build/index.d.ts +884 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1839 -0
- package/build/index.js.map +1 -0
- package/package.json +44 -3
- package/src/builder.ts +649 -0
- package/src/deps.ts +33 -0
- package/src/domain.ts +135 -0
- package/src/errors.ts +102 -0
- package/src/evaluate.ts +628 -0
- package/src/index.ts +16 -0
- package/src/ir.ts +212 -0
- package/src/json.ts +76 -0
- package/src/list.ts +35 -0
- package/src/result.ts +44 -0
- package/src/sheet.ts +141 -0
- package/src/space.ts +322 -0
- package/src/trace.ts +37 -0
- package/src/traversal.ts +126 -0
- package/README.md +0 -3
package/build/index.js
ADDED
|
@@ -0,0 +1,1839 @@
|
|
|
1
|
+
//#region src/result.ts
|
|
2
|
+
const NONE = Object.freeze({ tag: "none" });
|
|
3
|
+
/** This function makes an `Optional` that holds `value`. */
|
|
4
|
+
const some = (value) => ({
|
|
5
|
+
tag: "some",
|
|
6
|
+
value
|
|
7
|
+
});
|
|
8
|
+
/** This function gives the shared empty `Optional`. */
|
|
9
|
+
const none = () => NONE;
|
|
10
|
+
/** This function makes a successful `Result`. */
|
|
11
|
+
const ok = (value) => ({
|
|
12
|
+
ok: true,
|
|
13
|
+
value
|
|
14
|
+
});
|
|
15
|
+
/** This function makes a failed `Result`. */
|
|
16
|
+
const fail = (error) => ({
|
|
17
|
+
ok: false,
|
|
18
|
+
error
|
|
19
|
+
});
|
|
20
|
+
/** This function tells if an `Optional` holds a value. */
|
|
21
|
+
const isSome = (o) => o.tag === "some";
|
|
22
|
+
/** This function tells if an `Optional` is empty. */
|
|
23
|
+
const isNone = (o) => o.tag === "none";
|
|
24
|
+
/** This function changes a `Result` into an `Optional`. The error is lost. */
|
|
25
|
+
const toOptional = (r) => r.ok ? some(r.value) : none();
|
|
26
|
+
/** This function gives the value of an `Optional`, or `fallback` if it is empty. */
|
|
27
|
+
const getOr = (o, fallback) => o.tag === "some" ? o.value : fallback;
|
|
28
|
+
/** This function applies `f` to the value of a `Result`. An error stays the same. */
|
|
29
|
+
const mapResult = (r, f) => r.ok ? ok(f(r.value)) : r;
|
|
30
|
+
/** This function applies `f` to the value of a `Result`, and `f` gives a `Result`. An error stays the same. */
|
|
31
|
+
const chainResult = (r, f) => r.ok ? f(r.value) : r;
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/errors.ts
|
|
34
|
+
const CODE_OF = {
|
|
35
|
+
"unknown-key": "#REF!",
|
|
36
|
+
"not-a-pair": "#REF!",
|
|
37
|
+
"out-of-bounds": "#REF!",
|
|
38
|
+
"no-offset": "#REF!",
|
|
39
|
+
"no-grid": "#REF!",
|
|
40
|
+
"no-tree": "#REF!",
|
|
41
|
+
"missing-field": "#N/A",
|
|
42
|
+
empty: "#N/A",
|
|
43
|
+
"not-instance": "#VALUE!",
|
|
44
|
+
"kind-mismatch": "#VALUE!",
|
|
45
|
+
"bad-expression": "#VALUE!",
|
|
46
|
+
"unknown-op": "#NAME?",
|
|
47
|
+
unbound: "#NAME?",
|
|
48
|
+
"not-finite": "#NUM!",
|
|
49
|
+
"invalid-value": "#NUM!",
|
|
50
|
+
threw: "#CALC!",
|
|
51
|
+
"undefined-result": "#CALC!",
|
|
52
|
+
args: "#ARGS",
|
|
53
|
+
cycle: "#CYCLE!"
|
|
54
|
+
};
|
|
55
|
+
/** This function gives the code of an error kind. */
|
|
56
|
+
const codeOf = (kind) => CODE_OF[kind];
|
|
57
|
+
/** This function makes a `VexError`. */
|
|
58
|
+
function vexError(kind, message, details = {}) {
|
|
59
|
+
return {
|
|
60
|
+
code: CODE_OF[kind],
|
|
61
|
+
kind,
|
|
62
|
+
message,
|
|
63
|
+
path: details.path ?? [],
|
|
64
|
+
...details.origin === void 0 ? {} : { origin: details.origin },
|
|
65
|
+
...details.focus === void 0 ? {} : { focus: details.focus },
|
|
66
|
+
...details.op === void 0 ? {} : { op: details.op },
|
|
67
|
+
...details.causes === void 0 ? {} : { causes: details.causes },
|
|
68
|
+
...details.thrown === void 0 ? {} : { thrown: details.thrown }
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** This function gives a short text for an error, for example `#REF! unknown-key: there is no key "Z"`. */
|
|
72
|
+
const formatError = (e) => `${e.code} ${e.kind}: ${e.message}`;
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/ir.ts
|
|
75
|
+
/** This function makes a literal. */
|
|
76
|
+
const lit = (value) => ({
|
|
77
|
+
tag: "lit",
|
|
78
|
+
value
|
|
79
|
+
});
|
|
80
|
+
/** This function splits a dotted path, for example `"pos.x"`, into its segments. */
|
|
81
|
+
const toPath = (path) => typeof path === "string" ? path.split(".").filter((s) => s.length > 0) : path;
|
|
82
|
+
/** This function makes a reference to a field path. With `at`, the reference reads at that address. */
|
|
83
|
+
function ref(path, at) {
|
|
84
|
+
const segments = toPath(path);
|
|
85
|
+
return at === void 0 || at.length === 0 ? {
|
|
86
|
+
tag: "ref",
|
|
87
|
+
path: segments
|
|
88
|
+
} : {
|
|
89
|
+
tag: "ref",
|
|
90
|
+
path: segments,
|
|
91
|
+
at
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** This function makes the application of an op to arguments. For a method op, the first argument is the receiver. */
|
|
95
|
+
const app = (op, ...args) => ({
|
|
96
|
+
tag: "app",
|
|
97
|
+
op,
|
|
98
|
+
args
|
|
99
|
+
});
|
|
100
|
+
/** This function binds names to expressions for the body. */
|
|
101
|
+
const let_ = (bind, body) => ({
|
|
102
|
+
tag: "let",
|
|
103
|
+
bind,
|
|
104
|
+
body
|
|
105
|
+
});
|
|
106
|
+
/** This function makes a reference to a bound name. */
|
|
107
|
+
const v = (name) => ({
|
|
108
|
+
tag: "var",
|
|
109
|
+
name
|
|
110
|
+
});
|
|
111
|
+
/** This function makes a record from field expressions. */
|
|
112
|
+
const rec = (fields) => ({
|
|
113
|
+
tag: "rec",
|
|
114
|
+
fields
|
|
115
|
+
});
|
|
116
|
+
/** This function evaluates `body` once for each target of `axis`. The result is a list. */
|
|
117
|
+
const each = (axis, body) => ({
|
|
118
|
+
tag: "each",
|
|
119
|
+
axis,
|
|
120
|
+
body
|
|
121
|
+
});
|
|
122
|
+
/** This function makes an expression of an extension kind. */
|
|
123
|
+
const ext = (kind, data) => ({
|
|
124
|
+
tag: "ext",
|
|
125
|
+
kind,
|
|
126
|
+
data
|
|
127
|
+
});
|
|
128
|
+
/** The move to an absolute key. */
|
|
129
|
+
const key = (k) => ({
|
|
130
|
+
t: "key",
|
|
131
|
+
key: k
|
|
132
|
+
});
|
|
133
|
+
/** The move to a position in the key order. */
|
|
134
|
+
const index = (i) => ({
|
|
135
|
+
t: "index",
|
|
136
|
+
i
|
|
137
|
+
});
|
|
138
|
+
/** The move to the other key of a space with two keys. */
|
|
139
|
+
const other = Object.freeze({ t: "other" });
|
|
140
|
+
/** The relative move in an array (one number) or a grid (two numbers). */
|
|
141
|
+
const offset = (...d) => ({
|
|
142
|
+
t: "offset",
|
|
143
|
+
d
|
|
144
|
+
});
|
|
145
|
+
/** The move back to the origin of the evaluation. */
|
|
146
|
+
const origin = Object.freeze({ t: "origin" });
|
|
147
|
+
/** The move to the parent of the focus in a tree space. */
|
|
148
|
+
const parent = Object.freeze({ t: "parent" });
|
|
149
|
+
/** The axes. */
|
|
150
|
+
const axes = {
|
|
151
|
+
/** Each key of the space. */
|
|
152
|
+
all: Object.freeze({ t: "all" }),
|
|
153
|
+
/** Each key except the focus. */
|
|
154
|
+
others: Object.freeze({ t: "others" }),
|
|
155
|
+
/** The other key of a pair. */
|
|
156
|
+
other: Object.freeze({ t: "other" }),
|
|
157
|
+
/** The neighbors of the focus in a grid. */
|
|
158
|
+
neighbors: (n = 8) => ({
|
|
159
|
+
t: "neighbors",
|
|
160
|
+
n
|
|
161
|
+
}),
|
|
162
|
+
/** The children of the focus in a tree, in key order. */
|
|
163
|
+
children: Object.freeze({ t: "children" }),
|
|
164
|
+
/** The ancestors of the focus in a tree: the parent first, the root last. */
|
|
165
|
+
ancestors: Object.freeze({ t: "ancestors" }),
|
|
166
|
+
/** The descendants of the focus in a tree, depth first, each parent before its children. */
|
|
167
|
+
descendants: Object.freeze({ t: "descendants" }),
|
|
168
|
+
/** The other children of the parent of the focus. For a root, the other roots. */
|
|
169
|
+
siblings: Object.freeze({ t: "siblings" }),
|
|
170
|
+
/** The targets of `axis` where `test` gives `true`. */
|
|
171
|
+
where: (axis, test) => ({
|
|
172
|
+
t: "where",
|
|
173
|
+
axis,
|
|
174
|
+
test
|
|
175
|
+
})
|
|
176
|
+
};
|
|
177
|
+
const isObject = (u) => typeof u === "object" && u !== null;
|
|
178
|
+
const isStringArray = (u) => Array.isArray(u) && u.every((s) => typeof s === "string");
|
|
179
|
+
/** This function tells if a value is a valid move. */
|
|
180
|
+
function isMove(u) {
|
|
181
|
+
if (!isObject(u)) return false;
|
|
182
|
+
switch (u["t"]) {
|
|
183
|
+
case "key": return typeof u["key"] === "string";
|
|
184
|
+
case "index": return typeof u["i"] === "number" && Number.isInteger(u["i"]);
|
|
185
|
+
case "offset": return Array.isArray(u["d"]) && u["d"].every((n) => typeof n === "number" && Number.isInteger(n));
|
|
186
|
+
case "other":
|
|
187
|
+
case "origin":
|
|
188
|
+
case "parent": return true;
|
|
189
|
+
default: return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/** This function tells if a value is a valid axis. */
|
|
193
|
+
function isAxis(u) {
|
|
194
|
+
if (!isObject(u)) return false;
|
|
195
|
+
switch (u["t"]) {
|
|
196
|
+
case "all":
|
|
197
|
+
case "others":
|
|
198
|
+
case "other":
|
|
199
|
+
case "children":
|
|
200
|
+
case "ancestors":
|
|
201
|
+
case "descendants":
|
|
202
|
+
case "siblings": return true;
|
|
203
|
+
case "neighbors": return u["n"] === 4 || u["n"] === 8;
|
|
204
|
+
case "where": return isAxis(u["axis"]) && isExpr(u["test"]);
|
|
205
|
+
default: return false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const isExprRecord = (u) => isObject(u) && !Array.isArray(u) && Object.values(u).every((e) => isExpr(e));
|
|
209
|
+
/** This function tells if a value is a valid expression. It checks the whole tree. */
|
|
210
|
+
function isExpr(u) {
|
|
211
|
+
if (!isObject(u)) return false;
|
|
212
|
+
switch (u["tag"]) {
|
|
213
|
+
case "lit": return "value" in u;
|
|
214
|
+
case "ref": return isStringArray(u["path"]) && (u["at"] === void 0 || Array.isArray(u["at"]) && u["at"].every(isMove));
|
|
215
|
+
case "app": return typeof u["op"] === "string" && Array.isArray(u["args"]) && u["args"].every((a) => isExpr(a));
|
|
216
|
+
case "let": return isExprRecord(u["bind"]) && isExpr(u["body"]);
|
|
217
|
+
case "var": return typeof u["name"] === "string";
|
|
218
|
+
case "rec": return isExprRecord(u["fields"]);
|
|
219
|
+
case "each": return isAxis(u["axis"]) && isExpr(u["body"]);
|
|
220
|
+
case "ext": return typeof u["kind"] === "string" && "data" in u;
|
|
221
|
+
default: return false;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/** This function gives the child expressions of an expression, in the order of their child index. */
|
|
225
|
+
function children(e) {
|
|
226
|
+
switch (e.tag) {
|
|
227
|
+
case "app": return e.args;
|
|
228
|
+
case "let": return [...Object.values(e.bind), e.body];
|
|
229
|
+
case "rec": return Object.values(e.fields);
|
|
230
|
+
case "each": return e.axis.t === "where" ? [e.body, ...whereTests(e.axis)] : [e.body];
|
|
231
|
+
case "lit":
|
|
232
|
+
case "ref":
|
|
233
|
+
case "var":
|
|
234
|
+
case "ext": return [];
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function whereTests(a) {
|
|
238
|
+
return a.t === "where" ? [...whereTests(a.axis), a.test] : [];
|
|
239
|
+
}
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/space.ts
|
|
242
|
+
var RecordSpace = class {
|
|
243
|
+
kind = "record";
|
|
244
|
+
keys;
|
|
245
|
+
#records;
|
|
246
|
+
constructor(records) {
|
|
247
|
+
const entries = Object.entries(records);
|
|
248
|
+
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
249
|
+
this.#records = new Map(entries);
|
|
250
|
+
}
|
|
251
|
+
has(k) {
|
|
252
|
+
return this.#records.has(k);
|
|
253
|
+
}
|
|
254
|
+
get(k) {
|
|
255
|
+
return this.#records.get(k);
|
|
256
|
+
}
|
|
257
|
+
coords() {}
|
|
258
|
+
keyAt() {}
|
|
259
|
+
};
|
|
260
|
+
var ArraySpace = class {
|
|
261
|
+
kind = "array";
|
|
262
|
+
keys;
|
|
263
|
+
#items;
|
|
264
|
+
constructor(items) {
|
|
265
|
+
const entries = items.map((item, i) => [`${i}`, item]);
|
|
266
|
+
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
267
|
+
this.#items = new Map(entries);
|
|
268
|
+
}
|
|
269
|
+
has(k) {
|
|
270
|
+
return this.#items.has(k);
|
|
271
|
+
}
|
|
272
|
+
get(k) {
|
|
273
|
+
return this.#items.get(k);
|
|
274
|
+
}
|
|
275
|
+
coords(k) {
|
|
276
|
+
return this.has(k) ? [Number(k)] : void 0;
|
|
277
|
+
}
|
|
278
|
+
keyAt(coords) {
|
|
279
|
+
const k = coords.join(",");
|
|
280
|
+
return coords.length === 1 && this.has(k) ? k : void 0;
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
var GridSpace = class {
|
|
284
|
+
kind = "grid";
|
|
285
|
+
keys;
|
|
286
|
+
#cells;
|
|
287
|
+
#coords;
|
|
288
|
+
constructor(rows) {
|
|
289
|
+
const entries = rows.flatMap((row, i) => row.map((cell, j) => [
|
|
290
|
+
`${i},${j}`,
|
|
291
|
+
cell,
|
|
292
|
+
Object.freeze([i, j])
|
|
293
|
+
]));
|
|
294
|
+
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
295
|
+
this.#cells = new Map(entries.map(([k, cell]) => [k, cell]));
|
|
296
|
+
this.#coords = new Map(entries.map(([k, , c]) => [k, c]));
|
|
297
|
+
}
|
|
298
|
+
has(k) {
|
|
299
|
+
return this.#cells.has(k);
|
|
300
|
+
}
|
|
301
|
+
get(k) {
|
|
302
|
+
return this.#cells.get(k);
|
|
303
|
+
}
|
|
304
|
+
coords(k) {
|
|
305
|
+
return this.#coords.get(k);
|
|
306
|
+
}
|
|
307
|
+
keyAt(coords) {
|
|
308
|
+
const k = coords.join(",");
|
|
309
|
+
return coords.length === 2 && this.has(k) ? k : void 0;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
var TreeSpace = class {
|
|
313
|
+
kind = "tree";
|
|
314
|
+
keys;
|
|
315
|
+
#records;
|
|
316
|
+
#parents;
|
|
317
|
+
#children;
|
|
318
|
+
constructor(records, parents) {
|
|
319
|
+
const entries = Object.entries(records);
|
|
320
|
+
this.keys = Object.freeze(entries.map(([k]) => k));
|
|
321
|
+
this.#records = new Map(entries);
|
|
322
|
+
const parentOf = /* @__PURE__ */ new Map();
|
|
323
|
+
for (const [k, p] of Object.entries(parents)) {
|
|
324
|
+
if (!this.#records.has(k)) throw new TypeError(`the tree has no key "${k}", but the parents name it`);
|
|
325
|
+
if (p === null || p === void 0) continue;
|
|
326
|
+
if (!this.#records.has(p)) throw new TypeError(`the parent "${p}" of "${k}" is not a key of the tree`);
|
|
327
|
+
parentOf.set(k, p);
|
|
328
|
+
}
|
|
329
|
+
const reachesRoot = /* @__PURE__ */ new Set();
|
|
330
|
+
for (const k of this.keys) {
|
|
331
|
+
const walk = /* @__PURE__ */ new Set();
|
|
332
|
+
for (let p = k; p !== void 0 && !reachesRoot.has(p); p = parentOf.get(p)) {
|
|
333
|
+
if (walk.has(p)) throw new TypeError(`the parents of "${k}" make a cycle, so the space is not a tree`);
|
|
334
|
+
walk.add(p);
|
|
335
|
+
}
|
|
336
|
+
for (const w of walk) reachesRoot.add(w);
|
|
337
|
+
}
|
|
338
|
+
this.#parents = parentOf;
|
|
339
|
+
const children = new Map(this.keys.map((k) => [k, []]));
|
|
340
|
+
for (const k of this.keys) {
|
|
341
|
+
const p = parentOf.get(k);
|
|
342
|
+
if (p !== void 0) children.get(p)?.push(k);
|
|
343
|
+
}
|
|
344
|
+
this.#children = new Map([...children].map(([k, c]) => [k, Object.freeze(c)]));
|
|
345
|
+
}
|
|
346
|
+
has(k) {
|
|
347
|
+
return this.#records.has(k);
|
|
348
|
+
}
|
|
349
|
+
get(k) {
|
|
350
|
+
return this.#records.get(k);
|
|
351
|
+
}
|
|
352
|
+
coords() {}
|
|
353
|
+
keyAt() {}
|
|
354
|
+
parentOf(k) {
|
|
355
|
+
return this.#parents.get(k);
|
|
356
|
+
}
|
|
357
|
+
childrenOf(k) {
|
|
358
|
+
return this.#children.get(k) ?? [];
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
/** The constructors of spaces. */
|
|
362
|
+
const space = {
|
|
363
|
+
/** This function makes a space from the entries of an object. The keys are the property names. */
|
|
364
|
+
record(records) {
|
|
365
|
+
return new RecordSpace(records);
|
|
366
|
+
},
|
|
367
|
+
/** This function makes a space from an array. The keys are `"0"`, `"1"` and so on. */
|
|
368
|
+
array(items) {
|
|
369
|
+
return new ArraySpace(items);
|
|
370
|
+
},
|
|
371
|
+
/** This function makes a space from rows of cells. The key of a cell is `"row,column"`. */
|
|
372
|
+
grid(rows) {
|
|
373
|
+
return new GridSpace(rows);
|
|
374
|
+
},
|
|
375
|
+
/**
|
|
376
|
+
* This function makes a tree space: the records by key, and the parent of each key. A key without a parent, or
|
|
377
|
+
* with the parent `null`, is a root. A parent that is not a key, or a cycle of parents, throws a `TypeError`.
|
|
378
|
+
*/
|
|
379
|
+
tree(records, parents) {
|
|
380
|
+
return new TreeSpace(records, parents);
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
/** This function applies one move to the focus. It gives the new focus, or a `#REF!` error. */
|
|
384
|
+
function applyMove(s, at, m) {
|
|
385
|
+
const refError = (kind, message) => fail(vexError(kind, message, {
|
|
386
|
+
origin: at.origin,
|
|
387
|
+
focus: at.focus
|
|
388
|
+
}));
|
|
389
|
+
switch (m.t) {
|
|
390
|
+
case "key": return s.has(m.key) ? ok(m.key) : refError("unknown-key", `the space has no key "${m.key}"`);
|
|
391
|
+
case "index": {
|
|
392
|
+
const k = s.keys[m.i];
|
|
393
|
+
return m.i >= 0 && k !== void 0 ? ok(k) : refError("out-of-bounds", `the space has no key at index ${m.i}`);
|
|
394
|
+
}
|
|
395
|
+
case "other": {
|
|
396
|
+
if (s.keys.length !== 2) return refError("not-a-pair", `"other" needs a space with 2 keys, but this space has ${s.keys.length}`);
|
|
397
|
+
const [a, b] = s.keys;
|
|
398
|
+
/* v8 ignore next -- @preserve */
|
|
399
|
+
return ok(at.focus === a ? b ?? at.focus : a ?? at.focus);
|
|
400
|
+
}
|
|
401
|
+
case "offset": {
|
|
402
|
+
const c = s.coords(at.focus);
|
|
403
|
+
if (c === void 0 || c.length !== m.d.length) return refError("no-offset", `an offset of length ${m.d.length} is not valid in a ${s.kind} space`);
|
|
404
|
+
/* v8 ignore next -- @preserve */
|
|
405
|
+
const target = c.map((x, i) => x + (m.d[i] ?? 0));
|
|
406
|
+
const k = s.keyAt(target);
|
|
407
|
+
return k === void 0 ? refError("out-of-bounds", `the offset [${m.d.join(",")}] from "${at.focus}" is outside the space`) : ok(k);
|
|
408
|
+
}
|
|
409
|
+
case "origin": return ok(at.origin);
|
|
410
|
+
case "parent": {
|
|
411
|
+
if (s.parentOf === void 0) return refError("no-tree", `"parent" needs a tree space, but this space is a ${s.kind}`);
|
|
412
|
+
const p = s.parentOf(at.focus);
|
|
413
|
+
return p === void 0 ? refError("out-of-bounds", `the key "${at.focus}" is a root, so it has no parent`) : ok(p);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
/** This function applies the moves of an address in order. It gives the key where the address points. */
|
|
418
|
+
function resolveAddr(s, at, addr) {
|
|
419
|
+
let focus = at.focus;
|
|
420
|
+
for (const m of addr ?? []) {
|
|
421
|
+
const r = applyMove(s, {
|
|
422
|
+
origin: at.origin,
|
|
423
|
+
focus
|
|
424
|
+
}, m);
|
|
425
|
+
if (!r.ok) return r;
|
|
426
|
+
focus = r.value;
|
|
427
|
+
}
|
|
428
|
+
return ok(focus);
|
|
429
|
+
}
|
|
430
|
+
const NEIGHBORS_4 = [
|
|
431
|
+
[-1, 0],
|
|
432
|
+
[0, -1],
|
|
433
|
+
[0, 1],
|
|
434
|
+
[1, 0]
|
|
435
|
+
];
|
|
436
|
+
const NEIGHBORS_8 = [
|
|
437
|
+
[-1, -1],
|
|
438
|
+
[-1, 0],
|
|
439
|
+
[-1, 1],
|
|
440
|
+
[0, -1],
|
|
441
|
+
[0, 1],
|
|
442
|
+
[1, -1],
|
|
443
|
+
[1, 0],
|
|
444
|
+
[1, 1]
|
|
445
|
+
];
|
|
446
|
+
function treeTargets(s, at, t) {
|
|
447
|
+
const parentOf = s.parentOf?.bind(s);
|
|
448
|
+
const childrenOf = s.childrenOf?.bind(s);
|
|
449
|
+
if (parentOf === void 0 || childrenOf === void 0) return fail(vexError("no-tree", `"${t}" needs a tree space, but this space is a ${s.kind}`, at));
|
|
450
|
+
switch (t) {
|
|
451
|
+
case "children": return ok(childrenOf(at.focus));
|
|
452
|
+
case "ancestors": {
|
|
453
|
+
const out = [];
|
|
454
|
+
for (let p = parentOf(at.focus); p !== void 0; p = parentOf(p)) out.push(p);
|
|
455
|
+
return ok(out);
|
|
456
|
+
}
|
|
457
|
+
case "descendants": {
|
|
458
|
+
const out = [];
|
|
459
|
+
const stack = childrenOf(at.focus).toReversed();
|
|
460
|
+
for (let k = stack.pop(); k !== void 0; k = stack.pop()) {
|
|
461
|
+
out.push(k);
|
|
462
|
+
stack.push(...childrenOf(k).toReversed());
|
|
463
|
+
}
|
|
464
|
+
return ok(out);
|
|
465
|
+
}
|
|
466
|
+
case "siblings": {
|
|
467
|
+
const p = parentOf(at.focus);
|
|
468
|
+
const group = p === void 0 ? s.keys.filter((k) => parentOf(k) === void 0) : childrenOf(p);
|
|
469
|
+
return ok(group.filter((k) => k !== at.focus));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* This function gives the targets of a base axis at a focus. It does not handle `where`, because the
|
|
475
|
+
* interpreter must evaluate the test.
|
|
476
|
+
*/
|
|
477
|
+
function axisTargets(s, at, a) {
|
|
478
|
+
switch (a.t) {
|
|
479
|
+
case "all": return ok(s.keys);
|
|
480
|
+
case "others": return ok(s.keys.filter((k) => k !== at.focus));
|
|
481
|
+
case "other": {
|
|
482
|
+
const r = applyMove(s, at, { t: "other" });
|
|
483
|
+
return r.ok ? ok([r.value]) : r;
|
|
484
|
+
}
|
|
485
|
+
case "neighbors": {
|
|
486
|
+
if (s.kind !== "grid") return fail(vexError("no-grid", `"neighbors" needs a grid space, but this space is a ${s.kind}`, at));
|
|
487
|
+
const deltas = a.n === 4 ? NEIGHBORS_4 : NEIGHBORS_8;
|
|
488
|
+
return ok(deltas.flatMap((d) => {
|
|
489
|
+
const r = applyMove(s, at, {
|
|
490
|
+
t: "offset",
|
|
491
|
+
d
|
|
492
|
+
});
|
|
493
|
+
return r.ok ? [r.value] : [];
|
|
494
|
+
}));
|
|
495
|
+
}
|
|
496
|
+
case "children":
|
|
497
|
+
case "ancestors":
|
|
498
|
+
case "descendants":
|
|
499
|
+
case "siblings": return treeTargets(s, at, a.t);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
//#endregion
|
|
503
|
+
//#region src/domain.ts
|
|
504
|
+
/** This function makes a domain. The result is frozen. */
|
|
505
|
+
function defineDomain(spec) {
|
|
506
|
+
return Object.freeze({
|
|
507
|
+
...spec,
|
|
508
|
+
kind: "vex.domain"
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
const FORBIDDEN = /* @__PURE__ */ new Set([
|
|
512
|
+
"constructor",
|
|
513
|
+
"__proto__",
|
|
514
|
+
"prototype",
|
|
515
|
+
"__defineGetter__",
|
|
516
|
+
"__defineSetter__",
|
|
517
|
+
"__lookupGetter__",
|
|
518
|
+
"__lookupSetter__"
|
|
519
|
+
]);
|
|
520
|
+
/** This function tells if a name can be an op name. */
|
|
521
|
+
const isOpName = (name) => name.length > 0 && !FORBIDDEN.has(name);
|
|
522
|
+
/**
|
|
523
|
+
* This function finds a method on a value: an own function property, or a function on the prototype chain.
|
|
524
|
+
* It stops at `Object.prototype` and `Function.prototype`, so inherited members (for example `toString`) are not ops.
|
|
525
|
+
*/
|
|
526
|
+
function findMethod(self, name) {
|
|
527
|
+
if (!isOpName(name) || self === null || self === void 0) return void 0;
|
|
528
|
+
if (typeof self !== "object" && typeof self !== "function") return void 0;
|
|
529
|
+
let o = self;
|
|
530
|
+
while (o !== null && o !== Object.prototype && o !== Function.prototype) {
|
|
531
|
+
const d = Object.getOwnPropertyDescriptor(o, name);
|
|
532
|
+
if (d !== void 0) return typeof d.value === "function" ? d.value : void 0;
|
|
533
|
+
o = Object.getPrototypeOf(o);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
const EMPTY_SPEC = Object.freeze({});
|
|
537
|
+
/** This function finds the op `name` for `self` in one domain. It gives `undefined` if the domain does not have it. */
|
|
538
|
+
function resolveOp(domain, self, name) {
|
|
539
|
+
if (!isOpName(name)) return void 0;
|
|
540
|
+
const declared = Object.hasOwn(domain.ops, name) ? domain.ops[name] : void 0;
|
|
541
|
+
if (declared?.fn !== void 0) {
|
|
542
|
+
const fn = declared.fn;
|
|
543
|
+
return {
|
|
544
|
+
domain,
|
|
545
|
+
spec: declared,
|
|
546
|
+
call: (args) => fn(self, ...args)
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
if (declared === void 0 && domain.methods !== "all") return void 0;
|
|
550
|
+
const method = findMethod(self, name);
|
|
551
|
+
if (method === void 0) return void 0;
|
|
552
|
+
return {
|
|
553
|
+
domain,
|
|
554
|
+
spec: declared ?? EMPTY_SPEC,
|
|
555
|
+
call: (args) => method.apply(self, [...args])
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
/** This function gives the domain of a value: the first domain whose `is` accepts it. */
|
|
559
|
+
function domainOf(domains, value) {
|
|
560
|
+
return domains.find((d) => d.is(value));
|
|
561
|
+
}
|
|
562
|
+
/** This function tells if the `liftScalar` setting lifts the argument at `index`. */
|
|
563
|
+
const liftsAt = (lift, index) => lift === true || Array.isArray(lift) && lift[index] === true;
|
|
564
|
+
//#endregion
|
|
565
|
+
//#region src/list.ts
|
|
566
|
+
/** This function makes a list. */
|
|
567
|
+
const vexList = (items) => Object.freeze({
|
|
568
|
+
kind: "vex.list",
|
|
569
|
+
items: Object.freeze([...items])
|
|
570
|
+
});
|
|
571
|
+
/** This function tells if a value is a Vex list. */
|
|
572
|
+
const isVexList = (u) => typeof u === "object" && u !== null && u.kind === "vex.list" && Array.isArray(u.items);
|
|
573
|
+
/** The names of the core list ops. They apply when the first argument is a list. */
|
|
574
|
+
const LIST_OPS = /* @__PURE__ */ new Set([
|
|
575
|
+
"count",
|
|
576
|
+
"sum",
|
|
577
|
+
"min",
|
|
578
|
+
"max",
|
|
579
|
+
"mean",
|
|
580
|
+
"any",
|
|
581
|
+
"all",
|
|
582
|
+
"none",
|
|
583
|
+
"values",
|
|
584
|
+
"keys",
|
|
585
|
+
"reduce",
|
|
586
|
+
"first",
|
|
587
|
+
"errors"
|
|
588
|
+
]);
|
|
589
|
+
/** This function tells if a name is the name of a core list op. */
|
|
590
|
+
const isListOp = (op) => LIST_OPS.has(op);
|
|
591
|
+
//#endregion
|
|
592
|
+
//#region src/evaluate.ts
|
|
593
|
+
/** The names of the special forms. Their arguments are not evaluated before the call. */
|
|
594
|
+
const SPECIAL_FORMS = /* @__PURE__ */ new Set([
|
|
595
|
+
"if",
|
|
596
|
+
"and",
|
|
597
|
+
"or",
|
|
598
|
+
"ifError"
|
|
599
|
+
]);
|
|
600
|
+
const isSpecialForm = (op) => SPECIAL_FORMS.has(op);
|
|
601
|
+
/**
|
|
602
|
+
* This function compiles an expression. The result gives the same results and the same traces as `evaluate`,
|
|
603
|
+
* because `evaluate` uses it too.
|
|
604
|
+
*/
|
|
605
|
+
function compile(expr, opts = {}) {
|
|
606
|
+
const root = new Compiler(opts).node(expr, []);
|
|
607
|
+
const run = (r) => {
|
|
608
|
+
const { space, origin } = r;
|
|
609
|
+
if (!space.has(origin)) return fail(vexError("unknown-key", `the space has no key "${origin}"`, {
|
|
610
|
+
origin,
|
|
611
|
+
focus: origin
|
|
612
|
+
}));
|
|
613
|
+
const env = new Map(Object.entries(r.vars ?? {}).map(([k, val]) => [k, ok(val)]));
|
|
614
|
+
try {
|
|
615
|
+
return root({
|
|
616
|
+
pos: {
|
|
617
|
+
origin,
|
|
618
|
+
focus: origin
|
|
619
|
+
},
|
|
620
|
+
env
|
|
621
|
+
}, {
|
|
622
|
+
space,
|
|
623
|
+
trace: r.trace
|
|
624
|
+
});
|
|
625
|
+
} catch (thrown) {
|
|
626
|
+
return fail(vexError("threw", "the evaluation threw an exception", {
|
|
627
|
+
origin,
|
|
628
|
+
focus: origin,
|
|
629
|
+
thrown
|
|
630
|
+
}));
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
return {
|
|
634
|
+
expr,
|
|
635
|
+
run,
|
|
636
|
+
explain: (r) => {
|
|
637
|
+
const sink = { events: [] };
|
|
638
|
+
const result = run({
|
|
639
|
+
...r,
|
|
640
|
+
trace: sink
|
|
641
|
+
});
|
|
642
|
+
return {
|
|
643
|
+
events: sink.events,
|
|
644
|
+
result
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
const CACHE = /* @__PURE__ */ new WeakMap();
|
|
650
|
+
function compiled(expr, opts) {
|
|
651
|
+
const hit = CACHE.get(expr);
|
|
652
|
+
if (hit !== void 0 && hit.domains === opts.domains && hit.fns === opts.fns && hit.extensions === opts.extensions) return hit.program;
|
|
653
|
+
const program = compile(expr, opts);
|
|
654
|
+
CACHE.set(expr, {
|
|
655
|
+
domains: opts.domains,
|
|
656
|
+
fns: opts.fns,
|
|
657
|
+
extensions: opts.extensions,
|
|
658
|
+
program
|
|
659
|
+
});
|
|
660
|
+
return program;
|
|
661
|
+
}
|
|
662
|
+
/** This function evaluates an expression at the origin of a space. It does not throw. */
|
|
663
|
+
function evaluate(expr, opts) {
|
|
664
|
+
return compiled(expr, opts).run(opts);
|
|
665
|
+
}
|
|
666
|
+
/** This function evaluates an expression and records a trace of each node. */
|
|
667
|
+
function explain(expr, opts) {
|
|
668
|
+
return compiled(expr, opts).explain(opts);
|
|
669
|
+
}
|
|
670
|
+
var Compiler = class {
|
|
671
|
+
#domains;
|
|
672
|
+
#fns;
|
|
673
|
+
#extensions;
|
|
674
|
+
constructor(opts) {
|
|
675
|
+
this.#domains = opts.domains ?? [];
|
|
676
|
+
this.#fns = opts.fns;
|
|
677
|
+
this.#extensions = opts.extensions;
|
|
678
|
+
}
|
|
679
|
+
#err(kind, message, f, path, extra = {}) {
|
|
680
|
+
return fail(vexError(kind, message, {
|
|
681
|
+
path,
|
|
682
|
+
origin: f.pos.origin,
|
|
683
|
+
focus: f.pos.focus,
|
|
684
|
+
...extra
|
|
685
|
+
}));
|
|
686
|
+
}
|
|
687
|
+
#emit(c, info, f, result, reads) {
|
|
688
|
+
if (c.trace !== void 0) {
|
|
689
|
+
const event = {
|
|
690
|
+
path: info.path,
|
|
691
|
+
tag: info.tag,
|
|
692
|
+
label: info.label,
|
|
693
|
+
origin: f.pos.origin,
|
|
694
|
+
focus: f.pos.focus,
|
|
695
|
+
...reads === void 0 ? {} : { reads },
|
|
696
|
+
result
|
|
697
|
+
};
|
|
698
|
+
c.trace.events.push(event);
|
|
699
|
+
}
|
|
700
|
+
return result;
|
|
701
|
+
}
|
|
702
|
+
/** This method compiles the expression `e` at the position `path` of the tree. */
|
|
703
|
+
node(e, path) {
|
|
704
|
+
const info = {
|
|
705
|
+
path,
|
|
706
|
+
tag: e.tag,
|
|
707
|
+
label: labelOf(e)
|
|
708
|
+
};
|
|
709
|
+
switch (e.tag) {
|
|
710
|
+
case "lit": {
|
|
711
|
+
const r = ok(e.value);
|
|
712
|
+
return (f, c) => this.#emit(c, info, f, r);
|
|
713
|
+
}
|
|
714
|
+
case "ref": return this.#ref(e, info);
|
|
715
|
+
case "var": {
|
|
716
|
+
const name = e.name;
|
|
717
|
+
return (f, c) => this.#emit(c, info, f, f.env.get(name) ?? this.#err("unbound", `the name "${name}" has no binding`, f, path));
|
|
718
|
+
}
|
|
719
|
+
case "let": {
|
|
720
|
+
const binds = Object.entries(e.bind).map(([name, be], i) => [name, this.node(be, [...path, i])]);
|
|
721
|
+
const body = this.node(e.body, [...path, binds.length]);
|
|
722
|
+
return (f, c) => {
|
|
723
|
+
const env = new Map(f.env);
|
|
724
|
+
for (const [name, n] of binds) env.set(name, n(f, c));
|
|
725
|
+
return this.#emit(c, info, f, body({
|
|
726
|
+
pos: f.pos,
|
|
727
|
+
env
|
|
728
|
+
}, c));
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
case "rec": {
|
|
732
|
+
const names = Object.keys(e.fields);
|
|
733
|
+
const fields = this.#nodes(Object.values(e.fields), path);
|
|
734
|
+
return (f, c) => {
|
|
735
|
+
const values = this.#all(fields, f, c, path, "fields");
|
|
736
|
+
if (!values.ok) return this.#emit(c, info, f, values);
|
|
737
|
+
const out = {};
|
|
738
|
+
names.forEach((n, i) => {
|
|
739
|
+
out[n] = values.value[i];
|
|
740
|
+
});
|
|
741
|
+
return this.#emit(c, info, f, ok(Object.freeze(out)));
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
case "app": return isSpecialForm(e.op) ? this.#special(e.op, this.#nodes(e.args, path), info) : this.#app(e.op, this.#nodes(e.args, path), info);
|
|
745
|
+
case "each": return this.#each(e, info);
|
|
746
|
+
case "ext": {
|
|
747
|
+
const handler = Object.hasOwn(this.#extensions ?? {}, e.kind) ? this.#extensions?.[e.kind] : void 0;
|
|
748
|
+
const { kind, data } = e;
|
|
749
|
+
if (handler === void 0) return (f, c) => this.#emit(c, info, f, this.#err("unknown-op", `no handler for the extension kind "${kind}"`, f, path));
|
|
750
|
+
return (f, c) => this.#emit(c, info, f, this.#locate(this.#guard(() => handler(data, {
|
|
751
|
+
space: c.space,
|
|
752
|
+
position: f.pos,
|
|
753
|
+
path
|
|
754
|
+
}), f, path, kind), f, path));
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
#nodes(es, path) {
|
|
759
|
+
return es.map((x, i) => this.node(x, [...path, i]));
|
|
760
|
+
}
|
|
761
|
+
/** This method gives an error of a handler the path, the origin and the focus of its node, if it has no origin. */
|
|
762
|
+
#locate(r, f, path) {
|
|
763
|
+
if (r.ok || r.error.origin !== void 0) return r;
|
|
764
|
+
return fail({
|
|
765
|
+
...r.error,
|
|
766
|
+
path,
|
|
767
|
+
origin: f.pos.origin,
|
|
768
|
+
focus: f.pos.focus
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
#guard(run, f, path, op) {
|
|
772
|
+
try {
|
|
773
|
+
return run();
|
|
774
|
+
} catch (thrown) {
|
|
775
|
+
return this.#err("threw", `"${op}" threw an exception`, f, path, {
|
|
776
|
+
op,
|
|
777
|
+
thrown
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
#ref(e, info) {
|
|
782
|
+
const { path } = info;
|
|
783
|
+
const segs = e.path;
|
|
784
|
+
const at = e.at ?? [];
|
|
785
|
+
const label = segs.join(".");
|
|
786
|
+
const steps = segs.map((seg, i) => ({
|
|
787
|
+
seg,
|
|
788
|
+
where: segs.slice(0, i + 1).join("."),
|
|
789
|
+
valid: isFieldName(seg)
|
|
790
|
+
}));
|
|
791
|
+
return (f, c) => {
|
|
792
|
+
let key = f.pos.focus;
|
|
793
|
+
if (at.length > 0) {
|
|
794
|
+
const where = resolveAddr(c.space, f.pos, at);
|
|
795
|
+
if (!where.ok) return this.#emit(c, info, f, fail({
|
|
796
|
+
...where.error,
|
|
797
|
+
path
|
|
798
|
+
}));
|
|
799
|
+
key = where.value;
|
|
800
|
+
}
|
|
801
|
+
const failed = (r) => this.#emit(c, info, f, r, [{
|
|
802
|
+
key,
|
|
803
|
+
path: segs,
|
|
804
|
+
ok: false
|
|
805
|
+
}]);
|
|
806
|
+
const missing = (where) => failed(this.#err("missing-field", `the record at "${key}" has no value at "${where}"`, f, path));
|
|
807
|
+
let value = c.space.get(key);
|
|
808
|
+
if (value === void 0 || value === null) return missing(label === "" ? "(record)" : label);
|
|
809
|
+
for (const { seg, where, valid } of steps) {
|
|
810
|
+
if (typeof value !== "object" && typeof value !== "function" || !valid) return missing(where);
|
|
811
|
+
try {
|
|
812
|
+
value = Reflect.get(value, seg);
|
|
813
|
+
} catch (thrown) {
|
|
814
|
+
return failed(this.#err("threw", `reading "${where}" at "${key}" threw an exception`, f, path, { thrown }));
|
|
815
|
+
}
|
|
816
|
+
if (value === void 0 || value === null) return missing(where);
|
|
817
|
+
}
|
|
818
|
+
return this.#emit(c, info, f, ok(value), [{
|
|
819
|
+
key,
|
|
820
|
+
path: segs,
|
|
821
|
+
ok: true
|
|
822
|
+
}]);
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
/** This method evaluates nodes applicatively. One failure stays as it is. Two or more give `#ARGS`. */
|
|
826
|
+
#all(nodes, f, c, path, what) {
|
|
827
|
+
const values = [];
|
|
828
|
+
let first;
|
|
829
|
+
let errors;
|
|
830
|
+
for (const n of nodes) {
|
|
831
|
+
const r = n(f, c);
|
|
832
|
+
if (r.ok) values.push(r.value);
|
|
833
|
+
else if (first === void 0) first = r.error;
|
|
834
|
+
else (errors ??= [first]).push(r.error);
|
|
835
|
+
}
|
|
836
|
+
if (first === void 0) return ok(values);
|
|
837
|
+
return errors === void 0 ? fail(first) : this.#err("args", `${errors.length} ${what} failed`, f, path, { causes: errors });
|
|
838
|
+
}
|
|
839
|
+
#each(e, info) {
|
|
840
|
+
const { path } = info;
|
|
841
|
+
const targets = this.#targets(e.axis, path, { next: 1 });
|
|
842
|
+
const body = this.node(e.body, [...path, 0]);
|
|
843
|
+
return (f, c) => {
|
|
844
|
+
const ts = targets(f, c);
|
|
845
|
+
if (!ts.ok) return this.#emit(c, info, f, ts);
|
|
846
|
+
const items = ts.value.map(({ key, test }) => {
|
|
847
|
+
if (test !== void 0 && !test.ok) return {
|
|
848
|
+
key,
|
|
849
|
+
result: test
|
|
850
|
+
};
|
|
851
|
+
return {
|
|
852
|
+
key,
|
|
853
|
+
result: body({
|
|
854
|
+
pos: {
|
|
855
|
+
origin: f.pos.origin,
|
|
856
|
+
focus: key
|
|
857
|
+
},
|
|
858
|
+
env: f.env
|
|
859
|
+
}, c)
|
|
860
|
+
};
|
|
861
|
+
});
|
|
862
|
+
return this.#emit(c, info, f, ok(vexList(items)));
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
/** This method compiles the targets of an axis. For `where`, a failed test keeps the target with its error. */
|
|
866
|
+
#targets(a, path, counter) {
|
|
867
|
+
if (a.t !== "where") return (f, c) => {
|
|
868
|
+
const keys = axisTargets(c.space, f.pos, a);
|
|
869
|
+
return keys.ok ? ok(keys.value.map((key) => ({ key }))) : fail({
|
|
870
|
+
...keys.error,
|
|
871
|
+
path
|
|
872
|
+
});
|
|
873
|
+
};
|
|
874
|
+
const inner = this.#targets(a.axis, path, counter);
|
|
875
|
+
const testPath = [...path, counter.next++];
|
|
876
|
+
const test = this.node(a.test, testPath);
|
|
877
|
+
return (f, c) => {
|
|
878
|
+
const ts = inner(f, c);
|
|
879
|
+
if (!ts.ok) return ts;
|
|
880
|
+
const out = [];
|
|
881
|
+
for (const t of ts.value) {
|
|
882
|
+
if (t.test !== void 0 && !t.test.ok) {
|
|
883
|
+
out.push(t);
|
|
884
|
+
continue;
|
|
885
|
+
}
|
|
886
|
+
const at = {
|
|
887
|
+
pos: {
|
|
888
|
+
origin: f.pos.origin,
|
|
889
|
+
focus: t.key
|
|
890
|
+
},
|
|
891
|
+
env: f.env
|
|
892
|
+
};
|
|
893
|
+
const r = test(at, c);
|
|
894
|
+
if (!r.ok) out.push({
|
|
895
|
+
key: t.key,
|
|
896
|
+
test: r
|
|
897
|
+
});
|
|
898
|
+
else if (r.value === true) out.push({ key: t.key });
|
|
899
|
+
else if (r.value !== false) out.push({
|
|
900
|
+
key: t.key,
|
|
901
|
+
test: this.#err("kind-mismatch", `the test of "where" gave a ${typeof r.value}, not a boolean`, at, testPath)
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
return ok(out);
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
#app(op, args, info) {
|
|
908
|
+
const { path } = info;
|
|
909
|
+
const listOp = isListOp(op) ? op : void 0;
|
|
910
|
+
const fn = this.#fns !== void 0 && Object.hasOwn(this.#fns, op) ? this.#fns[op] : void 0;
|
|
911
|
+
return (f, c) => {
|
|
912
|
+
const all = this.#all(args, f, c, path, "arguments");
|
|
913
|
+
if (!all.ok) return this.#emit(c, info, f, all);
|
|
914
|
+
const values = all.value;
|
|
915
|
+
const self = values[0];
|
|
916
|
+
if (listOp !== void 0 && isVexList(self)) return this.#emit(c, info, f, this.#listOp(listOp, self, values.slice(1), f, path));
|
|
917
|
+
let accepted;
|
|
918
|
+
if (values.length > 0) for (const d of this.#domains) {
|
|
919
|
+
if (!safeIs(d, self)) continue;
|
|
920
|
+
accepted ??= d;
|
|
921
|
+
const resolved = resolveOp(d, self, op);
|
|
922
|
+
if (resolved !== void 0) return this.#emit(c, info, f, this.#call(resolved, op, values.slice(1), f, path));
|
|
923
|
+
}
|
|
924
|
+
if (fn !== void 0) return this.#emit(c, info, f, this.#checkResult(this.#invoke(() => fn(...values), op, f, path), f, path, op));
|
|
925
|
+
if (accepted !== void 0) return this.#emit(c, info, f, this.#err("unknown-op", `the domain "${accepted.name}" has no op "${op}"`, f, path, { op }));
|
|
926
|
+
if (values.length === 0) return this.#emit(c, info, f, this.#err("unknown-op", `there is no function "${op}"`, f, path, { op }));
|
|
927
|
+
return this.#emit(c, info, f, this.#err("not-instance", `no domain accepts the receiver of "${op}" (${describeType(self)})`, f, path, { op }));
|
|
928
|
+
};
|
|
929
|
+
}
|
|
930
|
+
#special(op, args, info) {
|
|
931
|
+
const { path } = info;
|
|
932
|
+
const arg = (i, f, c) => {
|
|
933
|
+
const n = args[i];
|
|
934
|
+
return n === void 0 ? this.#err("bad-expression", `"${op}" needs argument ${i + 1}`, f, path, { op }) : n(f, c);
|
|
935
|
+
};
|
|
936
|
+
const bool = (i, f, c) => {
|
|
937
|
+
const r = arg(i, f, c);
|
|
938
|
+
if (!r.ok) return r;
|
|
939
|
+
return typeof r.value === "boolean" ? ok(r.value) : this.#err("kind-mismatch", `argument ${i + 1} of "${op}" is a ${describeType(r.value)}, not a boolean`, f, path, { op });
|
|
940
|
+
};
|
|
941
|
+
switch (op) {
|
|
942
|
+
case "if": return (f, c) => {
|
|
943
|
+
const cond = bool(0, f, c);
|
|
944
|
+
return this.#emit(c, info, f, cond.ok ? arg(cond.value ? 1 : 2, f, c) : cond);
|
|
945
|
+
};
|
|
946
|
+
case "and":
|
|
947
|
+
case "or": {
|
|
948
|
+
const stop = op === "or";
|
|
949
|
+
return (f, c) => {
|
|
950
|
+
if (args.length === 0) return this.#emit(c, info, f, this.#err("bad-expression", `"${op}" needs at least 1 argument`, f, path, { op }));
|
|
951
|
+
for (let i = 0; i < args.length; i++) {
|
|
952
|
+
const b = bool(i, f, c);
|
|
953
|
+
if (!b.ok) return this.#emit(c, info, f, b);
|
|
954
|
+
if (b.value === stop) return this.#emit(c, info, f, ok(b.value));
|
|
955
|
+
}
|
|
956
|
+
return this.#emit(c, info, f, ok(!stop));
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
case "ifError": return (f, c) => {
|
|
960
|
+
const first = arg(0, f, c);
|
|
961
|
+
return this.#emit(c, info, f, first.ok ? first : arg(1, f, c));
|
|
962
|
+
};
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
#call(op, name, rest, f, path) {
|
|
966
|
+
const { domain, spec } = op;
|
|
967
|
+
const lift = spec.liftScalar;
|
|
968
|
+
const from = domain.fromScalar;
|
|
969
|
+
let callArgs = rest;
|
|
970
|
+
if (lift !== void 0 && lift !== false && from !== void 0) {
|
|
971
|
+
const lifted = this.#invoke(() => rest.map((a, i) => typeof a === "number" && liftsAt(lift, i) ? from(a) : a), `${domain.name}.fromScalar`, f, path);
|
|
972
|
+
if (!lifted.ok) return lifted;
|
|
973
|
+
callArgs = lifted.value;
|
|
974
|
+
}
|
|
975
|
+
if (spec.params !== void 0) for (let i = 0; i < callArgs.length; i++) {
|
|
976
|
+
const kind = spec.params[i] ?? "any";
|
|
977
|
+
if (!matchesKind(kind, callArgs[i], domain)) {
|
|
978
|
+
const k = kind === "domain" ? "not-instance" : "kind-mismatch";
|
|
979
|
+
return this.#err(k, `argument ${i + 1} of "${name}" is a ${describeType(callArgs[i])}, not a ${kind === "domain" ? domain.name : kind}`, f, path, { op: name });
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
return this.#checkResult(this.#invoke(() => op.call(callArgs), name, f, path), f, path, name);
|
|
983
|
+
}
|
|
984
|
+
#invoke(run, op, f, path) {
|
|
985
|
+
try {
|
|
986
|
+
return ok(run());
|
|
987
|
+
} catch (thrown) {
|
|
988
|
+
return this.#err("threw", `"${op}" threw an exception`, f, path, {
|
|
989
|
+
op,
|
|
990
|
+
thrown
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
/** This method checks the value that an op gave: no `undefined`, finite numbers, and valid domain values. */
|
|
995
|
+
#checkResult(r, f, path, op) {
|
|
996
|
+
if (!r.ok) return r;
|
|
997
|
+
const value = r.value;
|
|
998
|
+
const extra = { op };
|
|
999
|
+
if (value === void 0 || value === null) return this.#err("undefined-result", `"${op}" gave no value`, f, path, extra);
|
|
1000
|
+
if (typeof value === "number" && !Number.isFinite(value)) return this.#err("not-finite", `"${op}" gave ${value}`, f, path, extra);
|
|
1001
|
+
const d = this.#domains.find((dom) => safeIs(dom, value));
|
|
1002
|
+
if (d?.valid !== void 0) {
|
|
1003
|
+
let valid;
|
|
1004
|
+
try {
|
|
1005
|
+
valid = d.valid(value);
|
|
1006
|
+
} catch (thrown) {
|
|
1007
|
+
return this.#err("threw", `the "valid" check of "${d.name}" threw an exception`, f, path, {
|
|
1008
|
+
...extra,
|
|
1009
|
+
thrown
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
if (!valid) return this.#err("invalid-value", `"${op}" gave a value that is not a valid ${d.name}`, f, path, extra);
|
|
1013
|
+
}
|
|
1014
|
+
return r;
|
|
1015
|
+
}
|
|
1016
|
+
#listOp(op, list, rest, f, path) {
|
|
1017
|
+
const optsArg = op === "reduce" ? rest[1] : rest[0];
|
|
1018
|
+
const strict = isOptions(optsArg) && optsArg.strict === true;
|
|
1019
|
+
const firstError = list.items.find((it) => !it.result.ok)?.result;
|
|
1020
|
+
if (strict && firstError !== void 0 && !firstError.ok) return fail(firstError.error);
|
|
1021
|
+
const okValues = list.items.flatMap((it) => it.result.ok ? [it.result.value] : []);
|
|
1022
|
+
const numbers = () => {
|
|
1023
|
+
const bad = okValues.find((x) => typeof x !== "number");
|
|
1024
|
+
return bad === void 0 ? ok(okValues) : this.#err("kind-mismatch", `"${op}" needs numbers, but an item is a ${describeType(bad)}`, f, path, { op });
|
|
1025
|
+
};
|
|
1026
|
+
const booleans = () => {
|
|
1027
|
+
const bad = okValues.find((x) => typeof x !== "boolean");
|
|
1028
|
+
return bad === void 0 ? ok(okValues) : this.#err("kind-mismatch", `"${op}" needs booleans, but an item is a ${describeType(bad)}`, f, path, { op });
|
|
1029
|
+
};
|
|
1030
|
+
const empty = () => this.#err("empty", `"${op}" has no values to work with`, f, path, { op });
|
|
1031
|
+
switch (op) {
|
|
1032
|
+
case "count": return ok(okValues.length);
|
|
1033
|
+
case "values": return ok(Object.freeze([...okValues]));
|
|
1034
|
+
case "keys": return ok(Object.freeze(list.items.filter((it) => it.result.ok).map((it) => it.key)));
|
|
1035
|
+
case "errors": return ok(Object.freeze(list.items.flatMap((it) => it.result.ok ? [] : [it.result.error])));
|
|
1036
|
+
case "first": return okValues.length > 0 ? ok(okValues[0]) : empty();
|
|
1037
|
+
case "sum": {
|
|
1038
|
+
const n = numbers();
|
|
1039
|
+
return n.ok ? this.#checkResult(ok(n.value.reduce((s, x) => s + x, 0)), f, path, op) : n;
|
|
1040
|
+
}
|
|
1041
|
+
case "mean": {
|
|
1042
|
+
const n = numbers();
|
|
1043
|
+
if (!n.ok) return n;
|
|
1044
|
+
return n.value.length === 0 ? empty() : this.#checkResult(ok(n.value.reduce((s, x) => s + x, 0) / n.value.length), f, path, op);
|
|
1045
|
+
}
|
|
1046
|
+
case "min":
|
|
1047
|
+
case "max": {
|
|
1048
|
+
const n = numbers();
|
|
1049
|
+
if (!n.ok) return n;
|
|
1050
|
+
if (n.value.length === 0) return empty();
|
|
1051
|
+
return ok(n.value.reduce((best, x) => op === "min" ? Math.min(best, x) : Math.max(best, x)));
|
|
1052
|
+
}
|
|
1053
|
+
case "any":
|
|
1054
|
+
case "all":
|
|
1055
|
+
case "none": {
|
|
1056
|
+
const b = booleans();
|
|
1057
|
+
if (!b.ok) return b;
|
|
1058
|
+
if (op === "any") return ok(b.value.some((x) => x));
|
|
1059
|
+
if (op === "all") return ok(b.value.every((x) => x));
|
|
1060
|
+
return ok(!b.value.some((x) => x));
|
|
1061
|
+
}
|
|
1062
|
+
case "reduce": return this.#reduce(rest[0], okValues, f, path);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
/** This method folds the values with a domain op, from the left. An empty list gives the identity of the op. */
|
|
1066
|
+
#reduce(opName, values, f, path) {
|
|
1067
|
+
if (typeof opName !== "string") return this.#err("kind-mismatch", `"reduce" needs an op name, but got a ${describeType(opName)}`, f, path, { op: "reduce" });
|
|
1068
|
+
if (values.length === 0) {
|
|
1069
|
+
for (const d of this.#domains) {
|
|
1070
|
+
const id = d.ops[opName]?.identity;
|
|
1071
|
+
if (id !== void 0 && Object.hasOwn(d.ops, opName)) return this.#invoke(id, opName, f, path);
|
|
1072
|
+
}
|
|
1073
|
+
return this.#err("empty", `"reduce" with "${opName}" has no values and the op has no identity`, f, path, { op: opName });
|
|
1074
|
+
}
|
|
1075
|
+
let acc = values[0];
|
|
1076
|
+
for (let i = 1; i < values.length; i++) {
|
|
1077
|
+
const d = this.#domains.find((dom) => safeIs(dom, acc) && resolveOp(dom, acc, opName) !== void 0);
|
|
1078
|
+
const op = d === void 0 ? void 0 : resolveOp(d, acc, opName);
|
|
1079
|
+
if (op === void 0) return this.#err("unknown-op", `no domain gives "${opName}" for a ${describeType(acc)}`, f, path, { op: opName });
|
|
1080
|
+
const r = this.#call(op, opName, [values[i]], f, path);
|
|
1081
|
+
if (!r.ok) return r;
|
|
1082
|
+
acc = r.value;
|
|
1083
|
+
}
|
|
1084
|
+
return ok(acc);
|
|
1085
|
+
}
|
|
1086
|
+
};
|
|
1087
|
+
const FORBIDDEN_FIELDS = /* @__PURE__ */ new Set([
|
|
1088
|
+
"__proto__",
|
|
1089
|
+
"constructor",
|
|
1090
|
+
"prototype"
|
|
1091
|
+
]);
|
|
1092
|
+
const isFieldName = (seg) => seg.length > 0 && !FORBIDDEN_FIELDS.has(seg);
|
|
1093
|
+
/** This function runs the `is` test of a domain. A test that throws counts as `false`. */
|
|
1094
|
+
function safeIs(d, value) {
|
|
1095
|
+
try {
|
|
1096
|
+
return d.is(value);
|
|
1097
|
+
} catch {
|
|
1098
|
+
return false;
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
const isOptions = (u) => typeof u === "object" && u !== null && !Array.isArray(u) && !isVexList(u);
|
|
1102
|
+
function matchesKind(kind, value, domain) {
|
|
1103
|
+
switch (kind) {
|
|
1104
|
+
case "domain": return safeIs(domain, value);
|
|
1105
|
+
case "number": return typeof value === "number";
|
|
1106
|
+
case "boolean": return typeof value === "boolean";
|
|
1107
|
+
case "string": return typeof value === "string";
|
|
1108
|
+
case "any": return true;
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
/** This function gives a short name for the type of a value, for error messages. */
|
|
1112
|
+
function describeType(u) {
|
|
1113
|
+
if (u === null) return "null";
|
|
1114
|
+
if (Array.isArray(u)) return "array";
|
|
1115
|
+
if (isVexList(u)) return "list";
|
|
1116
|
+
if (typeof u === "object") {
|
|
1117
|
+
const name = Object.getPrototypeOf(u)?.constructor?.name;
|
|
1118
|
+
return typeof name === "string" && name !== "Object" ? name : "object";
|
|
1119
|
+
}
|
|
1120
|
+
return typeof u;
|
|
1121
|
+
}
|
|
1122
|
+
/** This function gives the label of a node, for traces. */
|
|
1123
|
+
function labelOf(e) {
|
|
1124
|
+
switch (e.tag) {
|
|
1125
|
+
case "lit": return previewValue(e.value);
|
|
1126
|
+
case "ref": return e.path.length === 0 ? "(record)" : e.path.join(".");
|
|
1127
|
+
case "app": return e.op;
|
|
1128
|
+
case "let": return `let ${Object.keys(e.bind).join(", ")}`;
|
|
1129
|
+
case "var": return e.name;
|
|
1130
|
+
case "rec": return `{ ${Object.keys(e.fields).join(", ")} }`;
|
|
1131
|
+
case "each": return `each ${e.axis.t}`;
|
|
1132
|
+
case "ext": return e.kind;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
/** This function gives a short text for a value. */
|
|
1136
|
+
function previewValue(u) {
|
|
1137
|
+
if (typeof u === "string") return JSON.stringify(u);
|
|
1138
|
+
if (typeof u === "number" || typeof u === "boolean" || u === null || u === void 0) return String(u);
|
|
1139
|
+
if (isVexList(u)) return `list(${u.items.length})`;
|
|
1140
|
+
try {
|
|
1141
|
+
const json = JSON.stringify(u);
|
|
1142
|
+
if (json !== void 0) return json.length > 60 ? `${json.slice(0, 57)}...` : json;
|
|
1143
|
+
} catch {}
|
|
1144
|
+
return describeType(u);
|
|
1145
|
+
}
|
|
1146
|
+
const formatRead = (r) => `${r.ok ? "read" : "no value at"} ${r.key}.${r.path.length === 0 ? "(record)" : r.path.join(".")}`;
|
|
1147
|
+
/**
|
|
1148
|
+
* This function gives a text form of a trace. Each event has one line, in finish order. The indent of a line
|
|
1149
|
+
* shows the depth of the node. A domain with `show` gives the text of its values. The golden tests compare this text.
|
|
1150
|
+
*/
|
|
1151
|
+
function formatTrace(trace, domains = []) {
|
|
1152
|
+
const show = (u) => {
|
|
1153
|
+
if (isVexList(u)) return `[${u.items.map((it) => `${it.key}: ${result(it.result)}`).join(", ")}]`;
|
|
1154
|
+
const d = domains.find((dom) => dom.show !== void 0 && safeIs(dom, u));
|
|
1155
|
+
try {
|
|
1156
|
+
if (d?.show !== void 0) return d.show(u);
|
|
1157
|
+
} catch {}
|
|
1158
|
+
return previewValue(u);
|
|
1159
|
+
};
|
|
1160
|
+
const result = (r) => r.ok ? show(r.value) : formatError(r.error);
|
|
1161
|
+
const line = (e) => {
|
|
1162
|
+
const reads = e.reads === void 0 ? "" : ` [${e.reads.map(formatRead).join(", ")}]`;
|
|
1163
|
+
return `${" ".repeat(e.path.length)}${e.label} @${e.focus}${reads} = ${result(e.result)}`;
|
|
1164
|
+
};
|
|
1165
|
+
return [
|
|
1166
|
+
...trace.events.map(line),
|
|
1167
|
+
`result = ${result(trace.result)}`,
|
|
1168
|
+
""
|
|
1169
|
+
].join("\n");
|
|
1170
|
+
}
|
|
1171
|
+
/** This function gives the number of nodes of an expression. */
|
|
1172
|
+
const sizeOf = (e) => 1 + children(e).reduce((s, c) => s + sizeOf(c), 0);
|
|
1173
|
+
//#endregion
|
|
1174
|
+
//#region src/deps.ts
|
|
1175
|
+
/**
|
|
1176
|
+
* This function gives the static reads of an expression. A host can use them to recompute a program only
|
|
1177
|
+
* when a field that it reads changes.
|
|
1178
|
+
*/
|
|
1179
|
+
function deps(e) {
|
|
1180
|
+
const out = [];
|
|
1181
|
+
const walk = (x, axes) => {
|
|
1182
|
+
if (x.tag === "ref") {
|
|
1183
|
+
out.push({
|
|
1184
|
+
at: x.at ?? [],
|
|
1185
|
+
path: x.path,
|
|
1186
|
+
axes
|
|
1187
|
+
});
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
if (x.tag === "each") {
|
|
1191
|
+
const inner = [...axes, x.axis.t];
|
|
1192
|
+
children(x).forEach((c) => walk(c, inner));
|
|
1193
|
+
return;
|
|
1194
|
+
}
|
|
1195
|
+
children(x).forEach((c) => walk(c, axes));
|
|
1196
|
+
};
|
|
1197
|
+
walk(e, []);
|
|
1198
|
+
return out;
|
|
1199
|
+
}
|
|
1200
|
+
//#endregion
|
|
1201
|
+
//#region src/json.ts
|
|
1202
|
+
const isPlainJson = (u) => {
|
|
1203
|
+
if (u === null || typeof u === "string" || typeof u === "boolean") return true;
|
|
1204
|
+
if (typeof u === "number") return Number.isFinite(u);
|
|
1205
|
+
if (Array.isArray(u)) return u.every(isPlainJson);
|
|
1206
|
+
if (typeof u === "object") {
|
|
1207
|
+
const proto = Object.getPrototypeOf(u);
|
|
1208
|
+
return (proto === Object.prototype || proto === null) && Object.values(u).every(isPlainJson);
|
|
1209
|
+
}
|
|
1210
|
+
return false;
|
|
1211
|
+
};
|
|
1212
|
+
/**
|
|
1213
|
+
* This function gives the JSON text of an expression. A literal that is a domain value needs a domain with
|
|
1214
|
+
* `encode`. A literal that is not JSON data and has no such domain gives the error `#VALUE!`.
|
|
1215
|
+
*/
|
|
1216
|
+
function serialize(e, domains = []) {
|
|
1217
|
+
const failure = [];
|
|
1218
|
+
const replacer = function(k, value) {
|
|
1219
|
+
const holder = this;
|
|
1220
|
+
if (k !== "value" || holder === null || holder["tag"] !== "lit") return value;
|
|
1221
|
+
if (isPlainJson(value)) return value;
|
|
1222
|
+
const d = domains.find((dom) => dom.encode !== void 0 && dom.is(value));
|
|
1223
|
+
if (d?.encode !== void 0) return {
|
|
1224
|
+
$vex: "domain",
|
|
1225
|
+
domain: d.name,
|
|
1226
|
+
value: d.encode(value)
|
|
1227
|
+
};
|
|
1228
|
+
failure.push({ message: `a literal value is not JSON data and no domain encodes it` });
|
|
1229
|
+
return null;
|
|
1230
|
+
};
|
|
1231
|
+
let text;
|
|
1232
|
+
try {
|
|
1233
|
+
text = JSON.stringify(e, replacer);
|
|
1234
|
+
} catch (thrown) {
|
|
1235
|
+
return fail(vexError("threw", "the expression has no JSON form", { thrown }));
|
|
1236
|
+
}
|
|
1237
|
+
const first = failure[0];
|
|
1238
|
+
return first === void 0 ? ok(text) : fail(vexError("bad-expression", first.message));
|
|
1239
|
+
}
|
|
1240
|
+
const isEncoded = (u) => typeof u === "object" && u !== null && u.$vex === "domain" && typeof u.domain === "string";
|
|
1241
|
+
/** This function reads an expression from JSON text. It checks the whole tree. */
|
|
1242
|
+
function parse(text, domains = []) {
|
|
1243
|
+
let raw;
|
|
1244
|
+
const failure = [];
|
|
1245
|
+
try {
|
|
1246
|
+
raw = JSON.parse(text, (_k, value) => {
|
|
1247
|
+
if (!isEncoded(value)) return value;
|
|
1248
|
+
const d = domains.find((dom) => dom.name === value.domain);
|
|
1249
|
+
if (d?.decode === void 0) {
|
|
1250
|
+
failure.push(`no domain "${value.domain}" decodes a literal`);
|
|
1251
|
+
return value;
|
|
1252
|
+
}
|
|
1253
|
+
return d.decode(value.value);
|
|
1254
|
+
});
|
|
1255
|
+
} catch (thrown) {
|
|
1256
|
+
return fail(vexError("bad-expression", "the text is not JSON", { thrown }));
|
|
1257
|
+
}
|
|
1258
|
+
const first = failure[0];
|
|
1259
|
+
if (first !== void 0) return fail(vexError("bad-expression", first));
|
|
1260
|
+
return isExpr(raw) ? ok(raw) : fail(vexError("bad-expression", "the JSON is not a valid Vex expression"));
|
|
1261
|
+
}
|
|
1262
|
+
//#endregion
|
|
1263
|
+
//#region src/traversal.ts
|
|
1264
|
+
/** Standard monoids. */
|
|
1265
|
+
const monoids = {
|
|
1266
|
+
sum: {
|
|
1267
|
+
empty: 0,
|
|
1268
|
+
concat: (a, b) => a + b
|
|
1269
|
+
},
|
|
1270
|
+
product: {
|
|
1271
|
+
empty: 1,
|
|
1272
|
+
concat: (a, b) => a * b
|
|
1273
|
+
},
|
|
1274
|
+
all: {
|
|
1275
|
+
empty: true,
|
|
1276
|
+
concat: (a, b) => a && b
|
|
1277
|
+
},
|
|
1278
|
+
any: {
|
|
1279
|
+
empty: false,
|
|
1280
|
+
concat: (a, b) => a || b
|
|
1281
|
+
}
|
|
1282
|
+
};
|
|
1283
|
+
const EMPTY_SPACE = space.record({ $: {} });
|
|
1284
|
+
const opt = (r) => toOptional(r);
|
|
1285
|
+
/** This function makes a traversal from items. */
|
|
1286
|
+
function traversal(items, env, isStrict = false) {
|
|
1287
|
+
const reduce = (op, reduceOp) => {
|
|
1288
|
+
const list = vexList(items);
|
|
1289
|
+
const args = [
|
|
1290
|
+
v("$list"),
|
|
1291
|
+
...reduceOp === void 0 ? [] : [lit(reduceOp)],
|
|
1292
|
+
...isStrict ? [lit({ strict: true })] : []
|
|
1293
|
+
];
|
|
1294
|
+
return evaluate(app(op, ...args), {
|
|
1295
|
+
space: EMPTY_SPACE,
|
|
1296
|
+
origin: "$",
|
|
1297
|
+
domains: env.domains,
|
|
1298
|
+
vars: { $list: list },
|
|
1299
|
+
...env.options?.fns === void 0 ? {} : { fns: env.options.fns }
|
|
1300
|
+
});
|
|
1301
|
+
};
|
|
1302
|
+
const t = {
|
|
1303
|
+
items,
|
|
1304
|
+
strict: () => traversal(items, env, true),
|
|
1305
|
+
get: (k) => {
|
|
1306
|
+
const item = items.find((it) => it.key === k);
|
|
1307
|
+
return item === void 0 ? toOptional(fail(vexError("unknown-key", `no item has the key "${k}"`))) : toOptional(item.result);
|
|
1308
|
+
},
|
|
1309
|
+
values: () => items.flatMap((it) => it.result.ok ? [it.result.value] : []),
|
|
1310
|
+
keys: () => items.flatMap((it) => it.result.ok ? [it.key] : []),
|
|
1311
|
+
errors: () => items.flatMap((it) => it.result.ok ? [] : [it.result.error]),
|
|
1312
|
+
count: () => opt(reduce("count")),
|
|
1313
|
+
sum: () => opt(reduce("sum")),
|
|
1314
|
+
mean: () => opt(reduce("mean")),
|
|
1315
|
+
min: () => opt(reduce("min")),
|
|
1316
|
+
max: () => opt(reduce("max")),
|
|
1317
|
+
any: () => opt(reduce("any")),
|
|
1318
|
+
all: () => opt(reduce("all")),
|
|
1319
|
+
none: () => opt(reduce("none")),
|
|
1320
|
+
reduce: (op) => opt(reduce("reduce", op)),
|
|
1321
|
+
reduceResult: (op, reduceOp) => reduce(op, reduceOp),
|
|
1322
|
+
map: (f) => traversal(items.map((it) => {
|
|
1323
|
+
if (!it.result.ok) return {
|
|
1324
|
+
key: it.key,
|
|
1325
|
+
result: it.result
|
|
1326
|
+
};
|
|
1327
|
+
try {
|
|
1328
|
+
return {
|
|
1329
|
+
key: it.key,
|
|
1330
|
+
result: ok(f(it.result.value, it.key))
|
|
1331
|
+
};
|
|
1332
|
+
} catch (thrown) {
|
|
1333
|
+
return {
|
|
1334
|
+
key: it.key,
|
|
1335
|
+
result: fail(vexError("threw", "the map function threw an exception", {
|
|
1336
|
+
thrown,
|
|
1337
|
+
focus: it.key
|
|
1338
|
+
}))
|
|
1339
|
+
};
|
|
1340
|
+
}
|
|
1341
|
+
}), env, isStrict),
|
|
1342
|
+
fold: (monoid, f) => t.values().reduce((acc, x) => monoid.concat(acc, f(x)), monoid.empty)
|
|
1343
|
+
};
|
|
1344
|
+
return t;
|
|
1345
|
+
}
|
|
1346
|
+
//#endregion
|
|
1347
|
+
//#region src/sheet.ts
|
|
1348
|
+
/**
|
|
1349
|
+
* Sheets: named columns of formulas over a space. A formula can read a column at an address, like a cell
|
|
1350
|
+
* reference of a spreadsheet. A cell is one column at one key. A run evaluates each cell once, and each cell on a
|
|
1351
|
+
* cycle of reads gives `#CYCLE!`.
|
|
1352
|
+
*
|
|
1353
|
+
* A cell reference is the extension node `ext("vex.cell", { column, at })`. Thus the IR has no new kind, and the
|
|
1354
|
+
* JSON form, `deps` and the traces work without a change.
|
|
1355
|
+
*/
|
|
1356
|
+
/** The extension kind of a cell reference. */
|
|
1357
|
+
const CELL_KIND = "vex.cell";
|
|
1358
|
+
/** This function makes a cell reference: the column `column` at the address `at` from the focus. */
|
|
1359
|
+
const cell = (column, at = []) => ext(CELL_KIND, {
|
|
1360
|
+
column,
|
|
1361
|
+
at
|
|
1362
|
+
});
|
|
1363
|
+
const isCellRef = (u) => typeof u === "object" && u !== null && typeof u.column === "string" && Array.isArray(u.at) && u.at.every(isMove);
|
|
1364
|
+
const cycleError = (column, key) => fail(vexError("cycle", `the cell "${column}" at "${key}" is on a cycle of cell references`, {
|
|
1365
|
+
origin: key,
|
|
1366
|
+
focus: key
|
|
1367
|
+
}));
|
|
1368
|
+
var SheetRun = class {
|
|
1369
|
+
#columns;
|
|
1370
|
+
#opts;
|
|
1371
|
+
#done = /* @__PURE__ */ new Map();
|
|
1372
|
+
#visits = /* @__PURE__ */ new Map();
|
|
1373
|
+
#stack = [];
|
|
1374
|
+
#top = {
|
|
1375
|
+
id: "",
|
|
1376
|
+
column: "",
|
|
1377
|
+
key: "",
|
|
1378
|
+
index: -1,
|
|
1379
|
+
low: -1,
|
|
1380
|
+
selfLoop: false,
|
|
1381
|
+
result: cycleError("", "")
|
|
1382
|
+
};
|
|
1383
|
+
#next = 0;
|
|
1384
|
+
constructor(columns, opts) {
|
|
1385
|
+
this.#columns = columns;
|
|
1386
|
+
this.#opts = {
|
|
1387
|
+
...opts,
|
|
1388
|
+
extensions: {
|
|
1389
|
+
...opts.extensions,
|
|
1390
|
+
[CELL_KIND]: this.#handler
|
|
1391
|
+
}
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
#handler = (data, ctx) => {
|
|
1395
|
+
if (!isCellRef(data)) return fail(vexError("bad-expression", "a cell reference needs a column name and an address", { path: ctx.path }));
|
|
1396
|
+
const where = resolveAddr(ctx.space, ctx.position, data.at);
|
|
1397
|
+
if (!where.ok) return fail({
|
|
1398
|
+
...where.error,
|
|
1399
|
+
path: ctx.path
|
|
1400
|
+
});
|
|
1401
|
+
return this.cell(data.column, where.value);
|
|
1402
|
+
};
|
|
1403
|
+
/** This method gives the result of one cell: the column `column` at the key `key`. */
|
|
1404
|
+
cell(column, key) {
|
|
1405
|
+
const id = `${column}\u0000${key}`;
|
|
1406
|
+
const done = this.#done.get(id);
|
|
1407
|
+
if (done !== void 0) return done;
|
|
1408
|
+
const caller = this.#top;
|
|
1409
|
+
const seen = this.#visits.get(id);
|
|
1410
|
+
if (seen !== void 0) {
|
|
1411
|
+
caller.low = Math.min(caller.low, seen.index);
|
|
1412
|
+
if (caller === seen) seen.selfLoop = true;
|
|
1413
|
+
return cycleError(column, key);
|
|
1414
|
+
}
|
|
1415
|
+
const program = this.#columns.get(column);
|
|
1416
|
+
if (program === void 0) return fail(vexError("unbound", `the sheet has no column "${column}"`, {
|
|
1417
|
+
origin: key,
|
|
1418
|
+
focus: key
|
|
1419
|
+
}));
|
|
1420
|
+
const n = this.#next++;
|
|
1421
|
+
const visit = {
|
|
1422
|
+
id,
|
|
1423
|
+
column,
|
|
1424
|
+
key,
|
|
1425
|
+
index: n,
|
|
1426
|
+
low: n,
|
|
1427
|
+
selfLoop: false,
|
|
1428
|
+
result: cycleError(column, key)
|
|
1429
|
+
};
|
|
1430
|
+
this.#visits.set(id, visit);
|
|
1431
|
+
this.#stack.push(visit);
|
|
1432
|
+
this.#top = visit;
|
|
1433
|
+
visit.result = evaluate(program, {
|
|
1434
|
+
...this.#opts,
|
|
1435
|
+
origin: key
|
|
1436
|
+
});
|
|
1437
|
+
this.#top = caller;
|
|
1438
|
+
if (visit.low === visit.index) this.#finish(visit);
|
|
1439
|
+
else caller.low = Math.min(caller.low, visit.low);
|
|
1440
|
+
return this.#done.get(id) ?? cycleError(column, key);
|
|
1441
|
+
}
|
|
1442
|
+
/** This method takes the component of `root` from the stack, and sets the final result of each of its cells. */
|
|
1443
|
+
#finish(root) {
|
|
1444
|
+
const members = this.#stack.splice(this.#stack.indexOf(root));
|
|
1445
|
+
const cyclic = members.length > 1 || root.selfLoop;
|
|
1446
|
+
for (const m of members) {
|
|
1447
|
+
this.#visits.delete(m.id);
|
|
1448
|
+
this.#done.set(m.id, cyclic ? cycleError(m.column, m.key) : m.result);
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
/** This method evaluates one cell with a trace of the nodes of its column. The result is the result of the cell. */
|
|
1452
|
+
explain(column, key) {
|
|
1453
|
+
const result = this.cell(column, key);
|
|
1454
|
+
const program = this.#columns.get(column);
|
|
1455
|
+
if (program === void 0) return {
|
|
1456
|
+
events: [],
|
|
1457
|
+
result
|
|
1458
|
+
};
|
|
1459
|
+
return {
|
|
1460
|
+
events: explain(program, {
|
|
1461
|
+
...this.#opts,
|
|
1462
|
+
origin: key
|
|
1463
|
+
}).events,
|
|
1464
|
+
result
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
/**
|
|
1468
|
+
* This method evaluates the column `column` at each key before `key`, in key order. A recurrence over the keys,
|
|
1469
|
+
* for example a running total, then reads finished cells and does not go deep into the call stack.
|
|
1470
|
+
*/
|
|
1471
|
+
warm(column, key) {
|
|
1472
|
+
for (const k of this.#opts.space.keys) {
|
|
1473
|
+
if (k === key) return;
|
|
1474
|
+
this.cell(column, k);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
};
|
|
1478
|
+
//#endregion
|
|
1479
|
+
//#region src/builder.ts
|
|
1480
|
+
const argToken = (expr) => ({
|
|
1481
|
+
kind: "vex.arg",
|
|
1482
|
+
expr
|
|
1483
|
+
});
|
|
1484
|
+
const isArg = (u) => typeof u === "object" && u !== null && u.kind === "vex.arg";
|
|
1485
|
+
const isChainLike = (u) => typeof u === "object" && u !== null && (u.kind === "vex.chain" || u.kind === "vex.list-chain");
|
|
1486
|
+
/** This function turns a builder argument into an expression. A bare string is a field reference at `addr`. */
|
|
1487
|
+
function toExpr(a, addr) {
|
|
1488
|
+
if (typeof a === "string") return ref(toPath(a), addr);
|
|
1489
|
+
if (isArg(a)) return a.expr;
|
|
1490
|
+
if (isChainLike(a)) return a.program;
|
|
1491
|
+
return lit(a);
|
|
1492
|
+
}
|
|
1493
|
+
const PROXY_RESERVED = /* @__PURE__ */ new Set([
|
|
1494
|
+
"then",
|
|
1495
|
+
"catch",
|
|
1496
|
+
"finally",
|
|
1497
|
+
"toJSON",
|
|
1498
|
+
"valueOf",
|
|
1499
|
+
"toString",
|
|
1500
|
+
"constructor",
|
|
1501
|
+
"inspect",
|
|
1502
|
+
"asymmetricMatch",
|
|
1503
|
+
"$$typeof",
|
|
1504
|
+
"nodeType"
|
|
1505
|
+
]);
|
|
1506
|
+
var ChainImpl = class ChainImpl {
|
|
1507
|
+
kind = "vex.chain";
|
|
1508
|
+
#s;
|
|
1509
|
+
constructor(s) {
|
|
1510
|
+
this.#s = s;
|
|
1511
|
+
}
|
|
1512
|
+
get program() {
|
|
1513
|
+
return this.#s.expr;
|
|
1514
|
+
}
|
|
1515
|
+
get _() {
|
|
1516
|
+
const s = this.#s;
|
|
1517
|
+
return new Proxy(Object.create(null), { get: (_t, prop) => {
|
|
1518
|
+
if (typeof prop !== "string" || PROXY_RESERVED.has(prop)) return void 0;
|
|
1519
|
+
return (...args) => new ChainImpl({
|
|
1520
|
+
...s,
|
|
1521
|
+
expr: app(prop, s.expr, ...args.map((a) => toExpr(a, s.addr)))
|
|
1522
|
+
});
|
|
1523
|
+
} });
|
|
1524
|
+
}
|
|
1525
|
+
#opts(k) {
|
|
1526
|
+
const { env } = this.#s;
|
|
1527
|
+
return {
|
|
1528
|
+
space: env.space,
|
|
1529
|
+
origin: k,
|
|
1530
|
+
domains: env.domains,
|
|
1531
|
+
...env.options.fns === void 0 ? {} : { fns: env.options.fns },
|
|
1532
|
+
...env.options.extensions === void 0 ? {} : { extensions: env.options.extensions }
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
result(k) {
|
|
1536
|
+
return evaluate(this.#s.expr, this.#opts(k));
|
|
1537
|
+
}
|
|
1538
|
+
at(k) {
|
|
1539
|
+
return toOptional(this.result(k));
|
|
1540
|
+
}
|
|
1541
|
+
value(k) {
|
|
1542
|
+
return this.at(k);
|
|
1543
|
+
}
|
|
1544
|
+
explain(k) {
|
|
1545
|
+
return explain(this.#s.expr, this.#opts(k));
|
|
1546
|
+
}
|
|
1547
|
+
all() {
|
|
1548
|
+
const { env } = this.#s;
|
|
1549
|
+
return traversal(env.space.keys.map((k) => ({
|
|
1550
|
+
key: k,
|
|
1551
|
+
result: this.result(k)
|
|
1552
|
+
})), env);
|
|
1553
|
+
}
|
|
1554
|
+
from(field) {
|
|
1555
|
+
return new ChainImpl({
|
|
1556
|
+
...this.#s,
|
|
1557
|
+
expr: ref(toPath(field), this.#s.addr)
|
|
1558
|
+
});
|
|
1559
|
+
}
|
|
1560
|
+
#move(m) {
|
|
1561
|
+
return new ChainImpl({
|
|
1562
|
+
...this.#s,
|
|
1563
|
+
addr: [...this.#s.addr, m]
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
to(k) {
|
|
1567
|
+
return this.#move(key(k));
|
|
1568
|
+
}
|
|
1569
|
+
index(i) {
|
|
1570
|
+
return this.#move(index(i));
|
|
1571
|
+
}
|
|
1572
|
+
offset(...d) {
|
|
1573
|
+
return this.#move(offset(...d));
|
|
1574
|
+
}
|
|
1575
|
+
other() {
|
|
1576
|
+
return this.#move(other);
|
|
1577
|
+
}
|
|
1578
|
+
origin() {
|
|
1579
|
+
return this.#move(origin);
|
|
1580
|
+
}
|
|
1581
|
+
parent() {
|
|
1582
|
+
return this.#move(parent);
|
|
1583
|
+
}
|
|
1584
|
+
ifError(fallback) {
|
|
1585
|
+
return new ChainImpl({
|
|
1586
|
+
...this.#s,
|
|
1587
|
+
expr: app("ifError", this.#s.expr, toExpr(fallback, this.#s.addr))
|
|
1588
|
+
});
|
|
1589
|
+
}
|
|
1590
|
+
/** This method binds the current value to a fresh name, and gives a chain that starts from that name. */
|
|
1591
|
+
#bound() {
|
|
1592
|
+
const name = `$${this.#s.depth}`;
|
|
1593
|
+
return {
|
|
1594
|
+
name,
|
|
1595
|
+
start: new ChainImpl({
|
|
1596
|
+
...this.#s,
|
|
1597
|
+
expr: v(name),
|
|
1598
|
+
depth: this.#s.depth + 1
|
|
1599
|
+
})
|
|
1600
|
+
};
|
|
1601
|
+
}
|
|
1602
|
+
fork(branches) {
|
|
1603
|
+
const { name, start } = this.#bound();
|
|
1604
|
+
const fields = {};
|
|
1605
|
+
for (const [n, f] of Object.entries(branches)) fields[n] = f(start).program;
|
|
1606
|
+
return new ChainImpl({
|
|
1607
|
+
...this.#s,
|
|
1608
|
+
expr: let_({ [name]: this.#s.expr }, rec(fields))
|
|
1609
|
+
});
|
|
1610
|
+
}
|
|
1611
|
+
with(binds, body) {
|
|
1612
|
+
const depth = this.#s.depth;
|
|
1613
|
+
const bindExprs = {};
|
|
1614
|
+
const vars = {};
|
|
1615
|
+
for (const [n, a] of Object.entries(binds)) {
|
|
1616
|
+
const unique = `${n}$${depth}`;
|
|
1617
|
+
bindExprs[unique] = toExpr(a, this.#s.addr);
|
|
1618
|
+
vars[n] = argToken(v(unique));
|
|
1619
|
+
}
|
|
1620
|
+
const inner = body(new ChainImpl({
|
|
1621
|
+
...this.#s,
|
|
1622
|
+
depth: depth + 1
|
|
1623
|
+
}), vars);
|
|
1624
|
+
return new ChainImpl({
|
|
1625
|
+
...this.#s,
|
|
1626
|
+
expr: let_(bindExprs, inner.program)
|
|
1627
|
+
});
|
|
1628
|
+
}
|
|
1629
|
+
#axis(a, body, opts) {
|
|
1630
|
+
const { name, start } = this.#bound();
|
|
1631
|
+
const inTarget = new ChainImpl({
|
|
1632
|
+
...start.#s,
|
|
1633
|
+
addr: []
|
|
1634
|
+
});
|
|
1635
|
+
const inner = body(inTarget);
|
|
1636
|
+
const axis = opts?.where === void 0 ? a : {
|
|
1637
|
+
t: "where",
|
|
1638
|
+
axis: a,
|
|
1639
|
+
test: opts.where(inTarget).program
|
|
1640
|
+
};
|
|
1641
|
+
return new ListChainImpl({
|
|
1642
|
+
...this.#s,
|
|
1643
|
+
expr: let_({ [name]: this.#s.expr }, each(axis, inner.program))
|
|
1644
|
+
});
|
|
1645
|
+
}
|
|
1646
|
+
others(body, opts) {
|
|
1647
|
+
return this.#axis({ t: "others" }, body, opts);
|
|
1648
|
+
}
|
|
1649
|
+
neighbors(n, body, opts) {
|
|
1650
|
+
return this.#axis({
|
|
1651
|
+
t: "neighbors",
|
|
1652
|
+
n
|
|
1653
|
+
}, body, opts);
|
|
1654
|
+
}
|
|
1655
|
+
each(body, opts) {
|
|
1656
|
+
return this.#axis({ t: "all" }, body, opts);
|
|
1657
|
+
}
|
|
1658
|
+
children(body, opts) {
|
|
1659
|
+
return this.#axis({ t: "children" }, body, opts);
|
|
1660
|
+
}
|
|
1661
|
+
ancestors(body, opts) {
|
|
1662
|
+
return this.#axis({ t: "ancestors" }, body, opts);
|
|
1663
|
+
}
|
|
1664
|
+
descendants(body, opts) {
|
|
1665
|
+
return this.#axis({ t: "descendants" }, body, opts);
|
|
1666
|
+
}
|
|
1667
|
+
siblings(body, opts) {
|
|
1668
|
+
return this.#axis({ t: "siblings" }, body, opts);
|
|
1669
|
+
}
|
|
1670
|
+
};
|
|
1671
|
+
var ListChainImpl = class {
|
|
1672
|
+
kind = "vex.list-chain";
|
|
1673
|
+
#s;
|
|
1674
|
+
constructor(s) {
|
|
1675
|
+
this.#s = s;
|
|
1676
|
+
}
|
|
1677
|
+
get program() {
|
|
1678
|
+
return this.#s.expr;
|
|
1679
|
+
}
|
|
1680
|
+
#op(op, extra, opts) {
|
|
1681
|
+
const args = opts?.strict === true ? [...extra, lit({ strict: true })] : extra;
|
|
1682
|
+
return new ChainImpl({
|
|
1683
|
+
...this.#s,
|
|
1684
|
+
expr: app(op, this.#s.expr, ...args)
|
|
1685
|
+
});
|
|
1686
|
+
}
|
|
1687
|
+
count(opts) {
|
|
1688
|
+
return this.#op("count", [], opts);
|
|
1689
|
+
}
|
|
1690
|
+
sum(opts) {
|
|
1691
|
+
return this.#op("sum", [], opts);
|
|
1692
|
+
}
|
|
1693
|
+
mean(opts) {
|
|
1694
|
+
return this.#op("mean", [], opts);
|
|
1695
|
+
}
|
|
1696
|
+
min(opts) {
|
|
1697
|
+
return this.#op("min", [], opts);
|
|
1698
|
+
}
|
|
1699
|
+
max(opts) {
|
|
1700
|
+
return this.#op("max", [], opts);
|
|
1701
|
+
}
|
|
1702
|
+
any(opts) {
|
|
1703
|
+
return this.#op("any", [], opts);
|
|
1704
|
+
}
|
|
1705
|
+
all(opts) {
|
|
1706
|
+
return this.#op("all", [], opts);
|
|
1707
|
+
}
|
|
1708
|
+
none(opts) {
|
|
1709
|
+
return this.#op("none", [], opts);
|
|
1710
|
+
}
|
|
1711
|
+
values(opts) {
|
|
1712
|
+
return this.#op("values", [], opts);
|
|
1713
|
+
}
|
|
1714
|
+
first(opts) {
|
|
1715
|
+
return this.#op("first", [], opts);
|
|
1716
|
+
}
|
|
1717
|
+
reduce(op, opts) {
|
|
1718
|
+
return this.#op("reduce", [lit(op)], opts);
|
|
1719
|
+
}
|
|
1720
|
+
};
|
|
1721
|
+
var RootImpl = class {
|
|
1722
|
+
#env;
|
|
1723
|
+
constructor(env) {
|
|
1724
|
+
this.#env = env;
|
|
1725
|
+
}
|
|
1726
|
+
get space() {
|
|
1727
|
+
return this.#env.space;
|
|
1728
|
+
}
|
|
1729
|
+
#chain(expr) {
|
|
1730
|
+
return new ChainImpl({
|
|
1731
|
+
env: this.#env,
|
|
1732
|
+
expr,
|
|
1733
|
+
addr: [],
|
|
1734
|
+
depth: 0
|
|
1735
|
+
});
|
|
1736
|
+
}
|
|
1737
|
+
from(field) {
|
|
1738
|
+
return this.#chain(ref(toPath(field)));
|
|
1739
|
+
}
|
|
1740
|
+
start(value) {
|
|
1741
|
+
return this.#chain(toExpr(value, []));
|
|
1742
|
+
}
|
|
1743
|
+
of(k, field) {
|
|
1744
|
+
return argToken(ref(toPath(field), [key(k)]));
|
|
1745
|
+
}
|
|
1746
|
+
field(path) {
|
|
1747
|
+
return argToken(ref(toPath(path)));
|
|
1748
|
+
}
|
|
1749
|
+
lit(value) {
|
|
1750
|
+
return argToken(lit(value));
|
|
1751
|
+
}
|
|
1752
|
+
ofPath(k, path) {
|
|
1753
|
+
return argToken(ref(toPath(path), [key(k)]));
|
|
1754
|
+
}
|
|
1755
|
+
ext(kind, data = null) {
|
|
1756
|
+
return argToken(ext(kind, data));
|
|
1757
|
+
}
|
|
1758
|
+
sheet() {
|
|
1759
|
+
return new SheetImpl(this.#env, /* @__PURE__ */ new Map());
|
|
1760
|
+
}
|
|
1761
|
+
rec(fields) {
|
|
1762
|
+
const out = {};
|
|
1763
|
+
for (const [n, a] of Object.entries(fields)) out[n] = toExpr(a, []);
|
|
1764
|
+
return argToken(rec(out));
|
|
1765
|
+
}
|
|
1766
|
+
};
|
|
1767
|
+
var SheetRootImpl = class extends RootImpl {
|
|
1768
|
+
cell(column, at) {
|
|
1769
|
+
const addr = at === void 0 ? [] : typeof at === "string" ? [key(at)] : at;
|
|
1770
|
+
return argToken(cell(column, addr));
|
|
1771
|
+
}
|
|
1772
|
+
};
|
|
1773
|
+
var SheetImpl = class SheetImpl {
|
|
1774
|
+
#env;
|
|
1775
|
+
#columns;
|
|
1776
|
+
constructor(env, columns) {
|
|
1777
|
+
this.#env = env;
|
|
1778
|
+
this.#columns = columns;
|
|
1779
|
+
}
|
|
1780
|
+
get columns() {
|
|
1781
|
+
return Object.freeze(Object.fromEntries(this.#columns));
|
|
1782
|
+
}
|
|
1783
|
+
column(name, formula) {
|
|
1784
|
+
const program = formula(new SheetRootImpl(this.#env)).program;
|
|
1785
|
+
return new SheetImpl(this.#env, new Map([...this.#columns, [name, program]]));
|
|
1786
|
+
}
|
|
1787
|
+
#run() {
|
|
1788
|
+
const { space: s, domains, options } = this.#env;
|
|
1789
|
+
return new SheetRun(this.#columns, {
|
|
1790
|
+
space: s,
|
|
1791
|
+
domains,
|
|
1792
|
+
...options.fns === void 0 ? {} : { fns: options.fns },
|
|
1793
|
+
...options.extensions === void 0 ? {} : { extensions: options.extensions }
|
|
1794
|
+
});
|
|
1795
|
+
}
|
|
1796
|
+
result(k, column) {
|
|
1797
|
+
const run = this.#run();
|
|
1798
|
+
run.warm(column, k);
|
|
1799
|
+
return run.cell(column, k);
|
|
1800
|
+
}
|
|
1801
|
+
at(k, column) {
|
|
1802
|
+
return toOptional(this.result(k, column));
|
|
1803
|
+
}
|
|
1804
|
+
explain(k, column) {
|
|
1805
|
+
const run = this.#run();
|
|
1806
|
+
run.warm(column, k);
|
|
1807
|
+
return run.explain(column, k);
|
|
1808
|
+
}
|
|
1809
|
+
table() {
|
|
1810
|
+
const run = this.#run();
|
|
1811
|
+
const names = [...this.#columns.keys()];
|
|
1812
|
+
return this.#env.space.keys.map((key) => ({
|
|
1813
|
+
key,
|
|
1814
|
+
cells: Object.fromEntries(names.map((c) => [c, run.cell(c, key)]))
|
|
1815
|
+
}));
|
|
1816
|
+
}
|
|
1817
|
+
};
|
|
1818
|
+
/**
|
|
1819
|
+
* This function gives the entry point of the builder for a list of domains. The interpreter finds the op of a
|
|
1820
|
+
* value in the first domain that accepts the value.
|
|
1821
|
+
*/
|
|
1822
|
+
function vex(...domains) {
|
|
1823
|
+
const make = (options) => ({
|
|
1824
|
+
domains,
|
|
1825
|
+
over: (s) => new RootImpl({
|
|
1826
|
+
space: s,
|
|
1827
|
+
domains,
|
|
1828
|
+
options
|
|
1829
|
+
})
|
|
1830
|
+
});
|
|
1831
|
+
return {
|
|
1832
|
+
...make({}),
|
|
1833
|
+
withOptions: make
|
|
1834
|
+
};
|
|
1835
|
+
}
|
|
1836
|
+
//#endregion
|
|
1837
|
+
export { CELL_KIND, LIST_OPS, SPECIAL_FORMS, SheetRun, app, applyMove, axes, axisTargets, cell, chainResult, children, codeOf, compile, defineDomain, deps, describeType, domainOf, each, evaluate, explain, ext, fail, findMethod, formatError, formatTrace, getOr, index, isAxis, isExpr, isListOp, isMove, isNone, isOpName, isSome, isVexList, key, labelOf, let_, liftsAt, lit, mapResult, monoids, none, offset, ok, origin, other, parent, parse, previewValue, rec, ref, resolveAddr, resolveOp, serialize, sizeOf, some, space, toOptional, toPath, traversal, v, vex, vexError, vexList };
|
|
1838
|
+
|
|
1839
|
+
//# sourceMappingURL=index.js.map
|