@mark1russell7/vex 0.9.1 → 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 +18 -2
- package/build/index.d.ts +123 -62
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1869 -497
- package/build/index.js.map +1 -1
- package/package.json +5 -2
- package/src/builder.ts +54 -13
- package/src/deps.ts +174 -60
- package/src/document.ts +133 -248
- package/src/errors.ts +7 -3
- package/src/evaluate.ts +59 -13
- package/src/index.ts +4 -1
- 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.
|
|
@@ -1052,125 +1053,15 @@ const LIST_OPS = /* @__PURE__ */ new Set([
|
|
|
1052
1053
|
"errors",
|
|
1053
1054
|
"sortBy",
|
|
1054
1055
|
"take",
|
|
1055
|
-
"nth"
|
|
1056
|
+
"nth",
|
|
1057
|
+
"drop",
|
|
1058
|
+
"append",
|
|
1059
|
+
"flat",
|
|
1060
|
+
"without"
|
|
1056
1061
|
]);
|
|
1057
1062
|
/** This function tells if a name is the name of a core list op. */
|
|
1058
1063
|
const isListOp = (op) => LIST_OPS.has(op);
|
|
1059
1064
|
//#endregion
|
|
1060
|
-
//#region ../../node_modules/.pnpm/@mark1russell7+coeval@0.1.0/node_modules/@mark1russell7/coeval/build/index.js
|
|
1061
|
-
/** The keys that an envelope can have. */
|
|
1062
|
-
const ENVELOPE_KEYS = [
|
|
1063
|
-
"type",
|
|
1064
|
-
"value",
|
|
1065
|
-
"name",
|
|
1066
|
-
"own"
|
|
1067
|
-
];
|
|
1068
|
-
/**
|
|
1069
|
-
* This function tells if a value is a JSON record (not an array and not null).
|
|
1070
|
-
*
|
|
1071
|
-
* @param value - The value to examine.
|
|
1072
|
-
* @returns `true` for a record.
|
|
1073
|
-
*/
|
|
1074
|
-
function isRecord$1(value) {
|
|
1075
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1076
|
-
}
|
|
1077
|
-
/**
|
|
1078
|
-
* This function reads a field of a record. It reads only the own fields, thus `constructor` is not a field.
|
|
1079
|
-
*
|
|
1080
|
-
* @param record - The record.
|
|
1081
|
-
* @param key - The name of the field.
|
|
1082
|
-
* @returns The value of the field, or `undefined` when the record has no such field.
|
|
1083
|
-
*/
|
|
1084
|
-
function field(record, key) {
|
|
1085
|
-
return Object.hasOwn(record, key) ? record[key] : void 0;
|
|
1086
|
-
}
|
|
1087
|
-
/**
|
|
1088
|
-
* This function tells if a value is a key of a path: a text, or a whole number from 0.
|
|
1089
|
-
*
|
|
1090
|
-
* @param value - The value to examine.
|
|
1091
|
-
* @returns `true` for a key.
|
|
1092
|
-
*/
|
|
1093
|
-
function isKey(value) {
|
|
1094
|
-
return typeof value === "string" || typeof value === "number" && Number.isInteger(value) && value >= 0;
|
|
1095
|
-
}
|
|
1096
|
-
/**
|
|
1097
|
-
* This function tells if a value is a reference to a type. The reference is a text, a list of keys, or a document
|
|
1098
|
-
* of type `ref` whose path starts at the root `world`.
|
|
1099
|
-
*
|
|
1100
|
-
* @param value - The value to examine.
|
|
1101
|
-
* @returns `true` for a reference to a type.
|
|
1102
|
-
*/
|
|
1103
|
-
function isTypeRef(value) {
|
|
1104
|
-
if (typeof value === "string") return value !== "";
|
|
1105
|
-
if (Array.isArray(value)) return value.length > 0 && value.every(isKey);
|
|
1106
|
-
if (!isRecord$1(value)) return false;
|
|
1107
|
-
const path = field(value, "value");
|
|
1108
|
-
return field(value, "type") === "ref" && Array.isArray(path) && path[0] === "world";
|
|
1109
|
-
}
|
|
1110
|
-
/**
|
|
1111
|
-
* This function gives the path of a reference to a type, from the root of the world.
|
|
1112
|
-
*
|
|
1113
|
-
* @param type - The reference.
|
|
1114
|
-
* @returns The keys from the root of the world, or `undefined` for a computed step that has no fixed key.
|
|
1115
|
-
*/
|
|
1116
|
-
function typePath(type) {
|
|
1117
|
-
if (typeof type === "string") return [type];
|
|
1118
|
-
if (Array.isArray(type)) return type;
|
|
1119
|
-
const steps = type.value.slice(1);
|
|
1120
|
-
return steps.every(isKey) ? steps : void 0;
|
|
1121
|
-
}
|
|
1122
|
-
/**
|
|
1123
|
-
* This function gives the text of a type for a reader: its keys, joined with a dot.
|
|
1124
|
-
*
|
|
1125
|
-
* @param type - The reference to the type.
|
|
1126
|
-
* @returns The text, for example `git.push`.
|
|
1127
|
-
*/
|
|
1128
|
-
function typeName(type) {
|
|
1129
|
-
return typePath(type)?.join(".") ?? "(computed)";
|
|
1130
|
-
}
|
|
1131
|
-
/**
|
|
1132
|
-
* This function tells if a value has the form of an envelope. That form is a record with a reference to a type
|
|
1133
|
-
* and a `value`. It has no other keys than the keys of an envelope. A contract decides where an envelope is
|
|
1134
|
-
* expected. This function only checks the form.
|
|
1135
|
-
*
|
|
1136
|
-
* @param value - The value to examine.
|
|
1137
|
-
* @returns `true` when the value has the form of an envelope.
|
|
1138
|
-
*/
|
|
1139
|
-
function isEnvelope(value) {
|
|
1140
|
-
if (!isRecord$1(value)) return false;
|
|
1141
|
-
if (!isTypeRef(field(value, "type")) || !Object.hasOwn(value, "value")) return false;
|
|
1142
|
-
const name = field(value, "name");
|
|
1143
|
-
if (name !== void 0 && typeof name !== "string") return false;
|
|
1144
|
-
const own = field(value, "own");
|
|
1145
|
-
if (own !== void 0 && !isRecord$1(own)) return false;
|
|
1146
|
-
return Object.keys(value).every((key) => ENVELOPE_KEYS.includes(key));
|
|
1147
|
-
}
|
|
1148
|
-
/**
|
|
1149
|
-
* This function makes an envelope.
|
|
1150
|
-
*
|
|
1151
|
-
* @param type - The reference to the kind.
|
|
1152
|
-
* @param value - The value.
|
|
1153
|
-
* @param header - The optional name and own entries.
|
|
1154
|
-
* @returns The envelope.
|
|
1155
|
-
*/
|
|
1156
|
-
function typed(type, value, header = {}) {
|
|
1157
|
-
return {
|
|
1158
|
-
type,
|
|
1159
|
-
value,
|
|
1160
|
-
...header.name === void 0 ? {} : { name: header.name },
|
|
1161
|
-
...header.own === void 0 ? {} : { own: header.own }
|
|
1162
|
-
};
|
|
1163
|
-
}
|
|
1164
|
-
/**
|
|
1165
|
-
* This function makes a reference document: a path from a root of the context.
|
|
1166
|
-
*
|
|
1167
|
-
* @param path - The steps. The first step is a root: `self`, `names`, `world`, `event` or `caps`.
|
|
1168
|
-
* @returns The document of type `ref`.
|
|
1169
|
-
*/
|
|
1170
|
-
function ref$1(...path) {
|
|
1171
|
-
return typed("ref", path);
|
|
1172
|
-
}
|
|
1173
|
-
//#endregion
|
|
1174
1065
|
//#region src/codec.ts
|
|
1175
1066
|
/** This function tells if a value is a record: an object that is not an array. */
|
|
1176
1067
|
const isRecord = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
@@ -1213,57 +1104,1452 @@ function encodeValue(u, domains) {
|
|
|
1213
1104
|
value: out
|
|
1214
1105
|
} : out);
|
|
1215
1106
|
}
|
|
1216
|
-
const d = domains.find((dom) => dom.encode !== void 0 && dom.is(u));
|
|
1217
|
-
if (d?.encode !== void 0) return ok({
|
|
1218
|
-
$vex: "domain",
|
|
1219
|
-
domain: d.name,
|
|
1220
|
-
value: d.encode(u)
|
|
1221
|
-
});
|
|
1222
|
-
return fail(vexError("bad-expression", "a literal value is not JSON data and no domain encodes it"));
|
|
1223
|
-
}
|
|
1224
|
-
const NUMBERS = {
|
|
1225
|
-
"-0": -0,
|
|
1226
|
-
NaN: NaN,
|
|
1227
|
-
Infinity: Number.POSITIVE_INFINITY,
|
|
1228
|
-
"-Infinity": Number.NEGATIVE_INFINITY
|
|
1229
|
-
};
|
|
1230
|
-
/** This function reads a value from its JSON form. */
|
|
1231
|
-
function decodeValue(u, domains) {
|
|
1232
|
-
if (Array.isArray(u)) {
|
|
1233
|
-
const out = [];
|
|
1234
|
-
for (const x of u) {
|
|
1235
|
-
const r = decodeValue(x, domains);
|
|
1236
|
-
if (!r.ok) return r;
|
|
1237
|
-
out.push(r.value);
|
|
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);
|
|
1238
2486
|
}
|
|
1239
|
-
return ok(out);
|
|
1240
2487
|
}
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
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, {});
|
|
1248
2535
|
}
|
|
1249
|
-
return ok(out);
|
|
1250
|
-
};
|
|
1251
|
-
const kind = u["$vex"];
|
|
1252
|
-
if (kind === "escape" && isRecord(u["value"])) return fields(u["value"]);
|
|
1253
|
-
if (kind === "number" && typeof u["value"] === "string" && Object.hasOwn(NUMBERS, u["value"])) return ok(NUMBERS[u["value"]]);
|
|
1254
|
-
if (kind === "domain" && typeof u["domain"] === "string") {
|
|
1255
|
-
const name = u["domain"];
|
|
1256
|
-
const d = domains.find((dom) => dom.name === name);
|
|
1257
|
-
if (d?.decode === void 0) return fail(vexError("bad-expression", `no domain "${name}" decodes a literal`));
|
|
1258
|
-
return ok(d.decode(u["value"]));
|
|
1259
2536
|
}
|
|
1260
|
-
|
|
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
|
+
});
|
|
1261
2547
|
}
|
|
1262
2548
|
//#endregion
|
|
1263
2549
|
//#region src/document.ts
|
|
1264
2550
|
/**
|
|
1265
|
-
* The documents of Vex. A document is the form of an expression in coeval, the
|
|
1266
|
-
*
|
|
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:
|
|
1267
2553
|
* <https://github.com/mark1russell7/coeval/blob/main/docs/DECISIONS.md>.
|
|
1268
2554
|
*
|
|
1269
2555
|
* | Expression | Document |
|
|
@@ -1284,27 +2570,12 @@ function decodeValue(u, domains) {
|
|
|
1284
2570
|
* | an axis | `{ "type": ["axes", name], "value": { ...parameters } }` |
|
|
1285
2571
|
*
|
|
1286
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.
|
|
1287
2577
|
*/
|
|
1288
|
-
/** The
|
|
1289
|
-
const PLAIN_AXES = /* @__PURE__ */ new Set([
|
|
1290
|
-
"all",
|
|
1291
|
-
"others",
|
|
1292
|
-
"other",
|
|
1293
|
-
"children",
|
|
1294
|
-
"ancestors",
|
|
1295
|
-
"descendants",
|
|
1296
|
-
"siblings",
|
|
1297
|
-
"successors",
|
|
1298
|
-
"predecessors",
|
|
1299
|
-
"reachable"
|
|
1300
|
-
]);
|
|
1301
|
-
/** The moves without a value. */
|
|
1302
|
-
const PLAIN_MOVES = /* @__PURE__ */ new Map([
|
|
1303
|
-
["other", other],
|
|
1304
|
-
["origin", origin],
|
|
1305
|
-
["parent", parent]
|
|
1306
|
-
]);
|
|
1307
|
-
/** The stop of a conversion. The functions of this module throw it, and the top of the conversion gives its error. */
|
|
2578
|
+
/** The stop of a reading. The functions of this module throw it, and the top of the reading gives its error. */
|
|
1308
2579
|
var Stop = class {
|
|
1309
2580
|
error;
|
|
1310
2581
|
constructor(error) {
|
|
@@ -1313,16 +2584,20 @@ var Stop = class {
|
|
|
1313
2584
|
};
|
|
1314
2585
|
/** This function gives the location of a child: a JSON Pointer, with the escapes of RFC 6901. */
|
|
1315
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}`;
|
|
1316
2591
|
/** This function stops the reading of a document that is not valid at `where`. */
|
|
1317
2592
|
function bad(where, reason) {
|
|
1318
|
-
throw new Stop(vexError("bad-expression",
|
|
2593
|
+
throw new Stop(vexError("bad-expression", notValid(where, reason)));
|
|
1319
2594
|
}
|
|
1320
2595
|
/** This function does a conversion. It gives its value, or the error of the conversion. It does not throw. */
|
|
1321
2596
|
function attempt(work, threw, deep) {
|
|
1322
2597
|
try {
|
|
1323
2598
|
return ok(work());
|
|
1324
2599
|
} catch (thrown) {
|
|
1325
|
-
if (thrown instanceof Stop) return fail(thrown.error);
|
|
2600
|
+
if (thrown instanceof Stop || thrown instanceof WriteStop) return fail(thrown.error);
|
|
1326
2601
|
if (isStackOverflow(thrown)) return fail(vexError("depth", deep, { thrown }));
|
|
1327
2602
|
return fail(vexError("threw", threw, { thrown }));
|
|
1328
2603
|
}
|
|
@@ -1342,131 +2617,41 @@ function isDocument(u) {
|
|
|
1342
2617
|
function toDocument(e, domains = []) {
|
|
1343
2618
|
const shape = shapeError(e);
|
|
1344
2619
|
if (shape !== void 0) return fail(vexError("bad-expression", shape));
|
|
1345
|
-
|
|
2620
|
+
const options = {
|
|
2621
|
+
domains,
|
|
2622
|
+
literals: "json",
|
|
2623
|
+
order: "document"
|
|
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");
|
|
2626
|
+
}
|
|
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];
|
|
1346
2635
|
}
|
|
1347
2636
|
/**
|
|
1348
|
-
* This function reads an expression from a document (DOC.TERM). It is the inverse of `toDocument`.
|
|
1349
|
-
*
|
|
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).
|
|
1350
2640
|
*/
|
|
1351
2641
|
function fromDocument(doc, domains = []) {
|
|
1352
|
-
const
|
|
1353
|
-
if (!
|
|
1354
|
-
const
|
|
1355
|
-
|
|
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");
|
|
1356
2648
|
}
|
|
1357
|
-
/** The
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
/** This method gives the document of a term. A literal is its JSON value, except a value with the shape of an envelope. */
|
|
1364
|
-
term(e) {
|
|
1365
|
-
if (e.tag !== "lit") return this.envelope(e);
|
|
1366
|
-
const value = this.#value(e.value);
|
|
1367
|
-
return isEnvelope(value) ? typed("lit", value) : value;
|
|
1368
|
-
}
|
|
1369
|
-
/** This method gives the document of a term as an envelope. Here a literal is an envelope too. */
|
|
1370
|
-
envelope(e) {
|
|
1371
|
-
switch (e.tag) {
|
|
1372
|
-
case "lit": return typed("lit", this.#value(e.value));
|
|
1373
|
-
case "ref": return ref$1("self", ...this.#moves(e.at), ...e.path);
|
|
1374
|
-
case "var": return ref$1("names", e.name);
|
|
1375
|
-
case "app": return typed(["ops", ...e.op.split(".")], e.args.map((a) => this.term(a)));
|
|
1376
|
-
case "let": return typed("let", {
|
|
1377
|
-
bind: this.#names(e.bind),
|
|
1378
|
-
body: this.term(e.body)
|
|
1379
|
-
});
|
|
1380
|
-
case "fn": return typed("fn", {
|
|
1381
|
-
params: [...e.params],
|
|
1382
|
-
body: this.term(e.body)
|
|
1383
|
-
});
|
|
1384
|
-
case "call": return typed("call", {
|
|
1385
|
-
fn: ref$1("names", e.fn),
|
|
1386
|
-
args: e.args.map((a) => this.term(a))
|
|
1387
|
-
});
|
|
1388
|
-
case "rec": return typed("record", this.#names(e.fields));
|
|
1389
|
-
case "each": return typed("each", {
|
|
1390
|
-
axis: this.#axis(e.axis),
|
|
1391
|
-
body: this.term(e.body),
|
|
1392
|
-
...this.#at(e.at)
|
|
1393
|
-
});
|
|
1394
|
-
case "fold": return typed("fold", {
|
|
1395
|
-
axis: this.#axis(e.axis),
|
|
1396
|
-
init: this.#names(e.init),
|
|
1397
|
-
step: this.#names(e.step),
|
|
1398
|
-
result: this.term(e.result),
|
|
1399
|
-
...this.#at(e.at)
|
|
1400
|
-
});
|
|
1401
|
-
case "iterate": return typed("iterate", {
|
|
1402
|
-
limit: e.limit,
|
|
1403
|
-
init: this.#names(e.init),
|
|
1404
|
-
step: this.#names(e.step),
|
|
1405
|
-
...e.until === void 0 ? {} : { until: this.term(e.until) },
|
|
1406
|
-
result: this.term(e.result)
|
|
1407
|
-
});
|
|
1408
|
-
case "ext": return typed(["ext", ...e.kind.split(".")], e.data);
|
|
1409
|
-
}
|
|
1410
|
-
}
|
|
1411
|
-
/** This method gives the JSON form of a literal value (IR.JSON). */
|
|
1412
|
-
#value(u) {
|
|
1413
|
-
const r = encodeValue(u, this.#domains);
|
|
1414
|
-
if (!r.ok) throw new Stop(r.error);
|
|
1415
|
-
return r.value;
|
|
1416
|
-
}
|
|
1417
|
-
/** This method gives the documents of the terms of a record of names. */
|
|
1418
|
-
#names(names) {
|
|
1419
|
-
return Object.fromEntries(Object.entries(names).map(([name, x]) => [name, this.term(x)]));
|
|
1420
|
-
}
|
|
1421
|
-
/** This method gives the step documents of the moves of an address. */
|
|
1422
|
-
#moves(at) {
|
|
1423
|
-
return (at ?? []).map((m) => this.#move(m));
|
|
1424
|
-
}
|
|
1425
|
-
/** This method gives the step document of a move. A computed key or position is an envelope (DOC.MOVE). */
|
|
1426
|
-
#move(m) {
|
|
1427
|
-
switch (m.t) {
|
|
1428
|
-
case "key": return typed(["moves", "key"], typeof m.key === "string" ? m.key : this.envelope(m.key));
|
|
1429
|
-
case "index": return typed(["moves", "index"], typeof m.i === "number" ? m.i : this.envelope(m.i));
|
|
1430
|
-
case "offset": return typed(["moves", "offset"], [...m.d]);
|
|
1431
|
-
case "other":
|
|
1432
|
-
case "origin":
|
|
1433
|
-
case "parent": return typed(["moves", m.t], null);
|
|
1434
|
-
}
|
|
1435
|
-
}
|
|
1436
|
-
/** This method gives the field `at` of `each` and `fold`: a reference from `self` with moves only, or no field. */
|
|
1437
|
-
#at(at) {
|
|
1438
|
-
return at === void 0 || at.length === 0 ? {} : { at: ref$1("self", ...this.#moves(at)) };
|
|
1439
|
-
}
|
|
1440
|
-
/** This method gives the document of an axis (DOC.AXIS). */
|
|
1441
|
-
#axis(a) {
|
|
1442
|
-
const name = ["axes", a.t];
|
|
1443
|
-
switch (a.t) {
|
|
1444
|
-
case "neighbors": return typed(name, { n: a.n });
|
|
1445
|
-
case "window": return typed(name, {
|
|
1446
|
-
from: a.from,
|
|
1447
|
-
to: a.to
|
|
1448
|
-
});
|
|
1449
|
-
case "table": return typed(name, { name: a.name });
|
|
1450
|
-
case "list": return typed(name, { list: this.term(a.list) });
|
|
1451
|
-
case "reverse": return typed(name, { axis: this.#axis(a.axis) });
|
|
1452
|
-
case "where": return typed(name, {
|
|
1453
|
-
axis: this.#axis(a.axis),
|
|
1454
|
-
test: this.term(a.test)
|
|
1455
|
-
});
|
|
1456
|
-
case "all":
|
|
1457
|
-
case "others":
|
|
1458
|
-
case "other":
|
|
1459
|
-
case "children":
|
|
1460
|
-
case "ancestors":
|
|
1461
|
-
case "descendants":
|
|
1462
|
-
case "siblings":
|
|
1463
|
-
case "successors":
|
|
1464
|
-
case "predecessors":
|
|
1465
|
-
case "reachable": return typed(name, {});
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
|
-
};
|
|
1469
|
-
/** The reader of documents. Its methods throw a `Stop` at the first part that is not valid. */
|
|
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
|
+
*/
|
|
1470
2655
|
var Reader = class {
|
|
1471
2656
|
#domains;
|
|
1472
2657
|
constructor(domains) {
|
|
@@ -1479,65 +2664,39 @@ var Reader = class {
|
|
|
1479
2664
|
/** This method reads a term in an envelope. */
|
|
1480
2665
|
#envelope(u, where) {
|
|
1481
2666
|
const at = into(where, "value");
|
|
1482
|
-
const
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
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);
|
|
1486
2673
|
}
|
|
1487
|
-
|
|
2674
|
+
const f = u.value;
|
|
1488
2675
|
switch (head) {
|
|
1489
2676
|
case "lit": return lit(this.#value(u.value, at));
|
|
1490
2677
|
case "ref": return this.#ref(u.value, at);
|
|
1491
|
-
case "let":
|
|
1492
|
-
const f = fields(u.value, at, ["bind", "body"]);
|
|
1493
|
-
return let_(this.#names(f["bind"], into(at, "bind")), this.term(f["body"], into(at, "body")));
|
|
1494
|
-
}
|
|
1495
|
-
case "fn": {
|
|
1496
|
-
const f = fields(u.value, at, ["params", "body"]);
|
|
1497
|
-
const params = this.#list(f["params"], into(at, "params"));
|
|
1498
|
-
if (!params.every((p) => typeof p === "string")) return bad(into(at, "params"), "a parameter is not a text");
|
|
1499
|
-
return fn(params, this.term(f["body"], into(at, "body")));
|
|
1500
|
-
}
|
|
2678
|
+
case "let": return let_(this.#binds(f["bind"], into(at, "bind")), this.term(f["body"], into(at, "body")));
|
|
1501
2679
|
case "call": {
|
|
1502
|
-
const f = fields(u.value, at, ["fn", "args"]);
|
|
1503
2680
|
const name = this.#function(f["fn"], into(at, "fn"));
|
|
1504
|
-
return call(name, ...
|
|
2681
|
+
return call(name, ...f["args"].map((x, i) => this.term(x, into(into(at, "args"), i))));
|
|
1505
2682
|
}
|
|
1506
2683
|
case "record": return rec(this.#names(u.value, at));
|
|
1507
|
-
case "each":
|
|
1508
|
-
const f = fields(u.value, at, ["axis", "body"], ["at"]);
|
|
1509
|
-
return each(this.#axis(f["axis"], into(at, "axis")), this.term(f["body"], into(at, "body")), this.#address(f["at"], into(at, "at")));
|
|
1510
|
-
}
|
|
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")));
|
|
1511
2685
|
case "fold": {
|
|
1512
|
-
const
|
|
1513
|
-
|
|
1514
|
-
"init",
|
|
1515
|
-
"step",
|
|
1516
|
-
"result"
|
|
1517
|
-
], ["at"]);
|
|
1518
|
-
return fold(this.#axis(f["axis"], into(at, "axis")), this.#names(f["init"], into(at, "init")), this.#names(f["step"], into(at, "step")), this.term(f["result"], into(at, "result")), this.#address(f["at"], into(at, "at")));
|
|
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")));
|
|
1519
2688
|
}
|
|
1520
|
-
|
|
1521
|
-
const
|
|
1522
|
-
"limit",
|
|
1523
|
-
"init",
|
|
1524
|
-
"step",
|
|
1525
|
-
"result"
|
|
1526
|
-
], ["until"]);
|
|
1527
|
-
const init = this.#names(f["init"], into(at, "init"));
|
|
1528
|
-
const step = this.#names(f["step"], into(at, "step"));
|
|
2689
|
+
default: {
|
|
2690
|
+
const [init, step] = this.#loop(f, at);
|
|
1529
2691
|
const until = f["until"] === void 0 ? void 0 : this.term(f["until"], into(at, "until"));
|
|
1530
2692
|
return iterate(f["limit"], init, step, this.term(f["result"], into(at, "result")), until);
|
|
1531
2693
|
}
|
|
1532
|
-
default: return bad(where, `the type "${typeName(u.type)}" is not a term of Vex`);
|
|
1533
2694
|
}
|
|
1534
2695
|
}
|
|
1535
|
-
/** This method gives the keys of the type of an envelope.
|
|
2696
|
+
/** This method gives the keys of the type of an envelope. A term of Vex has no header (DOC.TERM). */
|
|
1536
2697
|
#type(u, where) {
|
|
1537
2698
|
if (u.name !== void 0 || u.own !== void 0) return bad(where, "a term of Vex has no name and no own entries");
|
|
1538
|
-
|
|
1539
|
-
if (keys === void 0) return bad(into(where, "type"), "the type has a computed step");
|
|
1540
|
-
return keys;
|
|
2699
|
+
return typePath(u.type);
|
|
1541
2700
|
}
|
|
1542
2701
|
/** This method gives the name of an op or an extension: its keys, joined with dots (DOC.OPS, DOC.EXT). */
|
|
1543
2702
|
#name(keys, where) {
|
|
@@ -1548,20 +2707,31 @@ var Reader = class {
|
|
|
1548
2707
|
const r = decodeValue(u, this.#domains);
|
|
1549
2708
|
return r.ok ? r.value : bad(where, r.error.message);
|
|
1550
2709
|
}
|
|
1551
|
-
/** This method reads a list. */
|
|
1552
|
-
#list(u, where) {
|
|
1553
|
-
return Array.isArray(u) ? u : bad(where, "the value is not a list");
|
|
1554
|
-
}
|
|
1555
2710
|
/** This method reads a record of names and the term of each name. */
|
|
1556
2711
|
#names(u, where) {
|
|
1557
|
-
if (!isRecord(u)) return bad(where, "the value is not a record");
|
|
1558
2712
|
return Object.fromEntries(Object.entries(u).map(([name, x]) => [name, this.term(x, into(where, name))]));
|
|
1559
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
|
+
}
|
|
1560
2731
|
/** This method reads a reference: the fields of `self` after its moves, or a name of `names` (DOC.REF). */
|
|
1561
|
-
#ref(
|
|
1562
|
-
const [root, ...steps] =
|
|
2732
|
+
#ref(path, where) {
|
|
2733
|
+
const [root, ...steps] = path;
|
|
1563
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");
|
|
1564
|
-
if (root !== "self") return bad(into(where, 0), `Vex reads only from the roots "self" and "names", and this reference starts at ${JSON.stringify(root) ?? "nothing"}`);
|
|
1565
2735
|
const moves = [];
|
|
1566
2736
|
const names = [];
|
|
1567
2737
|
steps.forEach((step, i) => {
|
|
@@ -1575,88 +2745,58 @@ var Reader = class {
|
|
|
1575
2745
|
}
|
|
1576
2746
|
/** This method reads the function of a call: a reference to a name (DOC.CALL). */
|
|
1577
2747
|
#function(u, where) {
|
|
1578
|
-
const
|
|
1579
|
-
return
|
|
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] }");
|
|
1580
2750
|
}
|
|
1581
2751
|
/** This method reads the field `at` of `each` and `fold`: a reference from `self` with moves only (DOC.ADDRESS). */
|
|
1582
2752
|
#address(u, where) {
|
|
1583
2753
|
if (u === void 0) return void 0;
|
|
1584
|
-
const e =
|
|
1585
|
-
return e
|
|
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] }");
|
|
1586
2756
|
}
|
|
1587
|
-
/**
|
|
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
|
+
*/
|
|
1588
2761
|
#move(u, where) {
|
|
1589
|
-
const
|
|
2762
|
+
const kind = String(this.#type(u, where)[1]);
|
|
1590
2763
|
const at = into(where, "value");
|
|
1591
|
-
if (head !== "moves" || typeof kind !== "string" || rest.length > 0) return bad(where, `Vex has no step of the type "${typeName(u.type)}"`);
|
|
1592
2764
|
const value = u.value;
|
|
1593
2765
|
switch (kind) {
|
|
1594
|
-
case "key": return typeof value === "string" ? key(value) :
|
|
1595
|
-
case "index": return typeof value === "number" ? index(value) :
|
|
1596
|
-
case "offset": return
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
return value === null ? plain : bad(at, `the value of the move "${kind}" is not null`);
|
|
1601
|
-
}
|
|
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;
|
|
1602
2772
|
}
|
|
1603
2773
|
}
|
|
1604
|
-
/** This method reads an axis (DOC.AXIS). The check
|
|
2774
|
+
/** This method reads an axis (DOC.AXIS). The check gave it the parameters of its kind. */
|
|
1605
2775
|
#axis(u, where) {
|
|
1606
|
-
|
|
1607
|
-
const [head, name, ...rest] = this.#type(u, where);
|
|
1608
|
-
if (head !== "axes" || typeof name !== "string" || rest.length > 0) return bad(where, `the type "${typeName(u.type)}" is not an axis`);
|
|
2776
|
+
const name = String(this.#type(u, where)[1]);
|
|
1609
2777
|
const at = into(where, "value");
|
|
2778
|
+
const f = u.value;
|
|
1610
2779
|
switch (name) {
|
|
1611
|
-
case "
|
|
2780
|
+
case "list": return {
|
|
1612
2781
|
t: name,
|
|
1613
|
-
|
|
2782
|
+
list: this.term(f["list"], into(at, "list"))
|
|
1614
2783
|
};
|
|
1615
|
-
case "
|
|
1616
|
-
const f = fields(u.value, at, ["from", "to"]);
|
|
1617
|
-
return {
|
|
1618
|
-
t: name,
|
|
1619
|
-
from: f["from"],
|
|
1620
|
-
to: f["to"]
|
|
1621
|
-
};
|
|
1622
|
-
}
|
|
1623
|
-
case "table": return {
|
|
2784
|
+
case "reverse": return {
|
|
1624
2785
|
t: name,
|
|
1625
|
-
|
|
2786
|
+
axis: this.#axis(f["axis"], into(at, "axis"))
|
|
1626
2787
|
};
|
|
1627
|
-
case "
|
|
2788
|
+
case "where": return {
|
|
1628
2789
|
t: name,
|
|
1629
|
-
|
|
2790
|
+
axis: this.#axis(f["axis"], into(at, "axis")),
|
|
2791
|
+
test: this.term(f["test"], into(at, "test"))
|
|
1630
2792
|
};
|
|
1631
|
-
|
|
2793
|
+
default: return {
|
|
1632
2794
|
t: name,
|
|
1633
|
-
|
|
2795
|
+
...f
|
|
1634
2796
|
};
|
|
1635
|
-
case "where": {
|
|
1636
|
-
const f = fields(u.value, at, ["axis", "test"]);
|
|
1637
|
-
return {
|
|
1638
|
-
t: name,
|
|
1639
|
-
axis: this.#axis(f["axis"], into(at, "axis")),
|
|
1640
|
-
test: this.term(f["test"], into(at, "test"))
|
|
1641
|
-
};
|
|
1642
|
-
}
|
|
1643
|
-
default:
|
|
1644
|
-
if (!PLAIN_AXES.has(name)) return bad(where, `Vex has no axis "${name}"`);
|
|
1645
|
-
fields(u.value, at, []);
|
|
1646
|
-
return { t: name };
|
|
1647
2797
|
}
|
|
1648
2798
|
}
|
|
1649
2799
|
};
|
|
1650
|
-
/**
|
|
1651
|
-
* This function reads the value of a construct: a record with each necessary field, and no field outside the necessary
|
|
1652
|
-
* and the optional fields. Thus a wrong name of a field gives an error, and the round trip keeps each field.
|
|
1653
|
-
*/
|
|
1654
|
-
function fields(u, where, required, optional = []) {
|
|
1655
|
-
if (!isRecord(u)) return bad(where, "the value is not a record");
|
|
1656
|
-
for (const name of required) if (!Object.hasOwn(u, name)) bad(where, `the value has no field "${name}"`);
|
|
1657
|
-
for (const name of Object.keys(u)) if (!required.includes(name) && !optional.includes(name)) bad(into(where, name), `the field "${name}" is not a field of this term`);
|
|
1658
|
-
return u;
|
|
1659
|
-
}
|
|
1660
2800
|
//#endregion
|
|
1661
2801
|
//#region src/evaluate.ts
|
|
1662
2802
|
/** The names of the special forms. Their arguments are not evaluated before the call. */
|
|
@@ -1758,6 +2898,7 @@ function compile(source, opts = {}) {
|
|
|
1758
2898
|
}
|
|
1759
2899
|
const CACHE = /* @__PURE__ */ new WeakMap();
|
|
1760
2900
|
function compiled(expr, opts) {
|
|
2901
|
+
if (typeof expr !== "object" || expr === null) return compile(expr, opts);
|
|
1761
2902
|
const hit = CACHE.get(expr);
|
|
1762
2903
|
if (hit !== void 0 && hit.domains === opts.domains && hit.fns === opts.fns && hit.extensions === opts.extensions) return hit.program;
|
|
1763
2904
|
const program = compile(expr, opts);
|
|
@@ -2364,13 +3505,12 @@ var Compiler = class {
|
|
|
2364
3505
|
return r;
|
|
2365
3506
|
}
|
|
2366
3507
|
#listOp(op, list, rest, f, path) {
|
|
2367
|
-
const optsArg = op
|
|
2368
|
-
const depth = list.items.find((it) => !it.result.ok && isDepthError(it.result.error))?.result;
|
|
2369
|
-
if (depth !== void 0 && !depth.ok) return fail(depth.error);
|
|
3508
|
+
const optsArg = WITH_ARGUMENT.has(op) ? rest[1] : rest[0];
|
|
2370
3509
|
const strict = optsArg?.strict === true;
|
|
2371
|
-
const
|
|
2372
|
-
if (
|
|
2373
|
-
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] : []);
|
|
2374
3514
|
const numbers = () => {
|
|
2375
3515
|
const bad = okValues.find((x) => typeof x !== "number");
|
|
2376
3516
|
return bad === void 0 ? ok(okValues) : this.#err("kind-mismatch", `"${op}" needs numbers, but an item is a ${describeType(bad)}`, f, path, { op });
|
|
@@ -2384,13 +3524,26 @@ var Compiler = class {
|
|
|
2384
3524
|
const n = rest[0];
|
|
2385
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 });
|
|
2386
3526
|
};
|
|
2387
|
-
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 });
|
|
2388
3528
|
switch (op) {
|
|
2389
3529
|
case "sortBy": return this.#sortBy(okItems, okValues, f, path);
|
|
2390
|
-
case "take":
|
|
3530
|
+
case "take":
|
|
3531
|
+
case "drop": {
|
|
2391
3532
|
const n = whole();
|
|
2392
|
-
|
|
3533
|
+
if (!n.ok) return n;
|
|
3534
|
+
return ok(vexList(op === "take" ? okItems.slice(0, n.value) : okItems.slice(n.value)));
|
|
3535
|
+
}
|
|
3536
|
+
case "append": {
|
|
3537
|
+
const more = other();
|
|
3538
|
+
return more.ok ? ok(vexList([...okItems, ...more.value])) : more;
|
|
2393
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);
|
|
2394
3547
|
case "nth": {
|
|
2395
3548
|
const i = whole();
|
|
2396
3549
|
if (!i.ok) return i;
|
|
@@ -2398,7 +3551,7 @@ var Compiler = class {
|
|
|
2398
3551
|
}
|
|
2399
3552
|
case "count": return ok(okValues.length);
|
|
2400
3553
|
case "values": return ok(Object.freeze([...okValues]));
|
|
2401
|
-
case "keys": return ok(Object.freeze(
|
|
3554
|
+
case "keys": return ok(Object.freeze(okItems.map((it) => it.key)));
|
|
2402
3555
|
case "errors": return ok(Object.freeze(list.items.flatMap((it) => it.result.ok ? [] : [it.result.error])));
|
|
2403
3556
|
case "first": return okValues.length > 0 ? ok(okValues[0]) : empty();
|
|
2404
3557
|
case "sum": {
|
|
@@ -2430,6 +3583,20 @@ var Compiler = class {
|
|
|
2430
3583
|
}
|
|
2431
3584
|
}
|
|
2432
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
|
+
/**
|
|
2433
3600
|
* This method sorts the items of a list by their values, from the smallest (LIST.SORT). The sort is stable. The values
|
|
2434
3601
|
* are all numbers without NaN, or all texts. Texts compare by their UTF-16 code units.
|
|
2435
3602
|
*/
|
|
@@ -2474,6 +3641,26 @@ var Compiler = class {
|
|
|
2474
3641
|
return ok(acc);
|
|
2475
3642
|
}
|
|
2476
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
|
+
}
|
|
2477
3664
|
const FORBIDDEN_FIELDS = /* @__PURE__ */ new Set([
|
|
2478
3665
|
"__proto__",
|
|
2479
3666
|
"constructor",
|
|
@@ -2577,90 +3764,168 @@ function formatTrace(trace, domains = []) {
|
|
|
2577
3764
|
const sizeOf = (e) => 1 + children(e).reduce((s, c) => s + sizeOf(c), 0);
|
|
2578
3765
|
//#endregion
|
|
2579
3766
|
//#region src/deps.ts
|
|
2580
|
-
/** This function gives the functions of a scope without some names: a parameter or a binding hides a function. */
|
|
2581
|
-
const without = (fns, names) => new Map([...fns].filter(([n]) => !names.includes(n)));
|
|
2582
3767
|
/**
|
|
2583
|
-
*
|
|
2584
|
-
*
|
|
2585
|
-
*
|
|
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).
|
|
2586
3771
|
*/
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
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();
|
|
2593
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(".");
|
|
2594
3786
|
/**
|
|
2595
|
-
* This function gives the static reads of an expression. A host can use them to recompute a program only
|
|
2596
|
-
*
|
|
2597
|
-
*
|
|
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).
|
|
2598
3791
|
*/
|
|
2599
3792
|
function deps(e) {
|
|
2600
|
-
const
|
|
2601
|
-
const
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
}
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
x: x.body,
|
|
2625
|
-
fns: inner
|
|
2626
|
-
});
|
|
2627
|
-
for (const b of Object.values(x.bind).toReversed()) if (b.tag !== "fn") todo.push({
|
|
2628
|
-
...item,
|
|
2629
|
-
x: b
|
|
2630
|
-
});
|
|
2631
|
-
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;
|
|
2632
3817
|
}
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
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;
|
|
2645
3844
|
}
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
3845
|
+
if (kind === "fn") {
|
|
3846
|
+
fn = parent;
|
|
3847
|
+
fnVisits.set(parent.doc, parent);
|
|
3848
|
+
}
|
|
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;
|
|
2662
3870
|
}
|
|
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);
|
|
3876
|
+
}
|
|
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);
|
|
2663
3885
|
}
|
|
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
|
+
}
|
|
3925
|
+
}
|
|
3926
|
+
flush();
|
|
3927
|
+
};
|
|
3928
|
+
emit(groups.get(void 0) ?? [], void 0, []);
|
|
2664
3929
|
return out;
|
|
2665
3930
|
}
|
|
2666
3931
|
//#endregion
|
|
@@ -2807,6 +4072,96 @@ function parse(text, domains = []) {
|
|
|
2807
4072
|
}
|
|
2808
4073
|
}
|
|
2809
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
|
|
2810
4165
|
//#region src/traversal.ts
|
|
2811
4166
|
/** Standard monoids. */
|
|
2812
4167
|
const monoids = {
|
|
@@ -3282,7 +4637,7 @@ var SheetRun = class SheetRun {
|
|
|
3282
4637
|
*/
|
|
3283
4638
|
#compiled(t, column, program) {
|
|
3284
4639
|
const compile = this.#compile;
|
|
3285
|
-
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;
|
|
3286
4641
|
const { domains, fns, extensions } = this.#compileOptions;
|
|
3287
4642
|
const hit = PROGRAMS.get(program);
|
|
3288
4643
|
if (hit !== void 0 && hit.compile === compile && hit.domains === domains && hit.fns === fns && hit.extensions === extensions) return hit.program;
|
|
@@ -3426,6 +4781,11 @@ const runtimeOptions = (options) => ({
|
|
|
3426
4781
|
const kindOf = (u) => u?.kind;
|
|
3427
4782
|
const isArg = (u) => kindOf(u) === "vex.arg";
|
|
3428
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
|
+
}
|
|
3429
4789
|
/** This function turns a builder argument into an expression. A bare string is a field reference at `addr`. */
|
|
3430
4790
|
function toExpr(a, addr) {
|
|
3431
4791
|
if (typeof a === "string") return ref(toPath(a), addr);
|
|
@@ -3823,6 +5183,18 @@ var ListChainImpl = class ListChainImpl {
|
|
|
3823
5183
|
nth(i, opts) {
|
|
3824
5184
|
return this.#op("nth", [lit(i)], opts);
|
|
3825
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
|
+
}
|
|
3826
5198
|
each(body, opts) {
|
|
3827
5199
|
const start = new ChainImpl({
|
|
3828
5200
|
...this.#s,
|
|
@@ -3995,6 +5367,6 @@ function vex(...domains) {
|
|
|
3995
5367
|
};
|
|
3996
5368
|
}
|
|
3997
5369
|
//#endregion
|
|
3998
|
-
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, fromDocument, getOr, index, isAxis, isDepthError, isDocument, 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, toDocument, 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 };
|
|
3999
5371
|
|
|
4000
5372
|
//# sourceMappingURL=index.js.map
|