@mark1russell7/vex-compiler 0.17.2 → 0.18.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 +35 -3
- package/build/facts-3ZtCfd0v.d.ts +37 -0
- package/build/facts-3ZtCfd0v.d.ts.map +1 -0
- package/build/index.d.ts +162 -99
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1036 -4233
- package/build/index.js.map +1 -1
- package/build/memo-08WGBHWI.js +577 -0
- package/build/memo-08WGBHWI.js.map +1 -0
- package/build/module-entry.d.ts +100 -0
- package/build/module-entry.d.ts.map +1 -0
- package/build/module-entry.js +6968 -0
- package/build/module-entry.js.map +1 -0
- package/package.json +17 -6
- package/src/facts.ts +52 -41
- package/src/fn-text.ts +58 -0
- package/src/index.ts +2 -3
- package/src/js.ts +59 -33
- package/src/loop.ts +98 -10
- package/src/lower.ts +487 -125
- package/src/memo.ts +215 -94
- package/src/module-entry.ts +7 -0
- package/src/module-tidy.ts +491 -0
- package/src/module.ts +2731 -314
- package/src/runtime.ts +326 -36
- package/src/scope-key.ts +283 -0
- package/src/sheet-order.ts +13 -99
- package/src/tiers.ts +27 -12
- package/src/unshared.ts +125 -0
package/src/runtime.ts
CHANGED
|
@@ -1,33 +1,54 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The runtime of generated code. Each function follows the rule of the interpreter of the core. Where the interpreter
|
|
3
|
-
* gives an error value, the function gives `FALLBACK`, and
|
|
4
|
-
*
|
|
3
|
+
* gives an error value, the function gives `FALLBACK`, and it records the detail of the failure in two variables. On the
|
|
4
|
+
* value path, the runtime makes no error values and no `Result` objects (docs/COMPILER.md §4.1). A list op on a list
|
|
5
|
+
* value or an array is the exception: it runs the code of the interpreter (COMPILE.LIST.VALUE, COMPILE.LIST.ARRAY).
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* the program.
|
|
7
|
+
* The failure path of generated code keeps a lazy error: the site, the position and the operands. When the program ends
|
|
8
|
+
* with it, `take` gives the error of the interpreter. It uses the error texts of the core (`materialize`).
|
|
9
|
+
*
|
|
10
|
+
* Generated code uses the runtime inside a `try`, and an exception gives `FALLBACK` for the whole program. Tier 1 then
|
|
11
|
+
* gives the result. A function of the runtime catches an exception only where the interpreter makes a value of it. In
|
|
12
|
+
* an op, it is `#CALC!`, and in `safeIs`, it is `false`. A full call stack is `#DEPTH!`, which no list op and no
|
|
13
|
+
* `ifError` hides, so it goes on to the program. An extension throws for `#DEPTH!` too (COMPILE.EXT).
|
|
10
14
|
*/
|
|
11
15
|
import {
|
|
12
16
|
axisTargets,
|
|
17
|
+
errorAt,
|
|
18
|
+
errorText,
|
|
13
19
|
findMethod,
|
|
20
|
+
fixRounds,
|
|
21
|
+
fixValueError,
|
|
22
|
+
isDepthError,
|
|
14
23
|
isListOp,
|
|
15
24
|
isStackOverflow,
|
|
25
|
+
asList,
|
|
16
26
|
isVexList,
|
|
17
27
|
liftsAt,
|
|
28
|
+
listOps,
|
|
18
29
|
matchesKind,
|
|
19
30
|
fixAddr,
|
|
31
|
+
noKeyError,
|
|
20
32
|
resolveAddr,
|
|
21
33
|
safeIs,
|
|
34
|
+
vexError,
|
|
22
35
|
vexList,
|
|
36
|
+
worstRow,
|
|
23
37
|
type Addr,
|
|
24
38
|
type AnyDomain,
|
|
25
39
|
type Axis,
|
|
40
|
+
type ErrorExtra,
|
|
41
|
+
type ErrorText,
|
|
26
42
|
type FixedMove,
|
|
27
43
|
type FreeFn,
|
|
44
|
+
type ParamKind,
|
|
45
|
+
type Position,
|
|
46
|
+
type Result,
|
|
28
47
|
type Space,
|
|
48
|
+
type VexError,
|
|
29
49
|
} from "@mark1russell7/vex";
|
|
30
50
|
import { entryOf, type Entry, type Method, type Options } from "./facts.ts";
|
|
51
|
+
import type { ErrorSite } from "./loop.ts";
|
|
31
52
|
|
|
32
53
|
/** The value of a program that stops at a failed check. */
|
|
33
54
|
export const FALLBACK: unique symbol = Symbol("vex.fallback");
|
|
@@ -57,6 +78,11 @@ export interface Targets {
|
|
|
57
78
|
readonly idx: readonly (number | undefined)[];
|
|
58
79
|
}
|
|
59
80
|
|
|
81
|
+
/** The targets of the axis `list`: the key, the value and the position of each item, in the order of the loop. */
|
|
82
|
+
export interface ListTargets extends Targets {
|
|
83
|
+
readonly values: readonly unknown[];
|
|
84
|
+
}
|
|
85
|
+
|
|
60
86
|
/** An entry of a memo: the value of a node for one space, or no value after an error. */
|
|
61
87
|
export interface MemoEntry {
|
|
62
88
|
readonly v: unknown;
|
|
@@ -84,6 +110,43 @@ const NEGATIVE_ZERO = Symbol("-0");
|
|
|
84
110
|
/** This function tells if a memo keeps an entry with these keys: keys without objects and functions. */
|
|
85
111
|
const kept = (keys: readonly unknown[]): boolean => keys.every((k) => (typeof k !== "object" || k === null) && typeof k !== "function");
|
|
86
112
|
|
|
113
|
+
/**
|
|
114
|
+
* The detail of the failure of an op or of an extension, with the meaning of the two variables of the record:
|
|
115
|
+
* - `threw`: an op threw, and `a` is the thrown value.
|
|
116
|
+
* - `lift`: `fromScalar` threw. `a` is the thrown value, and `b` the name of the domain.
|
|
117
|
+
* - `valid`: a check `valid` threw. `a` is the thrown value, and `b` the name of the domain.
|
|
118
|
+
* - `bad`: a result failed its checks, and `a` is the result.
|
|
119
|
+
* - `param`: an argument has the wrong kind, and `a` is a `ParamFailure`.
|
|
120
|
+
* - `no-op`: no domain and no function has the op. `a` tells if the op has a receiver, and `b` is the receiver.
|
|
121
|
+
* - `no-step`: no domain gives a step of `reduce`, and `a` is the accumulator.
|
|
122
|
+
* - `no-handler`: an extension has no handler.
|
|
123
|
+
* - `foreign`: a handler gave an error, and `a` is the error.
|
|
124
|
+
* - `list`: a list op or `fill` gave an error of its own, and `a` is the error.
|
|
125
|
+
*/
|
|
126
|
+
type Detail = "threw" | "lift" | "valid" | "bad" | "param" | "no-op" | "no-step" | "no-handler" | "foreign" | "list";
|
|
127
|
+
|
|
128
|
+
/** An argument of an op that does not have the kind of its parameter. */
|
|
129
|
+
interface ParamFailure {
|
|
130
|
+
readonly index: number;
|
|
131
|
+
readonly value: unknown;
|
|
132
|
+
readonly kind: ParamKind;
|
|
133
|
+
readonly domain: string;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A failure that the program keeps: its site, its position, its operands and the detail of the runtime. `materialize`
|
|
138
|
+
* makes the error of it only if the error is the result of the program.
|
|
139
|
+
*/
|
|
140
|
+
export interface LazyError {
|
|
141
|
+
readonly site: ErrorSite;
|
|
142
|
+
readonly origin: string;
|
|
143
|
+
readonly focus: string;
|
|
144
|
+
readonly operands: readonly unknown[];
|
|
145
|
+
readonly detail: Detail;
|
|
146
|
+
readonly a: unknown;
|
|
147
|
+
readonly b: unknown;
|
|
148
|
+
}
|
|
149
|
+
|
|
87
150
|
/** The functions of the runtime. The names are the names of `RuntimeFn` in the loop IR. */
|
|
88
151
|
export interface Runtime {
|
|
89
152
|
readonly F: Fallback;
|
|
@@ -97,6 +160,10 @@ export interface Runtime {
|
|
|
97
160
|
axis(axis: BaseAxis): (s: Space, origin: string, focus: string) => Targets | Fallback;
|
|
98
161
|
table(tables: Readonly<Record<string, Space>> | undefined, name: string): unknown;
|
|
99
162
|
keys(s: Space): readonly string[];
|
|
163
|
+
/** This function gives the index of a key in the keys of a space, or `undefined` for a key outside the space. */
|
|
164
|
+
rowIndex(s: Space, key: string): number | undefined;
|
|
165
|
+
/** This function gives the largest number of rounds of a column of a space (FIX.LIMIT). */
|
|
166
|
+
rounds(s: Space): number;
|
|
100
167
|
/** This function gives the dispatch of an op name with `arity` arguments. Generated code gets it once, when it loads. */
|
|
101
168
|
site(name: string, arity: number): Site;
|
|
102
169
|
/** This function gives a new memo. Generated code gets one for each node of the memo, when it loads. */
|
|
@@ -105,34 +172,81 @@ export interface Runtime {
|
|
|
105
172
|
domain(index: number): AnyDomain | undefined;
|
|
106
173
|
/** An exception of an op is the error #CALC! of that op, so this function gives `FALLBACK`. A full call stack goes on. */
|
|
107
174
|
threw(thrown: unknown): Fallback;
|
|
175
|
+
/** This function records a result that failed its checks. It gives `FALLBACK`. */
|
|
176
|
+
bad(value: unknown): Fallback;
|
|
177
|
+
/** This function records an exception of the check `valid` of the domain at an index. It gives `FALLBACK`. */
|
|
178
|
+
invalid(thrown: unknown, domain: number): Fallback;
|
|
179
|
+
/** This function gives the lazy error of a failed check, with the record of the last failure. */
|
|
180
|
+
err(site: ErrorSite, origin: string, focus: string, ...operands: readonly unknown[]): LazyError;
|
|
181
|
+
/** This function gives the lazy error of the arguments that failed, as `#all` of the interpreter: `undefined` for no error. */
|
|
182
|
+
args(site: ErrorSite, origin: string, focus: string, errors: readonly (LazyError | undefined)[]): LazyError | undefined;
|
|
183
|
+
/** This function ends a program with a lazy error. It gives `FALLBACK`, so `load` keeps its contract. */
|
|
184
|
+
raise(error: LazyError): Fallback;
|
|
185
|
+
/** This function gives the error of the last `raise`, once. It gives `undefined` when the run ended in another way. */
|
|
186
|
+
take(): VexError | undefined;
|
|
108
187
|
step(name: string, acc: unknown, value: unknown): unknown;
|
|
109
188
|
result(value: unknown): unknown;
|
|
110
189
|
opts(value: unknown): unknown;
|
|
111
190
|
rec(names: readonly string[], values: readonly unknown[]): unknown;
|
|
112
191
|
list(keys: readonly string[], values: readonly unknown[]): unknown;
|
|
192
|
+
/**
|
|
193
|
+
* This function gives the targets of the axis `list` (AXIS.LIST): the keys, the values and the positions of the items.
|
|
194
|
+
* A value that is not a list gives `FALLBACK`, and the site gives the error. Generated code makes no error item, so an
|
|
195
|
+
* error item throws, and tier 1 gives the result.
|
|
196
|
+
*/
|
|
197
|
+
items(value: unknown, reverse: boolean): ListTargets | Fallback;
|
|
113
198
|
frozen(values: readonly unknown[]): unknown;
|
|
114
199
|
ext(kind: string, data: unknown, s: Space, origin: string, focus: string, path: readonly number[]): unknown;
|
|
115
200
|
var(vars: Readonly<Record<string, unknown>> | undefined, name: string): unknown;
|
|
116
201
|
}
|
|
117
202
|
|
|
203
|
+
/** The path and the position of a call of a list op. An error with this path is an error of the op, not of an item. */
|
|
204
|
+
const HERE: readonly number[] = [];
|
|
205
|
+
const NOWHERE: Position = { origin: "", focus: "" };
|
|
206
|
+
|
|
207
|
+
/** The interpreter reads the own enumerable properties of the variables, with `Object.entries`. */
|
|
208
|
+
const hasVar = (vars: Readonly<Record<string, unknown>> | undefined, name: string): vars is Readonly<Record<string, unknown>> => vars !== undefined && Object.prototype.propertyIsEnumerable.call(vars, name);
|
|
209
|
+
|
|
118
210
|
/** This function makes the runtime of one set of options. */
|
|
119
211
|
export function createRuntime(options: RuntimeOptions): Runtime {
|
|
120
212
|
const domains = options.domains ?? [];
|
|
121
213
|
const fns = options.fns;
|
|
122
214
|
const extensions = options.extensions ?? {};
|
|
123
215
|
const F: Fallback = FALLBACK;
|
|
216
|
+
const lists = listOps(options);
|
|
124
217
|
|
|
125
|
-
|
|
126
|
-
|
|
218
|
+
// The record of the last failure. Each failure writes it before `err` reads it, and a failure makes no object here.
|
|
219
|
+
let detail: Detail = "threw";
|
|
220
|
+
let detailA: unknown;
|
|
221
|
+
let detailB: unknown;
|
|
222
|
+
// The error of the last `raise`.
|
|
223
|
+
let raised: LazyError | undefined;
|
|
224
|
+
const record = (d: Detail, a?: unknown, b?: unknown): Fallback => {
|
|
225
|
+
detail = d;
|
|
226
|
+
detailA = a;
|
|
227
|
+
detailB = b;
|
|
127
228
|
return F;
|
|
128
229
|
};
|
|
129
230
|
|
|
231
|
+
// An exception of an op, of `fromScalar` or of a check `valid`.
|
|
232
|
+
const failure = (d: Detail, thrown: unknown, domain?: string): Fallback => {
|
|
233
|
+
if (isStackOverflow(thrown)) throw thrown;
|
|
234
|
+
return record(d, thrown, domain);
|
|
235
|
+
};
|
|
236
|
+
const threw = (thrown: unknown): Fallback => failure("threw", thrown);
|
|
237
|
+
|
|
130
238
|
// The checks of the result of an op: a value, a finite number, and a valid value of its first domain.
|
|
131
239
|
const checked = (value: unknown): unknown => {
|
|
132
|
-
if (value === undefined || value === null) return
|
|
133
|
-
if (typeof value === "number" && !Number.isFinite(value)) return
|
|
240
|
+
if (value === undefined || value === null) return record("bad", value);
|
|
241
|
+
if (typeof value === "number" && !Number.isFinite(value)) return record("bad", value);
|
|
134
242
|
for (const d of domains) {
|
|
135
|
-
if (safeIs(d, value))
|
|
243
|
+
if (!safeIs(d, value)) continue;
|
|
244
|
+
if (d.valid === undefined) return value;
|
|
245
|
+
try {
|
|
246
|
+
return d.valid(value) ? value : record("bad", value);
|
|
247
|
+
} catch (thrown) {
|
|
248
|
+
return failure("valid", thrown, d.name);
|
|
249
|
+
}
|
|
136
250
|
}
|
|
137
251
|
return value;
|
|
138
252
|
};
|
|
@@ -140,16 +254,28 @@ export function createRuntime(options: RuntimeOptions): Runtime {
|
|
|
140
254
|
// The call of an op, as `#call` of the interpreter: the lift of numbers, the kinds of the parameters, the call, and
|
|
141
255
|
// the checks of the result.
|
|
142
256
|
const invoke = (e: Entry, call: (args: readonly unknown[]) => unknown, rest: readonly unknown[]): unknown => {
|
|
257
|
+
const lift = e.spec.liftScalar;
|
|
258
|
+
const from = e.domain.fromScalar;
|
|
259
|
+
let args = rest;
|
|
260
|
+
if (lift && from !== undefined) {
|
|
261
|
+
try {
|
|
262
|
+
args = rest.map((a, i): unknown => (typeof a === "number" && liftsAt(lift, i) ? from(a) : a));
|
|
263
|
+
} catch (thrown) {
|
|
264
|
+
return failure("lift", thrown, e.domain.name);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const params = e.spec.params;
|
|
268
|
+
if (params !== undefined) {
|
|
269
|
+
const index = args.findIndex((a, i) => !matchesKind(params[i] ?? "any", a, e.domain));
|
|
270
|
+
if (index !== -1) return record("param", { index, value: args[index], kind: params[index] ?? "any", domain: e.domain.name } satisfies ParamFailure);
|
|
271
|
+
}
|
|
272
|
+
let value: unknown;
|
|
143
273
|
try {
|
|
144
|
-
|
|
145
|
-
const from = e.domain.fromScalar;
|
|
146
|
-
const args = lift && from !== undefined ? rest.map((a, i): unknown => (typeof a === "number" && liftsAt(lift, i) ? from(a) : a)) : rest;
|
|
147
|
-
const params = e.spec.params;
|
|
148
|
-
if (params !== undefined && !args.every((a, i) => matchesKind(params[i] ?? "any", a, e.domain))) return F;
|
|
149
|
-
return checked(call(args));
|
|
274
|
+
value = call(args);
|
|
150
275
|
} catch (thrown) {
|
|
151
276
|
return threw(thrown);
|
|
152
277
|
}
|
|
278
|
+
return checked(value);
|
|
153
279
|
};
|
|
154
280
|
|
|
155
281
|
// The op of a name for a receiver: the first domain that accepts the receiver and has the op. `undefined` is no op.
|
|
@@ -170,9 +296,129 @@ export function createRuntime(options: RuntimeOptions): Runtime {
|
|
|
170
296
|
|
|
171
297
|
const entriesOf = (name: string): readonly (Entry | undefined)[] => domains.map((d) => entryOf(d, name));
|
|
172
298
|
|
|
299
|
+
// The result of a list op: the code of the interpreter, for each list (COMPILE.LIST.VALUE). An error of the op is an
|
|
300
|
+
// error of the site. #DEPTH! and the error of an item stop the program, so tier 1 gives the result (V-064, V-066).
|
|
301
|
+
const listResult = (r: Result<unknown>): unknown => {
|
|
302
|
+
if (r.ok) return r.value;
|
|
303
|
+
// Stryker disable next-line StringLiteral: the program catches the exception, so no one reads its message
|
|
304
|
+
if (r.error.path !== HERE || isDepthError(r.error)) throw new Error("tier 1 gives the error of a list op");
|
|
305
|
+
return record("list", r.error);
|
|
306
|
+
};
|
|
307
|
+
|
|
173
308
|
// The entries of the op names of reduce. A program can compute such a name, so the runtime keeps them by name.
|
|
174
309
|
const stepEntries = new Map<string, readonly (Entry | undefined)[]>();
|
|
175
310
|
|
|
311
|
+
// This function gives the error of a result that failed its checks, as `#checkResult` of the interpreter.
|
|
312
|
+
const badText = (op: string, value: unknown): ErrorText => {
|
|
313
|
+
if (value === undefined || value === null) return errorText.noValue(op);
|
|
314
|
+
if (typeof value === "number" && !Number.isFinite(value)) return errorText.notFinite(op, value);
|
|
315
|
+
// The checks of a domain gave this failure, so a domain accepts the value.
|
|
316
|
+
return errorText.invalid(op, (domains.find((d) => safeIs(d, value)) as AnyDomain).name);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
// This function gives the error of a failure of an op without a domain, as the end of `#app` of the interpreter.
|
|
320
|
+
const noOpText = (op: string, receiver: boolean, self: unknown): ErrorText => {
|
|
321
|
+
const accepted = receiver ? domains.find((d) => safeIs(d, self)) : undefined;
|
|
322
|
+
if (accepted !== undefined) return errorText.domainNoOp(accepted.name, op);
|
|
323
|
+
return receiver ? errorText.noDomain(op, self) : errorText.noOp(op);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// This function gives the error of a failure of the op `op`, from the record of the failure.
|
|
327
|
+
const opError = (e: LazyError, op: string, path: readonly number[], at: (text: ErrorText, extra?: ErrorExtra) => VexError): VexError => {
|
|
328
|
+
switch (e.detail) {
|
|
329
|
+
case "list":
|
|
330
|
+
return { ...(e.a as VexError), path, origin: e.origin, focus: e.focus };
|
|
331
|
+
case "threw":
|
|
332
|
+
return at(errorText.threw(op), { thrown: e.a });
|
|
333
|
+
case "lift":
|
|
334
|
+
return at(errorText.threw(`${e.b as string}.fromScalar`), { thrown: e.a });
|
|
335
|
+
case "valid":
|
|
336
|
+
return at(errorText.validThrew(op, e.b as string), { thrown: e.a });
|
|
337
|
+
case "bad":
|
|
338
|
+
return at(badText(op, e.a));
|
|
339
|
+
case "param": {
|
|
340
|
+
const p = e.a as ParamFailure;
|
|
341
|
+
return at(errorText.badParam(op, p.index, p.value, p.kind, p.domain));
|
|
342
|
+
}
|
|
343
|
+
case "no-step":
|
|
344
|
+
return at(errorText.noStep(op, e.a));
|
|
345
|
+
// The two details of an extension have no op site, so this case is the only other detail.
|
|
346
|
+
case "no-op":
|
|
347
|
+
case "foreign":
|
|
348
|
+
case "no-handler":
|
|
349
|
+
return at(noOpText(op, e.a as boolean, e.b));
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
// This function gives the error of an extension, as `#guard` and `#locate` of the interpreter.
|
|
354
|
+
const extError = (e: LazyError, kind: string, pos: Position, path: readonly number[], at: (text: ErrorText, extra?: ErrorExtra) => VexError): VexError => {
|
|
355
|
+
if (e.detail === "threw") return at(errorText.threw(kind), { thrown: e.a });
|
|
356
|
+
if (e.detail === "no-handler") return at(errorText.noHandler(kind));
|
|
357
|
+
const error = e.a as VexError;
|
|
358
|
+
return error.origin === undefined ? { ...error, path, origin: pos.origin, focus: pos.focus } : error;
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
/** This function makes the error of a lazy error: the error of the interpreter, with the texts of the core. */
|
|
362
|
+
const materialize = (e: LazyError): VexError => {
|
|
363
|
+
const { site, operands } = e;
|
|
364
|
+
if (site.t === "key") return noKeyError(e.origin);
|
|
365
|
+
const pos = { origin: e.origin, focus: e.focus };
|
|
366
|
+
const { path } = site;
|
|
367
|
+
const at = (text: ErrorText, extra?: ErrorExtra): VexError => errorAt(text, pos, path, extra);
|
|
368
|
+
const [first, second] = operands;
|
|
369
|
+
switch (site.t) {
|
|
370
|
+
case "text":
|
|
371
|
+
return at(site.text);
|
|
372
|
+
case "call": {
|
|
373
|
+
const vars = first as Readonly<Record<string, unknown>> | undefined;
|
|
374
|
+
return at(hasVar(vars, site.name) ? errorText.notFunction(site.name) : errorText.noFunction(site.name));
|
|
375
|
+
}
|
|
376
|
+
case "missing":
|
|
377
|
+
return at(errorText.missing(first as string, site.where));
|
|
378
|
+
case "boolean":
|
|
379
|
+
return at(errorText.notBoolean(site.op, site.i, first));
|
|
380
|
+
case "items":
|
|
381
|
+
return at(errorText.needsItems(site.op, site.what, first));
|
|
382
|
+
case "until":
|
|
383
|
+
return at(errorText.untilKind(first));
|
|
384
|
+
case "limit":
|
|
385
|
+
return at(errorText.limit(first as number));
|
|
386
|
+
case "test":
|
|
387
|
+
return at(errorText.whereKind(first));
|
|
388
|
+
case "listAxis":
|
|
389
|
+
return at(errorText.axisList(first));
|
|
390
|
+
case "op":
|
|
391
|
+
return opError(e, site.op, path, at);
|
|
392
|
+
case "step":
|
|
393
|
+
return opError(e, first as string, path, at);
|
|
394
|
+
case "ext":
|
|
395
|
+
return extError(e, site.kind, pos, path, at);
|
|
396
|
+
case "addr": {
|
|
397
|
+
const values = second as readonly unknown[] | undefined;
|
|
398
|
+
const fixed = values === undefined ? undefined : fixAddr(site.at, values, pos);
|
|
399
|
+
if (fixed?.ok === false) return { ...fixed.error, path };
|
|
400
|
+
const r = resolveAddr(first as Space, pos, fixed === undefined ? (site.at as readonly FixedMove[]) : fixed.value);
|
|
401
|
+
return { ...(r as Extract<typeof r, { readonly ok: false }>).error, path };
|
|
402
|
+
}
|
|
403
|
+
case "axis": {
|
|
404
|
+
const r = axisTargets(first as Space, { origin: e.origin, focus: second as string }, site.axis);
|
|
405
|
+
return { ...(r as Extract<typeof r, { readonly ok: false }>).error, path };
|
|
406
|
+
}
|
|
407
|
+
case "args":
|
|
408
|
+
return at(errorText.args(operands.length, site.what), { causes: (operands as readonly LazyError[]).map(materialize) });
|
|
409
|
+
case "fixLimit":
|
|
410
|
+
return at(errorText.fixLimit(site.name, first as number));
|
|
411
|
+
case "row":
|
|
412
|
+
return at(fixValueError(site.name, site.join, first) as ErrorText);
|
|
413
|
+
case "lastKey":
|
|
414
|
+
return at(errorText.lastKey(site.name, e.focus));
|
|
415
|
+
case "rows": {
|
|
416
|
+
const causes = (operands as readonly LazyError[]).map(materialize);
|
|
417
|
+
return worstRow(causes) ?? at(errorText.args(causes.length, "rows"), { causes });
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
176
422
|
// The index of each key of a space, for the targets of an axis. A miss of the cache only makes the map again.
|
|
177
423
|
const indexes = new WeakMap<Space, ReadonlyMap<string, number>>();
|
|
178
424
|
const indexOf = (s: Space): ReadonlyMap<string, number> => {
|
|
@@ -221,6 +467,8 @@ export function createRuntime(options: RuntimeOptions): Runtime {
|
|
|
221
467
|
return t ?? F;
|
|
222
468
|
},
|
|
223
469
|
keys: (s) => s.keys,
|
|
470
|
+
rowIndex: (s, key) => indexOf(s).get(key),
|
|
471
|
+
rounds: (s) => fixRounds(s.keys.length),
|
|
224
472
|
// The dispatch of `app` in the interpreter: the first domain that accepts the receiver and has the op, and else a
|
|
225
473
|
// free function. Generated code has the fast path of each site, and it uses this dispatch for each other case.
|
|
226
474
|
site(name, arity) {
|
|
@@ -228,9 +476,7 @@ export function createRuntime(options: RuntimeOptions): Runtime {
|
|
|
228
476
|
const listOp = isListOp(name);
|
|
229
477
|
const fn = fns !== undefined && Object.hasOwn(fns, name) ? fns[name] : undefined;
|
|
230
478
|
const generic = (values: readonly unknown[]): unknown => {
|
|
231
|
-
|
|
232
|
-
// Stryker disable next-line StringLiteral: the program catches the exception, so no one reads its message
|
|
233
|
-
if (listOp && isVexList(values[0])) throw new Error("tier 1 gives a list op on a list value");
|
|
479
|
+
if (listOp && isVexList(values[0])) return listResult(lists.list(name, values[0], values.slice(1), NOWHERE, HERE));
|
|
234
480
|
if (values.length > 0) {
|
|
235
481
|
const r = dispatch(entries, name, values, (m) => {
|
|
236
482
|
site.m = m;
|
|
@@ -238,12 +484,18 @@ export function createRuntime(options: RuntimeOptions): Runtime {
|
|
|
238
484
|
if (r !== undefined) return r;
|
|
239
485
|
}
|
|
240
486
|
// LIST.ARRAY: after the domains and the function of the host, a list op on an array is a list op on a list.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
487
|
+
if (listOp && fn === undefined && Array.isArray(values[0])) {
|
|
488
|
+
const r = lists.array(name, values[0], values.slice(1), NOWHERE, HERE);
|
|
489
|
+
// The value of `errors` has the position of the call, and a site has no position (COMPILE.LIST.ARRAY). FALLBACK
|
|
490
|
+
// is an error, and a list op skips an error item, so an exception must stop the program (V-064).
|
|
491
|
+
// Stryker disable next-line StringLiteral: the program catches the exception, so no one reads its message
|
|
492
|
+
if (name === "errors" && r.ok && (r.value as readonly unknown[]).length > 0) throw new Error("tier 1 gives the errors of an array");
|
|
493
|
+
return listResult(r);
|
|
494
|
+
}
|
|
495
|
+
if (name === "fill" && fn === undefined) return listResult(lists.fill(values, NOWHERE, HERE));
|
|
244
496
|
try {
|
|
245
|
-
|
|
246
|
-
return
|
|
497
|
+
if (fn === undefined) return record("no-op", values.length > 0, values[0]);
|
|
498
|
+
return checked(fn(...values));
|
|
247
499
|
} catch (thrown) {
|
|
248
500
|
return threw(thrown);
|
|
249
501
|
}
|
|
@@ -291,17 +543,33 @@ export function createRuntime(options: RuntimeOptions): Runtime {
|
|
|
291
543
|
},
|
|
292
544
|
domain: (index) => domains[index],
|
|
293
545
|
threw,
|
|
546
|
+
bad: (value) => record("bad", value),
|
|
547
|
+
invalid: (thrown, domain) => failure("valid", thrown, (domains[domain] as AnyDomain).name),
|
|
548
|
+
err: (site, origin, focus, ...operands) => ({ site, origin, focus, operands, detail, a: detailA, b: detailB }),
|
|
549
|
+
args(site, origin, focus, errors) {
|
|
550
|
+
const failed = errors.filter((e) => e !== undefined);
|
|
551
|
+
return failed.length < 2 ? failed[0] : { site, origin, focus, operands: failed, detail, a: undefined, b: undefined };
|
|
552
|
+
},
|
|
553
|
+
raise(error) {
|
|
554
|
+
raised = error;
|
|
555
|
+
return F;
|
|
556
|
+
},
|
|
557
|
+
take() {
|
|
558
|
+
const e = raised;
|
|
559
|
+
if (e === undefined) return undefined;
|
|
560
|
+
raised = undefined;
|
|
561
|
+
return materialize(e);
|
|
562
|
+
},
|
|
294
563
|
// One step of `reduce`: the first domain that accepts the accumulator and has the op. No free function.
|
|
295
564
|
step(name, acc, value) {
|
|
296
565
|
let entries = stepEntries.get(name);
|
|
297
|
-
//
|
|
298
|
-
// Stryker disable ConditionalExpression,BlockStatement,CallExpression
|
|
566
|
+
// Stryker disable ConditionalExpression,BlockStatement,CallExpression: a miss of the cache only reads the op tables again
|
|
299
567
|
if (entries === undefined) {
|
|
300
568
|
entries = entriesOf(name);
|
|
301
569
|
stepEntries.set(name, entries);
|
|
302
570
|
}
|
|
303
571
|
// Stryker restore ConditionalExpression,BlockStatement,CallExpression
|
|
304
|
-
return dispatch(entries, name, [acc, value]) ??
|
|
572
|
+
return dispatch(entries, name, [acc, value]) ?? record("no-step", acc);
|
|
305
573
|
},
|
|
306
574
|
result(value) {
|
|
307
575
|
try {
|
|
@@ -315,17 +583,39 @@ export function createRuntime(options: RuntimeOptions): Runtime {
|
|
|
315
583
|
// Object.fromEntries gives an own field for each name, also for "__proto__" (V-062).
|
|
316
584
|
rec: (names, values) => Object.freeze(Object.fromEntries(names.map((n, i) => [n, values[i]]))),
|
|
317
585
|
list: (keys, values) => vexList(keys.map((key, i) => ({ key, result: { ok: true, value: values[i] } }))),
|
|
586
|
+
items(value, reverse) {
|
|
587
|
+
// Stryker disable next-line ArrowFunction,StringLiteral: an error item throws below, so no one reads this error
|
|
588
|
+
const listed = asList(value, (message) => vexError("kind-mismatch", message, NOWHERE));
|
|
589
|
+
if (listed === undefined) return F;
|
|
590
|
+
const keys: string[] = [];
|
|
591
|
+
const values: unknown[] = [];
|
|
592
|
+
const idx: number[] = [];
|
|
593
|
+
for (const { key, result } of listed.items) {
|
|
594
|
+
// Stryker disable next-line StringLiteral: the program catches the exception, so no one reads its message
|
|
595
|
+
if (!result.ok) throw new Error("tier 1 gives the axis list over an error item");
|
|
596
|
+
keys.push(key);
|
|
597
|
+
values.push(result.value);
|
|
598
|
+
idx.push(idx.length);
|
|
599
|
+
}
|
|
600
|
+
return reverse ? { keys: keys.toReversed(), values: values.toReversed(), idx: idx.toReversed() } : { keys, values, idx };
|
|
601
|
+
},
|
|
318
602
|
frozen: (values) => Object.freeze([...values]),
|
|
319
603
|
ext(kind, data, s, origin, focus, path) {
|
|
320
604
|
const handler = Object.hasOwn(extensions, kind) ? extensions[kind] : undefined;
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
605
|
+
if (handler === undefined) return record("no-handler");
|
|
606
|
+
let r: ReturnType<typeof handler>;
|
|
607
|
+
try {
|
|
608
|
+
r = handler(data, { space: s, position: { origin, focus }, path });
|
|
609
|
+
} catch (thrown) {
|
|
610
|
+
// As for an op: an exception is #CALC!, and a full call stack goes on to the program.
|
|
611
|
+
return threw(thrown);
|
|
612
|
+
}
|
|
613
|
+
if (r.ok) return r.value;
|
|
614
|
+
// EVAL.DEPTH: no list op and no ifError hides #DEPTH!, so it stops the program as an exception (COMPILE.EXT).
|
|
615
|
+
// Stryker disable next-line StringLiteral: the program catches the exception, so no one reads its message
|
|
616
|
+
if (isDepthError(r.error)) throw new Error("an extension gave #DEPTH!");
|
|
617
|
+
return record("foreign", r.error);
|
|
329
618
|
},
|
|
619
|
+
var: (vars, name) => (hasVar(vars, name) ? vars[name] : F),
|
|
330
620
|
};
|
|
331
621
|
}
|