@mark1russell7/vex 0.13.3 → 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 -47
- package/build/index.d.ts.map +1 -1
- package/build/index.js +292 -94
- 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 +86 -24
- package/src/ir.ts +25 -10
- package/src/item-names.ts +29 -0
- package/src/kernel.ts +67 -13
- package/src/write.ts +7 -2
package/build/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Failure, Held, STD_EXT, THING, agree, check, context, formulaDefinitions, formulaEvaluators, isEnvelope, ok as ok$1, pathMatches, prepare, ref as ref$1, reference, refsOf, sameValue, typeName, typePath, typed } from "@mark1russell7/coeval";
|
|
2
|
-
|
|
2
|
+
// src/result.ts
|
|
3
3
|
const NONE$1 = Object.freeze({ tag: "none" });
|
|
4
4
|
/** This function makes an `Optional` that holds `value`. */
|
|
5
5
|
const some = (value) => ({
|
|
@@ -30,8 +30,7 @@ const getOr = (o, fallback) => o.tag === "some" ? o.value : fallback;
|
|
|
30
30
|
const mapResult = (r, f) => r.ok ? ok(f(r.value)) : r;
|
|
31
31
|
/** This function applies `f` to the value of a `Result`, and `f` gives a `Result`. An error stays the same. */
|
|
32
32
|
const chainResult = (r, f) => r.ok ? f(r.value) : r;
|
|
33
|
-
|
|
34
|
-
//#region src/errors.ts
|
|
33
|
+
// src/errors.ts
|
|
35
34
|
const CODE_OF = {
|
|
36
35
|
"unknown-key": "#REF!",
|
|
37
36
|
"not-a-pair": "#REF!",
|
|
@@ -85,8 +84,38 @@ function vexError(kind, message, details = {}) {
|
|
|
85
84
|
}
|
|
86
85
|
/** This function gives a short text for an error, for example `#REF! unknown-key: there is no key "Z"`. */
|
|
87
86
|
const formatError = (e) => `${e.code} ${e.kind}: ${e.message}`;
|
|
88
|
-
|
|
89
|
-
|
|
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
|
+
}
|
|
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
|
+
*/
|
|
90
119
|
/** The largest number of steps of `iterate` (ITERATE.LIMIT). */
|
|
91
120
|
const MAX_ITERATIONS = 1e5;
|
|
92
121
|
/** This function makes a literal. */
|
|
@@ -132,38 +161,47 @@ const rec = (fields) => ({
|
|
|
132
161
|
});
|
|
133
162
|
/**
|
|
134
163
|
* This function evaluates `body` once for each target of `axis`. The result is a list. With `at`, the axis finds its
|
|
135
|
-
* 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).
|
|
136
166
|
*/
|
|
137
|
-
function each(axis, body, at) {
|
|
167
|
+
function each(axis, body, at, as) {
|
|
168
|
+
const named = as === void 0 ? {} : { as };
|
|
138
169
|
return at === void 0 || at.length === 0 ? {
|
|
139
170
|
tag: "each",
|
|
140
171
|
axis,
|
|
141
|
-
body
|
|
172
|
+
body,
|
|
173
|
+
...named
|
|
142
174
|
} : {
|
|
143
175
|
tag: "each",
|
|
144
176
|
axis,
|
|
145
177
|
body,
|
|
146
|
-
at
|
|
178
|
+
at,
|
|
179
|
+
...named
|
|
147
180
|
};
|
|
148
181
|
}
|
|
149
182
|
/**
|
|
150
183
|
* This function makes a fold: a step at each target of `axis`, in order. `init` gives the names and their first
|
|
151
|
-
* 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).
|
|
152
187
|
*/
|
|
153
|
-
function fold(axis, init, step, result, at) {
|
|
188
|
+
function fold(axis, init, step, result, at, as) {
|
|
189
|
+
const named = as === void 0 ? {} : { as };
|
|
154
190
|
return at === void 0 || at.length === 0 ? {
|
|
155
191
|
tag: "fold",
|
|
156
192
|
axis,
|
|
157
193
|
init,
|
|
158
194
|
step,
|
|
159
|
-
result
|
|
195
|
+
result,
|
|
196
|
+
...named
|
|
160
197
|
} : {
|
|
161
198
|
tag: "fold",
|
|
162
199
|
axis,
|
|
163
200
|
init,
|
|
164
201
|
step,
|
|
165
202
|
result,
|
|
166
|
-
at
|
|
203
|
+
at,
|
|
204
|
+
...named
|
|
167
205
|
};
|
|
168
206
|
}
|
|
169
207
|
/**
|
|
@@ -371,6 +409,8 @@ function shapeError(u) {
|
|
|
371
409
|
if (bad.axis) return "the expression has an axis that is not valid";
|
|
372
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`;
|
|
373
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}`;
|
|
374
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";
|
|
375
415
|
}
|
|
376
416
|
/**
|
|
@@ -481,7 +521,7 @@ function badPart(todo, whole) {
|
|
|
481
521
|
break;
|
|
482
522
|
case "each": {
|
|
483
523
|
const at = addrParts(u["at"], moveNumber);
|
|
484
|
-
if (at === void 0) return item;
|
|
524
|
+
if (at === void 0 || itemNamesError(u) !== void 0) return item;
|
|
485
525
|
push(true, [u["axis"]]);
|
|
486
526
|
push(false, [u["body"], ...at]);
|
|
487
527
|
break;
|
|
@@ -489,7 +529,7 @@ function badPart(todo, whole) {
|
|
|
489
529
|
case "fold": {
|
|
490
530
|
const at = addrParts(u["at"], moveNumber);
|
|
491
531
|
const names = loopParts(u);
|
|
492
|
-
if (at === void 0 || names === void 0) return item;
|
|
532
|
+
if (at === void 0 || names === void 0 || itemNamesError(u) !== void 0) return item;
|
|
493
533
|
push(true, [u["axis"]]);
|
|
494
534
|
push(false, [...names, ...at]);
|
|
495
535
|
break;
|
|
@@ -560,8 +600,7 @@ function listOf(a) {
|
|
|
560
600
|
const base = x.t === "reverse" ? x.axis : x;
|
|
561
601
|
return base.t === "list" ? base.list : void 0;
|
|
562
602
|
}
|
|
563
|
-
|
|
564
|
-
//#region src/space.ts
|
|
603
|
+
// src/space.ts
|
|
565
604
|
var RecordSpace = class {
|
|
566
605
|
kind = "record";
|
|
567
606
|
keys;
|
|
@@ -965,8 +1004,7 @@ function windowTargets(s, at, from, to) {
|
|
|
965
1004
|
}
|
|
966
1005
|
return ok(out);
|
|
967
1006
|
}
|
|
968
|
-
|
|
969
|
-
//#region src/domain.ts
|
|
1007
|
+
// src/domain.ts
|
|
970
1008
|
/** This function makes a domain. The result is frozen. */
|
|
971
1009
|
function defineDomain(spec) {
|
|
972
1010
|
return Object.freeze({
|
|
@@ -1027,8 +1065,7 @@ function domainOf(domains, value) {
|
|
|
1027
1065
|
}
|
|
1028
1066
|
/** This function tells if the `liftScalar` setting lifts the argument at `index`. */
|
|
1029
1067
|
const liftsAt = (lift, index) => lift === true || Array.isArray(lift) && lift[index] === true;
|
|
1030
|
-
|
|
1031
|
-
//#region src/list.ts
|
|
1068
|
+
// src/list.ts
|
|
1032
1069
|
/** This function makes a list. */
|
|
1033
1070
|
const vexList = (items) => Object.freeze({
|
|
1034
1071
|
kind: "vex.list",
|
|
@@ -1094,8 +1131,7 @@ const LIST_OPS = /* @__PURE__ */ new Set([
|
|
|
1094
1131
|
]);
|
|
1095
1132
|
/** This function tells if a name is the name of a core list op. */
|
|
1096
1133
|
const isListOp = (op) => LIST_OPS.has(op);
|
|
1097
|
-
|
|
1098
|
-
//#region src/codec.ts
|
|
1134
|
+
// src/codec.ts
|
|
1099
1135
|
/** This function tells if a value is a record: an object that is not an array. */
|
|
1100
1136
|
const isRecord = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
1101
1137
|
const isPlainRecord = (u) => {
|
|
@@ -1184,8 +1220,7 @@ function decodeValue(u, domains) {
|
|
|
1184
1220
|
}
|
|
1185
1221
|
return fields(u);
|
|
1186
1222
|
}
|
|
1187
|
-
|
|
1188
|
-
//#region src/array-list.ts
|
|
1223
|
+
// src/array-list.ts
|
|
1189
1224
|
/**
|
|
1190
1225
|
* This function gives the list of an array (LIST.ARRAY). The key of each item is its position, from "0". A position
|
|
1191
1226
|
* without a value, or with `null`, gives an error item from `missing`, as a missing field does (REF.MISSING).
|
|
@@ -1215,8 +1250,7 @@ const fillList = (n, value) => vexList(Array.from({ length: n }, (_x, i) => ({
|
|
|
1215
1250
|
})));
|
|
1216
1251
|
/** This function tells if a value can be the value of `indexOf`: a number, a text or a boolean (LIST.INDEXOF). */
|
|
1217
1252
|
const isIndexValue = (u) => typeof u === "number" || typeof u === "string" || typeof u === "boolean";
|
|
1218
|
-
|
|
1219
|
-
//#region src/same.ts
|
|
1253
|
+
// src/same.ts
|
|
1220
1254
|
/**
|
|
1221
1255
|
* The comparison of the values of Vex for `agree` of the kernel (EVAL.AGREE), and the test of a Vex error. The package
|
|
1222
1256
|
* index does not export this module: `kernel.ts` and `plan.ts` use it.
|
|
@@ -1243,8 +1277,7 @@ function sameVex(a, b) {
|
|
|
1243
1277
|
if (!isInstance(a) || !isInstance(b)) return void 0;
|
|
1244
1278
|
return Object.getPrototypeOf(a) === Object.getPrototypeOf(b) && sameValue({ ...a }, { ...b }, sameVex);
|
|
1245
1279
|
}
|
|
1246
|
-
|
|
1247
|
-
//#region src/kernel.ts
|
|
1280
|
+
// src/kernel.ts
|
|
1248
1281
|
/**
|
|
1249
1282
|
* Vex in the kernel of coeval (decision D13 of coeval, and decision D13 of Vex in `docs/REVIEW.md`). This module gives
|
|
1250
1283
|
* the contracts of the kinds of Vex as data, and a machine of the kernel that evaluates the documents of Vex.
|
|
@@ -1340,6 +1373,12 @@ const LOOP_SCOPE = [{
|
|
|
1340
1373
|
["result"]
|
|
1341
1374
|
]
|
|
1342
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
|
+
};
|
|
1343
1382
|
/**
|
|
1344
1383
|
* This function gives the world of Vex. The module uses it one time, and the annotation `__PURE__` lets a bundler
|
|
1345
1384
|
* remove the world from a program that does not use it.
|
|
@@ -1420,15 +1459,21 @@ function worldOfVex() {
|
|
|
1420
1459
|
verbs: { eval: "vex.each" },
|
|
1421
1460
|
parts: closed({
|
|
1422
1461
|
axis: AXIS,
|
|
1462
|
+
as: ITEM_NAMES,
|
|
1423
1463
|
body: TERM,
|
|
1424
1464
|
at: ADDRESS
|
|
1425
1465
|
}, ["axis", "body"]),
|
|
1466
|
+
scope: [{
|
|
1467
|
+
names: { listAt: ["as"] },
|
|
1468
|
+
in: [["body"]]
|
|
1469
|
+
}],
|
|
1426
1470
|
summary: "The body at each target of an axis: a list."
|
|
1427
1471
|
},
|
|
1428
1472
|
fold: {
|
|
1429
1473
|
verbs: { eval: "vex.fold" },
|
|
1430
1474
|
parts: closed({
|
|
1431
1475
|
axis: AXIS,
|
|
1476
|
+
as: ITEM_NAMES,
|
|
1432
1477
|
init: NAMES,
|
|
1433
1478
|
step: NAMES,
|
|
1434
1479
|
result: TERM,
|
|
@@ -1439,7 +1484,10 @@ function worldOfVex() {
|
|
|
1439
1484
|
"step",
|
|
1440
1485
|
"result"
|
|
1441
1486
|
]),
|
|
1442
|
-
scope: LOOP_SCOPE,
|
|
1487
|
+
scope: [...LOOP_SCOPE, {
|
|
1488
|
+
names: { listAt: ["as"] },
|
|
1489
|
+
in: [["step", "*"]]
|
|
1490
|
+
}],
|
|
1443
1491
|
summary: "A step at each target of an axis, in order, and a result."
|
|
1444
1492
|
},
|
|
1445
1493
|
iterate: {
|
|
@@ -1731,7 +1779,12 @@ const axisEvaluator = ({ typePath, value, context }) => {
|
|
|
1731
1779
|
if (!r.ok) return r;
|
|
1732
1780
|
const items = asList(r.value, missing);
|
|
1733
1781
|
if (items === void 0) return failed("kind-mismatch", `the axis "list" needs a list, not a ${describe(r.value)}`);
|
|
1734
|
-
|
|
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
|
+
} : {
|
|
1735
1788
|
key,
|
|
1736
1789
|
error: result.error
|
|
1737
1790
|
}));
|
|
@@ -1788,7 +1841,7 @@ function whereTargets(base, test, ctx, from) {
|
|
|
1788
1841
|
key: t.key,
|
|
1789
1842
|
error: toVexError(r.error)
|
|
1790
1843
|
});
|
|
1791
|
-
else if (r.value === true) out.push(
|
|
1844
|
+
else if (r.value === true) out.push(t);
|
|
1792
1845
|
else if (r.value !== false) out.push({
|
|
1793
1846
|
key: t.key,
|
|
1794
1847
|
error: vexError("kind-mismatch", `the test of "where" gave a ${typeof r.value}, not a boolean`)
|
|
@@ -1843,6 +1896,20 @@ function bindAll(record, ctx, names) {
|
|
|
1843
1896
|
for (const [name, v] of values) setName(out, name, v);
|
|
1844
1897
|
return ok$1(out);
|
|
1845
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;
|
|
1846
1913
|
/** The evaluators of the loops: each, fold and iterate (LOOP.NAMES, FOLD.ORDER, ITERATE.LIMIT). */
|
|
1847
1914
|
const LOOPS = {
|
|
1848
1915
|
"vex.each": ({ value, context }) => {
|
|
@@ -1850,12 +1917,14 @@ const LOOPS = {
|
|
|
1850
1917
|
if (!found.ok) return found;
|
|
1851
1918
|
const { axis, from, targets } = found.value;
|
|
1852
1919
|
const body = partAt(value, "body");
|
|
1853
|
-
const
|
|
1854
|
-
|
|
1855
|
-
|
|
1920
|
+
const as = itemNamesOf(value);
|
|
1921
|
+
const items = targets.map((t) => ({
|
|
1922
|
+
key: t.key,
|
|
1923
|
+
result: t.error === void 0 ? toVexResult(body.evaluate({
|
|
1856
1924
|
...context,
|
|
1857
|
-
self: placeOf(axis, from, key)
|
|
1858
|
-
|
|
1925
|
+
self: placeOf(axis, from, t.key),
|
|
1926
|
+
names: withItem$1(context.names, as, t)
|
|
1927
|
+
})) : fail(t.error)
|
|
1859
1928
|
}));
|
|
1860
1929
|
return ok$1(vexList(items));
|
|
1861
1930
|
},
|
|
@@ -1867,15 +1936,16 @@ const LOOPS = {
|
|
|
1867
1936
|
const found = targetsOf(value, context);
|
|
1868
1937
|
if (!found.ok) return found;
|
|
1869
1938
|
const { axis, from, targets } = found.value;
|
|
1870
|
-
|
|
1871
|
-
|
|
1939
|
+
const as = itemNamesOf(value);
|
|
1940
|
+
for (const t of targets) {
|
|
1941
|
+
if (t.error !== void 0) return {
|
|
1872
1942
|
ok: false,
|
|
1873
|
-
error: toKernelError(error)
|
|
1943
|
+
error: toKernelError(t.error)
|
|
1874
1944
|
};
|
|
1875
1945
|
const next = bindAll(step, {
|
|
1876
1946
|
...context,
|
|
1877
|
-
self: placeOf(axis, from, key),
|
|
1878
|
-
names
|
|
1947
|
+
self: placeOf(axis, from, t.key),
|
|
1948
|
+
names: withItem$1(names, as, t)
|
|
1879
1949
|
}, names);
|
|
1880
1950
|
if (!next.ok) return next;
|
|
1881
1951
|
names = next.value;
|
|
@@ -2062,12 +2132,13 @@ function listOp(op, items, rest, domains) {
|
|
|
2062
2132
|
case "errors": return Object.freeze(items.flatMap((it) => it.result.ok ? [] : [it.result.error]));
|
|
2063
2133
|
case "first": return vs.length > 0 ? vs[0] : empty();
|
|
2064
2134
|
case "sortBy": {
|
|
2065
|
-
const
|
|
2066
|
-
|
|
2135
|
+
const ks = sortValues(o, vs, domains);
|
|
2136
|
+
const kind = typeof ks[0];
|
|
2137
|
+
if (ks.some((x) => typeof x !== kind || kind !== "number" && kind !== "string" || Number.isNaN(x))) stop("kind-mismatch", "\"sortBy\" needs numbers without NaN or texts, all of one kind");
|
|
2067
2138
|
return vexList(good.map((it, i) => ({
|
|
2068
2139
|
it,
|
|
2069
2140
|
i
|
|
2070
|
-
})).toSorted((a, b) => compare(
|
|
2141
|
+
})).toSorted((a, b) => compare(ks[a.i], ks[b.i])).map(({ it }) => it));
|
|
2071
2142
|
}
|
|
2072
2143
|
case "take":
|
|
2073
2144
|
case "drop":
|
|
@@ -2112,6 +2183,19 @@ function listOp(op, items, rest, domains) {
|
|
|
2112
2183
|
default: return reduce(rest[0], vs, o, domains);
|
|
2113
2184
|
}
|
|
2114
2185
|
}
|
|
2186
|
+
/**
|
|
2187
|
+
* This function gives the sort values of `sortBy` (LIST.SORT.BY). Without the option `by`, they are the values. With
|
|
2188
|
+
* it, the op of that name applies to each value, in order, and the first error stops the sort.
|
|
2189
|
+
*/
|
|
2190
|
+
function sortValues(o, vs, domains) {
|
|
2191
|
+
const by = o?.by;
|
|
2192
|
+
if (by === void 0) return vs;
|
|
2193
|
+
if (typeof by !== "string") return stop("kind-mismatch", "\"sortBy\" needs an op name in the option \"by\"");
|
|
2194
|
+
return vs.map((x) => {
|
|
2195
|
+
const found = domains.filter((d) => accepts(d, x)).map((d) => opOf(d, x, by)).find((f) => f !== void 0);
|
|
2196
|
+
return found === void 0 ? stop("unknown-op", `no domain gives "${by}" for a ${describe(x)}`) : callFound(found, by, x, [], domains);
|
|
2197
|
+
});
|
|
2198
|
+
}
|
|
2115
2199
|
/** This function compares two numbers or two texts for `sortBy`. Equal values keep their order (LIST.SORT). */
|
|
2116
2200
|
const compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
2117
2201
|
/** This function folds the values of a list with a domain op, from the left (LIST.REDUCE, LIST.REDUCE.EMPTY). */
|
|
@@ -2303,8 +2387,7 @@ function vexChecker() {
|
|
|
2303
2387
|
function vexDefinitions(opts = {}) {
|
|
2304
2388
|
return formulaDefinitions(profileOf(opts));
|
|
2305
2389
|
}
|
|
2306
|
-
|
|
2307
|
-
//#region src/write.ts
|
|
2390
|
+
// src/write.ts
|
|
2308
2391
|
/**
|
|
2309
2392
|
* The writer of the documents of Vex: the coeval document of an expression (DOC.TERM). It uses a stack, not recursion,
|
|
2310
2393
|
* so an expression of any depth has a document. The package index does not export this module: `toDocument`, `deps`
|
|
@@ -2326,6 +2409,8 @@ const envelope = (e) => ({
|
|
|
2326
2409
|
t: "envelope",
|
|
2327
2410
|
e
|
|
2328
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] };
|
|
2329
2414
|
/** This function gives a record from names and the documents of their values, from an index of the documents. */
|
|
2330
2415
|
function namesOf(names, docs, from) {
|
|
2331
2416
|
const out = {};
|
|
@@ -2468,12 +2553,15 @@ var Writer = class {
|
|
|
2468
2553
|
const axis = docs[0];
|
|
2469
2554
|
const body = docs[1];
|
|
2470
2555
|
const address = at.make(docs.slice(2));
|
|
2556
|
+
const as = itemsOf(e.as);
|
|
2471
2557
|
return this.#keep(typed("each", reads ? {
|
|
2472
2558
|
body,
|
|
2473
2559
|
axis,
|
|
2560
|
+
...as,
|
|
2474
2561
|
...address
|
|
2475
2562
|
} : {
|
|
2476
2563
|
axis,
|
|
2564
|
+
...as,
|
|
2477
2565
|
body,
|
|
2478
2566
|
...address
|
|
2479
2567
|
}), e);
|
|
@@ -2504,13 +2592,16 @@ var Writer = class {
|
|
|
2504
2592
|
};
|
|
2505
2593
|
const result = docs[after];
|
|
2506
2594
|
const address = at.make(docs.slice(after + 1));
|
|
2595
|
+
const as = itemsOf(e.as);
|
|
2507
2596
|
return this.#keep(typed("fold", reads ? {
|
|
2508
2597
|
...names,
|
|
2509
2598
|
axis,
|
|
2599
|
+
...as,
|
|
2510
2600
|
...address,
|
|
2511
2601
|
result
|
|
2512
2602
|
} : {
|
|
2513
2603
|
axis,
|
|
2604
|
+
...as,
|
|
2514
2605
|
...names,
|
|
2515
2606
|
result,
|
|
2516
2607
|
...address
|
|
@@ -2636,8 +2727,7 @@ function writeDocument(e, opts) {
|
|
|
2636
2727
|
e
|
|
2637
2728
|
});
|
|
2638
2729
|
}
|
|
2639
|
-
|
|
2640
|
-
//#region src/document.ts
|
|
2730
|
+
// src/document.ts
|
|
2641
2731
|
/**
|
|
2642
2732
|
* The documents of Vex. A document is the form of an expression in coeval, the kernel of the family. Each term is an
|
|
2643
2733
|
* envelope `{ type, value }`, and a reference is a path from a root. The rules are decision D12 of coeval:
|
|
@@ -2653,18 +2743,20 @@ function writeDocument(e, opts) {
|
|
|
2653
2743
|
* | `fn` | `{ "type": "fn", "value": { "params", "body" } }` |
|
|
2654
2744
|
* | `call` | `{ "type": "call", "value": { "fn": { "type": "ref", "value": ["names", name] }, "args" } }` |
|
|
2655
2745
|
* | `rec` | `{ "type": "record", "value": { ...fields } }` |
|
|
2656
|
-
* | `each` | `{ "type": "each", "value": { "axis", "body", "at"? } }` |
|
|
2657
|
-
* | `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"? } }` |
|
|
2658
2748
|
* | `iterate` | `{ "type": "iterate", "value": { "limit", "init", "step", "until"?, "result" } }` |
|
|
2659
2749
|
* | `ext` | `{ "type": ["ext", ...keys], "value": data }` |
|
|
2660
2750
|
* | a move | `{ "type": ["moves", kind], "value": v }` |
|
|
2661
2751
|
* | an axis | `{ "type": ["axes", name], "value": { ...parameters } }` |
|
|
2662
2752
|
*
|
|
2663
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).
|
|
2664
2755
|
*
|
|
2665
2756
|
* The kernel checks a document by the contracts of `VEX_WORLD` (DOC.CHECK). A contract gives the shape of a kind, the
|
|
2666
2757
|
* data of its fields and the kinds of each position. Then `fromDocument` reads the expression. It checks only the grammar
|
|
2667
|
-
* 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.
|
|
2668
2760
|
*/
|
|
2669
2761
|
/** The stop of a reading. The functions of this module throw it, and the top of the reading gives its error. */
|
|
2670
2762
|
var Stop = class {
|
|
@@ -2772,10 +2864,16 @@ var Reader = class {
|
|
|
2772
2864
|
return call(name, ...f["args"].map((x, i) => this.term(x, into(into(at, "args"), i))));
|
|
2773
2865
|
}
|
|
2774
2866
|
case "record": return rec(this.#names(u.value, at));
|
|
2775
|
-
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
|
+
}
|
|
2776
2872
|
case "fold": {
|
|
2777
2873
|
const [init, step] = this.#loop(f, at);
|
|
2778
|
-
|
|
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);
|
|
2779
2877
|
}
|
|
2780
2878
|
default: {
|
|
2781
2879
|
const [init, step] = this.#loop(f, at);
|
|
@@ -2819,6 +2917,18 @@ var Reader = class {
|
|
|
2819
2917
|
const extra = Object.keys(step).find((name) => !Object.hasOwn(init, name));
|
|
2820
2918
|
return extra === void 0 ? [init, step] : bad(into(into(at, "step"), extra), `the name "${extra}" of step is not a name of init`);
|
|
2821
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
|
+
}
|
|
2822
2932
|
/** This method reads a reference: the fields of `self` after its moves, or a name of `names` (DOC.REF). */
|
|
2823
2933
|
#ref(path, where) {
|
|
2824
2934
|
const [root, ...steps] = path;
|
|
@@ -2888,8 +2998,7 @@ var Reader = class {
|
|
|
2888
2998
|
}
|
|
2889
2999
|
}
|
|
2890
3000
|
};
|
|
2891
|
-
|
|
2892
|
-
//#region src/evaluate.ts
|
|
3001
|
+
// src/evaluate.ts
|
|
2893
3002
|
/** The names of the special forms. Their arguments are not evaluated before the call. */
|
|
2894
3003
|
const SPECIAL_FORMS = /* @__PURE__ */ new Set([
|
|
2895
3004
|
"if",
|
|
@@ -3011,6 +3120,18 @@ function explain(expr, opts) {
|
|
|
3011
3120
|
}
|
|
3012
3121
|
/** This function gives the axis inside the `where` axes of an axis. */
|
|
3013
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
|
+
}
|
|
3014
3135
|
var Compiler = class {
|
|
3015
3136
|
#domains;
|
|
3016
3137
|
#fns;
|
|
@@ -3294,7 +3415,8 @@ var Compiler = class {
|
|
|
3294
3415
|
}
|
|
3295
3416
|
const ts = targets(from, inner);
|
|
3296
3417
|
if (!ts.ok) return this.#emit(c, info, f, ts);
|
|
3297
|
-
const items = ts.value.map((
|
|
3418
|
+
const items = ts.value.map((t) => {
|
|
3419
|
+
const { key, test } = t;
|
|
3298
3420
|
if (test !== void 0 && !test.ok) return {
|
|
3299
3421
|
key,
|
|
3300
3422
|
result: test
|
|
@@ -3306,7 +3428,7 @@ var Compiler = class {
|
|
|
3306
3428
|
origin: base.t === "table" ? key : f.pos.origin,
|
|
3307
3429
|
focus: key
|
|
3308
3430
|
},
|
|
3309
|
-
env: f.env
|
|
3431
|
+
env: withItem(f.env, e.as, t)
|
|
3310
3432
|
}, inner)
|
|
3311
3433
|
};
|
|
3312
3434
|
});
|
|
@@ -3330,14 +3452,14 @@ var Compiler = class {
|
|
|
3330
3452
|
}
|
|
3331
3453
|
return ok(env);
|
|
3332
3454
|
},
|
|
3333
|
-
next: (f, c) => {
|
|
3455
|
+
next: (f, c, names = f.env) => {
|
|
3334
3456
|
const values = [];
|
|
3335
3457
|
for (const [name, n] of steps) {
|
|
3336
3458
|
const r = n(f, c);
|
|
3337
3459
|
if (!r.ok) return r;
|
|
3338
3460
|
values.push([name, r]);
|
|
3339
3461
|
}
|
|
3340
|
-
const env = new Map(
|
|
3462
|
+
const env = new Map(names);
|
|
3341
3463
|
for (const [name, r] of values) env.set(name, r);
|
|
3342
3464
|
return ok(env);
|
|
3343
3465
|
}
|
|
@@ -3383,15 +3505,16 @@ var Compiler = class {
|
|
|
3383
3505
|
}
|
|
3384
3506
|
const ts = targets(from, inner);
|
|
3385
3507
|
if (!ts.ok) return this.#emit(c, info, f, ts);
|
|
3386
|
-
for (const
|
|
3508
|
+
for (const t of ts.value) {
|
|
3509
|
+
const { key, test } = t;
|
|
3387
3510
|
if (test !== void 0 && !test.ok) return this.#emit(c, info, f, test);
|
|
3388
3511
|
const after = names.next({
|
|
3389
3512
|
pos: {
|
|
3390
3513
|
origin: base.t === "table" ? key : f.pos.origin,
|
|
3391
3514
|
focus: key
|
|
3392
3515
|
},
|
|
3393
|
-
env
|
|
3394
|
-
}, inner);
|
|
3516
|
+
env: withItem(env, e.as, t)
|
|
3517
|
+
}, inner, env);
|
|
3395
3518
|
if (!after.ok) return this.#emit(c, info, f, after);
|
|
3396
3519
|
env = after.value;
|
|
3397
3520
|
}
|
|
@@ -3479,7 +3602,7 @@ var Compiler = class {
|
|
|
3479
3602
|
key: t.key,
|
|
3480
3603
|
test: r
|
|
3481
3604
|
});
|
|
3482
|
-
else if (r.value === true) out.push(
|
|
3605
|
+
else if (r.value === true) out.push(t);
|
|
3483
3606
|
else if (r.value !== false) out.push({
|
|
3484
3607
|
key: t.key,
|
|
3485
3608
|
test: this.#err("kind-mismatch", `the test of "where" gave a ${typeof r.value}, not a boolean`, at, testPath)
|
|
@@ -3499,7 +3622,12 @@ var Compiler = class {
|
|
|
3499
3622
|
if (!r.ok) return r;
|
|
3500
3623
|
const listed = asList(r.value, this.#missing(f, path));
|
|
3501
3624
|
if (listed === void 0) return this.#err("kind-mismatch", `the axis "list" needs a list, not a ${describeType(r.value)}`, f, path);
|
|
3502
|
-
|
|
3625
|
+
let index = 0;
|
|
3626
|
+
const items = listed.items.map(({ key, result }) => result.ok ? {
|
|
3627
|
+
key,
|
|
3628
|
+
value: result.value,
|
|
3629
|
+
index: index++
|
|
3630
|
+
} : {
|
|
3503
3631
|
key,
|
|
3504
3632
|
test: result
|
|
3505
3633
|
});
|
|
@@ -3637,7 +3765,7 @@ var Compiler = class {
|
|
|
3637
3765
|
return second !== void 0 ? goodItems(second, strict) : this.#err("kind-mismatch", `"${op}" needs a list as its second argument, not a ${describeType(rest[0])}`, f, path, { op });
|
|
3638
3766
|
};
|
|
3639
3767
|
switch (op) {
|
|
3640
|
-
case "sortBy": return this.#sortBy(okItems, okValues, f, path);
|
|
3768
|
+
case "sortBy": return this.#sortBy(okItems, okValues, optsArg, f, path);
|
|
3641
3769
|
case "take":
|
|
3642
3770
|
case "drop": {
|
|
3643
3771
|
const n = whole();
|
|
@@ -3730,21 +3858,49 @@ var Compiler = class {
|
|
|
3730
3858
|
return ok(vexList(out));
|
|
3731
3859
|
}
|
|
3732
3860
|
/**
|
|
3733
|
-
* This method sorts the items of a list by their values, from the smallest (LIST.SORT). The sort is stable. The
|
|
3734
|
-
* are all numbers without NaN, or all texts. Texts compare by their UTF-16 code units.
|
|
3861
|
+
* This method sorts the items of a list by their sort values, from the smallest (LIST.SORT). The sort is stable. The
|
|
3862
|
+
* sort values are all numbers without NaN, or all texts. Texts compare by their UTF-16 code units. Without the option
|
|
3863
|
+
* `by`, the sort value of an item is its value (LIST.SORT.BY).
|
|
3735
3864
|
*/
|
|
3736
|
-
#sortBy(items, values, f, path) {
|
|
3737
|
-
const
|
|
3738
|
-
const
|
|
3739
|
-
if (
|
|
3740
|
-
const
|
|
3741
|
-
|
|
3742
|
-
|
|
3865
|
+
#sortBy(items, values, opts, f, path) {
|
|
3866
|
+
const by = opts?.by;
|
|
3867
|
+
const sorted = by === void 0 ? ok(values) : this.#sortValues(by, values, f, path);
|
|
3868
|
+
if (!sorted.ok) return sorted;
|
|
3869
|
+
const keys = sorted.value;
|
|
3870
|
+
const kind = typeof keys[0];
|
|
3871
|
+
const bad = keys.findIndex((x) => typeof x !== kind || kind !== "number" && kind !== "string" || Number.isNaN(x));
|
|
3872
|
+
if (bad >= 0) {
|
|
3873
|
+
const what = typeof by === "string" ? `"${by}" of an item` : "an item";
|
|
3874
|
+
return this.#err("kind-mismatch", `"sortBy" needs numbers without NaN or texts, all of one kind, but ${what} is ${Number.isNaN(keys[bad]) ? "NaN" : `a ${describeType(keys[bad])}`}`, f, path, { op: "sortBy" });
|
|
3875
|
+
}
|
|
3876
|
+
const order = keys.map((_x, i) => i).toSorted((i, j) => {
|
|
3877
|
+
const a = keys[i];
|
|
3878
|
+
const b = keys[j];
|
|
3743
3879
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
3744
3880
|
});
|
|
3745
3881
|
return ok(vexList(order.map((i) => items[i])));
|
|
3746
3882
|
}
|
|
3747
3883
|
/**
|
|
3884
|
+
* This method gives the sort value of each item for the option `by` of `sortBy` (LIST.SORT.BY). The op of the name
|
|
3885
|
+
* `by` applies to the value of each item, in the order of the list. The first error is the result.
|
|
3886
|
+
*/
|
|
3887
|
+
#sortValues(by, values, f, path) {
|
|
3888
|
+
if (typeof by !== "string") return this.#err("kind-mismatch", `the option "by" of "sortBy" needs an op name, but got a ${describeType(by)}`, f, path, { op: "sortBy" });
|
|
3889
|
+
const out = [];
|
|
3890
|
+
for (const x of values) {
|
|
3891
|
+
let op;
|
|
3892
|
+
for (const d of this.#domains) {
|
|
3893
|
+
op = safeIs(d, x) ? resolveOp(d, x, by) : void 0;
|
|
3894
|
+
if (op !== void 0) break;
|
|
3895
|
+
}
|
|
3896
|
+
if (op === void 0) return this.#err("unknown-op", `no domain gives "${by}" for a ${describeType(x)}`, f, path, { op: by });
|
|
3897
|
+
const r = this.#call(op, by, [], f, path);
|
|
3898
|
+
if (!r.ok) return r;
|
|
3899
|
+
out.push(r.value);
|
|
3900
|
+
}
|
|
3901
|
+
return ok(out);
|
|
3902
|
+
}
|
|
3903
|
+
/**
|
|
3748
3904
|
* This method folds the values with a domain op, from the left. An empty list gives the identity of the op, if one
|
|
3749
3905
|
* domain has an identity for the op (LIST.EMPTY). The option `domain` names that domain when more domains have one.
|
|
3750
3906
|
*/
|
|
@@ -3914,8 +4070,7 @@ function formatTrace(trace, domains = []) {
|
|
|
3914
4070
|
}
|
|
3915
4071
|
/** This function gives the number of nodes of an expression. */
|
|
3916
4072
|
const sizeOf = (e) => 1 + children(e).reduce((s, c) => s + sizeOf(c), 0);
|
|
3917
|
-
|
|
3918
|
-
//#region src/deps.ts
|
|
4073
|
+
// src/deps.ts
|
|
3919
4074
|
/**
|
|
3920
4075
|
* The static reads of a program (DEPS.READS). The static kit of coeval finds the references of the document of the
|
|
3921
4076
|
* program, by the contracts of Vex (`refsOf`). Vex adds only what is its own: the axes around a read, and the reads of
|
|
@@ -3963,7 +4118,7 @@ function deps(e) {
|
|
|
3963
4118
|
if (!rule.in.some((pattern) => pathMatches(child.slot, pattern))) continue;
|
|
3964
4119
|
const source = rule.names;
|
|
3965
4120
|
const at = (source.keysAt ?? source.listAt ?? []).reduce((x, k) => x[String(k)], around.doc.value);
|
|
3966
|
-
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;
|
|
3967
4122
|
found = fnVisits.get(at[name]);
|
|
3968
4123
|
break search;
|
|
3969
4124
|
}
|
|
@@ -4080,8 +4235,7 @@ function deps(e) {
|
|
|
4080
4235
|
emit(groups.get(void 0) ?? [], void 0, []);
|
|
4081
4236
|
return out;
|
|
4082
4237
|
}
|
|
4083
|
-
|
|
4084
|
-
//#region src/json.ts
|
|
4238
|
+
// src/json.ts
|
|
4085
4239
|
/** This function applies `f` to each literal value of a JSON tree of an expression. */
|
|
4086
4240
|
function mapValues(u, f) {
|
|
4087
4241
|
if (!isRecord(u)) return ok(u);
|
|
@@ -4223,8 +4377,7 @@ function parse(text, domains = []) {
|
|
|
4223
4377
|
return fail(vexError("threw", "a decoder of a domain threw an exception", { thrown }));
|
|
4224
4378
|
}
|
|
4225
4379
|
}
|
|
4226
|
-
|
|
4227
|
-
//#region src/plan.ts
|
|
4380
|
+
// src/plan.ts
|
|
4228
4381
|
/**
|
|
4229
4382
|
* The reference tier of Vex: a program as a plan of the kernel of coeval (decision D13 of coeval). The kernel evaluates
|
|
4230
4383
|
* the document of the program with the machine of `kernel.ts`. The closure compiler and the loop compiler give its
|
|
@@ -4313,8 +4466,7 @@ function kernelPlan(source, opts = {}) {
|
|
|
4313
4466
|
run: (r) => toVexResult(evaluate(r))
|
|
4314
4467
|
};
|
|
4315
4468
|
}
|
|
4316
|
-
|
|
4317
|
-
//#region src/traversal.ts
|
|
4469
|
+
// src/traversal.ts
|
|
4318
4470
|
/** Standard monoids. */
|
|
4319
4471
|
const monoids = {
|
|
4320
4472
|
sum: {
|
|
@@ -4396,8 +4548,7 @@ function traversal(items, env, isStrict = false) {
|
|
|
4396
4548
|
};
|
|
4397
4549
|
return t;
|
|
4398
4550
|
}
|
|
4399
|
-
|
|
4400
|
-
//#region src/sheet.ts
|
|
4551
|
+
// src/sheet.ts
|
|
4401
4552
|
/**
|
|
4402
4553
|
* Sheets: named columns of formulas over a space. A formula can read a column at an address, like a cell
|
|
4403
4554
|
* reference of a spreadsheet. A cell is one column at one key. A run evaluates each cell once, and each cell on a
|
|
@@ -4919,8 +5070,7 @@ var SheetRun = class SheetRun {
|
|
|
4919
5070
|
if ("space" in t) this.#warmTo(t, column, key);
|
|
4920
5071
|
}
|
|
4921
5072
|
};
|
|
4922
|
-
|
|
4923
|
-
//#region src/builder.ts
|
|
5073
|
+
// src/builder.ts
|
|
4924
5074
|
const argToken = (expr) => ({
|
|
4925
5075
|
kind: "vex.arg",
|
|
4926
5076
|
expr
|
|
@@ -4955,6 +5105,37 @@ function stepExprs(step, unique, addr) {
|
|
|
4955
5105
|
}
|
|
4956
5106
|
return out;
|
|
4957
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
|
+
}
|
|
4958
5139
|
const PROXY_RESERVED = /* @__PURE__ */ new Set([
|
|
4959
5140
|
"then",
|
|
4960
5141
|
"catch",
|
|
@@ -5266,9 +5447,15 @@ var ListChainImpl = class ListChainImpl {
|
|
|
5266
5447
|
expr: loop.body,
|
|
5267
5448
|
addr: []
|
|
5268
5449
|
});
|
|
5269
|
-
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;
|
|
5270
5457
|
const r = result(names.vars);
|
|
5271
|
-
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);
|
|
5272
5459
|
return chainOf({
|
|
5273
5460
|
...this.#s,
|
|
5274
5461
|
expr: e.tag === "let" ? let_(e.bind, folded) : folded
|
|
@@ -5355,11 +5542,14 @@ var ListChainImpl = class ListChainImpl {
|
|
|
5355
5542
|
return this.#list("flat", [], opts);
|
|
5356
5543
|
}
|
|
5357
5544
|
each(body, opts) {
|
|
5358
|
-
const
|
|
5545
|
+
const inner = {
|
|
5359
5546
|
...this.#s,
|
|
5360
|
-
expr: lit(null),
|
|
5361
5547
|
addr: [],
|
|
5362
5548
|
depth: this.#s.depth + 1
|
|
5549
|
+
};
|
|
5550
|
+
const start = new ChainImpl({
|
|
5551
|
+
...inner,
|
|
5552
|
+
expr: lit(null)
|
|
5363
5553
|
});
|
|
5364
5554
|
const list = {
|
|
5365
5555
|
t: "list",
|
|
@@ -5374,9 +5564,18 @@ var ListChainImpl = class ListChainImpl {
|
|
|
5374
5564
|
axis: ordered,
|
|
5375
5565
|
test: opts.where(start).program
|
|
5376
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;
|
|
5377
5576
|
return new ListChainImpl({
|
|
5378
5577
|
...this.#s,
|
|
5379
|
-
expr: each(axis,
|
|
5578
|
+
expr: each(axis, program, void 0, as)
|
|
5380
5579
|
});
|
|
5381
5580
|
}
|
|
5382
5581
|
result(k) {
|
|
@@ -5534,7 +5733,6 @@ function vex(...domains) {
|
|
|
5534
5733
|
withOptions: make
|
|
5535
5734
|
};
|
|
5536
5735
|
}
|
|
5537
|
-
//#endregion
|
|
5538
5736
|
export { CELL_KIND, LIST_OPS, MAX_DOCUMENT_DEPTH, MAX_ITERATIONS, MAX_NESTING, SPECIAL_FORMS, SheetRun, TERM_KINDS, VEX_WORLD, 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, fromDocument, getOr, index, isAxis, isDepthError, isDocument, isExpr, isFieldName, isFixedAddr, isListOp, isMove, isNone, isOpName, isSome, isStackOverflow, isVexList, iterate, kernelPlan, 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, toDocument, toKernelError, toOptional, toPath, toVexError, toVexResult, traversal, v, vex, vexChecker, vexDefinitions, vexError, vexList, vexMachine, vexSelf };
|
|
5539
5737
|
|
|
5540
5738
|
//# sourceMappingURL=index.js.map
|