@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/src/loop.ts CHANGED
@@ -1,11 +1,12 @@
1
- import type { PrimitiveKind } from "@mark1russell7/vex";
1
+ import type { Addr, Axis, ErrorText, FixJoin, PrimitiveKind } from "@mark1russell7/vex";
2
2
 
3
3
  /**
4
4
  * The loop IR (docs/COMPILER.md §3.3): a small language of statements between the Vex IR and the back ends. It has
5
5
  * locals, loops over arrays, checks and the functions of the runtime. A back end reads only this IR.
6
6
  *
7
- * A check that fails stops the program with the value `FALLBACK`. Then tier 1 does the evaluation again, so the result
8
- * is the result of the interpreter (§4.1).
7
+ * A check that fails stops the program with a lazy error, and a materialize step gives the error of the interpreter
8
+ * (§4.1). Where the error has a use, a check that cannot name its error stops the program with the value `FALLBACK`, and
9
+ * tier 1 evaluates it. A check with the failure `STOP` stops the program in each context, because its failure is no error.
9
10
  */
10
11
 
11
12
  /** The fixed locals of each program: the space, the origin, the variables and the tables of the run. */
@@ -22,14 +23,22 @@ export type RuntimeFn =
22
23
  | "addr" // addr(space, origin, focus, address, values?): the key of an address, with the values of its computed moves
23
24
  | "table" // table(tables, name): the space of a table
24
25
  | "keys" // keys(space): the keys of a space
26
+ | "rowIndex" // rowIndex(space, key): the index of a key in the keys of a space, or undefined
27
+ | "rounds" // rounds(space): the largest number of rounds of a column of the space (FIX.LIMIT)
25
28
  | "step" // step(name, acc, value): one step of reduce
26
29
  | "result" // result(value): the checks of the value of an op
27
30
  | "opts" // opts(value): the read of the option strict of a list op
28
31
  | "rec" // rec(names, values): a frozen record
29
32
  | "list" // list(keys, values): a list of an axis
33
+ | "items" // items(value, reverse): the targets of the axis list, or FALLBACK for a value that is not a list
30
34
  | "frozen" // frozen(array): a frozen copy
31
35
  | "ext" // ext(kind, data, space, origin, focus, path): an extension
32
- | "var"; // var(vars, name): a variable of the run
36
+ | "var" // var(vars, name): a variable of the run
37
+ | "err" // err(site, origin, focus, ...operands): the lazy error of a failed check
38
+ | "raise" // raise(error): the end of the program with a lazy error
39
+ | "args" // args(site, origin, focus, errors): the error of the arguments that failed, or undefined for no error
40
+ | "bad" // bad(value): the record of a result of an op that failed its checks
41
+ | "invalid"; // invalid(thrown, domain): the record of an exception of a check `valid`
33
42
 
34
43
  /** The primitive operations of the loop IR. They copy no arithmetic of a domain (§4.2). */
35
44
  export type PrimOp =
@@ -43,16 +52,19 @@ export type PrimOp =
43
52
  | "min" // Math.min, the minimum of a reduction
44
53
  | "max" // Math.max, the maximum of a reduction
45
54
  | "isNumber"
55
+ | "isFinite" // Number.isFinite: a number that is not NaN and not an infinity
46
56
  | "isBoolean"
47
57
  | "isString"
48
58
  | "isValue" // not undefined and not null
49
59
  | "isObject" // an object or a function
60
+ | "unless" // unless(ok, error): the error, or undefined when ok is true
50
61
  | "get" // get(space, key): the record of a key
51
62
  | "member" // member(object, name): the property of an object
52
63
  | "records" // records(space): the records of the space by index, or undefined (SPACE.RECORDS)
53
64
  | "recordAt" // recordAt(records, index, space, key): the record at an index, or else the record of the key
54
65
  | "keysOf" // keysOf(targets): the keys of the targets of an axis
55
66
  | "idxOf" // idxOf(targets): the index of each target in the keys of the space
67
+ | "valuesOf" // valuesOf(targets): the value of each target of the axis list
56
68
  | "memoGet" // memoGet(memo, space, keys): the entry of a space and the values of its keys, or undefined
57
69
  | "memoPut" // memoPut(memo, space, keys, value, ok): the new entry of a space and the values of its keys
58
70
  | "entryValue" // entryValue(entry): the value of an entry of a memo
@@ -65,6 +77,10 @@ export type LExpr =
65
77
  | { readonly t: "num"; readonly v: number }
66
78
  | { readonly t: "bool"; readonly v: boolean }
67
79
  | { readonly t: "fallback" }
