trilean 1.13.12 → 2.0.1
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 +1 -1
- package/dist/evaluator-operations.js +24 -4
- package/dist-cli/trilean.mjs +25 -5
- package/package.json +10 -20
- package/schemas/trilean.schema.json +1 -1
- package/dist/builtin-functions.cjs +0 -242
- package/dist/builtin-functions.d.cts +0 -13
- package/dist/complex-division.cjs +0 -47
- package/dist/complex-division.d.cts +0 -23
- package/dist/complex.cjs +0 -35
- package/dist/complex.d.cts +0 -23
- package/dist/computed-value.cjs +0 -80
- package/dist/computed-value.d.cts +0 -48
- package/dist/derived-aggregates.cjs +0 -73
- package/dist/derived-aggregates.d.cts +0 -9
- package/dist/derived-connectives.cjs +0 -39
- package/dist/derived-connectives.d.cts +0 -14
- package/dist/derived-patterns.cjs +0 -93
- package/dist/derived-patterns.d.cts +0 -4
- package/dist/derived-values.cjs +0 -23
- package/dist/derived-values.d.cts +0 -7
- package/dist/dist-ed78Tuwe.js +0 -1612
- package/dist/dist-ySX7cb5d.cjs +0 -1612
- package/dist/duration-order.cjs +0 -83
- package/dist/duration-order.d.cts +0 -21
- package/dist/evaluate-document.cjs +0 -92
- package/dist/evaluate-document.d.cts +0 -42
- package/dist/evaluation.cjs +0 -47
- package/dist/evaluation.d.cts +0 -29
- package/dist/evaluator-budget.cjs +0 -37
- package/dist/evaluator-budget.d.cts +0 -22
- package/dist/evaluator-collection.cjs +0 -33
- package/dist/evaluator-collection.d.cts +0 -21
- package/dist/evaluator-compiled.cjs +0 -310
- package/dist/evaluator-compiled.d.cts +0 -18
- package/dist/evaluator-effect.cjs +0 -57
- package/dist/evaluator-effect.d.cts +0 -40
- package/dist/evaluator-factory.cjs +0 -31
- package/dist/evaluator-factory.d.cts +0 -34
- package/dist/evaluator-literals.cjs +0 -42
- package/dist/evaluator-literals.d.cts +0 -8
- package/dist/evaluator-operations.cjs +0 -440
- package/dist/evaluator-operations.d.cts +0 -27
- package/dist/evaluator-test-helpers.cjs +0 -15030
- package/dist/evaluator-test-helpers.d.cts +0 -10
- package/dist/evaluator-test-helpers.d.ts +0 -10
- package/dist/evaluator-test-helpers.js +0 -14902
- package/dist/evaluator.cjs +0 -228
- package/dist/evaluator.d.cts +0 -12
- package/dist/functions.cjs +0 -4
- package/dist/functions.d.cts +0 -12
- package/dist/index.cjs +0 -97
- package/dist/index.d.cts +0 -18
- package/dist/input-resolvers.cjs +0 -109
- package/dist/input-resolvers.d.cts +0 -22
- package/dist/integer-power.cjs +0 -19
- package/dist/integer-power.d.cts +0 -11
- package/dist/json-value.cjs +0 -12
- package/dist/json-value.d.cts +0 -8
- package/dist/plain-record.cjs +0 -7
- package/dist/plain-record.d.cts +0 -2
- package/dist/resolvers.cjs +0 -0
- package/dist/resolvers.d.cts +0 -54
- package/dist/tree.cjs +0 -332
- package/dist/tree.d.cts +0 -446
|
@@ -1,310 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_evaluation = require("./evaluation.cjs");
|
|
3
|
-
const require_evaluator_budget = require("./evaluator-budget.cjs");
|
|
4
|
-
const require_evaluator = require("./evaluator.cjs");
|
|
5
|
-
const require_evaluator_effect = require("./evaluator-effect.cjs");
|
|
6
|
-
const require_evaluator_literals = require("./evaluator-literals.cjs");
|
|
7
|
-
const require_evaluator_operations = require("./evaluator-operations.cjs");
|
|
8
|
-
// src/evaluator-compiled.ts
|
|
9
|
-
/** Never charged: handed to the nodes of a tree compiled without budget counting, none of which reads it. */
|
|
10
|
-
const UNLIMITED_BUDGET = { checkNode: () => void 0 };
|
|
11
|
-
/** 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. */
|
|
12
|
-
const NO_TREE_KEYS = /* @__PURE__ */ new Set();
|
|
13
|
-
/** The message for the `accumulator` node outside a fold's `combine`, which is everywhere a compiled node can sit. */
|
|
14
|
-
const ACCUMULATOR_OUTSIDE_FOLD = "accumulator used outside a reduce fold's combine expression";
|
|
15
|
-
/** Compiles the nodes of one tree, for one setting of `counted`. */
|
|
16
|
-
var NodeCompiler = class {
|
|
17
|
-
functions;
|
|
18
|
-
maxNestingDepth;
|
|
19
|
-
counted;
|
|
20
|
-
/** 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. */
|
|
21
|
-
nodesCompiled = 0;
|
|
22
|
-
/** Whether any node was left to the interpreter. */
|
|
23
|
-
interpreted = false;
|
|
24
|
-
/**
|
|
25
|
-
* `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.
|
|
26
|
-
*/
|
|
27
|
-
constructor(functions, maxNestingDepth, counted) {
|
|
28
|
-
this.functions = functions;
|
|
29
|
-
this.maxNestingDepth = maxNestingDepth;
|
|
30
|
-
this.counted = counted;
|
|
31
|
-
}
|
|
32
|
-
/** In a counted tree, charges the budget for the node before running it; otherwise `run` as it is. */
|
|
33
|
-
charged(depth, run) {
|
|
34
|
-
if (!this.counted) return run;
|
|
35
|
-
return (context, resolvers, budget) => budget.checkNode(depth) ?? run(context, resolvers, budget);
|
|
36
|
-
}
|
|
37
|
-
/** A node left to the interpreter. The interpreter charges the budget for it and its descendants itself, so this is not wrapped in `charged`. */
|
|
38
|
-
interpretedPredicate(node, depth) {
|
|
39
|
-
this.interpreted = true;
|
|
40
|
-
const { functions } = this;
|
|
41
|
-
return (context, resolvers, budget) => require_evaluator_effect.runSync(require_evaluator.evaluatePredicateInternal(node, context, resolvers, void 0, functions, NO_TREE_KEYS, 0, budget, depth));
|
|
42
|
-
}
|
|
43
|
-
interpretedValue(node, depth) {
|
|
44
|
-
this.interpreted = true;
|
|
45
|
-
const { functions } = this;
|
|
46
|
-
return (context, resolvers, budget) => require_evaluator_effect.runSync(require_evaluator.evaluateValueInternal(node, context, resolvers, void 0, functions, NO_TREE_KEYS, 0, budget, depth));
|
|
47
|
-
}
|
|
48
|
-
predicate(node, depth) {
|
|
49
|
-
if (depth >= this.maxNestingDepth) return this.interpretedPredicate(node, depth);
|
|
50
|
-
this.nodesCompiled += 1;
|
|
51
|
-
const child = (operand) => this.predicate(operand, depth + 1);
|
|
52
|
-
const value = (operand) => this.value(operand, depth + 1, false);
|
|
53
|
-
switch (node.kind) {
|
|
54
|
-
case "not": {
|
|
55
|
-
const operand = child(node.operand);
|
|
56
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
57
|
-
const result = operand(context, resolvers, budget);
|
|
58
|
-
return result.status === "indeterminate" ? result : require_evaluation.definite(!result.value);
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
case "and": {
|
|
62
|
-
const left = child(node.left);
|
|
63
|
-
const right = child(node.right);
|
|
64
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
65
|
-
const leftResult = left(context, resolvers, budget);
|
|
66
|
-
const rightResult = right(context, resolvers, budget);
|
|
67
|
-
return require_evaluator_operations.combineAnd(leftResult, rightResult);
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
case "or": {
|
|
71
|
-
const left = child(node.left);
|
|
72
|
-
const right = child(node.right);
|
|
73
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
74
|
-
const leftResult = left(context, resolvers, budget);
|
|
75
|
-
const rightResult = right(context, resolvers, budget);
|
|
76
|
-
return require_evaluator_operations.combineOr(leftResult, rightResult);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
case "allOf": {
|
|
80
|
-
const operands = node.operands.map(child);
|
|
81
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
82
|
-
let combined = require_evaluation.definite(true);
|
|
83
|
-
for (const operand of operands) combined = require_evaluator_operations.combineAnd(combined, operand(context, resolvers, budget));
|
|
84
|
-
return combined;
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
case "anyOf": {
|
|
88
|
-
const operands = node.operands.map(child);
|
|
89
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
90
|
-
let combined = require_evaluation.definite(false);
|
|
91
|
-
for (const operand of operands) combined = require_evaluator_operations.combineOr(combined, operand(context, resolvers, budget));
|
|
92
|
-
return combined;
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
case "compare": {
|
|
96
|
-
const left = value(node.left);
|
|
97
|
-
const right = value(node.right);
|
|
98
|
-
const { op } = node;
|
|
99
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
100
|
-
const leftResult = left(context, resolvers, budget);
|
|
101
|
-
const rightResult = right(context, resolvers, budget);
|
|
102
|
-
if (leftResult.status === "indeterminate") return leftResult;
|
|
103
|
-
if (rightResult.status === "indeterminate") return rightResult;
|
|
104
|
-
return require_evaluator_operations.compareValues(op, leftResult.value, rightResult.value);
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
case "textCompare": {
|
|
108
|
-
const left = value(node.left);
|
|
109
|
-
const right = value(node.right);
|
|
110
|
-
const { op } = node;
|
|
111
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
112
|
-
const leftResult = left(context, resolvers, budget);
|
|
113
|
-
const rightResult = right(context, resolvers, budget);
|
|
114
|
-
if (leftResult.status === "indeterminate") return leftResult;
|
|
115
|
-
if (rightResult.status === "indeterminate") return rightResult;
|
|
116
|
-
return require_evaluator_operations.compareText(op, leftResult.value, rightResult.value);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
case "memberOf": {
|
|
120
|
-
const operand = value(node.operand);
|
|
121
|
-
const candidates = node.candidates.map(value);
|
|
122
|
-
const matched = node.op === "in";
|
|
123
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
124
|
-
const operandResult = operand(context, resolvers, budget);
|
|
125
|
-
if (operandResult.status === "indeterminate") return operandResult;
|
|
126
|
-
const outcomes = candidates.map((candidate) => {
|
|
127
|
-
const candidateResult = candidate(context, resolvers, budget);
|
|
128
|
-
if (candidateResult.status === "indeterminate") return candidateResult;
|
|
129
|
-
return require_evaluator_operations.computeMembershipMatch(operandResult.value, candidateResult.value);
|
|
130
|
-
});
|
|
131
|
-
for (const outcome of outcomes) if (outcome.status === "definite" && outcome.value) return require_evaluation.definite(matched);
|
|
132
|
-
const reason = require_evaluation.firstIndeterminate(...outcomes);
|
|
133
|
-
if (reason !== void 0) return {
|
|
134
|
-
status: "indeterminate",
|
|
135
|
-
reason
|
|
136
|
-
};
|
|
137
|
-
return require_evaluation.definite(!matched);
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
case "exists": {
|
|
141
|
-
const operand = value(node.operand);
|
|
142
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
143
|
-
const result = operand(context, resolvers, budget);
|
|
144
|
-
if (result.status === "indeterminate") {
|
|
145
|
-
if (result.reason.code === "not-yet-known" || require_evaluator_budget.isResourceExhaustion(result.reason)) return result;
|
|
146
|
-
if (result.reason.code === "not-found") return require_evaluation.definite(false);
|
|
147
|
-
}
|
|
148
|
-
return require_evaluation.definite(true);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
case "some":
|
|
152
|
-
case "every":
|
|
153
|
-
case "treeReference": return this.interpretedPredicate(node, depth);
|
|
154
|
-
default: return this.interpretedPredicate(node, depth);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* 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.
|
|
159
|
-
*/
|
|
160
|
-
value(node, depth, fresh) {
|
|
161
|
-
if (depth >= this.maxNestingDepth) return this.interpretedValue(node, depth);
|
|
162
|
-
this.nodesCompiled += 1;
|
|
163
|
-
const child = (operand) => this.value(operand, depth + 1, false);
|
|
164
|
-
const handedOver = (operand) => this.value(operand, depth + 1, true);
|
|
165
|
-
switch (node.kind) {
|
|
166
|
-
case "reference": {
|
|
167
|
-
const { key, unit } = node;
|
|
168
|
-
const keyText = JSON.stringify(key);
|
|
169
|
-
const notFound = `no value found for reference key ${keyText}`;
|
|
170
|
-
const notYetKnown = `the value for reference key ${keyText} is not yet known`;
|
|
171
|
-
return this.charged(depth, (context, resolvers) => {
|
|
172
|
-
const resolution = require_evaluator_effect.settleSync(resolvers.resolveValue(key, context));
|
|
173
|
-
if (!resolution.found) return require_evaluator_operations.absenceEvaluation(resolution, notFound, notYetKnown);
|
|
174
|
-
const unitError = require_evaluator_operations.checkReferenceUnit(unit, resolution.value);
|
|
175
|
-
if (unitError !== void 0) return unitError;
|
|
176
|
-
return require_evaluation.definite(resolution.value);
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
case "accumulator": return this.charged(depth, () => require_evaluation.indeterminate("wrong-type", ACCUMULATOR_OUTSIDE_FOLD));
|
|
180
|
-
case "call": {
|
|
181
|
-
const args = node.args.map(handedOver);
|
|
182
|
-
const { fn } = node;
|
|
183
|
-
const { functions } = this;
|
|
184
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
185
|
-
const results = args.map((arg) => arg(context, resolvers, budget));
|
|
186
|
-
const values = [];
|
|
187
|
-
for (const result of results) {
|
|
188
|
-
if (result.status === "indeterminate") return result;
|
|
189
|
-
values.push(result.value);
|
|
190
|
-
}
|
|
191
|
-
return require_evaluator_operations.invokeRegisteredFunction(functions, fn, values);
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
case "numberLiteral":
|
|
195
|
-
case "textLiteral":
|
|
196
|
-
case "booleanLiteral":
|
|
197
|
-
case "instantLiteral":
|
|
198
|
-
case "durationLiteral":
|
|
199
|
-
case "complexLiteral": {
|
|
200
|
-
if (fresh) return this.charged(depth, () => require_evaluation.definite(require_evaluator_literals.literalValue(node)));
|
|
201
|
-
const shared = require_evaluation.definite(require_evaluator_literals.literalValue(node));
|
|
202
|
-
return this.charged(depth, () => shared);
|
|
203
|
-
}
|
|
204
|
-
case "arithmetic": {
|
|
205
|
-
const left = child(node.left);
|
|
206
|
-
const right = child(node.right);
|
|
207
|
-
const { op } = node;
|
|
208
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
209
|
-
const leftResult = left(context, resolvers, budget);
|
|
210
|
-
const rightResult = right(context, resolvers, budget);
|
|
211
|
-
if (leftResult.status === "indeterminate") return leftResult;
|
|
212
|
-
if (rightResult.status === "indeterminate") return rightResult;
|
|
213
|
-
return require_evaluator_operations.applyArithmetic(op, leftResult.value, rightResult.value);
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
case "negate": {
|
|
217
|
-
const operand = child(node.operand);
|
|
218
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
219
|
-
const result = operand(context, resolvers, budget);
|
|
220
|
-
if (result.status === "indeterminate") return result;
|
|
221
|
-
return require_evaluator_operations.applyNegate(result.value);
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
case "lookup": {
|
|
225
|
-
const keys = node.keys.map(handedOver);
|
|
226
|
-
const { table } = node;
|
|
227
|
-
const tableText = JSON.stringify(table);
|
|
228
|
-
const notFound = `no match found in lookup table ${tableText}`;
|
|
229
|
-
const notYetKnown = `the match in lookup table ${tableText} is not yet known`;
|
|
230
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
231
|
-
const results = keys.map((key) => key(context, resolvers, budget));
|
|
232
|
-
const keyValues = [];
|
|
233
|
-
for (const result of results) {
|
|
234
|
-
if (result.status === "indeterminate") return result;
|
|
235
|
-
keyValues.push(result.value);
|
|
236
|
-
}
|
|
237
|
-
const resolution = require_evaluator_effect.settleSync(resolvers.resolveLookup(table, keyValues, context));
|
|
238
|
-
if (!resolution.found) return require_evaluator_operations.absenceEvaluation(resolution, notFound, notYetKnown);
|
|
239
|
-
return require_evaluation.definite(resolution.value);
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
case "conditional": {
|
|
243
|
-
const fallback = child(node.fallback);
|
|
244
|
-
const cases = node.cases.map(({ when, then }) => ({
|
|
245
|
-
when: this.predicate(when, depth + 1),
|
|
246
|
-
then: child(then)
|
|
247
|
-
}));
|
|
248
|
-
if ((node.hitPolicy ?? "first") === "first") return this.charged(depth, (context, resolvers, budget) => {
|
|
249
|
-
for (const { when, then } of cases) {
|
|
250
|
-
const whenResult = when(context, resolvers, budget);
|
|
251
|
-
if (whenResult.status === "indeterminate") return whenResult;
|
|
252
|
-
if (whenResult.value) return then(context, resolvers, budget);
|
|
253
|
-
}
|
|
254
|
-
return fallback(context, resolvers, budget);
|
|
255
|
-
});
|
|
256
|
-
return this.charged(depth, (context, resolvers, budget) => {
|
|
257
|
-
const whenResults = cases.map(({ when }) => when(context, resolvers, budget));
|
|
258
|
-
let matches = 0;
|
|
259
|
-
let matched;
|
|
260
|
-
cases.forEach(({ then }, index) => {
|
|
261
|
-
const whenResult = whenResults[index];
|
|
262
|
-
if (whenResult?.status === "definite" && whenResult.value) {
|
|
263
|
-
matches += 1;
|
|
264
|
-
matched = then;
|
|
265
|
-
}
|
|
266
|
-
});
|
|
267
|
-
if (matches >= 2) return require_evaluation.indeterminate("domain-error", "more than one case matched under the 'unique' hit policy");
|
|
268
|
-
const reason = require_evaluation.firstIndeterminate(...whenResults);
|
|
269
|
-
if (reason !== void 0) return {
|
|
270
|
-
status: "indeterminate",
|
|
271
|
-
reason
|
|
272
|
-
};
|
|
273
|
-
return (matched ?? fallback)(context, resolvers, budget);
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
case "delegate": {
|
|
277
|
-
const { system, payload } = node;
|
|
278
|
-
const notFound = `delegate handler for external system '${system}' reported no value`;
|
|
279
|
-
const notYetKnown = `the value from the delegate handler for external system '${system}' is not yet known`;
|
|
280
|
-
const noHandler = `no delegate handler registered for external system '${system}'`;
|
|
281
|
-
return this.charged(depth, (context, resolvers) => {
|
|
282
|
-
if (resolvers.resolveDelegate === void 0) return require_evaluation.indeterminate("wrong-type", noHandler);
|
|
283
|
-
const resolution = require_evaluator_effect.settleSync(resolvers.resolveDelegate(system, payload, context));
|
|
284
|
-
if (!resolution.found) return require_evaluator_operations.absenceEvaluation(resolution, notFound, notYetKnown);
|
|
285
|
-
return require_evaluation.definite(resolution.value);
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
case "fold":
|
|
289
|
-
case "treeReference": return this.interpretedValue(node, depth);
|
|
290
|
-
default: return this.interpretedValue(node, depth);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
};
|
|
294
|
-
/**
|
|
295
|
-
* 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).
|
|
296
|
-
*
|
|
297
|
-
* 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.
|
|
298
|
-
*
|
|
299
|
-
* 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.
|
|
300
|
-
*
|
|
301
|
-
* 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).
|
|
302
|
-
*/
|
|
303
|
-
function compilePredicateTree(node, functions, maxNodes, maxNestingDepth) {
|
|
304
|
-
const uncounted = new NodeCompiler(functions, maxNestingDepth, false);
|
|
305
|
-
const uncountedRun = uncounted.predicate(node, 0);
|
|
306
|
-
if (!uncounted.interpreted && uncounted.nodesCompiled <= maxNodes) return (context, resolvers) => uncountedRun(context, resolvers, UNLIMITED_BUDGET);
|
|
307
|
-
const countedRun = new NodeCompiler(functions, maxNestingDepth, true).predicate(node, 0);
|
|
308
|
-
return (context, resolvers) => countedRun(context, resolvers, require_evaluator_budget.createEvaluationBudget(maxNodes, maxNestingDepth));
|
|
309
|
-
}
|
|
310
|
-
exports.compilePredicateTree = compilePredicateTree;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Evaluation } from "./evaluation.cjs";
|
|
2
|
-
import { FunctionRegistry } from "./functions.cjs";
|
|
3
|
-
import { EvaluationContext, SyncResolvers } from "./resolvers.cjs";
|
|
4
|
-
import { PredicateNode } from "./tree.cjs";
|
|
5
|
-
/**
|
|
6
|
-
* A predicate tree prepared for repeated evaluation: the same result as `createSyncEvaluator`'s `evaluatePredicate` over the tree it was compiled from, for any context and resolvers.
|
|
7
|
-
*/
|
|
8
|
-
export type CompiledPredicate = (context: EvaluationContext, resolvers: Readonly<SyncResolvers>) => Evaluation<boolean>;
|
|
9
|
-
/**
|
|
10
|
-
* 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).
|
|
11
|
-
*
|
|
12
|
-
* 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.
|
|
13
|
-
*
|
|
14
|
-
* 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.
|
|
15
|
-
*
|
|
16
|
-
* 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).
|
|
17
|
-
*/
|
|
18
|
-
export declare function compilePredicateTree(node: PredicateNode, functions: Readonly<FunctionRegistry>, maxNodes: number, maxNestingDepth: number): CompiledPredicate;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
// src/evaluator-effect.ts
|
|
3
|
-
/**
|
|
4
|
-
* Suspends until `value` is available. The driver resumes the task with exactly that value (awaited, in the async driver), and the generator's own "next" type is `T`, so the result is typed without an assertion.
|
|
5
|
-
*/
|
|
6
|
-
function* wait(value) {
|
|
7
|
-
return yield {
|
|
8
|
-
kind: "wait",
|
|
9
|
-
value
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* 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).
|
|
14
|
-
*/
|
|
15
|
-
function* all(tasks) {
|
|
16
|
-
return yield {
|
|
17
|
-
kind: "all",
|
|
18
|
-
tasks
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
function isThenable(value) {
|
|
22
|
-
return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
|
|
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
|
-
}
|
|
31
|
-
/** Runs a task to completion, awaiting each suspension and running an `all` group's tasks concurrently. */
|
|
32
|
-
async function runAsync(task) {
|
|
33
|
-
let step = task.next();
|
|
34
|
-
while (step.done !== true) {
|
|
35
|
-
const suspension = step.value;
|
|
36
|
-
const result = suspension.kind === "wait" ? await suspension.value : await Promise.all(suspension.tasks.map(runAsync));
|
|
37
|
-
step = task.next(result);
|
|
38
|
-
}
|
|
39
|
-
return step.value;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
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`).
|
|
43
|
-
*/
|
|
44
|
-
function runSync(task) {
|
|
45
|
-
let step = task.next();
|
|
46
|
-
while (step.done !== true) {
|
|
47
|
-
const suspension = step.value;
|
|
48
|
-
const result = suspension.kind === "wait" ? settleSync(suspension.value) : suspension.tasks.map(runSync);
|
|
49
|
-
step = task.next(result);
|
|
50
|
-
}
|
|
51
|
-
return step.value;
|
|
52
|
-
}
|
|
53
|
-
exports.all = all;
|
|
54
|
-
exports.runAsync = runAsync;
|
|
55
|
-
exports.runSync = runSync;
|
|
56
|
-
exports.settleSync = settleSync;
|
|
57
|
-
exports.wait = wait;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The evaluator's node rules are written once, as generator functions that describe *what* to wait for rather than *how* to wait for it. `evaluator.ts` `yield*`s two kinds of suspension and never touches a promise itself:
|
|
3
|
-
* - `wait`: a single resolver result that is either already a value or a promise of one.
|
|
4
|
-
* - `all`: sub-evaluations that are independent of one another, which the async driver runs concurrently and the synchronous driver runs one after another.
|
|
5
|
-
* Two drivers then run the same generator: `runAsync` awaits every suspension, `runSync` requires every one to already be a value. Because both drive one implementation of the node rules, the synchronous and asynchronous evaluators cannot drift apart in how a node is evaluated.
|
|
6
|
-
*/
|
|
7
|
-
/** A single value that may still be a promise; `wait` resolves it in whichever driver runs the surrounding task. */
|
|
8
|
-
type Awaitable<T> = T | Promise<T>;
|
|
9
|
-
/** What a task suspends on: one resolver result (`wait`) or independent sub-tasks (`all`). */
|
|
10
|
-
export type Suspension = {
|
|
11
|
-
readonly kind: "wait";
|
|
12
|
-
readonly value: unknown;
|
|
13
|
-
} | {
|
|
14
|
-
readonly kind: "all";
|
|
15
|
-
readonly tasks: readonly Runnable<unknown>[];
|
|
16
|
-
};
|
|
17
|
-
/** A suspendable computation: yields `Suspension`s and returns `T`. Its "next" type is `never` so that only a `yield*` of `wait` or `all` (whose results are typed by their own operands) can sit inside one; a driver resumes it through `Runnable`, which is deliberately looser. */
|
|
18
|
-
export type Task<T> = Generator<Suspension, T, never>;
|
|
19
|
-
/** What a driver needs from a task: resume it with whatever the last suspension produced. Generator methods are bivariant, so every `Task` is a `Runnable`. */
|
|
20
|
-
type Runnable<T> = Iterator<Suspension, T, unknown>;
|
|
21
|
-
type TaskResults<T extends readonly Runnable<unknown>[]> = { -readonly [K in keyof T]: T[K] extends Task<infer R> ? R : never; };
|
|
22
|
-
/**
|
|
23
|
-
* Suspends until `value` is available. The driver resumes the task with exactly that value (awaited, in the async driver), and the generator's own "next" type is `T`, so the result is typed without an assertion.
|
|
24
|
-
*/
|
|
25
|
-
export declare function wait<T>(value: Awaitable<T>): Generator<Suspension, T, T>;
|
|
26
|
-
/**
|
|
27
|
-
* 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).
|
|
28
|
-
*/
|
|
29
|
-
export declare function all<const T extends readonly Runnable<unknown>[]>(tasks: T): Generator<Suspension, TaskResults<T>, TaskResults<T>>;
|
|
30
|
-
/**
|
|
31
|
-
* 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.
|
|
32
|
-
*/
|
|
33
|
-
export declare function settleSync<T>(value: T): T;
|
|
34
|
-
/** Runs a task to completion, awaiting each suspension and running an `all` group's tasks concurrently. */
|
|
35
|
-
export declare function runAsync<T>(task: Readonly<Runnable<T>>): Promise<T>;
|
|
36
|
-
/**
|
|
37
|
-
* 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`).
|
|
38
|
-
*/
|
|
39
|
-
export declare function runSync<T>(task: Readonly<Runnable<T>>): T;
|
|
40
|
-
export {};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_evaluator_budget = require("./evaluator-budget.cjs");
|
|
3
|
-
const require_evaluator = require("./evaluator.cjs");
|
|
4
|
-
const require_evaluator_compiled = require("./evaluator-compiled.cjs");
|
|
5
|
-
const require_functions = require("./functions.cjs");
|
|
6
|
-
const require_evaluator_effect = require("./evaluator-effect.cjs");
|
|
7
|
-
// src/evaluator-factory.ts
|
|
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.
|
|
10
|
-
*/
|
|
11
|
-
function createEvaluator({ functions = require_functions.emptyFunctionRegistry, maxNodes = require_evaluator_budget.DEFAULT_MAX_EVALUATION_NODES, maxNestingDepth = 500 }) {
|
|
12
|
-
return {
|
|
13
|
-
evaluatePredicate: async (node, context, resolvers) => require_evaluator_effect.runAsync(require_evaluator.evaluatePredicateInternal(node, context, resolvers, void 0, functions, /* @__PURE__ */ new Set(), 0, require_evaluator_budget.createEvaluationBudget(maxNodes, maxNestingDepth), 0)),
|
|
14
|
-
evaluateValue: async (node, context, resolvers) => require_evaluator_effect.runAsync(require_evaluator.evaluateValueInternal(node, context, resolvers, void 0, functions, /* @__PURE__ */ new Set(), 0, require_evaluator_budget.createEvaluationBudget(maxNodes, maxNestingDepth), 0))
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* The synchronous counterpart of `createEvaluator`: the same node rules, options and `Evaluation` results, over `SyncResolvers`, returning the result directly instead of a promise. It runs the same implementation as the asynchronous evaluator, so the two cannot disagree about a node. One difference follows from having nothing to wait on: independent operands (both sides of an `and`, the candidates of a `memberOf`, the items of a collection) are evaluated one after another rather than concurrently, so when a `maxNodes` or `maxNestingDepth` budget is exhausted partway through a tree, which operand meets the limit first can differ from the asynchronous evaluator.
|
|
19
|
-
*/
|
|
20
|
-
function createSyncEvaluator({ functions = require_functions.emptyFunctionRegistry, maxNodes = require_evaluator_budget.DEFAULT_MAX_EVALUATION_NODES, maxNestingDepth = 500 }) {
|
|
21
|
-
return {
|
|
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)),
|
|
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)
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
const { evaluatePredicate, evaluateValue } = createEvaluator({});
|
|
28
|
-
exports.createEvaluator = createEvaluator;
|
|
29
|
-
exports.createSyncEvaluator = createSyncEvaluator;
|
|
30
|
-
exports.evaluatePredicate = evaluatePredicate;
|
|
31
|
-
exports.evaluateValue = evaluateValue;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ComputedValue } from "./computed-value.cjs";
|
|
2
|
-
import { Evaluation } from "./evaluation.cjs";
|
|
3
|
-
import { CompiledPredicate } from "./evaluator-compiled.cjs";
|
|
4
|
-
import { FunctionRegistry } from "./functions.cjs";
|
|
5
|
-
import { EvaluationContext, Resolvers, SyncResolvers } from "./resolvers.cjs";
|
|
6
|
-
import { ExpressionNode, PredicateNode } from "./tree.cjs";
|
|
7
|
-
/**
|
|
8
|
-
* 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.
|
|
9
|
-
*/
|
|
10
|
-
/** Options shared by `createEvaluator` and `createSyncEvaluator`. */
|
|
11
|
-
export interface EvaluatorOptions {
|
|
12
|
-
functions?: FunctionRegistry;
|
|
13
|
-
/** Caps the total number of predicate/expression nodes a single `evaluatePredicate`/`evaluateValue` call may visit -- see `createEvaluationBudget`. Defaults to `DEFAULT_MAX_EVALUATION_NODES`. */
|
|
14
|
-
maxNodes?: number;
|
|
15
|
-
/** Caps ordinary recursive-descent nesting depth, independent of `MAX_TREE_REFERENCE_DEPTH`'s own `treeReference` chain-depth cap -- see `createEvaluationBudget`. Defaults to `DEFAULT_MAX_NESTING_DEPTH`. */
|
|
16
|
-
maxNestingDepth?: number;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* 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.
|
|
20
|
-
*/
|
|
21
|
-
export declare function createEvaluator({ functions, maxNodes, maxNestingDepth }: EvaluatorOptions): {
|
|
22
|
-
evaluatePredicate: (node: PredicateNode, context: EvaluationContext, resolvers: Readonly<Resolvers>) => Promise<Evaluation<boolean>>;
|
|
23
|
-
evaluateValue: (node: ExpressionNode, context: EvaluationContext, resolvers: Readonly<Resolvers>) => Promise<Evaluation<ComputedValue>>;
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* The synchronous counterpart of `createEvaluator`: the same node rules, options and `Evaluation` results, over `SyncResolvers`, returning the result directly instead of a promise. It runs the same implementation as the asynchronous evaluator, so the two cannot disagree about a node. One difference follows from having nothing to wait on: independent operands (both sides of an `and`, the candidates of a `memberOf`, the items of a collection) are evaluated one after another rather than concurrently, so when a `maxNodes` or `maxNestingDepth` budget is exhausted partway through a tree, which operand meets the limit first can differ from the asynchronous evaluator.
|
|
27
|
-
*/
|
|
28
|
-
export declare function createSyncEvaluator({ functions, maxNodes, maxNestingDepth }: EvaluatorOptions): {
|
|
29
|
-
evaluatePredicate: (node: PredicateNode, context: EvaluationContext, resolvers: Readonly<SyncResolvers>) => Evaluation<boolean>;
|
|
30
|
-
evaluateValue: (node: ExpressionNode, context: EvaluationContext, resolvers: Readonly<SyncResolvers>) => Evaluation<ComputedValue>;
|
|
31
|
-
/** 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. */
|
|
32
|
-
compilePredicate: (node: PredicateNode) => CompiledPredicate;
|
|
33
|
-
};
|
|
34
|
-
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>>;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_complex = require("./complex.cjs");
|
|
3
|
-
// 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
|
-
exports.literalValue = literalValue;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { ComputedValue } from "./computed-value.cjs";
|
|
2
|
-
import { BooleanLiteralNode, ComplexLiteralNode, DurationLiteralNode, InstantLiteralNode, NumberLiteralNode, TextLiteralNode } from "./tree.cjs";
|
|
3
|
-
/** Every expression node kind that is a constant: it evaluates to a value without consulting a resolver or any other node. */
|
|
4
|
-
export type LiteralNode = NumberLiteralNode | TextLiteralNode | BooleanLiteralNode | InstantLiteralNode | DurationLiteralNode | ComplexLiteralNode;
|
|
5
|
-
/**
|
|
6
|
-
* 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.
|
|
7
|
-
*/
|
|
8
|
-
export declare function literalValue(node: Readonly<LiteralNode>): ComputedValue;
|