@mark1russell7/vex 0.7.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/build/index.d.ts +79 -6
- package/build/index.d.ts.map +1 -1
- package/build/index.js +353 -27
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/builder.ts +120 -20
- package/src/deps.ts +49 -4
- package/src/evaluate.ts +111 -2
- package/src/ir.ts +62 -9
- package/src/json.ts +15 -1
- package/src/list.ts +3 -3
- package/src/sheet.ts +11 -4
- package/src/space.ts +8 -0
package/build/index.js
CHANGED
|
@@ -185,6 +185,21 @@ function iterate(limit, init, step, result, until) {
|
|
|
185
185
|
result
|
|
186
186
|
};
|
|
187
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* This function makes a function (FN.BIND): the names of its parameters and its body. A function is only the value of
|
|
190
|
+
* a binding of `let`. The body sees the names of the place of that `let`, so a function cannot call itself.
|
|
191
|
+
*/
|
|
192
|
+
const fn = (params, body) => ({
|
|
193
|
+
tag: "fn",
|
|
194
|
+
params,
|
|
195
|
+
body
|
|
196
|
+
});
|
|
197
|
+
/** This function makes a call of a bound function (FN.CALL). The body evaluates at the position of the call. */
|
|
198
|
+
const call = (name, ...args) => ({
|
|
199
|
+
tag: "call",
|
|
200
|
+
fn: name,
|
|
201
|
+
args
|
|
202
|
+
});
|
|
188
203
|
/** This function makes an expression of an extension kind. */
|
|
189
204
|
const ext = (kind, data) => ({
|
|
190
205
|
tag: "ext",
|
|
@@ -267,6 +282,22 @@ const axes = {
|
|
|
267
282
|
predecessors: Object.freeze({ t: "predecessors" }),
|
|
268
283
|
/** Each key that a path of edges reaches from the focus, in breadth-first order, without the focus. */
|
|
269
284
|
reachable: Object.freeze({ t: "reachable" }),
|
|
285
|
+
/**
|
|
286
|
+
* The targets of `axis` in the reverse order (AXIS.REVERSE). The inner axis is a base axis: not `where`, `reverse` or
|
|
287
|
+
* `table`. A test goes outside: `where(reverse(axis), test)`.
|
|
288
|
+
*/
|
|
289
|
+
reverse: (axis) => ({
|
|
290
|
+
t: "reverse",
|
|
291
|
+
axis
|
|
292
|
+
}),
|
|
293
|
+
/**
|
|
294
|
+
* The keys of the items of the list that `list` gives, in the order of the list (AXIS.LIST). The expression evaluates
|
|
295
|
+
* before the targets. An error item gives an error item for its target.
|
|
296
|
+
*/
|
|
297
|
+
list: (list) => ({
|
|
298
|
+
t: "list",
|
|
299
|
+
list
|
|
300
|
+
}),
|
|
270
301
|
/** The targets of `axis` where `test` gives `true`. */
|
|
271
302
|
where: (axis, test) => ({
|
|
272
303
|
t: "where",
|
|
@@ -320,6 +351,8 @@ const TAGS = /* @__PURE__ */ new Set([
|
|
|
320
351
|
"each",
|
|
321
352
|
"fold",
|
|
322
353
|
"iterate",
|
|
354
|
+
"fn",
|
|
355
|
+
"call",
|
|
323
356
|
"ext"
|
|
324
357
|
]);
|
|
325
358
|
/**
|
|
@@ -336,6 +369,7 @@ function shapeError(u) {
|
|
|
336
369
|
const tag = isObject(bad.u) ? bad.u["tag"] : void 0;
|
|
337
370
|
if (bad.axis) return "the expression has an axis that is not valid";
|
|
338
371
|
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`;
|
|
372
|
+
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";
|
|
339
373
|
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";
|
|
340
374
|
}
|
|
341
375
|
/**
|
|
@@ -392,6 +426,15 @@ function badPart(todo, whole) {
|
|
|
392
426
|
} else if (t === "where") {
|
|
393
427
|
push(true, [u["axis"]]);
|
|
394
428
|
push(false, [u["test"]]);
|
|
429
|
+
} else if (t === "list") push(false, [u["list"]]);
|
|
430
|
+
else if (t === "reverse") {
|
|
431
|
+
const inner = u["axis"];
|
|
432
|
+
if (!isObject(inner) || [
|
|
433
|
+
"where",
|
|
434
|
+
"reverse",
|
|
435
|
+
"table"
|
|
436
|
+
].includes(inner["t"])) return item;
|
|
437
|
+
push(true, [inner]);
|
|
395
438
|
} else if (![
|
|
396
439
|
"all",
|
|
397
440
|
"others",
|
|
@@ -422,7 +465,11 @@ function badPart(todo, whole) {
|
|
|
422
465
|
break;
|
|
423
466
|
case "let":
|
|
424
467
|
if (!isObject(u["bind"]) || Array.isArray(u["bind"])) return item;
|
|
425
|
-
push(false, [...Object.values(u["bind"]), u["body"]]);
|
|
468
|
+
push(false, [...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), u["body"]]);
|
|
469
|
+
break;
|
|
470
|
+
case "call":
|
|
471
|
+
if (typeof u["fn"] !== "string" || !Array.isArray(u["args"])) return item;
|
|
472
|
+
push(false, u["args"]);
|
|
426
473
|
break;
|
|
427
474
|
case "var":
|
|
428
475
|
if (typeof u["name"] !== "string") return item;
|
|
@@ -466,18 +513,20 @@ function badPart(todo, whole) {
|
|
|
466
513
|
*/
|
|
467
514
|
function children(e) {
|
|
468
515
|
switch (e.tag) {
|
|
469
|
-
case "app":
|
|
516
|
+
case "app":
|
|
517
|
+
case "call": return e.args;
|
|
518
|
+
case "fn": return [e.body];
|
|
470
519
|
case "let": return [...Object.values(e.bind), e.body];
|
|
471
520
|
case "rec": return Object.values(e.fields);
|
|
472
521
|
case "each": return [
|
|
473
522
|
e.body,
|
|
474
|
-
...
|
|
523
|
+
...axisExprs(e.axis),
|
|
475
524
|
...addrExprs(e.at)
|
|
476
525
|
];
|
|
477
526
|
case "fold": return [
|
|
478
527
|
...Object.values(e.init),
|
|
479
528
|
...Object.values(e.step),
|
|
480
|
-
...
|
|
529
|
+
...axisExprs(e.axis),
|
|
481
530
|
...addrExprs(e.at),
|
|
482
531
|
e.result
|
|
483
532
|
];
|
|
@@ -493,11 +542,22 @@ function children(e) {
|
|
|
493
542
|
case "ext": return [];
|
|
494
543
|
}
|
|
495
544
|
}
|
|
496
|
-
/**
|
|
497
|
-
function
|
|
545
|
+
/**
|
|
546
|
+
* This function gives the expressions of an axis, in the order of their child indexes. The expression of the axis
|
|
547
|
+
* `list` comes first, and then the tests of the `where` axes from the inside out. It uses a loop, not recursion.
|
|
548
|
+
*/
|
|
549
|
+
function axisExprs(a) {
|
|
498
550
|
const out = [];
|
|
499
551
|
for (let x = a; x.t === "where"; x = x.axis) out.unshift(x.test);
|
|
500
|
-
|
|
552
|
+
const list = listOf(a);
|
|
553
|
+
return list === void 0 ? out : [list, ...out];
|
|
554
|
+
}
|
|
555
|
+
/** This function gives the expression of the axis `list` inside an axis, through `where` and `reverse`, if it has one. */
|
|
556
|
+
function listOf(a) {
|
|
557
|
+
let x = a;
|
|
558
|
+
while (x.t === "where") x = x.axis;
|
|
559
|
+
const base = x.t === "reverse" ? x.axis : x;
|
|
560
|
+
return base.t === "list" ? base.list : void 0;
|
|
501
561
|
}
|
|
502
562
|
//#endregion
|
|
503
563
|
//#region src/space.ts
|
|
@@ -860,7 +920,12 @@ function axisTargets(s, at, a) {
|
|
|
860
920
|
case "successors":
|
|
861
921
|
case "predecessors":
|
|
862
922
|
case "reachable": return graphTargets(s, at, a.t);
|
|
923
|
+
case "reverse": {
|
|
924
|
+
const r = axisTargets(s, at, a.axis);
|
|
925
|
+
return r.ok ? ok(r.value.toReversed()) : r;
|
|
926
|
+
}
|
|
863
927
|
case "table": return fail(vexError("no-table", `the axis "table" needs the tables of the evaluation, and "${a.name}" is not one of them`, at));
|
|
928
|
+
case "list": return fail(vexError("bad-expression", "the axis \"list\" needs the evaluator, which gives the value of its list", at));
|
|
864
929
|
}
|
|
865
930
|
}
|
|
866
931
|
/**
|
|
@@ -984,7 +1049,10 @@ const LIST_OPS = /* @__PURE__ */ new Set([
|
|
|
984
1049
|
"keys",
|
|
985
1050
|
"reduce",
|
|
986
1051
|
"first",
|
|
987
|
-
"errors"
|
|
1052
|
+
"errors",
|
|
1053
|
+
"sortBy",
|
|
1054
|
+
"take",
|
|
1055
|
+
"nth"
|
|
988
1056
|
]);
|
|
989
1057
|
/** This function tells if a name is the name of a core list op. */
|
|
990
1058
|
const isListOp = (op) => LIST_OPS.has(op);
|
|
@@ -1002,6 +1070,17 @@ const isSpecialForm = (op) => SPECIAL_FORMS.has(op);
|
|
|
1002
1070
|
* This function compiles an expression. The result gives the same results and the same traces as `evaluate`,
|
|
1003
1071
|
* because `evaluate` uses it too.
|
|
1004
1072
|
*/
|
|
1073
|
+
/** A bound function (FN.BIND): its parameters, its compiled body and the names of the place of its binding. */
|
|
1074
|
+
var Closure = class {
|
|
1075
|
+
params;
|
|
1076
|
+
body;
|
|
1077
|
+
env;
|
|
1078
|
+
constructor(params, body, env) {
|
|
1079
|
+
this.params = params;
|
|
1080
|
+
this.body = body;
|
|
1081
|
+
this.env = env;
|
|
1082
|
+
}
|
|
1083
|
+
};
|
|
1005
1084
|
/** The deepest nesting of an expression that `compile` accepts (EVAL.NESTING). */
|
|
1006
1085
|
const MAX_NESTING = 500;
|
|
1007
1086
|
const DEPTH_MESSAGE = "the evaluation is too deep for the call stack of the engine";
|
|
@@ -1143,8 +1222,14 @@ var Compiler = class {
|
|
|
1143
1222
|
case "ref": return this.#ref(e, info);
|
|
1144
1223
|
case "var": {
|
|
1145
1224
|
const name = e.name;
|
|
1146
|
-
return (f, c) =>
|
|
1225
|
+
return (f, c) => {
|
|
1226
|
+
const bound = f.env.get(name);
|
|
1227
|
+
if (bound?.ok === true && bound.value instanceof Closure) return this.#emit(c, info, f, this.#err("kind-mismatch", `the name "${name}" is a function, not a value`, f, path));
|
|
1228
|
+
return this.#emit(c, info, f, bound ?? this.#err("unbound", `the name "${name}" has no binding`, f, path));
|
|
1229
|
+
};
|
|
1147
1230
|
}
|
|
1231
|
+
case "fn": return this.#fn(e, path);
|
|
1232
|
+
case "call": return this.#callFn(e, info);
|
|
1148
1233
|
case "let": {
|
|
1149
1234
|
const binds = Object.entries(e.bind).map(([name, be], i) => [name, this.node(be, [...path, i])]);
|
|
1150
1235
|
const body = this.node(e.body, [...path, binds.length]);
|
|
@@ -1190,6 +1275,36 @@ var Compiler = class {
|
|
|
1190
1275
|
#nodes(es, path) {
|
|
1191
1276
|
return es.map((x, i) => this.node(x, [...path, i]));
|
|
1192
1277
|
}
|
|
1278
|
+
/**
|
|
1279
|
+
* This method compiles a function (FN.BIND). Its value is a closure: the parameters, the body, and the names of the
|
|
1280
|
+
* place of its binding. A binding of `let` does not see the other bindings, so a function cannot call itself.
|
|
1281
|
+
*/
|
|
1282
|
+
#fn(e, path) {
|
|
1283
|
+
const body = this.node(e.body, [...path, 0]);
|
|
1284
|
+
return (f) => ok(new Closure(e.params, body, f.env));
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* This method compiles a call (FN.CALL, FN.ARGS). Each parameter gets the result of its argument at the call, so an
|
|
1288
|
+
* argument that fails has an effect only where the body reads it. The body evaluates at the position of the call.
|
|
1289
|
+
*/
|
|
1290
|
+
#callFn(e, info) {
|
|
1291
|
+
const { path } = info;
|
|
1292
|
+
const args = this.#nodes(e.args, path);
|
|
1293
|
+
return (f, c) => {
|
|
1294
|
+
const bound = f.env.get(e.fn);
|
|
1295
|
+
if (bound === void 0) return this.#emit(c, info, f, this.#err("unbound", `the function "${e.fn}" has no binding`, f, path));
|
|
1296
|
+
if (!bound.ok) return this.#emit(c, info, f, bound);
|
|
1297
|
+
const callee = bound.value;
|
|
1298
|
+
if (!(callee instanceof Closure)) return this.#emit(c, info, f, this.#err("kind-mismatch", `the name "${e.fn}" is a value, not a function`, f, path));
|
|
1299
|
+
if (callee.params.length !== args.length) return this.#emit(c, info, f, this.#err("bad-expression", `the function "${e.fn}" takes ${String(callee.params.length)} arguments, not ${String(args.length)}`, f, path));
|
|
1300
|
+
const env = new Map(callee.env);
|
|
1301
|
+
callee.params.forEach((p, i) => env.set(p, args[i](f, c)));
|
|
1302
|
+
return this.#emit(c, info, f, callee.body({
|
|
1303
|
+
pos: f.pos,
|
|
1304
|
+
env
|
|
1305
|
+
}, c));
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1193
1308
|
/** This method gives an error of a handler the path, the origin and the focus of its node, if it has no origin. */
|
|
1194
1309
|
#locate(r, f, path) {
|
|
1195
1310
|
if (r.ok || r.error.origin !== void 0) return r;
|
|
@@ -1481,6 +1596,8 @@ var Compiler = class {
|
|
|
1481
1596
|
/** This method compiles the targets of an axis. For `where`, a failed test keeps the target with its error. */
|
|
1482
1597
|
#targets(a, path, counter) {
|
|
1483
1598
|
if (a.t === "table") return (_f, c) => ok(c.space.keys.map((key) => ({ key })));
|
|
1599
|
+
const base = a.t === "reverse" ? a.axis : a;
|
|
1600
|
+
if (base.t === "list") return this.#listTargets(base.list, a.t === "reverse", path, counter);
|
|
1484
1601
|
if (a.t !== "where") return (f, c) => {
|
|
1485
1602
|
const keys = axisTargets(c.space, f.pos, a);
|
|
1486
1603
|
return keys.ok ? ok(keys.value.map((key) => ({ key }))) : fail({
|
|
@@ -1522,6 +1639,23 @@ var Compiler = class {
|
|
|
1522
1639
|
return ok(out);
|
|
1523
1640
|
};
|
|
1524
1641
|
}
|
|
1642
|
+
/**
|
|
1643
|
+
* This method compiles the targets of the axis list (AXIS.LIST): the keys of the items of a list, in its order. The
|
|
1644
|
+
* expression evaluates at the key of the address. An error item keeps its target with its error.
|
|
1645
|
+
*/
|
|
1646
|
+
#listTargets(list, reverse, path, counter) {
|
|
1647
|
+
const node = this.node(list, [...path, counter.next++]);
|
|
1648
|
+
return (f, c) => {
|
|
1649
|
+
const r = node(f, c);
|
|
1650
|
+
if (!r.ok) return r;
|
|
1651
|
+
if (!isVexList(r.value)) return this.#err("kind-mismatch", `the axis "list" needs a list, not a ${describeType(r.value)}`, f, path);
|
|
1652
|
+
const items = r.value.items.map(({ key, result }) => result.ok ? { key } : {
|
|
1653
|
+
key,
|
|
1654
|
+
test: result
|
|
1655
|
+
});
|
|
1656
|
+
return ok(reverse ? items.toReversed() : items);
|
|
1657
|
+
};
|
|
1658
|
+
}
|
|
1525
1659
|
#app(op, args, info) {
|
|
1526
1660
|
const { path } = info;
|
|
1527
1661
|
const listOp = isListOp(op) ? op : void 0;
|
|
@@ -1626,7 +1760,7 @@ var Compiler = class {
|
|
|
1626
1760
|
return r;
|
|
1627
1761
|
}
|
|
1628
1762
|
#listOp(op, list, rest, f, path) {
|
|
1629
|
-
const optsArg = op === "reduce" ? rest[1] : rest[0];
|
|
1763
|
+
const optsArg = op === "reduce" || op === "take" || op === "nth" ? rest[1] : rest[0];
|
|
1630
1764
|
const depth = list.items.find((it) => !it.result.ok && isDepthError(it.result.error))?.result;
|
|
1631
1765
|
if (depth !== void 0 && !depth.ok) return fail(depth.error);
|
|
1632
1766
|
const strict = optsArg?.strict === true;
|
|
@@ -1642,7 +1776,22 @@ var Compiler = class {
|
|
|
1642
1776
|
return bad === void 0 ? ok(okValues) : this.#err("kind-mismatch", `"${op}" needs booleans, but an item is a ${describeType(bad)}`, f, path, { op });
|
|
1643
1777
|
};
|
|
1644
1778
|
const empty = () => this.#err("empty", `"${op}" has no values to work with`, f, path, { op });
|
|
1779
|
+
const whole = () => {
|
|
1780
|
+
const n = rest[0];
|
|
1781
|
+
return Number.isSafeInteger(n) && n >= 0 ? ok(n) : this.#err("kind-mismatch", `"${op}" needs a whole number from 0, not ${describeType(n)}`, f, path, { op });
|
|
1782
|
+
};
|
|
1783
|
+
const okItems = list.items.filter((it) => it.result.ok);
|
|
1645
1784
|
switch (op) {
|
|
1785
|
+
case "sortBy": return this.#sortBy(okItems, okValues, f, path);
|
|
1786
|
+
case "take": {
|
|
1787
|
+
const n = whole();
|
|
1788
|
+
return n.ok ? ok(vexList(okItems.slice(0, n.value))) : n;
|
|
1789
|
+
}
|
|
1790
|
+
case "nth": {
|
|
1791
|
+
const i = whole();
|
|
1792
|
+
if (!i.ok) return i;
|
|
1793
|
+
return i.value < okValues.length ? ok(okValues[i.value]) : this.#err("empty", `"nth" has no item at the position ${String(i.value)}, and the list has ${String(okValues.length)} items`, f, path, { op });
|
|
1794
|
+
}
|
|
1646
1795
|
case "count": return ok(okValues.length);
|
|
1647
1796
|
case "values": return ok(Object.freeze([...okValues]));
|
|
1648
1797
|
case "keys": return ok(Object.freeze(list.items.filter((it) => it.result.ok).map((it) => it.key)));
|
|
@@ -1677,6 +1826,21 @@ var Compiler = class {
|
|
|
1677
1826
|
}
|
|
1678
1827
|
}
|
|
1679
1828
|
/**
|
|
1829
|
+
* This method sorts the items of a list by their values, from the smallest (LIST.SORT). The sort is stable. The values
|
|
1830
|
+
* are all numbers without NaN, or all texts. Texts compare by their UTF-16 code units.
|
|
1831
|
+
*/
|
|
1832
|
+
#sortBy(items, values, f, path) {
|
|
1833
|
+
const kind = typeof values[0];
|
|
1834
|
+
const bad = values.findIndex((x) => typeof x !== kind || kind !== "number" && kind !== "string" || Number.isNaN(x));
|
|
1835
|
+
if (bad >= 0) return this.#err("kind-mismatch", `"sortBy" needs numbers without NaN or texts, all of one kind, but an item is ${Number.isNaN(values[bad]) ? "NaN" : `a ${describeType(values[bad])}`}`, f, path, { op: "sortBy" });
|
|
1836
|
+
const order = values.map((_x, i) => i).toSorted((i, j) => {
|
|
1837
|
+
const a = values[i];
|
|
1838
|
+
const b = values[j];
|
|
1839
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
1840
|
+
});
|
|
1841
|
+
return ok(vexList(order.map((i) => items[i])));
|
|
1842
|
+
}
|
|
1843
|
+
/**
|
|
1680
1844
|
* This method folds the values with a domain op, from the left. An empty list gives the identity of the op, if one
|
|
1681
1845
|
* domain has an identity for the op (LIST.EMPTY). The option `domain` names that domain when more domains have one.
|
|
1682
1846
|
*/
|
|
@@ -1749,6 +1913,8 @@ function labelOf(e) {
|
|
|
1749
1913
|
case "ref": return e.path.length === 0 ? "(record)" : e.path.join(".");
|
|
1750
1914
|
case "app": return e.op;
|
|
1751
1915
|
case "let": return `let ${Object.keys(e.bind).join(", ")}`;
|
|
1916
|
+
case "fn": return `fn (${e.params.join(", ")})`;
|
|
1917
|
+
case "call": return `call ${e.fn}`;
|
|
1752
1918
|
case "var": return e.name;
|
|
1753
1919
|
case "rec": return `{ ${Object.keys(e.fields).join(", ")} }`;
|
|
1754
1920
|
case "each":
|
|
@@ -1807,6 +1973,20 @@ function formatTrace(trace, domains = []) {
|
|
|
1807
1973
|
const sizeOf = (e) => 1 + children(e).reduce((s, c) => s + sizeOf(c), 0);
|
|
1808
1974
|
//#endregion
|
|
1809
1975
|
//#region src/deps.ts
|
|
1976
|
+
/** This function gives the functions of a scope without some names: a parameter or a binding hides a function. */
|
|
1977
|
+
const without = (fns, names) => new Map([...fns].filter(([n]) => !names.includes(n)));
|
|
1978
|
+
/**
|
|
1979
|
+
* This function gives the functions that each child of a node sees. The names of a loop hide the functions with those
|
|
1980
|
+
* names in its steps, its test of until and its result. The values of init, the tests of where and an address see the
|
|
1981
|
+
* functions outside.
|
|
1982
|
+
*/
|
|
1983
|
+
function childFns(x, fns, count) {
|
|
1984
|
+
if (x.tag !== "fold" && x.tag !== "iterate") return () => fns;
|
|
1985
|
+
const hidden = without(fns, Object.keys(x.init));
|
|
1986
|
+
const first = Object.keys(x.init).length;
|
|
1987
|
+
const steps = first + Object.keys(x.step).length;
|
|
1988
|
+
return (i) => i >= first && (i < steps || i === count - 1 || x.tag === "iterate") ? hidden : fns;
|
|
1989
|
+
}
|
|
1810
1990
|
/**
|
|
1811
1991
|
* This function gives the static reads of an expression. A host can use them to recompute a program only
|
|
1812
1992
|
* when a field that it reads changes. A computed move reads a key that only a run knows. Its address keeps the
|
|
@@ -1817,7 +1997,8 @@ function deps(e) {
|
|
|
1817
1997
|
const todo = [{
|
|
1818
1998
|
x: e,
|
|
1819
1999
|
axes: [],
|
|
1820
|
-
axisAt: []
|
|
2000
|
+
axisAt: [],
|
|
2001
|
+
fns: /* @__PURE__ */ new Map()
|
|
1821
2002
|
}];
|
|
1822
2003
|
for (let item = todo.pop(); item !== void 0; item = todo.pop()) {
|
|
1823
2004
|
const { x } = item;
|
|
@@ -1827,20 +2008,54 @@ function deps(e) {
|
|
|
1827
2008
|
axes: item.axes,
|
|
1828
2009
|
axisAt: item.axisAt
|
|
1829
2010
|
});
|
|
2011
|
+
if (x.tag === "let") {
|
|
2012
|
+
const names = Object.keys(x.bind);
|
|
2013
|
+
const inner = new Map(without(item.fns, names));
|
|
2014
|
+
for (const [name, b] of Object.entries(x.bind)) if (b.tag === "fn") inner.set(name, {
|
|
2015
|
+
fn: b,
|
|
2016
|
+
fns: item.fns
|
|
2017
|
+
});
|
|
2018
|
+
todo.push({
|
|
2019
|
+
...item,
|
|
2020
|
+
x: x.body,
|
|
2021
|
+
fns: inner
|
|
2022
|
+
});
|
|
2023
|
+
for (const b of Object.values(x.bind).toReversed()) if (b.tag !== "fn") todo.push({
|
|
2024
|
+
...item,
|
|
2025
|
+
x: b
|
|
2026
|
+
});
|
|
2027
|
+
continue;
|
|
2028
|
+
}
|
|
2029
|
+
if (x.tag === "call") {
|
|
2030
|
+
const bound = item.fns.get(x.fn);
|
|
2031
|
+
if (bound !== void 0) todo.push({
|
|
2032
|
+
...item,
|
|
2033
|
+
x: bound.fn.body,
|
|
2034
|
+
fns: without(bound.fns, bound.fn.params)
|
|
2035
|
+
});
|
|
2036
|
+
for (const a of x.args.toReversed()) todo.push({
|
|
2037
|
+
...item,
|
|
2038
|
+
x: a
|
|
2039
|
+
});
|
|
2040
|
+
continue;
|
|
2041
|
+
}
|
|
1830
2042
|
const all = children(x);
|
|
2043
|
+
const fnsOf = childFns(x, item.fns, all.length);
|
|
1831
2044
|
const axial = x.tag === "each" || x.tag === "fold";
|
|
1832
2045
|
const first = x.tag === "fold" ? Object.keys(x.init).length : 0;
|
|
1833
2046
|
const last = axial ? all.length - addrExprs(x.at).length - (x.tag === "fold" ? 1 : 0) : 0;
|
|
2047
|
+
const listAt = axial && listOf(x.axis) !== void 0 ? x.tag === "fold" ? first + Object.keys(x.step).length : 1 : -1;
|
|
1834
2048
|
const axes = axial ? [...item.axes, x.axis.t] : item.axes;
|
|
1835
2049
|
const axisAt = axial ? [...item.axisAt, x.at ?? []] : item.axisAt;
|
|
1836
|
-
for (let i = all.length - 1; i >= 0; i--)
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
2050
|
+
for (let i = all.length - 1; i >= 0; i--) {
|
|
2051
|
+
const inside = i >= first && i < last && i !== listAt;
|
|
2052
|
+
todo.push({
|
|
2053
|
+
x: all[i],
|
|
2054
|
+
axes: inside ? axes : item.axes,
|
|
2055
|
+
axisAt: inside ? axisAt : item.axisAt,
|
|
2056
|
+
fns: fnsOf(i)
|
|
2057
|
+
});
|
|
2058
|
+
}
|
|
1844
2059
|
}
|
|
1845
2060
|
return out;
|
|
1846
2061
|
}
|
|
@@ -1981,7 +2196,22 @@ function mapValues(u, f) {
|
|
|
1981
2196
|
out["at"] = moves;
|
|
1982
2197
|
};
|
|
1983
2198
|
const axis = (a) => {
|
|
1984
|
-
if (!isRecord(a)
|
|
2199
|
+
if (!isRecord(a)) return ok(a);
|
|
2200
|
+
if (a["t"] === "list") {
|
|
2201
|
+
const list = mapValues(a["list"], f);
|
|
2202
|
+
return list.ok ? ok({
|
|
2203
|
+
...a,
|
|
2204
|
+
list: list.value
|
|
2205
|
+
}) : list;
|
|
2206
|
+
}
|
|
2207
|
+
if (a["t"] === "reverse") {
|
|
2208
|
+
const inner = axis(a["axis"]);
|
|
2209
|
+
return inner.ok ? ok({
|
|
2210
|
+
...a,
|
|
2211
|
+
axis: inner.value
|
|
2212
|
+
}) : inner;
|
|
2213
|
+
}
|
|
2214
|
+
if (a["t"] !== "where") return ok(a);
|
|
1985
2215
|
const inner = axis(a["axis"]);
|
|
1986
2216
|
if (!inner.ok) return inner;
|
|
1987
2217
|
const test = mapValues(a["test"], f);
|
|
@@ -2015,6 +2245,8 @@ function mapValues(u, f) {
|
|
|
2015
2245
|
return each("init") ?? each("step") ?? child("result") ?? address() ?? ok(out);
|
|
2016
2246
|
}
|
|
2017
2247
|
case "iterate": return each("init") ?? each("step") ?? (u["until"] === void 0 ? void 0 : child("until")) ?? child("result") ?? ok(out);
|
|
2248
|
+
case "fn": return child("body") ?? ok(out);
|
|
2249
|
+
case "call": return each("args") ?? ok(out);
|
|
2018
2250
|
case "ref": return address() ?? ok(out);
|
|
2019
2251
|
default: return ok(out);
|
|
2020
2252
|
}
|
|
@@ -2190,6 +2422,10 @@ function setDone(t, column, key, result) {
|
|
|
2190
2422
|
}
|
|
2191
2423
|
/** This function adds the direction of one move or one axis of a read of a column from itself. */
|
|
2192
2424
|
function addDirection(d, m) {
|
|
2425
|
+
if (m.t === "reverse") {
|
|
2426
|
+
addDirection(d, m.axis);
|
|
2427
|
+
return;
|
|
2428
|
+
}
|
|
2193
2429
|
if (m.t === "offset" || m.t === "window") {
|
|
2194
2430
|
const steps = m.t === "offset" ? [m.d.find((x) => x !== 0) ?? 0] : [m.from, m.to];
|
|
2195
2431
|
if (steps.some((x) => x < 0)) d.add("before");
|
|
@@ -2224,7 +2460,12 @@ function readsOf(t, self) {
|
|
|
2224
2460
|
if (e.tag === "each" || e.tag === "fold") {
|
|
2225
2461
|
const { base, tests } = partsOf(e.axis);
|
|
2226
2462
|
for (const x of [...e.tag === "each" ? [e.body] : Object.values(e.step), ...tests]) todo.push([x, base]);
|
|
2227
|
-
|
|
2463
|
+
const list = listOf(e.axis);
|
|
2464
|
+
for (const x of [
|
|
2465
|
+
...e.tag === "fold" ? [...Object.values(e.init), e.result] : [],
|
|
2466
|
+
...addrExprs(e.at),
|
|
2467
|
+
...list === void 0 ? [] : [list]
|
|
2468
|
+
]) todo.push([x, axis]);
|
|
2228
2469
|
} else if (e.tag !== "ext") for (const child of children(e)) todo.push([child, axis]);
|
|
2229
2470
|
else if (e.kind === "vex.cell" && isCellRef(e.data)) {
|
|
2230
2471
|
columns.add(e.data.column);
|
|
@@ -2818,6 +3059,37 @@ var ChainImpl = class ChainImpl {
|
|
|
2818
3059
|
expr: let_(bindExprs, inner.program)
|
|
2819
3060
|
});
|
|
2820
3061
|
}
|
|
3062
|
+
functions(fns, body) {
|
|
3063
|
+
const depth = this.#s.depth;
|
|
3064
|
+
const binds = {};
|
|
3065
|
+
const calls = {};
|
|
3066
|
+
for (const [n, def] of Object.entries(fns)) {
|
|
3067
|
+
if (def.kind !== "vex.fn") throw new TypeError(`the function "${n}" is not a function of root.fn`);
|
|
3068
|
+
const unique = `${n}$${depth}`;
|
|
3069
|
+
const params = def.params.map((_k, i) => `${unique}$${i}`);
|
|
3070
|
+
const chains = params.map((p) => new ChainImpl({
|
|
3071
|
+
...this.#s,
|
|
3072
|
+
expr: v(p),
|
|
3073
|
+
addr: [],
|
|
3074
|
+
depth: depth + 1
|
|
3075
|
+
}));
|
|
3076
|
+
binds[unique] = fn(params, def.build(...chains).program);
|
|
3077
|
+
calls[n] = (...args) => new ChainImpl({
|
|
3078
|
+
...this.#s,
|
|
3079
|
+
expr: call(unique, ...args.map((a) => toExpr(a, []))),
|
|
3080
|
+
addr: [],
|
|
3081
|
+
depth: depth + 1
|
|
3082
|
+
});
|
|
3083
|
+
}
|
|
3084
|
+
const inner = body(new ChainImpl({
|
|
3085
|
+
...this.#s,
|
|
3086
|
+
depth: depth + 1
|
|
3087
|
+
}), calls);
|
|
3088
|
+
return new ChainImpl({
|
|
3089
|
+
...this.#s,
|
|
3090
|
+
expr: let_(binds, inner.program)
|
|
3091
|
+
});
|
|
3092
|
+
}
|
|
2821
3093
|
/**
|
|
2822
3094
|
* This method gives the names of a loop. Each name of `init` gets a name that is unique at the depth of the chain.
|
|
2823
3095
|
* It also gets its first value at the address of the chain, and a chain of its value.
|
|
@@ -2860,9 +3132,13 @@ var ChainImpl = class ChainImpl {
|
|
|
2860
3132
|
addr: []
|
|
2861
3133
|
});
|
|
2862
3134
|
const inner = body(inTarget);
|
|
2863
|
-
const
|
|
3135
|
+
const ordered = opts?.reverse === true ? {
|
|
3136
|
+
t: "reverse",
|
|
3137
|
+
axis: a
|
|
3138
|
+
} : a;
|
|
3139
|
+
const axis = opts?.where === void 0 ? ordered : {
|
|
2864
3140
|
t: "where",
|
|
2865
|
-
axis:
|
|
3141
|
+
axis: ordered,
|
|
2866
3142
|
test: opts.where(inTarget).program
|
|
2867
3143
|
};
|
|
2868
3144
|
return new ListChainImpl({
|
|
@@ -2928,7 +3204,7 @@ function evalOptions(s, k) {
|
|
|
2928
3204
|
...env.options.extensions === void 0 ? {} : { extensions: env.options.extensions }
|
|
2929
3205
|
};
|
|
2930
3206
|
}
|
|
2931
|
-
var ListChainImpl = class {
|
|
3207
|
+
var ListChainImpl = class ListChainImpl {
|
|
2932
3208
|
kind = "vex.list-chain";
|
|
2933
3209
|
#s;
|
|
2934
3210
|
constructor(s) {
|
|
@@ -2938,7 +3214,9 @@ var ListChainImpl = class {
|
|
|
2938
3214
|
return this.#s.expr;
|
|
2939
3215
|
}
|
|
2940
3216
|
fold(init, step, result) {
|
|
2941
|
-
const
|
|
3217
|
+
const e = this.#s.expr;
|
|
3218
|
+
const loop = e.tag === "let" ? e.body : e;
|
|
3219
|
+
if (loop.tag !== "each") throw new TypeError("fold needs the list of an axis: go over the items of the list with each first");
|
|
2942
3220
|
const names = new ChainImpl({
|
|
2943
3221
|
...this.#s,
|
|
2944
3222
|
expr: lit(null)
|
|
@@ -2949,9 +3227,10 @@ var ListChainImpl = class {
|
|
|
2949
3227
|
addr: []
|
|
2950
3228
|
});
|
|
2951
3229
|
const next = stepExprs(step(names.vars, item), names.unique, []);
|
|
3230
|
+
const folded = fold(loop.axis, names.first, next, toExpr(result(names.vars), this.#s.addr), loop.at);
|
|
2952
3231
|
return new ChainImpl({
|
|
2953
3232
|
...this.#s,
|
|
2954
|
-
expr:
|
|
3233
|
+
expr: e.tag === "let" ? let_(e.bind, folded) : folded
|
|
2955
3234
|
});
|
|
2956
3235
|
}
|
|
2957
3236
|
#op(op, extra, opts) {
|
|
@@ -3004,6 +3283,46 @@ var ListChainImpl = class {
|
|
|
3004
3283
|
reduce(op, opts) {
|
|
3005
3284
|
return this.#op("reduce", [lit(op)], opts);
|
|
3006
3285
|
}
|
|
3286
|
+
#list(op, extra, opts) {
|
|
3287
|
+
return new ListChainImpl({
|
|
3288
|
+
...this.#s,
|
|
3289
|
+
expr: this.#op(op, extra, opts).program
|
|
3290
|
+
});
|
|
3291
|
+
}
|
|
3292
|
+
sortBy(opts) {
|
|
3293
|
+
return this.#list("sortBy", [], opts);
|
|
3294
|
+
}
|
|
3295
|
+
take(n, opts) {
|
|
3296
|
+
return this.#list("take", [lit(n)], opts);
|
|
3297
|
+
}
|
|
3298
|
+
nth(i, opts) {
|
|
3299
|
+
return this.#op("nth", [lit(i)], opts);
|
|
3300
|
+
}
|
|
3301
|
+
each(body, opts) {
|
|
3302
|
+
const start = new ChainImpl({
|
|
3303
|
+
...this.#s,
|
|
3304
|
+
expr: lit(null),
|
|
3305
|
+
addr: [],
|
|
3306
|
+
depth: this.#s.depth + 1
|
|
3307
|
+
});
|
|
3308
|
+
const list = {
|
|
3309
|
+
t: "list",
|
|
3310
|
+
list: this.#s.expr
|
|
3311
|
+
};
|
|
3312
|
+
const ordered = opts?.reverse === true ? {
|
|
3313
|
+
t: "reverse",
|
|
3314
|
+
axis: list
|
|
3315
|
+
} : list;
|
|
3316
|
+
const axis = opts?.where === void 0 ? ordered : {
|
|
3317
|
+
t: "where",
|
|
3318
|
+
axis: ordered,
|
|
3319
|
+
test: opts.where(start).program
|
|
3320
|
+
};
|
|
3321
|
+
return new ListChainImpl({
|
|
3322
|
+
...this.#s,
|
|
3323
|
+
expr: each(axis, body(start).program)
|
|
3324
|
+
});
|
|
3325
|
+
}
|
|
3007
3326
|
result(k) {
|
|
3008
3327
|
return evaluate(this.#s.expr, evalOptions(this.#s, k));
|
|
3009
3328
|
}
|
|
@@ -3054,6 +3373,13 @@ var RootImpl = class {
|
|
|
3054
3373
|
sheet() {
|
|
3055
3374
|
return new SheetImpl(this.#env, /* @__PURE__ */ new Map());
|
|
3056
3375
|
}
|
|
3376
|
+
fn(params, build) {
|
|
3377
|
+
return {
|
|
3378
|
+
kind: "vex.fn",
|
|
3379
|
+
params,
|
|
3380
|
+
build
|
|
3381
|
+
};
|
|
3382
|
+
}
|
|
3057
3383
|
rec(fields) {
|
|
3058
3384
|
const out = {};
|
|
3059
3385
|
for (const [n, a] of Object.entries(fields)) out[n] = toExpr(a, []);
|
|
@@ -3144,6 +3470,6 @@ function vex(...domains) {
|
|
|
3144
3470
|
};
|
|
3145
3471
|
}
|
|
3146
3472
|
//#endregion
|
|
3147
|
-
export { CELL_KIND, LIST_OPS, MAX_ITERATIONS, MAX_NESTING, SPECIAL_FORMS, SheetRun, addrExprs, app, applyMove, axes, axisTargets, cell, chainResult, children, codeOf, compile, defineDomain, deps, describeType, domainOf, each, evaluate, explain, ext, fail, findMethod, fixAddr, fold, formatError, formatTrace, getOr, index, isAxis, isDepthError, isExpr, isFieldName, isFixedAddr, isListOp, isMove, isNone, isOpName, isSome, isStackOverflow, isVexList, iterate, key, labelOf, let_, liftsAt, lit, mapResult, matchesKind, monoids, moveExpr, nestingOf, none, offset, ok, origin, other, parent, parse, previewValue, rec, ref, resolveAddr, resolveOp, safeIs, serialize, shapeError, sizeOf, some, space, toOptional, toPath, traversal, v, vex, vexError, vexList };
|
|
3473
|
+
export { CELL_KIND, LIST_OPS, MAX_ITERATIONS, MAX_NESTING, SPECIAL_FORMS, SheetRun, addrExprs, app, applyMove, axes, axisExprs, axisTargets, call, cell, chainResult, children, codeOf, compile, defineDomain, deps, describeType, domainOf, each, evaluate, explain, ext, fail, findMethod, fixAddr, fn, fold, formatError, formatTrace, getOr, index, isAxis, isDepthError, isExpr, isFieldName, isFixedAddr, isListOp, isMove, isNone, isOpName, isSome, isStackOverflow, isVexList, iterate, key, labelOf, let_, liftsAt, listOf, lit, mapResult, matchesKind, monoids, moveExpr, nestingOf, none, offset, ok, origin, other, parent, parse, previewValue, rec, ref, resolveAddr, resolveOp, safeIs, serialize, shapeError, sizeOf, some, space, toOptional, toPath, traversal, v, vex, vexError, vexList };
|
|
3148
3474
|
|
|
3149
3475
|
//# sourceMappingURL=index.js.map
|