trilean 1.11.1 → 1.13.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.
@@ -0,0 +1,310 @@
1
+ import { definite, firstIndeterminate, indeterminate } from "./evaluation.js";
2
+ import { createEvaluationBudget } from "./evaluator-budget.js";
3
+ import { runSync, settleSync } from "./evaluator-effect.js";
4
+ import { absenceEvaluation, applyArithmetic, applyNegate, checkReferenceUnit, combineAnd, combineOr, compareText, compareValues, computeMembershipMatch, invokeRegisteredFunction } from "./evaluator-operations.js";
5
+ import { literalValue } from "./evaluator-literals.js";
6
+ import { evaluatePredicateInternal, evaluateValueInternal } from "./evaluator.js";
7
+ //#region src/evaluator-compiled.ts
8
+ /** Never charged: handed to the nodes of a tree compiled without budget counting, none of which reads it. */
9
+ const UNLIMITED_BUDGET = { checkNode: () => void 0 };
10
+ /** The `visitedTreeKeys` of a call that has not entered any `treeReference`; the interpreter copies it rather than mutating it, so one instance serves every call. */
11
+ const NO_TREE_KEYS = /* @__PURE__ */ new Set();
12
+ /** The message for the `accumulator` node outside a fold's `combine`, which is everywhere a compiled node can sit. */
13
+ const ACCUMULATOR_OUTSIDE_FOLD = "accumulator used outside a reduce fold's combine expression";
14
+ /** Compiles the nodes of one tree, for one setting of `counted`. */
15
+ var NodeCompiler = class {
16
+ functions;
17
+ maxNestingDepth;
18
+ counted;
19
+ /** The number of nodes compiled natively so far; an upper bound on the nodes one evaluation can visit, because every compiled node is visited at most once per call. */
20
+ nodesCompiled = 0;
21
+ /** Whether any node was left to the interpreter. */
22
+ interpreted = false;
23
+ /**
24
+ * `functions` is the registry `call` nodes resolve against. A node at `maxNestingDepth` or deeper is left to the interpreter, so compilation never descends further than an evaluation would. `counted` says whether every compiled node charges the call's `EvaluationBudget` before doing its own work, exactly as the interpreter does; a tree that can never exhaust its budget is compiled with it off, which removes the charge from every node.
25
+ */
26
+ constructor(functions, maxNestingDepth, counted) {
27
+ this.functions = functions;
28
+ this.maxNestingDepth = maxNestingDepth;
29
+ this.counted = counted;
30
+ }
31
+ /** In a counted tree, charges the budget for the node before running it; otherwise `run` as it is. */
32
+ charged(depth, run) {
33
+ if (!this.counted) return run;
34
+ return (context, resolvers, budget) => budget.checkNode(depth) ?? run(context, resolvers, budget);
35
+ }
36
+ /** A node left to the interpreter. The interpreter charges the budget for it and its descendants itself, so this is not wrapped in `charged`. */
37
+ interpretedPredicate(node, depth) {
38
+ this.interpreted = true;
39
+ const { functions } = this;
40
+ return (context, resolvers, budget) => runSync(evaluatePredicateInternal(node, context, resolvers, void 0, functions, NO_TREE_KEYS, 0, budget, depth));
41
+ }
42
+ interpretedValue(node, depth) {
43
+ this.interpreted = true;
44
+ const { functions } = this;
45
+ return (context, resolvers, budget) => runSync(evaluateValueInternal(node, context, resolvers, void 0, functions, NO_TREE_KEYS, 0, budget, depth));
46
+ }
47
+ predicate(node, depth) {
48
+ if (depth >= this.maxNestingDepth) return this.interpretedPredicate(node, depth);
49
+ this.nodesCompiled += 1;
50
+ const child = (operand) => this.predicate(operand, depth + 1);
51
+ const value = (operand) => this.value(operand, depth + 1, false);
52
+ switch (node.kind) {
53
+ case "not": {
54
+ const operand = child(node.operand);
55
+ return this.charged(depth, (context, resolvers, budget) => {
56
+ const result = operand(context, resolvers, budget);
57
+ return result.status === "indeterminate" ? result : definite(!result.value);
58
+ });
59
+ }
60
+ case "and": {
61
+ const left = child(node.left);
62
+ const right = child(node.right);
63
+ return this.charged(depth, (context, resolvers, budget) => {
64
+ const leftResult = left(context, resolvers, budget);
65
+ const rightResult = right(context, resolvers, budget);
66
+ return combineAnd(leftResult, rightResult);
67
+ });
68
+ }
69
+ case "or": {
70
+ const left = child(node.left);
71
+ const right = child(node.right);
72
+ return this.charged(depth, (context, resolvers, budget) => {
73
+ const leftResult = left(context, resolvers, budget);
74
+ const rightResult = right(context, resolvers, budget);
75
+ return combineOr(leftResult, rightResult);
76
+ });
77
+ }
78
+ case "allOf": {
79
+ const operands = node.operands.map(child);
80
+ return this.charged(depth, (context, resolvers, budget) => {
81
+ let combined = definite(true);
82
+ for (const operand of operands) combined = combineAnd(combined, operand(context, resolvers, budget));
83
+ return combined;
84
+ });
85
+ }
86
+ case "anyOf": {
87
+ const operands = node.operands.map(child);
88
+ return this.charged(depth, (context, resolvers, budget) => {
89
+ let combined = definite(false);
90
+ for (const operand of operands) combined = combineOr(combined, operand(context, resolvers, budget));
91
+ return combined;
92
+ });
93
+ }
94
+ case "compare": {
95
+ const left = value(node.left);
96
+ const right = value(node.right);
97
+ const { op } = node;
98
+ return this.charged(depth, (context, resolvers, budget) => {
99
+ const leftResult = left(context, resolvers, budget);
100
+ const rightResult = right(context, resolvers, budget);
101
+ if (leftResult.status === "indeterminate") return leftResult;
102
+ if (rightResult.status === "indeterminate") return rightResult;
103
+ return compareValues(op, leftResult.value, rightResult.value);
104
+ });
105
+ }
106
+ case "textCompare": {
107
+ const left = value(node.left);
108
+ const right = value(node.right);
109
+ const { op } = node;
110
+ return this.charged(depth, (context, resolvers, budget) => {
111
+ const leftResult = left(context, resolvers, budget);
112
+ const rightResult = right(context, resolvers, budget);
113
+ if (leftResult.status === "indeterminate") return leftResult;
114
+ if (rightResult.status === "indeterminate") return rightResult;
115
+ return compareText(op, leftResult.value, rightResult.value);
116
+ });
117
+ }
118
+ case "memberOf": {
119
+ const operand = value(node.operand);
120
+ const candidates = node.candidates.map(value);
121
+ const matched = node.op === "in";
122
+ return this.charged(depth, (context, resolvers, budget) => {
123
+ const operandResult = operand(context, resolvers, budget);
124
+ if (operandResult.status === "indeterminate") return operandResult;
125
+ const outcomes = candidates.map((candidate) => {
126
+ const candidateResult = candidate(context, resolvers, budget);
127
+ if (candidateResult.status === "indeterminate") return candidateResult;
128
+ return computeMembershipMatch(operandResult.value, candidateResult.value);
129
+ });
130
+ for (const outcome of outcomes) if (outcome.status === "definite" && outcome.value) return definite(matched);
131
+ const reason = firstIndeterminate(...outcomes);
132
+ if (reason !== void 0) return {
133
+ status: "indeterminate",
134
+ reason
135
+ };
136
+ return definite(!matched);
137
+ });
138
+ }
139
+ case "exists": {
140
+ const operand = value(node.operand);
141
+ return this.charged(depth, (context, resolvers, budget) => {
142
+ const result = operand(context, resolvers, budget);
143
+ if (result.status === "indeterminate") {
144
+ if (result.reason.code === "not-yet-known") return result;
145
+ if (result.reason.code === "not-found") return definite(false);
146
+ }
147
+ return definite(true);
148
+ });
149
+ }
150
+ case "some":
151
+ case "every":
152
+ case "treeReference": return this.interpretedPredicate(node, depth);
153
+ default: return this.interpretedPredicate(node, depth);
154
+ }
155
+ }
156
+ /**
157
+ * Compiles an expression node. `fresh` says whether its value will be handed to code outside this package (a registered function or a resolver): a literal is then built anew on each call, as the interpreter builds it, so no mutation by that code can reach a later call; otherwise it is built once and shared, since only this package's own operators read it.
158
+ */
159
+ value(node, depth, fresh) {
160
+ if (depth >= this.maxNestingDepth) return this.interpretedValue(node, depth);
161
+ this.nodesCompiled += 1;
162
+ const child = (operand) => this.value(operand, depth + 1, false);
163
+ const handedOver = (operand) => this.value(operand, depth + 1, true);
164
+ switch (node.kind) {
165
+ case "reference": {
166
+ const { key, unit } = node;
167
+ const keyText = JSON.stringify(key);
168
+ const notFound = `no value found for reference key ${keyText}`;
169
+ const notYetKnown = `the value for reference key ${keyText} is not yet known`;
170
+ return this.charged(depth, (context, resolvers) => {
171
+ const resolution = settleSync(resolvers.resolveValue(key, context));
172
+ if (!resolution.found) return absenceEvaluation(resolution, notFound, notYetKnown);
173
+ const unitError = checkReferenceUnit(unit, resolution.value);
174
+ if (unitError !== void 0) return unitError;
175
+ return definite(resolution.value);
176
+ });
177
+ }
178
+ case "accumulator": return this.charged(depth, () => indeterminate("wrong-type", ACCUMULATOR_OUTSIDE_FOLD));
179
+ case "call": {
180
+ const args = node.args.map(handedOver);
181
+ const { fn } = node;
182
+ const { functions } = this;
183
+ return this.charged(depth, (context, resolvers, budget) => {
184
+ const results = args.map((arg) => arg(context, resolvers, budget));
185
+ const values = [];
186
+ for (const result of results) {
187
+ if (result.status === "indeterminate") return result;
188
+ values.push(result.value);
189
+ }
190
+ return invokeRegisteredFunction(functions, fn, values);
191
+ });
192
+ }
193
+ case "numberLiteral":
194
+ case "textLiteral":
195
+ case "booleanLiteral":
196
+ case "instantLiteral":
197
+ case "durationLiteral":
198
+ case "complexLiteral": {
199
+ if (fresh) return this.charged(depth, () => definite(literalValue(node)));
200
+ const shared = definite(literalValue(node));
201
+ return this.charged(depth, () => shared);
202
+ }
203
+ case "arithmetic": {
204
+ const left = child(node.left);
205
+ const right = child(node.right);
206
+ const { op } = node;
207
+ return this.charged(depth, (context, resolvers, budget) => {
208
+ const leftResult = left(context, resolvers, budget);
209
+ const rightResult = right(context, resolvers, budget);
210
+ if (leftResult.status === "indeterminate") return leftResult;
211
+ if (rightResult.status === "indeterminate") return rightResult;
212
+ return applyArithmetic(op, leftResult.value, rightResult.value);
213
+ });
214
+ }
215
+ case "negate": {
216
+ const operand = child(node.operand);
217
+ return this.charged(depth, (context, resolvers, budget) => {
218
+ const result = operand(context, resolvers, budget);
219
+ if (result.status === "indeterminate") return result;
220
+ return applyNegate(result.value);
221
+ });
222
+ }
223
+ case "lookup": {
224
+ const keys = node.keys.map(handedOver);
225
+ const { table } = node;
226
+ const tableText = JSON.stringify(table);
227
+ const notFound = `no match found in lookup table ${tableText}`;
228
+ const notYetKnown = `the match in lookup table ${tableText} is not yet known`;
229
+ return this.charged(depth, (context, resolvers, budget) => {
230
+ const results = keys.map((key) => key(context, resolvers, budget));
231
+ const keyValues = [];
232
+ for (const result of results) {
233
+ if (result.status === "indeterminate") return result;
234
+ keyValues.push(result.value);
235
+ }
236
+ const resolution = settleSync(resolvers.resolveLookup(table, keyValues, context));
237
+ if (!resolution.found) return absenceEvaluation(resolution, notFound, notYetKnown);
238
+ return definite(resolution.value);
239
+ });
240
+ }
241
+ case "conditional": {
242
+ const fallback = child(node.fallback);
243
+ const cases = node.cases.map(({ when, then }) => ({
244
+ when: this.predicate(when, depth + 1),
245
+ then: child(then)
246
+ }));
247
+ if ((node.hitPolicy ?? "first") === "first") return this.charged(depth, (context, resolvers, budget) => {
248
+ for (const { when, then } of cases) {
249
+ const whenResult = when(context, resolvers, budget);
250
+ if (whenResult.status === "indeterminate") return whenResult;
251
+ if (whenResult.value) return then(context, resolvers, budget);
252
+ }
253
+ return fallback(context, resolvers, budget);
254
+ });
255
+ return this.charged(depth, (context, resolvers, budget) => {
256
+ const whenResults = cases.map(({ when }) => when(context, resolvers, budget));
257
+ let matches = 0;
258
+ let matched;
259
+ cases.forEach(({ then }, index) => {
260
+ const whenResult = whenResults[index];
261
+ if (whenResult?.status === "definite" && whenResult.value) {
262
+ matches += 1;
263
+ matched = then;
264
+ }
265
+ });
266
+ if (matches >= 2) return indeterminate("domain-error", "more than one case matched under the 'unique' hit policy");
267
+ const reason = firstIndeterminate(...whenResults);
268
+ if (reason !== void 0) return {
269
+ status: "indeterminate",
270
+ reason
271
+ };
272
+ return (matched ?? fallback)(context, resolvers, budget);
273
+ });
274
+ }
275
+ case "delegate": {
276
+ const { system, payload } = node;
277
+ const notFound = `delegate handler for external system '${system}' reported no value`;
278
+ const notYetKnown = `the value from the delegate handler for external system '${system}' is not yet known`;
279
+ const noHandler = `no delegate handler registered for external system '${system}'`;
280
+ return this.charged(depth, (context, resolvers) => {
281
+ if (resolvers.resolveDelegate === void 0) return indeterminate("wrong-type", noHandler);
282
+ const resolution = settleSync(resolvers.resolveDelegate(system, payload, context));
283
+ if (!resolution.found) return absenceEvaluation(resolution, notFound, notYetKnown);
284
+ return definite(resolution.value);
285
+ });
286
+ }
287
+ case "fold":
288
+ case "treeReference": return this.interpretedValue(node, depth);
289
+ default: return this.interpretedValue(node, depth);
290
+ }
291
+ }
292
+ };
293
+ /**
294
+ * Compiles `node` to a function that evaluates it over the resolvers of each call, with the same `Evaluation` as the synchronous interpreter: status, value, and the reason an indeterminate result carries, including which of several candidate reasons it is. The tree is walked once here, so each call does no per-node dispatch, builds no generator or suspension objects, and precomputes everything that depends only on the tree (the messages of absence, the literal values).
295
+ *
296
+ * Compiled natively: `not`, `and`, `or`, `allOf`, `anyOf`, `compare`, `textCompare`, `memberOf` and `exists`, and the expression kinds `reference`, `accumulator`, every literal, `arithmetic`, `negate`, `call`, `lookup`, `conditional` (both hit policies) and `delegate`. Left to the interpreter, as a subtree evaluated by `evaluatePredicateInternal` or `evaluateValueInternal` with the same budget: `some`, `every`, `fold` and `treeReference`, since each re-evaluates a subtree per item or per resolved tree. A tree containing any of them, or more nodes than `maxNodes`, charges the budget at every node exactly as the interpreter does, so a limit is met at the same node; any other tree cannot exhaust either limit and skips the charge.
297
+ *
298
+ * Operands are evaluated in the interpreter's synchronous order, and every operand is evaluated even when an earlier one has settled the result, so the resolvers are called in the same sequence as `evaluatePredicate` calls them. A resolver that returns a promise makes the call throw, as it does there.
299
+ *
300
+ * Precondition: `node` is a well-formed `PredicateNode` and is not mutated after this call, since what the tree fixes is read here once. Errors: none at compile time; a call throws exactly when the interpreter would (a resolver or registered function that throws, a promise from a resolver).
301
+ */
302
+ function compilePredicateTree(node, functions, maxNodes, maxNestingDepth) {
303
+ const uncounted = new NodeCompiler(functions, maxNestingDepth, false);
304
+ const uncountedRun = uncounted.predicate(node, 0);
305
+ if (!uncounted.interpreted && uncounted.nodesCompiled <= maxNodes) return (context, resolvers) => uncountedRun(context, resolvers, UNLIMITED_BUDGET);
306
+ const countedRun = new NodeCompiler(functions, maxNestingDepth, true).predicate(node, 0);
307
+ return (context, resolvers) => countedRun(context, resolvers, createEvaluationBudget(maxNodes, maxNestingDepth));
308
+ }
309
+ //#endregion
310
+ export { compilePredicateTree };
@@ -21,6 +21,13 @@ function* all(tasks) {
21
21
  function isThenable(value) {
22
22
  return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
23
23
  }
24
+ /**
25
+ * The synchronous driver's rule for a resolver result: it is returned as it is, or, when it is a promise, the evaluation throws a `TypeError`, since a synchronous evaluator has nothing to do with one. Shared by `runSync` and by every other synchronous evaluation path, so they cannot disagree about what a promise from a resolver means.
26
+ */
27
+ function settleSync(value) {
28
+ if (isThenable(value)) throw new TypeError("a resolver supplied to a synchronous evaluator returned a promise; use the asynchronous evaluator for asynchronous resolvers");
29
+ return value;
30
+ }
24
31
  /** Runs a task to completion, awaiting each suspension and running an `all` group's tasks concurrently. */
25
32
  async function runAsync(task) {
26
33
  let step = task.next();
@@ -32,17 +39,13 @@ async function runAsync(task) {
32
39
  return step.value;
33
40
  }
34
41
  /**
35
- * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws, since a synchronous evaluator has nothing to do with one.
42
+ * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws (see `settleSync`).
36
43
  */
37
44
  function runSync(task) {
38
45
  let step = task.next();
39
46
  while (step.done !== true) {
40
47
  const suspension = step.value;
41
- let result;
42
- if (suspension.kind === "wait") {
43
- if (isThenable(suspension.value)) throw new TypeError("a resolver supplied to a synchronous evaluator returned a promise; use the asynchronous evaluator for asynchronous resolvers");
44
- result = suspension.value;
45
- } else result = suspension.tasks.map(runSync);
48
+ const result = suspension.kind === "wait" ? settleSync(suspension.value) : suspension.tasks.map(runSync);
46
49
  step = task.next(result);
47
50
  }
48
51
  return step.value;
@@ -51,4 +54,5 @@ function runSync(task) {
51
54
  exports.all = all;
52
55
  exports.runAsync = runAsync;
53
56
  exports.runSync = runSync;
57
+ exports.settleSync = settleSync;
54
58
  exports.wait = wait;
@@ -28,10 +28,14 @@ export declare function wait<T>(value: Awaitable<T>): Generator<Suspension, T, T
28
28
  * Suspends until every task has finished and returns their results in order. The tasks are independent of one another by construction, so a driver may interleave them (async) or run them in sequence (sync).
29
29
  */
30
30
  export declare function all<const T extends readonly Runnable<unknown>[]>(tasks: T): Generator<Suspension, TaskResults<T>, TaskResults<T>>;
31
+ /**
32
+ * The synchronous driver's rule for a resolver result: it is returned as it is, or, when it is a promise, the evaluation throws a `TypeError`, since a synchronous evaluator has nothing to do with one. Shared by `runSync` and by every other synchronous evaluation path, so they cannot disagree about what a promise from a resolver means.
33
+ */
34
+ export declare function settleSync<T>(value: T): T;
31
35
  /** Runs a task to completion, awaiting each suspension and running an `all` group's tasks concurrently. */
32
36
  export declare function runAsync<T>(task: Readonly<Runnable<T>>): Promise<T>;
33
37
  /**
34
- * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws, since a synchronous evaluator has nothing to do with one.
38
+ * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws (see `settleSync`).
35
39
  */
36
40
  export declare function runSync<T>(task: Readonly<Runnable<T>>): T;
37
41
  //#endregion
@@ -28,10 +28,14 @@ export declare function wait<T>(value: Awaitable<T>): Generator<Suspension, T, T
28
28
  * Suspends until every task has finished and returns their results in order. The tasks are independent of one another by construction, so a driver may interleave them (async) or run them in sequence (sync).
29
29
  */
30
30
  export declare function all<const T extends readonly Runnable<unknown>[]>(tasks: T): Generator<Suspension, TaskResults<T>, TaskResults<T>>;
31
+ /**
32
+ * The synchronous driver's rule for a resolver result: it is returned as it is, or, when it is a promise, the evaluation throws a `TypeError`, since a synchronous evaluator has nothing to do with one. Shared by `runSync` and by every other synchronous evaluation path, so they cannot disagree about what a promise from a resolver means.
33
+ */
34
+ export declare function settleSync<T>(value: T): T;
31
35
  /** Runs a task to completion, awaiting each suspension and running an `all` group's tasks concurrently. */
32
36
  export declare function runAsync<T>(task: Readonly<Runnable<T>>): Promise<T>;
33
37
  /**
34
- * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws, since a synchronous evaluator has nothing to do with one.
38
+ * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws (see `settleSync`).
35
39
  */
36
40
  export declare function runSync<T>(task: Readonly<Runnable<T>>): T;
37
41
  //#endregion
@@ -20,6 +20,13 @@ function* all(tasks) {
20
20
  function isThenable(value) {
21
21
  return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
22
22
  }
23
+ /**
24
+ * The synchronous driver's rule for a resolver result: it is returned as it is, or, when it is a promise, the evaluation throws a `TypeError`, since a synchronous evaluator has nothing to do with one. Shared by `runSync` and by every other synchronous evaluation path, so they cannot disagree about what a promise from a resolver means.
25
+ */
26
+ function settleSync(value) {
27
+ if (isThenable(value)) throw new TypeError("a resolver supplied to a synchronous evaluator returned a promise; use the asynchronous evaluator for asynchronous resolvers");
28
+ return value;
29
+ }
23
30
  /** Runs a task to completion, awaiting each suspension and running an `all` group's tasks concurrently. */
24
31
  async function runAsync(task) {
25
32
  let step = task.next();
@@ -31,20 +38,16 @@ async function runAsync(task) {
31
38
  return step.value;
32
39
  }
33
40
  /**
34
- * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws, since a synchronous evaluator has nothing to do with one.
41
+ * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws (see `settleSync`).
35
42
  */
36
43
  function runSync(task) {
37
44
  let step = task.next();
38
45
  while (step.done !== true) {
39
46
  const suspension = step.value;
40
- let result;
41
- if (suspension.kind === "wait") {
42
- if (isThenable(suspension.value)) throw new TypeError("a resolver supplied to a synchronous evaluator returned a promise; use the asynchronous evaluator for asynchronous resolvers");
43
- result = suspension.value;
44
- } else result = suspension.tasks.map(runSync);
47
+ const result = suspension.kind === "wait" ? settleSync(suspension.value) : suspension.tasks.map(runSync);
45
48
  step = task.next(result);
46
49
  }
47
50
  return step.value;
48
51
  }
49
52
  //#endregion
50
- export { all, runAsync, runSync, wait };
53
+ export { all, runAsync, runSync, settleSync, wait };
@@ -2,6 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_evaluator_budget = require("./evaluator-budget.cjs");
3
3
  const require_evaluator_effect = require("./evaluator-effect.cjs");
4
4
  const require_evaluator = require("./evaluator.cjs");
5
+ const require_evaluator_compiled = require("./evaluator-compiled.cjs");
5
6
  const require_functions = require("./functions.cjs");
6
7
  //#region src/evaluator-factory.ts
7
8
  /**
@@ -19,7 +20,8 @@ function createEvaluator({ functions = require_functions.emptyFunctionRegistry,
19
20
  function createSyncEvaluator({ functions = require_functions.emptyFunctionRegistry, maxNodes = require_evaluator_budget.DEFAULT_MAX_EVALUATION_NODES, maxNestingDepth = 500 }) {
20
21
  return {
21
22
  evaluatePredicate: (node, context, resolvers) => require_evaluator_effect.runSync(require_evaluator.evaluatePredicateInternal(node, context, resolvers, void 0, functions, /* @__PURE__ */ new Set(), 0, require_evaluator_budget.createEvaluationBudget(maxNodes, maxNestingDepth), 0)),
22
- evaluateValue: (node, context, resolvers) => require_evaluator_effect.runSync(require_evaluator.evaluateValueInternal(node, context, resolvers, void 0, functions, /* @__PURE__ */ new Set(), 0, require_evaluator_budget.createEvaluationBudget(maxNodes, maxNestingDepth), 0))
23
+ evaluateValue: (node, context, resolvers) => require_evaluator_effect.runSync(require_evaluator.evaluateValueInternal(node, context, resolvers, void 0, functions, /* @__PURE__ */ new Set(), 0, require_evaluator_budget.createEvaluationBudget(maxNodes, maxNestingDepth), 0)),
24
+ compilePredicate: (node) => require_evaluator_compiled.compilePredicateTree(node, functions, maxNodes, maxNestingDepth)
23
25
  };
24
26
  }
25
27
  const { evaluatePredicate, evaluateValue } = createEvaluator({});
@@ -3,6 +3,7 @@ import { FunctionRegistry } from "./functions.cjs";
3
3
  import { ExpressionNode, PredicateNode } from "./tree.cjs";
4
4
  import { Evaluation } from "./evaluation.cjs";
5
5
  import { EvaluationContext, Resolvers, SyncResolvers } from "./resolvers.cjs";
6
+ import { CompiledPredicate } from "./evaluator-compiled.cjs";
6
7
  //#region src/evaluator-factory.d.ts
7
8
  /**
8
9
  * Builds a bound `{ evaluatePredicate, evaluateValue }` pair over a caller-supplied function registry for `call` nodes -- the registry is bound once, at construction time, unlike `resolvers`, which are supplied fresh to every call. `maxNodes` and `maxNestingDepth` configure that same pair's own `EvaluationBudget` (see its doc comment in evaluator-budget.ts), created fresh for every top-level `evaluatePredicate`/`evaluateValue` invocation so budgets never leak between unrelated calls. The bare module-level `evaluatePredicate`/`evaluateValue` exports below are `createEvaluator({})`'s output, so both caps default to `DEFAULT_MAX_EVALUATION_NODES`/`DEFAULT_MAX_NESTING_DEPTH` for every caller that does not explicitly configure them.
@@ -28,6 +29,8 @@ export declare function createEvaluator({ functions, maxNodes, maxNestingDepth }
28
29
  export declare function createSyncEvaluator({ functions, maxNodes, maxNestingDepth }: EvaluatorOptions): {
29
30
  evaluatePredicate: (node: PredicateNode, context: EvaluationContext, resolvers: Readonly<SyncResolvers>) => Evaluation<boolean>;
30
31
  evaluateValue: (node: ExpressionNode, context: EvaluationContext, resolvers: Readonly<SyncResolvers>) => Evaluation<ComputedValue>;
32
+ /** Prepares `node` for repeated evaluation under changing resolvers; see `CompiledPredicate` and `compilePredicateTree`. The function it returns gives the result `evaluatePredicate` gives for the same node, context and resolvers, without walking the tree afresh on each call, and is bound to this evaluator's functions and limits. */
33
+ compilePredicate: (node: PredicateNode) => CompiledPredicate;
31
34
  };
32
35
  export declare const evaluatePredicate: (node: PredicateNode, context: EvaluationContext, resolvers: Readonly<Resolvers>) => Promise<Evaluation<boolean>>, evaluateValue: (node: ExpressionNode, context: EvaluationContext, resolvers: Readonly<Resolvers>) => Promise<Evaluation<ComputedValue>>;
33
36
  //#endregion
@@ -3,6 +3,7 @@ import { FunctionRegistry } from "./functions.js";
3
3
  import { ExpressionNode, PredicateNode } from "./tree.js";
4
4
  import { Evaluation } from "./evaluation.js";
5
5
  import { EvaluationContext, Resolvers, SyncResolvers } from "./resolvers.js";
6
+ import { CompiledPredicate } from "./evaluator-compiled.js";
6
7
  //#region src/evaluator-factory.d.ts
7
8
  /**
8
9
  * Builds a bound `{ evaluatePredicate, evaluateValue }` pair over a caller-supplied function registry for `call` nodes -- the registry is bound once, at construction time, unlike `resolvers`, which are supplied fresh to every call. `maxNodes` and `maxNestingDepth` configure that same pair's own `EvaluationBudget` (see its doc comment in evaluator-budget.ts), created fresh for every top-level `evaluatePredicate`/`evaluateValue` invocation so budgets never leak between unrelated calls. The bare module-level `evaluatePredicate`/`evaluateValue` exports below are `createEvaluator({})`'s output, so both caps default to `DEFAULT_MAX_EVALUATION_NODES`/`DEFAULT_MAX_NESTING_DEPTH` for every caller that does not explicitly configure them.
@@ -28,6 +29,8 @@ export declare function createEvaluator({ functions, maxNodes, maxNestingDepth }
28
29
  export declare function createSyncEvaluator({ functions, maxNodes, maxNestingDepth }: EvaluatorOptions): {
29
30
  evaluatePredicate: (node: PredicateNode, context: EvaluationContext, resolvers: Readonly<SyncResolvers>) => Evaluation<boolean>;
30
31
  evaluateValue: (node: ExpressionNode, context: EvaluationContext, resolvers: Readonly<SyncResolvers>) => Evaluation<ComputedValue>;
32
+ /** Prepares `node` for repeated evaluation under changing resolvers; see `CompiledPredicate` and `compilePredicateTree`. The function it returns gives the result `evaluatePredicate` gives for the same node, context and resolvers, without walking the tree afresh on each call, and is bound to this evaluator's functions and limits. */
33
+ compilePredicate: (node: PredicateNode) => CompiledPredicate;
31
34
  };