80
+ /** The value `undefined`: no error. */
81
+ | { readonly t: "none" }
82
+ /** The `ErrorSite` at the index `i` of the constants. Only a failure reads it. */
83
+ | { readonly t: "site"; readonly i: number }
68
84
  | { readonly t: "array"; readonly items: readonly LExpr[] }
69
85
  | { readonly t: "prim"; readonly op: PrimOp; readonly args: readonly LExpr[] }
70
86
  | { readonly t: "call"; readonly fn: RuntimeFn; readonly args: readonly LExpr[] }
@@ -78,19 +94,68 @@ export type LExpr =
78
94
  /** The targets of the base axis that is the constant `axis`, at the space, the origin and the focus of `args`. */
79
95
  | { readonly t: "targets"; readonly axis: number; readonly args: readonly [LExpr, LExpr, LExpr] };
80
96
 
97
+ /**
98
+ * The site of an error: the facts of the error that the lowering knows (docs/COMPILER.md §4.1). The operands of a
99
+ * failure give the other facts. Each site has the path of its node, and `materialize` of the runtime reads it.
100
+ */
101
+ export type ErrorSite =
102
+ /** The origin is not a key of the space. */
103
+ | { readonly t: "key" }
104
+ /** The text does not depend on a value of the run. */
105
+ | { readonly t: "text"; readonly text: ErrorText; readonly path: readonly number[] }
106
+ /** A call of a name that is not a function here. Its operand is the variables of the run. */
107
+ | { readonly t: "call"; readonly name: string; readonly path: readonly number[] }
108
+ /** A reference that reads no value. Its operand is the key. */
109
+ | { readonly t: "missing"; readonly where: string; readonly path: readonly number[] }
110
+ /** An argument of a special form that is not a boolean. Its operand is the value. */
111
+ | { readonly t: "boolean"; readonly op: string; readonly i: number; readonly path: readonly number[] }
112
+ /** An item of a list op of the wrong kind. Its operand is the item. */
113
+ | { readonly t: "items"; readonly op: string; readonly what: "numbers" | "booleans"; readonly path: readonly number[] }
114
+ /** The test `until` of `iterate`, without a boolean. Its operand is the value. */
115
+ | { readonly t: "until"; readonly path: readonly number[] }
116
+ /** The limit of `iterate`, with no true value of the test. Its operand is the number of steps. */
117
+ | { readonly t: "limit"; readonly path: readonly number[] }
118
+ /** The round limit of a column, with rows that change. Its operand is the number of rounds. */
119
+ | { readonly t: "fixLimit"; readonly name: string; readonly path: readonly number[] }
120
+ /** A value of a row of the wrong kind, or a number that is not finite. Its operand is the value. */
121
+ | { readonly t: "row"; readonly name: string; readonly join: FixJoin; readonly path: readonly number[] }
122
+ /** A read of a column at a key that is not a row. */
123
+ | { readonly t: "lastKey"; readonly name: string; readonly path: readonly number[] }
124
+ /** Two or more rows of a round that failed (FIX.ERROR). Its operands are their errors. */
125
+ | { readonly t: "rows"; readonly path: readonly number[] }
126
+ /** The test of `where`, without a boolean. Its operand is the value. */
127
+ | { readonly t: "test"; readonly path: readonly number[] }
128
+ /** The axis `list` over a value that is not a list. Its operand is the value. */
129
+ | { readonly t: "listAxis"; readonly path: readonly number[] }
130
+ /** A failure of an op. The runtime recorded the detail of the failure. */
131
+ | { readonly t: "op"; readonly op: string; readonly path: readonly number[] }
132
+ /** A failure of a step of `reduce`. Its operand is the name of the op. */
133
+ | { readonly t: "step"; readonly path: readonly number[] }
134
+ /** A failure of an extension. The runtime recorded the detail of the failure. */
135
+ | { readonly t: "ext"; readonly kind: string; readonly path: readonly number[] }
136
+ /** An address that gives no key. Its operands are the space and the values of its computed moves. */
137
+ | { readonly t: "addr"; readonly at: Addr; readonly path: readonly number[] }
138
+ /** An axis that gives no targets. Its operands are the space and the focus. */
139
+ | { readonly t: "axis"; readonly axis: Exclude<Axis, { readonly t: "where" }>; readonly path: readonly number[] }
140
+ /** Two or more arguments or fields that failed. Its operands are their errors. */
141
+ | { readonly t: "args"; readonly what: string; readonly path: readonly number[] };
142
+
81
143
  /**
82
144
  * What a failed check does. The interpreter gives an error value at the node of the check, and the context of the node
83
145
  * decides what happens to that error:
84
146
  *
85
- * - `return`: the program stops with `FALLBACK`, and tier 1 evaluates it again.
147
+ * - `return`: the program stops with the lazy error of the check.
148
+ * - `stop`: the program stops with `FALLBACK`, and tier 1 evaluates it again. The check has no lazy error.
86
149
  * - `continue`: the item of a list op is an error item. The list op skips it, as the interpreter does. With the option
87
150
  * `strict`, the list op fails instead, and `strict.else` tells what that failure does.
88
- * - `break`: the first argument of `ifError` failed, so the block ends, and the second argument gives the value.
151
+ * - `break`: the block ends. For the first argument of `ifError`, the second argument then gives the value. With `keep`,
152
+ * the local `keep` gets the lazy error, so a later read of it can stop the program.
89
153
  */
