@mark1russell7/vex 0.9.1 → 0.11.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 +163 -67
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1948 -485
- package/build/index.js.map +1 -1
- package/package.json +5 -2
- package/src/builder.ts +126 -32
- package/src/deps.ts +174 -60
- package/src/document.ts +133 -248
- package/src/errors.ts +7 -3
- package/src/evaluate.ts +106 -13
- package/src/index.ts +4 -1
- package/src/kernel.ts +904 -0
- package/src/list.ts +41 -6
- 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/src/document.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The documents of Vex. A document is the form of an expression in coeval, the
|
|
3
|
-
*
|
|
2
|
+
* The documents of Vex. A document is the form of an expression in coeval, the kernel of the family. Each term is an
|
|
3
|
+
* envelope `{ type, value }`, and a reference is a path from a root. The rules are decision D12 of coeval:
|
|
4
4
|
* <https://github.com/mark1russell7/coeval/blob/main/docs/DECISIONS.md>.
|
|
5
5
|
*
|
|
6
6
|
* | Expression | Document |
|
|
@@ -21,27 +21,23 @@
|
|
|
21
21
|
* | an axis | `{ "type": ["axes", name], "value": { ...parameters } }` |
|
|
22
22
|
*
|
|
23
23
|
* The address `at` of `each` and `fold` is a reference from `self` with moves only. An empty address is no address.
|
|
24
|
+
*
|
|
25
|
+
* The kernel checks a document by the contracts of `VEX_WORLD` (DOC.CHECK). A contract gives the shape of a kind, the
|
|
26
|
+
* data of its fields and the kinds of each position. Then `fromDocument` reads the expression. It checks only the grammar
|
|
27
|
+
* of Vex: the steps of a reference, the address, the function of a call and the names of a loop.
|
|
24
28
|
*/
|
|
25
|
-
import {
|
|
26
|
-
import { decodeValue
|
|
29
|
+
import { check, isEnvelope, typeName, typePath, type Envelope, type Issue, type Key, type Machine } from "@mark1russell7/coeval";
|
|
30
|
+
import { decodeValue } from "./codec.ts";
|
|
27
31
|
import type { AnyDomain } from "./domain.ts";
|
|
28
32
|
import { isStackOverflow, vexError, type VexError } from "./errors.ts";
|
|
29
33
|
import { app, call, each, ext, fn, fold, index, iterate, key, let_, lit, offset, origin, other, parent, rec, ref, shapeError, v, type Addr, type Axis, type Expr, type Move, type Names } from "./ir.ts";
|
|
34
|
+
import { MAX_DOCUMENT_DEPTH, TERM_KINDS, vexChecker } from "./kernel.ts";
|
|
30
35
|
import { fail, ok, type Result } from "./result.ts";
|
|
36
|
+
import { WriteStop, writeDocument, type WriteOptions } from "./write.ts";
|
|
31
37
|
|
|
32
38
|
export type { Envelope, Json } from "@mark1russell7/coeval";
|
|
33
39
|
|
|
34
|
-
/** The
|
|
35
|
-
const PLAIN_AXES: ReadonlySet<string> = new Set(["all", "others", "other", "children", "ancestors", "descendants", "siblings", "successors", "predecessors", "reachable"]);
|
|
36
|
-
|
|
37
|
-
/** The moves without a value. */
|
|
38
|
-
const PLAIN_MOVES: ReadonlyMap<string, Move> = new Map([
|
|
39
|
-
["other", other],
|
|
40
|
-
["origin", origin],
|
|
41
|
-
["parent", parent],
|
|
42
|
-
]);
|
|
43
|
-
|
|
44
|
-
/** The stop of a conversion. The functions of this module throw it, and the top of the conversion gives its error. */
|
|
40
|
+
/** The stop of a reading. The functions of this module throw it, and the top of the reading gives its error. */
|
|
45
41
|
class Stop {
|
|
46
42
|
readonly error: VexError;
|
|
47
43
|
|
|
@@ -51,11 +47,17 @@ class Stop {
|
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
/** This function gives the location of a child: a JSON Pointer, with the escapes of RFC 6901. */
|
|
54
|
-
const into = (where: string, step:
|
|
50
|
+
const into = (where: string, step: Key): string => `${where}/${String(step).replaceAll("~", "~0").replaceAll("/", "~1")}`;
|
|
51
|
+
|
|
52
|
+
/** This function gives the JSON Pointer of a path of keys. */
|
|
53
|
+
const pointer = (keys: readonly Key[]): string => keys.reduce<string>(into, "");
|
|
54
|
+
|
|
55
|
+
/** This function gives the message of a document that is not valid at `where`. */
|
|
56
|
+
const notValid = (where: string, reason: string): string => `the document is not valid at ${where === "" ? "its root" : `"${where}"`}: ${reason}`;
|
|
55
57
|
|
|
56
58
|
/** This function stops the reading of a document that is not valid at `where`. */
|
|
57
59
|
function bad(where: string, reason: string): never {
|
|
58
|
-
throw new Stop(vexError("bad-expression",
|
|
60
|
+
throw new Stop(vexError("bad-expression", notValid(where, reason)));
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
/** This function does a conversion. It gives its value, or the error of the conversion. It does not throw. */
|
|
@@ -63,7 +65,7 @@ function attempt<T>(work: () => T, threw: string, deep: string): Result<T> {
|
|
|
63
65
|
try {
|
|
64
66
|
return ok(work());
|
|
65
67
|
} catch (thrown) {
|
|
66
|
-
if (thrown instanceof Stop) return fail(thrown.error);
|
|
68
|
+
if (thrown instanceof Stop || thrown instanceof WriteStop) return fail(thrown.error);
|
|
67
69
|
if (isStackOverflow(thrown)) return fail(vexError("depth", deep, { thrown }));
|
|
68
70
|
return fail(vexError("threw", threw, { thrown }));
|
|
69
71
|
}
|
|
@@ -74,7 +76,7 @@ function attempt<T>(work: () => T, threw: string, deep: string): Result<T> {
|
|
|
74
76
|
* envelope, because it has the key `tag`. Thus `compile` and `evaluate` can take either one (DOC.ENTRY).
|
|
75
77
|
*/
|
|
76
78
|
export function isDocument(u: unknown): u is Envelope {
|
|
77
|
-
return isEnvelope(u
|
|
79
|
+
return isEnvelope(u);
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
/**
|
|
@@ -86,143 +88,49 @@ export function isDocument(u: unknown): u is Envelope {
|
|
|
86
88
|
export function toDocument(e: Expr, domains: readonly AnyDomain[] = []): Result<Envelope> {
|
|
87
89
|
const shape = shapeError(e);
|
|
88
90
|
if (shape !== undefined) return fail(vexError("bad-expression", shape));
|
|
91
|
+
const options: WriteOptions = {
|
|
92
|
+
domains,
|
|
93
|
+
literals: "json",
|
|
94
|
+
// Stryker disable next-line StringLiteral: the writer tests only the order "reads", and each other order is the order of DOC.CONSTRUCT
|
|
95
|
+
order: "document",
|
|
96
|
+
};
|
|
89
97
|
// A getter of a literal value can throw, so the conversion is inside the attempt.
|
|
90
|
-
return attempt(() =>
|
|
98
|
+
return attempt(() => writeDocument(e, options), "the expression has no document", "a literal value is too deep for the call stack of the engine");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// The machine of the checks: the contracts of Vex, and the names of its evaluators and walkers, without their code.
|
|
102
|
+
let checker: Machine | undefined;
|
|
103
|
+
|
|
104
|
+
/** This function gives the first problem of a document that the kernel finds by the contracts of Vex (DOC.CHECK). */
|
|
105
|
+
function problemOf(doc: unknown): Issue | undefined {
|
|
106
|
+
checker ??= vexChecker();
|
|
107
|
+
// A bare value at the root is a literal. The names of a let, a function, a loop and the caller are free names.
|
|
108
|
+
return check(checker, doc, { declaredType: "lit", names: "any" })[0];
|
|
91
109
|
}
|
|
92
110
|
|
|
93
111
|
/**
|
|
94
|
-
* This function reads an expression from a document (DOC.TERM). It is the inverse of `toDocument`.
|
|
95
|
-
*
|
|
112
|
+
* This function reads an expression from a document (DOC.TERM). It is the inverse of `toDocument`. The kernel checks
|
|
113
|
+
* the document first (DOC.CHECK). A document that is not valid gives `#VALUE!` with its location, and a document nested
|
|
114
|
+
* deeper than the kernel takes gives `#DEPTH!` (DOC.ERROR). The expression of a valid document is valid (EVAL.SHAPE).
|
|
96
115
|
*/
|
|
97
116
|
// Stryker disable next-line ArrayDeclaration: a list with a value that is not a domain decodes no value
|
|
98
117
|
export function fromDocument(doc: unknown, domains: readonly AnyDomain[] = []): Result<Expr> {
|
|
99
|
-
const
|
|
100
|
-
if (!
|
|
101
|
-
const
|
|
102
|
-
|
|
118
|
+
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");
|
|
119
|
+
if (!problem.ok) return problem;
|
|
120
|
+
const issue = problem.value;
|
|
121
|
+
if (issue?.code === "#DEPTH!") return fail(vexError("depth", `the document is nested deeper than ${String(MAX_DOCUMENT_DEPTH)} documents`));
|
|
122
|
+
// Stryker disable next-line ArrayDeclaration: the check of coeval gives each issue the keys of its value
|
|
123
|
+
if (issue !== undefined) return fail(vexError("bad-expression", notValid(pointer(issue.at ?? []), issue.reason)));
|
|
124
|
+
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");
|
|
103
125
|
}
|
|
104
126
|
|
|
105
|
-
/** The
|
|
106
|
-
|
|
107
|
-
readonly #domains: readonly AnyDomain[];
|
|
127
|
+
/** The kinds of the terms, as the first key of their type. */
|
|
128
|
+
const TERMS: ReadonlySet<string> = /* @__PURE__ */ new Set(/* @__PURE__ */ TERM_KINDS.map((k) => k[0] as string));
|
|
108
129
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
/** This method gives the document of a term. A literal is its JSON value, except a value with the shape of an envelope. */
|
|
114
|
-
term(e: Expr): Json {
|
|
115
|
-
if (e.tag !== "lit") return this.envelope(e);
|
|
116
|
-
const value = this.#value(e.value);
|
|
117
|
-
return isEnvelope(value) ? typed("lit", value) : value;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** This method gives the document of a term as an envelope. Here a literal is an envelope too. */
|
|
121
|
-
envelope(e: Expr): Envelope {
|
|
122
|
-
switch (e.tag) {
|
|
123
|
-
case "lit":
|
|
124
|
-
return typed("lit", this.#value(e.value));
|
|
125
|
-
case "ref":
|
|
126
|
-
return path("self", ...this.#moves(e.at), ...e.path);
|
|
127
|
-
case "var":
|
|
128
|
-
return path("names", e.name);
|
|
129
|
-
case "app":
|
|
130
|
-
return typed(["ops", ...e.op.split(".")], e.args.map((a) => this.term(a)));
|
|
131
|
-
case "let":
|
|
132
|
-
return typed("let", { bind: this.#names(e.bind), body: this.term(e.body) });
|
|
133
|
-
case "fn":
|
|
134
|
-
return typed("fn", { params: [...e.params], body: this.term(e.body) });
|
|
135
|
-
case "call":
|
|
136
|
-
return typed("call", { fn: path("names", e.fn), args: e.args.map((a) => this.term(a)) });
|
|
137
|
-
case "rec":
|
|
138
|
-
return typed("record", this.#names(e.fields));
|
|
139
|
-
case "each":
|
|
140
|
-
return typed("each", { axis: this.#axis(e.axis), body: this.term(e.body), ...this.#at(e.at) });
|
|
141
|
-
case "fold":
|
|
142
|
-
return typed("fold", { axis: this.#axis(e.axis), init: this.#names(e.init), step: this.#names(e.step), result: this.term(e.result), ...this.#at(e.at) });
|
|
143
|
-
case "iterate":
|
|
144
|
-
return typed("iterate", {
|
|
145
|
-
limit: e.limit,
|
|
146
|
-
init: this.#names(e.init),
|
|
147
|
-
step: this.#names(e.step),
|
|
148
|
-
...(e.until === undefined ? {} : { until: this.term(e.until) }),
|
|
149
|
-
result: this.term(e.result),
|
|
150
|
-
});
|
|
151
|
-
case "ext":
|
|
152
|
-
return typed(["ext", ...e.kind.split(".")], e.data as Json);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/** This method gives the JSON form of a literal value (IR.JSON). */
|
|
157
|
-
#value(u: unknown): Json {
|
|
158
|
-
const r = encodeValue(u, this.#domains);
|
|
159
|
-
if (!r.ok) throw new Stop(r.error);
|
|
160
|
-
return r.value as Json;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** This method gives the documents of the terms of a record of names. */
|
|
164
|
-
#names(names: Names): JsonRecord {
|
|
165
|
-
return Object.fromEntries(Object.entries(names).map(([name, x]) => [name, this.term(x)]));
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** This method gives the step documents of the moves of an address. */
|
|
169
|
-
#moves(at: Addr | undefined): Json[] {
|
|
170
|
-
return (at ?? []).map((m) => this.#move(m));
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/** This method gives the step document of a move. A computed key or position is an envelope (DOC.MOVE). */
|
|
174
|
-
#move(m: Move): Envelope {
|
|
175
|
-
switch (m.t) {
|
|
176
|
-
case "key":
|
|
177
|
-
return typed(["moves", "key"], typeof m.key === "string" ? m.key : this.envelope(m.key));
|
|
178
|
-
case "index":
|
|
179
|
-
return typed(["moves", "index"], typeof m.i === "number" ? m.i : this.envelope(m.i));
|
|
180
|
-
case "offset":
|
|
181
|
-
return typed(["moves", "offset"], [...m.d]);
|
|
182
|
-
case "other":
|
|
183
|
-
case "origin":
|
|
184
|
-
case "parent":
|
|
185
|
-
return typed(["moves", m.t], null);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** This method gives the field `at` of `each` and `fold`: a reference from `self` with moves only, or no field. */
|
|
190
|
-
#at(at: Addr | undefined): { readonly at?: Envelope } {
|
|
191
|
-
return at === undefined || at.length === 0 ? {} : { at: path("self", ...this.#moves(at)) };
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/** This method gives the document of an axis (DOC.AXIS). */
|
|
195
|
-
#axis(a: Axis): Envelope {
|
|
196
|
-
const name = ["axes", a.t];
|
|
197
|
-
switch (a.t) {
|
|
198
|
-
case "neighbors":
|
|
199
|
-
return typed(name, { n: a.n });
|
|
200
|
-
case "window":
|
|
201
|
-
return typed(name, { from: a.from, to: a.to });
|
|
202
|
-
case "table":
|
|
203
|
-
return typed(name, { name: a.name });
|
|
204
|
-
case "list":
|
|
205
|
-
return typed(name, { list: this.term(a.list) });
|
|
206
|
-
case "reverse":
|
|
207
|
-
return typed(name, { axis: this.#axis(a.axis) });
|
|
208
|
-
case "where":
|
|
209
|
-
return typed(name, { axis: this.#axis(a.axis), test: this.term(a.test) });
|
|
210
|
-
case "all":
|
|
211
|
-
case "others":
|
|
212
|
-
case "other":
|
|
213
|
-
case "children":
|
|
214
|
-
case "ancestors":
|
|
215
|
-
case "descendants":
|
|
216
|
-
case "siblings":
|
|
217
|
-
case "successors":
|
|
218
|
-
case "predecessors":
|
|
219
|
-
case "reachable":
|
|
220
|
-
return typed(name, {});
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/** The reader of documents. Its methods throw a `Stop` at the first part that is not valid. */
|
|
130
|
+
/**
|
|
131
|
+
* The reader of documents. The kernel checked the document, so each kind has the shape of its contract. The reader
|
|
132
|
+
* checks the grammar of Vex, and its methods throw a `Stop` at the first part that is not valid.
|
|
133
|
+
*/
|
|
226
134
|
class Reader {
|
|
227
135
|
readonly #domains: readonly AnyDomain[];
|
|
228
136
|
|
|
@@ -238,70 +146,53 @@ class Reader {
|
|
|
238
146
|
/** This method reads a term in an envelope. */
|
|
239
147
|
#envelope(u: Envelope, where: string): Expr {
|
|
240
148
|
const at = into(where, "value");
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
149
|
+
const keys = this.#type(u, where);
|
|
150
|
+
const head = String(keys[0]);
|
|
151
|
+
// The contracts permit only the kinds of the terms in the position of a term. The root has no position. The check gave
|
|
152
|
+
// the type a kind of the world, so the first key tells if the type is a term.
|
|
153
|
+
if (!TERMS.has(head)) return bad(where, `the type "${typeName(u.type)}" is not a term of Vex`);
|
|
154
|
+
if (head === "ops" || head === "ext") {
|
|
155
|
+
const name = this.#name(keys.slice(1), into(where, "type"));
|
|
156
|
+
return head === "ops" ? app(name, ...(u.value as readonly unknown[]).map((x, i) => this.term(x, into(at, i)))) : ext(name, u.value);
|
|
245
157
|
}
|
|
246
|
-
|
|
158
|
+
const f = u.value as Readonly<Record<string, unknown>>;
|
|
247
159
|
switch (head) {
|
|
248
160
|
case "lit":
|
|
249
161
|
return lit(this.#value(u.value, at));
|
|
250
162
|
case "ref":
|
|
251
|
-
return this.#ref(u.value, at);
|
|
252
|
-
case "let":
|
|
253
|
-
|
|
254
|
-
return let_(this.#names(f["bind"], into(at, "bind")), this.term(f["body"], into(at, "body")));
|
|
255
|
-
}
|
|
256
|
-
case "fn": {
|
|
257
|
-
const f = fields(u.value, at, ["params", "body"]);
|
|
258
|
-
const params = this.#list(f["params"], into(at, "params"));
|
|
259
|
-
if (!params.every((p) => typeof p === "string")) return bad(into(at, "params"), "a parameter is not a text");
|
|
260
|
-
return fn(params, this.term(f["body"], into(at, "body")));
|
|
261
|
-
}
|
|
163
|
+
return this.#ref(u.value as readonly unknown[], at);
|
|
164
|
+
case "let":
|
|
165
|
+
return let_(this.#binds(f["bind"], into(at, "bind")), this.term(f["body"], into(at, "body")));
|
|
262
166
|
case "call": {
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
return call(name, ...this.#list(f["args"], into(at, "args")).map((x, i) => this.term(x, into(into(at, "args"), i))));
|
|
167
|
+
const name = this.#function(f["fn"] as Envelope, into(at, "fn"));
|
|
168
|
+
return call(name, ...(f["args"] as readonly unknown[]).map((x, i) => this.term(x, into(into(at, "args"), i))));
|
|
266
169
|
}
|
|
267
170
|
case "record":
|
|
268
171
|
return rec(this.#names(u.value, at));
|
|
269
|
-
case "each":
|
|
270
|
-
|
|
271
|
-
return each(this.#axis(f["axis"], into(at, "axis")), this.term(f["body"], into(at, "body")), this.#address(f["at"], into(at, "at")));
|
|
272
|
-
}
|
|
172
|
+
case "each":
|
|
173
|
+
return each(this.#axis(f["axis"] as Envelope, into(at, "axis")), this.term(f["body"], into(at, "body")), this.#address(f["at"], into(at, "at")));
|
|
273
174
|
case "fold": {
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
const init = this.#names(f["init"], into(at, "init"));
|
|
277
|
-
const step = this.#names(f["step"], into(at, "step"));
|
|
278
|
-
return fold(axis, init, step, this.term(f["result"], into(at, "result")), this.#address(f["at"], into(at, "at")));
|
|
175
|
+
const [init, step] = this.#loop(f, at);
|
|
176
|
+
return fold(this.#axis(f["axis"] as Envelope, into(at, "axis")), init, step, this.term(f["result"], into(at, "result")), this.#address(f["at"], into(at, "at")));
|
|
279
177
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const init = this.#
|
|
283
|
-
const step = this.#names(f["step"], into(at, "step"));
|
|
178
|
+
default: {
|
|
179
|
+
// iterate: the contract gives a whole number from 0 to MAX_ITERATIONS for the limit (ITERATE.LIMIT).
|
|
180
|
+
const [init, step] = this.#loop(f, at);
|
|
284
181
|
const until = f["until"] === undefined ? undefined : this.term(f["until"], into(at, "until"));
|
|
285
|
-
// The limit is a number of the IR. The check of the expression tells a bad limit (ITERATE.LIMIT).
|
|
286
182
|
return iterate(f["limit"] as number, init, step, this.term(f["result"], into(at, "result")), until);
|
|
287
183
|
}
|
|
288
|
-
default:
|
|
289
|
-
// A construct of a newer version of Vex, or of another language.
|
|
290
|
-
return bad(where, `the type "${typeName(u.type)}" is not a term of Vex`);
|
|
291
184
|
}
|
|
292
185
|
}
|
|
293
186
|
|
|
294
|
-
/** This method gives the keys of the type of an envelope.
|
|
295
|
-
#type(u: Envelope, where: string): readonly [
|
|
187
|
+
/** This method gives the keys of the type of an envelope. A term of Vex has no header (DOC.TERM). */
|
|
188
|
+
#type(u: Envelope, where: string): readonly Key[] {
|
|
296
189
|
if (u.name !== undefined || u.own !== undefined) return bad(where, "a term of Vex has no name and no own entries");
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
// A type is a text, or a list with one key or more, so its path has a first key.
|
|
300
|
-
return keys as readonly [string | number, ...(string | number)[]];
|
|
190
|
+
// The check found the kind of the type, so the type has no computed step.
|
|
191
|
+
return typePath(u.type) as readonly Key[];
|
|
301
192
|
}
|
|
302
193
|
|
|
303
194
|
/** This method gives the name of an op or an extension: its keys, joined with dots (DOC.OPS, DOC.EXT). */
|
|
304
|
-
#name(keys: readonly
|
|
195
|
+
#name(keys: readonly Key[], where: string): string {
|
|
305
196
|
return keys.every((k) => typeof k === "string") ? keys.join(".") : bad(where, "a key of the name is not a text");
|
|
306
197
|
}
|
|
307
198
|
|
|
@@ -311,22 +202,37 @@ class Reader {
|
|
|
311
202
|
return r.ok ? r.value : bad(where, r.error.message);
|
|
312
203
|
}
|
|
313
204
|
|
|
314
|
-
/** This method reads a list. */
|
|
315
|
-
#list(u: unknown, where: string): readonly unknown[] {
|
|
316
|
-
return Array.isArray(u) ? (u as readonly unknown[]) : bad(where, "the value is not a list");
|
|
317
|
-
}
|
|
318
|
-
|
|
319
205
|
/** This method reads a record of names and the term of each name. */
|
|
320
206
|
#names(u: unknown, where: string): Names {
|
|
321
|
-
|
|
322
|
-
|
|
207
|
+
return Object.fromEntries(Object.entries(u as Readonly<Record<string, unknown>>).map(([name, x]) => [name, this.term(x, into(where, name))]));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** This method reads the bindings of a let. A binding can be a function (FN.BIND). */
|
|
211
|
+
#binds(u: unknown, where: string): Names {
|
|
212
|
+
return Object.fromEntries(
|
|
213
|
+
Object.entries(u as Readonly<Record<string, unknown>>).map(([name, x]) => {
|
|
214
|
+
const at = into(where, name);
|
|
215
|
+
if (!isDocument(x) || typeName(x.type) !== "fn") return [name, this.term(x, at)];
|
|
216
|
+
this.#type(x, at);
|
|
217
|
+
const f = x.value as { readonly params: readonly string[]; readonly body: unknown };
|
|
218
|
+
return [name, fn(f.params, this.term(f.body, into(into(at, "value"), "body")))];
|
|
219
|
+
}),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** This method reads the names of a loop. Each name of `step` is a name of `init` (LOOP.NAMES). */
|
|
224
|
+
#loop(f: Readonly<Record<string, unknown>>, at: string): readonly [Names, Names] {
|
|
225
|
+
const init = this.#names(f["init"], into(at, "init"));
|
|
226
|
+
const step = this.#names(f["step"], into(at, "step"));
|
|
227
|
+
const extra = Object.keys(step).find((name) => !Object.hasOwn(init, name));
|
|
228
|
+
return extra === undefined ? [init, step] : bad(into(into(at, "step"), extra), `the name "${extra}" of step is not a name of init`);
|
|
323
229
|
}
|
|
324
230
|
|
|
325
231
|
/** This method reads a reference: the fields of `self` after its moves, or a name of `names` (DOC.REF). */
|
|
326
|
-
#ref(
|
|
327
|
-
const [root, ...steps] =
|
|
232
|
+
#ref(path: readonly unknown[], where: string): Expr {
|
|
233
|
+
const [root, ...steps] = path;
|
|
234
|
+
// The check gives each reference a root that Vex reads: "self" or "names".
|
|
328
235
|
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");
|
|
329
|
-
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"}`);
|
|
330
236
|
const moves: Move[] = [];
|
|
331
237
|
const names: string[] = [];
|
|
332
238
|
steps.forEach((step, i) => {
|
|
@@ -340,77 +246,56 @@ class Reader {
|
|
|
340
246
|
}
|
|
341
247
|
|
|
342
248
|
/** This method reads the function of a call: a reference to a name (DOC.CALL). */
|
|
343
|
-
#function(u:
|
|
344
|
-
const
|
|
345
|
-
return
|
|
249
|
+
#function(u: Envelope, where: string): string {
|
|
250
|
+
const steps = u.value as readonly unknown[];
|
|
251
|
+
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] }');
|
|
346
252
|
}
|
|
347
253
|
|
|
348
254
|
/** This method reads the field `at` of `each` and `fold`: a reference from `self` with moves only (DOC.ADDRESS). */
|
|
349
255
|
#address(u: unknown, where: string): Addr | undefined {
|
|
350
256
|
if (u === undefined) return undefined;
|
|
351
|
-
const e =
|
|
352
|
-
return e
|
|
257
|
+
const e = this.#envelope(u as Envelope, where);
|
|
258
|
+
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] }');
|
|
353
259
|
}
|
|
354
260
|
|
|
355
|
-
/**
|
|
261
|
+
/**
|
|
262
|
+
* This method reads a move (DOC.MOVE). The value of `key` and `index` is a term: a text key and a number position are
|
|
263
|
+
* fixed, and another term is computed. The check gave the other moves their values.
|
|
264
|
+
*/
|
|
356
265
|
#move(u: Envelope, where: string): Move {
|
|
357
|
-
const
|
|
266
|
+
const kind = String(this.#type(u, where)[1]);
|
|
358
267
|
const at = into(where, "value");
|
|
359
|
-
if (head !== "moves" || typeof kind !== "string" || rest.length > 0) return bad(where, `Vex has no step of the type "${typeName(u.type)}"`);
|
|
360
268
|
const value = u.value;
|
|
361
269
|
switch (kind) {
|
|
362
270
|
case "key":
|
|
363
|
-
return typeof value === "string" ? key(value) :
|
|
271
|
+
return typeof value === "string" ? key(value) : key(this.term(value, at));
|
|
364
272
|
case "index":
|
|
365
|
-
return typeof value === "number" ? index(value) :
|
|
273
|
+
return typeof value === "number" ? index(value) : index(this.term(value, at));
|
|
366
274
|
case "offset":
|
|
367
|
-
return
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
return
|
|
372
|
-
|
|
275
|
+
return offset(...(value as readonly number[]));
|
|
276
|
+
case "other":
|
|
277
|
+
return other;
|
|
278
|
+
case "origin":
|
|
279
|
+
return origin;
|
|
280
|
+
default:
|
|
281
|
+
return parent;
|
|
373
282
|
}
|
|
374
283
|
}
|
|
375
284
|
|
|
376
|
-
/** This method reads an axis (DOC.AXIS). The check
|
|
377
|
-
#axis(u:
|
|
378
|
-
|
|
379
|
-
const [head, name, ...rest] = this.#type(u, where);
|
|
380
|
-
if (head !== "axes" || typeof name !== "string" || rest.length > 0) return bad(where, `the type "${typeName(u.type)}" is not an axis`);
|
|
285
|
+
/** This method reads an axis (DOC.AXIS). The check gave it the parameters of its kind. */
|
|
286
|
+
#axis(u: Envelope, where: string): Axis {
|
|
287
|
+
const name = String(this.#type(u, where)[1]);
|
|
381
288
|
const at = into(where, "value");
|
|
289
|
+
const f = u.value as Readonly<Record<string, unknown>>;
|
|
382
290
|
switch (name) {
|
|
383
|
-
case "neighbors":
|
|
384
|
-
return { t: name, n: fields(u.value, at, ["n"])["n"] as 4 | 8 };
|
|
385
|
-
case "window": {
|
|
386
|
-
const f = fields(u.value, at, ["from", "to"]);
|
|
387
|
-
return { t: name, from: f["from"] as number, to: f["to"] as number };
|
|
388
|
-
}
|
|
389
|
-
case "table":
|
|
390
|
-
return { t: name, name: fields(u.value, at, ["name"])["name"] as string };
|
|
391
291
|
case "list":
|
|
392
|
-
return { t: name, list: this.term(
|
|
292
|
+
return { t: name, list: this.term(f["list"], into(at, "list")) };
|
|
393
293
|
case "reverse":
|
|
394
|
-
return { t: name, axis: this.#axis(
|
|
395
|
-
case "where":
|
|
396
|
-
|
|
397
|
-
return { t: name, axis: this.#axis(f["axis"], into(at, "axis")), test: this.term(f["test"], into(at, "test")) };
|
|
398
|
-
}
|
|
294
|
+
return { t: name, axis: this.#axis(f["axis"] as Envelope, into(at, "axis")) };
|
|
295
|
+
case "where":
|
|
296
|
+
return { t: name, axis: this.#axis(f["axis"] as Envelope, into(at, "axis")), test: this.term(f["test"], into(at, "test")) };
|
|
399
297
|
default:
|
|
400
|
-
|
|
401
|
-
fields(u.value, at, []);
|
|
402
|
-
return { t: name } as Axis;
|
|
298
|
+
return { t: name, ...f } as Axis;
|
|
403
299
|
}
|
|
404
300
|
}
|
|
405
301
|
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* This function reads the value of a construct: a record with each necessary field, and no field outside the necessary
|
|
409
|
-
* and the optional fields. Thus a wrong name of a field gives an error, and the round trip keeps each field.
|
|
410
|
-
*/
|
|
411
|
-
function fields(u: unknown, where: string, required: readonly string[], optional: readonly string[] = []): Readonly<Record<string, unknown>> {
|
|
412
|
-
if (!isRecord(u)) return bad(where, "the value is not a record");
|
|
413
|
-
for (const name of required) if (!Object.hasOwn(u, name)) bad(where, `the value has no field "${name}"`);
|
|
414
|
-
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`);
|
|
415
|
-
return u;
|
|
416
|
-
}
|
package/src/errors.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
import type { KernelCode } from "@mark1russell7/coeval";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
* The error codes of Vex. The names come from the spreadsheet error values, because a Vex program is a
|
|
3
|
-
*
|
|
4
|
+
* The error codes of Vex. The names come from the spreadsheet error values, because a Vex program is a spreadsheet
|
|
5
|
+
* formula over records. Each code is a code of the kernel of coeval with the same meaning. For example, `#N/A` is a step
|
|
6
|
+
* that reaches nothing, and `#DEPTH!` is a full call stack. The type checks it: a code that the kernel does not have is
|
|
7
|
+
* not a member.
|
|
4
8
|
*/
|
|
5
|
-
export type ErrorCode = "#REF!" | "#N/A" | "#VALUE!" | "#NAME?" | "#NUM!" | "#CALC!" | "#ARGS" | "#CYCLE!" | "#DEPTH!"
|
|
9
|
+
export type ErrorCode = Extract<KernelCode, "#REF!" | "#N/A" | "#VALUE!" | "#NAME?" | "#NUM!" | "#CALC!" | "#ARGS" | "#CYCLE!" | "#DEPTH!">;
|
|
6
10
|
|
|
7
11
|
/** The cause of an error, in more detail than the code. */
|
|
8
12
|
export type ErrorKind =
|