metano-runtime 2.1.0 → 2.3.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 +139 -0
- package/dist/system/equality.d.ts +8 -0
- package/dist/system/equality.js +13 -5
- package/dist/system/json/index.d.ts +1 -0
- package/dist/system/json/index.js +1 -0
- package/dist/system/json/union-guard-registry.d.ts +30 -0
- package/dist/system/json/union-guard-registry.js +34 -0
- package/dist/system/linq/compare-keys.d.ts +3 -2
- package/dist/system/linq/compare-keys.js +9 -7
- package/dist/system/linq/expr-tree-visitor.d.ts +173 -0
- package/dist/system/linq/expr-tree-visitor.js +417 -0
- package/dist/system/linq/expr-tree.d.ts +156 -0
- package/dist/system/linq/expr-tree.js +18 -0
- package/dist/system/linq/index.d.ts +29 -23
- package/dist/system/linq/index.js +25 -70
- package/dist/system/linq/linq.d.ts +107 -0
- package/dist/system/linq/linq.js +46 -0
- package/dist/system/linq/operators.d.ts +34 -0
- package/dist/system/linq/operators.js +352 -0
- package/dist/system/linq/terminals.d.ts +22 -0
- package/dist/system/linq/terminals.js +314 -0
- package/dist/system/linq/types.d.ts +249 -0
- package/dist/system/linq/types.js +0 -0
- package/package.json +2 -1
- package/dist/system/linq/append-enumerable.d.ts +0 -8
- package/dist/system/linq/append-enumerable.js +0 -15
- package/dist/system/linq/concat-enumerable.d.ts +0 -8
- package/dist/system/linq/concat-enumerable.js +0 -15
- package/dist/system/linq/distinct-by-enumerable.d.ts +0 -8
- package/dist/system/linq/distinct-by-enumerable.js +0 -21
- package/dist/system/linq/distinct-enumerable.d.ts +0 -7
- package/dist/system/linq/distinct-enumerable.js +0 -19
- package/dist/system/linq/enumerable-base.d.ts +0 -98
- package/dist/system/linq/enumerable-base.js +0 -306
- package/dist/system/linq/enumerable.d.ts +0 -16
- package/dist/system/linq/enumerable.js +0 -33
- package/dist/system/linq/except-enumerable.d.ts +0 -8
- package/dist/system/linq/except-enumerable.js +0 -22
- package/dist/system/linq/group-by-enumerable.d.ts +0 -8
- package/dist/system/linq/group-by-enumerable.js +0 -31
- package/dist/system/linq/intersect-enumerable.d.ts +0 -8
- package/dist/system/linq/intersect-enumerable.js +0 -22
- package/dist/system/linq/order-by-enumerable.d.ts +0 -9
- package/dist/system/linq/order-by-enumerable.js +0 -22
- package/dist/system/linq/prepend-enumerable.d.ts +0 -8
- package/dist/system/linq/prepend-enumerable.js +0 -15
- package/dist/system/linq/reverse-enumerable.d.ts +0 -7
- package/dist/system/linq/reverse-enumerable.js +0 -15
- package/dist/system/linq/select-enumerable.d.ts +0 -8
- package/dist/system/linq/select-enumerable.js +0 -17
- package/dist/system/linq/select-many-enumerable.d.ts +0 -8
- package/dist/system/linq/select-many-enumerable.js +0 -17
- package/dist/system/linq/skip-enumerable.d.ts +0 -8
- package/dist/system/linq/skip-enumerable.js +0 -21
- package/dist/system/linq/skip-while-enumerable.d.ts +0 -8
- package/dist/system/linq/skip-while-enumerable.js +0 -21
- package/dist/system/linq/source-enumerable.d.ts +0 -7
- package/dist/system/linq/source-enumerable.js +0 -12
- package/dist/system/linq/take-enumerable.d.ts +0 -8
- package/dist/system/linq/take-enumerable.js +0 -20
- package/dist/system/linq/take-while-enumerable.d.ts +0 -8
- package/dist/system/linq/take-while-enumerable.js +0 -19
- package/dist/system/linq/then-by-enumerable.d.ts +0 -9
- package/dist/system/linq/then-by-enumerable.js +0 -61
- package/dist/system/linq/union-enumerable.d.ts +0 -8
- package/dist/system/linq/union-enumerable.js +0 -27
- package/dist/system/linq/where-enumerable.d.ts +0 -8
- package/dist/system/linq/where-enumerable.js +0 -18
- package/dist/system/linq/zip-enumerable.d.ts +0 -9
- package/dist/system/linq/zip-enumerable.js +0 -24
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/** Default fallback name used when a meta tree has no `param` node. */
|
|
2
|
+
const DEFAULT_PARAM_NAME = "x";
|
|
3
|
+
/**
|
|
4
|
+
* Recursive scan for the first {@link ExprParam} node bound at the
|
|
5
|
+
* *current* lambda level. Visits every child of every node kind so a
|
|
6
|
+
* predicate that mentions the parameter inside a conditional branch,
|
|
7
|
+
* a method-call argument, or any other nested position still
|
|
8
|
+
* resolves. Returns `null` when the body has no param reference —
|
|
9
|
+
* callers default to a fallback (typically `"x"`) so a literal-only
|
|
10
|
+
* body still materializes.
|
|
11
|
+
*
|
|
12
|
+
* Lambda / new boundaries are NOT crossed: a nested lambda binds its
|
|
13
|
+
* own param symbol, and walking through it would shadow the outer
|
|
14
|
+
* binding with the inner one. The outer caller (e.g.
|
|
15
|
+
* {@link compileLambdaBody}) is responsible for resolving the body
|
|
16
|
+
* of a top-level lambda before consulting this helper, if applicable.
|
|
17
|
+
*/
|
|
18
|
+
export function readParamName(tree) {
|
|
19
|
+
switch (tree.kind) {
|
|
20
|
+
case "param":
|
|
21
|
+
return tree.name;
|
|
22
|
+
case "capture":
|
|
23
|
+
case "literal":
|
|
24
|
+
case "lambda":
|
|
25
|
+
case "new":
|
|
26
|
+
case "ref":
|
|
27
|
+
return null;
|
|
28
|
+
case "member":
|
|
29
|
+
return readParamName(tree.target);
|
|
30
|
+
case "call":
|
|
31
|
+
return (tree.target ? readParamName(tree.target) : null) ?? firstParamName(tree.args);
|
|
32
|
+
case "binary":
|
|
33
|
+
return readParamName(tree.left) ?? readParamName(tree.right);
|
|
34
|
+
case "unary":
|
|
35
|
+
return readParamName(tree.operand);
|
|
36
|
+
case "conditional":
|
|
37
|
+
return (readParamName(tree.condition) ??
|
|
38
|
+
readParamName(tree.whenTrue) ??
|
|
39
|
+
readParamName(tree.whenFalse));
|
|
40
|
+
case "let":
|
|
41
|
+
// Bindings are pre-computed subtrees that may contain the param
|
|
42
|
+
// reference too (e.g. `$0 = x.profile.age`); scan them before
|
|
43
|
+
// falling through to the body so a hoisted root still produces
|
|
44
|
+
// the right parameter name.
|
|
45
|
+
for (const binding of tree.bindings) {
|
|
46
|
+
const found = readParamName(binding.value);
|
|
47
|
+
if (found !== null)
|
|
48
|
+
return found;
|
|
49
|
+
}
|
|
50
|
+
return readParamName(tree.body);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function firstParamName(args) {
|
|
54
|
+
for (const arg of args) {
|
|
55
|
+
const found = readParamName(arg);
|
|
56
|
+
if (found !== null)
|
|
57
|
+
return found;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Generic ordering comparator for heterogeneous primitives. Numbers,
|
|
63
|
+
* strings, and bigints fall through to the native `<`/`>` operators
|
|
64
|
+
* after a uniform cast — the same approach the runtime's `compareKeys`
|
|
65
|
+
* helper uses. Lets the dynamic evaluator compare values without
|
|
66
|
+
* burning `as any` casts into every binary branch.
|
|
67
|
+
*/
|
|
68
|
+
export function compareValues(left, right) {
|
|
69
|
+
const a = left;
|
|
70
|
+
const b = right;
|
|
71
|
+
if (a < b)
|
|
72
|
+
return -1;
|
|
73
|
+
if (a > b)
|
|
74
|
+
return 1;
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Evaluates an {@link ExprTree} against a scope and returns the JS
|
|
79
|
+
* value the tree denotes. Default behavior for mock providers; same
|
|
80
|
+
* semantics the closure path would have produced for the same lambda.
|
|
81
|
+
*
|
|
82
|
+
* Throws when the tree mentions a kind the MVP evaluator does not
|
|
83
|
+
* support (currently `lambda` / `new`) or when a `param` node's name
|
|
84
|
+
* does not match {@link ExprTreeScope.paramName}.
|
|
85
|
+
*/
|
|
86
|
+
export function evaluateExprTree(tree, scope) {
|
|
87
|
+
switch (tree.kind) {
|
|
88
|
+
case "param":
|
|
89
|
+
return evaluateParam(tree, scope);
|
|
90
|
+
case "capture":
|
|
91
|
+
return evaluateCapture(tree, scope);
|
|
92
|
+
case "literal":
|
|
93
|
+
return tree.value;
|
|
94
|
+
case "member":
|
|
95
|
+
return evaluateMember(tree, scope);
|
|
96
|
+
case "call":
|
|
97
|
+
return evaluateCall(tree, scope);
|
|
98
|
+
case "binary":
|
|
99
|
+
return evaluateBinary(tree, scope);
|
|
100
|
+
case "unary":
|
|
101
|
+
return evaluateUnary(tree, scope);
|
|
102
|
+
case "conditional":
|
|
103
|
+
return evaluateExprTree(tree.condition, scope)
|
|
104
|
+
? evaluateExprTree(tree.whenTrue, scope)
|
|
105
|
+
: evaluateExprTree(tree.whenFalse, scope);
|
|
106
|
+
case "let":
|
|
107
|
+
return evaluateLet(tree, scope);
|
|
108
|
+
case "ref":
|
|
109
|
+
return evaluateRef(tree, scope);
|
|
110
|
+
case "lambda":
|
|
111
|
+
case "new":
|
|
112
|
+
throw new Error(`evaluateExprTree: kind '${tree.kind}' not supported in MVP`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Closes over the captures bundle and returns a `(item) => unknown`
|
|
117
|
+
* function the caller can apply directly — predicate for `where`,
|
|
118
|
+
* projector for `select`, key selector for `orderBy`, etc.
|
|
119
|
+
*
|
|
120
|
+
* The parameter name is read from the first {@link ExprParam} node in
|
|
121
|
+
* `meta.tree`; pass {@link fallbackParamName} (defaults to `"x"`) to
|
|
122
|
+
* support literal-only bodies that never mention the param.
|
|
123
|
+
*/
|
|
124
|
+
export function compileLambdaBody(meta, fallbackParamName = DEFAULT_PARAM_NAME) {
|
|
125
|
+
const captures = meta.captures ?? {};
|
|
126
|
+
// Top-level lambda meta: bind to the outer lambda's first param so
|
|
127
|
+
// nested lambdas (whose `params` would shadow it) cannot hijack the
|
|
128
|
+
// resolution via the body scan.
|
|
129
|
+
const topLevelParam = meta.tree.kind === "lambda" ? meta.tree.params[0]?.name : null;
|
|
130
|
+
const paramName = topLevelParam ?? readParamName(meta.tree) ?? fallbackParamName;
|
|
131
|
+
return (item) => evaluateExprTree(meta.tree, { paramName, paramValue: item, captures });
|
|
132
|
+
}
|
|
133
|
+
function evaluateParam(node, scope) {
|
|
134
|
+
if (node.name !== scope.paramName) {
|
|
135
|
+
throw new Error(`evaluateExprTree: param '${node.name}' does not match scope '${scope.paramName}'`);
|
|
136
|
+
}
|
|
137
|
+
return scope.paramValue;
|
|
138
|
+
}
|
|
139
|
+
function evaluateCapture(node, scope) {
|
|
140
|
+
if (!(node.name in scope.captures)) {
|
|
141
|
+
throw new Error(`evaluateExprTree: capture '${node.name}' missing from captures bundle`);
|
|
142
|
+
}
|
|
143
|
+
return scope.captures[node.name];
|
|
144
|
+
}
|
|
145
|
+
function evaluateMember(node, scope) {
|
|
146
|
+
const target = evaluateExprTree(node.target, scope);
|
|
147
|
+
if (target === null || target === undefined) {
|
|
148
|
+
// Mirror plain JS property-access semantics: reading a member off
|
|
149
|
+
// null/undefined throws a TypeError. The closure path the provider
|
|
150
|
+
// cross-checks against behaves the same way, so divergence here
|
|
151
|
+
// would silently mask bugs.
|
|
152
|
+
throw new TypeError(`evaluateExprTree: cannot read member '${node.member}' from ${target === null ? "null" : "undefined"}`);
|
|
153
|
+
}
|
|
154
|
+
return target[node.member];
|
|
155
|
+
}
|
|
156
|
+
function evaluateCall(node, scope) {
|
|
157
|
+
// Free-function calls (`target === null`) lack a runtime receiver in
|
|
158
|
+
// the MVP shape — providers that care can resolve them via captures
|
|
159
|
+
// or a global-scope override. Surfacing as unsupported here lets the
|
|
160
|
+
// surrounding stage fall back to the closure rather than guess.
|
|
161
|
+
if (node.target === null) {
|
|
162
|
+
throw new Error(`evaluateExprTree: free-function call '${node.method}' is not supported by the default evaluator`);
|
|
163
|
+
}
|
|
164
|
+
const target = evaluateExprTree(node.target, scope);
|
|
165
|
+
const args = node.args.map((a) => evaluateExprTree(a, scope));
|
|
166
|
+
if (target === null || target === undefined) {
|
|
167
|
+
throw new TypeError(`evaluateExprTree: cannot invoke '${node.method}' on ${target === null ? "null" : "undefined"}`);
|
|
168
|
+
}
|
|
169
|
+
const fn = target[node.method];
|
|
170
|
+
if (typeof fn !== "function") {
|
|
171
|
+
throw new Error(`evaluateExprTree: '${node.method}' is not callable on the resolved target`);
|
|
172
|
+
}
|
|
173
|
+
return fn.apply(target, args);
|
|
174
|
+
}
|
|
175
|
+
function evaluateBinary(node, scope) {
|
|
176
|
+
// Short-circuit boolean ops keep semantics aligned with the closure
|
|
177
|
+
// path; lazy evaluation matters when one side has side effects.
|
|
178
|
+
if (node.op === "&&") {
|
|
179
|
+
return (Boolean(evaluateExprTree(node.left, scope)) && Boolean(evaluateExprTree(node.right, scope)));
|
|
180
|
+
}
|
|
181
|
+
if (node.op === "||") {
|
|
182
|
+
return (Boolean(evaluateExprTree(node.left, scope)) || Boolean(evaluateExprTree(node.right, scope)));
|
|
183
|
+
}
|
|
184
|
+
const left = evaluateExprTree(node.left, scope);
|
|
185
|
+
const right = evaluateExprTree(node.right, scope);
|
|
186
|
+
switch (node.op) {
|
|
187
|
+
case "==":
|
|
188
|
+
// biome-ignore lint/suspicious/noDoubleEquals: predicate parity with C# == comparison
|
|
189
|
+
return left == right;
|
|
190
|
+
case "!=":
|
|
191
|
+
// biome-ignore lint/suspicious/noDoubleEquals: predicate parity with C# != comparison
|
|
192
|
+
return left != right;
|
|
193
|
+
case "<":
|
|
194
|
+
return compareValues(left, right) < 0;
|
|
195
|
+
case "<=":
|
|
196
|
+
return compareValues(left, right) <= 0;
|
|
197
|
+
case ">":
|
|
198
|
+
return compareValues(left, right) > 0;
|
|
199
|
+
case ">=":
|
|
200
|
+
return compareValues(left, right) >= 0;
|
|
201
|
+
case "+":
|
|
202
|
+
// `+` overloads on string concatenation in both C# and JS. The
|
|
203
|
+
// cast satisfies the type checker — the JS runtime falls back to
|
|
204
|
+
// string concatenation automatically when either operand is a
|
|
205
|
+
// string, matching the closure path.
|
|
206
|
+
return left + right;
|
|
207
|
+
case "-":
|
|
208
|
+
return left - right;
|
|
209
|
+
case "*":
|
|
210
|
+
return left * right;
|
|
211
|
+
case "/":
|
|
212
|
+
// C# `int / int` is integer division; JS `/` is always float.
|
|
213
|
+
// Bridging requires per-node int-vs-double type info the IR does
|
|
214
|
+
// not currently distinguish. The default evaluator mirrors JS
|
|
215
|
+
// semantics — float division — which matches what the closure
|
|
216
|
+
// path emits for the same predicate today.
|
|
217
|
+
return left / right;
|
|
218
|
+
case "%":
|
|
219
|
+
return left % right;
|
|
220
|
+
default:
|
|
221
|
+
throw new Error(`evaluateExprTree: unsupported binary op '${node.op}'`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function evaluateLet(node, scope) {
|
|
225
|
+
// Evaluate bindings in declaration order so an earlier binding's
|
|
226
|
+
// value is visible to a later binding (matches how the compiler
|
|
227
|
+
// emits the $0/$1/… sequence).
|
|
228
|
+
const refs = { ...(scope.refs ?? {}) };
|
|
229
|
+
for (const binding of node.bindings) {
|
|
230
|
+
refs[binding.name] = evaluateExprTree(binding.value, { ...scope, refs });
|
|
231
|
+
}
|
|
232
|
+
return evaluateExprTree(node.body, { ...scope, refs });
|
|
233
|
+
}
|
|
234
|
+
function evaluateRef(node, scope) {
|
|
235
|
+
const refs = scope.refs;
|
|
236
|
+
if (refs === undefined || !(node.name in refs)) {
|
|
237
|
+
throw new Error(`evaluateExprTree: ref '${node.name}' resolves outside any enclosing let binding`);
|
|
238
|
+
}
|
|
239
|
+
return refs[node.name];
|
|
240
|
+
}
|
|
241
|
+
function evaluateUnary(node, scope) {
|
|
242
|
+
const operand = evaluateExprTree(node.operand, scope);
|
|
243
|
+
switch (node.op) {
|
|
244
|
+
case "!":
|
|
245
|
+
return !operand;
|
|
246
|
+
case "-":
|
|
247
|
+
return -operand;
|
|
248
|
+
case "+":
|
|
249
|
+
return +operand;
|
|
250
|
+
default:
|
|
251
|
+
throw new Error(`evaluateExprTree: unsupported unary op '${node.op}'`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Abstract visitor over {@link ExprTree}. Each `visitX` method handles
|
|
256
|
+
* one node kind. Default implementations recurse via {@link visit},
|
|
257
|
+
* so a subclass only overrides the methods it needs.
|
|
258
|
+
*
|
|
259
|
+
* Generic parameter `R` is the value the visitor produces — a SQL
|
|
260
|
+
* fragment, a GraphQL field, a JS value, an AST node in another IR,
|
|
261
|
+
* etc. Implementations decide how children's results combine in their
|
|
262
|
+
* override (e.g. a SQL emitter's `visitBinary` calls `this.visit(left)`
|
|
263
|
+
* and `this.visit(right)` and emits `(left) op (right)`).
|
|
264
|
+
*
|
|
265
|
+
* Example:
|
|
266
|
+
*
|
|
267
|
+
* ```ts
|
|
268
|
+
* class SqlPredicate extends ExprTreeVisitor<string> {
|
|
269
|
+
* protected visitBinary(node: ExprBinary): string {
|
|
270
|
+
* return `(${this.visit(node.left)} ${node.op} ${this.visit(node.right)})`;
|
|
271
|
+
* }
|
|
272
|
+
* protected visitMember(node: ExprMember): string {
|
|
273
|
+
* return `${this.visit(node.target)}.${node.member}`;
|
|
274
|
+
* }
|
|
275
|
+
* protected visitParam(node: ExprParam): string {
|
|
276
|
+
* return node.name;
|
|
277
|
+
* }
|
|
278
|
+
* protected visitLiteral(node: ExprLiteral): string {
|
|
279
|
+
* return JSON.stringify(node.value);
|
|
280
|
+
* }
|
|
281
|
+
* // capture / call / unary / conditional inherit the default recurse
|
|
282
|
+
* // behavior; override the ones the dialect actually translates.
|
|
283
|
+
* }
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
export class ExprTreeVisitor {
|
|
287
|
+
/**
|
|
288
|
+
* Dispatches to the appropriate `visitX` method. Subclasses can
|
|
289
|
+
* override to add tracing or to short-circuit on a specific node
|
|
290
|
+
* shape, but the default implementation is usually enough.
|
|
291
|
+
*/
|
|
292
|
+
visit(tree) {
|
|
293
|
+
switch (tree.kind) {
|
|
294
|
+
case "param":
|
|
295
|
+
return this.visitParam(tree);
|
|
296
|
+
case "capture":
|
|
297
|
+
return this.visitCapture(tree);
|
|
298
|
+
case "literal":
|
|
299
|
+
return this.visitLiteral(tree);
|
|
300
|
+
case "member":
|
|
301
|
+
return this.visitMember(tree);
|
|
302
|
+
case "call":
|
|
303
|
+
return this.visitCall(tree);
|
|
304
|
+
case "binary":
|
|
305
|
+
return this.visitBinary(tree);
|
|
306
|
+
case "unary":
|
|
307
|
+
return this.visitUnary(tree);
|
|
308
|
+
case "conditional":
|
|
309
|
+
return this.visitConditional(tree);
|
|
310
|
+
case "lambda":
|
|
311
|
+
return this.visitLambda(tree);
|
|
312
|
+
case "new":
|
|
313
|
+
return this.visitNew(tree);
|
|
314
|
+
case "let":
|
|
315
|
+
return this.visitLet(tree);
|
|
316
|
+
case "ref":
|
|
317
|
+
return this.visitRef(tree);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Combines the results of recursing into multiple children. Default
|
|
322
|
+
* returns the **last** result, which suits visitors that produce a
|
|
323
|
+
* value per node and only need the final aggregate (e.g. a side-effect
|
|
324
|
+
* collector). When the child list is empty (e.g. a free-function
|
|
325
|
+
* call with no args, an empty `new` expression), the visitor falls
|
|
326
|
+
* back to {@link leaf} so subclasses still get one well-defined
|
|
327
|
+
* extension point. Override to fold lists, concatenate strings, etc.
|
|
328
|
+
*/
|
|
329
|
+
combine(results) {
|
|
330
|
+
if (results.length === 0) {
|
|
331
|
+
return this.leaf();
|
|
332
|
+
}
|
|
333
|
+
// Non-null assertion is safe — we just guarded the empty case.
|
|
334
|
+
return results[results.length - 1];
|
|
335
|
+
}
|
|
336
|
+
visitParam(_node) {
|
|
337
|
+
return this.leaf();
|
|
338
|
+
}
|
|
339
|
+
visitCapture(_node) {
|
|
340
|
+
return this.leaf();
|
|
341
|
+
}
|
|
342
|
+
visitLiteral(_node) {
|
|
343
|
+
return this.leaf();
|
|
344
|
+
}
|
|
345
|
+
visitMember(node) {
|
|
346
|
+
// Single-child interior node still routes through `combine` so a
|
|
347
|
+
// subclass that overrides only `combine` (e.g. a SQL emitter that
|
|
348
|
+
// parenthesizes every interior node) sees every recurse path
|
|
349
|
+
// consistently — including the unary/member/lambda branches.
|
|
350
|
+
return this.combine([this.visit(node.target)]);
|
|
351
|
+
}
|
|
352
|
+
visitCall(node) {
|
|
353
|
+
const results = [];
|
|
354
|
+
if (node.target !== null)
|
|
355
|
+
results.push(this.visit(node.target));
|
|
356
|
+
for (const arg of node.args)
|
|
357
|
+
results.push(this.visit(arg));
|
|
358
|
+
return this.combine(results);
|
|
359
|
+
}
|
|
360
|
+
visitBinary(node) {
|
|
361
|
+
return this.combine([this.visit(node.left), this.visit(node.right)]);
|
|
362
|
+
}
|
|
363
|
+
visitUnary(node) {
|
|
364
|
+
return this.combine([this.visit(node.operand)]);
|
|
365
|
+
}
|
|
366
|
+
visitConditional(node) {
|
|
367
|
+
return this.combine([
|
|
368
|
+
this.visit(node.condition),
|
|
369
|
+
this.visit(node.whenTrue),
|
|
370
|
+
this.visit(node.whenFalse),
|
|
371
|
+
]);
|
|
372
|
+
}
|
|
373
|
+
visitLambda(node) {
|
|
374
|
+
return this.combine([this.visit(node.body)]);
|
|
375
|
+
}
|
|
376
|
+
visitNew(node) {
|
|
377
|
+
const results = node.args.map((a) => this.visit(a));
|
|
378
|
+
for (const init of node.initializers ?? []) {
|
|
379
|
+
results.push(this.visit(init.value));
|
|
380
|
+
}
|
|
381
|
+
return this.combine(results);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Hoisted common-subexpression wrapper produced by the compiler
|
|
385
|
+
* (#209). Default behavior recurses through every binding value
|
|
386
|
+
* (in declaration order) and then the body, threading the children
|
|
387
|
+
* through {@link combine}.
|
|
388
|
+
*/
|
|
389
|
+
visitLet(node) {
|
|
390
|
+
const results = [];
|
|
391
|
+
for (const binding of node.bindings)
|
|
392
|
+
results.push(this.visit(binding.value));
|
|
393
|
+
results.push(this.visit(node.body));
|
|
394
|
+
return this.combine(results);
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Reference to a binding introduced by an enclosing {@link ExprLet}.
|
|
398
|
+
* Default behavior is to treat it as a leaf — subclasses that build
|
|
399
|
+
* a SQL/expression dialect should override and emit the alias the
|
|
400
|
+
* matching binding generated.
|
|
401
|
+
*/
|
|
402
|
+
visitRef(_node) {
|
|
403
|
+
return this.leaf();
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Default result for true leaf nodes ({@link visitParam},
|
|
407
|
+
* {@link visitCapture}, {@link visitLiteral}) AND for interior
|
|
408
|
+
* nodes whose recurse-list is empty (e.g. a zero-arg method call,
|
|
409
|
+
* a `new` expression with no args and no initializers). Subclasses
|
|
410
|
+
* that inherit the default recurse must override <c>leaf</c> so
|
|
411
|
+
* {@link combine} has something to fold — the base throws to
|
|
412
|
+
* force a deliberate choice.
|
|
413
|
+
*/
|
|
414
|
+
leaf() {
|
|
415
|
+
throw new Error("ExprTreeVisitor.leaf: no default leaf value — override visitParam/visitCapture/visitLiteral or leaf()");
|
|
416
|
+
}
|
|
417
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expression tree shape — IR for a lambda body, populated by the
|
|
3
|
+
* compiler when the call site uses an opt-in {@link queryable} surface
|
|
4
|
+
* (see `[Queryable]` attribute on the C# side, or a `Expression<Func<…>>`
|
|
5
|
+
* parameter type lifted from the BCL).
|
|
6
|
+
*
|
|
7
|
+
* Two consumers read the same tree:
|
|
8
|
+
*
|
|
9
|
+
* - **IQueryable provider** (SQL / GraphQL / OData) walks the tree by
|
|
10
|
+
* `kind` to translate `(u) => u.age > 18` into `WHERE age > 18` —
|
|
11
|
+
* without parsing closure source.
|
|
12
|
+
* - **Runtime** ignores the tree and calls the paired closure directly
|
|
13
|
+
* (LINQ-to-Objects fallback).
|
|
14
|
+
*
|
|
15
|
+
* MVP subset: enough for the common LINQ predicate / projector shapes
|
|
16
|
+
* (comparison, member access, method calls, ternary, basic arithmetic).
|
|
17
|
+
* Extend with new variants as concrete cases land.
|
|
18
|
+
*/
|
|
19
|
+
export type BinaryOp = "==" | "!=" | "<" | "<=" | ">" | ">=" | "+" | "-" | "*" | "/" | "%" | "&&" | "||" | "&" | "|" | "^" | "<<" | ">>";
|
|
20
|
+
export type UnaryOp = "!" | "-" | "+" | "~";
|
|
21
|
+
/**
|
|
22
|
+
* Qualified type reference attached to {@link ExprParam}, {@link ExprCapture},
|
|
23
|
+
* and {@link ExprLiteral} nodes. `name` is the simple identifier the provider
|
|
24
|
+
* uses for dispatch / column resolution; `from` is the cross-package origin
|
|
25
|
+
* (the `[EmitPackage]` id on the C# side) so providers can disambiguate two
|
|
26
|
+
* same-named types coming from different packages. Local and primitive types
|
|
27
|
+
* omit `from`.
|
|
28
|
+
*/
|
|
29
|
+
export interface ExprTypeRef {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly from?: string;
|
|
32
|
+
}
|
|
33
|
+
/** Reference to a lambda parameter — `(u) => …` introduces `u`. */
|
|
34
|
+
export interface ExprParam {
|
|
35
|
+
readonly kind: "param";
|
|
36
|
+
readonly name: string;
|
|
37
|
+
/** Qualified type emitted by the compiler when known. */
|
|
38
|
+
readonly type?: ExprTypeRef;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Reference to a closure-captured local — `(u) => u.age > minAge`
|
|
42
|
+
* captures `minAge` from the outer scope. Provider reads `name` and
|
|
43
|
+
* looks the runtime value up via the descriptor's separate `captures`
|
|
44
|
+
* record (avoids burning the value into the tree, lets the same tree
|
|
45
|
+
* re-bind to different capture environments).
|
|
46
|
+
*/
|
|
47
|
+
export interface ExprCapture {
|
|
48
|
+
readonly kind: "capture";
|
|
49
|
+
readonly name: string;
|
|
50
|
+
/** Qualified type emitted by the compiler when known. */
|
|
51
|
+
readonly type?: ExprTypeRef;
|
|
52
|
+
}
|
|
53
|
+
/** Constant value: literal, captured const, default. */
|
|
54
|
+
export interface ExprLiteral {
|
|
55
|
+
readonly kind: "literal";
|
|
56
|
+
readonly value: unknown;
|
|
57
|
+
/** Qualified type emitted by the compiler when known. */
|
|
58
|
+
readonly type?: ExprTypeRef;
|
|
59
|
+
}
|
|
60
|
+
/** Property / field access: `target.member`. */
|
|
61
|
+
export interface ExprMember {
|
|
62
|
+
readonly kind: "member";
|
|
63
|
+
readonly target: ExprTree;
|
|
64
|
+
readonly member: string;
|
|
65
|
+
}
|
|
66
|
+
/** Method or extension call: `target.method(args)` or `static(args)`. */
|
|
67
|
+
export interface ExprCall {
|
|
68
|
+
readonly kind: "call";
|
|
69
|
+
readonly target: ExprTree | null;
|
|
70
|
+
readonly method: string;
|
|
71
|
+
readonly args: readonly ExprTree[];
|
|
72
|
+
}
|
|
73
|
+
/** Binary op: `left op right`. */
|
|
74
|
+
export interface ExprBinary {
|
|
75
|
+
readonly kind: "binary";
|
|
76
|
+
readonly op: BinaryOp;
|
|
77
|
+
readonly left: ExprTree;
|
|
78
|
+
readonly right: ExprTree;
|
|
79
|
+
}
|
|
80
|
+
/** Unary op: `op operand`. */
|
|
81
|
+
export interface ExprUnary {
|
|
82
|
+
readonly kind: "unary";
|
|
83
|
+
readonly op: UnaryOp;
|
|
84
|
+
readonly operand: ExprTree;
|
|
85
|
+
}
|
|
86
|
+
/** Ternary: `condition ? whenTrue : whenFalse`. */
|
|
87
|
+
export interface ExprConditional {
|
|
88
|
+
readonly kind: "conditional";
|
|
89
|
+
readonly condition: ExprTree;
|
|
90
|
+
readonly whenTrue: ExprTree;
|
|
91
|
+
readonly whenFalse: ExprTree;
|
|
92
|
+
}
|
|
93
|
+
/** Lambda — for nested expressions like `selectMany((u) => u.orders.where(o => …))`. */
|
|
94
|
+
export interface ExprLambda {
|
|
95
|
+
readonly kind: "lambda";
|
|
96
|
+
readonly params: readonly ExprParam[];
|
|
97
|
+
readonly body: ExprTree;
|
|
98
|
+
}
|
|
99
|
+
/** New-object: `new T { p = v, … }` or `new T(args)`. */
|
|
100
|
+
export interface ExprNew {
|
|
101
|
+
readonly kind: "new";
|
|
102
|
+
/**
|
|
103
|
+
* Qualified type emitted by the compiler when known. Same shape as the
|
|
104
|
+
* `type` field on {@link ExprParam} / {@link ExprCapture} / {@link ExprLiteral}
|
|
105
|
+
* so a provider can dispatch DTO constructors across packages via the
|
|
106
|
+
* combined `name + from` key.
|
|
107
|
+
*/
|
|
108
|
+
readonly type?: ExprTypeRef;
|
|
109
|
+
readonly args: readonly ExprTree[];
|
|
110
|
+
readonly initializers?: readonly {
|
|
111
|
+
readonly name: string;
|
|
112
|
+
readonly value: ExprTree;
|
|
113
|
+
}[];
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Single `name = value` binding inside an {@link ExprLet}. Names are
|
|
117
|
+
* synthetic identifiers (`$0`, `$1`, …) chosen by the compiler's
|
|
118
|
+
* hoisting pass; their scope is the surrounding {@link ExprLet.body}.
|
|
119
|
+
*/
|
|
120
|
+
export interface ExprLetBinding {
|
|
121
|
+
readonly name: string;
|
|
122
|
+
readonly value: ExprTree;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Common-subexpression wrapper introduced by the compiler when a pure
|
|
126
|
+
* subtree repeats inside `body` — every previous occurrence is
|
|
127
|
+
* rewritten as an {@link ExprRef} pointing at the binding's `name`.
|
|
128
|
+
* Mirrors `let bindings in body` from functional IRs.
|
|
129
|
+
*/
|
|
130
|
+
export interface ExprLet {
|
|
131
|
+
readonly kind: "let";
|
|
132
|
+
readonly bindings: readonly ExprLetBinding[];
|
|
133
|
+
readonly body: ExprTree;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Reference to an {@link ExprLetBinding} introduced by an enclosing
|
|
137
|
+
* {@link ExprLet}. The evaluator resolves `name` against the nearest
|
|
138
|
+
* binding scope; providers translate it to the dialect's name for the
|
|
139
|
+
* shared value (a SQL alias, a query-string variable, …).
|
|
140
|
+
*/
|
|
141
|
+
export interface ExprRef {
|
|
142
|
+
readonly kind: "ref";
|
|
143
|
+
readonly name: string;
|
|
144
|
+
}
|
|
145
|
+
/** Discriminated union of every supported node. */
|
|
146
|
+
export type ExprTree = ExprParam | ExprCapture | ExprLiteral | ExprMember | ExprCall | ExprBinary | ExprUnary | ExprConditional | ExprLambda | ExprNew | ExprLet | ExprRef;
|
|
147
|
+
/**
|
|
148
|
+
* Bundle threaded by the compiler when an opt-in queryable surface is
|
|
149
|
+
* detected (`IQueryable<T>` receiver or `[Queryable]` attribute).
|
|
150
|
+
* Carries the lambda's expression tree plus any closure captures the
|
|
151
|
+
* provider needs to evaluate at translation time.
|
|
152
|
+
*/
|
|
153
|
+
export interface QueryableMeta {
|
|
154
|
+
readonly tree: ExprTree;
|
|
155
|
+
readonly captures?: Readonly<Record<string, unknown>>;
|
|
156
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expression tree shape — IR for a lambda body, populated by the
|
|
3
|
+
* compiler when the call site uses an opt-in {@link queryable} surface
|
|
4
|
+
* (see `[Queryable]` attribute on the C# side, or a `Expression<Func<…>>`
|
|
5
|
+
* parameter type lifted from the BCL).
|
|
6
|
+
*
|
|
7
|
+
* Two consumers read the same tree:
|
|
8
|
+
*
|
|
9
|
+
* - **IQueryable provider** (SQL / GraphQL / OData) walks the tree by
|
|
10
|
+
* `kind` to translate `(u) => u.age > 18` into `WHERE age > 18` —
|
|
11
|
+
* without parsing closure source.
|
|
12
|
+
* - **Runtime** ignores the tree and calls the paired closure directly
|
|
13
|
+
* (LINQ-to-Objects fallback).
|
|
14
|
+
*
|
|
15
|
+
* MVP subset: enough for the common LINQ predicate / projector shapes
|
|
16
|
+
* (comparison, member access, method calls, ternary, basic arithmetic).
|
|
17
|
+
* Extend with new variants as concrete cases land.
|
|
18
|
+
*/
|
|
@@ -1,23 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export {
|
|
23
|
-
export {
|
|
1
|
+
/**
|
|
2
|
+
* Descriptor-based LINQ runtime (#20).
|
|
3
|
+
*
|
|
4
|
+
* Each operator is a standalone factory the bundler drops when unused.
|
|
5
|
+
* Every factory returns a tagged descriptor (`kind` + lambdas + `apply`)
|
|
6
|
+
* so runtime walks via `apply` while an IQueryable provider can
|
|
7
|
+
* introspect the chain by reading `kind` and the captured lambdas.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { linq, where, select, toArray } from "metano-runtime/system/linq";
|
|
13
|
+
*
|
|
14
|
+
* const result = linq(
|
|
15
|
+
* items,
|
|
16
|
+
* where(x => x.active),
|
|
17
|
+
* select(x => x.name),
|
|
18
|
+
* toArray(),
|
|
19
|
+
* );
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export { LINQ_STAGES, getStages, linq, stages } from "./linq.ts";
|
|
23
|
+
export type { Stage } from "./linq.ts";
|
|
24
|
+
export type { BinaryOp, ExprBinary, ExprCall, ExprCapture, ExprConditional, ExprLambda, ExprLiteral, ExprMember, ExprNew, ExprParam, ExprTree, ExprTypeRef, ExprUnary, QueryableMeta, UnaryOp, } from "./expr-tree.ts";
|
|
25
|
+
export { ExprTreeVisitor, compareValues, compileLambdaBody, evaluateExprTree, readParamName, } from "./expr-tree-visitor.ts";
|
|
26
|
+
export type { ExprTreeScope } from "./expr-tree-visitor.ts";
|
|
27
|
+
export type { AnyOperator, AnyTerminal, OperatorBase, OperatorKind, TerminalBase, TerminalKind, AppendOp, ConcatOp, DistinctByOp, DistinctOp, GroupByOp, Grouping, OrderByDescendingOp, OrderByOp, ThenByDescendingOp, ThenByOp, PrependOp, ReverseOp, SelectManyOp, SelectOp, SkipOp, SkipWhileOp, TakeOp, TakeWhileOp, WhereOp, ZipOp, AggregateTerm, AllTerm, AnyTerm, AverageTerm, ContainsTerm, CountTerm, FirstOrDefaultTerm, FirstTerm, LastOrDefaultTerm, LastTerm, MaxTerm, MaxByTerm, MinTerm, MinByTerm, SingleTerm, SingleOrDefaultTerm, SumTerm, ToArrayTerm, ToMapTerm, ToSetTerm, } from "./types.ts";
|
|
28
|
+
export { where, select, selectMany, take, skip, takeWhile, skipWhile, distinct, distinctBy, concat, append, prepend, reverse, orderBy, orderByDescending, thenBy, thenByDescending, groupBy, zip, } from "./operators.ts";
|
|
29
|
+
export { toArray, toMap, toSet, first, firstOrDefault, last, lastOrDefault, single, singleOrDefault, any, all, count, sum, min, max, minBy, maxBy, average, contains, aggregate, } from "./terminals.ts";
|