@mark1russell7/vex-compiler 0.0.0-bootstrap.0 → 0.7.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 ADDED
@@ -0,0 +1,138 @@
1
+ import type { PrimitiveKind } from "@mark1russell7/vex";
2
+
3
+ /**
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
+ * locals, loops over arrays, checks and the functions of the runtime. A back end reads only this IR.
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).
9
+ */
10
+
11
+ /** The fixed locals of each program: the space, the origin, the variables and the tables of the run. */
12
+ export const SPACE = 0;
13
+ export const ORIGIN = 1;
14
+ export const VARS = 2;
15
+ export const TABLES = 3;
16
+ /** The number of fixed locals. */
17
+ export const FIXED_LOCALS = 4;
18
+
19
+ /** The functions of the runtime that generated code calls. Each one gives `FALLBACK` for a value it cannot give. */
20
+ export type RuntimeFn =
21
+ | "has" // has(space, key): boolean
22
+ | "addr" // addr(space, origin, focus, address, values?): the key of an address, with the values of its computed moves
23
+ | "table" // table(tables, name): the space of a table
24
+ | "keys" // keys(space): the keys of a space
25
+ | "step" // step(name, acc, value): one step of reduce
26
+ | "result" // result(value): the checks of the value of an op
27
+ | "opts" // opts(value): the read of the option strict of a list op
28
+ | "rec" // rec(names, values): a frozen record
29
+ | "list" // list(keys, values): a list of an axis
30
+ | "frozen" // frozen(array): a frozen copy
31
+ | "ext" // ext(kind, data, space, origin, focus, path): an extension
32
+ | "var"; // var(vars, name): a variable of the run
33
+
34
+ /** The primitive operations of the loop IR. They copy no arithmetic of a domain (§4.2). */
35
+ export type PrimOp =
36
+ | "+" // the sum of a reduction, in the order of the interpreter
37
+ | "/" // the mean of a reduction
38
+ | "==="
39
+ | "!=="
40
+ | "!"
41
+ | "||"
42
+ | "&&"
43
+ | "min" // Math.min, the minimum of a reduction
44
+ | "max" // Math.max, the maximum of a reduction
45
+ | "isNumber"
46
+ | "isBoolean"
47
+ | "isString"
48
+ | "isValue" // not undefined and not null
49
+ | "isObject" // an object or a function
50
+ | "get" // get(space, key): the record of a key
51
+ | "member" // member(object, name): the property of an object
52
+ | "records" // records(space): the records of the space by index, or undefined (SPACE.RECORDS)
53
+ | "recordAt" // recordAt(records, index, space, key): the record at an index, or else the record of the key
54
+ | "keysOf" // keysOf(targets): the keys of the targets of an axis
55
+ | "idxOf" // idxOf(targets): the index of each target in the keys of the space
56
+ | "memoGet" // memoGet(memo, space, keys): the entry of a space and the values of its keys, or undefined
57
+ | "memoPut" // memoPut(memo, space, keys, value, ok): the new entry of a space and the values of its keys
58
+ | "entryValue" // entryValue(entry): the value of an entry of a memo
59
+ | "entryOk"; // entryOk(entry): true if the entry has a value
60
+
61
+ /** A value of the loop IR. */
62
+ export type LExpr =
63
+ | { readonly t: "const"; readonly i: number }
64
+ | { readonly t: "local"; readonly n: number }
65
+ | { readonly t: "num"; readonly v: number }
66
+ | { readonly t: "bool"; readonly v: boolean }
67
+ | { readonly t: "fallback" }
68
+ | { readonly t: "array"; readonly items: readonly LExpr[] }
69
+ | { readonly t: "prim"; readonly op: PrimOp; readonly args: readonly LExpr[] }
70
+ | { readonly t: "call"; readonly fn: RuntimeFn; readonly args: readonly LExpr[] }
71
+ /**
72
+ * The dispatch of the op whose name is the constant `name`, with the values of its arguments, the receiver first.
73
+ * `kinds` are the kinds of the values that the lowering knows (KIND.STATIC).
74
+ */
75
+ | { readonly t: "op"; readonly name: number; readonly args: readonly LExpr[]; readonly kinds: readonly (PrimitiveKind | undefined)[] }
76
+ /** The memo `id` of the program: the value of one node for each space (§7.2). */
77
+ | { readonly t: "memo"; readonly id: number }
78
+ /** The targets of the base axis that is the constant `axis`, at the space, the origin and the focus of `args`. */
79
+ | { readonly t: "targets"; readonly axis: number; readonly args: readonly [LExpr, LExpr, LExpr] };
80
+
81
+ /**
82
+ * What a failed check does. The interpreter gives an error value at the node of the check, and the context of the node
83
+ * decides what happens to that error:
84
+ *
85
+ * - `return`: the program stops with `FALLBACK`, and tier 1 evaluates it again.
86
+ * - `continue`: the item of a list op is an error item. The list op skips it, as the interpreter does. With the option
87
+ * `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.
89
+ */
90
+ export type Fail =
91
+ | { readonly t: "return" }
92
+ | { readonly t: "continue"; readonly label: number; readonly strict?: { readonly cond: LExpr; readonly else: Fail } }
93
+ | { readonly t: "break"; readonly label: number };
94
+
95
+ /** A statement of the loop IR. */
96
+ export type LStmt =
97
+ /** A new local with a value. */
98
+ | { readonly t: "let"; readonly n: number; readonly value: LExpr }
99
+ /** A new value of a local. */
100
+ | { 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 }
103
+ /** A check that fails always: the interpreter always gives an error here. */
104
+ | { readonly t: "fail"; readonly fail: Fail }
105
+ | { readonly t: "if"; readonly cond: LExpr; readonly yes: readonly LStmt[]; readonly no: readonly LStmt[] }
106
+ /**
107
+ * A loop over the items of an array, in order. The local `array` holds the array, `index` the index, and `item` each
108
+ * item. A `continue` with the label goes to the next item.
109
+ */
110
+ | { readonly t: "loop"; readonly label: number; readonly item: number; readonly index: number; readonly array: number; readonly over: LExpr; readonly body: readonly LStmt[] }
111
+ /** A block with a label. A `break` with the label ends it. */
112
+ | { readonly t: "block"; readonly label: number; readonly body: readonly LStmt[] }
113
+ /** A loop with a label that repeats its body until a `break` with the label ends it. */
114
+ | { readonly t: "repeat"; readonly label: number; readonly body: readonly LStmt[] }
115
+ /** The next item of the innermost loop. */
116
+ | { readonly t: "next" }
117
+ /** A value at the end of an array. */
118
+ | { readonly t: "push"; readonly n: number; readonly value: LExpr }
119
+ | { readonly t: "return"; readonly value: LExpr };
120
+
121
+ /** A program of the loop IR: its statements, the number of its locals and its constants. Each local is a name in the function. */
122
+ export interface LoopProgram {
123
+ readonly body: readonly LStmt[];
124
+ readonly locals: number;
125
+ /** The constants, by index: literals, paths, addresses, axes and names. Generated code reads them by index (§5). */
126
+ readonly constants: readonly unknown[];
127
+ }
128
+
129
+ // The constructors of the values of the loop IR.
130
+ export const local = (n: number): LExpr => ({ t: "local", n });
131
+ export const constant = (i: number): LExpr => ({ t: "const", i });
132
+ export const num = (v: number): LExpr => ({ t: "num", v });
133
+ export const bool = (v: boolean): LExpr => ({ t: "bool", v });
134
+ export const FALLBACK_EXPR: LExpr = { t: "fallback" };
135
+ export const RETURN: Fail = Object.freeze({ t: "return" });
136
+ export const array = (...items: LExpr[]): LExpr => ({ t: "array", items });
137
+ export const prim = (op: PrimOp, ...args: LExpr[]): LExpr => ({ t: "prim", op, args });
138
+ export const call = (fn: RuntimeFn, ...args: LExpr[]): LExpr => ({ t: "call", fn, args });