@dancrumb/cant-shared 0.0.0-stage → 0.10.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,376 @@
1
+ import type { BinaryOperator, Cardinality } from './ast.ts';
2
+ import type { BaseValue, Value } from './values.ts';
3
+ /**
4
+ * A compiled program: what the compiler gives the interpreter, with every name resolved and
5
+ * every runtime decision that depends on a type made explicit (docs/adr/0039-compiled-programs.md).
6
+ * It is plain data, so it can be written to and read from JSON.
7
+ */
8
+ export interface CompiledProgram {
9
+ format: typeof COMPILED_FORMAT;
10
+ /** Identifies this build, so that its source map can be matched with it. */
11
+ build: string;
12
+ entry: ModuleId;
13
+ types: Record<SymbolId, CompiledType>;
14
+ actions: Record<SymbolId, CompiledAction>;
15
+ constants: Record<SymbolId, Value>;
16
+ relations: CompiledRelation[];
17
+ permissions: CompiledPermissions[];
18
+ /** The entry module's triggers, the only ones that take effect. */
19
+ triggers: CompiledTrigger[];
20
+ }
21
+ /** The version of the compiled program's structure, which the interpreter checks. */
22
+ export declare const COMPILED_FORMAT = 1;
23
+ /** The source of each module, kept apart from the compiled program (docs/adr/0039-compiled-programs.md). */
24
+ export interface SourceMap {
25
+ format: typeof COMPILED_FORMAT;
26
+ build: string;
27
+ sources: Record<ModuleId, string>;
28
+ }
29
+ /** A module: its file's path relative to the program's root, such as `lib/payments.cant`, or `cant::IO`. */
30
+ export type ModuleId = string;
31
+ /** A declaration: its module, `#`, and its name, such as `lib/payments.cant#ChargeCard`. */
32
+ export type SymbolId = string;
33
+ /** Where something is in a module, with 1-based lines and columns. */
34
+ export interface SourcePosition {
35
+ module: ModuleId;
36
+ start: {
37
+ line: number;
38
+ column: number;
39
+ };
40
+ end: {
41
+ line: number;
42
+ column: number;
43
+ };
44
+ }
45
+ export type CompiledTypeRef = {
46
+ kind: 'base';
47
+ name: 'TEXT' | 'NUMBER' | 'BOOLEAN' | 'DATE_TIME' | 'DURATION' | 'FILE';
48
+ } | {
49
+ kind: 'unknown';
50
+ }
51
+ /** The element type of an empty collection whose type nothing decides. */
52
+ | {
53
+ kind: 'nothing';
54
+ } | {
55
+ kind: 'named';
56
+ symbol: SymbolId;
57
+ } | {
58
+ kind: 'enum';
59
+ values: BaseValue[];
60
+ } | {
61
+ kind: 'array' | 'set';
62
+ element: CompiledTypeRef;
63
+ } | {
64
+ kind: 'map';
65
+ value: CompiledTypeRef;
66
+ } | {
67
+ kind: 'optional';
68
+ inner: CompiledTypeRef;
69
+ }
70
+ /** An anonymous model's type, from an object value. */
71
+ | {
72
+ kind: 'object';
73
+ fields: Record<string, CompiledTypeRef>;
74
+ }
75
+ /** `ERROR`, any error, on the right of `instanceOf`. */
76
+ | {
77
+ kind: 'anyError';
78
+ }
79
+ /** The type of `@subject` before it is narrowed. */
80
+ | {
81
+ kind: 'subject';
82
+ };
83
+ export type CompiledType = CompiledEnum | CompiledModel | CompiledError | CompiledEntity;
84
+ export interface CompiledEnum {
85
+ kind: 'enum';
86
+ members: [name: string, value: BaseValue][];
87
+ }
88
+ export interface CompiledField {
89
+ name: string;
90
+ type: CompiledTypeRef;
91
+ optional: boolean;
92
+ default?: CompiledExpression;
93
+ }
94
+ export interface CompiledModel {
95
+ kind: 'model';
96
+ fields: CompiledField[];
97
+ /** The order to work out defaults in, as indexes into `fields`. */
98
+ defaultOrder: number[];
99
+ }
100
+ export interface CompiledError {
101
+ kind: 'error';
102
+ data?: {
103
+ model: SymbolId;
104
+ } | {
105
+ fields: CompiledField[];
106
+ defaultOrder: number[];
107
+ };
108
+ }
109
+ export interface CompiledEntity {
110
+ kind: 'entity';
111
+ model: SymbolId;
112
+ /** The fields a program sees, by their names, each naming the model's field it holds. */
113
+ exposed: Record<string, string>;
114
+ identity: ({
115
+ field: string;
116
+ } | {
117
+ navigation: string;
118
+ })[];
119
+ navigation: Record<string, CompiledNavigationField>;
120
+ subject: boolean;
121
+ group?: {
122
+ kind: string;
123
+ roles: string[];
124
+ };
125
+ membership?: {
126
+ member: string;
127
+ group: string;
128
+ roles?: string;
129
+ };
130
+ }
131
+ export interface CompiledNavigationField {
132
+ entity: SymbolId;
133
+ cardinality: Cardinality;
134
+ inverse: string;
135
+ /** An index into `relations`. */
136
+ relation: number;
137
+ }
138
+ export interface CompiledRelationEnd {
139
+ entity: SymbolId;
140
+ field: string;
141
+ cardinality: Cardinality;
142
+ }
143
+ export interface CompiledRelation {
144
+ left: CompiledRelationEnd;
145
+ right: CompiledRelationEnd;
146
+ }
147
+ export interface CompiledPermissions {
148
+ group: string;
149
+ role?: string;
150
+ permissions: string[];
151
+ }
152
+ export interface CompiledParameter {
153
+ name: string;
154
+ type: CompiledTypeRef;
155
+ optional: boolean;
156
+ default?: CompiledExpression;
157
+ }
158
+ export interface CompiledAction {
159
+ parameters: CompiledParameter[];
160
+ returns: CompiledTypeRef | {
161
+ kind: 'void';
162
+ };
163
+ /** Whether the return type is marked `?`. */
164
+ optional: boolean;
165
+ raises: SymbolId[];
166
+ passes: SymbolId[] | 'all';
167
+ /** The order to work out defaults in, as indexes into `parameters`. */
168
+ defaultOrder: number[];
169
+ implementation: {
170
+ kind: 'body';
171
+ locals: number;
172
+ body: CompiledStatement[];
173
+ }
174
+ /** A native module's path, relative to the program's root. */
175
+ | {
176
+ kind: 'native';
177
+ module: string;
178
+ }
179
+ /** Provided by the interpreter, as the standard library is. */
180
+ | {
181
+ kind: 'standard';
182
+ };
183
+ position?: SourcePosition;
184
+ }
185
+ export interface CompiledAssertion {
186
+ test: CompiledExpression;
187
+ message: CompiledExpression;
188
+ }
189
+ export interface CompiledTrigger {
190
+ action: SymbolId;
191
+ source: string;
192
+ parameters: Record<string, Value>;
193
+ /** A test case's subject: an entity, and its model's fields (docs/adr/0037-test-case-trigger-source.md). */
194
+ subject?: {
195
+ entity: SymbolId;
196
+ fields: Value;
197
+ };
198
+ /** Its arguments, by parameter name. Their code has no locals. */
199
+ arguments: Record<string, CompiledExpression>;
200
+ auth?: CompiledExpression;
201
+ assertions: CompiledAssertion[];
202
+ position: SourcePosition;
203
+ }
204
+ interface At {
205
+ position: SourcePosition;
206
+ }
207
+ /** An argument left out of a call, so the parameter's default is used, or it is left empty. */
208
+ export interface OmittedArgument {
209
+ type: 'omitted';
210
+ }
211
+ export type CompiledExpression = At & ({
212
+ type: 'value';
213
+ value: Value;
214
+ } | {
215
+ type: 'constant';
216
+ symbol: SymbolId;
217
+ } | {
218
+ type: 'local';
219
+ slot: number;
220
+ }
221
+ /** Another field of the object whose default is being worked out. */
222
+ | {
223
+ type: 'field';
224
+ name: string;
225
+ } | {
226
+ type: 'builtIn';
227
+ name: string;
228
+ } | {
229
+ type: 'template';
230
+ parts: (string | CompiledExpression)[];
231
+ } | {
232
+ type: 'array' | 'set';
233
+ element: CompiledTypeRef;
234
+ elements: CompiledExpression[];
235
+ } | {
236
+ type: 'map';
237
+ value: CompiledTypeRef;
238
+ members: [string, CompiledExpression][];
239
+ } | {
240
+ type: 'object';
241
+ members: [string, CompiledExpression][];
242
+ }
243
+ /** Make an object value into an object of a model, filling in its defaults. */
244
+ | {
245
+ type: 'construct';
246
+ model: SymbolId;
247
+ value: CompiledExpression;
248
+ }
249
+ /** Use an object value as a map. */
250
+ | {
251
+ type: 'toMap';
252
+ value: CompiledExpression;
253
+ }
254
+ /** Check a value against an enum's values, raising InvalidEnumValue if it isn't one. */
255
+ | {
256
+ type: 'toEnum';
257
+ enum: CompiledTypeRef;
258
+ value: CompiledExpression;
259
+ }
260
+ /** Use the value of a result that may hold an error, panicking if it does. */
261
+ | {
262
+ type: 'unwrap';
263
+ value: CompiledExpression;
264
+ } | {
265
+ type: 'member';
266
+ object: CompiledExpression;
267
+ name: string;
268
+ } | {
269
+ type: 'index';
270
+ object: CompiledExpression;
271
+ index: CompiledExpression;
272
+ } | {
273
+ type: 'call';
274
+ action: SymbolId;
275
+ arguments: (CompiledExpression | OmittedArgument)[];
276
+ }
277
+ /** A call to a standard library action, whose signature isn't written down yet, with its arguments as written. */
278
+ | {
279
+ type: 'callStandard';
280
+ action: SymbolId;
281
+ arguments: {
282
+ name?: string;
283
+ value: CompiledExpression;
284
+ }[];
285
+ } | {
286
+ type: 'unary';
287
+ operator: '!' | '-' | 'isEmpty' | 'ignore';
288
+ argument: CompiledExpression;
289
+ } | {
290
+ type: 'binary';
291
+ operator: BinaryOperator;
292
+ left: CompiledExpression;
293
+ right: CompiledExpression;
294
+ } | {
295
+ type: 'instanceOf';
296
+ value: CompiledExpression;
297
+ target: CompiledTypeRef;
298
+ } | {
299
+ type: 'can';
300
+ permission: string;
301
+ group?: CompiledExpression;
302
+ }
303
+ /** `@subject is <entity>`, `@subject is @anonymous` or `@subject is @defined`. */
304
+ | {
305
+ type: 'is';
306
+ target: {
307
+ entity: SymbolId;
308
+ } | {
309
+ builtIn: 'anonymous' | 'defined';
310
+ };
311
+ } | {
312
+ type: 'hasRole';
313
+ role: string;
314
+ group: CompiledExpression;
315
+ });
316
+ /** Where an assignment or `delete` puts its value: a member or an element. */
317
+ export type CompiledTarget = Extract<CompiledExpression, {
318
+ type: 'member' | 'index';
319
+ }>;
320
+ export type CompiledStatement = At & ({
321
+ type: 'expression';
322
+ expression: CompiledExpression;
323
+ }
324
+ /** Give a local its value. */
325
+ | {
326
+ type: 'let';
327
+ slot: number;
328
+ value: CompiledExpression;
329
+ }
330
+ /** `ignored` when the statement is under `ignore`, so the errors it can raise are ignored. */
331
+ | {
332
+ type: 'assign';
333
+ target: CompiledTarget;
334
+ value: CompiledExpression;
335
+ ignored: boolean;
336
+ } | {
337
+ type: 'delete';
338
+ target: CompiledTarget;
339
+ ignored: boolean;
340
+ } | {
341
+ type: 'return';
342
+ value?: CompiledExpression | {
343
+ type: 'nothing';
344
+ };
345
+ } | {
346
+ type: 'raise';
347
+ error: SymbolId;
348
+ data?: CompiledExpression;
349
+ } | {
350
+ type: 'pass';
351
+ value: CompiledExpression;
352
+ } | {
353
+ type: 'assert';
354
+ test: CompiledExpression;
355
+ message?: CompiledExpression;
356
+ } | {
357
+ type: 'break';
358
+ } | {
359
+ type: 'continue';
360
+ } | {
361
+ type: 'for';
362
+ slot: number;
363
+ iterable: CompiledExpression;
364
+ body: CompiledStatement[];
365
+ } | {
366
+ type: 'while';
367
+ test: CompiledExpression;
368
+ body: CompiledStatement[];
369
+ } | {
370
+ type: 'if';
371
+ test: CompiledExpression;
372
+ then: CompiledStatement[];
373
+ else?: CompiledStatement[];
374
+ });
375
+ export {};
376
+ //# sourceMappingURL=compiled.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compiled.d.ts","sourceRoot":"","sources":["../src/compiled.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,OAAO,eAAe,CAAC;IAC/B,4EAA4E;IAC5E,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,QAAQ,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;IACtC,OAAO,EAAE,MAAM,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;IAC1C,SAAS,EAAE,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACnC,SAAS,EAAE,gBAAgB,EAAE,CAAC;IAC9B,WAAW,EAAE,mBAAmB,EAAE,CAAC;IACnC,mEAAmE;IACnE,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC7B;AAED,qFAAqF;AACrF,eAAO,MAAM,eAAe,IAAI,CAAC;AAEjC,4GAA4G;AAC5G,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,OAAO,eAAe,CAAC;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;CACnC;AAED,4GAA4G;A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AE,iBAAiB,EAAE,CAAA;CAAE,GACtE;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,kBAAkB,CAAC;IAAC,IAAI,EAAE,iBAAiB,EAAE,CAAC;IAAC,IAAI,CAAC,EAAE,iBAAiB,EAAE,CAAA;CAAE,CAClG,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** The version of the compiled program's structure, which the interpreter checks. */
2
+ export const COMPILED_FORMAT = 1;
3
+ //# sourceMappingURL=compiled.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compiled.js","sourceRoot":"","sources":["../src/compiled.ts"],"names":[],"mappings":"AAsBA,qFAAqF;AACrF,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAC"}
@@ -0,0 +1,48 @@
1
+ import type { Position } from 'unist';
2
+ import type { Expression, Identifier, Name } from './ast.ts';
3
+ import { type Value } from './values.ts';
4
+ /**
5
+ * What the names in an expression mean. The compiler and the interpreter each provide one, from
6
+ * their own view of the program: the compiler from its symbol tables, the interpreter from a
7
+ * compiled program (docs/adr/0039-compiled-programs.md).
8
+ */
9
+ export interface EvaluationScope {
10
+ /** The value a name has, or `undefined` if it isn't known before the program runs. */
11
+ value(name: Name): Value | undefined;
12
+ /** The value of `member` of the enum `object` names, or `undefined` if `object` doesn't name an enum. */
13
+ enumMember(object: Name, member: Identifier): Value | undefined;
14
+ }
15
+ /** A built-in error that an expression raises (docs/adr/0019-operators.md, 0023). */
16
+ export type RaisedError = 'DivisionByZero' | 'FractionalModulo';
17
+ /** What evaluating an expression gives. */
18
+ export type Evaluation = {
19
+ kind: 'value';
20
+ value: Value;
21
+ }
22
+ /** It raises a built-in error, at `position`. */
23
+ | {
24
+ kind: 'error';
25
+ error: RaisedError;
26
+ position: Position;
27
+ }
28
+ /** It panics, at `position` (docs/adr/0023-error-handling.md). */
29
+ | {
30
+ kind: 'panic';
31
+ message: string;
32
+ position: Position;
33
+ }
34
+ /** `expression` isn't a constant expression, so its value isn't known (docs/adr/0023-error-handling.md). */
35
+ | {
36
+ kind: 'notConstant';
37
+ expression: Expression;
38
+ };
39
+ /**
40
+ * Evaluate a constant expression (docs/adr/0023-error-handling.md, 0034): literals, strings,
41
+ * enum members, constants, objects, and the operators on them (docs/adr/0019-operators.md,
42
+ * 0026). Anything else, such as a call or a collection, is not a constant expression.
43
+ *
44
+ * The compiler has already checked the expression's types, so an operand of the wrong type ends
45
+ * evaluation as not constant rather than being reported again.
46
+ */
47
+ export declare function evaluate(expression: Expression, scope: EvaluationScope): Evaluation;
48
+ //# sourceMappingURL=evaluate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluate.d.ts","sourceRoot":"","sources":["../src/evaluate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,KAAK,EAAoB,UAAU,EAAE,UAAU,EAAE,IAAI,EAAmB,MAAM,UAAU,CAAC;AAChG,OAAO,EAWL,KAAK,KAAK,EACX,MAAM,aAAa,CAAC;AAErB;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,sFAAsF;IACtF,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,KAAK,GAAG,SAAS,CAAC;IACrC,yGAAyG;IACzG,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,GAAG,KAAK,GAAG,SAAS,CAAC;CACjE;AAED,qFAAqF;AACrF,MAAM,MAAM,WAAW,GAAG,gBAAgB,GAAG,kBAAkB,CAAC;AAEhE,2CAA2C;AAC3C,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE;AACjC,iDAAiD;GAC/C;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,WAAW,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE;AAC3D,kEAAkE;GAChE;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE;AACxD,4GAA4G;GAC1G;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,UAAU,EAAE,UAAU,CAAA;CAAE,CAAC;AAWpD;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,eAAe,GAAG,UAAU,CAOnF"}
@@ -0,0 +1,224 @@
1
+ import { boolean, compare, DATE_TIME_LIMIT, dateTimeMilliseconds, durationMilliseconds, equal, number, text, toText, } from './values.js';
2
+ /** A value the caller can't work with, such as an operand of the wrong type, ends evaluation. */
3
+ class Stop extends Error {
4
+ result;
5
+ constructor(result) {
6
+ super(result.kind);
7
+ this.result = result;
8
+ }
9
+ }
10
+ /**
11
+ * Evaluate a constant expression (docs/adr/0023-error-handling.md, 0034): literals, strings,
12
+ * enum members, constants, objects, and the operators on them (docs/adr/0019-operators.md,
13
+ * 0026). Anything else, such as a call or a collection, is not a constant expression.
14
+ *
15
+ * The compiler has already checked the expression's types, so an operand of the wrong type ends
16
+ * evaluation as not constant rather than being reported again.
17
+ */
18
+ export function evaluate(expression, scope) {
19
+ try {
20
+ return { kind: 'value', value: new Evaluator(scope).value(expression) };
21
+ }
22
+ catch (stop) {
23
+ if (stop instanceof Stop)
24
+ return stop.result;
25
+ throw stop;
26
+ }
27
+ }
28
+ class Evaluator {
29
+ scope;
30
+ constructor(scope) {
31
+ this.scope = scope;
32
+ }
33
+ value(expression) {
34
+ switch (expression.type) {
35
+ case 'number':
36
+ return number(expression.value);
37
+ case 'boolean':
38
+ return boolean(expression.value);
39
+ case 'dateTime':
40
+ return { kind: 'dateTime', value: dateTimeMilliseconds(expression.value) };
41
+ case 'duration':
42
+ return { kind: 'duration', value: durationMilliseconds(expression.value) };
43
+ case 'string':
44
+ return text(expression.parts.map((part) => (part.type === 'text' ? part.value : toText(this.value(part.expression)))).join(''));
45
+ case 'identifier':
46
+ case 'qualifiedIdentifier':
47
+ return this.scope.value(expression) ?? notConstant(expression);
48
+ case 'object': {
49
+ const members = [];
50
+ for (const item of expression.items) {
51
+ // A built-in name, such as `@entity` in a test case's subject, names a type, not a
52
+ // value (docs/adr/0037-test-case-trigger-source.md), so it isn't a member.
53
+ if (item.name.type === 'builtIn')
54
+ continue;
55
+ const name = item.name.type === 'identifier' ? item.name.value : this.text(item.name);
56
+ members.push([name, this.value(item.value)]);
57
+ }
58
+ return { kind: 'object', members };
59
+ }
60
+ case 'member': {
61
+ const object = expression.object;
62
+ if (object.type === 'identifier' || object.type === 'qualifiedIdentifier') {
63
+ const member = this.scope.enumMember(object, expression.property);
64
+ if (member !== undefined)
65
+ return member;
66
+ }
67
+ return this.member(this.value(object), expression.property.value, expression);
68
+ }
69
+ case 'index': {
70
+ const object = this.value(expression.object);
71
+ const index = this.value(expression.index);
72
+ return index.kind === 'text' ? this.member(object, index.value, expression) : notConstant(expression);
73
+ }
74
+ case 'unary':
75
+ return this.unary(expression);
76
+ case 'binary':
77
+ return this.binary(expression);
78
+ default:
79
+ return notConstant(expression);
80
+ }
81
+ }
82
+ text(expression) {
83
+ const value = this.value(expression);
84
+ return value.kind === 'text' ? value.value : notConstant(expression);
85
+ }
86
+ member(object, name, expression) {
87
+ if (object.kind !== 'object')
88
+ return notConstant(expression);
89
+ return object.members.find(([member]) => member === name)?.[1] ?? notConstant(expression);
90
+ }
91
+ unary(expression) {
92
+ const argument = this.base(expression.argument);
93
+ switch (expression.operator) {
94
+ case '!':
95
+ return argument.kind === 'boolean' ? boolean(!argument.value) : notConstant(expression);
96
+ case '-':
97
+ if (argument.kind === 'number')
98
+ return number(-argument.value);
99
+ if (argument.kind === 'duration')
100
+ return { kind: 'duration', value: 0 - argument.value };
101
+ return notConstant(expression);
102
+ default:
103
+ // `isEmpty` and `ignore` need optionals and results, which constants never are.
104
+ return notConstant(expression);
105
+ }
106
+ }
107
+ binary(expression) {
108
+ const { operator } = expression;
109
+ if (operator === 'and' || operator === 'or') {
110
+ // Short-circuiting: the right-hand side is only evaluated when it decides the result.
111
+ const left = this.boolean(expression.left);
112
+ if (operator === 'and' ? !left : left)
113
+ return boolean(left);
114
+ return boolean(this.boolean(expression.right));
115
+ }
116
+ if (operator === 'orElse' || operator === 'in')
117
+ return notConstant(expression);
118
+ const leftValue = this.value(expression.left);
119
+ const rightValue = this.value(expression.right);
120
+ if (operator === '==')
121
+ return boolean(equal(leftValue, rightValue));
122
+ if (operator === '!=')
123
+ return boolean(!equal(leftValue, rightValue));
124
+ if (operator === '<' || operator === '<=' || operator === '>' || operator === '>=') {
125
+ const order = compare(leftValue, rightValue);
126
+ if (order === undefined)
127
+ return notConstant(expression);
128
+ return boolean(operator === '<' ? order < 0 : operator === '<=' ? order <= 0 : operator === '>' ? order > 0 : order >= 0);
129
+ }
130
+ // Arithmetic acts on enum members' values (docs/adr/0019-operators.md).
131
+ const left = baseOf(leftValue) ?? notConstant(expression.left);
132
+ const right = baseOf(rightValue) ?? notConstant(expression.right);
133
+ return this.arithmetic(expression, left, right);
134
+ }
135
+ /** The arithmetic operators, on numbers, and on dates and durations (docs/adr/0019-operators.md, 0026). */
136
+ arithmetic(expression, left, right) {
137
+ const { operator } = expression;
138
+ if (left.kind === 'number' && right.kind === 'number') {
139
+ switch (operator) {
140
+ case '+':
141
+ return this.finite(left.value + right.value, expression);
142
+ case '-':
143
+ return this.finite(left.value - right.value, expression);
144
+ case '*':
145
+ return this.finite(left.value * right.value, expression);
146
+ case '**':
147
+ return this.finite(left.value ** right.value, expression);
148
+ case '/':
149
+ if (right.value === 0)
150
+ return raise('DivisionByZero', expression);
151
+ return this.finite(left.value / right.value, expression);
152
+ case '%':
153
+ if (right.value === 0)
154
+ return raise('DivisionByZero', expression);
155
+ if (!Number.isInteger(left.value) || !Number.isInteger(right.value))
156
+ return raise('FractionalModulo', expression);
157
+ // A mathematical modulo: the result has the sign of the right operand.
158
+ return number((((left.value % right.value) + right.value) % right.value) + 0);
159
+ default:
160
+ return notConstant(expression);
161
+ }
162
+ }
163
+ const duration = (milliseconds) => ({ kind: 'duration', value: Math.trunc(milliseconds) + 0 });
164
+ const dateTime = (milliseconds) => {
165
+ if (Math.abs(milliseconds) > DATE_TIME_LIMIT) {
166
+ panic('The result is outside the range of instants a DATE_TIME can hold.', expression);
167
+ }
168
+ return { kind: 'dateTime', value: milliseconds };
169
+ };
170
+ switch (`${left.kind} ${operator} ${right.kind}`) {
171
+ case 'dateTime + duration':
172
+ return dateTime(left.value + right.value);
173
+ case 'duration + dateTime':
174
+ return dateTime(left.value + right.value);
175
+ case 'dateTime - duration':
176
+ return dateTime(left.value - right.value);
177
+ case 'dateTime - dateTime':
178
+ return duration(left.value - right.value);
179
+ case 'duration + duration':
180
+ return duration(left.value + right.value);
181
+ case 'duration - duration':
182
+ return duration(left.value - right.value);
183
+ case 'duration * number':
184
+ return duration(left.value * right.value);
185
+ case 'number * duration':
186
+ return duration(left.value * right.value);
187
+ case 'duration / number':
188
+ if (right.value === 0)
189
+ return raise('DivisionByZero', expression);
190
+ return duration(left.value / right.value);
191
+ case 'duration / duration':
192
+ if (right.value === 0)
193
+ return raise('DivisionByZero', expression);
194
+ return number(left.value / right.value);
195
+ default:
196
+ return notConstant(expression);
197
+ }
198
+ }
199
+ /** Cant has no `Infinity` or `NaN` (docs/adr/0019-operators.md), so a result that would be one panics. */
200
+ finite(value, expression) {
201
+ if (!Number.isFinite(value))
202
+ panic('The result is not a number Cant can hold: it is too large, or not a real number.', expression);
203
+ return number(value + 0);
204
+ }
205
+ base(expression) {
206
+ return baseOf(this.value(expression)) ?? notConstant(expression);
207
+ }
208
+ boolean(expression) {
209
+ const value = this.base(expression);
210
+ return value.kind === 'boolean' ? value.value : notConstant(expression);
211
+ }
212
+ }
213
+ /** A base value, or an enum member's value, which operators act on. */
214
+ const baseOf = (value) => value.kind === 'object' ? undefined : value.kind === 'enum' ? value.value : value;
215
+ const notConstant = (expression) => {
216
+ throw new Stop({ kind: 'notConstant', expression });
217
+ };
218
+ const raise = (error, expression) => {
219
+ throw new Stop({ kind: 'error', error, position: expression.position });
220
+ };
221
+ const panic = (message, expression) => {
222
+ throw new Stop({ kind: 'panic', message, position: expression.position });
223
+ };
224
+ //# sourceMappingURL=evaluate.js.map