@mark1russell7/vex 0.9.0 → 0.10.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/README.md +36 -1
- package/build/index.d.ts +155 -28
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2124 -227
- package/build/index.js.map +1 -1
- package/package.json +5 -1
- package/src/builder.ts +54 -13
- package/src/codec.ts +104 -0
- package/src/deps.ts +174 -60
- package/src/document.ts +301 -0
- package/src/errors.ts +7 -3
- package/src/evaluate.ts +82 -28
- package/src/index.ts +5 -1
- package/src/json.ts +11 -96
- package/src/kernel.ts +897 -0
- package/src/list.ts +6 -3
- package/src/plan.ts +88 -0
- package/src/same.ts +31 -0
- package/src/sheet.ts +2 -1
- package/src/write.ts +257 -0
package/build/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
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";
|
|
1
2
|
//#region src/result.ts
|
|
2
|
-
const NONE = Object.freeze({ tag: "none" });
|
|
3
|
+
const NONE$1 = Object.freeze({ tag: "none" });
|
|
3
4
|
/** This function makes an `Optional` that holds `value`. */
|
|
4
5
|
const some = (value) => ({
|
|
5
6
|
tag: "some",
|
|
6
7
|
value
|
|
7
8
|
});
|
|
8
9
|
/** This function gives the shared empty `Optional`. */
|
|
9
|
-
const none = () => NONE;
|
|
10
|
+
const none = () => NONE$1;
|
|
10
11
|
/** This function makes a successful `Result`. */
|
|
11
12
|
const ok = (value) => ({
|
|
12
13
|
ok: true,
|
|
@@ -973,7 +974,7 @@ function defineDomain(spec) {
|
|
|
973
974
|
kind: "vex.domain"
|
|
974
975
|
});
|
|
975
976
|
}
|
|
976
|
-
const FORBIDDEN = /* @__PURE__ */ new Set([
|
|
977
|
+
const FORBIDDEN$1 = /* @__PURE__ */ new Set([
|
|
977
978
|
"constructor",
|
|
978
979
|
"__proto__",
|
|
979
980
|
"prototype",
|
|
@@ -983,7 +984,7 @@ const FORBIDDEN = /* @__PURE__ */ new Set([
|
|
|
983
984
|
"__lookupSetter__"
|
|
984
985
|
]);
|
|
985
986
|
/** This function tells if a name can be an op name. */
|
|
986
|
-
const isOpName = (name) => name.length > 0 && !FORBIDDEN.has(name);
|
|
987
|
+
const isOpName = (name) => name.length > 0 && !FORBIDDEN$1.has(name);
|
|
987
988
|
/**
|
|
988
989
|
* This function finds a method on a value: an own function property, or a function on the prototype chain.
|
|
989
990
|
* It stops at `Object.prototype` and `Function.prototype`, so inherited members (for example `toString`) are not ops.
|
|
@@ -1011,51 +1012,1791 @@ function resolveOp(domain, self, name) {
|
|
|
1011
1012
|
call: (args) => fn(self, ...args)
|
|
1012
1013
|
};
|
|
1013
1014
|
}
|
|
1014
|
-
if (declared === void 0 && domain.methods !== "all") return void 0;
|
|
1015
|
-
const method = findMethod(self, name);
|
|
1016
|
-
if (method === void 0) return void 0;
|
|
1017
|
-
return {
|
|
1018
|
-
domain,
|
|
1019
|
-
spec: declared ?? EMPTY_SPEC,
|
|
1020
|
-
call: (args) => method.apply(self, [...args])
|
|
1015
|
+
if (declared === void 0 && domain.methods !== "all") return void 0;
|
|
1016
|
+
const method = findMethod(self, name);
|
|
1017
|
+
if (method === void 0) return void 0;
|
|
1018
|
+
return {
|
|
1019
|
+
domain,
|
|
1020
|
+
spec: declared ?? EMPTY_SPEC,
|
|
1021
|
+
call: (args) => method.apply(self, [...args])
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
/** This function gives the domain of a value: the first domain whose `is` accepts it. */
|
|
1025
|
+
function domainOf(domains, value) {
|
|
1026
|
+
return domains.find((d) => d.is(value));
|
|
1027
|
+
}
|
|
1028
|
+
/** This function tells if the `liftScalar` setting lifts the argument at `index`. */
|
|
1029
|
+
const liftsAt = (lift, index) => lift === true || Array.isArray(lift) && lift[index] === true;
|
|
1030
|
+
//#endregion
|
|
1031
|
+
//#region src/list.ts
|
|
1032
|
+
/** This function makes a list. */
|
|
1033
|
+
const vexList = (items) => Object.freeze({
|
|
1034
|
+
kind: "vex.list",
|
|
1035
|
+
items: Object.freeze([...items])
|
|
1036
|
+
});
|
|
1037
|
+
/** This function tells if a value is a Vex list. */
|
|
1038
|
+
const isVexList = (u) => u?.kind === "vex.list" && Array.isArray(u.items);
|
|
1039
|
+
/** The names of the core list ops. They apply when the first argument is a list. */
|
|
1040
|
+
const LIST_OPS = /* @__PURE__ */ new Set([
|
|
1041
|
+
"count",
|
|
1042
|
+
"sum",
|
|
1043
|
+
"min",
|
|
1044
|
+
"max",
|
|
1045
|
+
"mean",
|
|
1046
|
+
"any",
|
|
1047
|
+
"all",
|
|
1048
|
+
"none",
|
|
1049
|
+
"values",
|
|
1050
|
+
"keys",
|
|
1051
|
+
"reduce",
|
|
1052
|
+
"first",
|
|
1053
|
+
"errors",
|
|
1054
|
+
"sortBy",
|
|
1055
|
+
"take",
|
|
1056
|
+
"nth",
|
|
1057
|
+
"drop",
|
|
1058
|
+
"append",
|
|
1059
|
+
"flat",
|
|
1060
|
+
"without"
|
|
1061
|
+
]);
|
|
1062
|
+
/** This function tells if a name is the name of a core list op. */
|
|
1063
|
+
const isListOp = (op) => LIST_OPS.has(op);
|
|
1064
|
+
//#endregion
|
|
1065
|
+
//#region src/codec.ts
|
|
1066
|
+
/** This function tells if a value is a record: an object that is not an array. */
|
|
1067
|
+
const isRecord = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
1068
|
+
const isPlainRecord = (u) => {
|
|
1069
|
+
if (!isRecord(u)) return false;
|
|
1070
|
+
const proto = Object.getPrototypeOf(u);
|
|
1071
|
+
return proto === Object.prototype || proto === null;
|
|
1072
|
+
};
|
|
1073
|
+
/** This function gives the JSON form of a value, or the reason why the value has no JSON form. */
|
|
1074
|
+
function encodeValue(u, domains) {
|
|
1075
|
+
if (u === null || typeof u === "string" || typeof u === "boolean") return ok(u);
|
|
1076
|
+
if (typeof u === "number") {
|
|
1077
|
+
if (Object.is(u, -0)) return ok({
|
|
1078
|
+
$vex: "number",
|
|
1079
|
+
value: "-0"
|
|
1080
|
+
});
|
|
1081
|
+
return Number.isFinite(u) ? ok(u) : ok({
|
|
1082
|
+
$vex: "number",
|
|
1083
|
+
value: Number.isNaN(u) ? "NaN" : u > 0 ? "Infinity" : "-Infinity"
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
if (Array.isArray(u)) {
|
|
1087
|
+
const out = [];
|
|
1088
|
+
for (const x of u) {
|
|
1089
|
+
const r = encodeValue(x, domains);
|
|
1090
|
+
if (!r.ok) return r;
|
|
1091
|
+
out.push(r.value);
|
|
1092
|
+
}
|
|
1093
|
+
return ok(out);
|
|
1094
|
+
}
|
|
1095
|
+
if (isPlainRecord(u)) {
|
|
1096
|
+
const out = {};
|
|
1097
|
+
for (const [k, x] of Object.entries(u)) {
|
|
1098
|
+
const r = encodeValue(x, domains);
|
|
1099
|
+
if (!r.ok) return r;
|
|
1100
|
+
out[k] = r.value;
|
|
1101
|
+
}
|
|
1102
|
+
return ok(Object.hasOwn(u, "$vex") ? {
|
|
1103
|
+
$vex: "escape",
|
|
1104
|
+
value: out
|
|
1105
|
+
} : out);
|
|
1106
|
+
}
|
|
1107
|
+
const d = domains.find((dom) => dom.encode !== void 0 && dom.is(u));
|
|
1108
|
+
if (d?.encode !== void 0) return ok({
|
|
1109
|
+
$vex: "domain",
|
|
1110
|
+
domain: d.name,
|
|
1111
|
+
value: d.encode(u)
|
|
1112
|
+
});
|
|
1113
|
+
return fail(vexError("bad-expression", "a literal value is not JSON data and no domain encodes it"));
|
|
1114
|
+
}
|
|
1115
|
+
const NUMBERS = {
|
|
1116
|
+
"-0": -0,
|
|
1117
|
+
NaN: NaN,
|
|
1118
|
+
Infinity: Number.POSITIVE_INFINITY,
|
|
1119
|
+
"-Infinity": Number.NEGATIVE_INFINITY
|
|
1120
|
+
};
|
|
1121
|
+
/** This function reads a value from its JSON form. */
|
|
1122
|
+
function decodeValue(u, domains) {
|
|
1123
|
+
if (Array.isArray(u)) {
|
|
1124
|
+
const out = [];
|
|
1125
|
+
for (const x of u) {
|
|
1126
|
+
const r = decodeValue(x, domains);
|
|
1127
|
+
if (!r.ok) return r;
|
|
1128
|
+
out.push(r.value);
|
|
1129
|
+
}
|
|
1130
|
+
return ok(out);
|
|
1131
|
+
}
|
|
1132
|
+
if (!isRecord(u)) return ok(u);
|
|
1133
|
+
const fields = (record) => {
|
|
1134
|
+
const out = {};
|
|
1135
|
+
for (const [k, x] of Object.entries(record)) {
|
|
1136
|
+
const r = decodeValue(x, domains);
|
|
1137
|
+
if (!r.ok) return r;
|
|
1138
|
+
out[k] = r.value;
|
|
1139
|
+
}
|
|
1140
|
+
return ok(out);
|
|
1141
|
+
};
|
|
1142
|
+
const kind = u["$vex"];
|
|
1143
|
+
if (kind === "escape" && isRecord(u["value"])) return fields(u["value"]);
|
|
1144
|
+
if (kind === "number" && typeof u["value"] === "string" && Object.hasOwn(NUMBERS, u["value"])) return ok(NUMBERS[u["value"]]);
|
|
1145
|
+
if (kind === "domain" && typeof u["domain"] === "string") {
|
|
1146
|
+
const name = u["domain"];
|
|
1147
|
+
const d = domains.find((dom) => dom.name === name);
|
|
1148
|
+
if (d?.decode === void 0) return fail(vexError("bad-expression", `no domain "${name}" decodes a literal`));
|
|
1149
|
+
return ok(d.decode(u["value"]));
|
|
1150
|
+
}
|
|
1151
|
+
return fields(u);
|
|
1152
|
+
}
|
|
1153
|
+
//#endregion
|
|
1154
|
+
//#region src/same.ts
|
|
1155
|
+
/**
|
|
1156
|
+
* The comparison of the values of Vex for `agree` of the kernel (EVAL.AGREE), and the test of a Vex error. The package
|
|
1157
|
+
* index does not export this module: `kernel.ts` and `plan.ts` use it.
|
|
1158
|
+
*/
|
|
1159
|
+
/** This function tells if a value is a Vex error: it has a code, a kind, a message and a path. */
|
|
1160
|
+
function isVexError(u) {
|
|
1161
|
+
const e = u;
|
|
1162
|
+
return typeof e?.code === "string" && typeof e.kind === "string" && typeof e.message === "string" && Array.isArray(e.path);
|
|
1163
|
+
}
|
|
1164
|
+
/** This function tells if a value is a value of a class: an object that is not a plain record and not an array. */
|
|
1165
|
+
function isInstance(u) {
|
|
1166
|
+
if (typeof u !== "object" || u === null || Array.isArray(u)) return false;
|
|
1167
|
+
const proto = Object.getPrototypeOf(u);
|
|
1168
|
+
return proto !== Object.prototype && proto !== null;
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
1171
|
+
* This function compares two values of Vex for `agree`. Two Vex errors agree when they have the same code, because
|
|
1172
|
+
* only the closure compiler gives the location of an error (EVAL.LOCATION). Two values of one class agree when their
|
|
1173
|
+
* fields agree, for example two values of a domain from two decodes of a literal. The rules of `sameValue` compare
|
|
1174
|
+
* each other pair.
|
|
1175
|
+
*/
|
|
1176
|
+
function sameVex(a, b) {
|
|
1177
|
+
if (isVexError(a) && isVexError(b)) return a.code === b.code;
|
|
1178
|
+
if (!isInstance(a) || !isInstance(b)) return void 0;
|
|
1179
|
+
return Object.getPrototypeOf(a) === Object.getPrototypeOf(b) && sameValue({ ...a }, { ...b }, sameVex);
|
|
1180
|
+
}
|
|
1181
|
+
//#endregion
|
|
1182
|
+
//#region src/kernel.ts
|
|
1183
|
+
/**
|
|
1184
|
+
* Vex in the kernel of coeval (decision D13 of coeval, and decision D13 of Vex in `docs/REVIEW.md`). This module gives
|
|
1185
|
+
* the contracts of the kinds of Vex as data, and a machine of the kernel that evaluates the documents of Vex.
|
|
1186
|
+
*
|
|
1187
|
+
* - The static kit of the kernel checks a document by these contracts (`fromDocument`, DOC.ERROR) and finds its
|
|
1188
|
+
* references (`deps`, DEPS.READS).
|
|
1189
|
+
* - The machine is the reference tier of Vex. The closure compiler and the loop compiler give its results
|
|
1190
|
+
* (EVAL.REFERENCE). It evaluates the shared constructs with the formula kit of the kernel, and a profile gives the
|
|
1191
|
+
* rules of Vex. Vex adds only what is its own: the moves, the axes, the loops and the dispatch on the domains.
|
|
1192
|
+
*/
|
|
1193
|
+
/** The kinds of the terms of Vex. A part in the position of a term has one of these kinds (DOC.TERM). */
|
|
1194
|
+
const TERM_KINDS = [
|
|
1195
|
+
["lit"],
|
|
1196
|
+
["ref"],
|
|
1197
|
+
["record"],
|
|
1198
|
+
["let"],
|
|
1199
|
+
["call"],
|
|
1200
|
+
["each"],
|
|
1201
|
+
["fold"],
|
|
1202
|
+
["iterate"],
|
|
1203
|
+
["ops"],
|
|
1204
|
+
["ext"]
|
|
1205
|
+
];
|
|
1206
|
+
/** A term: an envelope of a term kind, or a bare value, which is a literal. */
|
|
1207
|
+
const TERM = {
|
|
1208
|
+
shape: "term",
|
|
1209
|
+
of: "lit",
|
|
1210
|
+
kinds: TERM_KINDS
|
|
1211
|
+
};
|
|
1212
|
+
const TERMS$1 = {
|
|
1213
|
+
shape: "list",
|
|
1214
|
+
item: TERM
|
|
1215
|
+
};
|
|
1216
|
+
const NAMES = {
|
|
1217
|
+
shape: "map",
|
|
1218
|
+
item: TERM
|
|
1219
|
+
};
|
|
1220
|
+
const AXIS = {
|
|
1221
|
+
shape: "term",
|
|
1222
|
+
kinds: [["axes"]]
|
|
1223
|
+
};
|
|
1224
|
+
/** The address of `each` and `fold`: a reference that gives a key of the space, not a value (DOC.ADDRESS). */
|
|
1225
|
+
const ADDRESS = {
|
|
1226
|
+
shape: "term",
|
|
1227
|
+
kinds: [["ref"]],
|
|
1228
|
+
address: true
|
|
1229
|
+
};
|
|
1230
|
+
const NO_VALUE = {
|
|
1231
|
+
shape: "one",
|
|
1232
|
+
values: [null]
|
|
1233
|
+
};
|
|
1234
|
+
/** This function gives the shape of a record with these fields and no other fields (DOC.CONSTRUCT). */
|
|
1235
|
+
const closed = (fields, required) => ({
|
|
1236
|
+
shape: "record",
|
|
1237
|
+
fields,
|
|
1238
|
+
required,
|
|
1239
|
+
closed: true
|
|
1240
|
+
});
|
|
1241
|
+
/** The axes without parameters. */
|
|
1242
|
+
const PLAIN_AXES = [
|
|
1243
|
+
"all",
|
|
1244
|
+
"others",
|
|
1245
|
+
"other",
|
|
1246
|
+
"children",
|
|
1247
|
+
"ancestors",
|
|
1248
|
+
"descendants",
|
|
1249
|
+
"siblings",
|
|
1250
|
+
"successors",
|
|
1251
|
+
"predecessors",
|
|
1252
|
+
"reachable"
|
|
1253
|
+
];
|
|
1254
|
+
/** This function gives the kind of an axis with these parameters (DOC.AXIS). */
|
|
1255
|
+
const axisKind = (fields) => ({
|
|
1256
|
+
verbs: { eval: "vex.axis" },
|
|
1257
|
+
parts: closed(fields, Object.keys(fields)),
|
|
1258
|
+
summary: "An axis of Vex: the targets of a body."
|
|
1259
|
+
});
|
|
1260
|
+
/** This function gives the kind of a move. Its document is a step of a path, and a computed key is its part (DOC.MOVE). */
|
|
1261
|
+
const move = (walker, parts) => ({
|
|
1262
|
+
verbs: {
|
|
1263
|
+
eval: "formula.step",
|
|
1264
|
+
step: walker
|
|
1265
|
+
},
|
|
1266
|
+
parts,
|
|
1267
|
+
summary: "A move of Vex: a step to another key of the space."
|
|
1268
|
+
});
|
|
1269
|
+
/** The scope of a loop: the names of `init`, for the steps, the test `until` and the result (LOOP.NAMES). */
|
|
1270
|
+
const LOOP_SCOPE = [{
|
|
1271
|
+
names: { keysAt: ["init"] },
|
|
1272
|
+
in: [
|
|
1273
|
+
["step", "*"],
|
|
1274
|
+
["until"],
|
|
1275
|
+
["result"]
|
|
1276
|
+
]
|
|
1277
|
+
}];
|
|
1278
|
+
/**
|
|
1279
|
+
* This function gives the world of Vex. The module uses it one time, and the annotation `__PURE__` lets a bundler
|
|
1280
|
+
* remove the world from a program that does not use it.
|
|
1281
|
+
*/
|
|
1282
|
+
function worldOfVex() {
|
|
1283
|
+
/** A binding of a `let`: a term, or a function (FN.BIND). */
|
|
1284
|
+
const BIND = {
|
|
1285
|
+
shape: "term",
|
|
1286
|
+
of: "lit",
|
|
1287
|
+
kinds: [...TERM_KINDS, ["fn"]]
|
|
1288
|
+
};
|
|
1289
|
+
/** The axes that `reverse` can hold: each axis but `where`, `reverse` and `table` (AXIS.REVERSE). */
|
|
1290
|
+
const BASE_AXES = [
|
|
1291
|
+
...PLAIN_AXES,
|
|
1292
|
+
"neighbors",
|
|
1293
|
+
"window",
|
|
1294
|
+
"list"
|
|
1295
|
+
];
|
|
1296
|
+
return {
|
|
1297
|
+
kinds: {
|
|
1298
|
+
lit: {
|
|
1299
|
+
verbs: { eval: "vex.lit" },
|
|
1300
|
+
summary: "A literal: its value in the JSON form of IR.JSON."
|
|
1301
|
+
},
|
|
1302
|
+
ref: {
|
|
1303
|
+
verbs: { eval: "formula.ref" },
|
|
1304
|
+
parts: { shape: "deep" },
|
|
1305
|
+
summary: "A reference: the fields of self after its moves, or a name."
|
|
1306
|
+
},
|
|
1307
|
+
record: {
|
|
1308
|
+
verbs: { eval: "formula.record" },
|
|
1309
|
+
parts: NAMES,
|
|
1310
|
+
summary: "A record of the values of its fields."
|
|
1311
|
+
},
|
|
1312
|
+
let: {
|
|
1313
|
+
verbs: { eval: "formula.let" },
|
|
1314
|
+
parts: closed({
|
|
1315
|
+
bind: {
|
|
1316
|
+
shape: "map",
|
|
1317
|
+
item: BIND
|
|
1318
|
+
},
|
|
1319
|
+
body: TERM
|
|
1320
|
+
}, ["bind", "body"]),
|
|
1321
|
+
scope: [{
|
|
1322
|
+
names: { keysAt: ["bind"] },
|
|
1323
|
+
in: [["body"]]
|
|
1324
|
+
}],
|
|
1325
|
+
summary: "Names for the body. A binding does not see the other bindings."
|
|
1326
|
+
},
|
|
1327
|
+
fn: {
|
|
1328
|
+
verbs: { eval: "vex.fn" },
|
|
1329
|
+
parts: closed({
|
|
1330
|
+
params: {
|
|
1331
|
+
shape: "list",
|
|
1332
|
+
item: { shape: "text" },
|
|
1333
|
+
distinct: true
|
|
1334
|
+
},
|
|
1335
|
+
body: TERM
|
|
1336
|
+
}, ["params", "body"]),
|
|
1337
|
+
scope: [{
|
|
1338
|
+
names: { listAt: ["params"] },
|
|
1339
|
+
in: [["body"]]
|
|
1340
|
+
}],
|
|
1341
|
+
summary: "A function of a let. Its body evaluates at the position of each call."
|
|
1342
|
+
},
|
|
1343
|
+
call: {
|
|
1344
|
+
verbs: { eval: "vex.call" },
|
|
1345
|
+
parts: closed({
|
|
1346
|
+
fn: {
|
|
1347
|
+
shape: "term",
|
|
1348
|
+
kinds: [["ref"]]
|
|
1349
|
+
},
|
|
1350
|
+
args: TERMS$1
|
|
1351
|
+
}, ["fn", "args"]),
|
|
1352
|
+
summary: "A call of a function of a let."
|
|
1353
|
+
},
|
|
1354
|
+
each: {
|
|
1355
|
+
verbs: { eval: "vex.each" },
|
|
1356
|
+
parts: closed({
|
|
1357
|
+
axis: AXIS,
|
|
1358
|
+
body: TERM,
|
|
1359
|
+
at: ADDRESS
|
|
1360
|
+
}, ["axis", "body"]),
|
|
1361
|
+
summary: "The body at each target of an axis: a list."
|
|
1362
|
+
},
|
|
1363
|
+
fold: {
|
|
1364
|
+
verbs: { eval: "vex.fold" },
|
|
1365
|
+
parts: closed({
|
|
1366
|
+
axis: AXIS,
|
|
1367
|
+
init: NAMES,
|
|
1368
|
+
step: NAMES,
|
|
1369
|
+
result: TERM,
|
|
1370
|
+
at: ADDRESS
|
|
1371
|
+
}, [
|
|
1372
|
+
"axis",
|
|
1373
|
+
"init",
|
|
1374
|
+
"step",
|
|
1375
|
+
"result"
|
|
1376
|
+
]),
|
|
1377
|
+
scope: LOOP_SCOPE,
|
|
1378
|
+
summary: "A step at each target of an axis, in order, and a result."
|
|
1379
|
+
},
|
|
1380
|
+
iterate: {
|
|
1381
|
+
verbs: { eval: "vex.iterate" },
|
|
1382
|
+
parts: closed({
|
|
1383
|
+
limit: {
|
|
1384
|
+
shape: "number",
|
|
1385
|
+
whole: true,
|
|
1386
|
+
min: 0,
|
|
1387
|
+
max: MAX_ITERATIONS
|
|
1388
|
+
},
|
|
1389
|
+
init: NAMES,
|
|
1390
|
+
step: NAMES,
|
|
1391
|
+
until: TERM,
|
|
1392
|
+
result: TERM
|
|
1393
|
+
}, [
|
|
1394
|
+
"limit",
|
|
1395
|
+
"init",
|
|
1396
|
+
"step",
|
|
1397
|
+
"result"
|
|
1398
|
+
]),
|
|
1399
|
+
scope: LOOP_SCOPE,
|
|
1400
|
+
summary: "Up to limit steps, until a test is true, and a result."
|
|
1401
|
+
},
|
|
1402
|
+
ops: {
|
|
1403
|
+
kinds: {},
|
|
1404
|
+
fallback: {
|
|
1405
|
+
verbs: { eval: "formula.op" },
|
|
1406
|
+
parts: TERMS$1,
|
|
1407
|
+
summary: "An op, applied to the values of its arguments."
|
|
1408
|
+
}
|
|
1409
|
+
},
|
|
1410
|
+
ext: {
|
|
1411
|
+
kinds: {},
|
|
1412
|
+
fallback: {
|
|
1413
|
+
verbs: { eval: "formula.ext" },
|
|
1414
|
+
summary: "An extension: its handler gives the value of its data."
|
|
1415
|
+
}
|
|
1416
|
+
},
|
|
1417
|
+
axes: { kinds: {
|
|
1418
|
+
...Object.fromEntries(PLAIN_AXES.map((name) => [name, axisKind({})])),
|
|
1419
|
+
neighbors: axisKind({ n: {
|
|
1420
|
+
shape: "one",
|
|
1421
|
+
values: [4, 8]
|
|
1422
|
+
} }),
|
|
1423
|
+
window: axisKind({
|
|
1424
|
+
from: { shape: "number" },
|
|
1425
|
+
to: { shape: "number" }
|
|
1426
|
+
}),
|
|
1427
|
+
table: axisKind({ name: { shape: "text" } }),
|
|
1428
|
+
list: axisKind({ list: TERM }),
|
|
1429
|
+
reverse: axisKind({ axis: {
|
|
1430
|
+
shape: "term",
|
|
1431
|
+
kinds: BASE_AXES.map((name) => ["axes", name])
|
|
1432
|
+
} }),
|
|
1433
|
+
where: axisKind({
|
|
1434
|
+
axis: AXIS,
|
|
1435
|
+
test: TERM
|
|
1436
|
+
})
|
|
1437
|
+
} },
|
|
1438
|
+
moves: { kinds: {
|
|
1439
|
+
key: move("vex.move.key", TERM),
|
|
1440
|
+
index: move("vex.move.index", TERM),
|
|
1441
|
+
offset: move("vex.move.offset", {
|
|
1442
|
+
shape: "list",
|
|
1443
|
+
item: { shape: "number" }
|
|
1444
|
+
}),
|
|
1445
|
+
other: move("vex.move.other", NO_VALUE),
|
|
1446
|
+
origin: move("vex.move.origin", NO_VALUE),
|
|
1447
|
+
parent: move("vex.move.parent", NO_VALUE)
|
|
1448
|
+
} }
|
|
1449
|
+
},
|
|
1450
|
+
defaults: {}
|
|
1451
|
+
};
|
|
1452
|
+
}
|
|
1453
|
+
/**
|
|
1454
|
+
* The world of Vex: the contract of each kind of a document of Vex, as data. A contract gives the shape of the parts,
|
|
1455
|
+
* the data of the fields and the names that the kind binds. The static kit of the kernel reads it.
|
|
1456
|
+
*/
|
|
1457
|
+
const VEX_WORLD = /* @__PURE__ */ worldOfVex();
|
|
1458
|
+
/** The deepest nesting of documents that the machine takes. A deeper document gives `#DEPTH!` (DOC.ERROR). */
|
|
1459
|
+
const MAX_DOCUMENT_DEPTH = 4096;
|
|
1460
|
+
/** The kind of a Vex error for each code of the kernel. Another code is a document that is not valid. */
|
|
1461
|
+
const KIND_OF = {
|
|
1462
|
+
"#REF!": "unknown-key",
|
|
1463
|
+
"#N/A": "missing-field",
|
|
1464
|
+
"#VALUE!": "kind-mismatch",
|
|
1465
|
+
"#NAME?": "unbound",
|
|
1466
|
+
"#NUM!": "not-finite",
|
|
1467
|
+
"#CALC!": "threw",
|
|
1468
|
+
"#ARGS": "args",
|
|
1469
|
+
"#CYCLE!": "cycle",
|
|
1470
|
+
"#DEPTH!": "depth"
|
|
1471
|
+
};
|
|
1472
|
+
/** This function gives the error of the kernel for a Vex error. The Vex error is its cause, so no detail is lost. */
|
|
1473
|
+
const toKernelError = (e) => ({
|
|
1474
|
+
code: e.code,
|
|
1475
|
+
reason: e.message,
|
|
1476
|
+
cause: e
|
|
1477
|
+
});
|
|
1478
|
+
/** This function gives the Vex error of an error of the kernel. A code that Vex does not have gives `#VALUE!`. */
|
|
1479
|
+
function toVexError(e) {
|
|
1480
|
+
if (isVexError(e.cause)) return e.cause;
|
|
1481
|
+
return vexError(KIND_OF[e.code] ?? "bad-expression", e.reason, e.cause === void 0 ? {} : { thrown: e.cause });
|
|
1482
|
+
}
|
|
1483
|
+
/** This function gives the Vex result of a result of the kernel. */
|
|
1484
|
+
const toVexResult = (r) => r.ok ? ok(r.value) : fail(toVexError(r.error));
|
|
1485
|
+
/** This function makes an error of the kernel from a kind of Vex error. */
|
|
1486
|
+
const kernelError = (kind, message, details = {}) => toKernelError(vexError(kind, message, details));
|
|
1487
|
+
/** This function makes a failed result of the kernel. */
|
|
1488
|
+
const failed = (kind, message) => ({
|
|
1489
|
+
ok: false,
|
|
1490
|
+
error: kernelError(kind, message)
|
|
1491
|
+
});
|
|
1492
|
+
/** This function stops host code with an error of the kernel. The kernel catches the `Failure` and gives its error. */
|
|
1493
|
+
const stop = (kind, message) => {
|
|
1494
|
+
throw new Failure(kernelError(kind, message));
|
|
1495
|
+
};
|
|
1496
|
+
/** This function tells if an error is `#DEPTH!`. Some rules do not hide it (EVAL.DEPTH). */
|
|
1497
|
+
const isDepth = (e) => e.code === "#DEPTH!";
|
|
1498
|
+
/** The segments that are not fields (REF.FORBIDDEN). */
|
|
1499
|
+
const FORBIDDEN = /* @__PURE__ */ new Set([
|
|
1500
|
+
"__proto__",
|
|
1501
|
+
"constructor",
|
|
1502
|
+
"prototype"
|
|
1503
|
+
]);
|
|
1504
|
+
/**
|
|
1505
|
+
* A position of an evaluation, as a host thing of the kernel: the space, the tables, the origin and the focus. It is the
|
|
1506
|
+
* root `self` of the context. A move gives a new position, and a field step reads the record at the focus. A move that
|
|
1507
|
+
* fails gives a position with the failure. The failure is the result when a reader uses the position, so the checks of
|
|
1508
|
+
* the computed keys come first (NAV.COMPUTED).
|
|
1509
|
+
*/
|
|
1510
|
+
var Place = class Place {
|
|
1511
|
+
space;
|
|
1512
|
+
tables;
|
|
1513
|
+
origin;
|
|
1514
|
+
focus;
|
|
1515
|
+
failure;
|
|
1516
|
+
constructor(space, tables, origin, focus, failure) {
|
|
1517
|
+
this[THING] = true;
|
|
1518
|
+
this.space = space;
|
|
1519
|
+
this.tables = tables;
|
|
1520
|
+
this.origin = origin;
|
|
1521
|
+
this.focus = focus;
|
|
1522
|
+
this.failure = failure;
|
|
1523
|
+
}
|
|
1524
|
+
/** The text of the position for a trace. */
|
|
1525
|
+
get label() {
|
|
1526
|
+
return `key ${this.focus}`;
|
|
1527
|
+
}
|
|
1528
|
+
/** This method gives the position of a key of the same space, with the same origin. */
|
|
1529
|
+
to(focus) {
|
|
1530
|
+
return new Place(this.space, this.tables, this.origin, focus);
|
|
1531
|
+
}
|
|
1532
|
+
/** This method gives the position with a failure, for a move that failed. */
|
|
1533
|
+
failed(failure) {
|
|
1534
|
+
return new Place(this.space, this.tables, this.origin, this.focus, failure);
|
|
1535
|
+
}
|
|
1536
|
+
/** This method reads one field of the record at the focus. */
|
|
1537
|
+
deref(key) {
|
|
1538
|
+
return this.resolve([key])?.thing;
|
|
1539
|
+
}
|
|
1540
|
+
/**
|
|
1541
|
+
* This method reads the fields of a path in the record at the focus (REF.PATH). A missing field, `null` and a segment
|
|
1542
|
+
* that is not a field reach nothing, and the kernel gives `#N/A` (REF.MISSING, REF.FORBIDDEN). A getter that throws
|
|
1543
|
+
* gives `#CALC!`, because the kernel catches the exception.
|
|
1544
|
+
*/
|
|
1545
|
+
resolve(keys) {
|
|
1546
|
+
if (this.failure !== void 0) throw new Failure(this.failure);
|
|
1547
|
+
let value = this.space.get(this.focus);
|
|
1548
|
+
if (value === void 0 || value === null) return void 0;
|
|
1549
|
+
for (const key of keys) {
|
|
1550
|
+
const seg = String(key);
|
|
1551
|
+
if (typeof value !== "object" && typeof value !== "function" || seg === "" || FORBIDDEN.has(seg)) return void 0;
|
|
1552
|
+
value = Reflect.get(value, seg);
|
|
1553
|
+
if (value === void 0 || value === null) return void 0;
|
|
1554
|
+
}
|
|
1555
|
+
return { thing: value };
|
|
1556
|
+
}
|
|
1557
|
+
/** This method gives the record at the focus: the value of a reference without fields. */
|
|
1558
|
+
toJson() {
|
|
1559
|
+
if (this.failure !== void 0) throw new Failure(this.failure);
|
|
1560
|
+
const record = this.space.get(this.focus);
|
|
1561
|
+
return record === void 0 || record === null ? stop("missing-field", `the record at "${this.focus}" has no value`) : record;
|
|
1562
|
+
}
|
|
1563
|
+
};
|
|
1564
|
+
/**
|
|
1565
|
+
* This function gives the root `self` of an evaluation in the kernel. It is the position at the origin of a space,
|
|
1566
|
+
* with the tables of the axis table (AXIS.TABLE).
|
|
1567
|
+
*/
|
|
1568
|
+
function vexSelf(space, origin, tables = {}) {
|
|
1569
|
+
return new Place(space, tables, origin, origin);
|
|
1570
|
+
}
|
|
1571
|
+
/**
|
|
1572
|
+
* This function gives the walker of a kind of move. `fix` gives the fixed move of the value of the step, or the reason
|
|
1573
|
+
* why the value is not valid, which gives `#VALUE!` (NAV.COMPUTED).
|
|
1574
|
+
*/
|
|
1575
|
+
function moveWalker(fix) {
|
|
1576
|
+
return ({ data, here }) => {
|
|
1577
|
+
if (!(here instanceof Place)) return {
|
|
1578
|
+
ok: false,
|
|
1579
|
+
reason: "a move starts at a key of a space",
|
|
1580
|
+
code: "#REF!"
|
|
1581
|
+
};
|
|
1582
|
+
const fixed = fix(data);
|
|
1583
|
+
if (typeof fixed === "string") return {
|
|
1584
|
+
ok: false,
|
|
1585
|
+
reason: fixed,
|
|
1586
|
+
code: "#VALUE!"
|
|
1587
|
+
};
|
|
1588
|
+
if (here.failure !== void 0) return {
|
|
1589
|
+
ok: true,
|
|
1590
|
+
thing: here
|
|
1591
|
+
};
|
|
1592
|
+
const r = applyMove(here.space, here, fixed);
|
|
1593
|
+
return {
|
|
1594
|
+
ok: true,
|
|
1595
|
+
thing: r.ok ? here.to(r.value) : here.failed(toKernelError(r.error))
|
|
1596
|
+
};
|
|
1597
|
+
};
|
|
1598
|
+
}
|
|
1599
|
+
/** The walkers of the moves of Vex. */
|
|
1600
|
+
const WALKERS = {
|
|
1601
|
+
"vex.move.key": /* @__PURE__ */ moveWalker((d) => typeof d === "string" ? {
|
|
1602
|
+
t: "key",
|
|
1603
|
+
key: d
|
|
1604
|
+
} : `the key of a move is a ${typeof d}, not a text`),
|
|
1605
|
+
"vex.move.index": /* @__PURE__ */ moveWalker((d) => typeof d === "number" ? {
|
|
1606
|
+
t: "index",
|
|
1607
|
+
i: d
|
|
1608
|
+
} : `the position of a move is a ${typeof d}, not a number`),
|
|
1609
|
+
"vex.move.offset": /* @__PURE__ */ moveWalker((d) => ({
|
|
1610
|
+
t: "offset",
|
|
1611
|
+
d
|
|
1612
|
+
})),
|
|
1613
|
+
"vex.move.other": /* @__PURE__ */ moveWalker(() => ({ t: "other" })),
|
|
1614
|
+
"vex.move.origin": /* @__PURE__ */ moveWalker(() => ({ t: "origin" })),
|
|
1615
|
+
"vex.move.parent": /* @__PURE__ */ moveWalker(() => ({ t: "parent" }))
|
|
1616
|
+
};
|
|
1617
|
+
/** This function gives the position of a target. In the space of the axis table, the origin is the target (AXIS.TABLE). */
|
|
1618
|
+
const placeOf = (axis, from, key) => axis.table === void 0 ? from.to(key) : new Place(from.space, from.tables, key, key);
|
|
1619
|
+
/** This function gives the parts of a record value, by name. */
|
|
1620
|
+
const partsOf$1 = (value) => value ?? {};
|
|
1621
|
+
/** This function gives the part of a field that the contract makes necessary. */
|
|
1622
|
+
const partAt = (value, name) => partsOf$1(value)[name];
|
|
1623
|
+
/** This function gives the position of a context. A context without a position gives an error. */
|
|
1624
|
+
function selfOf(ctx) {
|
|
1625
|
+
return ctx.self instanceof Place ? ctx.self : kernelError("unknown-key", "the context has no key of a space");
|
|
1626
|
+
}
|
|
1627
|
+
/** This function gives the position where an axis finds its targets: the key of the address, or the focus (AXIS.ADDRESS). */
|
|
1628
|
+
function startOf(at, ctx) {
|
|
1629
|
+
if (at === void 0) return selfOf(ctx);
|
|
1630
|
+
const r = at.evaluate();
|
|
1631
|
+
if (!r.ok) return r.error;
|
|
1632
|
+
const where = r.value;
|
|
1633
|
+
return where.failure ?? where;
|
|
1634
|
+
}
|
|
1635
|
+
/** This function gives the position in the space of the axis: the space of its table for the axis table (AXIS.TABLE). */
|
|
1636
|
+
function spaceOf(axis, from) {
|
|
1637
|
+
if (axis.table === void 0) return from;
|
|
1638
|
+
const space = Object.hasOwn(from.tables, axis.table) ? from.tables[axis.table] : void 0;
|
|
1639
|
+
return space === void 0 ? kernelError("no-table", `the evaluation has no table "${axis.table}"`) : new Place(space, from.tables, from.origin, from.focus);
|
|
1640
|
+
}
|
|
1641
|
+
/**
|
|
1642
|
+
* This function is the evaluator of the axes. An axis document gives a value that finds the targets from a position.
|
|
1643
|
+
* The list of `list` evaluates at that position, and the test of `where` at each target (AXIS.LIST, AXIS.WHERE).
|
|
1644
|
+
*/
|
|
1645
|
+
const axisEvaluator = ({ typePath, value, context }) => {
|
|
1646
|
+
const name = String(typePath[1]);
|
|
1647
|
+
const fields = partsOf$1(value);
|
|
1648
|
+
if (name === "table") {
|
|
1649
|
+
const table = value.name;
|
|
1650
|
+
return ok$1({
|
|
1651
|
+
table,
|
|
1652
|
+
targets: (from) => ok$1(from.space.keys.map((key) => ({ key })))
|
|
1653
|
+
});
|
|
1654
|
+
}
|
|
1655
|
+
if (name === "list") {
|
|
1656
|
+
const list = fields["list"];
|
|
1657
|
+
return ok$1({
|
|
1658
|
+
table: void 0,
|
|
1659
|
+
targets: (from) => {
|
|
1660
|
+
const r = list.evaluate({
|
|
1661
|
+
...context,
|
|
1662
|
+
self: from
|
|
1663
|
+
});
|
|
1664
|
+
if (!r.ok) return r;
|
|
1665
|
+
if (!isVexList(r.value)) return failed("kind-mismatch", `the axis "list" needs a list, not a ${describe(r.value)}`);
|
|
1666
|
+
return ok$1(r.value.items.map(({ key, result }) => result.ok ? { key } : {
|
|
1667
|
+
key,
|
|
1668
|
+
error: result.error
|
|
1669
|
+
}));
|
|
1670
|
+
}
|
|
1671
|
+
});
|
|
1672
|
+
}
|
|
1673
|
+
if (name === "reverse" || name === "where") {
|
|
1674
|
+
const r = fields["axis"].evaluate();
|
|
1675
|
+
if (!r.ok) return r;
|
|
1676
|
+
const base = r.value;
|
|
1677
|
+
const test = fields["test"];
|
|
1678
|
+
if (test === void 0) return ok$1({
|
|
1679
|
+
table: base.table,
|
|
1680
|
+
targets: (from) => {
|
|
1681
|
+
const ts = base.targets(from);
|
|
1682
|
+
return ts.ok ? ok$1(ts.value.toReversed()) : ts;
|
|
1683
|
+
}
|
|
1684
|
+
});
|
|
1685
|
+
return ok$1({
|
|
1686
|
+
table: base.table,
|
|
1687
|
+
targets: (from) => whereTargets(base, test, context, from)
|
|
1688
|
+
});
|
|
1689
|
+
}
|
|
1690
|
+
const ir = {
|
|
1691
|
+
t: name,
|
|
1692
|
+
...value
|
|
1693
|
+
};
|
|
1694
|
+
return ok$1({
|
|
1695
|
+
table: void 0,
|
|
1696
|
+
targets: (from) => {
|
|
1697
|
+
const keys = axisTargets(from.space, from, ir);
|
|
1698
|
+
return keys.ok ? ok$1(keys.value.map((key) => ({ key }))) : {
|
|
1699
|
+
ok: false,
|
|
1700
|
+
error: toKernelError(keys.error)
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1703
|
+
});
|
|
1704
|
+
};
|
|
1705
|
+
/** This function keeps the targets where the test gives `true`. A failed test, or a value that is not a boolean, gives an error item. */
|
|
1706
|
+
function whereTargets(base, test, ctx, from) {
|
|
1707
|
+
const ts = base.targets(from);
|
|
1708
|
+
if (!ts.ok) return ts;
|
|
1709
|
+
const out = [];
|
|
1710
|
+
for (const t of ts.value) {
|
|
1711
|
+
if (t.error !== void 0) {
|
|
1712
|
+
out.push(t);
|
|
1713
|
+
continue;
|
|
1714
|
+
}
|
|
1715
|
+
const r = test.evaluate({
|
|
1716
|
+
...ctx,
|
|
1717
|
+
self: placeOf(base, from, t.key)
|
|
1718
|
+
});
|
|
1719
|
+
if (!r.ok) out.push({
|
|
1720
|
+
key: t.key,
|
|
1721
|
+
error: toVexError(r.error)
|
|
1722
|
+
});
|
|
1723
|
+
else if (r.value === true) out.push({ key: t.key });
|
|
1724
|
+
else if (r.value !== false) out.push({
|
|
1725
|
+
key: t.key,
|
|
1726
|
+
error: vexError("kind-mismatch", `the test of "where" gave a ${typeof r.value}, not a boolean`)
|
|
1727
|
+
});
|
|
1728
|
+
}
|
|
1729
|
+
return ok$1(out);
|
|
1730
|
+
}
|
|
1731
|
+
/** This function finds the targets of the axis of a loop, from the address or the focus, in the space of the axis. */
|
|
1732
|
+
function targetsOf(value, ctx) {
|
|
1733
|
+
const start = startOf(partsOf$1(value)["at"], ctx);
|
|
1734
|
+
if (!(start instanceof Place)) return {
|
|
1735
|
+
ok: false,
|
|
1736
|
+
error: start
|
|
1737
|
+
};
|
|
1738
|
+
const a = partAt(value, "axis").evaluate();
|
|
1739
|
+
if (!a.ok) return a;
|
|
1740
|
+
const axis = a.value;
|
|
1741
|
+
const from = spaceOf(axis, start);
|
|
1742
|
+
if (!(from instanceof Place)) return {
|
|
1743
|
+
ok: false,
|
|
1744
|
+
error: from
|
|
1745
|
+
};
|
|
1746
|
+
const ts = axis.targets(from);
|
|
1747
|
+
return ts.ok ? ok$1({
|
|
1748
|
+
axis,
|
|
1749
|
+
from,
|
|
1750
|
+
targets: ts.value
|
|
1751
|
+
}) : ts;
|
|
1752
|
+
}
|
|
1753
|
+
/**
|
|
1754
|
+
* This function sets a name of a new record of names, also the name `__proto__`. The record is new and gets each
|
|
1755
|
+
* name one time, so the property needs no other attributes. A later record copies it with a spread, so it is enumerable.
|
|
1756
|
+
*/
|
|
1757
|
+
function setName(names, name, value) {
|
|
1758
|
+
Object.defineProperty(names, name, {
|
|
1759
|
+
value,
|
|
1760
|
+
enumerable: true
|
|
1761
|
+
});
|
|
1762
|
+
}
|
|
1763
|
+
/**
|
|
1764
|
+
* This function evaluates the parts of a record of names in order (LOOP.NAMES). It gives the names with their new
|
|
1765
|
+
* values, or the first error. Each value reads the names before the step, so the names change only after all values.
|
|
1766
|
+
*/
|
|
1767
|
+
function bindAll(record, ctx, names) {
|
|
1768
|
+
const values = [];
|
|
1769
|
+
for (const [name, part] of Object.entries(partsOf$1(record))) {
|
|
1770
|
+
const r = part.evaluate(ctx);
|
|
1771
|
+
if (!r.ok) return r;
|
|
1772
|
+
values.push([name, r.value]);
|
|
1773
|
+
}
|
|
1774
|
+
const out = { ...names };
|
|
1775
|
+
for (const [name, v] of values) setName(out, name, v);
|
|
1776
|
+
return ok$1(out);
|
|
1777
|
+
}
|
|
1778
|
+
/** The evaluators of the loops: each, fold and iterate (LOOP.NAMES, FOLD.ORDER, ITERATE.LIMIT). */
|
|
1779
|
+
const LOOPS = {
|
|
1780
|
+
"vex.each": ({ value, context }) => {
|
|
1781
|
+
const found = targetsOf(value, context);
|
|
1782
|
+
if (!found.ok) return found;
|
|
1783
|
+
const { axis, from, targets } = found.value;
|
|
1784
|
+
const body = partAt(value, "body");
|
|
1785
|
+
const items = targets.map(({ key, error }) => ({
|
|
1786
|
+
key,
|
|
1787
|
+
result: error === void 0 ? toVexResult(body.evaluate({
|
|
1788
|
+
...context,
|
|
1789
|
+
self: placeOf(axis, from, key)
|
|
1790
|
+
})) : fail(error)
|
|
1791
|
+
}));
|
|
1792
|
+
return ok$1(vexList(items));
|
|
1793
|
+
},
|
|
1794
|
+
"vex.fold": ({ value, context }) => {
|
|
1795
|
+
const { init, step } = partsOf$1(value);
|
|
1796
|
+
const first = bindAll(init, context, context.names);
|
|
1797
|
+
if (!first.ok) return first;
|
|
1798
|
+
let names = first.value;
|
|
1799
|
+
const found = targetsOf(value, context);
|
|
1800
|
+
if (!found.ok) return found;
|
|
1801
|
+
const { axis, from, targets } = found.value;
|
|
1802
|
+
for (const { key, error } of targets) {
|
|
1803
|
+
if (error !== void 0) return {
|
|
1804
|
+
ok: false,
|
|
1805
|
+
error: toKernelError(error)
|
|
1806
|
+
};
|
|
1807
|
+
const next = bindAll(step, {
|
|
1808
|
+
...context,
|
|
1809
|
+
self: placeOf(axis, from, key),
|
|
1810
|
+
names
|
|
1811
|
+
}, names);
|
|
1812
|
+
if (!next.ok) return next;
|
|
1813
|
+
names = next.value;
|
|
1814
|
+
}
|
|
1815
|
+
return partAt(value, "result").evaluate({
|
|
1816
|
+
...context,
|
|
1817
|
+
names
|
|
1818
|
+
});
|
|
1819
|
+
},
|
|
1820
|
+
"vex.iterate": ({ value, context }) => {
|
|
1821
|
+
const { init, step, until } = partsOf$1(value);
|
|
1822
|
+
const limit = value.limit;
|
|
1823
|
+
const first = bindAll(init, context, context.names);
|
|
1824
|
+
if (!first.ok) return first;
|
|
1825
|
+
let names = first.value;
|
|
1826
|
+
for (let k = 0;; k++) {
|
|
1827
|
+
const here = {
|
|
1828
|
+
...context,
|
|
1829
|
+
names
|
|
1830
|
+
};
|
|
1831
|
+
if (until !== void 0) {
|
|
1832
|
+
const t = until.evaluate(here);
|
|
1833
|
+
if (!t.ok) return t;
|
|
1834
|
+
if (t.value === true) break;
|
|
1835
|
+
if (t.value !== false) return failed("kind-mismatch", `the test "until" of "iterate" gave a ${typeof t.value}, not a boolean`);
|
|
1836
|
+
}
|
|
1837
|
+
if (k === limit) {
|
|
1838
|
+
if (until !== void 0) return failed("limit", `"iterate" made its ${String(limit)} steps, and its test "until" is not true`);
|
|
1839
|
+
break;
|
|
1840
|
+
}
|
|
1841
|
+
const next = bindAll(step, here, names);
|
|
1842
|
+
if (!next.ok) return next;
|
|
1843
|
+
names = next.value;
|
|
1844
|
+
}
|
|
1845
|
+
return partAt(value, "result").evaluate({
|
|
1846
|
+
...context,
|
|
1847
|
+
names
|
|
1848
|
+
});
|
|
1849
|
+
}
|
|
1850
|
+
};
|
|
1851
|
+
/**
|
|
1852
|
+
* A function of a `let` (FN.BIND). A reference to its name is an error, because a function is not a value (FN.VALUE).
|
|
1853
|
+
* It keeps the names of the place of its binding, and its body evaluates at the position of each call (FN.CALL).
|
|
1854
|
+
*/
|
|
1855
|
+
var Bound = class extends Held {
|
|
1856
|
+
params;
|
|
1857
|
+
body;
|
|
1858
|
+
names;
|
|
1859
|
+
constructor(params, body, names) {
|
|
1860
|
+
super(kernelError("kind-mismatch", "the name is a function, not a value"));
|
|
1861
|
+
this.params = params;
|
|
1862
|
+
this.body = body;
|
|
1863
|
+
this.names = names;
|
|
1864
|
+
}
|
|
1865
|
+
};
|
|
1866
|
+
/** The evaluators of the functions of Vex (FN.BIND, FN.CALL, FN.ARGS). */
|
|
1867
|
+
const FUNCTIONS = {
|
|
1868
|
+
"vex.fn": ({ value, context }) => ok$1(new Bound(value.params, partAt(value, "body"), context.names)),
|
|
1869
|
+
"vex.call": ({ doc, value, context }) => {
|
|
1870
|
+
const name = String(doc.value.fn.value[1]);
|
|
1871
|
+
const args = partsOf$1(value)["args"];
|
|
1872
|
+
if (!Object.hasOwn(context.names, name)) return failed("unbound", `the function "${name}" has no binding`);
|
|
1873
|
+
const bound = context.names[name];
|
|
1874
|
+
if (!(bound instanceof Bound)) return bound instanceof Held ? {
|
|
1875
|
+
ok: false,
|
|
1876
|
+
error: bound.error
|
|
1877
|
+
} : failed("kind-mismatch", `the name "${name}" is a value, not a function`);
|
|
1878
|
+
if (bound.params.length !== args.length) return failed("bad-expression", `the function "${name}" takes ${String(bound.params.length)} arguments, not ${String(args.length)}`);
|
|
1879
|
+
const names = { ...bound.names };
|
|
1880
|
+
bound.params.forEach((p, i) => {
|
|
1881
|
+
const r = args[i].evaluate();
|
|
1882
|
+
setName(names, p, r.ok ? r.value : new Held(r.error));
|
|
1883
|
+
});
|
|
1884
|
+
return bound.body.evaluate({
|
|
1885
|
+
...context,
|
|
1886
|
+
names
|
|
1887
|
+
});
|
|
1888
|
+
}
|
|
1889
|
+
};
|
|
1890
|
+
/** This function gives a short name for the type of a value, for a message. */
|
|
1891
|
+
function describe(u) {
|
|
1892
|
+
if (u === null) return "null";
|
|
1893
|
+
if (Array.isArray(u)) return "array";
|
|
1894
|
+
if (isVexList(u)) return "list";
|
|
1895
|
+
return typeof u;
|
|
1896
|
+
}
|
|
1897
|
+
/** This function applies the test of a domain to a value. A test that throws rejects the value. */
|
|
1898
|
+
function accepts(d, x) {
|
|
1899
|
+
let accepted = false;
|
|
1900
|
+
try {
|
|
1901
|
+
accepted = d.is(x);
|
|
1902
|
+
} catch {}
|
|
1903
|
+
return accepted;
|
|
1904
|
+
}
|
|
1905
|
+
/** This function finds an own method of a receiver or of its prototypes, but not of `Object` or `Function` (CALL.METHOD). */
|
|
1906
|
+
function methodOf(self, name) {
|
|
1907
|
+
if (FORBIDDEN.has(name) || typeof self !== "object" && typeof self !== "function") return void 0;
|
|
1908
|
+
for (let o = self; o !== null && o !== Object.prototype && o !== Function.prototype; o = Object.getPrototypeOf(o)) {
|
|
1909
|
+
const d = Object.getOwnPropertyDescriptor(o, name);
|
|
1910
|
+
if (d !== void 0) return typeof d.value === "function" ? d.value : void 0;
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
/** This function checks the value of an op (CALL.RESULT): a value, a finite number, and a valid value of its domain. */
|
|
1914
|
+
function checked(x, op, domains) {
|
|
1915
|
+
if (x === void 0 || x === null) return stop("undefined-result", `"${op}" gave no value`);
|
|
1916
|
+
if (typeof x === "number" && !Number.isFinite(x)) return stop("not-finite", `"${op}" gave ${String(x)}`);
|
|
1917
|
+
const d = domains.find((dom) => accepts(dom, x));
|
|
1918
|
+
if (d?.valid !== void 0 && !d.valid(x)) return stop("invalid-value", `"${op}" gave a value that is not a valid ${d.name}`);
|
|
1919
|
+
return x;
|
|
1920
|
+
}
|
|
1921
|
+
/** This function finds the op of a domain for a receiver, or `undefined` (CALL.UNKNOWN-OP). */
|
|
1922
|
+
function opOf(d, self, op) {
|
|
1923
|
+
const spec = Object.hasOwn(d.ops, op) && !FORBIDDEN.has(op) ? d.ops[op] : void 0;
|
|
1924
|
+
const method = spec !== void 0 || d.methods === "all" ? methodOf(self, op) : void 0;
|
|
1925
|
+
return spec?.fn === void 0 && method === void 0 ? void 0 : {
|
|
1926
|
+
domain: d,
|
|
1927
|
+
spec,
|
|
1928
|
+
method
|
|
1929
|
+
};
|
|
1930
|
+
}
|
|
1931
|
+
/** This function applies the op of a domain: it lifts the numbers, checks the kinds of the arguments and checks the value. */
|
|
1932
|
+
function callFound(found, op, self, args, domains) {
|
|
1933
|
+
const { domain, spec } = found;
|
|
1934
|
+
const lift = spec?.liftScalar;
|
|
1935
|
+
const from = domain.fromScalar;
|
|
1936
|
+
const rest = from === void 0 ? args : args.map((a, i) => typeof a === "number" && (lift === true || Array.isArray(lift) && lift[i] === true) ? from(a) : a);
|
|
1937
|
+
const params = spec?.params;
|
|
1938
|
+
if (params !== void 0) rest.forEach((a, i) => {
|
|
1939
|
+
const k = params[i] ?? "any";
|
|
1940
|
+
if (k !== "any" && !(k === "domain" ? accepts(domain, a) : typeof a === k)) stop(k === "domain" ? "not-instance" : "kind-mismatch", `argument ${String(i + 1)} of "${op}" is a ${describe(a)}, not a ${k === "domain" ? domain.name : k}`);
|
|
1941
|
+
});
|
|
1942
|
+
return checked(spec?.fn !== void 0 ? spec.fn(self, ...rest) : found.method.call(self, ...rest), op, domains);
|
|
1943
|
+
}
|
|
1944
|
+
/** The list ops with a second argument before their options: a count, a position, an op name or a list. */
|
|
1945
|
+
const WITH_ARGUMENT$1 = /* @__PURE__ */ new Set([
|
|
1946
|
+
"reduce",
|
|
1947
|
+
"take",
|
|
1948
|
+
"nth",
|
|
1949
|
+
"drop",
|
|
1950
|
+
"append",
|
|
1951
|
+
"without"
|
|
1952
|
+
]);
|
|
1953
|
+
/**
|
|
1954
|
+
* This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
|
|
1955
|
+
* error comes first (EVAL.DEPTH). With `strict`, the first error item is the result.
|
|
1956
|
+
*/
|
|
1957
|
+
function goodOf(items, strict) {
|
|
1958
|
+
const depth = items.find((it) => !it.result.ok && it.result.error.code === "#DEPTH!");
|
|
1959
|
+
if (depth !== void 0 && !depth.result.ok) throw new Failure(toKernelError(depth.result.error));
|
|
1960
|
+
const firstBad = items.find((it) => !it.result.ok);
|
|
1961
|
+
if (strict && firstBad !== void 0 && !firstBad.result.ok) throw new Failure(toKernelError(firstBad.result.error));
|
|
1962
|
+
return items.filter((it) => it.result.ok);
|
|
1963
|
+
}
|
|
1964
|
+
/** This function gives the items of the list `other` of `append` and `without` (LIST.BUILD). Another value gives `#VALUE!`. */
|
|
1965
|
+
function otherOf(op, other, strict) {
|
|
1966
|
+
return isVexList(other) ? goodOf(other.items, strict) : stop("kind-mismatch", `"${op}" needs a list as its second argument, not a ${describe(other)}`);
|
|
1967
|
+
}
|
|
1968
|
+
/** This function joins the lists that are the values of the items, in order (LIST.FLAT). Each list follows LIST.BUILD. */
|
|
1969
|
+
function flat(vs, strict) {
|
|
1970
|
+
const out = [];
|
|
1971
|
+
for (const inner of vs) {
|
|
1972
|
+
if (!isVexList(inner)) return stop("kind-mismatch", `"flat" needs a list as the value of each item, but an item is a ${describe(inner)}`);
|
|
1973
|
+
out.push(...goodOf(inner.items, strict));
|
|
1974
|
+
}
|
|
1975
|
+
return vexList(out);
|
|
1976
|
+
}
|
|
1977
|
+
/** This function applies a list op (LIST.*). The error items are skipped, except with `{ strict: true }`, and except `#DEPTH!`. */
|
|
1978
|
+
function listOp(op, items, rest, domains) {
|
|
1979
|
+
const o = WITH_ARGUMENT$1.has(op) ? rest[1] : rest[0];
|
|
1980
|
+
const strict = o?.strict === true;
|
|
1981
|
+
const good = goodOf(items, strict);
|
|
1982
|
+
const vs = good.map((it) => it.result.ok ? it.result.value : void 0);
|
|
1983
|
+
const nums = () => vs.every((x) => typeof x === "number") ? vs : stop("kind-mismatch", `"${op}" needs numbers`);
|
|
1984
|
+
const bools = () => vs.every((x) => typeof x === "boolean") ? vs : stop("kind-mismatch", `"${op}" needs booleans`);
|
|
1985
|
+
const empty = () => stop("empty", `"${op}" has no values to work with`);
|
|
1986
|
+
switch (op) {
|
|
1987
|
+
case "count": return vs.length;
|
|
1988
|
+
case "values": return Object.freeze([...vs]);
|
|
1989
|
+
case "keys": return Object.freeze(good.map((it) => it.key));
|
|
1990
|
+
case "errors": return Object.freeze(items.flatMap((it) => it.result.ok ? [] : [it.result.error]));
|
|
1991
|
+
case "first": return vs.length > 0 ? vs[0] : empty();
|
|
1992
|
+
case "sortBy": {
|
|
1993
|
+
const kind = typeof vs[0];
|
|
1994
|
+
if (vs.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");
|
|
1995
|
+
return vexList(good.map((it, i) => ({
|
|
1996
|
+
it,
|
|
1997
|
+
i
|
|
1998
|
+
})).toSorted((a, b) => compare(vs[a.i], vs[b.i])).map(({ it }) => it));
|
|
1999
|
+
}
|
|
2000
|
+
case "take":
|
|
2001
|
+
case "drop":
|
|
2002
|
+
case "nth": {
|
|
2003
|
+
const n = rest[0];
|
|
2004
|
+
if (!Number.isSafeInteger(n) || n < 0) return stop("kind-mismatch", `"${op}" needs a whole number from 0`);
|
|
2005
|
+
if (op === "take") return vexList(good.slice(0, n));
|
|
2006
|
+
if (op === "drop") return vexList(good.slice(n));
|
|
2007
|
+
return n < vs.length ? vs[n] : stop("empty", `"nth" has no item at the position ${String(n)}`);
|
|
2008
|
+
}
|
|
2009
|
+
case "append": return vexList([...good, ...otherOf(op, rest[0], strict)]);
|
|
2010
|
+
case "without": {
|
|
2011
|
+
const gone = new Set(otherOf(op, rest[0], strict).map((it) => it.key));
|
|
2012
|
+
return vexList(good.filter((it) => !gone.has(it.key)));
|
|
2013
|
+
}
|
|
2014
|
+
case "flat": return flat(vs, strict);
|
|
2015
|
+
case "sum": return checked(nums().reduce((s, x) => s + x, 0), op, []);
|
|
2016
|
+
case "mean": {
|
|
2017
|
+
const n = nums();
|
|
2018
|
+
return n.length === 0 ? empty() : checked(n.reduce((s, x) => s + x, 0) / n.length, op, []);
|
|
2019
|
+
}
|
|
2020
|
+
case "min":
|
|
2021
|
+
case "max": {
|
|
2022
|
+
const n = nums();
|
|
2023
|
+
return n.length === 0 ? empty() : n.reduce((best, x) => op === "min" ? Math.min(best, x) : Math.max(best, x));
|
|
2024
|
+
}
|
|
2025
|
+
case "any": return bools().some(Boolean);
|
|
2026
|
+
case "all": return bools().every(Boolean);
|
|
2027
|
+
case "none": return !bools().some(Boolean);
|
|
2028
|
+
default: return reduce(rest[0], vs, o, domains);
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
/** This function compares two numbers or two texts for `sortBy`. Equal values keep their order (LIST.SORT). */
|
|
2032
|
+
const compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
2033
|
+
/** This function folds the values of a list with a domain op, from the left (LIST.REDUCE, LIST.REDUCE.EMPTY). */
|
|
2034
|
+
function reduce(name, vs, o, domains) {
|
|
2035
|
+
if (typeof name !== "string") return stop("kind-mismatch", "\"reduce\" needs an op name");
|
|
2036
|
+
if (vs.length === 0) {
|
|
2037
|
+
const named = o?.domain;
|
|
2038
|
+
if (named !== void 0 && !domains.some((d) => d.name === named)) return stop("unknown-op", `"reduce" names the domain ${JSON.stringify(named) ?? "?"}, but the program has no such domain`);
|
|
2039
|
+
const ds = domains.filter((d) => Object.hasOwn(d.ops, name) && d.ops[name].identity !== void 0 && (named === void 0 || d.name === named));
|
|
2040
|
+
const only = ds.length === 1 ? ds[0]?.ops[name] : void 0;
|
|
2041
|
+
return only === void 0 ? stop("empty", `"reduce" with "${name}" has no values, and no one identity`) : only.identity();
|
|
2042
|
+
}
|
|
2043
|
+
let acc = vs[0];
|
|
2044
|
+
for (const x of vs.slice(1)) {
|
|
2045
|
+
const found = domains.filter((d) => accepts(d, acc)).map((d) => opOf(d, acc, name)).find((f) => f !== void 0);
|
|
2046
|
+
if (found === void 0) return stop("unknown-op", `no domain gives "${name}" for a ${describe(acc)}`);
|
|
2047
|
+
acc = callFound(found, name, acc, [x], domains);
|
|
2048
|
+
}
|
|
2049
|
+
return acc;
|
|
2050
|
+
}
|
|
2051
|
+
/**
|
|
2052
|
+
* This function gives the op of a name: the dispatch of Vex at run time. A list op applies to a list. Else the first
|
|
2053
|
+
* domain that accepts the receiver and has the op applies it. Else a free function applies (CALL.FREE).
|
|
2054
|
+
*/
|
|
2055
|
+
function dispatch(op, domains, fns) {
|
|
2056
|
+
const free = Object.hasOwn(fns, op) ? fns[op] : void 0;
|
|
2057
|
+
const listed = LIST_OPS.has(op);
|
|
2058
|
+
return (...values) => {
|
|
2059
|
+
const self = values[0];
|
|
2060
|
+
if (listed && isVexList(self)) return listOp(op, self.items, values.slice(1), domains);
|
|
2061
|
+
let accepted = false;
|
|
2062
|
+
if (values.length > 0) for (const d of domains) {
|
|
2063
|
+
if (!accepts(d, self)) continue;
|
|
2064
|
+
accepted = true;
|
|
2065
|
+
const found = opOf(d, self, op);
|
|
2066
|
+
if (found !== void 0) return callFound(found, op, self, values.slice(1), domains);
|
|
2067
|
+
}
|
|
2068
|
+
if (free !== void 0) return checked(free(...values), op, domains);
|
|
2069
|
+
if (accepted || values.length === 0) return stop("unknown-op", accepted ? `the domain of the receiver has no op "${op}"` : `there is no function "${op}"`);
|
|
2070
|
+
return stop("not-instance", `no domain accepts the receiver of "${op}" (${describe(self)})`);
|
|
2071
|
+
};
|
|
2072
|
+
}
|
|
2073
|
+
/** This function gives an argument of a special form. A missing argument gives `#VALUE!`. */
|
|
2074
|
+
const argOf = (args, i, op) => args[i]?.() ?? failed("bad-expression", `"${op}" needs argument ${String(i + 1)}`);
|
|
2075
|
+
/** This function gives an argument of a special form that must be a boolean (FORM.IF, FORM.AND-OR). */
|
|
2076
|
+
function boolOf(args, i, op) {
|
|
2077
|
+
const r = argOf(args, i, op);
|
|
2078
|
+
return !r.ok || typeof r.value === "boolean" ? r : failed("kind-mismatch", `argument ${String(i + 1)} of "${op}" is a ${describe(r.value)}, not a boolean`);
|
|
2079
|
+
}
|
|
2080
|
+
/** This function gives `and` or `or`: they stop at the first argument that decides (FORM.AND-OR). */
|
|
2081
|
+
const logic = (op) => (args) => {
|
|
2082
|
+
if (args.length === 0) return failed("bad-expression", `"${op}" needs at least 1 argument`);
|
|
2083
|
+
for (let i = 0; i < args.length; i++) {
|
|
2084
|
+
const b = boolOf(args, i, op);
|
|
2085
|
+
if (!b.ok || b.value === (op === "or")) return b;
|
|
2086
|
+
}
|
|
2087
|
+
return ok$1(op === "and");
|
|
2088
|
+
};
|
|
2089
|
+
/** The special forms of Vex: lazy ops of the formula kit (FORM.IF, FORM.AND-OR, FORM.IFERROR). */
|
|
2090
|
+
const FORMS = {
|
|
2091
|
+
if: (args) => {
|
|
2092
|
+
const c = boolOf(args, 0, "if");
|
|
2093
|
+
return c.ok ? argOf(args, c.value === true ? 1 : 2, "if") : c;
|
|
2094
|
+
},
|
|
2095
|
+
and: /* @__PURE__ */ logic("and"),
|
|
2096
|
+
or: /* @__PURE__ */ logic("or"),
|
|
2097
|
+
ifError: (args) => {
|
|
2098
|
+
const first = argOf(args, 0, "ifError");
|
|
2099
|
+
return first.ok || isDepth(first.error) ? first : argOf(args, 1, "ifError");
|
|
2100
|
+
}
|
|
2101
|
+
};
|
|
2102
|
+
/**
|
|
2103
|
+
* This function gives the profile of Vex for the formula kit: the dispatch of the ops, the special forms and the
|
|
2104
|
+
* extensions. Two or more failed arguments or fields give `#ARGS` (CALL.ARGS, REC.FIELDS).
|
|
2105
|
+
*/
|
|
2106
|
+
function profileOf(opts) {
|
|
2107
|
+
const domains = opts.domains ?? [];
|
|
2108
|
+
const fns = opts.fns ?? {};
|
|
2109
|
+
const extensions = opts.extensions ?? {};
|
|
2110
|
+
const ext = Object.create(null);
|
|
2111
|
+
for (const name of Object.keys(STD_EXT)) ext[name] = () => failed("unknown-op", `no handler for the extension kind "${name}"`);
|
|
2112
|
+
for (const [name, handler] of Object.entries(extensions)) ext[name] = (data, ctx) => {
|
|
2113
|
+
const self = selfOf(ctx);
|
|
2114
|
+
if (!(self instanceof Place)) return {
|
|
2115
|
+
ok: false,
|
|
2116
|
+
error: self
|
|
2117
|
+
};
|
|
2118
|
+
const r = handler(data, {
|
|
2119
|
+
space: self.space,
|
|
2120
|
+
position: {
|
|
2121
|
+
origin: self.origin,
|
|
2122
|
+
focus: self.focus
|
|
2123
|
+
},
|
|
2124
|
+
path: []
|
|
2125
|
+
});
|
|
2126
|
+
if (r.ok) return r;
|
|
2127
|
+
return {
|
|
2128
|
+
ok: false,
|
|
2129
|
+
error: toKernelError(r.error.origin === void 0 ? {
|
|
2130
|
+
...r.error,
|
|
2131
|
+
origin: self.origin,
|
|
2132
|
+
focus: self.focus
|
|
2133
|
+
} : r.error)
|
|
2134
|
+
};
|
|
2135
|
+
};
|
|
2136
|
+
return {
|
|
2137
|
+
ops: (name) => dispatch(name, domains, fns),
|
|
2138
|
+
lazy: FORMS,
|
|
2139
|
+
ext,
|
|
2140
|
+
combine: (errors) => errors.find(isDepth) ?? kernelError("args", `${String(errors.length)} arguments failed`, { causes: errors.map(toVexError) })
|
|
2141
|
+
};
|
|
2142
|
+
}
|
|
2143
|
+
/** The evaluators of the literals: the JSON form of IR.JSON, and a value of the host (DOC.LITERAL). */
|
|
2144
|
+
function literals(domains) {
|
|
2145
|
+
return {
|
|
2146
|
+
"vex.lit": ({ doc }) => {
|
|
2147
|
+
const decoded = decodeValue(doc.value, domains);
|
|
2148
|
+
return decoded.ok ? ok$1(decoded.value) : {
|
|
2149
|
+
ok: false,
|
|
2150
|
+
error: toKernelError(decoded.error)
|
|
2151
|
+
};
|
|
2152
|
+
},
|
|
2153
|
+
"vex.host": ({ doc }) => ok$1(doc.value)
|
|
2154
|
+
};
|
|
2155
|
+
}
|
|
2156
|
+
/**
|
|
2157
|
+
* The rules of the machines of Vex. The values of a space are data, so a field that has the shape of a reference stays
|
|
2158
|
+
* a value. A reference starts at `self` or `names`, and a document has a limit of nesting.
|
|
2159
|
+
*/
|
|
2160
|
+
const RULES = {
|
|
2161
|
+
plain: true,
|
|
2162
|
+
roots: ["self", "names"],
|
|
2163
|
+
maxDepth: MAX_DOCUMENT_DEPTH
|
|
2164
|
+
};
|
|
2165
|
+
/**
|
|
2166
|
+
* This function makes the machine of the kernel for Vex: the world of Vex, and the formula kit with the profile of
|
|
2167
|
+
* Vex. Vex adds its evaluators and its walkers. It is the reference tier of Vex (EVAL.REFERENCE).
|
|
2168
|
+
*/
|
|
2169
|
+
function vexMachine(opts = {}) {
|
|
2170
|
+
return {
|
|
2171
|
+
world: VEX_WORLD,
|
|
2172
|
+
evaluators: {
|
|
2173
|
+
...formulaEvaluators(profileOf(opts)),
|
|
2174
|
+
...literals(opts.domains ?? []),
|
|
2175
|
+
...LOOPS,
|
|
2176
|
+
...FUNCTIONS,
|
|
2177
|
+
"vex.axis": axisEvaluator
|
|
2178
|
+
},
|
|
2179
|
+
walkers: WALKERS,
|
|
2180
|
+
...RULES
|
|
2181
|
+
};
|
|
2182
|
+
}
|
|
2183
|
+
/** The code of each evaluator and walker of the machine of the checks. A check uses only their names. */
|
|
2184
|
+
const NO_CODE = () => stop("bad-expression", "the machine of the checks evaluates nothing");
|
|
2185
|
+
/**
|
|
2186
|
+
* This function makes the machine of the checks (DOC.CHECK). It has the world of Vex, and the names of the evaluators
|
|
2187
|
+
* and the walkers of `vexMachine` without their code. The static kit of the kernel needs only the names. Thus a
|
|
2188
|
+
* program that only reads documents does not hold the evaluators.
|
|
2189
|
+
*/
|
|
2190
|
+
function vexChecker() {
|
|
2191
|
+
const evaluators = {};
|
|
2192
|
+
const walkers = {};
|
|
2193
|
+
const todo = Object.values(VEX_WORLD.kinds);
|
|
2194
|
+
for (let x = todo.pop(); x !== void 0; x = todo.pop()) {
|
|
2195
|
+
const kind = x;
|
|
2196
|
+
const { eval: name, step } = kind.verbs ?? {};
|
|
2197
|
+
if (name !== void 0) evaluators[name] = NO_CODE;
|
|
2198
|
+
if (step !== void 0) walkers[step] = NO_CODE;
|
|
2199
|
+
todo.push(...Object.values(kind.kinds ?? {}), ...kind.fallback === void 0 ? [] : [kind.fallback]);
|
|
2200
|
+
}
|
|
2201
|
+
return {
|
|
2202
|
+
world: VEX_WORLD,
|
|
2203
|
+
evaluators,
|
|
2204
|
+
walkers,
|
|
2205
|
+
...RULES
|
|
2206
|
+
};
|
|
2207
|
+
}
|
|
2208
|
+
/**
|
|
2209
|
+
* This function gives the definitions of the machine of Vex for the reference interpreter of the kernel. The formula
|
|
2210
|
+
* kit gets its definitions with the profile of Vex. The evaluators of Vex are functions, so they need no definition.
|
|
2211
|
+
*/
|
|
2212
|
+
function vexDefinitions(opts = {}) {
|
|
2213
|
+
return formulaDefinitions(profileOf(opts));
|
|
2214
|
+
}
|
|
2215
|
+
//#endregion
|
|
2216
|
+
//#region src/write.ts
|
|
2217
|
+
/**
|
|
2218
|
+
* The writer of the documents of Vex: the coeval document of an expression (DOC.TERM). It uses a stack, not recursion,
|
|
2219
|
+
* so an expression of any depth has a document. The package index does not export this module: `toDocument`, `deps`
|
|
2220
|
+
* and `kernelPlan` use it.
|
|
2221
|
+
*/
|
|
2222
|
+
/** The stop of a write: a literal without a JSON form. The caller gives its error. */
|
|
2223
|
+
var WriteStop = class {
|
|
2224
|
+
error;
|
|
2225
|
+
constructor(error) {
|
|
2226
|
+
this.error = error;
|
|
2227
|
+
}
|
|
2228
|
+
};
|
|
2229
|
+
const NONE = [];
|
|
2230
|
+
const term = (e) => ({
|
|
2231
|
+
t: "term",
|
|
2232
|
+
e
|
|
2233
|
+
});
|
|
2234
|
+
const envelope = (e) => ({
|
|
2235
|
+
t: "envelope",
|
|
2236
|
+
e
|
|
2237
|
+
});
|
|
2238
|
+
/** This function gives a record from names and the documents of their values, from an index of the documents. */
|
|
2239
|
+
function namesOf(names, docs, from) {
|
|
2240
|
+
const out = {};
|
|
2241
|
+
names.forEach((name, i) => {
|
|
2242
|
+
Object.defineProperty(out, name, {
|
|
2243
|
+
value: docs[from + i],
|
|
2244
|
+
enumerable: true,
|
|
2245
|
+
writable: true,
|
|
2246
|
+
configurable: true
|
|
2247
|
+
});
|
|
2248
|
+
});
|
|
2249
|
+
return out;
|
|
2250
|
+
}
|
|
2251
|
+
/** The writer of one document. */
|
|
2252
|
+
var Writer = class {
|
|
2253
|
+
#opts;
|
|
2254
|
+
constructor(opts) {
|
|
2255
|
+
this.#opts = opts;
|
|
2256
|
+
}
|
|
2257
|
+
/** This method writes the document of a job, with a stack of plans. */
|
|
2258
|
+
write(root) {
|
|
2259
|
+
const stack = [{
|
|
2260
|
+
plan: this.#plan(root),
|
|
2261
|
+
docs: []
|
|
2262
|
+
}];
|
|
2263
|
+
for (;;) {
|
|
2264
|
+
const top = stack[stack.length - 1];
|
|
2265
|
+
const next = top.plan.jobs[top.docs.length];
|
|
2266
|
+
if (next !== void 0) {
|
|
2267
|
+
stack.push({
|
|
2268
|
+
plan: this.#plan(next),
|
|
2269
|
+
docs: []
|
|
2270
|
+
});
|
|
2271
|
+
continue;
|
|
2272
|
+
}
|
|
2273
|
+
const doc = top.plan.make(top.docs);
|
|
2274
|
+
stack.pop();
|
|
2275
|
+
const parent = stack[stack.length - 1];
|
|
2276
|
+
if (parent === void 0) return doc;
|
|
2277
|
+
parent.docs.push(doc);
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
/** This method keeps the expression of an envelope, for `deps`. */
|
|
2281
|
+
#keep(doc, e) {
|
|
2282
|
+
this.#opts.nodes?.set(doc, e);
|
|
2283
|
+
return doc;
|
|
2284
|
+
}
|
|
2285
|
+
/** This method gives the document of a literal: its JSON form, or a value of the host (DOC.LITERAL). */
|
|
2286
|
+
#literal(value, root) {
|
|
2287
|
+
const host = {
|
|
2288
|
+
type: "lit",
|
|
2289
|
+
value,
|
|
2290
|
+
own: { eval: "vex.host" }
|
|
2291
|
+
};
|
|
2292
|
+
let r;
|
|
2293
|
+
try {
|
|
2294
|
+
r = encodeValue(value, this.#opts.domains);
|
|
2295
|
+
} catch (thrown) {
|
|
2296
|
+
if (this.#opts.literals === "json") throw thrown;
|
|
2297
|
+
return host;
|
|
2298
|
+
}
|
|
2299
|
+
if (!r.ok) {
|
|
2300
|
+
if (this.#opts.literals === "json") throw new WriteStop(r.error);
|
|
2301
|
+
return host;
|
|
2302
|
+
}
|
|
2303
|
+
const json = r.value;
|
|
2304
|
+
return root || isEnvelope(json) ? typed("lit", json) : json;
|
|
2305
|
+
}
|
|
2306
|
+
#plan(job) {
|
|
2307
|
+
if (job.t === "axis") return this.#axis(job.a);
|
|
2308
|
+
if (job.t === "move") return this.#move(job.m);
|
|
2309
|
+
const e = job.e;
|
|
2310
|
+
if (e.tag === "lit") return {
|
|
2311
|
+
jobs: NONE,
|
|
2312
|
+
make: () => this.#literal(e.value, job.t === "envelope")
|
|
2313
|
+
};
|
|
2314
|
+
return this.#envelope(e);
|
|
2315
|
+
}
|
|
2316
|
+
#envelope(e) {
|
|
2317
|
+
const reads = this.#opts.order === "reads";
|
|
2318
|
+
switch (e.tag) {
|
|
2319
|
+
case "ref": return {
|
|
2320
|
+
jobs: (e.at ?? []).map((m) => ({
|
|
2321
|
+
t: "move",
|
|
2322
|
+
m
|
|
2323
|
+
})),
|
|
2324
|
+
make: (docs) => this.#keep(ref$1("self", ...docs, ...e.path), e)
|
|
2325
|
+
};
|
|
2326
|
+
case "var": return {
|
|
2327
|
+
jobs: NONE,
|
|
2328
|
+
make: () => ref$1("names", e.name)
|
|
2329
|
+
};
|
|
2330
|
+
case "app": return {
|
|
2331
|
+
jobs: e.args.map(term),
|
|
2332
|
+
make: (docs) => typed(["ops", ...e.op.split(".")], [...docs])
|
|
2333
|
+
};
|
|
2334
|
+
case "let": {
|
|
2335
|
+
const names = Object.keys(e.bind);
|
|
2336
|
+
return {
|
|
2337
|
+
jobs: [...Object.values(e.bind).map(term), term(e.body)],
|
|
2338
|
+
make: (docs) => typed("let", {
|
|
2339
|
+
bind: namesOf(names, docs, 0),
|
|
2340
|
+
body: docs[names.length]
|
|
2341
|
+
})
|
|
2342
|
+
};
|
|
2343
|
+
}
|
|
2344
|
+
case "fn": return {
|
|
2345
|
+
jobs: [term(e.body)],
|
|
2346
|
+
make: (docs) => typed("fn", {
|
|
2347
|
+
params: [...e.params],
|
|
2348
|
+
body: docs[0]
|
|
2349
|
+
})
|
|
2350
|
+
};
|
|
2351
|
+
case "call": return {
|
|
2352
|
+
jobs: e.args.map(term),
|
|
2353
|
+
make: (docs) => this.#keep(typed("call", {
|
|
2354
|
+
fn: ref$1("names", e.fn),
|
|
2355
|
+
args: [...docs]
|
|
2356
|
+
}), e)
|
|
2357
|
+
};
|
|
2358
|
+
case "rec": {
|
|
2359
|
+
const names = Object.keys(e.fields);
|
|
2360
|
+
return {
|
|
2361
|
+
jobs: Object.values(e.fields).map(term),
|
|
2362
|
+
make: (docs) => typed("record", namesOf(names, docs, 0))
|
|
2363
|
+
};
|
|
2364
|
+
}
|
|
2365
|
+
case "each": {
|
|
2366
|
+
const at = this.#at(e.at);
|
|
2367
|
+
return {
|
|
2368
|
+
jobs: [
|
|
2369
|
+
{
|
|
2370
|
+
t: "axis",
|
|
2371
|
+
a: e.axis
|
|
2372
|
+
},
|
|
2373
|
+
term(e.body),
|
|
2374
|
+
...at.jobs
|
|
2375
|
+
],
|
|
2376
|
+
make: (docs) => {
|
|
2377
|
+
const axis = docs[0];
|
|
2378
|
+
const body = docs[1];
|
|
2379
|
+
const address = at.make(docs.slice(2));
|
|
2380
|
+
return this.#keep(typed("each", reads ? {
|
|
2381
|
+
body,
|
|
2382
|
+
axis,
|
|
2383
|
+
...address
|
|
2384
|
+
} : {
|
|
2385
|
+
axis,
|
|
2386
|
+
body,
|
|
2387
|
+
...address
|
|
2388
|
+
}), e);
|
|
2389
|
+
}
|
|
2390
|
+
};
|
|
2391
|
+
}
|
|
2392
|
+
case "fold": {
|
|
2393
|
+
const init = Object.keys(e.init);
|
|
2394
|
+
const step = Object.keys(e.step);
|
|
2395
|
+
const at = this.#at(e.at);
|
|
2396
|
+
const after = 1 + init.length + step.length;
|
|
2397
|
+
return {
|
|
2398
|
+
jobs: [
|
|
2399
|
+
{
|
|
2400
|
+
t: "axis",
|
|
2401
|
+
a: e.axis
|
|
2402
|
+
},
|
|
2403
|
+
...Object.values(e.init).map(term),
|
|
2404
|
+
...Object.values(e.step).map(term),
|
|
2405
|
+
term(e.result),
|
|
2406
|
+
...at.jobs
|
|
2407
|
+
],
|
|
2408
|
+
make: (docs) => {
|
|
2409
|
+
const axis = docs[0];
|
|
2410
|
+
const names = {
|
|
2411
|
+
init: namesOf(init, docs, 1),
|
|
2412
|
+
step: namesOf(step, docs, 1 + init.length)
|
|
2413
|
+
};
|
|
2414
|
+
const result = docs[after];
|
|
2415
|
+
const address = at.make(docs.slice(after + 1));
|
|
2416
|
+
return this.#keep(typed("fold", reads ? {
|
|
2417
|
+
...names,
|
|
2418
|
+
axis,
|
|
2419
|
+
...address,
|
|
2420
|
+
result
|
|
2421
|
+
} : {
|
|
2422
|
+
axis,
|
|
2423
|
+
...names,
|
|
2424
|
+
result,
|
|
2425
|
+
...address
|
|
2426
|
+
}), e);
|
|
2427
|
+
}
|
|
2428
|
+
};
|
|
2429
|
+
}
|
|
2430
|
+
case "iterate": {
|
|
2431
|
+
const init = Object.keys(e.init);
|
|
2432
|
+
const step = Object.keys(e.step);
|
|
2433
|
+
const until = e.until === void 0 ? NONE : [term(e.until)];
|
|
2434
|
+
const after = init.length + step.length;
|
|
2435
|
+
return {
|
|
2436
|
+
jobs: [
|
|
2437
|
+
...Object.values(e.init).map(term),
|
|
2438
|
+
...Object.values(e.step).map(term),
|
|
2439
|
+
...until,
|
|
2440
|
+
term(e.result)
|
|
2441
|
+
],
|
|
2442
|
+
make: (docs) => typed("iterate", {
|
|
2443
|
+
limit: e.limit,
|
|
2444
|
+
init: namesOf(init, docs, 0),
|
|
2445
|
+
step: namesOf(step, docs, init.length),
|
|
2446
|
+
...e.until === void 0 ? {} : { until: docs[after] },
|
|
2447
|
+
result: docs[after + until.length]
|
|
2448
|
+
})
|
|
2449
|
+
};
|
|
2450
|
+
}
|
|
2451
|
+
case "ext": return {
|
|
2452
|
+
jobs: NONE,
|
|
2453
|
+
make: () => typed(["ext", ...e.kind.split(".")], e.data)
|
|
2454
|
+
};
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
/** This method gives the plan of the field `at` of `each` and `fold`: a reference from `self` with moves only (DOC.ADDRESS). */
|
|
2458
|
+
#at(at) {
|
|
2459
|
+
if (at === void 0 || at.length === 0) return {
|
|
2460
|
+
jobs: NONE,
|
|
2461
|
+
make: () => ({})
|
|
2462
|
+
};
|
|
2463
|
+
return {
|
|
2464
|
+
jobs: at.map((m) => ({
|
|
2465
|
+
t: "move",
|
|
2466
|
+
m
|
|
2467
|
+
})),
|
|
2468
|
+
make: (docs) => ({ at: ref$1("self", ...docs) })
|
|
2469
|
+
};
|
|
2470
|
+
}
|
|
2471
|
+
/** This method gives the plan of a move. A computed key or position is an envelope (DOC.MOVE). */
|
|
2472
|
+
#move(m) {
|
|
2473
|
+
switch (m.t) {
|
|
2474
|
+
case "key": return typeof m.key === "string" ? this.#fixed(["moves", "key"], m.key) : {
|
|
2475
|
+
jobs: [envelope(m.key)],
|
|
2476
|
+
make: (docs) => typed(["moves", "key"], docs[0])
|
|
2477
|
+
};
|
|
2478
|
+
case "index": return typeof m.i === "number" ? this.#fixed(["moves", "index"], m.i) : {
|
|
2479
|
+
jobs: [envelope(m.i)],
|
|
2480
|
+
make: (docs) => typed(["moves", "index"], docs[0])
|
|
2481
|
+
};
|
|
2482
|
+
case "offset": return this.#fixed(["moves", "offset"], [...m.d]);
|
|
2483
|
+
case "other":
|
|
2484
|
+
case "origin":
|
|
2485
|
+
case "parent": return this.#fixed(["moves", m.t], null);
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
#fixed(type, value) {
|
|
2489
|
+
return {
|
|
2490
|
+
jobs: NONE,
|
|
2491
|
+
make: () => typed(type, value)
|
|
2492
|
+
};
|
|
2493
|
+
}
|
|
2494
|
+
/** This method gives the plan of an axis (DOC.AXIS). */
|
|
2495
|
+
#axis(a) {
|
|
2496
|
+
const type = ["axes", a.t];
|
|
2497
|
+
switch (a.t) {
|
|
2498
|
+
case "neighbors": return this.#fixed(type, { n: a.n });
|
|
2499
|
+
case "window": return this.#fixed(type, {
|
|
2500
|
+
from: a.from,
|
|
2501
|
+
to: a.to
|
|
2502
|
+
});
|
|
2503
|
+
case "table": return this.#fixed(type, { name: a.name });
|
|
2504
|
+
case "list": return {
|
|
2505
|
+
jobs: [term(a.list)],
|
|
2506
|
+
make: (docs) => typed(type, { list: docs[0] })
|
|
2507
|
+
};
|
|
2508
|
+
case "reverse": return {
|
|
2509
|
+
jobs: [{
|
|
2510
|
+
t: "axis",
|
|
2511
|
+
a: a.axis
|
|
2512
|
+
}],
|
|
2513
|
+
make: (docs) => typed(type, { axis: docs[0] })
|
|
2514
|
+
};
|
|
2515
|
+
case "where": return {
|
|
2516
|
+
jobs: [{
|
|
2517
|
+
t: "axis",
|
|
2518
|
+
a: a.axis
|
|
2519
|
+
}, term(a.test)],
|
|
2520
|
+
make: (docs) => typed(type, {
|
|
2521
|
+
axis: docs[0],
|
|
2522
|
+
test: docs[1]
|
|
2523
|
+
})
|
|
2524
|
+
};
|
|
2525
|
+
case "all":
|
|
2526
|
+
case "others":
|
|
2527
|
+
case "other":
|
|
2528
|
+
case "children":
|
|
2529
|
+
case "ancestors":
|
|
2530
|
+
case "descendants":
|
|
2531
|
+
case "siblings":
|
|
2532
|
+
case "successors":
|
|
2533
|
+
case "predecessors":
|
|
2534
|
+
case "reachable": return this.#fixed(type, {});
|
|
2535
|
+
}
|
|
2536
|
+
}
|
|
2537
|
+
};
|
|
2538
|
+
/**
|
|
2539
|
+
* This function writes the document of a valid expression. The root is always an envelope. It throws a `WriteStop` for
|
|
2540
|
+
* a literal without a JSON form in the mode `"json"`, and an exception of a getter of a literal value.
|
|
2541
|
+
*/
|
|
2542
|
+
function writeDocument(e, opts) {
|
|
2543
|
+
return new Writer(opts).write({
|
|
2544
|
+
t: "envelope",
|
|
2545
|
+
e
|
|
2546
|
+
});
|
|
2547
|
+
}
|
|
2548
|
+
//#endregion
|
|
2549
|
+
//#region src/document.ts
|
|
2550
|
+
/**
|
|
2551
|
+
* The documents of Vex. A document is the form of an expression in coeval, the kernel of the family. Each term is an
|
|
2552
|
+
* envelope `{ type, value }`, and a reference is a path from a root. The rules are decision D12 of coeval:
|
|
2553
|
+
* <https://github.com/mark1russell7/coeval/blob/main/docs/DECISIONS.md>.
|
|
2554
|
+
*
|
|
2555
|
+
* | Expression | Document |
|
|
2556
|
+
* |---|---|
|
|
2557
|
+
* | `lit` | the JSON value of the literal. A value with the shape of an envelope is `{ "type": "lit", "value": x }`. |
|
|
2558
|
+
* | `ref` | `{ "type": "ref", "value": ["self", ...moves, ...fields] }` |
|
|
2559
|
+
* | `var` | `{ "type": "ref", "value": ["names", name] }` |
|
|
2560
|
+
* | `app` | `{ "type": ["ops", ...keys], "value": [...args] }`. The keys are the name of the op, split at its dots. |
|
|
2561
|
+
* | `let` | `{ "type": "let", "value": { "bind", "body" } }` |
|
|
2562
|
+
* | `fn` | `{ "type": "fn", "value": { "params", "body" } }` |
|
|
2563
|
+
* | `call` | `{ "type": "call", "value": { "fn": { "type": "ref", "value": ["names", name] }, "args" } }` |
|
|
2564
|
+
* | `rec` | `{ "type": "record", "value": { ...fields } }` |
|
|
2565
|
+
* | `each` | `{ "type": "each", "value": { "axis", "body", "at"? } }` |
|
|
2566
|
+
* | `fold` | `{ "type": "fold", "value": { "axis", "init", "step", "result", "at"? } }` |
|
|
2567
|
+
* | `iterate` | `{ "type": "iterate", "value": { "limit", "init", "step", "until"?, "result" } }` |
|
|
2568
|
+
* | `ext` | `{ "type": ["ext", ...keys], "value": data }` |
|
|
2569
|
+
* | a move | `{ "type": ["moves", kind], "value": v }` |
|
|
2570
|
+
* | an axis | `{ "type": ["axes", name], "value": { ...parameters } }` |
|
|
2571
|
+
*
|
|
2572
|
+
* The address `at` of `each` and `fold` is a reference from `self` with moves only. An empty address is no address.
|
|
2573
|
+
*
|
|
2574
|
+
* The kernel checks a document by the contracts of `VEX_WORLD` (DOC.CHECK). A contract gives the shape of a kind, the
|
|
2575
|
+
* data of its fields and the kinds of each position. Then `fromDocument` reads the expression. It checks only the grammar
|
|
2576
|
+
* of Vex: the steps of a reference, the address, the function of a call and the names of a loop.
|
|
2577
|
+
*/
|
|
2578
|
+
/** The stop of a reading. The functions of this module throw it, and the top of the reading gives its error. */
|
|
2579
|
+
var Stop = class {
|
|
2580
|
+
error;
|
|
2581
|
+
constructor(error) {
|
|
2582
|
+
this.error = error;
|
|
2583
|
+
}
|
|
2584
|
+
};
|
|
2585
|
+
/** This function gives the location of a child: a JSON Pointer, with the escapes of RFC 6901. */
|
|
2586
|
+
const into = (where, step) => `${where}/${String(step).replaceAll("~", "~0").replaceAll("/", "~1")}`;
|
|
2587
|
+
/** This function gives the JSON Pointer of a path of keys. */
|
|
2588
|
+
const pointer = (keys) => keys.reduce(into, "");
|
|
2589
|
+
/** This function gives the message of a document that is not valid at `where`. */
|
|
2590
|
+
const notValid = (where, reason) => `the document is not valid at ${where === "" ? "its root" : `"${where}"`}: ${reason}`;
|
|
2591
|
+
/** This function stops the reading of a document that is not valid at `where`. */
|
|
2592
|
+
function bad(where, reason) {
|
|
2593
|
+
throw new Stop(vexError("bad-expression", notValid(where, reason)));
|
|
2594
|
+
}
|
|
2595
|
+
/** This function does a conversion. It gives its value, or the error of the conversion. It does not throw. */
|
|
2596
|
+
function attempt(work, threw, deep) {
|
|
2597
|
+
try {
|
|
2598
|
+
return ok(work());
|
|
2599
|
+
} catch (thrown) {
|
|
2600
|
+
if (thrown instanceof Stop || thrown instanceof WriteStop) return fail(thrown.error);
|
|
2601
|
+
if (isStackOverflow(thrown)) return fail(vexError("depth", deep, { thrown }));
|
|
2602
|
+
return fail(vexError("threw", threw, { thrown }));
|
|
2603
|
+
}
|
|
2604
|
+
}
|
|
2605
|
+
/**
|
|
2606
|
+
* This function tells if a value is a document: an envelope `{ type, value }`. A node of the tagged IR is not an
|
|
2607
|
+
* envelope, because it has the key `tag`. Thus `compile` and `evaluate` can take either one (DOC.ENTRY).
|
|
2608
|
+
*/
|
|
2609
|
+
function isDocument(u) {
|
|
2610
|
+
return isEnvelope(u);
|
|
2611
|
+
}
|
|
2612
|
+
/**
|
|
2613
|
+
* This function gives the document of an expression (DOC.TERM). The root is always an envelope, so `compile` can tell a
|
|
2614
|
+
* document from the tagged IR. A literal value has the JSON form of IR.JSON, so a domain value needs `encode` (DOC.LITERAL).
|
|
2615
|
+
* An expression that is not valid, or a literal without a JSON form, gives the error `#VALUE!` (DOC.ERROR).
|
|
2616
|
+
*/
|
|
2617
|
+
function toDocument(e, domains = []) {
|
|
2618
|
+
const shape = shapeError(e);
|
|
2619
|
+
if (shape !== void 0) return fail(vexError("bad-expression", shape));
|
|
2620
|
+
const options = {
|
|
2621
|
+
domains,
|
|
2622
|
+
literals: "json",
|
|
2623
|
+
order: "document"
|
|
1021
2624
|
};
|
|
2625
|
+
return attempt(() => writeDocument(e, options), "the expression has no document", "a literal value is too deep for the call stack of the engine");
|
|
1022
2626
|
}
|
|
1023
|
-
|
|
1024
|
-
function
|
|
1025
|
-
|
|
2627
|
+
let checker;
|
|
2628
|
+
/** This function gives the first problem of a document that the kernel finds by the contracts of Vex (DOC.CHECK). */
|
|
2629
|
+
function problemOf(doc) {
|
|
2630
|
+
checker ??= vexChecker();
|
|
2631
|
+
return check(checker, doc, {
|
|
2632
|
+
declaredType: "lit",
|
|
2633
|
+
names: "any"
|
|
2634
|
+
})[0];
|
|
1026
2635
|
}
|
|
1027
|
-
/**
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
const
|
|
2636
|
+
/**
|
|
2637
|
+
* This function reads an expression from a document (DOC.TERM). It is the inverse of `toDocument`. The kernel checks
|
|
2638
|
+
* the document first (DOC.CHECK). A document that is not valid gives `#VALUE!` with its location, and a document nested
|
|
2639
|
+
* deeper than the kernel takes gives `#DEPTH!` (DOC.ERROR). The expression of a valid document is valid (EVAL.SHAPE).
|
|
2640
|
+
*/
|
|
2641
|
+
function fromDocument(doc, domains = []) {
|
|
2642
|
+
const problem = attempt(() => problemOf(doc), "the check of the document threw an exception", "the document is too deep for the call stack of the engine");
|
|
2643
|
+
if (!problem.ok) return problem;
|
|
2644
|
+
const issue = problem.value;
|
|
2645
|
+
if (issue?.code === "#DEPTH!") return fail(vexError("depth", `the document is nested deeper than ${String(MAX_DOCUMENT_DEPTH)} documents`));
|
|
2646
|
+
if (issue !== void 0) return fail(vexError("bad-expression", notValid(pointer(issue.at ?? []), issue.reason)));
|
|
2647
|
+
return attempt(() => new Reader(domains).term(doc, ""), "a decoder of a domain threw an exception", "the document is too deep for the call stack of the engine");
|
|
2648
|
+
}
|
|
2649
|
+
/** The kinds of the terms, as the first key of their type. */
|
|
2650
|
+
const TERMS = /* @__PURE__ */ new Set(/* @__PURE__ */ TERM_KINDS.map((k) => k[0]));
|
|
2651
|
+
/**
|
|
2652
|
+
* The reader of documents. The kernel checked the document, so each kind has the shape of its contract. The reader
|
|
2653
|
+
* checks the grammar of Vex, and its methods throw a `Stop` at the first part that is not valid.
|
|
2654
|
+
*/
|
|
2655
|
+
var Reader = class {
|
|
2656
|
+
#domains;
|
|
2657
|
+
constructor(domains) {
|
|
2658
|
+
this.#domains = domains;
|
|
2659
|
+
}
|
|
2660
|
+
/** This method reads a term. An envelope is a term, and another value is a literal (DOC.TERM). */
|
|
2661
|
+
term(u, where) {
|
|
2662
|
+
return isDocument(u) ? this.#envelope(u, where) : lit(this.#value(u, where));
|
|
2663
|
+
}
|
|
2664
|
+
/** This method reads a term in an envelope. */
|
|
2665
|
+
#envelope(u, where) {
|
|
2666
|
+
const at = into(where, "value");
|
|
2667
|
+
const keys = this.#type(u, where);
|
|
2668
|
+
const head = String(keys[0]);
|
|
2669
|
+
if (!TERMS.has(head)) return bad(where, `the type "${typeName(u.type)}" is not a term of Vex`);
|
|
2670
|
+
if (head === "ops" || head === "ext") {
|
|
2671
|
+
const name = this.#name(keys.slice(1), into(where, "type"));
|
|
2672
|
+
return head === "ops" ? app(name, ...u.value.map((x, i) => this.term(x, into(at, i)))) : ext(name, u.value);
|
|
2673
|
+
}
|
|
2674
|
+
const f = u.value;
|
|
2675
|
+
switch (head) {
|
|
2676
|
+
case "lit": return lit(this.#value(u.value, at));
|
|
2677
|
+
case "ref": return this.#ref(u.value, at);
|
|
2678
|
+
case "let": return let_(this.#binds(f["bind"], into(at, "bind")), this.term(f["body"], into(at, "body")));
|
|
2679
|
+
case "call": {
|
|
2680
|
+
const name = this.#function(f["fn"], into(at, "fn"));
|
|
2681
|
+
return call(name, ...f["args"].map((x, i) => this.term(x, into(into(at, "args"), i))));
|
|
2682
|
+
}
|
|
2683
|
+
case "record": return rec(this.#names(u.value, at));
|
|
2684
|
+
case "each": return each(this.#axis(f["axis"], into(at, "axis")), this.term(f["body"], into(at, "body")), this.#address(f["at"], into(at, "at")));
|
|
2685
|
+
case "fold": {
|
|
2686
|
+
const [init, step] = this.#loop(f, at);
|
|
2687
|
+
return fold(this.#axis(f["axis"], into(at, "axis")), init, step, this.term(f["result"], into(at, "result")), this.#address(f["at"], into(at, "at")));
|
|
2688
|
+
}
|
|
2689
|
+
default: {
|
|
2690
|
+
const [init, step] = this.#loop(f, at);
|
|
2691
|
+
const until = f["until"] === void 0 ? void 0 : this.term(f["until"], into(at, "until"));
|
|
2692
|
+
return iterate(f["limit"], init, step, this.term(f["result"], into(at, "result")), until);
|
|
2693
|
+
}
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
/** This method gives the keys of the type of an envelope. A term of Vex has no header (DOC.TERM). */
|
|
2697
|
+
#type(u, where) {
|
|
2698
|
+
if (u.name !== void 0 || u.own !== void 0) return bad(where, "a term of Vex has no name and no own entries");
|
|
2699
|
+
return typePath(u.type);
|
|
2700
|
+
}
|
|
2701
|
+
/** This method gives the name of an op or an extension: its keys, joined with dots (DOC.OPS, DOC.EXT). */
|
|
2702
|
+
#name(keys, where) {
|
|
2703
|
+
return keys.every((k) => typeof k === "string") ? keys.join(".") : bad(where, "a key of the name is not a text");
|
|
2704
|
+
}
|
|
2705
|
+
/** This method gives the value of a literal from its JSON form (DOC.LITERAL). */
|
|
2706
|
+
#value(u, where) {
|
|
2707
|
+
const r = decodeValue(u, this.#domains);
|
|
2708
|
+
return r.ok ? r.value : bad(where, r.error.message);
|
|
2709
|
+
}
|
|
2710
|
+
/** This method reads a record of names and the term of each name. */
|
|
2711
|
+
#names(u, where) {
|
|
2712
|
+
return Object.fromEntries(Object.entries(u).map(([name, x]) => [name, this.term(x, into(where, name))]));
|
|
2713
|
+
}
|
|
2714
|
+
/** This method reads the bindings of a let. A binding can be a function (FN.BIND). */
|
|
2715
|
+
#binds(u, where) {
|
|
2716
|
+
return Object.fromEntries(Object.entries(u).map(([name, x]) => {
|
|
2717
|
+
const at = into(where, name);
|
|
2718
|
+
if (!isDocument(x) || typeName(x.type) !== "fn") return [name, this.term(x, at)];
|
|
2719
|
+
this.#type(x, at);
|
|
2720
|
+
const f = x.value;
|
|
2721
|
+
return [name, fn(f.params, this.term(f.body, into(into(at, "value"), "body")))];
|
|
2722
|
+
}));
|
|
2723
|
+
}
|
|
2724
|
+
/** This method reads the names of a loop. Each name of `step` is a name of `init` (LOOP.NAMES). */
|
|
2725
|
+
#loop(f, at) {
|
|
2726
|
+
const init = this.#names(f["init"], into(at, "init"));
|
|
2727
|
+
const step = this.#names(f["step"], into(at, "step"));
|
|
2728
|
+
const extra = Object.keys(step).find((name) => !Object.hasOwn(init, name));
|
|
2729
|
+
return extra === void 0 ? [init, step] : bad(into(into(at, "step"), extra), `the name "${extra}" of step is not a name of init`);
|
|
2730
|
+
}
|
|
2731
|
+
/** This method reads a reference: the fields of `self` after its moves, or a name of `names` (DOC.REF). */
|
|
2732
|
+
#ref(path, where) {
|
|
2733
|
+
const [root, ...steps] = path;
|
|
2734
|
+
if (root === "names") return steps.length === 1 && typeof steps[0] === "string" ? v(steps[0]) : bad(where, "a reference to a name has one step: the name");
|
|
2735
|
+
const moves = [];
|
|
2736
|
+
const names = [];
|
|
2737
|
+
steps.forEach((step, i) => {
|
|
2738
|
+
const at = into(where, i + 1);
|
|
2739
|
+
if (typeof step === "string") names.push(step);
|
|
2740
|
+
else if (!isDocument(step)) bad(at, "a step is not a text and not a move");
|
|
2741
|
+
else if (names.length > 0) bad(at, "a move comes after a field. Vex reads the fields at the end of the path");
|
|
2742
|
+
else moves.push(this.#move(step, at));
|
|
2743
|
+
});
|
|
2744
|
+
return ref(names, moves);
|
|
2745
|
+
}
|
|
2746
|
+
/** This method reads the function of a call: a reference to a name (DOC.CALL). */
|
|
2747
|
+
#function(u, where) {
|
|
2748
|
+
const steps = u.value;
|
|
2749
|
+
return steps[0] === "names" && steps.length === 2 && typeof steps[1] === "string" ? steps[1] : bad(where, "the function of a call is a reference to a name: { \"type\": \"ref\", \"value\": [\"names\", name] }");
|
|
2750
|
+
}
|
|
2751
|
+
/** This method reads the field `at` of `each` and `fold`: a reference from `self` with moves only (DOC.ADDRESS). */
|
|
2752
|
+
#address(u, where) {
|
|
2753
|
+
if (u === void 0) return void 0;
|
|
2754
|
+
const e = this.#envelope(u, where);
|
|
2755
|
+
return e.tag === "ref" && e.path.length === 0 ? e.at ?? [] : bad(where, "the address is a reference from \"self\" with moves only: { \"type\": \"ref\", \"value\": [\"self\", ...moves] }");
|
|
2756
|
+
}
|
|
2757
|
+
/**
|
|
2758
|
+
* This method reads a move (DOC.MOVE). The value of `key` and `index` is a term: a text key and a number position are
|
|
2759
|
+
* fixed, and another term is computed. The check gave the other moves their values.
|
|
2760
|
+
*/
|
|
2761
|
+
#move(u, where) {
|
|
2762
|
+
const kind = String(this.#type(u, where)[1]);
|
|
2763
|
+
const at = into(where, "value");
|
|
2764
|
+
const value = u.value;
|
|
2765
|
+
switch (kind) {
|
|
2766
|
+
case "key": return typeof value === "string" ? key(value) : key(this.term(value, at));
|
|
2767
|
+
case "index": return typeof value === "number" ? index(value) : index(this.term(value, at));
|
|
2768
|
+
case "offset": return offset(...value);
|
|
2769
|
+
case "other": return other;
|
|
2770
|
+
case "origin": return origin;
|
|
2771
|
+
default: return parent;
|
|
2772
|
+
}
|
|
2773
|
+
}
|
|
2774
|
+
/** This method reads an axis (DOC.AXIS). The check gave it the parameters of its kind. */
|
|
2775
|
+
#axis(u, where) {
|
|
2776
|
+
const name = String(this.#type(u, where)[1]);
|
|
2777
|
+
const at = into(where, "value");
|
|
2778
|
+
const f = u.value;
|
|
2779
|
+
switch (name) {
|
|
2780
|
+
case "list": return {
|
|
2781
|
+
t: name,
|
|
2782
|
+
list: this.term(f["list"], into(at, "list"))
|
|
2783
|
+
};
|
|
2784
|
+
case "reverse": return {
|
|
2785
|
+
t: name,
|
|
2786
|
+
axis: this.#axis(f["axis"], into(at, "axis"))
|
|
2787
|
+
};
|
|
2788
|
+
case "where": return {
|
|
2789
|
+
t: name,
|
|
2790
|
+
axis: this.#axis(f["axis"], into(at, "axis")),
|
|
2791
|
+
test: this.term(f["test"], into(at, "test"))
|
|
2792
|
+
};
|
|
2793
|
+
default: return {
|
|
2794
|
+
t: name,
|
|
2795
|
+
...f
|
|
2796
|
+
};
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
};
|
|
1059
2800
|
//#endregion
|
|
1060
2801
|
//#region src/evaluate.ts
|
|
1061
2802
|
/** The names of the special forms. Their arguments are not evaluated before the call. */
|
|
@@ -1066,10 +2807,6 @@ const SPECIAL_FORMS = /* @__PURE__ */ new Set([
|
|
|
1066
2807
|
"ifError"
|
|
1067
2808
|
]);
|
|
1068
2809
|
const isSpecialForm = (op) => SPECIAL_FORMS.has(op);
|
|
1069
|
-
/**
|
|
1070
|
-
* This function compiles an expression. The result gives the same results and the same traces as `evaluate`,
|
|
1071
|
-
* because `evaluate` uses it too.
|
|
1072
|
-
*/
|
|
1073
2810
|
/** A bound function (FN.BIND): its parameters, its compiled body and the names of the place of its binding. */
|
|
1074
2811
|
var Closure = class {
|
|
1075
2812
|
params;
|
|
@@ -1095,11 +2832,18 @@ function nestingOf(e) {
|
|
|
1095
2832
|
}
|
|
1096
2833
|
return deepest;
|
|
1097
2834
|
}
|
|
1098
|
-
|
|
1099
|
-
|
|
2835
|
+
/**
|
|
2836
|
+
* This function compiles an expression or a document (DOC.ENTRY). The result gives the same results and the same traces
|
|
2837
|
+
* as `evaluate`, because `evaluate` uses it too.
|
|
2838
|
+
*/
|
|
2839
|
+
function compile(source, opts = {}) {
|
|
2840
|
+
const read = isDocument(source) ? fromDocument(source, opts.domains) : ok(source);
|
|
2841
|
+
const expr = read.ok ? read.value : source;
|
|
2842
|
+
const shape = read.ok ? shapeError(expr) : read.error.message;
|
|
1100
2843
|
const nesting = shape === void 0 ? nestingOf(expr) : 0;
|
|
1101
2844
|
const bad = shape ?? (nesting > 500 ? `the expression is nested ${nesting} levels deep, and the limit is 500` : void 0);
|
|
1102
|
-
const
|
|
2845
|
+
const kind = read.ok ? "bad-expression" : read.error.kind;
|
|
2846
|
+
const root = bad === void 0 ? new Compiler(opts).node(expr, []) : (f) => fail(vexError(kind, bad, {
|
|
1103
2847
|
path: [],
|
|
1104
2848
|
origin: f.pos.origin,
|
|
1105
2849
|
focus: f.pos.focus
|
|
@@ -1154,6 +2898,7 @@ function compile(expr, opts = {}) {
|
|
|
1154
2898
|
}
|
|
1155
2899
|
const CACHE = /* @__PURE__ */ new WeakMap();
|
|
1156
2900
|
function compiled(expr, opts) {
|
|
2901
|
+
if (typeof expr !== "object" || expr === null) return compile(expr, opts);
|
|
1157
2902
|
const hit = CACHE.get(expr);
|
|
1158
2903
|
if (hit !== void 0 && hit.domains === opts.domains && hit.fns === opts.fns && hit.extensions === opts.extensions) return hit.program;
|
|
1159
2904
|
const program = compile(expr, opts);
|
|
@@ -1165,11 +2910,11 @@ function compiled(expr, opts) {
|
|
|
1165
2910
|
});
|
|
1166
2911
|
return program;
|
|
1167
2912
|
}
|
|
1168
|
-
/** This function evaluates an expression at the origin of a space. It does not throw. */
|
|
2913
|
+
/** This function evaluates an expression or a document at the origin of a space. It does not throw. */
|
|
1169
2914
|
function evaluate(expr, opts) {
|
|
1170
2915
|
return compiled(expr, opts).run(opts);
|
|
1171
2916
|
}
|
|
1172
|
-
/** This function evaluates an expression and records a trace of each node. */
|
|
2917
|
+
/** This function evaluates an expression or a document and records a trace of each node. */
|
|
1173
2918
|
function explain(expr, opts) {
|
|
1174
2919
|
return compiled(expr, opts).explain(opts);
|
|
1175
2920
|
}
|
|
@@ -1760,13 +3505,12 @@ var Compiler = class {
|
|
|
1760
3505
|
return r;
|
|
1761
3506
|
}
|
|
1762
3507
|
#listOp(op, list, rest, f, path) {
|
|
1763
|
-
const optsArg = op
|
|
1764
|
-
const depth = list.items.find((it) => !it.result.ok && isDepthError(it.result.error))?.result;
|
|
1765
|
-
if (depth !== void 0 && !depth.ok) return fail(depth.error);
|
|
3508
|
+
const optsArg = WITH_ARGUMENT.has(op) ? rest[1] : rest[0];
|
|
1766
3509
|
const strict = optsArg?.strict === true;
|
|
1767
|
-
const
|
|
1768
|
-
if (
|
|
1769
|
-
const
|
|
3510
|
+
const good = goodItems(list, strict);
|
|
3511
|
+
if (!good.ok) return good;
|
|
3512
|
+
const okItems = good.value;
|
|
3513
|
+
const okValues = okItems.flatMap((it) => it.result.ok ? [it.result.value] : []);
|
|
1770
3514
|
const numbers = () => {
|
|
1771
3515
|
const bad = okValues.find((x) => typeof x !== "number");
|
|
1772
3516
|
return bad === void 0 ? ok(okValues) : this.#err("kind-mismatch", `"${op}" needs numbers, but an item is a ${describeType(bad)}`, f, path, { op });
|
|
@@ -1780,13 +3524,26 @@ var Compiler = class {
|
|
|
1780
3524
|
const n = rest[0];
|
|
1781
3525
|
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
3526
|
};
|
|
1783
|
-
const
|
|
3527
|
+
const other = () => isVexList(rest[0]) ? goodItems(rest[0], strict) : this.#err("kind-mismatch", `"${op}" needs a list as its second argument, not a ${describeType(rest[0])}`, f, path, { op });
|
|
1784
3528
|
switch (op) {
|
|
1785
3529
|
case "sortBy": return this.#sortBy(okItems, okValues, f, path);
|
|
1786
|
-
case "take":
|
|
3530
|
+
case "take":
|
|
3531
|
+
case "drop": {
|
|
1787
3532
|
const n = whole();
|
|
1788
|
-
|
|
3533
|
+
if (!n.ok) return n;
|
|
3534
|
+
return ok(vexList(op === "take" ? okItems.slice(0, n.value) : okItems.slice(n.value)));
|
|
1789
3535
|
}
|
|
3536
|
+
case "append": {
|
|
3537
|
+
const more = other();
|
|
3538
|
+
return more.ok ? ok(vexList([...okItems, ...more.value])) : more;
|
|
3539
|
+
}
|
|
3540
|
+
case "without": {
|
|
3541
|
+
const more = other();
|
|
3542
|
+
if (!more.ok) return more;
|
|
3543
|
+
const gone = new Set(more.value.map((it) => it.key));
|
|
3544
|
+
return ok(vexList(okItems.filter((it) => !gone.has(it.key))));
|
|
3545
|
+
}
|
|
3546
|
+
case "flat": return this.#flat(okValues, strict, f, path);
|
|
1790
3547
|
case "nth": {
|
|
1791
3548
|
const i = whole();
|
|
1792
3549
|
if (!i.ok) return i;
|
|
@@ -1794,7 +3551,7 @@ var Compiler = class {
|
|
|
1794
3551
|
}
|
|
1795
3552
|
case "count": return ok(okValues.length);
|
|
1796
3553
|
case "values": return ok(Object.freeze([...okValues]));
|
|
1797
|
-
case "keys": return ok(Object.freeze(
|
|
3554
|
+
case "keys": return ok(Object.freeze(okItems.map((it) => it.key)));
|
|
1798
3555
|
case "errors": return ok(Object.freeze(list.items.flatMap((it) => it.result.ok ? [] : [it.result.error])));
|
|
1799
3556
|
case "first": return okValues.length > 0 ? ok(okValues[0]) : empty();
|
|
1800
3557
|
case "sum": {
|
|
@@ -1826,6 +3583,20 @@ var Compiler = class {
|
|
|
1826
3583
|
}
|
|
1827
3584
|
}
|
|
1828
3585
|
/**
|
|
3586
|
+
* This method joins the lists that are the values of the items of a list, in order (LIST.FLAT). Each list follows the
|
|
3587
|
+
* rules of LIST.BUILD. A value that is not a list gives `#VALUE!`.
|
|
3588
|
+
*/
|
|
3589
|
+
#flat(values, strict, f, path) {
|
|
3590
|
+
const out = [];
|
|
3591
|
+
for (const inner of values) {
|
|
3592
|
+
if (!isVexList(inner)) return this.#err("kind-mismatch", `"flat" needs a list as the value of each item, but an item is a ${describeType(inner)}`, f, path, { op: "flat" });
|
|
3593
|
+
const good = goodItems(inner, strict);
|
|
3594
|
+
if (!good.ok) return good;
|
|
3595
|
+
out.push(...good.value);
|
|
3596
|
+
}
|
|
3597
|
+
return ok(vexList(out));
|
|
3598
|
+
}
|
|
3599
|
+
/**
|
|
1829
3600
|
* This method sorts the items of a list by their values, from the smallest (LIST.SORT). The sort is stable. The values
|
|
1830
3601
|
* are all numbers without NaN, or all texts. Texts compare by their UTF-16 code units.
|
|
1831
3602
|
*/
|
|
@@ -1870,6 +3641,26 @@ var Compiler = class {
|
|
|
1870
3641
|
return ok(acc);
|
|
1871
3642
|
}
|
|
1872
3643
|
};
|
|
3644
|
+
/** The list ops with a second argument before their options: a count, a position, an op name or a list. */
|
|
3645
|
+
const WITH_ARGUMENT = /* @__PURE__ */ new Set([
|
|
3646
|
+
"reduce",
|
|
3647
|
+
"take",
|
|
3648
|
+
"nth",
|
|
3649
|
+
"drop",
|
|
3650
|
+
"append",
|
|
3651
|
+
"without"
|
|
3652
|
+
]);
|
|
3653
|
+
/**
|
|
3654
|
+
* This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
|
|
3655
|
+
* error comes first (EVAL.DEPTH). With `strict`, the first error item is the result.
|
|
3656
|
+
*/
|
|
3657
|
+
function goodItems(list, strict) {
|
|
3658
|
+
const depth = list.items.find((it) => !it.result.ok && isDepthError(it.result.error))?.result;
|
|
3659
|
+
if (depth !== void 0 && !depth.ok) return fail(depth.error);
|
|
3660
|
+
const firstError = list.items.find((it) => !it.result.ok)?.result;
|
|
3661
|
+
if (strict && firstError !== void 0 && !firstError.ok) return fail(firstError.error);
|
|
3662
|
+
return ok(list.items.filter((it) => it.result.ok));
|
|
3663
|
+
}
|
|
1873
3664
|
const FORBIDDEN_FIELDS = /* @__PURE__ */ new Set([
|
|
1874
3665
|
"__proto__",
|
|
1875
3666
|
"constructor",
|
|
@@ -1973,180 +3764,172 @@ function formatTrace(trace, domains = []) {
|
|
|
1973
3764
|
const sizeOf = (e) => 1 + children(e).reduce((s, c) => s + sizeOf(c), 0);
|
|
1974
3765
|
//#endregion
|
|
1975
3766
|
//#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
3767
|
/**
|
|
1979
|
-
*
|
|
1980
|
-
*
|
|
1981
|
-
*
|
|
3768
|
+
* The static reads of a program (DEPS.READS). The static kit of coeval finds the references of the document of the
|
|
3769
|
+
* program, by the contracts of Vex (`refsOf`). Vex adds only what is its own: the axes around a read, and the reads of
|
|
3770
|
+
* a function at each of its calls (FN.CALL).
|
|
1982
3771
|
*/
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
3772
|
+
const ROOT = {
|
|
3773
|
+
axes: void 0,
|
|
3774
|
+
axis: void 0,
|
|
3775
|
+
fn: void 0,
|
|
3776
|
+
calls: void 0
|
|
3777
|
+
};
|
|
3778
|
+
/** This function gives the items of a chain until the chain `end`, from the outside to the inside. */
|
|
3779
|
+
function itemsUntil(chain, end) {
|
|
3780
|
+
const out = [];
|
|
3781
|
+
for (let c = chain; c !== void 0 && c !== end; c = c.up) out.push(c.item);
|
|
3782
|
+
return out.toReversed();
|
|
1989
3783
|
}
|
|
3784
|
+
/** This function gives the kind of a visit, as its keys joined with dots, for example `each` or `axes.where`. */
|
|
3785
|
+
const kindOf$1 = (v) => (v.typePath ?? []).join(".");
|
|
1990
3786
|
/**
|
|
1991
|
-
* This function gives the static reads of an expression. A host can use them to recompute a program only
|
|
1992
|
-
*
|
|
1993
|
-
*
|
|
3787
|
+
* This function gives the static reads of an expression. A host can use them to recompute a program only when a field
|
|
3788
|
+
* that it reads changes. A computed move reads a key that only a run knows. Its address keeps the expression, and the
|
|
3789
|
+
* reads of that expression are in the list too (DEPS.COMPUTED). The kernel walks the document with a stack, so an
|
|
3790
|
+
* expression of any depth works (EVAL.NESTING).
|
|
1994
3791
|
*/
|
|
1995
3792
|
function deps(e) {
|
|
1996
|
-
const
|
|
1997
|
-
const
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
}
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
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;
|
|
3793
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
3794
|
+
const doc = writeDocument(e, {
|
|
3795
|
+
domains: [],
|
|
3796
|
+
literals: "host",
|
|
3797
|
+
order: "reads",
|
|
3798
|
+
nodes
|
|
3799
|
+
});
|
|
3800
|
+
const uses = refsOf(VEX_WORLD, doc);
|
|
3801
|
+
const memo = /* @__PURE__ */ new WeakMap();
|
|
3802
|
+
const functions = /* @__PURE__ */ new WeakMap();
|
|
3803
|
+
const fnVisits = /* @__PURE__ */ new Map();
|
|
3804
|
+
/** This function gives the function of a call: the visit of the binding of a let, or `undefined` (FN.CALL). */
|
|
3805
|
+
const functionOf = (call) => {
|
|
3806
|
+
const known = functions.get(call);
|
|
3807
|
+
if (known !== void 0) return known ?? void 0;
|
|
3808
|
+
const name = call.doc.value.fn.value[1];
|
|
3809
|
+
let found;
|
|
3810
|
+
search: for (let child = call, around = call.parent; around !== void 0; child = around, around = around.parent) for (const rule of around.kind?.scope ?? []) {
|
|
3811
|
+
if (!rule.in.some((pattern) => pathMatches(child.slot, pattern))) continue;
|
|
3812
|
+
const source = rule.names;
|
|
3813
|
+
const at = (source.keysAt ?? source.listAt ?? []).reduce((x, k) => x[String(k)], around.doc.value);
|
|
3814
|
+
if (!(source.keysAt !== void 0 ? Object.keys(at) : at).includes(name)) continue;
|
|
3815
|
+
found = fnVisits.get(at[name]);
|
|
3816
|
+
break search;
|
|
2041
3817
|
}
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
const
|
|
2048
|
-
const
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
3818
|
+
functions.set(call, found ?? null);
|
|
3819
|
+
return found;
|
|
3820
|
+
};
|
|
3821
|
+
/** This function gives the context of a child from the context of its parent. */
|
|
3822
|
+
const step = (outer, parent, child) => {
|
|
3823
|
+
const kind = kindOf$1(parent);
|
|
3824
|
+
const head = child.slot[0];
|
|
3825
|
+
let { axes, fn, calls } = outer;
|
|
3826
|
+
let axis;
|
|
3827
|
+
if (outer.axis !== void 0) {
|
|
3828
|
+
if (head === "test") axes = {
|
|
3829
|
+
item: outer.axis,
|
|
3830
|
+
up: axes
|
|
3831
|
+
};
|
|
3832
|
+
else if (head === "axis") axis = outer.axis;
|
|
3833
|
+
} else if (kind === "each" || kind === "fold") {
|
|
3834
|
+
const node = nodes.get(parent.doc);
|
|
3835
|
+
const around = {
|
|
3836
|
+
t: node.axis.t,
|
|
3837
|
+
at: node.at ?? []
|
|
3838
|
+
};
|
|
3839
|
+
if (head === "body" || head === "step") axes = {
|
|
3840
|
+
item: around,
|
|
3841
|
+
up: axes
|
|
3842
|
+
};
|
|
3843
|
+
else if (head === "axis") axis = around;
|
|
2058
3844
|
}
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
//#endregion
|
|
2063
|
-
//#region src/json.ts
|
|
2064
|
-
const isRecord = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
2065
|
-
const isPlainRecord = (u) => {
|
|
2066
|
-
if (!isRecord(u)) return false;
|
|
2067
|
-
const proto = Object.getPrototypeOf(u);
|
|
2068
|
-
return proto === Object.prototype || proto === null;
|
|
2069
|
-
};
|
|
2070
|
-
/** This function gives the JSON form of a value, or the reason why the value has no JSON form. */
|
|
2071
|
-
function encodeValue(u, domains) {
|
|
2072
|
-
if (u === null || typeof u === "string" || typeof u === "boolean") return ok(u);
|
|
2073
|
-
if (typeof u === "number") {
|
|
2074
|
-
if (Object.is(u, -0)) return ok({
|
|
2075
|
-
$vex: "number",
|
|
2076
|
-
value: "-0"
|
|
2077
|
-
});
|
|
2078
|
-
return Number.isFinite(u) ? ok(u) : ok({
|
|
2079
|
-
$vex: "number",
|
|
2080
|
-
value: Number.isNaN(u) ? "NaN" : u > 0 ? "Infinity" : "-Infinity"
|
|
2081
|
-
});
|
|
2082
|
-
}
|
|
2083
|
-
if (Array.isArray(u)) {
|
|
2084
|
-
const out = [];
|
|
2085
|
-
for (const x of u) {
|
|
2086
|
-
const r = encodeValue(x, domains);
|
|
2087
|
-
if (!r.ok) return r;
|
|
2088
|
-
out.push(r.value);
|
|
3845
|
+
if (kind === "fn") {
|
|
3846
|
+
fn = parent;
|
|
3847
|
+
fnVisits.set(parent.doc, parent);
|
|
2089
3848
|
}
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
3849
|
+
if (kind === "call") calls = {
|
|
3850
|
+
item: parent,
|
|
3851
|
+
up: calls
|
|
3852
|
+
};
|
|
3853
|
+
return {
|
|
3854
|
+
axes,
|
|
3855
|
+
axis,
|
|
3856
|
+
fn,
|
|
3857
|
+
calls
|
|
3858
|
+
};
|
|
3859
|
+
};
|
|
3860
|
+
/**
|
|
3861
|
+
* This function gives the context of a visit. It walks up to a known context with a loop, not with recursion. The
|
|
3862
|
+
* context of no visit is the context of the root.
|
|
3863
|
+
*/
|
|
3864
|
+
const contextOf = (v) => {
|
|
3865
|
+
const todo = [];
|
|
3866
|
+
let x = v;
|
|
3867
|
+
while (x !== void 0 && !memo.has(x)) {
|
|
3868
|
+
todo.push(x);
|
|
3869
|
+
x = x.parent;
|
|
2098
3870
|
}
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
const d = domains.find((dom) => dom.encode !== void 0 && dom.is(u));
|
|
2105
|
-
if (d?.encode !== void 0) return ok({
|
|
2106
|
-
$vex: "domain",
|
|
2107
|
-
domain: d.name,
|
|
2108
|
-
value: d.encode(u)
|
|
2109
|
-
});
|
|
2110
|
-
return fail(vexError("bad-expression", "a literal value is not JSON data and no domain encodes it"));
|
|
2111
|
-
}
|
|
2112
|
-
const NUMBERS = {
|
|
2113
|
-
"-0": -0,
|
|
2114
|
-
NaN: NaN,
|
|
2115
|
-
Infinity: Number.POSITIVE_INFINITY,
|
|
2116
|
-
"-Infinity": Number.NEGATIVE_INFINITY
|
|
2117
|
-
};
|
|
2118
|
-
/** This function reads a value from its JSON form. */
|
|
2119
|
-
function decodeValue(u, domains) {
|
|
2120
|
-
if (Array.isArray(u)) {
|
|
2121
|
-
const out = [];
|
|
2122
|
-
for (const x of u) {
|
|
2123
|
-
const r = decodeValue(x, domains);
|
|
2124
|
-
if (!r.ok) return r;
|
|
2125
|
-
out.push(r.value);
|
|
3871
|
+
let ctx = x === void 0 ? ROOT : memo.get(x);
|
|
3872
|
+
for (let i = todo.length - 1; i >= 0; i--) {
|
|
3873
|
+
const child = todo[i];
|
|
3874
|
+
ctx = child.parent === void 0 ? ROOT : step(ctx, child.parent, child);
|
|
3875
|
+
memo.set(child, ctx);
|
|
2126
3876
|
}
|
|
2127
|
-
return
|
|
3877
|
+
return ctx;
|
|
3878
|
+
};
|
|
3879
|
+
const groups = /* @__PURE__ */ new Map();
|
|
3880
|
+
for (const use of uses) {
|
|
3881
|
+
const fn = contextOf(use.visit).fn;
|
|
3882
|
+
const group = groups.get(fn) ?? [];
|
|
3883
|
+
group.push(use);
|
|
3884
|
+
groups.set(fn, group);
|
|
2128
3885
|
}
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
3886
|
+
const out = [];
|
|
3887
|
+
/** This function gives the reads of the references of a function at a call, inside the axes of the call. */
|
|
3888
|
+
const emit = (group, base, outside) => {
|
|
3889
|
+
const baseAxes = contextOf(base).axes;
|
|
3890
|
+
const inside = (v) => [...outside, ...itemsUntil(contextOf(v).axes, baseAxes)];
|
|
3891
|
+
const pending = [];
|
|
3892
|
+
const inCall = (v, call) => {
|
|
3893
|
+
for (let c = contextOf(v).calls; c !== void 0 && c.item.depth >= call.depth; c = c.up) if (c.item === call) return true;
|
|
3894
|
+
return false;
|
|
3895
|
+
};
|
|
3896
|
+
const flush = (until) => {
|
|
3897
|
+
for (;;) {
|
|
3898
|
+
const top = pending.at(-1);
|
|
3899
|
+
if (top === void 0 || inCall(until, top.call)) return;
|
|
3900
|
+
pending.pop();
|
|
3901
|
+
emit(groups.get(top.fn) ?? [], top.fn, top.axes);
|
|
3902
|
+
}
|
|
3903
|
+
};
|
|
3904
|
+
for (const use of group) {
|
|
3905
|
+
flush(use.visit);
|
|
3906
|
+
if (use.root === "self" && !use.address) {
|
|
3907
|
+
const node = nodes.get(use.visit.doc);
|
|
3908
|
+
const axes = inside(use.visit);
|
|
3909
|
+
out.push({
|
|
3910
|
+
at: node.at ?? [],
|
|
3911
|
+
path: node.path,
|
|
3912
|
+
axes: axes.map((a) => a.t),
|
|
3913
|
+
axisAt: axes.map((a) => a.at)
|
|
3914
|
+
});
|
|
3915
|
+
}
|
|
3916
|
+
const call = use.visit.parent;
|
|
3917
|
+
if (call !== void 0 && kindOf$1(call) === "call" && use.visit.slot[0] === "fn") {
|
|
3918
|
+
const fn = functionOf(call);
|
|
3919
|
+
if (fn !== void 0) pending.push({
|
|
3920
|
+
call,
|
|
3921
|
+
fn,
|
|
3922
|
+
axes: inside(call)
|
|
3923
|
+
});
|
|
3924
|
+
}
|
|
2136
3925
|
}
|
|
2137
|
-
|
|
3926
|
+
flush();
|
|
2138
3927
|
};
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
if (kind === "number" && typeof u["value"] === "string" && Object.hasOwn(NUMBERS, u["value"])) return ok(NUMBERS[u["value"]]);
|
|
2142
|
-
if (kind === "domain" && typeof u["domain"] === "string") {
|
|
2143
|
-
const name = u["domain"];
|
|
2144
|
-
const d = domains.find((dom) => dom.name === name);
|
|
2145
|
-
if (d?.decode === void 0) return fail(vexError("bad-expression", `no domain "${name}" decodes a literal`));
|
|
2146
|
-
return ok(d.decode(u["value"]));
|
|
2147
|
-
}
|
|
2148
|
-
return fields(u);
|
|
3928
|
+
emit(groups.get(void 0) ?? [], void 0, []);
|
|
3929
|
+
return out;
|
|
2149
3930
|
}
|
|
3931
|
+
//#endregion
|
|
3932
|
+
//#region src/json.ts
|
|
2150
3933
|
/** This function applies `f` to each literal value of a JSON tree of an expression. */
|
|
2151
3934
|
function mapValues(u, f) {
|
|
2152
3935
|
if (!isRecord(u)) return ok(u);
|
|
@@ -2264,7 +4047,10 @@ function serialize(e, domains = []) {
|
|
|
2264
4047
|
return fail(vexError("threw", "the expression has no JSON form", { thrown }));
|
|
2265
4048
|
}
|
|
2266
4049
|
}
|
|
2267
|
-
/**
|
|
4050
|
+
/**
|
|
4051
|
+
* This function reads an expression from JSON text. It checks the whole tree. The text can also be a document: then
|
|
4052
|
+
* `fromDocument` reads it (DOC.ENTRY).
|
|
4053
|
+
*/
|
|
2268
4054
|
function parse(text, domains = []) {
|
|
2269
4055
|
let raw;
|
|
2270
4056
|
try {
|
|
@@ -2272,6 +4058,10 @@ function parse(text, domains = []) {
|
|
|
2272
4058
|
} catch (thrown) {
|
|
2273
4059
|
return fail(vexError("bad-expression", "the text is not JSON", { thrown }));
|
|
2274
4060
|
}
|
|
4061
|
+
if (isDocument(raw)) {
|
|
4062
|
+
const read = fromDocument(raw, domains);
|
|
4063
|
+
return !read.ok || isExpr(read.value) ? read : fail(vexError("bad-expression", "the JSON is not a valid Vex expression"));
|
|
4064
|
+
}
|
|
2275
4065
|
try {
|
|
2276
4066
|
const tree = mapValues(raw, (v) => decodeValue(v, domains));
|
|
2277
4067
|
if (!tree.ok) return fail(tree.error);
|
|
@@ -2282,6 +4072,96 @@ function parse(text, domains = []) {
|
|
|
2282
4072
|
}
|
|
2283
4073
|
}
|
|
2284
4074
|
//#endregion
|
|
4075
|
+
//#region src/plan.ts
|
|
4076
|
+
/**
|
|
4077
|
+
* The reference tier of Vex: a program as a plan of the kernel of coeval (decision D13 of coeval). The kernel evaluates
|
|
4078
|
+
* the document of the program with the machine of `kernel.ts`. The closure compiler and the loop compiler give its
|
|
4079
|
+
* results (EVAL.REFERENCE), and the test kit compares them on random programs.
|
|
4080
|
+
*/
|
|
4081
|
+
/** This function gives the context of the kernel at an origin: the position of the origin, and the names of the caller. */
|
|
4082
|
+
const contextOf = ({ space, origin, vars, tables }) => context({
|
|
4083
|
+
self: vexSelf(space, origin, tables),
|
|
4084
|
+
names: vars ?? {}
|
|
4085
|
+
});
|
|
4086
|
+
/** This function gives the result of the kernel for a result of Vex. */
|
|
4087
|
+
const toOutcome = (r) => r.ok ? {
|
|
4088
|
+
ok: true,
|
|
4089
|
+
value: r.value
|
|
4090
|
+
} : {
|
|
4091
|
+
ok: false,
|
|
4092
|
+
error: toKernelError(r.error)
|
|
4093
|
+
};
|
|
4094
|
+
/**
|
|
4095
|
+
* This function gives the plan of a program in the kernel (EVAL.REFERENCE). A document stays as it is, after the check
|
|
4096
|
+
* of `fromDocument`. An expression gets its document: a literal has its JSON form, or the value of the host when it has
|
|
4097
|
+
* no JSON form. A program that is not valid gives the error of `compile` at each run (EVAL.SHAPE, EVAL.NESTING).
|
|
4098
|
+
*/
|
|
4099
|
+
function kernelPlan(source, opts = {}) {
|
|
4100
|
+
const domains = opts.domains ?? [];
|
|
4101
|
+
let doc;
|
|
4102
|
+
let bad;
|
|
4103
|
+
if (isDocument(source)) {
|
|
4104
|
+
const read = fromDocument(source, domains);
|
|
4105
|
+
if (read.ok) doc = source;
|
|
4106
|
+
else bad = read.error;
|
|
4107
|
+
} else {
|
|
4108
|
+
const shape = shapeError(source);
|
|
4109
|
+
const nesting = shape === void 0 ? nestingOf(source) : 0;
|
|
4110
|
+
const reason = shape ?? (nesting > 500 ? `the expression is nested ${String(nesting)} levels deep, and the limit is ${String(500)}` : void 0);
|
|
4111
|
+
if (reason === void 0) doc = writeDocument(source, {
|
|
4112
|
+
domains,
|
|
4113
|
+
literals: "host",
|
|
4114
|
+
order: "document"
|
|
4115
|
+
});
|
|
4116
|
+
else bad = vexError("bad-expression", reason);
|
|
4117
|
+
}
|
|
4118
|
+
const machine = vexMachine(opts);
|
|
4119
|
+
const plan = prepare(machine, doc);
|
|
4120
|
+
const early = ({ space, origin }) => {
|
|
4121
|
+
if (!space.has(origin)) return {
|
|
4122
|
+
ok: false,
|
|
4123
|
+
error: toKernelError(vexError("unknown-key", `the space has no key "${origin}"`, {
|
|
4124
|
+
origin,
|
|
4125
|
+
focus: origin
|
|
4126
|
+
}))
|
|
4127
|
+
};
|
|
4128
|
+
return bad === void 0 ? void 0 : {
|
|
4129
|
+
ok: false,
|
|
4130
|
+
error: toKernelError(bad)
|
|
4131
|
+
};
|
|
4132
|
+
};
|
|
4133
|
+
const evaluate = (r) => early(r) ?? plan.evaluate(contextOf(r));
|
|
4134
|
+
const reference$1 = (r) => early(r) ?? reference(machine, doc, contextOf(r), { definitions: vexDefinitions(opts) });
|
|
4135
|
+
const agree$1 = (r, other) => {
|
|
4136
|
+
const tier = other === void 0 ? {} : { other: () => toOutcome(other()) };
|
|
4137
|
+
const first = early(r);
|
|
4138
|
+
if (first === void 0) return agree(machine, doc, contextOf(r), {
|
|
4139
|
+
definitions: vexDefinitions(opts),
|
|
4140
|
+
equal: sameVex,
|
|
4141
|
+
...tier
|
|
4142
|
+
});
|
|
4143
|
+
const second = tier.other?.() ?? first;
|
|
4144
|
+
return !second.ok && second.error.code === first.error.code ? {
|
|
4145
|
+
ok: true,
|
|
4146
|
+
result: first,
|
|
4147
|
+
effects: []
|
|
4148
|
+
} : {
|
|
4149
|
+
ok: false,
|
|
4150
|
+
tier: "other",
|
|
4151
|
+
plan: first,
|
|
4152
|
+
other: second,
|
|
4153
|
+
reason: "The plan does not evaluate the program, and the tier gives another result."
|
|
4154
|
+
};
|
|
4155
|
+
};
|
|
4156
|
+
return {
|
|
4157
|
+
doc,
|
|
4158
|
+
evaluate,
|
|
4159
|
+
reference: reference$1,
|
|
4160
|
+
agree: agree$1,
|
|
4161
|
+
run: (r) => toVexResult(evaluate(r))
|
|
4162
|
+
};
|
|
4163
|
+
}
|
|
4164
|
+
//#endregion
|
|
2285
4165
|
//#region src/traversal.ts
|
|
2286
4166
|
/** Standard monoids. */
|
|
2287
4167
|
const monoids = {
|
|
@@ -2757,7 +4637,7 @@ var SheetRun = class SheetRun {
|
|
|
2757
4637
|
*/
|
|
2758
4638
|
#compiled(t, column, program) {
|
|
2759
4639
|
const compile = this.#compile;
|
|
2760
|
-
if (compile === void 0 || t.columns.get(column) !== program) return void 0;
|
|
4640
|
+
if (compile === void 0 || t.columns.get(column) !== program || typeof program !== "object" || program === null) return void 0;
|
|
2761
4641
|
const { domains, fns, extensions } = this.#compileOptions;
|
|
2762
4642
|
const hit = PROGRAMS.get(program);
|
|
2763
4643
|
if (hit !== void 0 && hit.compile === compile && hit.domains === domains && hit.fns === fns && hit.extensions === extensions) return hit.program;
|
|
@@ -2901,6 +4781,11 @@ const runtimeOptions = (options) => ({
|
|
|
2901
4781
|
const kindOf = (u) => u?.kind;
|
|
2902
4782
|
const isArg = (u) => kindOf(u) === "vex.arg";
|
|
2903
4783
|
const isChainLike = (u) => kindOf(u) === "vex.chain" || kindOf(u) === "vex.list-chain";
|
|
4784
|
+
/** This function gives the expression of the list of `append` and `without`. The list is a chain, not a literal. */
|
|
4785
|
+
function listProgram(op, other) {
|
|
4786
|
+
if (!isChainLike(other)) throw new TypeError(`${op} needs a list chain`);
|
|
4787
|
+
return other.program;
|
|
4788
|
+
}
|
|
2904
4789
|
/** This function turns a builder argument into an expression. A bare string is a field reference at `addr`. */
|
|
2905
4790
|
function toExpr(a, addr) {
|
|
2906
4791
|
if (typeof a === "string") return ref(toPath(a), addr);
|
|
@@ -3298,6 +5183,18 @@ var ListChainImpl = class ListChainImpl {
|
|
|
3298
5183
|
nth(i, opts) {
|
|
3299
5184
|
return this.#op("nth", [lit(i)], opts);
|
|
3300
5185
|
}
|
|
5186
|
+
drop(n, opts) {
|
|
5187
|
+
return this.#list("drop", [lit(n)], opts);
|
|
5188
|
+
}
|
|
5189
|
+
append(other, opts) {
|
|
5190
|
+
return this.#list("append", [listProgram("append", other)], opts);
|
|
5191
|
+
}
|
|
5192
|
+
without(other, opts) {
|
|
5193
|
+
return this.#list("without", [listProgram("without", other)], opts);
|
|
5194
|
+
}
|
|
5195
|
+
flat(opts) {
|
|
5196
|
+
return this.#list("flat", [], opts);
|
|
5197
|
+
}
|
|
3301
5198
|
each(body, opts) {
|
|
3302
5199
|
const start = new ChainImpl({
|
|
3303
5200
|
...this.#s,
|
|
@@ -3470,6 +5367,6 @@ function vex(...domains) {
|
|
|
3470
5367
|
};
|
|
3471
5368
|
}
|
|
3472
5369
|
//#endregion
|
|
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 };
|
|
5370
|
+
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 };
|
|
3474
5371
|
|
|
3475
5372
|
//# sourceMappingURL=index.js.map
|