90
154
  export type Fail =
91
155
  | { readonly t: "return" }
156
+ | { readonly t: "stop" }
92
157
  | { readonly t: "continue"; readonly label: number; readonly strict?: { readonly cond: LExpr; readonly else: Fail } }
93
- | { readonly t: "break"; readonly label: number };
158
+ | { readonly t: "break"; readonly label: number; readonly keep?: number };
94
159
 
95
160
  /** A statement of the loop IR. */
96
161
  export type LStmt =
@@ -98,10 +163,10 @@ export type LStmt =
98
163
  | { readonly t: "let"; readonly n: number; readonly value: LExpr }
99
164
  /** A new value of a local. */
100
165
  | { readonly t: "set"; readonly n: number; readonly value: LExpr }
101
- /** When `ok` is false, the statement does `fail`. The reason names the check. */
102
- | { readonly t: "check"; readonly ok: LExpr; readonly reason: string; readonly fail: Fail }
166
+ /** When `ok` is false, the statement does `fail`. The reason names the check, and `error` is its lazy error. */
167
+ | { readonly t: "check"; readonly ok: LExpr; readonly reason: string; readonly fail: Fail; readonly error?: LExpr }
103
168
  /** A check that fails always: the interpreter always gives an error here. */
104
- | { readonly t: "fail"; readonly fail: Fail }
169
+ | { readonly t: "fail"; readonly fail: Fail; readonly error?: LExpr }
105
170
  | { readonly t: "if"; readonly cond: LExpr; readonly yes: readonly LStmt[]; readonly no: readonly LStmt[] }
106
171
  /**
107
172
  * A loop over the items of an array, in order. The local `array` holds the array, `index` the index, and `item` each
@@ -110,6 +175,8 @@ export type LStmt =
110
175
  | { readonly t: "loop"; readonly label: number; readonly item: number; readonly index: number; readonly array: number; readonly over: LExpr; readonly body: readonly LStmt[] }
111
176
  /** A block with a label. A `break` with the label ends it. */
112
177
  | { readonly t: "block"; readonly label: number; readonly body: readonly LStmt[] }
178
+ /** The end of the block with the label. */
179
+ | { readonly t: "break"; readonly label: number }
113
180
  /** A loop with a label that repeats its body until a `break` with the label ends it. */
114
181
  | { readonly t: "repeat"; readonly label: number; readonly body: readonly LStmt[] }
115
182
  /** The next item of the innermost loop. */
@@ -133,6 +200,27 @@ export const num = (v: number): LExpr => ({ t: "num", v });
133
200
  export const bool = (v: boolean): LExpr => ({ t: "bool", v });
134
201
  export const FALLBACK_EXPR: LExpr = { t: "fallback" };
135
202
  export const RETURN: Fail = Object.freeze({ t: "return" });
203
+ export const STOP: Fail = Object.freeze({ t: "stop" });
136
204
  export const array = (...items: LExpr[]): LExpr => ({ t: "array", items });
137
205
  export const prim = (op: PrimOp, ...args: LExpr[]): LExpr => ({ t: "prim", op, args });
138
206
  export const call = (fn: RuntimeFn, ...args: LExpr[]): LExpr => ({ t: "call", fn, args });
207
+
208
+ /** This function tells if a failure uses the lazy error of its check. */
209
+ export function keeps(f: Fail): boolean {
210
+ switch (f.t) {
211
+ case "return":
212
+ return true;
213
+ case "stop":
214
+ return false;
215
+ case "break":
216
+ return f.keep !== undefined;
217
+ case "continue":
218
+ return f.strict !== undefined && keeps(f.strict.else);
219
+ }
220
+ }
221
+
222
+ /** This function gives the failure of a check without a lazy error. Where the error has a use, the program stops. */
223
+ export function plainOf(f: Fail): Fail {
224
+ if (f.t === "continue") return f.strict === undefined ? f : { ...f, strict: { cond: f.strict.cond, else: plainOf(f.strict.else) } };
225
+ return f.t === "break" && f.keep === undefined ? f : STOP;
226
+ }