32
35
  export declare const evaluatePredicate: (node: PredicateNode, context: EvaluationContext, resolvers: Readonly<Resolvers>) => Promise<Evaluation<boolean>>, evaluateValue: (node: ExpressionNode, context: EvaluationContext, resolvers: Readonly<Resolvers>) => Promise<Evaluation<ComputedValue>>;
33
36
  //#endregion
@@ -1,6 +1,7 @@
1
1
  import { DEFAULT_MAX_EVALUATION_NODES, createEvaluationBudget } from "./evaluator-budget.js";
2
2
  import { runAsync, runSync } from "./evaluator-effect.js";
3
3
  import { evaluatePredicateInternal, evaluateValueInternal } from "./evaluator.js";
4
+ import { compilePredicateTree } from "./evaluator-compiled.js";
4
5
  import { emptyFunctionRegistry } from "./functions.js";
5
6
  //#region src/evaluator-factory.ts
6
7
  /**
@@ -18,7 +19,8 @@ function createEvaluator({ functions = emptyFunctionRegistry, maxNodes = DEFAULT
18
19
  function createSyncEvaluator({ functions = emptyFunctionRegistry, maxNodes = DEFAULT_MAX_EVALUATION_NODES, maxNestingDepth = 500 }) {
19
20
  return {
20
21
  evaluatePredicate: (node, context, resolvers) => runSync(evaluatePredicateInternal(node, context, resolvers, void 0, functions, /* @__PURE__ */ new Set(), 0, createEvaluationBudget(maxNodes, maxNestingDepth), 0)),
