trilean 1.5.1 → 1.6.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/dist/complex.d.cts +1 -1
- package/dist/complex.d.ts +1 -1
- package/dist/computed-value-CbRF0Izc.d.cts +51 -0
- package/dist/computed-value-CbRF0Izc.d.ts +51 -0
- package/dist/computed-value.cjs +1 -1
- package/dist/computed-value.d.cts +1 -50
- package/dist/computed-value.d.ts +1 -50
- package/dist/computed-value.js +1 -1
- package/dist/evaluator-budget.cjs +24 -0
- package/dist/evaluator-budget.d.cts +21 -0
- package/dist/evaluator-budget.d.ts +21 -0
- package/dist/evaluator-budget.js +20 -0
- package/dist/evaluator-collection.cjs +33 -0
- package/dist/evaluator-collection.d.cts +23 -0
- package/dist/evaluator-collection.d.ts +23 -0
- package/dist/evaluator-collection.js +31 -0
- package/dist/evaluator-factory.cjs +19 -0
- package/dist/evaluator-factory.d.cts +22 -0
- package/dist/evaluator-factory.d.ts +22 -0
- package/dist/evaluator-factory.js +16 -0
- package/dist/evaluator-indeterminacy-helpers.cjs +180 -0
- package/dist/evaluator-indeterminacy-helpers.d.cts +57 -0
- package/dist/evaluator-indeterminacy-helpers.d.ts +57 -0
- package/dist/evaluator-indeterminacy-helpers.js +158 -0
- package/dist/evaluator-operations.cjs +385 -0
- package/dist/evaluator-operations.d.cts +27 -0
- package/dist/evaluator-operations.d.ts +27 -0
- package/dist/evaluator-operations.js +376 -0
- package/dist/evaluator-test-helpers.cjs +40 -0
- package/dist/evaluator-test-helpers.d.cts +13 -0
- package/dist/evaluator-test-helpers.d.ts +13 -0
- package/dist/evaluator-test-helpers.js +35 -0
- package/dist/evaluator.cjs +55 -454
- package/dist/evaluator.d.cts +6 -10
- package/dist/evaluator.d.ts +6 -10
- package/dist/evaluator.js +44 -442
- package/dist/functions.d.cts +2 -2
- package/dist/functions.d.ts +2 -2
- package/dist/index.cjs +5 -5
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/magic-string.es-BWtG0AyA.js +1017 -0
- package/dist/magic-string.es-BvKy4sJx.cjs +1017 -0
- package/dist/resolvers.d.cts +1 -1
- package/dist/resolvers.d.ts +1 -1
- package/dist/test.DNmyFkvJ-Dasq3mQO.cjs +13578 -0
- package/dist/test.DNmyFkvJ-SCEEf0VK.js +13573 -0
- package/package.json +2 -2
- package/schemas/trilean.schema.json +1 -1
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { combineUnitsForDivide, combineUnitsForMultiply, unitsEqual } from "./computed-value.js";
|
|
2
|
+
import { definite, firstIndeterminate, indeterminate } from "./evaluation.js";
|
|
3
|
+
import { RegexParseError, compilePattern } from "trilean-regex";
|
|
4
|
+
//#region src/evaluator-operations.ts
|
|
5
|
+
/** The three-valued AND table: `false` is absorbing regardless of the other operand's indeterminacy; otherwise both-definite folds to a boolean AND; otherwise indeterminate, with the tie-break rule (declared operand order, left before right) applied via `firstIndeterminate`. */
|
|
6
|
+
function combineAnd(left, right) {
|
|
7
|
+
if (left.status === "definite" && !left.value) return definite(false);
|
|
8
|
+
if (right.status === "definite" && !right.value) return definite(false);
|
|
9
|
+
const reason = firstIndeterminate(left, right);
|
|
10
|
+
if (reason !== void 0) return {
|
|
11
|
+
status: "indeterminate",
|
|
12
|
+
reason
|
|
13
|
+
};
|
|
14
|
+
return definite(true);
|
|
15
|
+
}
|
|
16
|
+
/** The three-valued OR table: mirror image of `combineAnd`, with `true` absorbing. */
|
|
17
|
+
function combineOr(left, right) {
|
|
18
|
+
if (left.status === "definite" && left.value) return definite(true);
|
|
19
|
+
if (right.status === "definite" && right.value) return definite(true);
|
|
20
|
+
const reason = firstIndeterminate(left, right);
|
|
21
|
+
if (reason !== void 0) return {
|
|
22
|
+
status: "indeterminate",
|
|
23
|
+
reason
|
|
24
|
+
};
|
|
25
|
+
return definite(false);
|
|
26
|
+
}
|
|
27
|
+
function applyComparisonOperator(op, left, right) {
|
|
28
|
+
switch (op) {
|
|
29
|
+
case "gt": return left > right;
|
|
30
|
+
case "gte": return left >= right;
|
|
31
|
+
case "lt": return left < right;
|
|
32
|
+
case "lte": return left <= right;
|
|
33
|
+
case "eq": return left === right;
|
|
34
|
+
case "neq": return left !== right;
|
|
35
|
+
default: throw new Error("unreachable comparison operator");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const millisecondsPerDurationUnit = {
|
|
39
|
+
ms: 1,
|
|
40
|
+
s: 1e3,
|
|
41
|
+
min: 6e4,
|
|
42
|
+
h: 36e5,
|
|
43
|
+
d: 864e5
|
|
44
|
+
};
|
|
45
|
+
/** Normalises a `duration`'s magnitude to milliseconds, the common base unit for combining or comparing two `duration`s (or an `instant` and a `duration`) of potentially different `DurationUnit`s -- shared by `compareValues`'s own `duration` branch below and by the temporal arithmetic further down. */
|
|
46
|
+
function toMilliseconds(value, unit) {
|
|
47
|
+
return value * millisecondsPerDurationUnit[unit];
|
|
48
|
+
}
|
|
49
|
+
/** `Date.parse` reports an unparseable timestamp as `NaN`, and every downstream use of that `NaN` then fails silently rather than loudly: every comparison against it is `false` (so `neq` between two unparseable instants would come back definitely `true`), a subtraction yields a `NaN`-magnitude duration, and `new Date(NaN).toISOString()` throws a `RangeError` outright -- a thrown exception for a data-quality problem, which this design never does (see `Evaluation<T>`'s own doc comment in evaluation.ts). An `instant` whose string will not parse is a value the operation cannot use, so every caller below reports it as `wrong-type`, exactly like any other unusable operand. */
|
|
50
|
+
function toEpochMilliseconds(value) {
|
|
51
|
+
const epochMilliseconds = Date.parse(value);
|
|
52
|
+
return Number.isNaN(epochMilliseconds) ? void 0 : epochMilliseconds;
|
|
53
|
+
}
|
|
54
|
+
function unparseableInstant(value) {
|
|
55
|
+
return indeterminate("wrong-type", `'${value}' is not a parseable ISO-8601 timestamp`);
|
|
56
|
+
}
|
|
57
|
+
/** The representable timestamp range is finite, so a valid instant shifted by a large enough duration lands outside it, where `new Date(...).toISOString()` throws a `RangeError`. Checking the shifted date against the platform's own notion of a valid time value keeps that inside the three-outcome model as a `domain-error` -- an operation pushed outside its valid domain, the same category as division by zero -- and avoids hardcoding the range bound. */
|
|
58
|
+
function instantFromEpochMilliseconds(epochMilliseconds) {
|
|
59
|
+
const shifted = new Date(epochMilliseconds);
|
|
60
|
+
if (Number.isNaN(shifted.getTime())) return indeterminate("domain-error", "the resulting instant falls outside the representable timestamp range");
|
|
61
|
+
return definite({
|
|
62
|
+
kind: "instant",
|
|
63
|
+
value: shifted.toISOString()
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/** `compare`'s two operands: valid kinds are `number` (matching units required), `instant` (ordered by parsed epoch millisecond), or `duration` (ordered by magnitude normalised to milliseconds) -- `text` is never valid here (see `textCompare`), and a kind mismatch between the two operands is `wrong-type`. Narrowing `right` against a literal `left.kind` case (rather than asserting it) is what lets both sides stay properly typed with no `as`. */
|
|
67
|
+
function compareValues(op, left, right) {
|
|
68
|
+
switch (left.kind) {
|
|
69
|
+
case "number":
|
|
70
|
+
if (right.kind !== "number") return indeterminate("wrong-type", `cannot compare a 'number' value with a '${right.kind}' value`);
|
|
71
|
+
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot compare numbers with incompatible units");
|
|
72
|
+
return definite(applyComparisonOperator(op, left.value, right.value));
|
|
73
|
+
case "text": return indeterminate("wrong-type", "text values are not comparable via 'compare'; use 'textCompare'");
|
|
74
|
+
case "boolean":
|
|
75
|
+
if (right.kind !== "boolean") return indeterminate("wrong-type", `cannot compare a 'boolean' value with a '${right.kind}' value`);
|
|
76
|
+
if (op !== "eq" && op !== "neq") return indeterminate("wrong-type", `booleans have no natural ordering; '${op}' is only defined for 'eq'/'neq'`);
|
|
77
|
+
return definite(op === "eq" ? left.value === right.value : left.value !== right.value);
|
|
78
|
+
case "instant": {
|
|
79
|
+
if (right.kind !== "instant") return indeterminate("wrong-type", `cannot compare an 'instant' value with a '${right.kind}' value`);
|
|
80
|
+
const leftEpoch = toEpochMilliseconds(left.value);
|
|
81
|
+
if (leftEpoch === void 0) return unparseableInstant(left.value);
|
|
82
|
+
const rightEpoch = toEpochMilliseconds(right.value);
|
|
83
|
+
if (rightEpoch === void 0) return unparseableInstant(right.value);
|
|
84
|
+
return definite(applyComparisonOperator(op, leftEpoch, rightEpoch));
|
|
85
|
+
}
|
|
86
|
+
case "duration":
|
|
87
|
+
if (right.kind !== "duration") return indeterminate("wrong-type", `cannot compare a 'duration' value with a '${right.kind}' value`);
|
|
88
|
+
return definite(applyComparisonOperator(op, toMilliseconds(left.value, left.unit), toMilliseconds(right.value, right.unit)));
|
|
89
|
+
case "complex":
|
|
90
|
+
if (op !== "eq" && op !== "neq") return indeterminate("wrong-type", `ordering operator '${op}' is not defined for complex values; the complex plane has no total order`);
|
|
91
|
+
if (right.kind !== "complex") return indeterminate("wrong-type", `cannot compare a 'complex' value with a '${right.kind}' value`);
|
|
92
|
+
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot compare complex values with incompatible units");
|
|
93
|
+
return definite(op === "eq" ? left.re === right.re && left.im === right.im : left.re !== right.re || left.im !== right.im);
|
|
94
|
+
default: throw new Error("unreachable computed-value kind");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** `textCompare`'s two operands must both resolve to the `text` computed-value kind -- any other kind, on either operand, is `wrong-type` (see the `textCompare` section of README.md). `equals`/`notEquals` are exact string equality; `matches`/`notMatches` interpret `right`'s text as an ECMAScript regular expression tested against `left`'s text; `portableMatches`/`portableNotMatches` interpret it as a pattern in `trilean-regex`'s own restricted grammar instead, matched by that package's finite-automaton reference matcher rather than the host's native `RegExp` engine -- see the `textCompare` section of README.md for when to reach for the portable pair instead of the plain one. An invalid pattern is `wrong-type` rather than a thrown exception either way -- every data-quality problem stays inside the `Evaluation` result, per `Evaluation<T>`'s own doc comment in evaluation.ts. */
|
|
98
|
+
function compareText(op, left, right) {
|
|
99
|
+
if (left.kind !== "text") return indeterminate("wrong-type", `'textCompare' requires 'text' operands; the left operand is '${left.kind}'`);
|
|
100
|
+
if (right.kind !== "text") return indeterminate("wrong-type", `'textCompare' requires 'text' operands; the right operand is '${right.kind}'`);
|
|
101
|
+
switch (op) {
|
|
102
|
+
case "equals": return definite(left.value === right.value);
|
|
103
|
+
case "notEquals": return definite(left.value !== right.value);
|
|
104
|
+
case "matches":
|
|
105
|
+
case "notMatches": {
|
|
106
|
+
let pattern;
|
|
107
|
+
try {
|
|
108
|
+
pattern = new RegExp(right.value);
|
|
109
|
+
} catch {
|
|
110
|
+
return indeterminate("wrong-type", `'${right.value}' is not a valid regular expression pattern`);
|
|
111
|
+
}
|
|
112
|
+
const isMatch = pattern.test(left.value);
|
|
113
|
+
return definite(op === "matches" ? isMatch : !isMatch);
|
|
114
|
+
}
|
|
115
|
+
case "portableMatches":
|
|
116
|
+
case "portableNotMatches": {
|
|
117
|
+
let compiled;
|
|
118
|
+
try {
|
|
119
|
+
compiled = compilePattern(right.value);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
const reason = error instanceof RegexParseError ? error.message : String(error);
|
|
122
|
+
return indeterminate("wrong-type", `'${right.value}' is not a valid trilean-regex pattern: ${reason}`);
|
|
123
|
+
}
|
|
124
|
+
const isMatch = compiled.test(left.value);
|
|
125
|
+
return definite(op === "portableMatches" ? isMatch : !isMatch);
|
|
126
|
+
}
|
|
127
|
+
default: throw new Error("unreachable text comparison operator");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** `memberOf`'s own value-equality test between two computed values -- kind-agnostic across `number`/`text`/`instant`/`duration` (see the `memberOf` section of README.md, and its "Derived aggregates" note that this equality is kind-agnostic across every computed-value kind), unlike `compare`'s own `eq`, which rejects a `text` operand outright and directs callers to `textCompare` instead. A kind mismatch, or a `number` pair with an incompatible unit, is `wrong-type` -- never simply "not equal". Narrowing `candidate` against a literal `operand.kind` case (rather than asserting it) is the same technique `compareValues` uses above. */
|
|
131
|
+
function computeMembershipMatch(operand, candidate) {
|
|
132
|
+
switch (operand.kind) {
|
|
133
|
+
case "number":
|
|
134
|
+
if (candidate.kind !== "number") return indeterminate("wrong-type", `cannot compare a 'number' value with a '${candidate.kind}' value for membership`);
|
|
135
|
+
if (!unitsEqual(operand.unit, candidate.unit)) return indeterminate("wrong-type", "cannot compare numbers with incompatible units for membership");
|
|
136
|
+
return definite(operand.value === candidate.value);
|
|
137
|
+
case "text":
|
|
138
|
+
if (candidate.kind !== "text") return indeterminate("wrong-type", `cannot compare a 'text' value with a '${candidate.kind}' value for membership`);
|
|
139
|
+
return definite(operand.value === candidate.value);
|
|
140
|
+
case "boolean":
|
|
141
|
+
if (candidate.kind !== "boolean") return indeterminate("wrong-type", `cannot compare a 'boolean' value with a '${candidate.kind}' value for membership`);
|
|
142
|
+
return definite(operand.value === candidate.value);
|
|
143
|
+
case "instant": {
|
|
144
|
+
if (candidate.kind !== "instant") return indeterminate("wrong-type", `cannot compare an 'instant' value with a '${candidate.kind}' value for membership`);
|
|
145
|
+
const operandEpoch = toEpochMilliseconds(operand.value);
|
|
146
|
+
if (operandEpoch === void 0) return unparseableInstant(operand.value);
|
|
147
|
+
const candidateEpoch = toEpochMilliseconds(candidate.value);
|
|
148
|
+
if (candidateEpoch === void 0) return unparseableInstant(candidate.value);
|
|
149
|
+
return definite(operandEpoch === candidateEpoch);
|
|
150
|
+
}
|
|
151
|
+
case "duration":
|
|
152
|
+
if (candidate.kind !== "duration") return indeterminate("wrong-type", `cannot compare a 'duration' value with a '${candidate.kind}' value for membership`);
|
|
153
|
+
return definite(toMilliseconds(operand.value, operand.unit) === toMilliseconds(candidate.value, candidate.unit));
|
|
154
|
+
case "complex":
|
|
155
|
+
if (candidate.kind !== "complex") return indeterminate("wrong-type", `cannot compare a 'complex' value with a '${candidate.kind}' value for membership`);
|
|
156
|
+
if (!unitsEqual(operand.unit, candidate.unit)) return indeterminate("wrong-type", "cannot compare complex values with incompatible units for membership");
|
|
157
|
+
return definite(operand.re === candidate.re && operand.im === candidate.im);
|
|
158
|
+
default: throw new Error("unreachable computed-value kind");
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** `power`/`modulo` have no defined unit-combination rule in this design (unlike `add`/`subtract`'s "identical units" requirement or `multiply`/`divide`'s dimensional-exponent combination) -- scoping them to dimensionless operands avoids inventing an unspecified unit-scaling semantics for a fractional or runtime-determined exponent. */
|
|
162
|
+
function isDimensionless(unit) {
|
|
163
|
+
return unitsEqual(unit, void 0);
|
|
164
|
+
}
|
|
165
|
+
/** `negate` is never sugar for "zero minus the value" (see the `arithmetic`/`negate` section of README.md) -- a `duration`'s magnitude is negated directly, in its own original unit, with no subtraction or millisecond normalisation involved. */
|
|
166
|
+
function applyNegate(operand) {
|
|
167
|
+
switch (operand.kind) {
|
|
168
|
+
case "number": return definite({
|
|
169
|
+
kind: "number",
|
|
170
|
+
value: -operand.value,
|
|
171
|
+
unit: operand.unit
|
|
172
|
+
});
|
|
173
|
+
case "duration": return definite({
|
|
174
|
+
kind: "duration",
|
|
175
|
+
value: -operand.value,
|
|
176
|
+
unit: operand.unit
|
|
177
|
+
});
|
|
178
|
+
case "complex": return definite({
|
|
179
|
+
kind: "complex",
|
|
180
|
+
re: -operand.re,
|
|
181
|
+
im: -operand.im,
|
|
182
|
+
unit: operand.unit
|
|
183
|
+
});
|
|
184
|
+
case "text":
|
|
185
|
+
case "instant":
|
|
186
|
+
case "boolean": return indeterminate("wrong-type", `cannot negate a '${operand.kind}' value`);
|
|
187
|
+
default: throw new Error("unreachable computed-value kind");
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function applyArithmeticOnNumbers(op, left, right) {
|
|
191
|
+
switch (op) {
|
|
192
|
+
case "add":
|
|
193
|
+
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot add numbers with incompatible units");
|
|
194
|
+
return definite({
|
|
195
|
+
kind: "number",
|
|
196
|
+
value: left.value + right.value,
|
|
197
|
+
unit: left.unit
|
|
198
|
+
});
|
|
199
|
+
case "subtract":
|
|
200
|
+
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot subtract numbers with incompatible units");
|
|
201
|
+
return definite({
|
|
202
|
+
kind: "number",
|
|
203
|
+
value: left.value - right.value,
|
|
204
|
+
unit: left.unit
|
|
205
|
+
});
|
|
206
|
+
case "multiply": return definite({
|
|
207
|
+
kind: "number",
|
|
208
|
+
value: left.value * right.value,
|
|
209
|
+
unit: combineUnitsForMultiply(left.unit, right.unit)
|
|
210
|
+
});
|
|
211
|
+
case "divide":
|
|
212
|
+
if (right.value === 0) return indeterminate("domain-error", "division by zero");
|
|
213
|
+
return definite({
|
|
214
|
+
kind: "number",
|
|
215
|
+
value: left.value / right.value,
|
|
216
|
+
unit: combineUnitsForDivide(left.unit, right.unit)
|
|
217
|
+
});
|
|
218
|
+
case "power":
|
|
219
|
+
if (!isDimensionless(left.unit) || !isDimensionless(right.unit)) return indeterminate("wrong-type", "'power' requires dimensionless operands");
|
|
220
|
+
if (left.value < 0 && !Number.isInteger(right.value)) return indeterminate("domain-error", "a negative base raised to a non-integer power is not a real number");
|
|
221
|
+
if (left.value === 0 && right.value < 0) return indeterminate("domain-error", "zero raised to a negative power is a division by zero");
|
|
222
|
+
return definite({
|
|
223
|
+
kind: "number",
|
|
224
|
+
value: left.value ** right.value
|
|
225
|
+
});
|
|
226
|
+
case "modulo":
|
|
227
|
+
if (!isDimensionless(left.unit) || !isDimensionless(right.unit)) return indeterminate("wrong-type", "'modulo' requires dimensionless operands");
|
|
228
|
+
if (right.value === 0) return indeterminate("domain-error", "modulo by zero");
|
|
229
|
+
return definite({
|
|
230
|
+
kind: "number",
|
|
231
|
+
value: left.value % right.value
|
|
232
|
+
});
|
|
233
|
+
default: throw new Error("unreachable arithmetic operator");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/** The only same-kind, non-`number` arithmetic this design defines: two `duration`s combine by normalising both to milliseconds first (see `toMilliseconds`), reporting the result in milliseconds -- `multiply`/`divide`/`power`/`modulo` have no representable result unit for a `duration` squared or a dimensionless ratio, so they are `wrong-type` rather than invented. */
|
|
237
|
+
function applyArithmeticOnDurations(op, left, right) {
|
|
238
|
+
switch (op) {
|
|
239
|
+
case "add": return definite({
|
|
240
|
+
kind: "duration",
|
|
241
|
+
value: toMilliseconds(left.value, left.unit) + toMilliseconds(right.value, right.unit),
|
|
242
|
+
unit: "ms"
|
|
243
|
+
});
|
|
244
|
+
case "subtract": return definite({
|
|
245
|
+
kind: "duration",
|
|
246
|
+
value: toMilliseconds(left.value, left.unit) - toMilliseconds(right.value, right.unit),
|
|
247
|
+
unit: "ms"
|
|
248
|
+
});
|
|
249
|
+
case "multiply":
|
|
250
|
+
case "divide":
|
|
251
|
+
case "power":
|
|
252
|
+
case "modulo": return indeterminate("wrong-type", `arithmetic operator '${op}' is not defined between two 'duration' values`);
|
|
253
|
+
default: throw new Error("unreachable arithmetic operator");
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/** Every real number is a complex number with a zero imaginary part, so promoting one is exact and total -- unlike the temporal cross-kind combinations above, which each had to be enumerated because no such embedding exists between an `instant` and a `duration`. This is what lets a tree mix real and complex terms freely instead of forcing every real literal to be written as a complex one. */
|
|
257
|
+
function toComplexValue(value) {
|
|
258
|
+
if (value.kind === "complex") return value;
|
|
259
|
+
return {
|
|
260
|
+
kind: "complex",
|
|
261
|
+
re: value.value,
|
|
262
|
+
im: 0,
|
|
263
|
+
unit: value.unit
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
/** The complex product, as its own component-level helper rather than only a branch of the operator switch below, because `power`'s repeated multiplication needs exactly this and must not re-derive it. */
|
|
267
|
+
function multiplyComplexValues(left, right) {
|
|
268
|
+
return {
|
|
269
|
+
kind: "complex",
|
|
270
|
+
re: left.re * right.re - left.im * right.im,
|
|
271
|
+
im: left.re * right.im + left.im * right.re,
|
|
272
|
+
unit: combineUnitsForMultiply(left.unit, right.unit)
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
/** Complex arithmetic, over the one canonical rectangular representation the `complex` kind stores (see the "Complex values" section of README.md). `add`/`subtract` are component-wise and carry the same identical-units requirement real numbers already have. `power` is deliberately excluded from this operator set rather than handled and rejected here: its exponent must stay an un-promoted real number, so the dispatcher routes it to `applyComplexPower` first, and excluding it from the type is what makes the compiler enforce that routing instead of leaving a dead branch behind. */
|
|
276
|
+
function applyArithmeticOnComplex(op, left, right) {
|
|
277
|
+
switch (op) {
|
|
278
|
+
case "add":
|
|
279
|
+
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot add complex values with incompatible units");
|
|
280
|
+
return definite({
|
|
281
|
+
kind: "complex",
|
|
282
|
+
re: left.re + right.re,
|
|
283
|
+
im: left.im + right.im,
|
|
284
|
+
unit: left.unit
|
|
285
|
+
});
|
|
286
|
+
case "subtract":
|
|
287
|
+
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot subtract complex values with incompatible units");
|
|
288
|
+
return definite({
|
|
289
|
+
kind: "complex",
|
|
290
|
+
re: left.re - right.re,
|
|
291
|
+
im: left.im - right.im,
|
|
292
|
+
unit: left.unit
|
|
293
|
+
});
|
|
294
|
+
case "multiply": return definite(multiplyComplexValues(left, right));
|
|
295
|
+
case "divide": {
|
|
296
|
+
const divisorSquaredMagnitude = right.re * right.re + right.im * right.im;
|
|
297
|
+
if (divisorSquaredMagnitude === 0) return indeterminate("domain-error", "division by zero");
|
|
298
|
+
return definite({
|
|
299
|
+
kind: "complex",
|
|
300
|
+
re: (left.re * right.re + left.im * right.im) / divisorSquaredMagnitude,
|
|
301
|
+
im: (left.im * right.re - left.re * right.im) / divisorSquaredMagnitude,
|
|
302
|
+
unit: combineUnitsForDivide(left.unit, right.unit)
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
case "modulo": return indeterminate("domain-error", "'modulo' is undefined for complex values");
|
|
306
|
+
default: throw new Error("unreachable arithmetic operator");
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/** `power` with a complex operand on either side, defined for exactly one case: a real integer exponent, evaluated as the repeated multiplication that integer exponentiation *is* (see the "Complex values" section of README.md for why an arbitrary complex exponent stays out of scope). */
|
|
310
|
+
function applyComplexPower(base, exponent) {
|
|
311
|
+
if (exponent.kind !== "number" || !Number.isInteger(exponent.value)) return indeterminate("wrong-type", "'power' with a complex operand requires a real integer exponent");
|
|
312
|
+
if (!isDimensionless(base.unit) || !isDimensionless(exponent.unit)) return indeterminate("wrong-type", "'power' requires dimensionless operands");
|
|
313
|
+
const complexUnit = {
|
|
314
|
+
kind: "complex",
|
|
315
|
+
re: 1,
|
|
316
|
+
im: 0,
|
|
317
|
+
unit: {}
|
|
318
|
+
};
|
|
319
|
+
const complexBase = toComplexValue(base);
|
|
320
|
+
let repeatedProduct = complexUnit;
|
|
321
|
+
for (let applied = 0; applied < Math.abs(exponent.value); applied += 1) repeatedProduct = multiplyComplexValues(repeatedProduct, complexBase);
|
|
322
|
+
if (exponent.value >= 0) return definite(repeatedProduct);
|
|
323
|
+
return applyArithmeticOnComplex("divide", complexUnit, repeatedProduct);
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Dispatches `arithmetic` by operand kind. The three cross-kind temporal combinations this design defines (`instant - instant`, `instant + duration`, `duration + instant`) are checked explicitly first, in that order, against the exact operator each requires; any other combination touching an `instant` is `wrong-type` (see "Temporal values" in README.md -- e.g. adding two instants, or subtracting a `duration` from an `instant`, are deliberately *not* defined). Same-kind `duration`/`duration` combinations are delegated to `applyArithmeticOnDurations`; a `duration` paired with anything other than an `instant` or another `duration` is `wrong-type`. Everything remaining requires two `number` operands.
|
|
327
|
+
*/
|
|
328
|
+
function applyArithmetic(op, left, right) {
|
|
329
|
+
if (left.kind === "instant" && right.kind === "instant" && op === "subtract") {
|
|
330
|
+
const leftEpoch = toEpochMilliseconds(left.value);
|
|
331
|
+
if (leftEpoch === void 0) return unparseableInstant(left.value);
|
|
332
|
+
const rightEpoch = toEpochMilliseconds(right.value);
|
|
333
|
+
if (rightEpoch === void 0) return unparseableInstant(right.value);
|
|
334
|
+
return definite({
|
|
335
|
+
kind: "duration",
|
|
336
|
+
value: leftEpoch - rightEpoch,
|
|
337
|
+
unit: "ms"
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
if (left.kind === "instant" && right.kind === "duration" && op === "add") {
|
|
341
|
+
const leftEpoch = toEpochMilliseconds(left.value);
|
|
342
|
+
if (leftEpoch === void 0) return unparseableInstant(left.value);
|
|
343
|
+
return instantFromEpochMilliseconds(leftEpoch + toMilliseconds(right.value, right.unit));
|
|
344
|
+
}
|
|
345
|
+
if (left.kind === "duration" && right.kind === "instant" && op === "add") {
|
|
346
|
+
const rightEpoch = toEpochMilliseconds(right.value);
|
|
347
|
+
if (rightEpoch === void 0) return unparseableInstant(right.value);
|
|
348
|
+
return instantFromEpochMilliseconds(rightEpoch + toMilliseconds(left.value, left.unit));
|
|
349
|
+
}
|
|
350
|
+
if (left.kind === "instant" || right.kind === "instant") return indeterminate("wrong-type", `arithmetic operator '${op}' is not defined between a '${left.kind}' and a '${right.kind}' value`);
|
|
351
|
+
if (left.kind === "duration" && right.kind === "duration") return applyArithmeticOnDurations(op, left, right);
|
|
352
|
+
if (left.kind === "duration" || right.kind === "duration") return indeterminate("wrong-type", `arithmetic operator '${op}' is not defined between a '${left.kind}' and a '${right.kind}' value`);
|
|
353
|
+
if (left.kind !== "number" && left.kind !== "complex") return indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${left.kind}' value`);
|
|
354
|
+
if (right.kind !== "number" && right.kind !== "complex") return indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${right.kind}' value`);
|
|
355
|
+
if (left.kind === "complex" || right.kind === "complex") {
|
|
356
|
+
if (op === "power") return applyComplexPower(left, right);
|
|
357
|
+
return applyArithmeticOnComplex(op, toComplexValue(left), toComplexValue(right));
|
|
358
|
+
}
|
|
359
|
+
return applyArithmeticOnNumbers(op, left, right);
|
|
360
|
+
}
|
|
361
|
+
/** `reference`'s optional expected-unit check: when `expectedUnit` is declared (from `node.unit`), the resolved value must be numeric (`number` or `complex`) and its own unit must match. Returns the `wrong-type` `Evaluation` to return immediately if the check fails, or `undefined` if the reference may resolve as-is. */
|
|
362
|
+
function checkReferenceUnit(expectedUnit, resolvedValue) {
|
|
363
|
+
if (expectedUnit === void 0) return void 0;
|
|
364
|
+
if (resolvedValue.kind !== "number" && resolvedValue.kind !== "complex") return indeterminate("wrong-type", "a unit was expected on a reference that resolved to a non-numeric value");
|
|
365
|
+
if (!unitsEqual(expectedUnit, resolvedValue.unit)) return indeterminate("wrong-type", "the resolved value's unit does not match the reference's expected unit");
|
|
366
|
+
}
|
|
367
|
+
/** `call`'s own function-registry dispatch, given the `fn` name off the tree and its already-evaluated `args`. `node.fn` comes off the serialised tree, which this design treats as data that may have been authored anywhere (see README.md's opening section), while `FunctionRegistry` is an ordinary object with `Object.prototype` on its chain. A bare index lookup would therefore resolve `toString`, `valueOf`, `constructor` and friends as though a consumer had registered them; only the registry's own keys count as registered function names. */
|
|
368
|
+
function invokeRegisteredFunction(functions, fnName, args) {
|
|
369
|
+
const fn = Object.hasOwn(functions, fnName) ? functions[fnName] : void 0;
|
|
370
|
+
if (fn === void 0) return indeterminate("wrong-type", `no function registered under the name '${fnName}'`);
|
|
371
|
+
const outcome = fn(args);
|
|
372
|
+
if ("domainError" in outcome) return indeterminate("domain-error", outcome.domainError);
|
|
373
|
+
return definite(outcome);
|
|
374
|
+
}
|
|
375
|
+
//#endregion
|
|
376
|
+
export { applyArithmetic, applyNegate, checkReferenceUnit, combineAnd, combineOr, compareText, compareValues, computeMembershipMatch, invokeRegisteredFunction };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_test_DNmyFkvJ = require("./test.DNmyFkvJ-Dasq3mQO.cjs");
|
|
3
|
+
//#region src/evaluator-test-helpers.ts
|
|
4
|
+
/** A resolver reporting a fixed `{ kind: "number", value: 10, unit: { m: 1 } }` for key `"present"`, and not-found for anything else -- `resolveLookup`/`resolveCollection` are never exercised by expression-level nodes and are stubbed only to satisfy `Resolvers`. Shared across the split `evaluator.*.test.ts` files below; a test needing genuinely different resolver behaviour spreads over it (`{ ...resolvers, resolveValue: ... }`) locally rather than mutating this shared instance. */
|
|
5
|
+
const resolvers = {
|
|
6
|
+
resolveValue: async (key) => Promise.resolve(key === "present" ? {
|
|
7
|
+
found: true,
|
|
8
|
+
value: {
|
|
9
|
+
kind: "number",
|
|
10
|
+
value: 10,
|
|
11
|
+
unit: { m: 1 }
|
|
12
|
+
}
|
|
13
|
+
} : { found: false }),
|
|
14
|
+
resolveLookup: async () => Promise.resolve({ found: false }),
|
|
15
|
+
resolveCollection: async () => Promise.resolve([])
|
|
16
|
+
};
|
|
17
|
+
function expectDefinite(evaluation, expected) {
|
|
18
|
+
require_test_DNmyFkvJ.globalExpect(evaluation).toEqual({
|
|
19
|
+
status: "definite",
|
|
20
|
+
value: expected
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function expectIndeterminate(evaluation, code) {
|
|
24
|
+
require_test_DNmyFkvJ.globalExpect(evaluation.status).toBe("indeterminate");
|
|
25
|
+
if (evaluation.status === "indeterminate") require_test_DNmyFkvJ.globalExpect(evaluation.reason.code).toBe(code);
|
|
26
|
+
}
|
|
27
|
+
/** A narrowing type guard for a resolver context treated as a plain record, following this repo's `object -> Record<string, unknown>` narrowing convention rather than an `as Record<string, unknown>` assertion. */
|
|
28
|
+
function isPlainRecord(value) {
|
|
29
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
30
|
+
}
|
|
31
|
+
/** `Array.isArray`'s own lib.es5.d.ts type predicate narrows to `any[]`, not `unknown[]` -- this re-typed wrapper is what lets a `resolveCollection` stub return a properly `unknown[]`-typed result instead of an implicit `any[]`. */
|
|
32
|
+
function isUnknownArray(value) {
|
|
33
|
+
return Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
exports.expectDefinite = expectDefinite;
|
|
37
|
+
exports.expectIndeterminate = expectIndeterminate;
|
|
38
|
+
exports.isPlainRecord = isPlainRecord;
|
|
39
|
+
exports.isUnknownArray = isUnknownArray;
|
|
40
|
+
exports.resolvers = resolvers;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Evaluation } from "./evaluation.cjs";
|
|
2
|
+
import { Resolvers } from "./resolvers.cjs";
|
|
3
|
+
//#region src/evaluator-test-helpers.d.ts
|
|
4
|
+
/** A resolver reporting a fixed `{ kind: "number", value: 10, unit: { m: 1 } }` for key `"present"`, and not-found for anything else -- `resolveLookup`/`resolveCollection` are never exercised by expression-level nodes and are stubbed only to satisfy `Resolvers`. Shared across the split `evaluator.*.test.ts` files below; a test needing genuinely different resolver behaviour spreads over it (`{ ...resolvers, resolveValue: ... }`) locally rather than mutating this shared instance. */
|
|
5
|
+
declare const resolvers: Resolvers;
|
|
6
|
+
declare function expectDefinite<T>(evaluation: Evaluation<T>, expected: T): void;
|
|
7
|
+
declare function expectIndeterminate<T>(evaluation: Evaluation<T>, code: "not-found" | "wrong-type" | "domain-error"): void;
|
|
8
|
+
/** A narrowing type guard for a resolver context treated as a plain record, following this repo's `object -> Record<string, unknown>` narrowing convention rather than an `as Record<string, unknown>` assertion. */
|
|
9
|
+
declare function isPlainRecord(value: unknown): value is Record<string, unknown>;
|
|
10
|
+
/** `Array.isArray`'s own lib.es5.d.ts type predicate narrows to `any[]`, not `unknown[]` -- this re-typed wrapper is what lets a `resolveCollection` stub return a properly `unknown[]`-typed result instead of an implicit `any[]`. */
|
|
11
|
+
declare function isUnknownArray(value: unknown): value is unknown[];
|
|
12
|
+
//#endregion
|
|
13
|
+
export { expectDefinite, expectIndeterminate, isPlainRecord, isUnknownArray, resolvers };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Evaluation } from "./evaluation.js";
|
|
2
|
+
import { Resolvers } from "./resolvers.js";
|
|
3
|
+
//#region src/evaluator-test-helpers.d.ts
|
|
4
|
+
/** A resolver reporting a fixed `{ kind: "number", value: 10, unit: { m: 1 } }` for key `"present"`, and not-found for anything else -- `resolveLookup`/`resolveCollection` are never exercised by expression-level nodes and are stubbed only to satisfy `Resolvers`. Shared across the split `evaluator.*.test.ts` files below; a test needing genuinely different resolver behaviour spreads over it (`{ ...resolvers, resolveValue: ... }`) locally rather than mutating this shared instance. */
|
|
5
|
+
declare const resolvers: Resolvers;
|
|
6
|
+
declare function expectDefinite<T>(evaluation: Evaluation<T>, expected: T): void;
|
|
7
|
+
declare function expectIndeterminate<T>(evaluation: Evaluation<T>, code: "not-found" | "wrong-type" | "domain-error"): void;
|
|
8
|
+
/** A narrowing type guard for a resolver context treated as a plain record, following this repo's `object -> Record<string, unknown>` narrowing convention rather than an `as Record<string, unknown>` assertion. */
|
|
9
|
+
declare function isPlainRecord(value: unknown): value is Record<string, unknown>;
|
|
10
|
+
/** `Array.isArray`'s own lib.es5.d.ts type predicate narrows to `any[]`, not `unknown[]` -- this re-typed wrapper is what lets a `resolveCollection` stub return a properly `unknown[]`-typed result instead of an implicit `any[]`. */
|
|
11
|
+
declare function isUnknownArray(value: unknown): value is unknown[];
|
|
12
|
+
//#endregion
|
|
13
|
+
export { expectDefinite, expectIndeterminate, isPlainRecord, isUnknownArray, resolvers };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { t as globalExpect } from "./test.DNmyFkvJ-SCEEf0VK.js";
|
|
2
|
+
//#region src/evaluator-test-helpers.ts
|
|
3
|
+
/** A resolver reporting a fixed `{ kind: "number", value: 10, unit: { m: 1 } }` for key `"present"`, and not-found for anything else -- `resolveLookup`/`resolveCollection` are never exercised by expression-level nodes and are stubbed only to satisfy `Resolvers`. Shared across the split `evaluator.*.test.ts` files below; a test needing genuinely different resolver behaviour spreads over it (`{ ...resolvers, resolveValue: ... }`) locally rather than mutating this shared instance. */
|
|
4
|
+
const resolvers = {
|
|
5
|
+
resolveValue: async (key) => Promise.resolve(key === "present" ? {
|
|
6
|
+
found: true,
|
|
7
|
+
value: {
|
|
8
|
+
kind: "number",
|
|
9
|
+
value: 10,
|
|
10
|
+
unit: { m: 1 }
|
|
11
|
+
}
|
|
12
|
+
} : { found: false }),
|
|
13
|
+
resolveLookup: async () => Promise.resolve({ found: false }),
|
|
14
|
+
resolveCollection: async () => Promise.resolve([])
|
|
15
|
+
};
|
|
16
|
+
function expectDefinite(evaluation, expected) {
|
|
17
|
+
globalExpect(evaluation).toEqual({
|
|
18
|
+
status: "definite",
|
|
19
|
+
value: expected
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function expectIndeterminate(evaluation, code) {
|
|
23
|
+
globalExpect(evaluation.status).toBe("indeterminate");
|
|
24
|
+
if (evaluation.status === "indeterminate") globalExpect(evaluation.reason.code).toBe(code);
|
|
25
|
+
}
|
|
26
|
+
/** A narrowing type guard for a resolver context treated as a plain record, following this repo's `object -> Record<string, unknown>` narrowing convention rather than an `as Record<string, unknown>` assertion. */
|
|
27
|
+
function isPlainRecord(value) {
|
|
28
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
29
|
+
}
|
|
30
|
+
/** `Array.isArray`'s own lib.es5.d.ts type predicate narrows to `any[]`, not `unknown[]` -- this re-typed wrapper is what lets a `resolveCollection` stub return a properly `unknown[]`-typed result instead of an implicit `any[]`. */
|
|
31
|
+
function isUnknownArray(value) {
|
|
32
|
+
return Array.isArray(value);
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
export { expectDefinite, expectIndeterminate, isPlainRecord, isUnknownArray, resolvers };
|