@mark1russell7/vex 0.13.4 → 0.14.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 +21 -13
- package/build/index.d.ts.map +1 -1
- package/build/index.js +218 -43
- package/build/index.js.map +1 -1
- package/package.json +2 -2
- package/src/builder.ts +58 -13
- package/src/deps.ts +3 -1
- package/src/document.ts +24 -7
- package/src/evaluate.ts +47 -14
- package/src/ir.ts +25 -10
- package/src/item-names.ts +29 -0
- package/src/kernel.ts +49 -10
- package/src/write.ts +7 -2
package/build/index.js
CHANGED
|
@@ -84,7 +84,38 @@ function vexError(kind, message, details = {}) {
|
|
|
84
84
|
}
|
|
85
85
|
/** This function gives a short text for an error, for example `#REF! unknown-key: there is no key "Z"`. */
|
|
86
86
|
const formatError = (e) => `${e.code} ${e.kind}: ${e.message}`;
|
|
87
|
+
// src/item-names.ts
|
|
88
|
+
/**
|
|
89
|
+
* The check of the names `as` of a loop over the axis `list` (AXIS.LIST.SCOPE). The package index does not export this
|
|
90
|
+
* module: `isExpr`, `shapeError` and `fromDocument` use it, so an expression and a document follow the same rule.
|
|
91
|
+
*/
|
|
92
|
+
const isObject$1 = (u) => typeof u === "object" && u !== null;
|
|
93
|
+
/** This function tells if the base axis of an axis is `list`, through `where` and `reverse`. It uses a loop, not recursion. */
|
|
94
|
+
function isListAxis(u) {
|
|
95
|
+
let x = u;
|
|
96
|
+
while (isObject$1(x) && x["t"] === "where") x = x["axis"];
|
|
97
|
+
if (isObject$1(x) && x["t"] === "reverse") x = x["axis"];
|
|
98
|
+
return isObject$1(x) && x["t"] === "list";
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* This function gives the reason that the names `as` of a loop are not valid, or `undefined` for valid names or no
|
|
102
|
+
* names (AXIS.LIST.SCOPE). The names are one or two distinct texts, the axis is `list`, and no name is a name of `init`.
|
|
103
|
+
*/
|
|
104
|
+
function itemNamesError(u) {
|
|
105
|
+
const as = u["as"];
|
|
106
|
+
if (as === void 0) return void 0;
|
|
107
|
+
if (!Array.isArray(as) || as.length === 0 || as.length > 2 || !as.every((n) => typeof n === "string")) return "\"as\" is a list of one or two names: the item, and then its index";
|
|
108
|
+
if (as[0] === as[1]) return `the two names of "as" are the same name "${String(as[0])}"`;
|
|
109
|
+
if (!isListAxis(u["axis"])) return "\"as\" needs the axis \"list\", also inside \"reverse\" and \"where\"";
|
|
110
|
+
const init = u["init"];
|
|
111
|
+
const clash = isObject$1(init) ? as.find((n) => Object.hasOwn(init, n)) : void 0;
|
|
112
|
+
return clash === void 0 ? void 0 : `the name "${clash}" of "as" is a name of "init" too`;
|
|
113
|
+
}
|
|
87
114
|
// src/ir.ts
|
|
115
|
+
/**
|
|
116
|
+
* The expression IR of Vex. It is plain JSON data. Its first three kinds (`lit`, `ref`, `app`) have the same
|
|
117
|
+
* shape as the expressions of `render`.
|
|
118
|
+
*/
|
|
88
119
|
/** The largest number of steps of `iterate` (ITERATE.LIMIT). */
|
|
89
120
|
const MAX_ITERATIONS = 1e5;
|
|
90
121
|
/** This function makes a literal. */
|
|
@@ -130,38 +161,47 @@ const rec = (fields) => ({
|
|
|
130
161
|
});
|
|
131
162
|
/**
|
|
132
163
|
* This function evaluates `body` once for each target of `axis`. The result is a list. With `at`, the axis finds its
|
|
133
|
-
* targets from the key of that address, not from the focus.
|
|
164
|
+
* targets from the key of that address, not from the focus. With `as`, the body of a loop over the axis `list` reads the
|
|
165
|
+
* value of each item and its position by these names (AXIS.LIST.ITEM, AXIS.LIST.INDEX).
|
|
134
166
|
*/
|
|
135
|
-
function each(axis, body, at) {
|
|
167
|
+
function each(axis, body, at, as) {
|
|
168
|
+
const named = as === void 0 ? {} : { as };
|
|
136
169
|
return at === void 0 || at.length === 0 ? {
|
|
137
170
|
tag: "each",
|
|
138
171
|
axis,
|
|
139
|
-
body
|
|
172
|
+
body,
|
|
173
|
+
...named
|
|
140
174
|
} : {
|
|
141
175
|
tag: "each",
|
|
142
176
|
axis,
|
|
143
177
|
body,
|
|
144
|
-
at
|
|
178
|
+
at,
|
|
179
|
+
...named
|
|
145
180
|
};
|
|
146
181
|
}
|
|
147
182
|
/**
|
|
148
183
|
* This function makes a fold: a step at each target of `axis`, in order. `init` gives the names and their first
|
|
149
|
-
* values, and `step` gives the next values. `result` gives the value from the last values (LOOP.NAMES, FOLD.ORDER).
|
|
184
|
+
* values, and `step` gives the next values. `result` gives the value from the last values (LOOP.NAMES, FOLD.ORDER). With
|
|
185
|
+
* `as`, the steps of a fold over the axis `list` read the value of each item and its position by these names
|
|
186
|
+
* (AXIS.LIST.ITEM, AXIS.LIST.INDEX).
|
|
150
187
|
*/
|
|
151
|
-
function fold(axis, init, step, result, at) {
|
|
188
|
+
function fold(axis, init, step, result, at, as) {
|
|
189
|
+
const named = as === void 0 ? {} : { as };
|
|
152
190
|
return at === void 0 || at.length === 0 ? {
|
|
153
191
|
tag: "fold",
|
|
154
192
|
axis,
|
|
155
193
|
init,
|
|
156
194
|
step,
|
|
157
|
-
result
|
|
195
|
+
result,
|
|
196
|
+
...named
|
|
158
197
|
} : {
|
|
159
198
|
tag: "fold",
|
|
160
199
|
axis,
|
|
161
200
|
init,
|
|
162
201
|
step,
|
|
163
202
|
result,
|
|
164
|
-
at
|
|
203
|
+
at,
|
|
204
|
+
...named
|
|
165
205
|
};
|
|
166
206
|
}
|
|
167
207
|
/**
|
|
@@ -369,6 +409,8 @@ function shapeError(u) {
|
|
|
369
409
|
if (bad.axis) return "the expression has an axis that is not valid";
|
|
370
410
|
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`;
|
|
371
411
|
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";
|
|
412
|
+
const named = tag === "each" || tag === "fold" ? itemNamesError(bad.u) : void 0;
|
|
413
|
+
if (named !== void 0) return `the expression has a node "${String(tag)}" with names "as" that are not valid: ${named}`;
|
|
372
414
|
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";
|
|
373
415
|
}
|
|
374
416
|
/**
|
|
@@ -479,7 +521,7 @@ function badPart(todo, whole) {
|
|
|
479
521
|
break;
|
|
480
522
|
case "each": {
|
|
481
523
|
const at = addrParts(u["at"], moveNumber);
|
|
482
|
-
if (at === void 0) return item;
|
|
524
|
+
if (at === void 0 || itemNamesError(u) !== void 0) return item;
|
|
483
525
|
push(true, [u["axis"]]);
|
|
484
526
|
push(false, [u["body"], ...at]);
|
|
485
527
|
break;
|
|
@@ -487,7 +529,7 @@ function badPart(todo, whole) {
|
|
|
487
529
|
case "fold": {
|
|
488
530
|
const at = addrParts(u["at"], moveNumber);
|
|
489
531
|
const names = loopParts(u);
|
|
490
|
-
if (at === void 0 || names === void 0) return item;
|
|
532
|
+
if (at === void 0 || names === void 0 || itemNamesError(u) !== void 0) return item;
|
|
491
533
|
push(true, [u["axis"]]);
|
|
492
534
|
push(false, [...names, ...at]);
|
|
493
535
|
break;
|
|
@@ -1331,6 +1373,12 @@ const LOOP_SCOPE = [{
|
|
|
1331
1373
|
["result"]
|
|
1332
1374
|
]
|
|
1333
1375
|
}];
|
|
1376
|
+
/** The names of the items of a loop over the axis `list`: one or two distinct texts (AXIS.LIST.SCOPE). */
|
|
1377
|
+
const ITEM_NAMES = {
|
|
1378
|
+
shape: "list",
|
|
1379
|
+
item: { shape: "text" },
|
|
1380
|
+
distinct: true
|
|
1381
|
+
};
|
|
1334
1382
|
/**
|
|
1335
1383
|
* This function gives the world of Vex. The module uses it one time, and the annotation `__PURE__` lets a bundler
|
|
1336
1384
|
* remove the world from a program that does not use it.
|
|
@@ -1411,15 +1459,21 @@ function worldOfVex() {
|
|
|
1411
1459
|
verbs: { eval: "vex.each" },
|
|
1412
1460
|
parts: closed({
|
|
1413
1461
|
axis: AXIS,
|
|
1462
|
+
as: ITEM_NAMES,
|
|
1414
1463
|
body: TERM,
|
|
1415
1464
|
at: ADDRESS
|
|
1416
1465
|
}, ["axis", "body"]),
|
|
1466
|
+
scope: [{
|
|
1467
|
+
names: { listAt: ["as"] },
|
|
1468
|
+
in: [["body"]]
|
|
1469
|
+
}],
|
|
1417
1470
|
summary: "The body at each target of an axis: a list."
|
|
1418
1471
|
},
|
|
1419
1472
|
fold: {
|
|
1420
1473
|
verbs: { eval: "vex.fold" },
|
|
1421
1474
|
parts: closed({
|
|
1422
1475
|
axis: AXIS,
|
|
1476
|
+
as: ITEM_NAMES,
|
|
1423
1477
|
init: NAMES,
|
|
1424
1478
|
step: NAMES,
|
|
1425
1479
|
result: TERM,
|
|
@@ -1430,7 +1484,10 @@ function worldOfVex() {
|
|
|
1430
1484
|
"step",
|
|
1431
1485
|
"result"
|
|
1432
1486
|
]),
|
|
1433
|
-
scope: LOOP_SCOPE,
|
|
1487
|
+
scope: [...LOOP_SCOPE, {
|
|
1488
|
+
names: { listAt: ["as"] },
|
|
1489
|
+
in: [["step", "*"]]
|
|
1490
|
+
}],
|
|
1434
1491
|
summary: "A step at each target of an axis, in order, and a result."
|
|
1435
1492
|
},
|
|
1436
1493
|
iterate: {
|
|
@@ -1722,7 +1779,12 @@ const axisEvaluator = ({ typePath, value, context }) => {
|
|
|
1722
1779
|
if (!r.ok) return r;
|
|
1723
1780
|
const items = asList(r.value, missing);
|
|
1724
1781
|
if (items === void 0) return failed("kind-mismatch", `the axis "list" needs a list, not a ${describe(r.value)}`);
|
|
1725
|
-
|
|
1782
|
+
let index = 0;
|
|
1783
|
+
return ok$1(items.items.map(({ key, result }) => result.ok ? {
|
|
1784
|
+
key,
|
|
1785
|
+
value: result.value,
|
|
1786
|
+
index: index++
|
|
1787
|
+
} : {
|
|
1726
1788
|
key,
|
|
1727
1789
|
error: result.error
|
|
1728
1790
|
}));
|
|
@@ -1779,7 +1841,7 @@ function whereTargets(base, test, ctx, from) {
|
|
|
1779
1841
|
key: t.key,
|
|
1780
1842
|
error: toVexError(r.error)
|
|
1781
1843
|
});
|
|
1782
|
-
else if (r.value === true) out.push(
|
|
1844
|
+
else if (r.value === true) out.push(t);
|
|
1783
1845
|
else if (r.value !== false) out.push({
|
|
1784
1846
|
key: t.key,
|
|
1785
1847
|
error: vexError("kind-mismatch", `the test of "where" gave a ${typeof r.value}, not a boolean`)
|
|
@@ -1834,6 +1896,20 @@ function bindAll(record, ctx, names) {
|
|
|
1834
1896
|
for (const [name, v] of values) setName(out, name, v);
|
|
1835
1897
|
return ok$1(out);
|
|
1836
1898
|
}
|
|
1899
|
+
/**
|
|
1900
|
+
* This function adds the names `as` of a loop to the names of a body or of a step. The first name holds the value of
|
|
1901
|
+
* the item, and the second its index (AXIS.LIST.ITEM, AXIS.LIST.INDEX). A loop without `as` keeps its names.
|
|
1902
|
+
*/
|
|
1903
|
+
function withItem$1(names, as, t) {
|
|
1904
|
+
if (as === void 0) return names;
|
|
1905
|
+
const out = { ...names };
|
|
1906
|
+
const [item, index] = as;
|
|
1907
|
+
setName(out, item, t.value);
|
|
1908
|
+
if (index !== void 0) setName(out, index, t.index);
|
|
1909
|
+
return out;
|
|
1910
|
+
}
|
|
1911
|
+
/** This function gives the names `as` of a loop, or `undefined` (AXIS.LIST.SCOPE). */
|
|
1912
|
+
const itemNamesOf = (value) => value.as;
|
|
1837
1913
|
/** The evaluators of the loops: each, fold and iterate (LOOP.NAMES, FOLD.ORDER, ITERATE.LIMIT). */
|
|
1838
1914
|
const LOOPS = {
|
|
1839
1915
|
"vex.each": ({ value, context }) => {
|
|
@@ -1841,12 +1917,14 @@ const LOOPS = {
|
|
|
1841
1917
|
if (!found.ok) return found;
|
|
1842
1918
|
const { axis, from, targets } = found.value;
|
|
1843
1919
|
const body = partAt(value, "body");
|
|
1844
|
-
const
|
|
1845
|
-
|
|
1846
|
-
|
|
1920
|
+
const as = itemNamesOf(value);
|
|
1921
|
+
const items = targets.map((t) => ({
|
|
1922
|
+
key: t.key,
|
|
1923
|
+
result: t.error === void 0 ? toVexResult(body.evaluate({
|
|
1847
1924
|
...context,
|
|
1848
|
-
self: placeOf(axis, from, key)
|
|
1849
|
-
|
|
1925
|
+
self: placeOf(axis, from, t.key),
|
|
1926
|
+
names: withItem$1(context.names, as, t)
|
|
1927
|
+
})) : fail(t.error)
|
|
1850
1928
|
}));
|
|
1851
1929
|
return ok$1(vexList(items));
|
|
1852
1930
|
},
|
|
@@ -1858,15 +1936,16 @@ const LOOPS = {
|
|
|
1858
1936
|
const found = targetsOf(value, context);
|
|
1859
1937
|
if (!found.ok) return found;
|
|
1860
1938
|
const { axis, from, targets } = found.value;
|
|
1861
|
-
|
|
1862
|
-
|
|
1939
|
+
const as = itemNamesOf(value);
|
|
1940
|
+
for (const t of targets) {
|
|
1941
|
+
if (t.error !== void 0) return {
|
|
1863
1942
|
ok: false,
|
|
1864
|
-
error: toKernelError(error)
|
|
1943
|
+
error: toKernelError(t.error)
|
|
1865
1944
|
};
|
|
1866
1945
|
const next = bindAll(step, {
|
|
1867
1946
|
...context,
|
|
1868
|
-
self: placeOf(axis, from, key),
|
|
1869
|
-
names
|
|
1947
|
+
self: placeOf(axis, from, t.key),
|
|
1948
|
+
names: withItem$1(names, as, t)
|
|
1870
1949
|
}, names);
|
|
1871
1950
|
if (!next.ok) return next;
|
|
1872
1951
|
names = next.value;
|
|
@@ -2330,6 +2409,8 @@ const envelope = (e) => ({
|
|
|
2330
2409
|
t: "envelope",
|
|
2331
2410
|
e
|
|
2332
2411
|
});
|
|
2412
|
+
/** This function gives the field `as` of a loop: a new list of its names, or no field (AXIS.LIST.SCOPE). */
|
|
2413
|
+
const itemsOf = (as) => as === void 0 ? {} : { as: [...as] };
|
|
2333
2414
|
/** This function gives a record from names and the documents of their values, from an index of the documents. */
|
|
2334
2415
|
function namesOf(names, docs, from) {
|
|
2335
2416
|
const out = {};
|
|
@@ -2472,12 +2553,15 @@ var Writer = class {
|
|
|
2472
2553
|
const axis = docs[0];
|
|
2473
2554
|
const body = docs[1];
|
|
2474
2555
|
const address = at.make(docs.slice(2));
|
|
2556
|
+
const as = itemsOf(e.as);
|
|
2475
2557
|
return this.#keep(typed("each", reads ? {
|
|
2476
2558
|
body,
|
|
2477
2559
|
axis,
|
|
2560
|
+
...as,
|
|
2478
2561
|
...address
|
|
2479
2562
|
} : {
|
|
2480
2563
|
axis,
|
|
2564
|
+
...as,
|
|
2481
2565
|
body,
|
|
2482
2566
|
...address
|
|
2483
2567
|
}), e);
|
|
@@ -2508,13 +2592,16 @@ var Writer = class {
|
|
|
2508
2592
|
};
|
|
2509
2593
|
const result = docs[after];
|
|
2510
2594
|
const address = at.make(docs.slice(after + 1));
|
|
2595
|
+
const as = itemsOf(e.as);
|
|
2511
2596
|
return this.#keep(typed("fold", reads ? {
|
|
2512
2597
|
...names,
|
|
2513
2598
|
axis,
|
|
2599
|
+
...as,
|
|
2514
2600
|
...address,
|
|
2515
2601
|
result
|
|
2516
2602
|
} : {
|
|
2517
2603
|
axis,
|
|
2604
|
+
...as,
|
|
2518
2605
|
...names,
|
|
2519
2606
|
result,
|
|
2520
2607
|
...address
|
|
@@ -2656,18 +2743,20 @@ function writeDocument(e, opts) {
|
|
|
2656
2743
|
* | `fn` | `{ "type": "fn", "value": { "params", "body" } }` |
|
|
2657
2744
|
* | `call` | `{ "type": "call", "value": { "fn": { "type": "ref", "value": ["names", name] }, "args" } }` |
|
|
2658
2745
|
* | `rec` | `{ "type": "record", "value": { ...fields } }` |
|
|
2659
|
-
* | `each` | `{ "type": "each", "value": { "axis", "body", "at"? } }` |
|
|
2660
|
-
* | `fold` | `{ "type": "fold", "value": { "axis", "init", "step", "result", "at"? } }` |
|
|
2746
|
+
* | `each` | `{ "type": "each", "value": { "axis", "as"?, "body", "at"? } }` |
|
|
2747
|
+
* | `fold` | `{ "type": "fold", "value": { "axis", "as"?, "init", "step", "result", "at"? } }` |
|
|
2661
2748
|
* | `iterate` | `{ "type": "iterate", "value": { "limit", "init", "step", "until"?, "result" } }` |
|
|
2662
2749
|
* | `ext` | `{ "type": ["ext", ...keys], "value": data }` |
|
|
2663
2750
|
* | a move | `{ "type": ["moves", kind], "value": v }` |
|
|
2664
2751
|
* | an axis | `{ "type": ["axes", name], "value": { ...parameters } }` |
|
|
2665
2752
|
*
|
|
2666
2753
|
* The address `at` of `each` and `fold` is a reference from `self` with moves only. An empty address is no address.
|
|
2754
|
+
* The field `as` of `each` and `fold` names the item of a loop over the axis `list`, and then its index (AXIS.LIST.SCOPE).
|
|
2667
2755
|
*
|
|
2668
2756
|
* The kernel checks a document by the contracts of `VEX_WORLD` (DOC.CHECK). A contract gives the shape of a kind, the
|
|
2669
2757
|
* data of its fields and the kinds of each position. Then `fromDocument` reads the expression. It checks only the grammar
|
|
2670
|
-
* of Vex: the steps of a reference, the address, the function of a call and the names of a loop.
|
|
2758
|
+
* of Vex: the steps of a reference, the address, the function of a call and the names of a loop. It also checks the
|
|
2759
|
+
* names of the items.
|
|
2671
2760
|
*/
|
|
2672
2761
|
/** The stop of a reading. The functions of this module throw it, and the top of the reading gives its error. */
|
|
2673
2762
|
var Stop = class {
|
|
@@ -2775,10 +2864,16 @@ var Reader = class {
|
|
|
2775
2864
|
return call(name, ...f["args"].map((x, i) => this.term(x, into(into(at, "args"), i))));
|
|
2776
2865
|
}
|
|
2777
2866
|
case "record": return rec(this.#names(u.value, at));
|
|
2778
|
-
case "each":
|
|
2867
|
+
case "each": {
|
|
2868
|
+
const axis = this.#axis(f["axis"], into(at, "axis"));
|
|
2869
|
+
const as = this.#items(f, axis, at);
|
|
2870
|
+
return each(axis, this.term(f["body"], into(at, "body")), this.#address(f["at"], into(at, "at")), as);
|
|
2871
|
+
}
|
|
2779
2872
|
case "fold": {
|
|
2780
2873
|
const [init, step] = this.#loop(f, at);
|
|
2781
|
-
|
|
2874
|
+
const axis = this.#axis(f["axis"], into(at, "axis"));
|
|
2875
|
+
const as = this.#items(f, axis, at);
|
|
2876
|
+
return fold(axis, init, step, this.term(f["result"], into(at, "result")), this.#address(f["at"], into(at, "at")), as);
|
|
2782
2877
|
}
|
|
2783
2878
|
default: {
|
|
2784
2879
|
const [init, step] = this.#loop(f, at);
|
|
@@ -2822,6 +2917,18 @@ var Reader = class {
|
|
|
2822
2917
|
const extra = Object.keys(step).find((name) => !Object.hasOwn(init, name));
|
|
2823
2918
|
return extra === void 0 ? [init, step] : bad(into(into(at, "step"), extra), `the name "${extra}" of step is not a name of init`);
|
|
2824
2919
|
}
|
|
2920
|
+
/**
|
|
2921
|
+
* This method reads the names `as` of a loop (AXIS.LIST.SCOPE). The contract gives a list of distinct texts. The
|
|
2922
|
+
* grammar of Vex needs one or two names, the axis `list` and no name of `init`.
|
|
2923
|
+
*/
|
|
2924
|
+
#items(f, axis, at) {
|
|
2925
|
+
const reason = itemNamesError({
|
|
2926
|
+
as: f["as"],
|
|
2927
|
+
axis,
|
|
2928
|
+
init: f["init"]
|
|
2929
|
+
});
|
|
2930
|
+
return reason === void 0 ? f["as"] : bad(into(at, "as"), reason);
|
|
2931
|
+
}
|
|
2825
2932
|
/** This method reads a reference: the fields of `self` after its moves, or a name of `names` (DOC.REF). */
|
|
2826
2933
|
#ref(path, where) {
|
|
2827
2934
|
const [root, ...steps] = path;
|
|
@@ -3013,6 +3120,18 @@ function explain(expr, opts) {
|
|
|
3013
3120
|
}
|
|
3014
3121
|
/** This function gives the axis inside the `where` axes of an axis. */
|
|
3015
3122
|
const baseAxis = (a) => a.t === "where" ? baseAxis(a.axis) : a;
|
|
3123
|
+
/**
|
|
3124
|
+
* This function adds the names `as` of a loop to the names of a body or of a step. The first name holds the value of
|
|
3125
|
+
* the item, and the second its index (AXIS.LIST.ITEM, AXIS.LIST.INDEX). A loop without `as` keeps its names.
|
|
3126
|
+
*/
|
|
3127
|
+
function withItem(env, as, t) {
|
|
3128
|
+
if (as === void 0) return env;
|
|
3129
|
+
const out = new Map(env);
|
|
3130
|
+
const [item, index] = as;
|
|
3131
|
+
out.set(item, ok(t.value));
|
|
3132
|
+
if (index !== void 0) out.set(index, ok(t.index));
|
|
3133
|
+
return out;
|
|
3134
|
+
}
|
|
3016
3135
|
var Compiler = class {
|
|
3017
3136
|
#domains;
|
|
3018
3137
|
#fns;
|
|
@@ -3296,7 +3415,8 @@ var Compiler = class {
|
|
|
3296
3415
|
}
|
|
3297
3416
|
const ts = targets(from, inner);
|
|
3298
3417
|
if (!ts.ok) return this.#emit(c, info, f, ts);
|
|
3299
|
-
const items = ts.value.map((
|
|
3418
|
+
const items = ts.value.map((t) => {
|
|
3419
|
+
const { key, test } = t;
|
|
3300
3420
|
if (test !== void 0 && !test.ok) return {
|
|
3301
3421
|
key,
|
|
3302
3422
|
result: test
|
|
@@ -3308,7 +3428,7 @@ var Compiler = class {
|
|
|
3308
3428
|
origin: base.t === "table" ? key : f.pos.origin,
|
|
3309
3429
|
focus: key
|
|
3310
3430
|
},
|
|
3311
|
-
env: f.env
|
|
3431
|
+
env: withItem(f.env, e.as, t)
|
|
3312
3432
|
}, inner)
|
|
3313
3433
|
};
|
|
3314
3434
|
});
|
|
@@ -3332,14 +3452,14 @@ var Compiler = class {
|
|
|
3332
3452
|
}
|
|
3333
3453
|
return ok(env);
|
|
3334
3454
|
},
|
|
3335
|
-
next: (f, c) => {
|
|
3455
|
+
next: (f, c, names = f.env) => {
|
|
3336
3456
|
const values = [];
|
|
3337
3457
|
for (const [name, n] of steps) {
|
|
3338
3458
|
const r = n(f, c);
|
|
3339
3459
|
if (!r.ok) return r;
|
|
3340
3460
|
values.push([name, r]);
|
|
3341
3461
|
}
|
|
3342
|
-
const env = new Map(
|
|
3462
|
+
const env = new Map(names);
|
|
3343
3463
|
for (const [name, r] of values) env.set(name, r);
|
|
3344
3464
|
return ok(env);
|
|
3345
3465
|
}
|
|
@@ -3385,15 +3505,16 @@ var Compiler = class {
|
|
|
3385
3505
|
}
|
|
3386
3506
|
const ts = targets(from, inner);
|
|
3387
3507
|
if (!ts.ok) return this.#emit(c, info, f, ts);
|
|
3388
|
-
for (const
|
|
3508
|
+
for (const t of ts.value) {
|
|
3509
|
+
const { key, test } = t;
|
|
3389
3510
|
if (test !== void 0 && !test.ok) return this.#emit(c, info, f, test);
|
|
3390
3511
|
const after = names.next({
|
|
3391
3512
|
pos: {
|
|
3392
3513
|
origin: base.t === "table" ? key : f.pos.origin,
|
|
3393
3514
|
focus: key
|
|
3394
3515
|
},
|
|
3395
|
-
env
|
|
3396
|
-
}, inner);
|
|
3516
|
+
env: withItem(env, e.as, t)
|
|
3517
|
+
}, inner, env);
|
|
3397
3518
|
if (!after.ok) return this.#emit(c, info, f, after);
|
|
3398
3519
|
env = after.value;
|
|
3399
3520
|
}
|
|
@@ -3481,7 +3602,7 @@ var Compiler = class {
|
|
|
3481
3602
|
key: t.key,
|
|
3482
3603
|
test: r
|
|
3483
3604
|
});
|
|
3484
|
-
else if (r.value === true) out.push(
|
|
3605
|
+
else if (r.value === true) out.push(t);
|
|
3485
3606
|
else if (r.value !== false) out.push({
|
|
3486
3607
|
key: t.key,
|
|
3487
3608
|
test: this.#err("kind-mismatch", `the test of "where" gave a ${typeof r.value}, not a boolean`, at, testPath)
|
|
@@ -3501,7 +3622,12 @@ var Compiler = class {
|
|
|
3501
3622
|
if (!r.ok) return r;
|
|
3502
3623
|
const listed = asList(r.value, this.#missing(f, path));
|
|
3503
3624
|
if (listed === void 0) return this.#err("kind-mismatch", `the axis "list" needs a list, not a ${describeType(r.value)}`, f, path);
|
|
3504
|
-
|
|
3625
|
+
let index = 0;
|
|
3626
|
+
const items = listed.items.map(({ key, result }) => result.ok ? {
|
|
3627
|
+
key,
|
|
3628
|
+
value: result.value,
|
|
3629
|
+
index: index++
|
|
3630
|
+
} : {
|
|
3505
3631
|
key,
|
|
3506
3632
|
test: result
|
|
3507
3633
|
});
|
|
@@ -3992,7 +4118,7 @@ function deps(e) {
|
|
|
3992
4118
|
if (!rule.in.some((pattern) => pathMatches(child.slot, pattern))) continue;
|
|
3993
4119
|
const source = rule.names;
|
|
3994
4120
|
const at = (source.keysAt ?? source.listAt ?? []).reduce((x, k) => x[String(k)], around.doc.value);
|
|
3995
|
-
if (!(source.keysAt !== void 0 ? Object.keys(at) : at).includes(name)) continue;
|
|
4121
|
+
if (!(source.keysAt !== void 0 ? Object.keys(at) : at ?? []).includes(name)) continue;
|
|
3996
4122
|
found = fnVisits.get(at[name]);
|
|
3997
4123
|
break search;
|
|
3998
4124
|
}
|
|
@@ -4979,6 +5105,37 @@ function stepExprs(step, unique, addr) {
|
|
|
4979
5105
|
}
|
|
4980
5106
|
return out;
|
|
4981
5107
|
}
|
|
5108
|
+
/**
|
|
5109
|
+
* The number of the open bodies of `each` and steps of `fold` of a list chain. The names `as` of a loop end with this
|
|
5110
|
+
* number, so a loop inside a body has other names than the loop around it (BUILD.LIST.ITEM).
|
|
5111
|
+
*/
|
|
5112
|
+
let itemDepth = 0;
|
|
5113
|
+
/** This function gives the value of `build`, with the next number for the names `as` inside it. */
|
|
5114
|
+
function inItemLoop(build) {
|
|
5115
|
+
itemDepth += 1;
|
|
5116
|
+
try {
|
|
5117
|
+
return build();
|
|
5118
|
+
} finally {
|
|
5119
|
+
itemDepth -= 1;
|
|
5120
|
+
}
|
|
5121
|
+
}
|
|
5122
|
+
/** This function tells if the expression reads the name, in one of its parts. It uses a stack, not recursion. */
|
|
5123
|
+
function reads(e, name) {
|
|
5124
|
+
const todo = [e];
|
|
5125
|
+
for (let x = todo.pop(); x !== void 0; x = todo.pop()) {
|
|
5126
|
+
if (x.tag === "var" && x.name === name) return true;
|
|
5127
|
+
todo.push(...children(x));
|
|
5128
|
+
}
|
|
5129
|
+
return false;
|
|
5130
|
+
}
|
|
5131
|
+
/**
|
|
5132
|
+
* This function gives the names `as` of a fold whose step reads the index. The axis must be `list`. The name of the item
|
|
5133
|
+
* of the loop of `each` stays, so the item of the step keeps its value (BUILD.LIST.ITEM).
|
|
5134
|
+
*/
|
|
5135
|
+
function foldItemNames(loop, index) {
|
|
5136
|
+
if (listOf(loop.axis) === void 0) throw new TypeError("the index of a fold needs the axis list: go over the items of a list with each first");
|
|
5137
|
+
return [loop.as?.[0] ?? `$item${itemDepth}`, index];
|
|
5138
|
+
}
|
|
4982
5139
|
const PROXY_RESERVED = /* @__PURE__ */ new Set([
|
|
4983
5140
|
"then",
|
|
4984
5141
|
"catch",
|
|
@@ -5290,9 +5447,15 @@ var ListChainImpl = class ListChainImpl {
|
|
|
5290
5447
|
expr: loop.body,
|
|
5291
5448
|
addr: []
|
|
5292
5449
|
});
|
|
5293
|
-
const
|
|
5450
|
+
const index = loop.as?.[1] ?? `$index${itemDepth}`;
|
|
5451
|
+
const next = stepExprs(inItemLoop(() => step(names.vars, item, new ChainImpl({
|
|
5452
|
+
...this.#s,
|
|
5453
|
+
expr: v(index),
|
|
5454
|
+
addr: []
|
|
5455
|
+
}))), names.unique, []);
|
|
5456
|
+
const as = Object.values(next).some((x) => reads(x, index)) ? foldItemNames(loop, index) : loop.as;
|
|
5294
5457
|
const r = result(names.vars);
|
|
5295
|
-
const folded = fold(loop.axis, names.first, next, toExpr(r, this.#s.addr), loop.at);
|
|
5458
|
+
const folded = fold(loop.axis, names.first, next, toExpr(r, this.#s.addr), loop.at, as);
|
|
5296
5459
|
return chainOf({
|
|
5297
5460
|
...this.#s,
|
|
5298
5461
|
expr: e.tag === "let" ? let_(e.bind, folded) : folded
|
|
@@ -5379,11 +5542,14 @@ var ListChainImpl = class ListChainImpl {
|
|
|
5379
5542
|
return this.#list("flat", [], opts);
|
|
5380
5543
|
}
|
|
5381
5544
|
each(body, opts) {
|
|
5382
|
-
const
|
|
5545
|
+
const inner = {
|
|
5383
5546
|
...this.#s,
|
|
5384
|
-
expr: lit(null),
|
|
5385
5547
|
addr: [],
|
|
5386
5548
|
depth: this.#s.depth + 1
|
|
5549
|
+
};
|
|
5550
|
+
const start = new ChainImpl({
|
|
5551
|
+
...inner,
|
|
5552
|
+
expr: lit(null)
|
|
5387
5553
|
});
|
|
5388
5554
|
const list = {
|
|
5389
5555
|
t: "list",
|
|
@@ -5398,9 +5564,18 @@ var ListChainImpl = class ListChainImpl {
|
|
|
5398
5564
|
axis: ordered,
|
|
5399
5565
|
test: opts.where(start).program
|
|
5400
5566
|
};
|
|
5567
|
+
const names = [`$item${itemDepth}`, `$index${itemDepth}`];
|
|
5568
|
+
const program = inItemLoop(() => body(start, new ChainImpl({
|
|
5569
|
+
...inner,
|
|
5570
|
+
expr: v(names[0])
|
|
5571
|
+
}), new ChainImpl({
|
|
5572
|
+
...inner,
|
|
5573
|
+
expr: v(names[1])
|
|
5574
|
+
})).program);
|
|
5575
|
+
const as = reads(program, names[1]) ? names : reads(program, names[0]) ? [names[0]] : void 0;
|
|
5401
5576
|
return new ListChainImpl({
|
|
5402
5577
|
...this.#s,
|
|
5403
|
-
expr: each(axis,
|
|
5578
|
+
expr: each(axis, program, void 0, as)
|
|
5404
5579
|
});
|
|
5405
5580
|
}
|
|
5406
5581
|
result(k) {
|