@mark1russell7/vex-compiler 0.17.1 → 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 +2735 -317
- 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/lower.ts
CHANGED
|
@@ -4,10 +4,38 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Each value that the interpreter can give as an error has a check. The block of the check tells what a failure does
|
|
6
6
|
* (`Fail`). A list op skips an error item, and an error in the first argument of `ifError` gives the second argument.
|
|
7
|
-
*
|
|
7
|
+
* These contexts drop the error, so the check has no error site. In each other context, the check has the site of its
|
|
8
|
+
* error, and the program ends with that error or a binding keeps it (`keeps`). Where the error has a use, a check without
|
|
9
|
+
* a site stops the program with `FALLBACK`. `#DEPTH!` of an extension stops the program too (COMPILE.EXT).
|
|
10
|
+
*
|
|
11
|
+
* A check for a result that only tier 1 can give is no error, so no context drops it (`#decide`, V-071).
|
|
12
|
+
*
|
|
13
|
+
* After a failure, the interpreter still evaluates the later arguments of a node, and two errors give `#ARGS`. The value
|
|
14
|
+
* path of generated code has no flags for this. A failure jumps to a copy of the later arguments (`#chain`), which only
|
|
15
|
+
* the failure path reads. A copy uses flags, so the size of the code stays near the size of the program times its depth.
|
|
8
16
|
*/
|
|
9
|
-
import {
|
|
10
|
-
|
|
17
|
+
import {
|
|
18
|
+
addrExprs,
|
|
19
|
+
axisExprs,
|
|
20
|
+
children,
|
|
21
|
+
errorText,
|
|
22
|
+
isFieldName,
|
|
23
|
+
joinKind,
|
|
24
|
+
limitAxis,
|
|
25
|
+
listOf,
|
|
26
|
+
MAX_ITERATIONS,
|
|
27
|
+
nestingError,
|
|
28
|
+
shapeError,
|
|
29
|
+
type Addr,
|
|
30
|
+
type Axis,
|
|
31
|
+
type ErrorText,
|
|
32
|
+
type Expr,
|
|
33
|
+
type ExprOf,
|
|
34
|
+
type FixJoin,
|
|
35
|
+
type Names,
|
|
36
|
+
type PrimitiveKind,
|
|
37
|
+
} from "@mark1russell7/vex";
|
|
38
|
+
import { factsOf, isFusedOp, MAX_CALLS, primitiveOf, type Facts, type FusedOp, type Schema } from "./facts.ts";
|
|
11
39
|
import { memoNodes } from "./memo.ts";
|
|
12
40
|
import {
|
|
13
41
|
array,
|
|
@@ -16,27 +44,36 @@ import {
|
|
|
16
44
|
constant,
|
|
17
45
|
FALLBACK_EXPR,
|
|
18
46
|
FIXED_LOCALS,
|
|
47
|
+
keeps,
|
|
19
48
|
local,
|
|
20
49
|
num,
|
|
21
50
|
ORIGIN,
|
|
51
|
+
plainOf,
|
|
22
52
|
prim,
|
|
23
53
|
RETURN,
|
|
24
54
|
SPACE,
|
|
55
|
+
STOP,
|
|
25
56
|
TABLES,
|
|
26
57
|
VARS,
|
|
58
|
+
type ErrorSite,
|
|
27
59
|
type Fail,
|
|
28
60
|
type LExpr,
|
|
29
61
|
type LoopProgram,
|
|
30
62
|
type LStmt,
|
|
31
63
|
} from "./loop.ts";
|
|
64
|
+
import { placesRead, unshared } from "./unshared.ts";
|
|
32
65
|
|
|
33
66
|
/** The error of an expression that the compiler cannot lower. Then the program operates in tier 1. */
|
|
34
67
|
export class Unsupported extends Error {}
|
|
35
68
|
|
|
36
|
-
/**
|
|
69
|
+
/**
|
|
70
|
+
* A bound name: its value, and the flag that tells if its binding gave a value. Without a flag, it always does. After a
|
|
71
|
+
* failure, `error` is the lazy error of the binding, if the context of the binding keeps it.
|
|
72
|
+
*/
|
|
37
73
|
interface Bound {
|
|
38
74
|
readonly value: LExpr;
|
|
39
75
|
readonly ok?: LExpr;
|
|
76
|
+
readonly error?: LExpr;
|
|
40
77
|
}
|
|
41
78
|
|
|
42
79
|
/** A bound function: its node, the path of its node, and the scope of its binding. */
|
|
@@ -56,6 +93,16 @@ interface Scope {
|
|
|
56
93
|
readonly fns: ReadonlyMap<string, BoundFn>;
|
|
57
94
|
/** The record at the focus. In a loop, it reads the record by its index (SPACE.RECORDS). */
|
|
58
95
|
readonly rec: LExpr;
|
|
96
|
+
/** The columns of the steps around the node (COMPILE.FIX): the last round of each name, and its join. */
|
|
97
|
+
readonly columns: ReadonlyMap<string, FixColumn>;
|
|
98
|
+
/** The index of the focus in the keys of the space, if the lowering knows it. A read of a column uses it. */
|
|
99
|
+
readonly slot: LExpr | undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A column in the step of its `fix`: the local of an array with the value of each row, and the join of the column. */
|
|
103
|
+
interface FixColumn {
|
|
104
|
+
readonly local: LExpr;
|
|
105
|
+
readonly join: FixJoin;
|
|
59
106
|
}
|
|
60
107
|
|
|
61
108
|
/** A list of statements, and what a failed check in it does. */
|
|
@@ -66,6 +113,18 @@ interface Block {
|
|
|
66
113
|
|
|
67
114
|
const block = (fail: Fail): Block => ({ stmts: [], fail });
|
|
68
115
|
|
|
116
|
+
/**
|
|
117
|
+
* The number of nodes that the failure paths of a program can copy. Past it, a node with two or more arguments that can
|
|
118
|
+
* fail keeps no error, so tier 1 gives its result. Thus the size of the code has a limit.
|
|
119
|
+
*/
|
|
120
|
+
export const MAX_COPIED = 2000;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The number of arguments of a node that `#chain` takes. Each argument that can fail adds a level of blocks, and the
|
|
124
|
+
* printer and the JavaScript parser recurse over the levels. A node with more arguments keeps no error.
|
|
125
|
+
*/
|
|
126
|
+
export const MAX_CHAINED = 64;
|
|
127
|
+
|
|
69
128
|
/** This function gives the axis inside the `where` axes of an axis. */
|
|
70
129
|
const baseAxis = (a: Axis): Exclude<Axis, { readonly t: "where" }> => (a.t === "where" ? baseAxis(a.axis) : a);
|
|
71
130
|
|
|
@@ -76,6 +135,9 @@ function whereTests(a: Axis): readonly Expr[] {
|
|
|
76
135
|
return out;
|
|
77
136
|
}
|
|
78
137
|
|
|
138
|
+
/** This function gives the number of children of an axis: its list, then the tests of `where`. */
|
|
139
|
+
const axisChildren = (a: Axis): number => whereTests(a).length + (listOf(a) === undefined ? 0 : 1);
|
|
140
|
+
|
|
79
141
|
/** This function gives the `each` under the `let` expressions of a list argument, or `undefined`. */
|
|
80
142
|
function eachUnderLets(e: Expr | undefined): ExprOf<"each"> | undefined {
|
|
81
143
|
let x = e;
|
|
@@ -83,6 +145,12 @@ function eachUnderLets(e: Expr | undefined): ExprOf<"each"> | undefined {
|
|
|
83
145
|
return x?.tag === "each" ? x : undefined;
|
|
84
146
|
}
|
|
85
147
|
|
|
148
|
+
/** The targets of a round of a column: each key of the space, with no address. */
|
|
149
|
+
const ROWS: Pick<ExprOf<"each">, "axis"> = { axis: { t: "all" } };
|
|
150
|
+
|
|
151
|
+
/** The primitive operation of each join (FIX.JOIN). The checks of a row give each join a value of its kind. */
|
|
152
|
+
const JOINS = { or: "||", and: "&&", max: "max", min: "min" } as const;
|
|
153
|
+
|
|
86
154
|
/** The kinds of the values of the primitive operations that always give a boolean. */
|
|
87
155
|
const BOOLEAN_PRIMS: ReadonlySet<string> = new Set(["!", "===", "!==", "isNumber", "isBoolean", "isString", "isValue", "isObject"]);
|
|
88
156
|
|
|
@@ -97,17 +165,20 @@ const join = (a: PrimitiveKind | undefined, b: PrimitiveKind | undefined): Primi
|
|
|
97
165
|
* lower. The facts of the options decide the op of a site whose values have known kinds (KIND.STATIC).
|
|
98
166
|
*/
|
|
99
167
|
export function lower(expr: Expr, facts: Facts = factsOf({}), schema: Schema = {}): LoopProgram {
|
|
168
|
+
// COMPILE.PLACES: the walks below take the time of the places, so the count comes first.
|
|
169
|
+
const places = placesRead([expr]);
|
|
170
|
+
if (places.error !== undefined) throw new Unsupported(places.error);
|
|
100
171
|
// EVAL.SHAPE and EVAL.NESTING: tier 1 gives the error of a program that is not valid or that is nested too deep.
|
|
101
172
|
const shape = shapeError(expr);
|
|
102
173
|
if (shape !== undefined) throw new Unsupported(shape);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const l = new Lowerer(memoNodes(
|
|
174
|
+
const deep = nestingError(expr);
|
|
175
|
+
if (deep !== undefined) throw new Unsupported(deep);
|
|
176
|
+
const plain = unshared(expr, places.shares);
|
|
177
|
+
const l = new Lowerer(memoNodes(plain, isFusedOp), facts, schema);
|
|
107
178
|
const b = block(RETURN);
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const value = l.expr(
|
|
179
|
+
const top: Scope = { space: local(SPACE), origin: local(ORIGIN), focus: local(ORIGIN), env: new Map(), fns: new Map(), rec: prim("get", local(SPACE), local(ORIGIN)), columns: new Map(), slot: undefined };
|
|
180
|
+
l.check(b, call("has", local(SPACE), local(ORIGIN)), "the origin is a key of the space", l.site(b, top, { t: "key" }));
|
|
181
|
+
const value = l.expr(plain, top, [], b);
|
|
111
182
|
b.stmts.push({ t: "return", value });
|
|
112
183
|
return { body: b.stmts, locals: l.locals, constants: l.constants };
|
|
113
184
|
}
|
|
@@ -116,8 +187,14 @@ class Lowerer {
|
|
|
116
187
|
locals = FIXED_LOCALS;
|
|
117
188
|
labels = 0;
|
|
118
189
|
memos = 0;
|
|
119
|
-
/** The number of bodies of functions that the lowering put at calls (MAX_CALLS). */
|
|
190
|
+
/** The number of bodies of functions that the lowering put at calls (MAX_CALLS). A copy does not count. */
|
|
120
191
|
#calls = 0;
|
|
192
|
+
/** The depth of the copies of the failure paths around the lowering. Inside a copy, a node has no memo. */
|
|
193
|
+
#copying = 0;
|
|
194
|
+
/** The number of nodes that the copies have so far (MAX_COPIED). */
|
|
195
|
+
#copied = 0;
|
|
196
|
+
/** The answers of `#mayExt`, for each map of functions. */
|
|
197
|
+
readonly #exts = new WeakMap<ReadonlyMap<string, BoundFn>, Map<Expr, boolean>>();
|
|
121
198
|
readonly #memo: ReadonlyMap<Expr, readonly Expr[]>;
|
|
122
199
|
/** The keys of the nodes of the memo whose code the lowering makes, with their values. */
|
|
123
200
|
#keys: ReadonlyMap<Expr, Bound> = new Map();
|
|
@@ -155,9 +232,18 @@ class Lowerer {
|
|
|
155
232
|
*/
|
|
156
233
|
#declared(b: Block, x: LExpr, kind: PrimitiveKind | undefined, reason: string): LExpr {
|
|
157
234
|
if (kind === undefined) return x;
|
|
158
|
-
b
|
|
235
|
+
this.#decide(b, prim(KIND_CHECKS[kind], x), reason);
|
|
159
236
|
return this.#typed(x, kind);
|
|
160
237
|
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* This method adds a check whose failure is no error of the program. Tier 1 decides the result, so the check stops the
|
|
241
|
+
* program in each block. `check` is for an error, and a context that catches an error drops its failure (V-071).
|
|
242
|
+
*/
|
|
243
|
+
#decide(b: Block, ok: LExpr, reason: string): void {
|
|
244
|
+
b.stmts.push({ t: "check", ok, reason, fail: STOP });
|
|
245
|
+
}
|
|
246
|
+
|
|
161
247
|
readonly constants: unknown[] = [];
|
|
162
248
|
readonly #pool = new Map<unknown, number>();
|
|
163
249
|
|
|
@@ -192,20 +278,34 @@ class Lowerer {
|
|
|
192
278
|
return local(n);
|
|
193
279
|
}
|
|
194
280
|
|
|
195
|
-
|
|
196
|
-
|
|
281
|
+
/**
|
|
282
|
+
* This method gives the lazy error of a site, where the block keeps it. `undefined` tells that the block drops the
|
|
283
|
+
* error, so the site is not a constant of the program.
|
|
284
|
+
*/
|
|
285
|
+
site(b: Block, s: Pick<Scope, "origin" | "focus">, site: ErrorSite, ...operands: LExpr[]): LExpr | undefined {
|
|
286
|
+
return keeps(b.fail) ? call("err", { t: "site", i: this.constants.push(site) - 1 }, s.origin, s.focus, ...operands) : undefined;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** This method gives the lazy error of a text. */
|
|
290
|
+
#text(b: Block, s: Scope, path: readonly number[], text: ErrorText): LExpr | undefined {
|
|
291
|
+
return this.site(b, s, { t: "text", text, path });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** This method adds a check. Without a lazy error, the failure is the plain failure of the block. */
|
|
295
|
+
check(b: Block, ok: LExpr, reason: string, error?: LExpr): void {
|
|
296
|
+
b.stmts.push(error === undefined ? { t: "check", ok, reason, fail: plainOf(b.fail) } : { t: "check", ok, reason, fail: b.fail, error });
|
|
197
297
|
}
|
|
198
298
|
|
|
199
299
|
/** This method gives a new local with the value of a call of the runtime, and a check that the value is not `FALLBACK`. */
|
|
200
|
-
checked(b: Block, value: LExpr, reason: string): LExpr {
|
|
300
|
+
checked(b: Block, value: LExpr, reason: string, error?: LExpr): LExpr {
|
|
201
301
|
const x = this.value(b, value);
|
|
202
|
-
this.check(b, prim("!==", x, FALLBACK_EXPR), reason);
|
|
302
|
+
this.check(b, prim("!==", x, FALLBACK_EXPR), reason, error);
|
|
203
303
|
return x;
|
|
204
304
|
}
|
|
205
305
|
|
|
206
306
|
/** This method adds a failure: the interpreter always gives an error here. */
|
|
207
|
-
fail(b: Block): LExpr {
|
|
208
|
-
b.stmts.push({ t: "fail", fail: b.fail });
|
|
307
|
+
fail(b: Block, error?: LExpr): LExpr {
|
|
308
|
+
b.stmts.push(error === undefined ? { t: "fail", fail: plainOf(b.fail) } : { t: "fail", fail: b.fail, error });
|
|
209
309
|
return FALLBACK_EXPR;
|
|
210
310
|
}
|
|
211
311
|
|
|
@@ -216,12 +316,20 @@ class Lowerer {
|
|
|
216
316
|
if (key.ok !== undefined) this.check(b, key.ok, "a key with a value");
|
|
217
317
|
return key.value;
|
|
218
318
|
}
|
|
219
|
-
const keys = this.#memo.get(e);
|
|
319
|
+
const keys = this.#copying > 0 ? undefined : this.#memo.get(e);
|
|
220
320
|
return keys === undefined ? this.#node(e, s, path, b) : this.#memoized(e, keys, s, path, b);
|
|
221
321
|
}
|
|
222
322
|
|
|
323
|
+
/** This method lowers a copy of a part of a failure path. */
|
|
324
|
+
#copy(make: () => void): void {
|
|
325
|
+
this.#copying++;
|
|
326
|
+
make();
|
|
327
|
+
this.#copying--;
|
|
328
|
+
}
|
|
329
|
+
|
|
223
330
|
/** This method lowers a node itself, without the memo. */
|
|
224
331
|
#node(e: Expr, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
332
|
+
if (this.#copying > 0) this.#copied++;
|
|
225
333
|
switch (e.tag) {
|
|
226
334
|
case "lit":
|
|
227
335
|
return this.constant(e.value);
|
|
@@ -230,12 +338,12 @@ class Lowerer {
|
|
|
230
338
|
case "var": {
|
|
231
339
|
const bound = s.env.get(e.name);
|
|
232
340
|
// A function is not a value (FN.VALUE): tier 1 gives the error. A name of a loop hides a function with its name.
|
|
233
|
-
if (bound === undefined && s.fns.has(e.name)) return this.fail(b);
|
|
341
|
+
if (bound === undefined && s.fns.has(e.name)) return this.fail(b, this.#text(b, s, path, errorText.functionValue(e.name)));
|
|
234
342
|
if (bound === undefined) {
|
|
235
|
-
const x = this.checked(b, call("var", local(VARS), this.constant(e.name)), "a variable");
|
|
343
|
+
const x = this.checked(b, call("var", local(VARS), this.constant(e.name)), "a variable", this.#text(b, s, path, errorText.unbound(e.name)));
|
|
236
344
|
return this.#declared(b, x, primitiveOf(this.#schema.vars?.[e.name]), "a variable of the schema");
|
|
237
345
|
}
|
|
238
|
-
|
|
346
|
+
this.#read(b, bound, "a binding with a value");
|
|
239
347
|
return bound.value;
|
|
240
348
|
}
|
|
241
349
|
case "let":
|
|
@@ -247,7 +355,7 @@ class Lowerer {
|
|
|
247
355
|
case "call":
|
|
248
356
|
return this.#call(e, s, path, b);
|
|
249
357
|
case "rec": {
|
|
250
|
-
const values = Object.values(e.fields)
|
|
358
|
+
const values = this.#all(Object.values(e.fields), s, path, 0, "fields", b);
|
|
251
359
|
return this.value(b, call("rec", this.constant(Object.keys(e.fields)), array(...values)));
|
|
252
360
|
}
|
|
253
361
|
case "app":
|
|
@@ -256,21 +364,27 @@ class Lowerer {
|
|
|
256
364
|
return this.#fold(e, s, path, b);
|
|
257
365
|
case "iterate":
|
|
258
366
|
return this.#iterate(e, s, path, b);
|
|
367
|
+
case "fix":
|
|
368
|
+
return this.#fix(e, s, path, b);
|
|
369
|
+
case "last": {
|
|
370
|
+
const column = s.columns.get(e.name);
|
|
371
|
+
// The lowering comes before the checks. A read outside a step fails a check, and that check gives the reason.
|
|
372
|
+
return column === undefined ? this.fail(b) : this.#rowOf(e.name, column, s, path, b);
|
|
373
|
+
}
|
|
259
374
|
case "each": {
|
|
260
|
-
// A list keeps its error items, and generated code makes no error
|
|
375
|
+
// A list keeps its error items, and generated code makes no error items. Thus an error item stops the program, and
|
|
376
|
+
// tier 1 gives the list.
|
|
261
377
|
const keys = this.locals++;
|
|
262
378
|
const values = this.locals++;
|
|
263
379
|
b.stmts.push({ t: "let", n: keys, value: array() }, { t: "let", n: values, value: array() });
|
|
264
|
-
this.#loop(e, s, path, b, () =>
|
|
380
|
+
this.#loop(e, s, path, b, () => STOP, (body, key, v) => {
|
|
265
381
|
body.stmts.push({ t: "push", n: keys, value: key }, { t: "push", n: values, value: v });
|
|
266
382
|
});
|
|
267
383
|
return this.value(b, call("list", local(keys), local(values)));
|
|
268
384
|
}
|
|
269
385
|
case "ext": {
|
|
270
|
-
//
|
|
271
|
-
|
|
272
|
-
b.stmts.push({ t: "check", ok: prim("!==", x, FALLBACK_EXPR), reason: "an extension", fail: RETURN });
|
|
273
|
-
return x;
|
|
386
|
+
// COMPILE.EXT: an error of an extension is an error of the block. The runtime throws for #DEPTH!, which stops the program.
|
|
387
|
+
return this.checked(b, call("ext", this.constant(e.kind), this.constant(e.data), s.space, s.origin, s.focus, this.constant(path)), "an extension", this.site(b, s, { t: "ext", kind: e.kind, path }));
|
|
274
388
|
}
|
|
275
389
|
}
|
|
276
390
|
}
|
|
@@ -279,40 +393,157 @@ class Lowerer {
|
|
|
279
393
|
* This method gives the value of a node that depends only on the space (§7.2). The memo has an entry for each space.
|
|
280
394
|
* Without the entry of the space, the node evaluates in a block, and an error of it makes an entry without a value.
|
|
281
395
|
* An exception stops the program, so the memo gets no entry for it.
|
|
396
|
+
*
|
|
397
|
+
* The entry has no error, because the error of a node depends on its position and on its child nodes. Where the context
|
|
398
|
+
* keeps the error, a copy of the node evaluates after a read of such an entry. The copy gives the error at this position.
|
|
399
|
+
* A node of the memo and its keys use no extension, so this second evaluation has no effect.
|
|
282
400
|
*/
|
|
283
401
|
#memoized(e: Expr, keys: readonly Expr[], s: Scope, path: readonly number[], b: Block): LExpr {
|
|
284
402
|
const memo: LExpr = { t: "memo", id: this.memos++ };
|
|
285
403
|
// Each key evaluates here, in a block of its own, as a binding does.
|
|
286
|
-
const values = keys.map((k): Bound => this.#attempt(b, (inner) => this.expr(k, s, path, inner)));
|
|
404
|
+
const values = keys.map((k): Bound => this.#attempt(b, (inner) => this.expr(k, s, path, inner), false));
|
|
287
405
|
const tuple = array(...values.flatMap((k) => [k.value, flag(k)]));
|
|
288
406
|
const h = this.locals++;
|
|
289
407
|
const outer = this.#keys;
|
|
290
408
|
this.#keys = new Map([...outer, ...keys.map((k, i): [Expr, Bound] => [k, values[i] as Bound])]);
|
|
291
409
|
const compute = block(b.fail);
|
|
292
|
-
const node = this.#attempt(compute, (inner) => this.#node(e, s, path, inner));
|
|
410
|
+
const node = this.#attempt(compute, (inner) => this.#node(e, s, path, inner), false);
|
|
293
411
|
this.#keys = outer;
|
|
294
412
|
compute.stmts.push({ t: "set", n: h, value: prim("memoPut", memo, s.space, tuple, node.value, flag(node)) });
|
|
295
413
|
b.stmts.push({ t: "let", n: h, value: prim("memoGet", memo, s.space, tuple) }, { t: "if", cond: prim("!", prim("isValue", local(h))), yes: compute.stmts, no: [] });
|
|
296
|
-
|
|
414
|
+
const entry = prim("entryOk", local(h));
|
|
415
|
+
const stop: LStmt = { t: "check", ok: entry, reason: "a value of the memo", fail: plainOf(b.fail) };
|
|
416
|
+
if (keeps(b.fail) && this.#copied < MAX_COPIED) {
|
|
417
|
+
const copy = block(b.fail);
|
|
418
|
+
this.#copy(() => this.#node(e, s, path, copy));
|
|
419
|
+
b.stmts.push({ t: "if", cond: prim("!", entry), yes: [...copy.stmts, stop], no: [] });
|
|
420
|
+
} else {
|
|
421
|
+
b.stmts.push(stop);
|
|
422
|
+
}
|
|
297
423
|
// Each entry of the memo comes from the code of the node, so it has the kind of the value of the node.
|
|
298
424
|
return this.#typed(this.value(b, prim("entryValue", local(h))), this.kindOf(node.value));
|
|
299
425
|
}
|
|
300
426
|
|
|
427
|
+
/**
|
|
428
|
+
* This method lowers the arguments of an op or the fields of a record, with the child indexes from `first`. After an
|
|
429
|
+
* error, the interpreter still evaluates the later arguments, and two errors give `#ARGS` (`#all` of the interpreter).
|
|
430
|
+
* Where the context keeps the error of two or more arguments that can fail, `#chain` or `#collect` lowers them.
|
|
431
|
+
*
|
|
432
|
+
* An extension in a later argument can give `#DEPTH!`. Thus, where the program catches the error, each argument before
|
|
433
|
+
* the last one that can use an extension has its own block (COMPILE.EXT). Then a failure skips no extension.
|
|
434
|
+
*/
|
|
435
|
+
#all(xs: readonly Expr[], s: Scope, path: readonly number[], first: number, what: string, b: Block): LExpr[] {
|
|
436
|
+
if (keeps(b.fail) && xs.filter((x) => x.tag !== "lit").length > 1) {
|
|
437
|
+
if (this.#copying > 0) return this.#collect(xs, s, path, first, what, b);
|
|
438
|
+
if (this.#copied < MAX_COPIED && xs.length <= MAX_CHAINED) return this.#chain(xs, s, path, first, what, b);
|
|
439
|
+
return this.#all(xs, s, path, first, what, { stmts: b.stmts, fail: plainOf(b.fail) });
|
|
440
|
+
}
|
|
441
|
+
const last = b.fail.t === "return" || b.fail.t === "stop" ? -1 : xs.findLastIndex((x) => this.#mayExt(x, s.fns));
|
|
442
|
+
const parts = xs.map((x, i): Bound => {
|
|
443
|
+
const at = [...path, first + i];
|
|
444
|
+
return i < last ? this.#attempt(b, (inner) => this.expr(x, s, at, inner)) : { value: this.expr(x, s, at, b) };
|
|
445
|
+
});
|
|
446
|
+
for (const x of parts) if (x.ok !== undefined) this.check(b, x.ok, "an argument with a value");
|
|
447
|
+
return parts.map((x) => x.value);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* This method lowers arguments on the value path, with no new check. A failure of an argument keeps its error in a
|
|
452
|
+
* local of the argument. It then jumps to the copy of the next argument that can fail. After the last copy, the errors
|
|
453
|
+
* give the error of the node (`args` of the runtime). The copies clear the locals, for the next turn of a loop.
|
|
454
|
+
*
|
|
455
|
+
* Each argument evaluates once: the value path gives up at the failure, and the copies start after it.
|
|
456
|
+
*/
|
|
457
|
+
#chain(xs: readonly Expr[], s: Scope, path: readonly number[], first: number, what: string, b: Block): LExpr[] {
|
|
458
|
+
const end = this.labels++;
|
|
459
|
+
const parts = xs.map((x, i) => {
|
|
460
|
+
const label = this.labels++;
|
|
461
|
+
const error = this.locals++;
|
|
462
|
+
const part = block({ t: "break", label, keep: error });
|
|
463
|
+
const at = [...path, first + i];
|
|
464
|
+
return { x, at, label, error, value: this.expr(x, s, at, part), stmts: part.stmts };
|
|
465
|
+
});
|
|
466
|
+
const values = parts.map((p) => p.value);
|
|
467
|
+
const failing = parts.filter((p) => p.stmts.length > 0);
|
|
468
|
+
if (failing.length === 0) return values;
|
|
469
|
+
let body: LStmt[] = [...parts.flatMap((p) => p.stmts), { t: "break", label: end }];
|
|
470
|
+
failing.forEach((p, i) => {
|
|
471
|
+
body = [{ t: "block", label: p.label, body }];
|
|
472
|
+
const next = failing[i + 1];
|
|
473
|
+
if (next === undefined) return;
|
|
474
|
+
const label = this.labels++;
|
|
475
|
+
const copy = block({ t: "break", label, keep: next.error });
|
|
476
|
+
this.#copy(() => this.expr(next.x, s, next.at, copy));
|
|
477
|
+
body.push({ t: "block", label, body: copy.stmts });
|
|
478
|
+
});
|
|
479
|
+
const inner = block(b.fail);
|
|
480
|
+
const error = this.value(inner, this.#argsError(s, path, what, failing.map((p) => local(p.error))));
|
|
481
|
+
inner.stmts.push(...failing.map((p): LStmt => ({ t: "set", n: p.error, value: { t: "none" } })));
|
|
482
|
+
this.fail(inner, error);
|
|
483
|
+
b.stmts.push({ t: "block", label: end, body: [...body, ...inner.stmts] });
|
|
484
|
+
return values;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* This method lowers arguments in a copy. The value path does not read a copy, so each argument can have its own block
|
|
489
|
+
* and its flag. A node in a copy thus adds no other copy, and the size of the code stays near the size of the node.
|
|
490
|
+
*/
|
|
491
|
+
#collect(xs: readonly Expr[], s: Scope, path: readonly number[], first: number, what: string, b: Block): LExpr[] {
|
|
492
|
+
const parts = xs.map((x, i): Bound => this.#attempt(b, (inner) => this.expr(x, s, [...path, first + i], inner)));
|
|
493
|
+
const errors = parts.flatMap((p) => (p.ok === undefined ? [] : [prim("unless", p.ok, p.value)]));
|
|
494
|
+
if (errors.length > 0) {
|
|
495
|
+
const error = this.value(b, this.#argsError(s, path, what, errors));
|
|
496
|
+
this.check(b, prim("!", prim("isValue", error)), "the arguments have values", error);
|
|
497
|
+
}
|
|
498
|
+
return parts.map((p) => p.value);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** This method gives the call of the runtime that makes the error of some arguments. */
|
|
502
|
+
#argsError(s: Scope, path: readonly number[], what: string, errors: readonly LExpr[]): LExpr {
|
|
503
|
+
const site = this.constants.push({ t: "args", what, path } satisfies ErrorSite) - 1;
|
|
504
|
+
return call("args", { t: "site", i: site }, s.origin, s.focus, array(...errors));
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* This method tells if an expression can call an extension. A call reads the body of its function, and the bodies of
|
|
509
|
+
* the functions of that body. A function cannot call itself, so the search ends. Each node has one answer for each
|
|
510
|
+
* map of functions, because functions can call each other many times (V-068).
|
|
511
|
+
*/
|
|
512
|
+
#mayExt(e: Expr, fns: ReadonlyMap<string, BoundFn>): boolean {
|
|
513
|
+
let answers = this.#exts.get(fns);
|
|
514
|
+
if (answers === undefined) {
|
|
515
|
+
answers = new Map();
|
|
516
|
+
this.#exts.set(fns, answers);
|
|
517
|
+
}
|
|
518
|
+
const known = answers.get(e);
|
|
519
|
+
if (known !== undefined) return known;
|
|
520
|
+
const bound = e.tag === "call" ? fns.get(e.fn) : undefined;
|
|
521
|
+
const answer = e.tag === "ext" || (bound !== undefined && this.#mayExt(bound.fn.body, bound.scope.fns)) || children(e).some((x) => this.#mayExt(x, fns));
|
|
522
|
+
answers.set(e, answer);
|
|
523
|
+
return answer;
|
|
524
|
+
}
|
|
525
|
+
|
|
301
526
|
/**
|
|
302
527
|
* This method evaluates an expression in a block whose errors end the block, as a binding does. It gives the value
|
|
303
|
-
* and the flag that tells if the value exists. Without statements, the expression always has a value, so it gets no
|
|
528
|
+
* and the flag that tells if the value exists. Without statements, the expression always has a value, so it gets no
|
|
529
|
+
* flag. With `keep`, a failure keeps its lazy error in the local of the value: only the failure path writes it there.
|
|
304
530
|
*/
|
|
305
|
-
#attempt(b: Block, make: (inner: Block) => LExpr): Bound {
|
|
531
|
+
#attempt(b: Block, make: (inner: Block) => LExpr, keep = keeps(b.fail)): Bound {
|
|
306
532
|
const label = this.labels++;
|
|
307
|
-
const
|
|
533
|
+
const v = this.locals++;
|
|
534
|
+
const attempt = block(keep ? { t: "break", label, keep: v } : { t: "break", label });
|
|
308
535
|
const value = make(attempt);
|
|
309
536
|
if (attempt.stmts.length === 0) return { value };
|
|
310
|
-
const v = this.locals++;
|
|
311
537
|
const ok = this.locals++;
|
|
312
538
|
this.#typed(local(v), this.kindOf(value));
|
|
313
539
|
attempt.stmts.push({ t: "set", n: v, value }, { t: "set", n: ok, value: bool(true) });
|
|
314
540
|
b.stmts.push({ t: "let", n: ok, value: bool(false) }, { t: "block", label, body: attempt.stmts });
|
|
315
|
-
return { value: local(v), ok: local(ok) };
|
|
541
|
+
return keep ? { value: local(v), ok: local(ok), error: local(v) } : { value: local(v), ok: local(ok) };
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** This method adds the check of a read of a bound name. An error of the binding is the error of the read. */
|
|
545
|
+
#read(b: Block, bound: Bound, reason: string): void {
|
|
546
|
+
if (bound.ok !== undefined) this.check(b, bound.ok, reason, bound.error);
|
|
316
547
|
}
|
|
317
548
|
|
|
318
549
|
/**
|
|
@@ -320,16 +551,19 @@ class Lowerer {
|
|
|
320
551
|
* A segment that cannot name a field always gives an error.
|
|
321
552
|
*/
|
|
322
553
|
#ref(e: ExprOf<"ref">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
323
|
-
const
|
|
324
|
-
|
|
554
|
+
const moved = e.at === undefined || e.at.length === 0 ? undefined : this.#address(e.at, s, path, 0, b);
|
|
555
|
+
const key = moved ?? s.focus;
|
|
556
|
+
const missing = (where: string): LExpr | undefined => this.site(b, s, { t: "missing", where, path }, key);
|
|
325
557
|
const n = this.locals++;
|
|
326
558
|
const x = local(n);
|
|
327
|
-
b.stmts.push({ t: "let", n, value:
|
|
328
|
-
this.check(b, prim("isValue", x), "a record");
|
|
329
|
-
for (const seg of e.path) {
|
|
330
|
-
|
|
559
|
+
b.stmts.push({ t: "let", n, value: moved === undefined ? s.rec : prim("get", s.space, moved) });
|
|
560
|
+
this.check(b, prim("isValue", x), "a record", missing(e.path.length === 0 ? "(record)" : e.path.join(".")));
|
|
561
|
+
for (const [i, seg] of e.path.entries()) {
|
|
562
|
+
const where = e.path.slice(0, i + 1).join(".");
|
|
563
|
+
if (!isFieldName(seg)) return this.fail(b, missing(where));
|
|
564
|
+
this.check(b, prim("isObject", x), "an object", missing(where));
|
|
331
565
|
b.stmts.push({ t: "set", n, value: prim("member", x, this.constant(seg)) });
|
|
332
|
-
this.check(b, prim("isValue", x), "a field");
|
|
566
|
+
this.check(b, prim("isValue", x), "a field", missing(where));
|
|
333
567
|
}
|
|
334
568
|
const [field, ...more] = e.path;
|
|
335
569
|
// Tier 2 knows only the primitive kinds. A value of a domain goes through the dispatch of the runtime.
|
|
@@ -343,29 +577,36 @@ class Lowerer {
|
|
|
343
577
|
*/
|
|
344
578
|
#bind(e: ExprOf<"let">, s: Scope, path: readonly number[], b: Block): Pick<Scope, "env" | "fns"> {
|
|
345
579
|
const env = new Map(s.env);
|
|
346
|
-
|
|
580
|
+
// A let with no function keeps the map of functions, so the answers of `#mayExt` for that map stay (V-089).
|
|
581
|
+
const fns = Object.values(e.bind).some((x) => x.tag === "fn") ? new Map(s.fns) : undefined;
|
|
347
582
|
Object.entries(e.bind).forEach(([name, x], i) => {
|
|
348
583
|
// A function evaluates at each call. A binding sees the scope of the let, so a function cannot call itself.
|
|
349
584
|
// A value hides a function of its name, because a read looks at the values first.
|
|
350
585
|
if (x.tag === "fn") {
|
|
351
|
-
fns
|
|
586
|
+
fns?.set(name, { fn: x, path: [...path, i], scope: s });
|
|
352
587
|
env.delete(name);
|
|
353
588
|
} else {
|
|
354
589
|
env.set(name, this.#attempt(b, (inner) => this.expr(x, s, [...path, i], inner)));
|
|
355
590
|
}
|
|
356
591
|
});
|
|
357
|
-
return { env, fns };
|
|
592
|
+
return { env, fns: fns ?? s.fns };
|
|
358
593
|
}
|
|
359
594
|
|
|
360
595
|
/**
|
|
361
596
|
* This method lowers a call (FN.CALL). The body of the function goes at the place of the call, with the values of the
|
|
362
|
-
* arguments for its parameters. The body sees the names of the place of its binding.
|
|
363
|
-
* to tier 1.
|
|
597
|
+
* arguments for its parameters. The body sees the names of the place of its binding.
|
|
364
598
|
*/
|
|
365
599
|
#call(e: ExprOf<"call">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
366
|
-
const
|
|
367
|
-
if (
|
|
368
|
-
|
|
600
|
+
const value = s.env.get(e.fn);
|
|
601
|
+
if (value !== undefined) {
|
|
602
|
+
this.#read(b, value, "a binding with a value");
|
|
603
|
+
return this.fail(b, this.#text(b, s, path, errorText.notFunction(e.fn)));
|
|
604
|
+
}
|
|
605
|
+
const bound = s.fns.get(e.fn);
|
|
606
|
+
// The name can be a variable of the run: only the run tells the error.
|
|
607
|
+
if (bound === undefined) return this.fail(b, this.site(b, s, { t: "call", name: e.fn, path }, local(VARS)));
|
|
608
|
+
if (bound.fn.params.length !== e.args.length) return this.fail(b, this.#text(b, s, path, errorText.arity(e.fn, bound.fn.params.length, e.args.length)));
|
|
609
|
+
if (this.#copying === 0 && ++this.#calls > MAX_CALLS) throw new Unsupported(`the calls put the bodies of functions in more than ${String(MAX_CALLS)} places`);
|
|
369
610
|
const env = new Map(bound.scope.env);
|
|
370
611
|
bound.fn.params.forEach((p, i) => env.set(p, this.#attempt(b, (inner) => this.expr(e.args[i] as Expr, s, [...path, i], inner))));
|
|
371
612
|
return this.expr(bound.fn.body, { ...s, env, fns: bound.scope.fns }, [...bound.path, 0], b);
|
|
@@ -378,32 +619,32 @@ class Lowerer {
|
|
|
378
619
|
return this.#if(args, s, path, b);
|
|
379
620
|
case "and":
|
|
380
621
|
case "or":
|
|
381
|
-
return args.length === 0 ? this.fail(b) : this.#andOr(op === "or", args, 0, s, path, b);
|
|
622
|
+
return args.length === 0 ? this.fail(b, this.#text(b, s, path, errorText.needsArguments(op))) : this.#andOr(op === "or", args, 0, s, path, b);
|
|
382
623
|
case "ifError":
|
|
383
624
|
return this.#ifError(args, s, path, b);
|
|
384
625
|
}
|
|
385
|
-
if (
|
|
626
|
+
if (isFusedOp(op)) {
|
|
386
627
|
const target = eachUnderLets(args[0]);
|
|
387
628
|
if (target !== undefined) return this.#fused(op, e, target, s, path, b);
|
|
388
629
|
}
|
|
389
|
-
const values =
|
|
630
|
+
const values = this.#all(args, s, path, 0, "arguments", b);
|
|
390
631
|
const kinds = values.map((x) => this.kindOf(x));
|
|
391
632
|
// A static site checks the kind of its result, so the result has the kind that the op declares (KIND.RETURNS).
|
|
392
633
|
const site = this.#facts.staticSite(op, kinds);
|
|
393
|
-
const x = this.checked(b, { t: "op", name: this.index(op), args: values, kinds }, "an op");
|
|
634
|
+
const x = this.checked(b, { t: "op", name: this.index(op), args: values, kinds }, "an op", this.site(b, s, { t: "op", op, path }));
|
|
394
635
|
return this.#typed(x, site?.t === "fn" ? primitiveOf(site.returns) : undefined);
|
|
395
636
|
}
|
|
396
637
|
|
|
397
638
|
/** The condition must be a boolean. Only the branch that the condition selects is evaluated. */
|
|
398
639
|
#if(args: readonly Expr[], s: Scope, path: readonly number[], b: Block): LExpr {
|
|
399
640
|
const [cond, yes, no] = args;
|
|
400
|
-
if (cond === undefined) return this.fail(b);
|
|
401
|
-
const c = this.#boolean(this.expr(cond, s, [...path, 0], b), b);
|
|
641
|
+
if (cond === undefined) return this.fail(b, this.#text(b, s, path, errorText.needsArgument("if", 0)));
|
|
642
|
+
const c = this.#boolean(this.expr(cond, s, [...path, 0], b), b, s, { t: "boolean", op: "if", i: 0, path });
|
|
402
643
|
const r = this.locals++;
|
|
403
644
|
const kinds: (PrimitiveKind | undefined)[] = [];
|
|
404
645
|
const branch = (x: Expr | undefined, i: number): LStmt[] => {
|
|
405
646
|
const inner = block(b.fail);
|
|
406
|
-
const value = x === undefined ? this.fail(inner) : this.expr(x, s, [...path, i], inner);
|
|
647
|
+
const value = x === undefined ? this.fail(inner, this.#text(inner, s, path, errorText.needsArgument("if", i))) : this.expr(x, s, [...path, i], inner);
|
|
407
648
|
kinds.push(this.kindOf(value));
|
|
408
649
|
inner.stmts.push({ t: "set", n: r, value });
|
|
409
650
|
return inner.stmts;
|
|
@@ -414,7 +655,7 @@ class Lowerer {
|
|
|
414
655
|
|
|
415
656
|
/** `or` stops at the first `true`, and `and` at the first `false`. Each argument before the stop must be a boolean. */
|
|
416
657
|
#andOr(isOr: boolean, args: readonly Expr[], i: number, s: Scope, path: readonly number[], b: Block, r = this.locals++): LExpr {
|
|
417
|
-
const x = this.#boolean(this.expr(args[i] as Expr, s, [...path, i], b), b);
|
|
658
|
+
const x = this.#boolean(this.expr(args[i] as Expr, s, [...path, i], b), b, s, { t: "boolean", op: isOr ? "or" : "and", i, path });
|
|
418
659
|
this.#typed(local(r), "boolean");
|
|
419
660
|
if (i === args.length - 1) {
|
|
420
661
|
b.stmts.push({ t: "set", n: r, value: x });
|
|
@@ -433,7 +674,7 @@ class Lowerer {
|
|
|
433
674
|
*/
|
|
434
675
|
#ifError(args: readonly Expr[], s: Scope, path: readonly number[], b: Block): LExpr {
|
|
435
676
|
const [first, second] = args;
|
|
436
|
-
if (first === undefined) return this.fail(b);
|
|
677
|
+
if (first === undefined) return this.fail(b, this.#text(b, s, path, errorText.needsArgument("ifError", 1)));
|
|
437
678
|
const label = this.labels++;
|
|
438
679
|
const r = this.locals++;
|
|
439
680
|
const ok = this.locals++;
|
|
@@ -441,20 +682,20 @@ class Lowerer {
|
|
|
441
682
|
const value = this.expr(first, s, [...path, 0], attempt);
|
|
442
683
|
attempt.stmts.push({ t: "set", n: r, value }, { t: "set", n: ok, value: bool(true) });
|
|
443
684
|
const otherwise = block(b.fail);
|
|
444
|
-
const fallback = second === undefined ? this.fail(otherwise) : this.expr(second, s, [...path, 1], otherwise);
|
|
685
|
+
const fallback = second === undefined ? this.fail(otherwise, this.#text(otherwise, s, path, errorText.needsArgument("ifError", 1))) : this.expr(second, s, [...path, 1], otherwise);
|
|
445
686
|
otherwise.stmts.push({ t: "set", n: r, value: fallback });
|
|
446
687
|
b.stmts.push({ t: "let", n: ok, value: bool(false) }, { t: "block", label, body: attempt.stmts }, { t: "if", cond: prim("!", local(ok)), yes: otherwise.stmts, no: [] });
|
|
447
688
|
return this.#typed(local(r), join(this.kindOf(value), this.kindOf(fallback)));
|
|
448
689
|
}
|
|
449
690
|
|
|
450
691
|
/** A value of a known kind needs no check. */
|
|
451
|
-
#boolean(x: LExpr, b: Block): LExpr {
|
|
452
|
-
if (this.kindOf(x) !== "boolean") this.check(b, prim("isBoolean", x), "a boolean");
|
|
692
|
+
#boolean(x: LExpr, b: Block, s: Pick<Scope, "origin" | "focus">, site: ErrorSite): LExpr {
|
|
693
|
+
if (this.kindOf(x) !== "boolean") this.check(b, prim("isBoolean", x), "a boolean", this.site(b, s, site, x));
|
|
453
694
|
return x;
|
|
454
695
|
}
|
|
455
696
|
|
|
456
|
-
#number(x: LExpr, b: Block): LExpr {
|
|
457
|
-
if (this.kindOf(x) !== "number") this.check(b, prim("isNumber", x), "a number");
|
|
697
|
+
#number(x: LExpr, b: Block, s: Scope, site: ErrorSite): LExpr {
|
|
698
|
+
if (this.kindOf(x) !== "number") this.check(b, prim("isNumber", x), "a number", this.site(b, s, site, x));
|
|
458
699
|
return x;
|
|
459
700
|
}
|
|
460
701
|
|
|
@@ -462,8 +703,13 @@ class Lowerer {
|
|
|
462
703
|
* This method lowers a list op over an `each` into one loop. Like the interpreter, the loop skips an error item, and
|
|
463
704
|
* with the option `strict` the list op fails at an error item. `errors` gives the error values, so an error item of
|
|
464
705
|
* `errors` stops the program.
|
|
706
|
+
*
|
|
707
|
+
* The loop checks the items in the order of the interpreter, so the first bad item is the item of the interpreter. Two
|
|
708
|
+
* cases differ. With `strict`, the first error item of the whole list comes before a check of the op. With an extension
|
|
709
|
+
* in the list, the interpreter evaluates each item before the op. In both cases, a check of the op keeps no error.
|
|
710
|
+
* The later arguments evaluate before the loop, and the interpreter evaluates them after the list, so they keep no error.
|
|
465
711
|
*/
|
|
466
|
-
#fused(op:
|
|
712
|
+
#fused(op: FusedOp, e: ExprOf<"app">, target: ExprOf<"each">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
467
713
|
// The `let` expressions above the `each`, from the outside in.
|
|
468
714
|
let inner = s;
|
|
469
715
|
let at = [...path, 0];
|
|
@@ -471,18 +717,23 @@ class Lowerer {
|
|
|
471
717
|
inner = { ...inner, ...this.#bind(x, inner, at, b) };
|
|
472
718
|
at = [...at, Object.keys(x.bind).length];
|
|
473
719
|
}
|
|
474
|
-
|
|
720
|
+
// COMPILE.EXT: the interpreter evaluates each item of the list before the op, and an extension in an item can give
|
|
721
|
+
// #DEPTH!. Thus a failure that would skip the later items stops the program, if the list can call an extension.
|
|
722
|
+
const skip: Block = this.#mayExt(e.args[0] as Expr, s.fns) ? { stmts: b.stmts, fail: STOP } : b;
|
|
723
|
+
const plain: Block = { stmts: b.stmts, fail: plainOf(skip.fail) };
|
|
724
|
+
const rest = this.#all(e.args.slice(1), s, path, 1, "arguments", plain);
|
|
475
725
|
const options = op === "reduce" ? rest[1] : rest[0];
|
|
476
726
|
const strict = options === undefined ? undefined : this.value(b, prim("===", call("opts", options), bool(true)));
|
|
477
727
|
const name = rest[0];
|
|
478
728
|
if (op === "reduce") {
|
|
479
|
-
if (name === undefined) return this.fail(
|
|
480
|
-
this.check(
|
|
729
|
+
if (name === undefined) return this.fail(plain);
|
|
730
|
+
this.check(plain, prim("isString", name), "an op name");
|
|
481
731
|
}
|
|
482
732
|
const items = (label: number): Fail => {
|
|
483
|
-
if (op === "errors") return
|
|
484
|
-
return strict === undefined ? { t: "continue", label } : { t: "continue", label, strict: { cond: strict, else:
|
|
733
|
+
if (op === "errors") return STOP;
|
|
734
|
+
return strict === undefined ? { t: "continue", label } : { t: "continue", label, strict: { cond: strict, else: skip.fail } };
|
|
485
735
|
};
|
|
736
|
+
const opFail = strict === undefined ? skip.fail : plain.fail;
|
|
486
737
|
|
|
487
738
|
const acc = this.locals++;
|
|
488
739
|
const n = this.typedLocal("number");
|
|
@@ -494,15 +745,17 @@ class Lowerer {
|
|
|
494
745
|
body.stmts.push({ t: "set", n, value: prim("+", local(n), num(1)) });
|
|
495
746
|
};
|
|
496
747
|
// A check of the list op itself is in the loop, but its failure is a failure of the list op.
|
|
497
|
-
const opCheck = (body: Block): Block => ({ stmts: body.stmts, fail:
|
|
748
|
+
const opCheck = (body: Block): Block => ({ stmts: body.stmts, fail: opFail });
|
|
498
749
|
const firstThen = (body: Block, v: LExpr, then: (later: Block) => LExpr): void => {
|
|
499
|
-
const later = block(
|
|
750
|
+
const later = block(opFail);
|
|
500
751
|
later.stmts.push({ t: "set", n: acc, value: then(later) });
|
|
501
752
|
body.stmts.push({ t: "if", cond: prim("===", local(n), num(0)), yes: [{ t: "set", n: acc, value: v }], no: later.stmts });
|
|
502
753
|
};
|
|
503
754
|
const nonEmpty = (): void => {
|
|
504
|
-
this.check(b, prim("!==", local(n), num(0)), "a list that is not empty");
|
|
755
|
+
this.check(b, prim("!==", local(n), num(0)), "a list that is not empty", this.#text(b, s, path, errorText.empty(op)));
|
|
505
756
|
};
|
|
757
|
+
const opError = (): LExpr | undefined => this.site(b, s, { t: "op", op, path });
|
|
758
|
+
const numbers: ErrorSite = { t: "items", op, what: "numbers", path };
|
|
506
759
|
|
|
507
760
|
switch (op) {
|
|
508
761
|
case "count":
|
|
@@ -512,16 +765,16 @@ class Lowerer {
|
|
|
512
765
|
case "mean":
|
|
513
766
|
init(num(0));
|
|
514
767
|
this.#loop(target, inner, at, b, items, (body, _key, v) => {
|
|
515
|
-
body.stmts.push({ t: "set", n: acc, value: prim("+", local(acc), this.#number(v, opCheck(body))) });
|
|
768
|
+
body.stmts.push({ t: "set", n: acc, value: prim("+", local(acc), this.#number(v, opCheck(body), s, numbers)) });
|
|
516
769
|
count(body);
|
|
517
770
|
});
|
|
518
|
-
if (op === "sum") return this.#typed(this.checked(b, call("result", local(acc)), "a finite sum"), "number");
|
|
771
|
+
if (op === "sum") return this.#typed(this.checked(b, call("result", local(acc)), "a finite sum", opError()), "number");
|
|
519
772
|
nonEmpty();
|
|
520
|
-
return this.#typed(this.checked(b, call("result", prim("/", local(acc), local(n))), "a finite mean"), "number");
|
|
773
|
+
return this.#typed(this.checked(b, call("result", prim("/", local(acc), local(n))), "a finite mean", opError()), "number");
|
|
521
774
|
case "min":
|
|
522
775
|
case "max":
|
|
523
776
|
this.#loop(target, inner, at, b, items, (body, _key, v) => {
|
|
524
|
-
this.#number(v, opCheck(body));
|
|
777
|
+
this.#number(v, opCheck(body), s, numbers);
|
|
525
778
|
firstThen(body, v, () => prim(op, local(acc), v));
|
|
526
779
|
count(body);
|
|
527
780
|
});
|
|
@@ -532,7 +785,7 @@ class Lowerer {
|
|
|
532
785
|
case "none":
|
|
533
786
|
init(bool(op === "all"));
|
|
534
787
|
this.#loop(target, inner, at, b, items, (body, _key, v) => {
|
|
535
|
-
body.stmts.push({ t: "set", n: acc, value: prim(op === "all" ? "&&" : "||", local(acc), this.#boolean(v, opCheck(body))) });
|
|
788
|
+
body.stmts.push({ t: "set", n: acc, value: prim(op === "all" ? "&&" : "||", local(acc), this.#boolean(v, opCheck(body), s, { t: "items", op, what: "booleans", path })) });
|
|
536
789
|
});
|
|
537
790
|
return op === "none" ? prim("!", local(acc)) : this.#typed(local(acc), "boolean");
|
|
538
791
|
case "first":
|
|
@@ -554,30 +807,25 @@ class Lowerer {
|
|
|
554
807
|
return this.value(b, call("frozen", array()));
|
|
555
808
|
case "reduce":
|
|
556
809
|
this.#loop(target, inner, at, b, items, (body, _key, v) => {
|
|
557
|
-
firstThen(body, v, (later) => this.checked(later, call("step", name as LExpr, local(acc), v), "a step of reduce"));
|
|
810
|
+
firstThen(body, v, (later) => this.checked(later, call("step", name as LExpr, local(acc), v), "a step of reduce", this.site(later, s, { t: "step", path }, name as LExpr)));
|
|
558
811
|
count(body);
|
|
559
812
|
});
|
|
560
|
-
|
|
813
|
+
// An empty list can give the identity of the op, so tier 1 gives its result. Without one identity, it is an error.
|
|
814
|
+
if (this.#emptyFails(e)) this.check(plain, prim("!==", local(n), num(0)), "a list that is not empty");
|
|
815
|
+
else this.#decide(b, prim("!==", local(n), num(0)), "a list that is not empty");
|
|
561
816
|
return local(acc);
|
|
562
|
-
// listReason stops a program with these ops before the lowering, so this case is defensive.
|
|
563
|
-
case "sortBy":
|
|
564
|
-
case "take":
|
|
565
|
-
case "nth":
|
|
566
|
-
case "drop":
|
|
567
|
-
case "append":
|
|
568
|
-
case "flat":
|
|
569
|
-
case "without":
|
|
570
|
-
case "get":
|
|
571
|
-
case "indexOf":
|
|
572
|
-
case "distinct":
|
|
573
|
-
case "group":
|
|
574
|
-
case "join":
|
|
575
|
-
/* v8 ignore next -- @preserve */
|
|
576
|
-
// Stryker disable next-line all: listReason stops these ops before the lowering
|
|
577
|
-
throw new Unsupported(`the list op "${op}"`);
|
|
578
817
|
}
|
|
579
818
|
}
|
|
580
819
|
|
|
820
|
+
/**
|
|
821
|
+
* This method tells if a `reduce` of an empty list always gives an error (LIST.EMPTY). The name of its op is a literal,
|
|
822
|
+
* and the domains do not have exactly one identity of the op. An option can name a domain, so a third argument tells nothing.
|
|
823
|
+
*/
|
|
824
|
+
#emptyFails(e: ExprOf<"app">): boolean {
|
|
825
|
+
const name = e.args[1] as Expr;
|
|
826
|
+
return e.args.length === 2 && name.tag === "lit" && this.#facts.identities(name.value as string) !== 1;
|
|
827
|
+
}
|
|
828
|
+
|
|
581
829
|
/**
|
|
582
830
|
* This method lowers the loop over the targets of an axis. The tests of `where` and the body evaluate at each target
|
|
583
831
|
* in a block whose failure is `items(label)`. Then `onItem` adds the statements for the value of the body.
|
|
@@ -588,65 +836,84 @@ class Lowerer {
|
|
|
588
836
|
*/
|
|
589
837
|
#address(at: Addr, s: Scope, path: readonly number[], first: number, b: Block): LExpr {
|
|
590
838
|
const values = addrExprs(at).map((x, i) => this.expr(x, s, [...path, first + i], b));
|
|
591
|
-
const
|
|
592
|
-
return this.checked(b, call("addr", ...
|
|
839
|
+
const moves = values.length === 0 ? [] : [array(...values)];
|
|
840
|
+
return this.checked(b, call("addr", s.space, s.origin, s.focus, this.constant(at), ...moves), "an address", this.site(b, s, { t: "addr", at, path }, s.space, ...moves));
|
|
593
841
|
}
|
|
594
842
|
|
|
595
843
|
#loop(e: ExprOf<"each">, s: Scope, path: readonly number[], b: Block, items: (label: number) => Fail, onItem: (body: Block, key: LExpr, value: LExpr) => void): void {
|
|
596
|
-
this.#targetLoop(e
|
|
597
|
-
onItem(body, key, this.expr(e.body, at, [...path, 0], body));
|
|
844
|
+
this.#targetLoop(e, s, path, 1, b, items, (body, key, at, names) => {
|
|
845
|
+
onItem(body, key, this.expr(e.body, { ...at, env: new Map([...at.env, ...names]) }, [...path, 0], body));
|
|
598
846
|
});
|
|
599
847
|
}
|
|
600
848
|
|
|
601
849
|
/**
|
|
602
|
-
* This method lowers the loop over the targets of an axis with
|
|
603
|
-
*
|
|
850
|
+
* This method lowers the loop over the targets of an axis with an address. The list of the axis `list` has the child
|
|
851
|
+
* index `firstTest`. The tests of `where` come next, and the expressions of the address come after them. `each` adds
|
|
852
|
+
* the statements of a target. The map `names` has the names `as` of the item. Only the body or the step reads them.
|
|
604
853
|
*/
|
|
605
854
|
#targetLoop(
|
|
606
|
-
|
|
607
|
-
addr: Addr | undefined,
|
|
855
|
+
e: Pick<ExprOf<"each">, "axis" | "at" | "as">,
|
|
608
856
|
s: Scope,
|
|
609
857
|
path: readonly number[],
|
|
610
858
|
firstTest: number,
|
|
611
859
|
b: Block,
|
|
612
860
|
items: (label: number) => Fail,
|
|
613
|
-
each: (body: Block, key: LExpr, at: Scope) => void,
|
|
861
|
+
each: (body: Block, key: LExpr, at: Scope, names: ReadonlyMap<string, Bound>) => void,
|
|
614
862
|
): void {
|
|
615
|
-
const
|
|
863
|
+
const { axis, at: addr, as } = e;
|
|
864
|
+
const list = listOf(axis);
|
|
865
|
+
const tests = firstTest + axisChildren(axis) - whereTests(axis).length;
|
|
866
|
+
const from = addr === undefined || addr.length === 0 ? s.focus : this.#address(addr, s, path, firstTest + axisChildren(axis), b);
|
|
616
867
|
const base = baseAxis(axis);
|
|
617
868
|
let space = s.space;
|
|
618
869
|
let over: LExpr;
|
|
619
870
|
let skip: LExpr | undefined;
|
|
620
871
|
// The index of the record of each target: the index of the loop, or else the index that the axis gives.
|
|
621
872
|
let idx: LExpr | undefined;
|
|
622
|
-
|
|
873
|
+
// The value and the position of each item of the axis list.
|
|
874
|
+
let item: { readonly values: LExpr; readonly nth: LExpr } | undefined;
|
|
875
|
+
if (list !== undefined) {
|
|
876
|
+
// The list evaluates at the key of the address (AXIS.LIST).
|
|
877
|
+
const moved: Scope = from === s.focus ? s : { ...s, focus: from, rec: prim("get", s.space, from), slot: undefined };
|
|
878
|
+
const value = this.value(b, this.expr(list, moved, [...path, firstTest], b));
|
|
879
|
+
const targets = this.checked(b, call("items", value, bool(base.t === "reverse")), "a list of the axis list", this.site(b, moved, { t: "listAxis", path }, value));
|
|
880
|
+
over = prim("keysOf", targets);
|
|
881
|
+
item = { values: prim("valuesOf", targets), nth: prim("idxOf", targets) };
|
|
882
|
+
} else if (base.t === "table") {
|
|
623
883
|
// The axis table moves the evaluation to the space of its table, and there the origin is the target.
|
|
624
|
-
space = this.checked(b, call("table", local(TABLES), this.constant(base.name)), "a table");
|
|
884
|
+
space = this.checked(b, call("table", local(TABLES), this.constant(base.name)), "a table", this.#text(b, s, path, errorText.noTable(base.name)));
|
|
625
885
|
over = call("keys", space);
|
|
626
886
|
} else if (base.t === "all" || base.t === "others") {
|
|
627
887
|
over = call("keys", s.space);
|
|
628
888
|
if (base.t === "others") skip = from;
|
|
629
889
|
} else {
|
|
630
|
-
const targets = this.checked(b, { t: "targets", axis: this.index(base), args: [s.space, s.origin, from] }, "the targets of an axis");
|
|
890
|
+
const targets = this.checked(b, { t: "targets", axis: this.index(base), args: [s.space, s.origin, from] }, "the targets of an axis", this.site(b, s, { t: "axis", axis: base, path }, s.space, from));
|
|
631
891
|
over = prim("keysOf", targets);
|
|
632
892
|
idx = this.value(b, prim("idxOf", targets));
|
|
633
893
|
}
|
|
634
|
-
|
|
894
|
+
// A key of a list can be outside the space, and it has no index of the space, so the record comes from the key.
|
|
895
|
+
const records = list === undefined ? this.value(b, prim("records", space)) : undefined;
|
|
635
896
|
const label = this.labels++;
|
|
636
897
|
const array_ = this.locals++;
|
|
637
898
|
const index = this.locals++;
|
|
638
|
-
const
|
|
639
|
-
const key = local(
|
|
899
|
+
const target = this.locals++;
|
|
900
|
+
const key = local(target);
|
|
640
901
|
const body = block(items(label));
|
|
641
902
|
if (skip !== undefined) body.stmts.push({ t: "if", cond: prim("===", key, skip), yes: [{ t: "next" }], no: [] });
|
|
642
903
|
const slot = idx === undefined ? local(index) : prim("member", idx, local(index));
|
|
643
|
-
const
|
|
904
|
+
const rec = records === undefined ? prim("get", space, key) : prim("recordAt", records, slot, space, key);
|
|
905
|
+
const at: Scope = { space, origin: base.t === "table" ? key : s.origin, focus: key, env: s.env, fns: s.fns, rec, columns: s.columns, slot: list === undefined ? slot : undefined };
|
|
644
906
|
whereTests(axis).forEach((test, i) => {
|
|
645
|
-
const
|
|
907
|
+
const testPath = [...path, tests + i];
|
|
908
|
+
const ok = this.#boolean(this.expr(test, at, testPath, body), body, at, { t: "test", path: testPath });
|
|
646
909
|
body.stmts.push({ t: "if", cond: prim("!", ok), yes: [{ t: "next" }], no: [] });
|
|
647
910
|
});
|
|
648
|
-
|
|
649
|
-
|
|
911
|
+
const names = new Map<string, Bound>();
|
|
912
|
+
const [name, position] = as ?? [];
|
|
913
|
+
if (name !== undefined && item !== undefined) names.set(name, { value: this.value(body, prim("member", item.values, local(index))) });
|
|
914
|
+
if (position !== undefined && item !== undefined) names.set(position, { value: this.#typed(this.value(body, prim("member", item.nth, local(index))), "number") });
|
|
915
|
+
each(body, key, at, names);
|
|
916
|
+
b.stmts.push({ t: "loop", label, item: target, index, array: array_, over, body: body.stmts });
|
|
650
917
|
}
|
|
651
918
|
|
|
652
919
|
/** This method gives a local for each name of a loop, with its first value at the node (LOOP.NAMES). */
|
|
@@ -683,20 +950,115 @@ class Lowerer {
|
|
|
683
950
|
const tests = first + Object.keys(e.step).length;
|
|
684
951
|
const { env, locals } = this.#loopNames(e.init, s, path, b);
|
|
685
952
|
const inner: Scope = { ...s, env };
|
|
953
|
+
const limit = this.#limit(e, s, path, tests, b);
|
|
954
|
+
const untilAt = tests + axisChildren(limitAxis(e) ?? { t: "all" });
|
|
686
955
|
const label = this.labels++;
|
|
687
956
|
const k = this.typedLocal("number");
|
|
688
957
|
b.stmts.push({ t: "let", n: k, value: num(0) });
|
|
689
958
|
const body = block(b.fail);
|
|
690
959
|
if (e.until !== undefined) {
|
|
691
|
-
const stop = this.#boolean(this.expr(e.until, inner, [...path,
|
|
960
|
+
const stop = this.#boolean(this.expr(e.until, inner, [...path, untilAt], body), body, inner, { t: "until", path: [...path, untilAt] });
|
|
692
961
|
body.stmts.push({ t: "check", ok: prim("!", stop), reason: "the test until", fail: { t: "break", label } });
|
|
693
962
|
}
|
|
694
963
|
// At the limit, an iteration without a test ends, and an iteration with a test fails.
|
|
695
|
-
|
|
964
|
+
const more = prim("!==", local(k), limit);
|
|
965
|
+
if (e.until === undefined) body.stmts.push({ t: "check", ok: more, reason: "the limit of iterate", fail: { t: "break", label } });
|
|
966
|
+
else this.check(body, more, "the limit of iterate", this.site(body, s, { t: "limit", path }, limit));
|
|
696
967
|
this.#loopStep(e.step, inner, path, first, body, locals);
|
|
697
968
|
body.stmts.push({ t: "set", n: k, value: prim("+", local(k), num(1)) });
|
|
698
969
|
b.stmts.push({ t: "repeat", label, body: body.stmts });
|
|
699
|
-
return this.expr(e.result, inner, [...path,
|
|
970
|
+
return this.expr(e.result, inner, [...path, untilAt + (e.until === undefined ? 0 : 1)], b);
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
/**
|
|
974
|
+
* This method lowers a fixed point (FIX.ROUNDS). The column is an array with the value of each key, in the order of
|
|
975
|
+
* the keys. The rows of `init` give its first values, and each round makes the array of the next values. A round
|
|
976
|
+
* without a change ends the loop with the old array, as the interpreter does. After the last round, an error stops
|
|
977
|
+
* the column (FIX.LIMIT). The value of the node is the row of the focus.
|
|
978
|
+
*/
|
|
979
|
+
#fix(e: ExprOf<"fix">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
980
|
+
const n = this.locals++;
|
|
981
|
+
const column: FixColumn = { local: local(n), join: e.join };
|
|
982
|
+
b.stmts.push({ t: "let", n, value: array() });
|
|
983
|
+
this.#rows(e, e.init, s, path, 0, b, (row, _at, v) => {
|
|
984
|
+
row.stmts.push({ t: "push", n, value: v });
|
|
985
|
+
});
|
|
986
|
+
const limit = this.value(b, call("rounds", s.space));
|
|
987
|
+
const label = this.labels++;
|
|
988
|
+
const k = this.typedLocal("number");
|
|
989
|
+
b.stmts.push({ t: "let", n: k, value: num(0) });
|
|
990
|
+
const body = block(b.fail);
|
|
991
|
+
this.check(body, prim("!==", local(k), limit), "the limit of the rounds", this.site(body, s, { t: "fixLimit", name: e.name, path }, limit));
|
|
992
|
+
const next = this.locals++;
|
|
993
|
+
const changed = this.locals++;
|
|
994
|
+
body.stmts.push({ t: "let", n: next, value: array() }, { t: "let", n: changed, value: bool(false) });
|
|
995
|
+
// The step has the column in its scope, so an inner `fix` with the same name hides the column.
|
|
996
|
+
this.#rows(e, e.step, { ...s, columns: new Map([...s.columns, [e.name, column]]) }, path, 1, body, (row, at, v) => {
|
|
997
|
+
const old = this.value(row, prim("member", column.local, at.slot as LExpr));
|
|
998
|
+
const value = this.value(row, prim(JOINS[e.join], old, v));
|
|
999
|
+
row.stmts.push({ t: "if", cond: prim("!==", value, old), yes: [{ t: "set", n: changed, value: bool(true) }], no: [] }, { t: "push", n: next, value });
|
|
1000
|
+
});
|
|
1001
|
+
body.stmts.push(
|
|
1002
|
+
{ t: "check", ok: local(changed), reason: "a round that changes a row", fail: { t: "break", label } },
|
|
1003
|
+
{ t: "set", n, value: local(next) },
|
|
1004
|
+
{ t: "set", n: k, value: prim("+", local(k), num(1)) },
|
|
1005
|
+
);
|
|
1006
|
+
b.stmts.push({ t: "repeat", label, body: body.stmts });
|
|
1007
|
+
return this.#rowOf(e.name, column, s, path, b);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* This method lowers the evaluation of `x` at each row of a column. An error in a row does not stop the next rows, so
|
|
1012
|
+
* each extension of the round evaluates (FIX.ERROR). After the loop, the errors of the rows give the error of the
|
|
1013
|
+
* node. `each` adds the statements of a row with a good value.
|
|
1014
|
+
*/
|
|
1015
|
+
#rows(e: ExprOf<"fix">, x: Expr, s: Scope, path: readonly number[], child: number, b: Block, each: (row: Block, at: Scope, value: LExpr) => void): void {
|
|
1016
|
+
const errors = this.locals++;
|
|
1017
|
+
b.stmts.push({ t: "let", n: errors, value: array() });
|
|
1018
|
+
const kind: ErrorSite = { t: "row", name: e.name, join: e.join, path };
|
|
1019
|
+
// Stryker disable next-line ArrowFunction: each check of a row is in its own block, so the loop reads no failure
|
|
1020
|
+
this.#targetLoop(ROWS, s, path, 0, b, () => b.fail, (body, _key, at) => {
|
|
1021
|
+
const row = this.#attempt(
|
|
1022
|
+
body,
|
|
1023
|
+
(inner) => {
|
|
1024
|
+
const v = this.value(inner, this.expr(x, at, [...path, child], inner));
|
|
1025
|
+
if (joinKind(e.join) === "boolean") this.#boolean(v, inner, at, kind);
|
|
1026
|
+
else this.check(inner, prim("isFinite", v), "a finite number", this.site(inner, at, kind, v));
|
|
1027
|
+
each(inner, at, v);
|
|
1028
|
+
return v;
|
|
1029
|
+
},
|
|
1030
|
+
true,
|
|
1031
|
+
);
|
|
1032
|
+
body.stmts.push({ t: "if", cond: prim("!", row.ok as LExpr), yes: [{ t: "push", n: errors, value: row.value }], no: [] });
|
|
1033
|
+
});
|
|
1034
|
+
const site = this.constants.push({ t: "rows", path } satisfies ErrorSite) - 1;
|
|
1035
|
+
this.check(b, prim("===", prim("member", local(errors), this.constant("length")), num(0)), "the rows have values", call("args", { t: "site", i: site }, s.origin, s.focus, local(errors)));
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/** This method reads the row of the focus. A focus that is not a key of the space has no row (FIX.LAST). */
|
|
1039
|
+
#rowOf(name: string, column: FixColumn, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
1040
|
+
const x = this.value(b, prim("member", column.local, s.slot ?? call("rowIndex", s.space, s.focus)));
|
|
1041
|
+
this.check(b, prim("isValue", x), "a row of the column", this.site(b, s, { t: "lastKey", name, path }));
|
|
1042
|
+
return this.#typed(x, joinKind(column.join));
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* This method gives the number of steps of an iteration. A data limit counts the targets of its axis. Its failure
|
|
1047
|
+
* stops the program, and tier 1 gives the error (ITERATE.LIMIT.ERROR).
|
|
1048
|
+
*/
|
|
1049
|
+
#limit(e: ExprOf<"iterate">, s: Scope, path: readonly number[], firstTest: number, b: Block): LExpr {
|
|
1050
|
+
const axis = limitAxis(e);
|
|
1051
|
+
if (axis === undefined) return num(e.limit as number);
|
|
1052
|
+
const n = this.typedLocal("number");
|
|
1053
|
+
b.stmts.push({ t: "let", n, value: num(0) });
|
|
1054
|
+
// The error of a limit that is not valid keeps its cause, so each failure here is the plain failure of the block.
|
|
1055
|
+
// COMPILE.EXT: the interpreter evaluates each test, and an extension in a test can give #DEPTH!. Thus a failed test stops the program.
|
|
1056
|
+
const plain: Block = { stmts: b.stmts, fail: axisExprs(axis).some((x) => this.#mayExt(x, s.fns)) ? STOP : plainOf(b.fail) };
|
|
1057
|
+
this.#targetLoop({ axis }, s, path, firstTest, plain, () => plain.fail, (body) => {
|
|
1058
|
+
body.stmts.push({ t: "set", n, value: prim("+", local(n), num(1)) });
|
|
1059
|
+
});
|
|
1060
|
+
this.check(b, prim("===", prim("min", local(n), num(MAX_ITERATIONS)), local(n)), "a data limit in the cap");
|
|
1061
|
+
return local(n);
|
|
700
1062
|
}
|
|
701
1063
|
|
|
702
1064
|
/** This method lowers a fold (FOLD.ORDER): the loop over the targets, with a step at each target. */
|
|
@@ -705,10 +1067,10 @@ class Lowerer {
|
|
|
705
1067
|
const tests = first + Object.keys(e.step).length;
|
|
706
1068
|
const { env, locals } = this.#loopNames(e.init, s, path, b);
|
|
707
1069
|
// An error of a test or of a step is the error of the fold.
|
|
708
|
-
this.#targetLoop(e
|
|
709
|
-
this.#loopStep(e.step, { ...at, env }, path, first, body, locals);
|
|
1070
|
+
this.#targetLoop(e, s, path, tests, b, () => b.fail, (body, _key, at, names) => {
|
|
1071
|
+
this.#loopStep(e.step, { ...at, env: new Map([...env, ...names]) }, path, first, body, locals);
|
|
710
1072
|
});
|
|
711
|
-
return this.expr(e.result, { ...s, env }, [...path, tests +
|
|
1073
|
+
return this.expr(e.result, { ...s, env }, [...path, tests + axisChildren(e.axis) + addrExprs(e.at).length], b);
|
|
712
1074
|
}
|
|
713
1075
|
}
|
|
714
1076
|
|