21
- evaluateValue: (node, context, resolvers) => runSync(evaluateValueInternal(node, context, resolvers, void 0, functions, /* @__PURE__ */ new Set(), 0, createEvaluationBudget(maxNodes, maxNestingDepth), 0))
22
+ evaluateValue: (node, context, resolvers) => runSync(evaluateValueInternal(node, context, resolvers, void 0, functions, /* @__PURE__ */ new Set(), 0, createEvaluationBudget(maxNodes, maxNestingDepth), 0)),
23
+ compilePredicate: (node) => compilePredicateTree(node, functions, maxNodes, maxNestingDepth)
22
24
  };
23
25
  }
24
26
  const { evaluatePredicate, evaluateValue } = createEvaluator({});
@@ -0,0 +1,43 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_complex = require("./complex.cjs");
3
+ //#region src/evaluator-literals.ts
4
+ /**
5
+ * The `ComputedValue` a literal node denotes, as a new object on every call. Whichever authoring form a `complexLiteral` used (see the "Complex values" section of README.md), it is normalised here to the single rectangular `ComputedValue`, so nothing downstream (arithmetic, compare, memberOf, negate) ever sees a polar-authored value. Shared by the interpreter and the compiled path so a literal means one thing in both.
6
+ */
7
+ function literalValue(node) {
8
+ switch (node.kind) {
9
+ case "numberLiteral": return {
10
+ kind: "number",
11
+ value: node.value,
12
+ unit: node.unit
13
+ };
14
+ case "textLiteral": return {
15
+ kind: "text",
16
+ value: node.value
17
+ };
18
+ case "booleanLiteral": return {
19
+ kind: "boolean",
20
+ value: node.value
21
+ };
22
+ case "instantLiteral": return {
23
+ kind: "instant",
24
+ value: node.value
25
+ };
26
+ case "durationLiteral": return {
27
+ kind: "duration",
28
+ value: node.value,
29
+ unit: node.unit
30
+ };
31
+ case "complexLiteral":
32
+ if ("re" in node) return {
33
+ kind: "complex",
34
+ re: node.re,
35
+ im: node.im,
36
+ unit: node.unit
37
+ };
38
+ return require_complex.complexFromPolar(node.magnitude, node.phase, node.unit);
39
+ default: throw new Error("unreachable literal node kind");
40
+ }
41
+ }
42
+ //#endregion
43
+ exports.literalValue = literalValue;
@@ -0,0 +1,10 @@
1
+ import { t as ComputedValue } from "./computed-value-CbRF0Izc.cjs";
2
+ import { BooleanLiteralNode, ComplexLiteralNode, DurationLiteralNode, InstantLiteralNode, NumberLiteralNode, TextLiteralNode } from "./tree.cjs";
3
+ //#region src/evaluator-literals.d.ts
4
+ /** Every expression node kind that is a constant: it evaluates to a value without consulting a resolver or any other node. */
5
+ export type LiteralNode = NumberLiteralNode | TextLiteralNode | BooleanLiteralNode | InstantLiteralNode | DurationLiteralNode | ComplexLiteralNode;
6
+ /**
7
+ * The `ComputedValue` a literal node denotes, as a new object on every call. Whichever authoring form a `complexLiteral` used (see the "Complex values" section of README.md), it is normalised here to the single rectangular `ComputedValue`, so nothing downstream (arithmetic, compare, memberOf, negate) ever sees a polar-authored value. Shared by the interpreter and the compiled path so a literal means one thing in both.
8
+ */
9
+ export declare function literalValue(node: Readonly<LiteralNode>): ComputedValue;
10
+ //#endregion
@@ -0,0 +1,10 @@
1
+ import { t as ComputedValue } from "./computed-value-CbRF0Izc.js";
2
+ import { BooleanLiteralNode, ComplexLiteralNode, DurationLiteralNode, InstantLiteralNode, NumberLiteralNode, TextLiteralNode } from "./tree.js";
3
+ //#region src/evaluator-literals.d.ts
4
+ /** Every expression node kind that is a constant: it evaluates to a value without consulting a resolver or any other node. */
5
+ export type LiteralNode = NumberLiteralNode | TextLiteralNode | BooleanLiteralNode | InstantLiteralNode | DurationLiteralNode | ComplexLiteralNode;
6
+ /**
7
+ * The `ComputedValue` a literal node denotes, as a new object on every call. Whichever authoring form a `complexLiteral` used (see the "Complex values" section of README.md), it is normalised here to the single rectangular `ComputedValue`, so nothing downstream (arithmetic, compare, memberOf, negate) ever sees a polar-authored value. Shared by the interpreter and the compiled path so a literal means one thing in both.
8
+ */
9
+ export declare function literalValue(node: Readonly<LiteralNode>): ComputedValue;
10
+ //#endregion
@@ -0,0 +1,42 @@
1
+ import { complexFromPolar } from "./complex.js";
2
+ //#region src/evaluator-literals.ts
3
+ /**
4
+ * The `ComputedValue` a literal node denotes, as a new object on every call. Whichever authoring form a `complexLiteral` used (see the "Complex values" section of README.md), it is normalised here to the single rectangular `ComputedValue`, so nothing downstream (arithmetic, compare, memberOf, negate) ever sees a polar-authored value. Shared by the interpreter and the compiled path so a literal means one thing in both.
5
+ */
6
+ function literalValue(node) {
7
+ switch (node.kind) {
8
+ case "numberLiteral": return {
9
+ kind: "number",
10
+ value: node.value,
11
+ unit: node.unit
12
+ };
13
+ case "textLiteral": return {
14
+ kind: "text",
15
+ value: node.value
16
+ };
17
+ case "booleanLiteral": return {
18
+ kind: "boolean",
19
+ value: node.value
20
+ };
21
+ case "instantLiteral": return {
22
+ kind: "instant",
23
+ value: node.value
24
+ };
25
+ case "durationLiteral": return {
26
+ kind: "duration",
27
+ value: node.value,
28
+ unit: node.unit
29
+ };
30
+ case "complexLiteral":
31
+ if ("re" in node) return {
32
+ kind: "complex",
33
+ re: node.re,
34
+ im: node.im,
35
+ unit: node.unit
36
+ };
37
+ return complexFromPolar(node.magnitude, node.phase, node.unit);
38
+ default: throw new Error("unreachable literal node kind");
39
+ }
40
+ }
41
+ //#endregion
42
+ export { literalValue };