@nova-energy/openamx 0.21.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -4
- package/dist/editor/highlighting.js +9 -0
- package/dist/parser/parseDocument.js +26 -13
- package/dist/parser/parseExpression.js +23 -5
- package/dist/renderer/diagramBrowserRuntime.js +1 -1
- package/dist/runtime/complex.js +131 -0
- package/dist/runtime/dateTime.js +232 -0
- package/dist/runtime/environment.js +9 -0
- package/dist/runtime/evaluateDocument.js +9 -14
- package/dist/runtime/evaluateExpression.js +175 -27
- package/dist/runtime/inputData.js +3 -13
- package/dist/runtime/moduleLoader.js +23 -11
- package/dist/runtime/outputData.js +18 -2
- package/dist/runtime/standardLibrary.js +79 -0
- package/dist/runtime/typescriptBridge.js +171 -0
- package/dist/runtime/valueSerialization.js +3 -0
- package/dist/transpiler/emit.js +192 -14
- package/dist/transpiler/graph.js +4 -3
- package/dist/transpilerRuntime.js +20 -14
- package/dist/typechecker/checkDocument.js +151 -24
- package/dist/typechecker/typescriptAnalysis.js +372 -0
- package/package.json +4 -2
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { inputError, staticError, throwInputErrors, throwInvalidLoopIterable, throwInvalidRangeBounds, throwInvalidReturnContext, throwFunctionFallthrough, throwListOperationError, throwMeasurementDomainError } from '../diagnostics/errors';
|
|
2
2
|
import { Environment } from './environment';
|
|
3
3
|
import { evaluateStandardLibraryCall, isMathematicalFunction } from './standardLibrary';
|
|
4
|
+
import { isValidDateTime } from './dateTime';
|
|
4
5
|
import { fieldRegistry } from '../typechecker/declarationRegistry';
|
|
5
6
|
import { canonicalUnit, composeUnits, isAffineUnit, isMeasurement, measurement, measurementDisplayValue, measurementPhysicalValue, powerUnit, sqrtMeasurement, unitFromMetadata } from './measurement';
|
|
7
|
+
import { complexBinary, complexUnary, createComplex, isComplex } from './complex';
|
|
6
8
|
import { createDiagramModel } from './diagramModel';
|
|
7
|
-
function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
9
|
+
export function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
8
10
|
const diagnostics = [];
|
|
11
|
+
const activeValues = new WeakSet();
|
|
9
12
|
const report = (message, target, actual, dataPath, fieldSource) => {
|
|
10
13
|
const diagnostic = inputError('AMX4003', message, {
|
|
11
14
|
file,
|
|
@@ -32,11 +35,72 @@ function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
|
32
35
|
return;
|
|
33
36
|
}
|
|
34
37
|
if (target.kind === 'list') {
|
|
35
|
-
|
|
38
|
+
let isArray;
|
|
39
|
+
try {
|
|
40
|
+
isArray = Array.isArray(actual);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
report('Expected a list value', target, actual, dataPath, fieldSource);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (!isArray) {
|
|
36
47
|
report('Expected a list value', target, actual, dataPath, fieldSource);
|
|
37
48
|
return;
|
|
38
49
|
}
|
|
39
|
-
|
|
50
|
+
const array = actual;
|
|
51
|
+
let prototype;
|
|
52
|
+
try {
|
|
53
|
+
prototype = Object.getPrototypeOf(array);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
report('Expected a plain list value', target, actual, dataPath, fieldSource);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (prototype !== Array.prototype) {
|
|
60
|
+
report('Expected a plain list value', target, actual, dataPath, fieldSource);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (activeValues.has(array)) {
|
|
64
|
+
report('Cyclic values are not supported', target, actual, dataPath, fieldSource);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
activeValues.add(array);
|
|
68
|
+
try {
|
|
69
|
+
for (let index = 0; index < array.length; index++) {
|
|
70
|
+
let descriptor;
|
|
71
|
+
try {
|
|
72
|
+
descriptor = Object.getOwnPropertyDescriptor(array, String(index));
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
report('Expected a plain list value', target, actual, `${dataPath}[${index}]`, fieldSource);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (!descriptor || !('value' in descriptor) || descriptor.enumerable !== true) {
|
|
79
|
+
report('Expected a dense data list', target.element, descriptor ? 'accessor' : 'missing', `${dataPath}[${index}]`, fieldSource);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
visit(descriptor.value, target.element, `${dataPath}[${index}]`, fieldSource);
|
|
83
|
+
}
|
|
84
|
+
let keys;
|
|
85
|
+
try {
|
|
86
|
+
keys = Reflect.ownKeys(array);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
report('Expected a plain list value', target, actual, dataPath, fieldSource);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
for (const key of keys) {
|
|
93
|
+
if (key === 'length')
|
|
94
|
+
continue;
|
|
95
|
+
const index = typeof key === 'string' ? Number(key) : Number.NaN;
|
|
96
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= array.length || String(index) !== key) {
|
|
97
|
+
report('Unsupported list property', target, actual, `${dataPath}.${String(key)}`, fieldSource);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
activeValues.delete(array);
|
|
103
|
+
}
|
|
40
104
|
return;
|
|
41
105
|
}
|
|
42
106
|
if (target.kind === 'measurement') {
|
|
@@ -61,10 +125,15 @@ function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
|
61
125
|
report('Expected a finite Number value', target, actual, dataPath, fieldSource);
|
|
62
126
|
return;
|
|
63
127
|
}
|
|
128
|
+
if (target.name === 'Complex') {
|
|
129
|
+
if (!isComplex(actual))
|
|
130
|
+
report('Expected a finite Complex value', target, actual, dataPath, fieldSource);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
64
133
|
if (target.name === 'String' || target.name === 'DateTime') {
|
|
65
134
|
if (typeof actual !== 'string')
|
|
66
135
|
report(`Expected a ${target.name} value`, target, actual, dataPath, fieldSource);
|
|
67
|
-
else if (target.name === 'DateTime' && !
|
|
136
|
+
else if (target.name === 'DateTime' && !isValidDateTime(actual))
|
|
68
137
|
report('Invalid RFC 3339 DateTime value', target, actual, dataPath, fieldSource);
|
|
69
138
|
return;
|
|
70
139
|
}
|
|
@@ -73,24 +142,92 @@ function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
|
73
142
|
report('Expected a Boolean value', target, actual, dataPath, fieldSource);
|
|
74
143
|
return;
|
|
75
144
|
}
|
|
145
|
+
const enumeration = env.enumDeclarations.get(target.name);
|
|
146
|
+
if (enumeration) {
|
|
147
|
+
const values = enumeration.resolvedValues ?? [];
|
|
148
|
+
if ((typeof actual !== 'number' && typeof actual !== 'string') || !values.includes(actual)) {
|
|
149
|
+
report(`Expected a member value of enum '${target.name}'`, target, actual, dataPath, fieldSource);
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
76
153
|
const declaration = env.recordTypes.get(target.name);
|
|
77
|
-
|
|
154
|
+
let isArray;
|
|
155
|
+
try {
|
|
156
|
+
isArray = Array.isArray(actual);
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
report(`Expected a plain ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (!declaration || typeof actual !== 'object' || actual === null || isArray) {
|
|
78
163
|
report(`Expected a ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
79
164
|
return;
|
|
80
165
|
}
|
|
166
|
+
let prototype;
|
|
167
|
+
try {
|
|
168
|
+
prototype = Object.getPrototypeOf(actual);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
report(`Expected a plain ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
175
|
+
report(`Expected a plain ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (activeValues.has(actual)) {
|
|
179
|
+
report('Cyclic values are not supported', target, actual, dataPath, fieldSource);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
activeValues.add(actual);
|
|
81
183
|
const record = actual;
|
|
82
184
|
const fieldNames = new Set(declaration.fields.map(field => field.name));
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
185
|
+
try {
|
|
186
|
+
let keys;
|
|
187
|
+
try {
|
|
188
|
+
keys = Reflect.ownKeys(record);
|
|
87
189
|
}
|
|
88
|
-
|
|
89
|
-
|
|
190
|
+
catch {
|
|
191
|
+
report(`Expected a plain ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
for (const field of declaration.fields) {
|
|
195
|
+
const fieldScope = fieldRegistry(field, env.dimensionRegistry);
|
|
196
|
+
let descriptor;
|
|
197
|
+
try {
|
|
198
|
+
descriptor = Object.getOwnPropertyDescriptor(record, field.name);
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
report(`Expected a plain data field '${field.name}'`, checkedType(field.annotation, fieldScope), 'accessor', `${dataPath}.${field.name}`, field.source);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (!descriptor) {
|
|
205
|
+
report(`Missing computed field '${field.name}'`, checkedType(field.annotation, fieldScope), 'missing', `${dataPath}.${field.name}`, field.source);
|
|
206
|
+
}
|
|
207
|
+
else if (!('value' in descriptor) || descriptor.enumerable !== true) {
|
|
208
|
+
report(`Expected a plain data field '${field.name}'`, checkedType(field.annotation, fieldScope), 'accessor', `${dataPath}.${field.name}`, field.source);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
visit(descriptor.value, checkedType(field.annotation, fieldScope), `${dataPath}.${field.name}`, field.source);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
for (const key of keys) {
|
|
215
|
+
if (typeof key !== 'string' || !fieldNames.has(key)) {
|
|
216
|
+
let actualValue = 'unsupported property';
|
|
217
|
+
try {
|
|
218
|
+
const descriptor = Object.getOwnPropertyDescriptor(record, key);
|
|
219
|
+
if (descriptor && 'value' in descriptor)
|
|
220
|
+
actualValue = descriptor.value;
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
actualValue = 'unavailable property';
|
|
224
|
+
}
|
|
225
|
+
report(`Unknown computed field '${String(key)}'`, target, actualValue, `${dataPath}.${String(key)}`, fieldSource);
|
|
226
|
+
}
|
|
90
227
|
}
|
|
91
228
|
}
|
|
92
|
-
|
|
93
|
-
|
|
229
|
+
finally {
|
|
230
|
+
activeValues.delete(actual);
|
|
94
231
|
}
|
|
95
232
|
};
|
|
96
233
|
visit(value, expected, valuePath);
|
|
@@ -143,17 +280,6 @@ function sameMeasurementVector(value, vector) {
|
|
|
143
280
|
return Object.keys(value.unit.vector).length === vector.size
|
|
144
281
|
&& [...vector].every(([identity, exponent]) => value.unit.vector[identity] === exponent);
|
|
145
282
|
}
|
|
146
|
-
function validDateTime(value) {
|
|
147
|
-
const parts = value.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(Z|[+-]\d{2}:\d{2})$/);
|
|
148
|
-
if (!parts)
|
|
149
|
-
return false;
|
|
150
|
-
const [, year, month, day, hour, minute, second, offset] = parts;
|
|
151
|
-
const date = new Date(0);
|
|
152
|
-
date.setUTCFullYear(+year, +month - 1, +day);
|
|
153
|
-
return date.getUTCFullYear() === +year && date.getUTCMonth() === +month - 1 && date.getUTCDate() === +day
|
|
154
|
-
&& +hour <= 23 && +minute <= 59 && +second <= 59
|
|
155
|
-
&& (offset === 'Z' || (+offset.slice(1, 3) <= 23 && +offset.slice(4) <= 59));
|
|
156
|
-
}
|
|
157
283
|
function matchesRuntimeType(value, target, env, file, registry = env.dimensionRegistry) {
|
|
158
284
|
if (target.type === 'nullableType') {
|
|
159
285
|
return value === null || matchesRuntimeType(value, target.element, env, file, registry);
|
|
@@ -167,12 +293,14 @@ function matchesRuntimeType(value, target, env, file, registry = env.dimensionRe
|
|
|
167
293
|
return isMeasurement(value) && sameMeasurementVector(value, dimension.vector);
|
|
168
294
|
if (name === 'Number')
|
|
169
295
|
return typeof value === 'number';
|
|
296
|
+
if (name === 'Complex')
|
|
297
|
+
return isComplex(value);
|
|
170
298
|
if (name === 'String')
|
|
171
299
|
return typeof value === 'string';
|
|
172
300
|
if (name === 'Boolean')
|
|
173
301
|
return typeof value === 'boolean';
|
|
174
302
|
if (name === 'DateTime')
|
|
175
|
-
return
|
|
303
|
+
return isValidDateTime(value);
|
|
176
304
|
const enumeration = env.enumDeclarations.get(name);
|
|
177
305
|
if (enumeration) {
|
|
178
306
|
if (typeof value !== 'number' && typeof value !== 'string')
|
|
@@ -224,6 +352,8 @@ export function evaluateExpression(node, env, file) {
|
|
|
224
352
|
return evalListAccess(node, env, file);
|
|
225
353
|
case 'numberLiteral':
|
|
226
354
|
return node.value;
|
|
355
|
+
case 'imaginaryLiteral':
|
|
356
|
+
return createComplex(0, node.coefficient, node.source, file);
|
|
227
357
|
case 'stringLiteral':
|
|
228
358
|
return node.value;
|
|
229
359
|
case 'stringInterpolation':
|
|
@@ -338,6 +468,9 @@ function evalBinary(node, env, file) {
|
|
|
338
468
|
}
|
|
339
469
|
return measurement(value, left.unit);
|
|
340
470
|
}
|
|
471
|
+
if (isComplex(left) || isComplex(right)) {
|
|
472
|
+
return complexBinary(node.operator, left, right, operatorSource, file);
|
|
473
|
+
}
|
|
341
474
|
const lNum = toNumber(left, node.left);
|
|
342
475
|
const rNum = toNumber(right, node.right);
|
|
343
476
|
return node.operator === '+' ? lNum + rNum : lNum - rNum;
|
|
@@ -375,6 +508,9 @@ function evalBinary(node, env, file) {
|
|
|
375
508
|
}
|
|
376
509
|
return measurement(value, unit);
|
|
377
510
|
}
|
|
511
|
+
if (isComplex(left) || isComplex(right)) {
|
|
512
|
+
return complexBinary(node.operator, left, right, operatorSource, file);
|
|
513
|
+
}
|
|
378
514
|
const lNum = toNumber(left, node.left);
|
|
379
515
|
const rNum = toNumber(right, node.right);
|
|
380
516
|
return node.operator === '*' ? lNum * rNum : lNum / rNum;
|
|
@@ -406,18 +542,24 @@ function evalBinary(node, env, file) {
|
|
|
406
542
|
return measurementPhysicalValue(result);
|
|
407
543
|
return result;
|
|
408
544
|
}
|
|
545
|
+
if (isComplex(left))
|
|
546
|
+
return complexBinary('^', left, right, operatorSource, file);
|
|
409
547
|
const lNum = toNumber(left, node.left);
|
|
410
548
|
const rNum = toNumber(right, node.right);
|
|
411
549
|
return Math.pow(lNum, rNum);
|
|
412
550
|
}
|
|
413
551
|
// Comparisons (produce boolean)
|
|
414
552
|
case '==':
|
|
553
|
+
if (isComplex(left) || isComplex(right))
|
|
554
|
+
return complexBinary('==', left, right, operatorSource, file);
|
|
415
555
|
if (isMeasurement(left) && isMeasurement(right)) {
|
|
416
556
|
return sameMeasurementVector(left, new Map(Object.entries(right.unit.vector)))
|
|
417
557
|
&& measurementPhysicalValue(left) === measurementPhysicalValue(right);
|
|
418
558
|
}
|
|
419
559
|
return left === right;
|
|
420
560
|
case '!=':
|
|
561
|
+
if (isComplex(left) || isComplex(right))
|
|
562
|
+
return complexBinary('!=', left, right, operatorSource, file);
|
|
421
563
|
if (isMeasurement(left) && isMeasurement(right)) {
|
|
422
564
|
return !sameMeasurementVector(left, new Map(Object.entries(right.unit.vector)))
|
|
423
565
|
|| measurementPhysicalValue(left) !== measurementPhysicalValue(right);
|
|
@@ -450,7 +592,11 @@ function evalBinary(node, env, file) {
|
|
|
450
592
|
}
|
|
451
593
|
function evalUnary(node, env, file) {
|
|
452
594
|
const arg = evaluateExpression(node.argument, env, file);
|
|
595
|
+
if (node.operator === '+' && isComplex(arg))
|
|
596
|
+
return complexUnary('+', arg, node.source, file);
|
|
453
597
|
if (node.operator === '-') {
|
|
598
|
+
if (isComplex(arg))
|
|
599
|
+
return complexUnary('-', arg, node.source, file);
|
|
454
600
|
if (isMeasurement(arg))
|
|
455
601
|
return measurement(-arg.value, arg.unit);
|
|
456
602
|
return -toNumber(arg, node.argument);
|
|
@@ -486,13 +632,13 @@ export function evaluateUnaryValue(operator, value, source, file) {
|
|
|
486
632
|
source
|
|
487
633
|
}, environment, file);
|
|
488
634
|
}
|
|
489
|
-
export function evaluateBuiltinValues(callee, values, source, file, dimensionRegistry) {
|
|
635
|
+
export function evaluateBuiltinValues(callee, values, source, file, dimensionRegistry, argumentSources = []) {
|
|
490
636
|
const environment = new Environment();
|
|
491
637
|
environment.dimensionRegistry = dimensionRegistry;
|
|
492
638
|
const argumentsList = values.map((value, index) => {
|
|
493
639
|
const name = `__amxRuntimeArgument${index}`;
|
|
494
640
|
environment.set(name, value);
|
|
495
|
-
return { type: 'identifier', name, source };
|
|
641
|
+
return { type: 'identifier', name, source: argumentSources[index] ?? source };
|
|
496
642
|
});
|
|
497
643
|
return evalFunctionCall({
|
|
498
644
|
type: 'functionCall',
|
|
@@ -875,6 +1021,8 @@ export function evaluateStatements(statements, env, file) {
|
|
|
875
1021
|
}
|
|
876
1022
|
}
|
|
877
1023
|
function snapshotValue(value) {
|
|
1024
|
+
if (isComplex(value))
|
|
1025
|
+
return value;
|
|
878
1026
|
if (isMeasurement(value))
|
|
879
1027
|
return `${value.value} ${value.unit.text}`;
|
|
880
1028
|
if (Array.isArray(value))
|
|
@@ -6,6 +6,7 @@ import { parseStrictCsvText, parseStrictJsonText } from './dataText';
|
|
|
6
6
|
import { isMeasurement, measurement, measurementPhysicalValue } from './measurement';
|
|
7
7
|
import { ExternalUnitTextError, parseExternalUnitText } from './externalUnits';
|
|
8
8
|
import { fieldRegistry } from '../typechecker/declarationRegistry';
|
|
9
|
+
import { isValidDateTime } from './dateTime';
|
|
9
10
|
function defaultsEnvironment(environment, registry) {
|
|
10
11
|
return registry && environment.dimensionRegistry !== registry
|
|
11
12
|
? new Environment(environment.recordTypes, environment.functions, registry)
|
|
@@ -204,7 +205,7 @@ function convertJson(value, annotation, pointer, declarationSource, types, envir
|
|
|
204
205
|
report(shapeDiagnostic(name === 'DateTime' ? 'Expected an RFC 3339 DateTime string' : 'Input value has the wrong scalar type', name, describe(value), pointer, context, declarationSource, fieldSource));
|
|
205
206
|
return value;
|
|
206
207
|
}
|
|
207
|
-
if (name === 'DateTime' && !
|
|
208
|
+
if (name === 'DateTime' && !isValidDateTime(value)) {
|
|
208
209
|
report(shapeDiagnostic('Invalid RFC 3339 DateTime value', name, describe(value), pointer, context, declarationSource, fieldSource));
|
|
209
210
|
}
|
|
210
211
|
return value;
|
|
@@ -381,7 +382,7 @@ function convertCsvCell(cell, annotation, dataPath, recordNumber, columnNumber,
|
|
|
381
382
|
return Number(cell.value);
|
|
382
383
|
}
|
|
383
384
|
if (base.name === 'DateTime') {
|
|
384
|
-
if (!
|
|
385
|
+
if (!isValidDateTime(cell.value))
|
|
385
386
|
report(shapeDiagnostic('Invalid RFC 3339 DateTime CSV cell', 'DateTime', cell.value, dataPath, location, undefined, fieldSource));
|
|
386
387
|
return cell.value;
|
|
387
388
|
}
|
|
@@ -522,17 +523,6 @@ function validateCsvEnumValue(value, name, declaration, dataPath, context, field
|
|
|
522
523
|
report(shapeDiagnostic(`Value is not a member of enum '${name}'`, name, String(value), dataPath, context, undefined, fieldSource));
|
|
523
524
|
}
|
|
524
525
|
}
|
|
525
|
-
function isDateTime(value) {
|
|
526
|
-
const parts = value.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(Z|[+-]\d{2}:\d{2})$/);
|
|
527
|
-
if (!parts)
|
|
528
|
-
return false;
|
|
529
|
-
const [, year, month, day, hour, minute, second, offset] = parts;
|
|
530
|
-
const date = new Date(0);
|
|
531
|
-
date.setUTCFullYear(+year, +month - 1, +day);
|
|
532
|
-
return date.getUTCFullYear() === +year && date.getUTCMonth() === +month - 1 && date.getUTCDate() === +day
|
|
533
|
-
&& +hour <= 23 && +minute <= 59 && +second <= 59
|
|
534
|
-
&& (offset === 'Z' || (+offset.slice(1, 3) <= 23 && +offset.slice(4) <= 59));
|
|
535
|
-
}
|
|
536
526
|
function actualType(value) {
|
|
537
527
|
if (value === null)
|
|
538
528
|
return 'null';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import * as fs from 'fs';
|
|
3
3
|
import { parseDocument, parseDocumentText } from '../parser/parseDocument';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { checkDocumentWithRuntimePlan } from '../typechecker/checkDocument';
|
|
5
|
+
import { evaluateDocumentNodes, prepareTypeScriptBlocks } from './typescriptBridge';
|
|
6
6
|
import { Environment } from './environment';
|
|
7
7
|
import { AmxError, moduleError, staticError, throwInputErrors } from '../diagnostics/errors';
|
|
8
8
|
import { describeInputSchema, loadInputValues, validateInputText } from './inputData';
|
|
@@ -253,7 +253,7 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
|
-
const
|
|
256
|
+
const checked = checkDocumentWithRuntimePlan(doc, canonicalPath, {
|
|
257
257
|
types: importedTypes,
|
|
258
258
|
enums: importedEnums,
|
|
259
259
|
functions: importedFunctions,
|
|
@@ -265,7 +265,20 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
265
265
|
isEntryModule: canonicalPath === realEntry
|
|
266
266
|
});
|
|
267
267
|
visiting.pop();
|
|
268
|
-
const record = {
|
|
268
|
+
const record = {
|
|
269
|
+
canonicalPath,
|
|
270
|
+
doc,
|
|
271
|
+
checkResult: checked.result,
|
|
272
|
+
typescriptBlocks: checked.typescriptBlocks,
|
|
273
|
+
importedTypes,
|
|
274
|
+
importedEnums,
|
|
275
|
+
importedEnumSources,
|
|
276
|
+
importedFunctions,
|
|
277
|
+
importedBindings,
|
|
278
|
+
importedBindingSources,
|
|
279
|
+
importedDimensions,
|
|
280
|
+
importedUnits
|
|
281
|
+
};
|
|
269
282
|
resolved.set(canonicalPath, record);
|
|
270
283
|
evaluationOrder.push(canonicalPath);
|
|
271
284
|
return record;
|
|
@@ -330,6 +343,11 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
330
343
|
};
|
|
331
344
|
}
|
|
332
345
|
const inputValues = await loadInputValues(inputDeclarations, entryTypes, options.inputMappings, options.validation, entryPath, registry, entryEnums);
|
|
346
|
+
const preparedTypeScript = new Map();
|
|
347
|
+
for (const canonicalPath of evaluationOrder) {
|
|
348
|
+
const record = resolved.get(canonicalPath);
|
|
349
|
+
preparedTypeScript.set(canonicalPath, prepareTypeScriptBlocks(record.typescriptBlocks, canonicalPath));
|
|
350
|
+
}
|
|
333
351
|
const evaluatedEnvironments = new Map();
|
|
334
352
|
for (const canonicalPath of evaluationOrder) {
|
|
335
353
|
const record = resolved.get(canonicalPath);
|
|
@@ -362,13 +380,7 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
362
380
|
for (const [name, value] of inputValues)
|
|
363
381
|
env.set(name, value);
|
|
364
382
|
}
|
|
365
|
-
|
|
366
|
-
const node = record.doc.nodes[nodeIndex];
|
|
367
|
-
if (node.type === 'executableCodeBlock') {
|
|
368
|
-
env.currentDocumentNodeIndex = nodeIndex;
|
|
369
|
-
evaluateStatements(node.statements, env, canonicalPath);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
383
|
+
evaluateDocumentNodes(record.doc, env, canonicalPath, record.typescriptBlocks, preparedTypeScript.get(canonicalPath));
|
|
372
384
|
evaluatedEnvironments.set(canonicalPath, env);
|
|
373
385
|
}
|
|
374
386
|
const entryEnv = evaluatedEnvironments.get(realEntry);
|
|
@@ -2,6 +2,8 @@ import * as path from 'path';
|
|
|
2
2
|
import { outputError } from '../diagnostics/errors';
|
|
3
3
|
import { isMeasurement, measurementPhysicalValue } from './measurement';
|
|
4
4
|
import { fieldRegistry } from '../typechecker/declarationRegistry';
|
|
5
|
+
import { isValidDateTime } from './dateTime';
|
|
6
|
+
import { formatComplex, isComplex } from './complex';
|
|
5
7
|
function typeName(type) {
|
|
6
8
|
if (type.kind === 'named')
|
|
7
9
|
return type.name;
|
|
@@ -12,7 +14,7 @@ function typeName(type) {
|
|
|
12
14
|
return type.kind === 'list' ? `${typeName(type.element)}[]` : `${typeName(type.element)}?`;
|
|
13
15
|
}
|
|
14
16
|
function isPrimitive(type) {
|
|
15
|
-
return type.kind === 'named' && ['Number', 'String', 'Boolean', 'DateTime'].includes(type.name);
|
|
17
|
+
return type.kind === 'named' && ['Number', 'String', 'Boolean', 'DateTime', 'Complex'].includes(type.name);
|
|
16
18
|
}
|
|
17
19
|
function isCsvScalar(type) {
|
|
18
20
|
return type.kind === 'measurement' || isPrimitive(type) || (type.kind === 'nullable' && isCsvScalar(type.element));
|
|
@@ -163,9 +165,16 @@ function jsonValue(value, type, output, recordTypes, registry) {
|
|
|
163
165
|
return invalidValue(output, `expected a finite Number, got ${String(value)}`);
|
|
164
166
|
return value;
|
|
165
167
|
}
|
|
168
|
+
if (type.name === 'Complex') {
|
|
169
|
+
if (!isComplex(value))
|
|
170
|
+
return invalidValue(output, 'expected a finite Complex value');
|
|
171
|
+
return { real: value.real, imaginary: value.imaginary };
|
|
172
|
+
}
|
|
166
173
|
if (type.name === 'String' || type.name === 'DateTime') {
|
|
167
174
|
if (typeof value !== 'string')
|
|
168
175
|
return invalidValue(output, `expected ${type.name}`);
|
|
176
|
+
if (type.name === 'DateTime' && !isValidDateTime(value))
|
|
177
|
+
return invalidValue(output, 'expected a valid offset-bearing DateTime');
|
|
169
178
|
return value;
|
|
170
179
|
}
|
|
171
180
|
if (type.name === 'Boolean') {
|
|
@@ -210,7 +219,12 @@ function csvCell(value, type, output, registry) {
|
|
|
210
219
|
}
|
|
211
220
|
if (type.kind !== 'named')
|
|
212
221
|
return invalidValue(output, `unsupported CSV cell type ${typeName(type)}`);
|
|
213
|
-
if (type.name === '
|
|
222
|
+
if (type.name === 'Complex') {
|
|
223
|
+
if (!isComplex(value))
|
|
224
|
+
return invalidValue(output, 'expected a finite Complex value');
|
|
225
|
+
text = formatComplex(value);
|
|
226
|
+
}
|
|
227
|
+
else if (type.name === 'Number') {
|
|
214
228
|
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
215
229
|
return invalidValue(output, `expected a finite Number, got ${String(value)}`);
|
|
216
230
|
text = JSON.stringify(value);
|
|
@@ -223,6 +237,8 @@ function csvCell(value, type, output, registry) {
|
|
|
223
237
|
else if (type.name === 'String' || type.name === 'DateTime') {
|
|
224
238
|
if (typeof value !== 'string')
|
|
225
239
|
return invalidValue(output, `expected ${type.name}`);
|
|
240
|
+
if (type.name === 'DateTime' && !isValidDateTime(value))
|
|
241
|
+
return invalidValue(output, 'expected a valid offset-bearing DateTime');
|
|
226
242
|
text = value;
|
|
227
243
|
}
|
|
228
244
|
else {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { AmxError } from '../diagnostics/errors';
|
|
2
2
|
import { isMeasurement } from './measurement';
|
|
3
|
+
import { complexBuiltin, isComplex } from './complex';
|
|
4
|
+
import { addToDateTime, createLocalDateTime, dateTimeDifference, isDateTimeUnit, isValidDateTime, nowLocalDateTime, subtractFromDateTime } from './dateTime';
|
|
3
5
|
/**
|
|
4
6
|
* Standard library for OpenAMX v0.1.
|
|
5
7
|
*
|
|
@@ -117,6 +119,34 @@ function ensureLibraryArity(callee, args, min, max, source, file) {
|
|
|
117
119
|
throw libraryError('AMX3004', `Invalid arity for '${callee}'`, source, file);
|
|
118
120
|
}
|
|
119
121
|
}
|
|
122
|
+
function dateTimeIntegerArgument(callee, value, label, source, file) {
|
|
123
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || !Number.isInteger(value)) {
|
|
124
|
+
throw libraryError('AMX3002', `${callee} requires a finite integer Number for ${label}`, source, file);
|
|
125
|
+
}
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
function dateTimeArgument(callee, value, source, file) {
|
|
129
|
+
if (typeof value !== 'string' || !isValidDateTime(value)) {
|
|
130
|
+
throw libraryError('AMX3002', `${callee} requires a valid offset-bearing DateTime`, source, file);
|
|
131
|
+
}
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
function dateTimeUnitArgument(callee, value, source, file) {
|
|
135
|
+
if (!isDateTimeUnit(value)) {
|
|
136
|
+
throw libraryError('AMX3002', `${callee} requires a DateTimeUnit member`, source, file);
|
|
137
|
+
}
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
function runDateTimeOperation(action, source, file) {
|
|
141
|
+
try {
|
|
142
|
+
return action();
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
if (error instanceof RangeError)
|
|
146
|
+
throw libraryError('AMX3002', error.message, source, file);
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
120
150
|
function textArgument(callee, value, source, file) {
|
|
121
151
|
if (typeof value !== 'string') {
|
|
122
152
|
throw libraryError('AMX3002', `${callee} requires a String, got ${runtimeType(value)}`, source, file);
|
|
@@ -199,6 +229,36 @@ function formatNumericValue(value, pattern, locale) {
|
|
|
199
229
|
}
|
|
200
230
|
export function evaluateStandardLibraryCall(callee, args, source, file, argumentSources = []) {
|
|
201
231
|
const argumentSource = (index) => argumentSources[index] ?? source;
|
|
232
|
+
if (callee === 'now') {
|
|
233
|
+
ensureLibraryArity(callee, args, 0, 0, source, file);
|
|
234
|
+
return nowLocalDateTime();
|
|
235
|
+
}
|
|
236
|
+
if (callee === 'createDateTime') {
|
|
237
|
+
ensureLibraryArity(callee, args, 3, 6, source, file);
|
|
238
|
+
const day = dateTimeIntegerArgument(callee, args[0], 'day', argumentSource(0), file);
|
|
239
|
+
const month = dateTimeIntegerArgument(callee, args[1], 'month', argumentSource(1), file);
|
|
240
|
+
const year = dateTimeIntegerArgument(callee, args[2], 'year', argumentSource(2), file);
|
|
241
|
+
const hours = args.length > 3 ? dateTimeIntegerArgument(callee, args[3], 'hours', argumentSource(3), file) : 0;
|
|
242
|
+
const minutes = args.length > 4 ? dateTimeIntegerArgument(callee, args[4], 'minutes', argumentSource(4), file) : 0;
|
|
243
|
+
const seconds = args.length > 5 ? dateTimeIntegerArgument(callee, args[5], 'seconds', argumentSource(5), file) : 0;
|
|
244
|
+
return runDateTimeOperation(() => createLocalDateTime(day, month, year, hours, minutes, seconds), argumentSource(0), file);
|
|
245
|
+
}
|
|
246
|
+
if (callee === 'dateDiff') {
|
|
247
|
+
ensureLibraryArity(callee, args, 3, 3, source, file);
|
|
248
|
+
const from = dateTimeArgument(callee, args[0], argumentSource(0), file);
|
|
249
|
+
const to = dateTimeArgument(callee, args[1], argumentSource(1), file);
|
|
250
|
+
const unit = dateTimeUnitArgument(callee, args[2], argumentSource(2), file);
|
|
251
|
+
return runDateTimeOperation(() => dateTimeDifference(from, to, unit), source, file);
|
|
252
|
+
}
|
|
253
|
+
if (callee === 'addToDateTime' || callee === 'subtractFromDateTime') {
|
|
254
|
+
ensureLibraryArity(callee, args, 3, 3, source, file);
|
|
255
|
+
const value = dateTimeArgument(callee, args[0], argumentSource(0), file);
|
|
256
|
+
const amount = dateTimeIntegerArgument(callee, args[1], 'value', argumentSource(1), file);
|
|
257
|
+
const unit = dateTimeUnitArgument(callee, args[2], argumentSource(2), file);
|
|
258
|
+
return runDateTimeOperation(() => callee === 'addToDateTime'
|
|
259
|
+
? addToDateTime(value, amount, unit)
|
|
260
|
+
: subtractFromDateTime(value, amount, unit), source, file);
|
|
261
|
+
}
|
|
202
262
|
if (callee === 'len') {
|
|
203
263
|
ensureLibraryArity(callee, args, 1, 1, source, file);
|
|
204
264
|
const value = args[0];
|
|
@@ -255,6 +315,12 @@ export function evaluateStandardLibraryCall(callee, args, source, file, argument
|
|
|
255
315
|
});
|
|
256
316
|
}
|
|
257
317
|
const value = args[0];
|
|
318
|
+
if (isComplex(value)) {
|
|
319
|
+
if (callee === 'sin' || callee === 'cos' || callee === 'exp' || callee === 'log') {
|
|
320
|
+
return complexBuiltin(callee, value, source, file);
|
|
321
|
+
}
|
|
322
|
+
throw libraryError('AMX3002', `Expected Number or dimensionless Measurement, got Complex`, argumentSource(0), file);
|
|
323
|
+
}
|
|
258
324
|
if (typeof value !== 'number') {
|
|
259
325
|
const actual = value === null ? 'null'
|
|
260
326
|
: Array.isArray(value) ? 'list'
|
|
@@ -279,6 +345,15 @@ export function evaluateStandardLibraryCall(callee, args, source, file, argument
|
|
|
279
345
|
return result;
|
|
280
346
|
}
|
|
281
347
|
switch (callee) {
|
|
348
|
+
case 'conjugate':
|
|
349
|
+
case 'arg': {
|
|
350
|
+
ensureArgCount(args, 1, callee, source, file);
|
|
351
|
+
const value = args[0];
|
|
352
|
+
if (!isComplex(value)) {
|
|
353
|
+
throw libraryError('AMX3002', `Expected Complex, got ${runtimeType(value)}`, argumentSource(0), file);
|
|
354
|
+
}
|
|
355
|
+
return complexBuiltin(callee, value, argumentSource(0), file);
|
|
356
|
+
}
|
|
282
357
|
case 'sum': {
|
|
283
358
|
ensureArgCount(args, 1, 'sum', source, file);
|
|
284
359
|
const list = ensureList(args[0], 'sum', source, file);
|
|
@@ -321,11 +396,15 @@ export function evaluateStandardLibraryCall(callee, args, source, file, argument
|
|
|
321
396
|
}
|
|
322
397
|
case 'abs': {
|
|
323
398
|
ensureArgCount(args, 1, 'abs', source, file);
|
|
399
|
+
if (isComplex(args[0]))
|
|
400
|
+
return complexBuiltin('abs', args[0], argumentSource(0), file);
|
|
324
401
|
const value = ensureNumber(args[0], 'abs value', source, file);
|
|
325
402
|
return Math.abs(value);
|
|
326
403
|
}
|
|
327
404
|
case 'sqrt': {
|
|
328
405
|
ensureArgCount(args, 1, 'sqrt', source, file);
|
|
406
|
+
if (isComplex(args[0]))
|
|
407
|
+
return complexBuiltin('sqrt', args[0], argumentSource(0), file);
|
|
329
408
|
const value = ensureNumber(args[0], 'sqrt value', source, file);
|
|
330
409
|
if (value < 0) {
|
|
331
410
|
const loc = source ? ` at line ${source.line}` : '';
|