@telorun/cel 0.0.0-stage → 0.108.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/LICENSE +17 -0
- package/README.md +281 -2
- package/dist/activation.d.ts +27 -0
- package/dist/activation.d.ts.map +1 -0
- package/dist/activation.js +24 -0
- package/dist/backend-runtime.d.ts +184 -0
- package/dist/backend-runtime.d.ts.map +1 -0
- package/dist/backend-runtime.js +425 -0
- package/dist/bounded-cache.d.ts +21 -0
- package/dist/bounded-cache.d.ts.map +1 -0
- package/dist/bounded-cache.js +42 -0
- package/dist/catalog-runtime.d.ts +59 -0
- package/dist/catalog-runtime.d.ts.map +1 -0
- package/dist/catalog-runtime.js +786 -0
- package/dist/cel-expression.d.ts +32 -0
- package/dist/cel-expression.d.ts.map +1 -0
- package/dist/cel-expression.js +29 -0
- package/dist/cel-map-value.d.ts +47 -0
- package/dist/cel-map-value.d.ts.map +1 -0
- package/dist/cel-map-value.js +85 -0
- package/dist/cel-program.d.ts +44 -0
- package/dist/cel-program.d.ts.map +1 -0
- package/dist/cel-program.js +72 -0
- package/dist/cel-type.d.ts +131 -0
- package/dist/cel-type.d.ts.map +1 -0
- package/dist/cel-type.js +293 -0
- package/dist/cel-value.d.ts +166 -0
- package/dist/cel-value.d.ts.map +1 -0
- package/dist/cel-value.js +225 -0
- package/dist/check-diagnostic.d.ts +54 -0
- package/dist/check-diagnostic.d.ts.map +1 -0
- package/dist/check-diagnostic.js +66 -0
- package/dist/checker.d.ts +86 -0
- package/dist/checker.d.ts.map +1 -0
- package/dist/checker.js +806 -0
- package/dist/closure-backend.d.ts +21 -0
- package/dist/closure-backend.d.ts.map +1 -0
- package/dist/closure-backend.js +487 -0
- package/dist/comprehension-bindings.d.ts +33 -0
- package/dist/comprehension-bindings.d.ts.map +1 -0
- package/dist/comprehension-bindings.js +50 -0
- package/dist/comprehension-runtime.d.ts +44 -0
- package/dist/comprehension-runtime.d.ts.map +1 -0
- package/dist/comprehension-runtime.js +137 -0
- package/dist/declared-chain.d.ts +35 -0
- package/dist/declared-chain.d.ts.map +1 -0
- package/dist/declared-chain.js +36 -0
- package/dist/duration-value.d.ts +70 -0
- package/dist/duration-value.d.ts.map +1 -0
- package/dist/duration-value.js +149 -0
- package/dist/emitted-module.d.ts +207 -0
- package/dist/emitted-module.d.ts.map +1 -0
- package/dist/emitted-module.js +359 -0
- package/dist/engine-version.d.ts +3 -0
- package/dist/engine-version.d.ts.map +1 -0
- package/dist/engine-version.js +8 -0
- package/dist/environment-digest.d.ts +44 -0
- package/dist/environment-digest.d.ts.map +1 -0
- package/dist/environment-digest.js +98 -0
- package/dist/environment.d.ts +291 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +474 -0
- package/dist/function-catalog.d.ts +66 -0
- package/dist/function-catalog.d.ts.map +1 -0
- package/dist/function-catalog.js +77 -0
- package/dist/function-registry.d.ts +118 -0
- package/dist/function-registry.d.ts.map +1 -0
- package/dist/function-registry.js +292 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +65 -0
- package/dist/integer-arithmetic.d.ts +27 -0
- package/dist/integer-arithmetic.d.ts.map +1 -0
- package/dist/integer-arithmetic.js +58 -0
- package/dist/js-emitter.d.ts +133 -0
- package/dist/js-emitter.d.ts.map +1 -0
- package/dist/js-emitter.js +568 -0
- package/dist/json-schema-type.d.ts +182 -0
- package/dist/json-schema-type.d.ts.map +1 -0
- package/dist/json-schema-type.js +487 -0
- package/dist/json-text-scan.d.ts +28 -0
- package/dist/json-text-scan.d.ts.map +1 -0
- package/dist/json-text-scan.js +159 -0
- package/dist/lexer.d.ts +103 -0
- package/dist/lexer.d.ts.map +1 -0
- package/dist/lexer.js +458 -0
- package/dist/macro-check.d.ts +41 -0
- package/dist/macro-check.d.ts.map +1 -0
- package/dist/macro-check.js +162 -0
- package/dist/macro-shape.d.ts +24 -0
- package/dist/macro-shape.d.ts.map +1 -0
- package/dist/macro-shape.js +55 -0
- package/dist/member-read.d.ts +89 -0
- package/dist/member-read.d.ts.map +1 -0
- package/dist/member-read.js +166 -0
- package/dist/namespace-resolution.d.ts +35 -0
- package/dist/namespace-resolution.d.ts.map +1 -0
- package/dist/namespace-resolution.js +160 -0
- package/dist/nominal-type.d.ts +63 -0
- package/dist/nominal-type.d.ts.map +1 -0
- package/dist/nominal-type.js +98 -0
- package/dist/nullable-access.d.ts +38 -0
- package/dist/nullable-access.d.ts.map +1 -0
- package/dist/nullable-access.js +93 -0
- package/dist/parse-limits.d.ts +26 -0
- package/dist/parse-limits.d.ts.map +1 -0
- package/dist/parse-limits.js +21 -0
- package/dist/parser.d.ts +48 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +503 -0
- package/dist/qualified-calls.d.ts +22 -0
- package/dist/qualified-calls.d.ts.map +1 -0
- package/dist/qualified-calls.js +27 -0
- package/dist/regular-expression.d.ts +48 -0
- package/dist/regular-expression.d.ts.map +1 -0
- package/dist/regular-expression.js +77 -0
- package/dist/reserved-words.d.ts +52 -0
- package/dist/reserved-words.d.ts.map +1 -0
- package/dist/reserved-words.js +77 -0
- package/dist/resolved-call.d.ts +46 -0
- package/dist/resolved-call.d.ts.map +1 -0
- package/dist/resolved-call.js +15 -0
- package/dist/root-references.d.ts +23 -0
- package/dist/root-references.d.ts.map +1 -0
- package/dist/root-references.js +120 -0
- package/dist/runtime-library.d.ts +67 -0
- package/dist/runtime-library.d.ts.map +1 -0
- package/dist/runtime-library.js +554 -0
- package/dist/serializer.d.ts +24 -0
- package/dist/serializer.d.ts.map +1 -0
- package/dist/serializer.js +256 -0
- package/dist/sha256.d.ts +20 -0
- package/dist/sha256.d.ts.map +1 -0
- package/dist/sha256.js +103 -0
- package/dist/signature.d.ts +72 -0
- package/dist/signature.d.ts.map +1 -0
- package/dist/signature.js +61 -0
- package/dist/signatures/function-catalog.json +788 -0
- package/dist/signatures/standard-library.json +229 -0
- package/dist/standard-library.d.ts +41 -0
- package/dist/standard-library.d.ts.map +1 -0
- package/dist/standard-library.js +85 -0
- package/dist/syntax-diagnostic.d.ts +61 -0
- package/dist/syntax-diagnostic.d.ts.map +1 -0
- package/dist/syntax-diagnostic.js +28 -0
- package/dist/syntax-tree.d.ts +160 -0
- package/dist/syntax-tree.d.ts.map +1 -0
- package/dist/syntax-tree.js +58 -0
- package/dist/timestamp-value.d.ts +62 -0
- package/dist/timestamp-value.d.ts.map +1 -0
- package/dist/timestamp-value.js +238 -0
- package/dist/tree-equality.d.ts +15 -0
- package/dist/tree-equality.d.ts.map +1 -0
- package/dist/tree-equality.js +105 -0
- package/dist/type-expression.d.ts +42 -0
- package/dist/type-expression.d.ts.map +1 -0
- package/dist/type-expression.js +154 -0
- package/dist/value-equality.d.ts +38 -0
- package/dist/value-equality.d.ts.map +1 -0
- package/dist/value-equality.js +196 -0
- package/dist/value-text.d.ts +25 -0
- package/dist/value-text.d.ts.map +1 -0
- package/dist/value-text.js +44 -0
- package/dist/zoned-calendar.d.ts +61 -0
- package/dist/zoned-calendar.d.ts.map +1 -0
- package/dist/zoned-calendar.js +143 -0
- package/package.json +56 -3
- package/src/activation.ts +32 -0
- package/src/backend-runtime.ts +604 -0
- package/src/bounded-cache.ts +45 -0
- package/src/catalog-runtime.ts +921 -0
- package/src/cel-expression.ts +53 -0
- package/src/cel-map-value.ts +97 -0
- package/src/cel-program.ts +103 -0
- package/src/cel-type.ts +359 -0
- package/src/cel-value.ts +361 -0
- package/src/check-diagnostic.ts +104 -0
- package/src/checker.ts +1045 -0
- package/src/closure-backend.ts +547 -0
- package/src/comprehension-bindings.ts +66 -0
- package/src/comprehension-runtime.ts +157 -0
- package/src/declared-chain.ts +45 -0
- package/src/duration-value.ts +160 -0
- package/src/emitted-module.ts +494 -0
- package/src/engine-version.ts +9 -0
- package/src/environment-digest.ts +111 -0
- package/src/environment.ts +761 -0
- package/src/function-catalog.ts +140 -0
- package/src/function-registry.ts +341 -0
- package/src/index.ts +407 -0
- package/src/integer-arithmetic.ts +64 -0
- package/src/js-emitter.ts +721 -0
- package/src/json-schema-type.ts +664 -0
- package/src/json-text-scan.ts +163 -0
- package/src/lexer.ts +562 -0
- package/src/macro-check.ts +197 -0
- package/src/macro-shape.ts +66 -0
- package/src/member-read.ts +167 -0
- package/src/namespace-resolution.ts +167 -0
- package/src/nominal-type.ts +149 -0
- package/src/nullable-access.ts +95 -0
- package/src/parse-limits.ts +36 -0
- package/src/parser.ts +554 -0
- package/src/qualified-calls.ts +39 -0
- package/src/regular-expression.ts +101 -0
- package/src/reserved-words.ts +94 -0
- package/src/resolved-call.ts +47 -0
- package/src/root-references.ts +126 -0
- package/src/runtime-library.ts +639 -0
- package/src/serializer.ts +262 -0
- package/src/sha256.ts +112 -0
- package/src/signature.ts +127 -0
- package/src/signatures/function-catalog.json +788 -0
- package/src/signatures/standard-library.json +235 -0
- package/src/standard-library.ts +149 -0
- package/src/syntax-diagnostic.ts +72 -0
- package/src/syntax-tree.ts +229 -0
- package/src/timestamp-value.ts +310 -0
- package/src/tree-equality.ts +130 -0
- package/src/type-expression.ts +182 -0
- package/src/value-equality.ts +201 -0
- package/src/value-text.ts +45 -0
- package/src/zoned-calendar.ts +178 -0
|
@@ -0,0 +1,721 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The JavaScript emitter: a tree compiled to **source**, not to closures.
|
|
3
|
+
*
|
|
4
|
+
* It decides nothing the closure backend decides differently. Every operator and standard
|
|
5
|
+
* function is the runtime library's, every comprehension is `comprehension-runtime.ts`, and
|
|
6
|
+
* every member read, name read, bool operand and call-site dispatch is `backend-runtime.ts`
|
|
7
|
+
* — so what this file produces is a tree of CALLS into those, in the same order, with the
|
|
8
|
+
* same short-circuit. That is why the two backends can be held to one answer case for case:
|
|
9
|
+
* the only thing that differs is how control gets from one call to the next.
|
|
10
|
+
*
|
|
11
|
+
* Three properties the shape of the output exists for:
|
|
12
|
+
*
|
|
13
|
+
* - **The runtime is a parameter, never an import.** The emitted module's default export is
|
|
14
|
+
* a factory; it names no specifier, so it loads from a `data:` URL, from a cache directory
|
|
15
|
+
* mounted anywhere, and under a host that resolves nothing. A module naming
|
|
16
|
+
* `@telorun/cel` would also silently accept a runtime of another version, which the cache
|
|
17
|
+
* key could not see.
|
|
18
|
+
* - **No bare property access implements a CEL read.** `a.b`, `a['b']`, `a[expr]`, `.?`,
|
|
19
|
+
* `[?]` and `has()` all emit a call to the member-read seam, so no key an author or a
|
|
20
|
+
* request wrote reaches a prototype, a method or a function property. The frame's own
|
|
21
|
+
* fields are the engine's structure and are read directly, as they are in a closure.
|
|
22
|
+
* - **Nothing is asynchronous.** No `async`, no `await`, no promise: a thenable is refused
|
|
23
|
+
* at the same doors, because the doors are the shared runtime's.
|
|
24
|
+
*
|
|
25
|
+
* Emission is **deterministic**: temporaries are numbered per emitted function in tree
|
|
26
|
+
* order, bindings per module in tree order, and every hoisted constant is keyed by its own
|
|
27
|
+
* text, so the same tree against the same environment is the same bytes.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
CelCompileError,
|
|
32
|
+
plainMemberChain,
|
|
33
|
+
prefixCandidates,
|
|
34
|
+
type CompileTarget,
|
|
35
|
+
} from "./backend-runtime.js";
|
|
36
|
+
import { namespaceMacroBinding, receiverMacroBinding } from "./comprehension-bindings.js";
|
|
37
|
+
import { splitDeclaredChain } from "./declared-chain.js";
|
|
38
|
+
import { isMacroCall } from "./macro-check.js";
|
|
39
|
+
import { CALL_SITE_DIRECT_ARITY } from "./runtime-library.js";
|
|
40
|
+
import type { CelLiteral, CelNode, CelSelectNode, SourceRange } from "./syntax-tree.js";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The names an emitted module destructures from the runtime it is handed, in the order it
|
|
44
|
+
* destructures them. It is the module's whole contract with the engine: `emitterRuntime`
|
|
45
|
+
* (`emitted-module.ts`) builds an object with exactly these keys, and a module emitted by
|
|
46
|
+
* one engine against another's runtime fails at load rather than running on a missing
|
|
47
|
+
* binding — which is the second reason the key carries the engine version.
|
|
48
|
+
*/
|
|
49
|
+
export const RUNTIME_BINDINGS = [
|
|
50
|
+
"asyncValueRefused",
|
|
51
|
+
"boolOperand",
|
|
52
|
+
"callSite",
|
|
53
|
+
"celAll",
|
|
54
|
+
"celError",
|
|
55
|
+
"celExists",
|
|
56
|
+
"celExistsOne",
|
|
57
|
+
"celFilter",
|
|
58
|
+
"celIterable",
|
|
59
|
+
"celMapComprehension",
|
|
60
|
+
"celMapFromEntries",
|
|
61
|
+
"celSome",
|
|
62
|
+
"celUint",
|
|
63
|
+
"constants",
|
|
64
|
+
"hasMember",
|
|
65
|
+
"isCelError",
|
|
66
|
+
"isCelOptional",
|
|
67
|
+
"none",
|
|
68
|
+
"optionalEntry",
|
|
69
|
+
"optionalOfNonZero",
|
|
70
|
+
"readHostValue",
|
|
71
|
+
"readName",
|
|
72
|
+
"readNameChain",
|
|
73
|
+
"readThrough",
|
|
74
|
+
"searchNameChain",
|
|
75
|
+
] as const;
|
|
76
|
+
|
|
77
|
+
export type RuntimeBinding = (typeof RUNTIME_BINDINGS)[number];
|
|
78
|
+
|
|
79
|
+
/** A name a macro bound, and the JavaScript name its value lives under. */
|
|
80
|
+
interface Scope {
|
|
81
|
+
readonly name: string;
|
|
82
|
+
readonly js: string;
|
|
83
|
+
readonly outer: Scope | undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function boundName(scope: Scope | undefined, name: string): string | undefined {
|
|
87
|
+
for (let at = scope; at; at = at.outer) if (at.name === name) return at.js;
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The temporaries one emitted function declares. Each emitted function — the expression's
|
|
93
|
+
* own, and every comprehension or binding body inside it — declares its own, because a
|
|
94
|
+
* body's closure outlives the statement that called it and a name it shares with its caller
|
|
95
|
+
* would clobber a value still in use.
|
|
96
|
+
*
|
|
97
|
+
* **Every name is unique across the MODULE, not within the function**, and that is not
|
|
98
|
+
* tidiness: a body declaring `let t0` would **shadow** the `t0` its caller holds a bound
|
|
99
|
+
* value in, so `cel.bind(n, 2, xs.map(e, e + n))` read the element where it meant the
|
|
100
|
+
* binding and answered `[2, 4, 6]` for `[3, 4, 5]`. Numbering per module makes the shape
|
|
101
|
+
* impossible rather than avoided.
|
|
102
|
+
*/
|
|
103
|
+
class Temporaries {
|
|
104
|
+
private readonly names: string[] = [];
|
|
105
|
+
|
|
106
|
+
constructor(private readonly allocate: () => string) {}
|
|
107
|
+
|
|
108
|
+
next(): string {
|
|
109
|
+
const name = this.allocate();
|
|
110
|
+
this.names.push(name);
|
|
111
|
+
return name;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The `let` that declares them, or nothing where the function used none. */
|
|
115
|
+
declaration(): string {
|
|
116
|
+
return this.names.length === 0 ? "" : `let ${this.names.join(", ")}; `;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* One module's worth of emission. The hoist table is shared by every expression in the
|
|
122
|
+
* module, so two expressions reading the same name share one range and one rest-list: a
|
|
123
|
+
* constant allocated per evaluation is the cost the closure backend does not pay, and
|
|
124
|
+
* hoisting is how the emitter does not pay it either.
|
|
125
|
+
*/
|
|
126
|
+
export class ModuleEmitter {
|
|
127
|
+
private readonly hoisted: string[] = [];
|
|
128
|
+
private readonly hoistedByText = new Map<string, string>();
|
|
129
|
+
private locals = 0;
|
|
130
|
+
private bindings = 0;
|
|
131
|
+
|
|
132
|
+
constructor(private readonly target: CompileTarget) {}
|
|
133
|
+
|
|
134
|
+
/** The source of one expression's function: a `CelStep` over the evaluation frame. */
|
|
135
|
+
emitFunction(root: CelNode): string {
|
|
136
|
+
const temporaries = this.temporaries();
|
|
137
|
+
const body = this.node(root, undefined, temporaries);
|
|
138
|
+
return `(frame) => { ${temporaries.declaration()}return ${body}; }`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A fresh function's temporaries, drawing names from the module's own numbering. */
|
|
142
|
+
private temporaries(): Temporaries {
|
|
143
|
+
return new Temporaries(() => {
|
|
144
|
+
const name = `t${this.locals}`;
|
|
145
|
+
this.locals += 1;
|
|
146
|
+
return name;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The `const` lines every emitted expression reads, in first-use order. */
|
|
151
|
+
hoistedLines(): readonly string[] {
|
|
152
|
+
return this.hoisted;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private hoist(text: string): string {
|
|
156
|
+
const held = this.hoistedByText.get(text);
|
|
157
|
+
if (held !== undefined) return held;
|
|
158
|
+
const name = `h${this.hoisted.length}`;
|
|
159
|
+
this.hoisted.push(`const ${name} = ${text};`);
|
|
160
|
+
this.hoistedByText.set(text, name);
|
|
161
|
+
return name;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private range(range: SourceRange): string {
|
|
165
|
+
return this.hoist(`[${range[0]}, ${range[1]}]`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The name a comprehension's element is bound to: a **parameter** of the body function,
|
|
170
|
+
* numbered per module so a nested body can never shadow the one around it.
|
|
171
|
+
*/
|
|
172
|
+
private elementName(): string {
|
|
173
|
+
const name = `b${this.bindings}`;
|
|
174
|
+
this.bindings += 1;
|
|
175
|
+
return name;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// --- the tree -----------------------------------------------------------
|
|
179
|
+
|
|
180
|
+
private node(node: CelNode, scope: Scope | undefined, fn: Temporaries): string {
|
|
181
|
+
switch (node.kind) {
|
|
182
|
+
case "literal":
|
|
183
|
+
return this.literal(node.literal);
|
|
184
|
+
case "ident":
|
|
185
|
+
return this.ident(node, scope);
|
|
186
|
+
case "list":
|
|
187
|
+
return this.list(node, scope, fn);
|
|
188
|
+
case "map":
|
|
189
|
+
return this.map(node, scope, fn);
|
|
190
|
+
case "select":
|
|
191
|
+
return this.select(node, scope, fn);
|
|
192
|
+
case "index":
|
|
193
|
+
return this.index(node, scope, fn);
|
|
194
|
+
case "unary":
|
|
195
|
+
return this.call(node.operator, "global", [this.node(node.operand, scope, fn)], node.range, fn);
|
|
196
|
+
case "binary":
|
|
197
|
+
return this.binary(node, scope, fn);
|
|
198
|
+
case "conditional":
|
|
199
|
+
return this.conditional(node, scope, fn);
|
|
200
|
+
case "call":
|
|
201
|
+
case "receiverCall":
|
|
202
|
+
return this.anyCall(node, scope, fn);
|
|
203
|
+
case "qcall":
|
|
204
|
+
return this.qualifiedCall(node, scope, fn);
|
|
205
|
+
case "unparsed":
|
|
206
|
+
throw new CelCompileError("the expression could not be read whole, so it cannot be compiled");
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* A literal as the source that builds its value. A plain value is written inline; a uint
|
|
212
|
+
* and a bytes literal are **hoisted**, because each is an object and allocating one per
|
|
213
|
+
* evaluation would be work the closure backend does once at compile time.
|
|
214
|
+
*/
|
|
215
|
+
private literal(literal: CelLiteral): string {
|
|
216
|
+
switch (literal.type) {
|
|
217
|
+
case "int":
|
|
218
|
+
return integerSource(literal.value);
|
|
219
|
+
case "uint":
|
|
220
|
+
return this.hoist(`celUint(${integerSource(literal.value)})`);
|
|
221
|
+
case "double":
|
|
222
|
+
return doubleSource(literal.value);
|
|
223
|
+
case "string":
|
|
224
|
+
return textSource(literal.value);
|
|
225
|
+
case "bytes":
|
|
226
|
+
return this.hoist(`new Uint8Array([${[...literal.value].join(", ")}])`);
|
|
227
|
+
case "bool":
|
|
228
|
+
return literal.value ? "true" : "false";
|
|
229
|
+
case "null":
|
|
230
|
+
return "null";
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// --- names --------------------------------------------------------------
|
|
235
|
+
|
|
236
|
+
private ident(node: Extract<CelNode, { kind: "ident" }>, scope: Scope | undefined): string {
|
|
237
|
+
if (!node.absolute) {
|
|
238
|
+
const held = boundName(scope, node.name);
|
|
239
|
+
if (held !== undefined) return held;
|
|
240
|
+
}
|
|
241
|
+
return `readName(frame.activation, constants, ${textSource(node.name)}, ${this.range(node.range)})`;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* A chain of plain member names rooted at a free name. The split over the names the host
|
|
246
|
+
* declared happens **here, at emit time**, through the same function the checker and the
|
|
247
|
+
* closure backend split it with — so the emitted code performs one activation read and
|
|
248
|
+
* then member reads, and can never read a different name than the one the check typed.
|
|
249
|
+
*/
|
|
250
|
+
private chain(segments: readonly string[], range: SourceRange): string {
|
|
251
|
+
const declaredSplit = splitDeclaredChain(segments, this.target.declares);
|
|
252
|
+
const at = this.range(range);
|
|
253
|
+
if (declaredSplit) {
|
|
254
|
+
const rest = this.hoist(`[${declaredSplit.rest.map(textSource).join(", ")}]`);
|
|
255
|
+
return `readNameChain(frame.activation, constants, ${textSource(declaredSplit.name)}, ${rest}, ${at})`;
|
|
256
|
+
}
|
|
257
|
+
// Nothing declares a prefix, so the activation is searched longest prefix first — the
|
|
258
|
+
// same fallback, over the same candidate list, that the closure backend builds.
|
|
259
|
+
const candidates = this.hoist(
|
|
260
|
+
`[${prefixCandidates(segments)
|
|
261
|
+
.map(
|
|
262
|
+
(candidate) =>
|
|
263
|
+
`{ name: ${textSource(candidate.name)}, rest: [${candidate.rest.map(textSource).join(", ")}] }`,
|
|
264
|
+
)
|
|
265
|
+
.join(", ")}]`,
|
|
266
|
+
);
|
|
267
|
+
return `searchNameChain(frame.activation, constants, ${candidates}, ${textSource(segments.join("."))}, ${at})`;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// --- member reads -------------------------------------------------------
|
|
271
|
+
|
|
272
|
+
private select(node: CelSelectNode, scope: Scope | undefined, fn: Temporaries): string {
|
|
273
|
+
const chain = plainMemberChain(node, (name) => boundName(scope, name) !== undefined);
|
|
274
|
+
if (chain) return this.chain(chain, node.range);
|
|
275
|
+
const operand = this.node(node.operand, scope, fn);
|
|
276
|
+
const at = this.range(node.range);
|
|
277
|
+
return this.carrying(
|
|
278
|
+
[operand],
|
|
279
|
+
fn,
|
|
280
|
+
([held]) => `readThrough(${held}, ${textSource(node.field)}, ${node.optional}, ${at})`,
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private index(
|
|
285
|
+
node: Extract<CelNode, { kind: "index" }>,
|
|
286
|
+
scope: Scope | undefined,
|
|
287
|
+
fn: Temporaries,
|
|
288
|
+
): string {
|
|
289
|
+
const operand = this.node(node.operand, scope, fn);
|
|
290
|
+
const key = this.node(node.index, scope, fn);
|
|
291
|
+
const at = this.range(node.range);
|
|
292
|
+
return this.carrying(
|
|
293
|
+
[operand, key],
|
|
294
|
+
fn,
|
|
295
|
+
([held, named]) => `readThrough(${held}, ${named}, ${node.optional}, ${at})`,
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// --- aggregates ---------------------------------------------------------
|
|
300
|
+
|
|
301
|
+
private list(
|
|
302
|
+
node: Extract<CelNode, { kind: "list" }>,
|
|
303
|
+
scope: Scope | undefined,
|
|
304
|
+
fn: Temporaries,
|
|
305
|
+
): string {
|
|
306
|
+
const out = fn.next();
|
|
307
|
+
const at = this.range(node.range);
|
|
308
|
+
const entries = node.elements.map((element) => ({
|
|
309
|
+
value: this.node(element.value, scope, fn),
|
|
310
|
+
optional: element.optional,
|
|
311
|
+
}));
|
|
312
|
+
let body = out;
|
|
313
|
+
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
314
|
+
const entry = entries[index]!;
|
|
315
|
+
const value = fn.next();
|
|
316
|
+
if (!entry.optional) {
|
|
317
|
+
body = `(${value} = ${entry.value}, isCelError(${value}) ? ${value} : (${out}.push(${value}), ${body}))`;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
// An absent optional entry leaves no element: the aggregate shrinks, which is the
|
|
321
|
+
// whole reason to write one.
|
|
322
|
+
const held = fn.next();
|
|
323
|
+
body =
|
|
324
|
+
`(${value} = ${entry.value}, isCelError(${value}) ? ${value} : ` +
|
|
325
|
+
`(${held} = optionalEntry(${value}, ${at}), isCelError(${held}) ? ${held} : ` +
|
|
326
|
+
`(${held}.present && ${out}.push(${held}.held), ${body})))`;
|
|
327
|
+
}
|
|
328
|
+
return `(${out} = [], ${body})`;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private map(
|
|
332
|
+
node: Extract<CelNode, { kind: "map" }>,
|
|
333
|
+
scope: Scope | undefined,
|
|
334
|
+
fn: Temporaries,
|
|
335
|
+
): string {
|
|
336
|
+
const out = fn.next();
|
|
337
|
+
const at = this.range(node.range);
|
|
338
|
+
const entries = node.entries.map((entry) => ({
|
|
339
|
+
key: this.node(entry.key, scope, fn),
|
|
340
|
+
value: this.node(entry.value, scope, fn),
|
|
341
|
+
optional: entry.optional,
|
|
342
|
+
}));
|
|
343
|
+
let body = `celMapFromEntries(${out}, ${at})`;
|
|
344
|
+
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
345
|
+
const entry = entries[index]!;
|
|
346
|
+
const key = fn.next();
|
|
347
|
+
const value = fn.next();
|
|
348
|
+
const kept = entry.optional ? fn.next() : undefined;
|
|
349
|
+
const written = kept
|
|
350
|
+
? `(${kept} = optionalEntry(${value}, ${at}), isCelError(${kept}) ? ${kept} : ` +
|
|
351
|
+
`(${kept}.present && ${out}.push(${key}, ${kept}.held), ${body}))`
|
|
352
|
+
: `(${out}.push(${key}, ${value}), ${body})`;
|
|
353
|
+
body =
|
|
354
|
+
`(${key} = ${entry.key}, isCelError(${key}) ? ${key} : ` +
|
|
355
|
+
`(${value} = ${entry.value}, isCelError(${value}) ? ${value} : ${written}))`;
|
|
356
|
+
}
|
|
357
|
+
return `(${out} = [], ${body})`;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// --- operators ----------------------------------------------------------
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* `&&` and `||` carry an error-valued operand through: `false && <error>` is `false` and
|
|
364
|
+
* `true || <error>` is `true`, whichever side the error is on. The emitted form is the
|
|
365
|
+
* closure backend's branch order written out, so an error on one side and a non-bool on
|
|
366
|
+
* the other decide the same way.
|
|
367
|
+
*/
|
|
368
|
+
private binary(
|
|
369
|
+
node: Extract<CelNode, { kind: "binary" }>,
|
|
370
|
+
scope: Scope | undefined,
|
|
371
|
+
fn: Temporaries,
|
|
372
|
+
): string {
|
|
373
|
+
if (node.operator !== "&&" && node.operator !== "||") {
|
|
374
|
+
return this.call(
|
|
375
|
+
node.operator,
|
|
376
|
+
"global",
|
|
377
|
+
[this.node(node.left, scope, fn), this.node(node.right, scope, fn)],
|
|
378
|
+
node.range,
|
|
379
|
+
fn,
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
const left = this.node(node.left, scope, fn);
|
|
383
|
+
const right = this.node(node.right, scope, fn);
|
|
384
|
+
const at = this.range(node.range);
|
|
385
|
+
const decided = node.operator === "&&" ? "false" : "true";
|
|
386
|
+
const undecided = node.operator === "&&" ? "true" : "false";
|
|
387
|
+
const a = fn.next();
|
|
388
|
+
const b = fn.next();
|
|
389
|
+
return (
|
|
390
|
+
`(${a} = boolOperand(${left}, ${at}), ${a} === ${decided} ? ${decided} : ` +
|
|
391
|
+
`(${b} = boolOperand(${right}, ${at}), ${b} === ${decided} ? ${decided} : ` +
|
|
392
|
+
`isCelError(${a}) ? ${a} : isCelError(${b}) ? ${b} : ${undecided}))`
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
private conditional(
|
|
397
|
+
node: Extract<CelNode, { kind: "conditional" }>,
|
|
398
|
+
scope: Scope | undefined,
|
|
399
|
+
fn: Temporaries,
|
|
400
|
+
): string {
|
|
401
|
+
const condition = this.node(node.condition, scope, fn);
|
|
402
|
+
const whenTrue = this.node(node.whenTrue, scope, fn);
|
|
403
|
+
const whenFalse = this.node(node.whenFalse, scope, fn);
|
|
404
|
+
const at = this.range(node.range);
|
|
405
|
+
const held = fn.next();
|
|
406
|
+
return (
|
|
407
|
+
`(${held} = boolOperand(${condition}, ${at}), isCelError(${held}) ? ${held} : ` +
|
|
408
|
+
`${held} ? ${whenTrue} : ${whenFalse})`
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// --- calls --------------------------------------------------------------
|
|
413
|
+
|
|
414
|
+
private anyCall(
|
|
415
|
+
node: Extract<CelNode, { kind: "call" | "receiverCall" }>,
|
|
416
|
+
scope: Scope | undefined,
|
|
417
|
+
fn: Temporaries,
|
|
418
|
+
): string {
|
|
419
|
+
// The same question the checker asks, in the same place: a macro is not a function, and
|
|
420
|
+
// it is recognised before any overload is looked for.
|
|
421
|
+
if (isMacroCall(node)) return this.macro(node, scope, fn);
|
|
422
|
+
const args = [
|
|
423
|
+
...(node.kind === "receiverCall" ? [this.node(node.receiver, scope, fn)] : []),
|
|
424
|
+
...node.args.map((argument) => this.node(argument, scope, fn)),
|
|
425
|
+
];
|
|
426
|
+
return this.call(node.name, node.kind === "call" ? "global" : "receiver", args, node.range, fn);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* One dispatch: evaluate the arguments, carry the first error out, then hand the values to
|
|
431
|
+
* the site **positionally**. The site is hoisted, so it is built once per loaded module and
|
|
432
|
+
* holds the overloads it resolved — exactly as a compiled closure's does. The arity is the
|
|
433
|
+
* dispatch key's, so the entry point is chosen here and no argument array is built.
|
|
434
|
+
*/
|
|
435
|
+
private call(
|
|
436
|
+
name: string,
|
|
437
|
+
form: "global" | "receiver",
|
|
438
|
+
args: readonly string[],
|
|
439
|
+
range: SourceRange,
|
|
440
|
+
fn: Temporaries,
|
|
441
|
+
): string {
|
|
442
|
+
const site = this.hoist(
|
|
443
|
+
`callSite(${textSource(name)}, ${textSource(form)}, ${this.range(range)})`,
|
|
444
|
+
);
|
|
445
|
+
// A call written wider than the bound takes the array form — the arity is the SOURCE's,
|
|
446
|
+
// so a width no signature can declare is still something an author may write.
|
|
447
|
+
return this.carrying(args, fn, (values) =>
|
|
448
|
+
values.length <= CALL_SITE_DIRECT_ARITY
|
|
449
|
+
? `${site}.call${values.length}(${values.join(", ")})`
|
|
450
|
+
: `${site}.call([${values.join(", ")}])`,
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private qualifiedCall(
|
|
455
|
+
node: Extract<CelNode, { kind: "qcall" }>,
|
|
456
|
+
scope: Scope | undefined,
|
|
457
|
+
fn: Temporaries,
|
|
458
|
+
): string {
|
|
459
|
+
const args = node.args.map((argument) => this.node(argument, scope, fn));
|
|
460
|
+
const at = this.range(node.range);
|
|
461
|
+
const context = this.hoist(`{ range: ${at} }`);
|
|
462
|
+
const bound = fn.next();
|
|
463
|
+
// The arguments are evaluated only once something IS bound, which is the closure
|
|
464
|
+
// backend's order: a call nothing bound is `unbound_function` even where an argument
|
|
465
|
+
// would have failed.
|
|
466
|
+
const dispatched = this.carrying(
|
|
467
|
+
args,
|
|
468
|
+
fn,
|
|
469
|
+
(values) => `readHostValue(${bound}([${values.join(", ")}], ${context}), ${at})`,
|
|
470
|
+
);
|
|
471
|
+
const refusal = `celError("unbound_function", ${textSource(
|
|
472
|
+
`unbound function '${node.namespace}.${node.name}' — nothing is bound under that name here`,
|
|
473
|
+
)}, ${at})`;
|
|
474
|
+
return (
|
|
475
|
+
`(${bound} = frame.namespaceFunction ? frame.namespaceFunction(${textSource(node.namespace)}, ` +
|
|
476
|
+
`${textSource(node.name)}) : undefined, !${bound} ? ${refusal} : ${dispatched})`
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// --- macros -------------------------------------------------------------
|
|
481
|
+
|
|
482
|
+
private macro(
|
|
483
|
+
node: Extract<CelNode, { kind: "call" | "receiverCall" }>,
|
|
484
|
+
scope: Scope | undefined,
|
|
485
|
+
fn: Temporaries,
|
|
486
|
+
): string {
|
|
487
|
+
if (node.kind === "call") return this.has(node, scope, fn);
|
|
488
|
+
const receiver = node.receiver;
|
|
489
|
+
if (receiver.kind === "ident" && receiver.name === "cel" && node.name === "bind") {
|
|
490
|
+
return this.celBind(node, scope, fn);
|
|
491
|
+
}
|
|
492
|
+
if (receiver.kind === "ident" && receiver.name === "optional") {
|
|
493
|
+
return this.optionalNamespace(node, scope, fn);
|
|
494
|
+
}
|
|
495
|
+
if ((node.name === "optMap" || node.name === "optFlatMap") && node.args.length === 2) {
|
|
496
|
+
return this.optionalBinding(node, scope, fn);
|
|
497
|
+
}
|
|
498
|
+
return this.comprehension(node, scope, fn);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** `has(a.b)` — presence, which a missing key answers `false` rather than erroring. */
|
|
502
|
+
private has(
|
|
503
|
+
node: Extract<CelNode, { kind: "call" }>,
|
|
504
|
+
scope: Scope | undefined,
|
|
505
|
+
fn: Temporaries,
|
|
506
|
+
): string {
|
|
507
|
+
const argument = node.args[0]!;
|
|
508
|
+
if (argument.kind !== "select") {
|
|
509
|
+
throw new CelCompileError("has() asks about a member, so its argument ends in a select");
|
|
510
|
+
}
|
|
511
|
+
const operand = this.node(argument.operand, scope, fn);
|
|
512
|
+
const at = this.range(node.range);
|
|
513
|
+
return this.carrying(
|
|
514
|
+
[operand],
|
|
515
|
+
fn,
|
|
516
|
+
([held]) => `hasMember(${held}, ${textSource(argument.field)}, ${at})`,
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
private celBind(
|
|
521
|
+
node: Extract<CelNode, { kind: "receiverCall" }>,
|
|
522
|
+
scope: Scope | undefined,
|
|
523
|
+
fn: Temporaries,
|
|
524
|
+
): string {
|
|
525
|
+
const binding = namespaceMacroBinding("cel", node.name, node.args.length);
|
|
526
|
+
if (!binding) {
|
|
527
|
+
throw new CelCompileError("cel.bind(name, value, body) takes a name, its value and a body");
|
|
528
|
+
}
|
|
529
|
+
const name = node.args[binding.variableArgument]!;
|
|
530
|
+
if (name.kind !== "ident") throw new CelCompileError("cel.bind binds a name");
|
|
531
|
+
const value = this.node(node.args[1]!, scope, fn);
|
|
532
|
+
// The bound value lives in a temporary of the enclosing function, so it is DECLARED
|
|
533
|
+
// there: a body nested inside this one captures it as a closure captures any `let`.
|
|
534
|
+
const held = fn.next();
|
|
535
|
+
const body = this.node(node.args[2]!, { name: name.name, js: held, outer: scope }, fn);
|
|
536
|
+
const at = this.range(node.range);
|
|
537
|
+
const refused = fn.next();
|
|
538
|
+
// A name is bound to a VALUE, so a value that must be awaited is refused here rather
|
|
539
|
+
// than inside the body, where every read of the name would meet it again.
|
|
540
|
+
return (
|
|
541
|
+
`(${held} = ${value}, isCelError(${held}) ? ${held} : ` +
|
|
542
|
+
`(${refused} = asyncValueRefused(${held}, ${at}), ${refused} ? ${refused} : ${body}))`
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/** `optional.of(v)`, `optional.ofNonZeroValue(v)` and `optional.none()`. */
|
|
547
|
+
private optionalNamespace(
|
|
548
|
+
node: Extract<CelNode, { kind: "receiverCall" }>,
|
|
549
|
+
scope: Scope | undefined,
|
|
550
|
+
fn: Temporaries,
|
|
551
|
+
): string {
|
|
552
|
+
if (node.name === "none") return "none";
|
|
553
|
+
if (node.args.length !== 1) {
|
|
554
|
+
throw new CelCompileError(`optional.${node.name}(value) takes one argument`);
|
|
555
|
+
}
|
|
556
|
+
const value = this.node(node.args[0]!, scope, fn);
|
|
557
|
+
const wrap = node.name === "ofNonZeroValue" ? "optionalOfNonZero" : "celSome";
|
|
558
|
+
return this.carrying([value], fn, ([held]) => `${wrap}(${held})`);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/** `opt.optMap(v, body)` wraps what the body answers; `optFlatMap` does not. */
|
|
562
|
+
private optionalBinding(
|
|
563
|
+
node: Extract<CelNode, { kind: "receiverCall" }>,
|
|
564
|
+
scope: Scope | undefined,
|
|
565
|
+
fn: Temporaries,
|
|
566
|
+
): string {
|
|
567
|
+
const name = node.args[0]!;
|
|
568
|
+
if (name.kind !== "ident") throw new CelCompileError(`${node.name} binds a name`);
|
|
569
|
+
const receiver = this.node(node.receiver, scope, fn);
|
|
570
|
+
const at = this.range(node.range);
|
|
571
|
+
const optional = fn.next();
|
|
572
|
+
const refused = fn.next();
|
|
573
|
+
const answered = fn.next();
|
|
574
|
+
const held = fn.next();
|
|
575
|
+
const body = this.node(node.args[1]!, { name: name.name, js: held, outer: scope }, fn);
|
|
576
|
+
const wrong = `celError("no_matching_overload", ${textSource(`${JSON.stringify(node.name)} reads an optional`)}, ${at})`;
|
|
577
|
+
const result =
|
|
578
|
+
node.name === "optMap"
|
|
579
|
+
? `celSome(${answered})`
|
|
580
|
+
: `isCelOptional(${answered}) ? ${answered} : celError("no_matching_overload", ` +
|
|
581
|
+
`${textSource("optFlatMap's body answers an optional")}, ${at})`;
|
|
582
|
+
// A host may hand over the optional itself, so what it HOLDS is a door of its own.
|
|
583
|
+
return (
|
|
584
|
+
`(${optional} = ${receiver}, isCelError(${optional}) ? ${optional} : ` +
|
|
585
|
+
`!isCelOptional(${optional}) ? ${wrong} : ` +
|
|
586
|
+
`!${optional}.present ? none : ` +
|
|
587
|
+
`(${refused} = asyncValueRefused(${optional}.held, ${at}), ${refused} ? ${refused} : ` +
|
|
588
|
+
`(${held} = ${optional}.held, ${answered} = ${body}, isCelError(${answered}) ? ${answered} : ${result})))`
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* A comprehension. The body becomes a function of the element, exactly as the closure
|
|
594
|
+
* backend passes one, so the macro's meaning — including which error outranks which
|
|
595
|
+
* decided answer — stays `comprehension-runtime.ts`'s alone.
|
|
596
|
+
*/
|
|
597
|
+
private comprehension(
|
|
598
|
+
node: Extract<CelNode, { kind: "receiverCall" }>,
|
|
599
|
+
scope: Scope | undefined,
|
|
600
|
+
fn: Temporaries,
|
|
601
|
+
): string {
|
|
602
|
+
const binding = receiverMacroBinding(node.name, node.args.length);
|
|
603
|
+
if (!binding) throw new CelCompileError(`${node.name} is not a comprehension`);
|
|
604
|
+
const name = node.args[binding.variableArgument]!;
|
|
605
|
+
if (name.kind !== "ident") throw new CelCompileError(`${node.name} binds a name`);
|
|
606
|
+
const receiver = this.node(node.receiver, scope, fn);
|
|
607
|
+
const at = this.range(node.range);
|
|
608
|
+
const element = this.elementName();
|
|
609
|
+
const inner: Scope = { name: name.name, js: element, outer: scope };
|
|
610
|
+
const bodies = binding.scopedArguments.map((argument) => this.body(node.args[argument]!, inner, element));
|
|
611
|
+
const elements = fn.next();
|
|
612
|
+
const call = ((): string => {
|
|
613
|
+
switch (node.name) {
|
|
614
|
+
case "all":
|
|
615
|
+
return `celAll(${elements}, ${bodies[0]}, ${at})`;
|
|
616
|
+
case "exists":
|
|
617
|
+
return `celExists(${elements}, ${bodies[0]}, ${at})`;
|
|
618
|
+
case "exists_one":
|
|
619
|
+
return `celExistsOne(${elements}, ${bodies[0]}, ${at})`;
|
|
620
|
+
case "filter":
|
|
621
|
+
return `celFilter(${elements}, ${bodies[0]}, ${at})`;
|
|
622
|
+
default:
|
|
623
|
+
return bodies.length === 2
|
|
624
|
+
? `celMapComprehension(${elements}, ${bodies[1]}, ${bodies[0]}, ${at})`
|
|
625
|
+
: `celMapComprehension(${elements}, ${bodies[0]}, undefined, ${at})`;
|
|
626
|
+
}
|
|
627
|
+
})();
|
|
628
|
+
return this.carrying(
|
|
629
|
+
[receiver],
|
|
630
|
+
fn,
|
|
631
|
+
([held]) =>
|
|
632
|
+
`(${elements} = celIterable(${held}, ${at}), isCelError(${elements}) ? ${elements} : ${call})`,
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/** A body a comprehension calls per element: its own function, with its own temporaries. */
|
|
637
|
+
private body(node: CelNode, scope: Scope, parameter: string): string {
|
|
638
|
+
const temporaries = this.temporaries();
|
|
639
|
+
const text = this.node(node, scope, temporaries);
|
|
640
|
+
return `(${parameter}) => { ${temporaries.declaration()}return ${text}; }`;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// --- the one sequencing rule --------------------------------------------
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Evaluate each expression in order and carry the first error out, then build the answer
|
|
647
|
+
* from the values. Every form that evaluates more than one thing goes through here, so a
|
|
648
|
+
* later operand is never evaluated after an earlier one failed — which is what makes an
|
|
649
|
+
* error a value that short-circuits rather than an exception.
|
|
650
|
+
*/
|
|
651
|
+
private carrying(
|
|
652
|
+
expressions: readonly string[],
|
|
653
|
+
fn: Temporaries,
|
|
654
|
+
answer: (values: readonly string[]) => string,
|
|
655
|
+
): string {
|
|
656
|
+
const values = expressions.map(() => fn.next());
|
|
657
|
+
let out = answer(values);
|
|
658
|
+
for (let at = expressions.length - 1; at >= 0; at -= 1) {
|
|
659
|
+
out = `(${values[at]} = ${expressions[at]}, isCelError(${values[at]}) ? ${values[at]} : ${out})`;
|
|
660
|
+
}
|
|
661
|
+
return out;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// --- writing a value as source ---------------------------------------------
|
|
666
|
+
|
|
667
|
+
/** An int or uint literal as a `bigint`, parenthesized where it is negative. */
|
|
668
|
+
function integerSource(value: bigint): string {
|
|
669
|
+
return value < 0n ? `(${value}n)` : `${value}n`;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* A double as source. `-0` is a value CEL tells apart from `0`, and an overflowing literal
|
|
674
|
+
* reads as an infinity, so neither may go through the shortest decimal form.
|
|
675
|
+
*/
|
|
676
|
+
function doubleSource(value: number): string {
|
|
677
|
+
if (Number.isNaN(value)) return "NaN";
|
|
678
|
+
if (value === Number.POSITIVE_INFINITY) return "Infinity";
|
|
679
|
+
if (value === Number.NEGATIVE_INFINITY) return "(-Infinity)";
|
|
680
|
+
if (Object.is(value, -0)) return "(-0)";
|
|
681
|
+
return value < 0 ? `(${value})` : `${value}`;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const ESCAPES: Readonly<Record<string, string>> = {
|
|
685
|
+
"\\": "\\\\",
|
|
686
|
+
'"': '\\"',
|
|
687
|
+
"\n": "\\n",
|
|
688
|
+
"\r": "\\r",
|
|
689
|
+
"\t": "\\t",
|
|
690
|
+
"\b": "\\b",
|
|
691
|
+
"\f": "\\f",
|
|
692
|
+
"\v": "\\v",
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* Text as a JavaScript string literal. Every character outside printable ASCII is written
|
|
697
|
+
* as an escape — a lone surrogate, a line separator and a zero-width joiner all read back
|
|
698
|
+
* as themselves, and the emitted module is pure ASCII however its expressions were written,
|
|
699
|
+
* so no encoding assumption about the file it is stored in can change its meaning.
|
|
700
|
+
*/
|
|
701
|
+
export function textSource(text: string): string {
|
|
702
|
+
let out = '"';
|
|
703
|
+
for (const unit of text) {
|
|
704
|
+
const held = ESCAPES[unit];
|
|
705
|
+
if (held !== undefined) {
|
|
706
|
+
out += held;
|
|
707
|
+
continue;
|
|
708
|
+
}
|
|
709
|
+
const code = unit.codePointAt(0)!;
|
|
710
|
+
if (code >= 0x20 && code <= 0x7e) {
|
|
711
|
+
out += unit;
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
if (code > 0xffff) {
|
|
715
|
+
out += `\\u{${code.toString(16)}}`;
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
out += `\\u${code.toString(16).padStart(4, "0")}`;
|
|
719
|
+
}
|
|
720
|
+
return `${out}"`;
|
|
721
|
+
}
|