@nova-energy/openamx 0.18.0 → 0.19.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 +20 -14
- package/dist/editor/highlighting.js +11 -1
- package/dist/parser/parseStatements.js +40 -0
- package/dist/renderer/chartModel.js +1 -0
- package/dist/renderer/diagramBrowserRuntime.js +1 -1
- package/dist/renderer/renderHtml.js +24 -7
- package/dist/renderer/reportDocx.js +13 -4
- package/dist/renderer/reportPdf.js +2 -0
- package/dist/runtime/evaluateExpression.js +64 -32
- package/dist/runtime/externalUnits.js +21 -6
- package/dist/runtime/inputData.js +7 -2
- package/dist/runtime/measurement.js +28 -1
- package/dist/runtime/outputData.js +5 -3
- package/dist/typechecker/checkDocument.js +152 -33
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@ import { inputError, staticError, throwInputErrors, throwInvalidLoopIterable, th
|
|
|
2
2
|
import { Environment } from './environment';
|
|
3
3
|
import { evaluateStandardLibraryCall, isMathematicalFunction } from './standardLibrary';
|
|
4
4
|
import { fieldRegistry } from '../typechecker/declarationRegistry';
|
|
5
|
-
import { canonicalUnit, composeUnits, isMeasurement, measurement, powerUnit, sqrtMeasurement, unitFromMetadata } from './measurement';
|
|
5
|
+
import { canonicalUnit, composeUnits, isAffineUnit, isMeasurement, measurement, measurementDisplayValue, measurementPhysicalValue, powerUnit, sqrtMeasurement, unitFromMetadata } from './measurement';
|
|
6
6
|
import { createDiagramModel } from './diagramModel';
|
|
7
7
|
function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
8
8
|
const diagnostics = [];
|
|
@@ -49,7 +49,7 @@ function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
|
49
49
|
|| [...target.vector].some(([identity, exponent]) => vector[identity] !== exponent)) {
|
|
50
50
|
report('Measurement dimension does not match the declared dimension', target, actual, dataPath, fieldSource);
|
|
51
51
|
}
|
|
52
|
-
if (!Number.isFinite(actual.value) || !Number.isFinite(actual
|
|
52
|
+
if (!Number.isFinite(actual.value) || !Number.isFinite(measurementPhysicalValue(actual))) {
|
|
53
53
|
report('Measurement value must be finite', target, actual, dataPath, fieldSource);
|
|
54
54
|
}
|
|
55
55
|
return;
|
|
@@ -258,7 +258,7 @@ export function evaluateExpression(node, env, file) {
|
|
|
258
258
|
if (typeof value !== 'number')
|
|
259
259
|
return staticError('AMX3007', 'Unit attachment requires a Number expression', node.source, file);
|
|
260
260
|
const result = measurement(value, unitFromMetadata(unit));
|
|
261
|
-
if (!Number.isFinite(result
|
|
261
|
+
if (!Number.isFinite(measurementPhysicalValue(result))) {
|
|
262
262
|
throwMeasurementDomainError('Measurement physical value must be finite', node.source, file);
|
|
263
263
|
}
|
|
264
264
|
return result;
|
|
@@ -271,10 +271,11 @@ export function evaluateExpression(node, env, file) {
|
|
|
271
271
|
if (!isMeasurement(value) || !sameMeasurementVector(value, unit.vector)) {
|
|
272
272
|
return staticError('AMX3007', `Cannot convert value to unit '${node.unit}'`, node.source, file);
|
|
273
273
|
}
|
|
274
|
-
const
|
|
274
|
+
const target = unitFromMetadata(unit);
|
|
275
|
+
const converted = measurementDisplayValue(measurementPhysicalValue(value), target);
|
|
275
276
|
if (!Number.isFinite(converted))
|
|
276
277
|
throwMeasurementDomainError('Converted measurement value must be finite', node.source, file);
|
|
277
|
-
return measurement(converted,
|
|
278
|
+
return measurement(converted, target);
|
|
278
279
|
}
|
|
279
280
|
case 'binaryExpression':
|
|
280
281
|
return evalBinary(node, env, file);
|
|
@@ -323,10 +324,15 @@ function evalBinary(node, env, file) {
|
|
|
323
324
|
if (!isMeasurement(left) || !isMeasurement(right) || !sameMeasurementVector(left, new Map(Object.entries(right.unit.vector)))) {
|
|
324
325
|
return staticError('AMX3007', 'Addition and subtraction require compatible measurements', operatorSource, file);
|
|
325
326
|
}
|
|
326
|
-
|
|
327
|
-
|
|
327
|
+
if (node.operator === '+' && (isAffineUnit(left.unit) || isAffineUnit(right.unit))) {
|
|
328
|
+
return staticError('AMX3007', 'Direct addition of affine measurements is not supported', operatorSource, file);
|
|
329
|
+
}
|
|
330
|
+
const physicalLeft = measurementPhysicalValue(left);
|
|
331
|
+
const physicalRight = measurementPhysicalValue(right);
|
|
328
332
|
const physical = node.operator === '+' ? physicalLeft + physicalRight : physicalLeft - physicalRight;
|
|
329
|
-
const value =
|
|
333
|
+
const value = node.operator === '+'
|
|
334
|
+
? measurementDisplayValue(physical, left.unit)
|
|
335
|
+
: physical / left.unit.scale;
|
|
330
336
|
if (!Number.isFinite(value) || !Number.isFinite(physical)) {
|
|
331
337
|
throwMeasurementDomainError('Measurement arithmetic result must be finite', operatorSource, file);
|
|
332
338
|
}
|
|
@@ -339,21 +345,25 @@ function evalBinary(node, env, file) {
|
|
|
339
345
|
case '*':
|
|
340
346
|
case '/': {
|
|
341
347
|
if (isMeasurement(left) || isMeasurement(right)) {
|
|
342
|
-
const numberUnit = { scale: 1, vector: {}, factors: [], text: '1' };
|
|
348
|
+
const numberUnit = { scale: 1, offset: 0, vector: {}, factors: [], text: '1' };
|
|
343
349
|
const leftMeasurement = isMeasurement(left) ? left : undefined;
|
|
344
350
|
const rightMeasurement = isMeasurement(right) ? right : undefined;
|
|
351
|
+
if (leftMeasurement && isAffineUnit(leftMeasurement.unit) || rightMeasurement && isAffineUnit(rightMeasurement.unit)) {
|
|
352
|
+
return staticError('AMX3007', node.operator === '*' ? 'Affine measurement multiplication is not supported' : 'Affine measurement division is not supported', operatorSource, file);
|
|
353
|
+
}
|
|
345
354
|
const leftValue = leftMeasurement ? leftMeasurement.value : toNumber(left, node.left);
|
|
346
355
|
const rightValue = rightMeasurement ? rightMeasurement.value : toNumber(right, node.right);
|
|
347
356
|
const leftUnit = leftMeasurement?.unit ?? numberUnit;
|
|
348
357
|
const rightUnit = rightMeasurement?.unit ?? numberUnit;
|
|
349
|
-
const
|
|
358
|
+
const leftPhysical = leftMeasurement ? measurementPhysicalValue(leftMeasurement) : leftValue;
|
|
359
|
+
const rightPhysical = rightMeasurement ? measurementPhysicalValue(rightMeasurement) : rightValue;
|
|
350
360
|
if (node.operator === '/' && rightPhysical === 0) {
|
|
351
361
|
throwMeasurementDomainError('Measurement division by zero', operatorSource, file);
|
|
352
362
|
}
|
|
353
363
|
const unit = composeUnits(leftUnit, rightUnit, node.operator === '/');
|
|
354
364
|
const physical = node.operator === '/'
|
|
355
|
-
?
|
|
356
|
-
:
|
|
365
|
+
? leftPhysical / rightPhysical
|
|
366
|
+
: leftPhysical * rightPhysical;
|
|
357
367
|
if (unit.vector && Object.keys(unit.vector).length === 0) {
|
|
358
368
|
if (!Number.isFinite(physical))
|
|
359
369
|
throwMeasurementDomainError('Measurement arithmetic result must be finite', operatorSource, file);
|
|
@@ -376,6 +386,9 @@ function evalBinary(node, env, file) {
|
|
|
376
386
|
}
|
|
377
387
|
case '^': {
|
|
378
388
|
if (isMeasurement(left)) {
|
|
389
|
+
if (isAffineUnit(left.unit)) {
|
|
390
|
+
return staticError('AMX3007', 'Affine measurement powers are not supported', operatorSource, file);
|
|
391
|
+
}
|
|
379
392
|
const exponent = toNumber(right, node.right);
|
|
380
393
|
if (!Number.isSafeInteger(exponent)) {
|
|
381
394
|
return staticError('AMX3007', 'A dimensional power exponent must be an integer literal', node.right.source, file);
|
|
@@ -388,9 +401,10 @@ function evalBinary(node, env, file) {
|
|
|
388
401
|
if (!Number.isFinite(value) || !Number.isFinite(unit.scale)) {
|
|
389
402
|
throwMeasurementDomainError('Measurement power result must be finite', operatorSource, file);
|
|
390
403
|
}
|
|
404
|
+
const result = measurement(value, unit);
|
|
391
405
|
if (Object.keys(unit.vector).length === 0)
|
|
392
|
-
return
|
|
393
|
-
return
|
|
406
|
+
return measurementPhysicalValue(result);
|
|
407
|
+
return result;
|
|
394
408
|
}
|
|
395
409
|
const lNum = toNumber(left, node.left);
|
|
396
410
|
const rNum = toNumber(right, node.right);
|
|
@@ -400,30 +414,30 @@ function evalBinary(node, env, file) {
|
|
|
400
414
|
case '==':
|
|
401
415
|
if (isMeasurement(left) && isMeasurement(right)) {
|
|
402
416
|
return sameMeasurementVector(left, new Map(Object.entries(right.unit.vector)))
|
|
403
|
-
&& left
|
|
417
|
+
&& measurementPhysicalValue(left) === measurementPhysicalValue(right);
|
|
404
418
|
}
|
|
405
419
|
return left === right;
|
|
406
420
|
case '!=':
|
|
407
421
|
if (isMeasurement(left) && isMeasurement(right)) {
|
|
408
422
|
return !sameMeasurementVector(left, new Map(Object.entries(right.unit.vector)))
|
|
409
|
-
|| left
|
|
423
|
+
|| measurementPhysicalValue(left) !== measurementPhysicalValue(right);
|
|
410
424
|
}
|
|
411
425
|
return left !== right;
|
|
412
426
|
case '>':
|
|
413
427
|
if (isMeasurement(left) && isMeasurement(right))
|
|
414
|
-
return left
|
|
428
|
+
return measurementPhysicalValue(left) > measurementPhysicalValue(right);
|
|
415
429
|
return toNumber(left, node.left) > toNumber(right, node.right);
|
|
416
430
|
case '>=':
|
|
417
431
|
if (isMeasurement(left) && isMeasurement(right))
|
|
418
|
-
return left
|
|
432
|
+
return measurementPhysicalValue(left) >= measurementPhysicalValue(right);
|
|
419
433
|
return toNumber(left, node.left) >= toNumber(right, node.right);
|
|
420
434
|
case '<':
|
|
421
435
|
if (isMeasurement(left) && isMeasurement(right))
|
|
422
|
-
return left
|
|
436
|
+
return measurementPhysicalValue(left) < measurementPhysicalValue(right);
|
|
423
437
|
return toNumber(left, node.left) < toNumber(right, node.right);
|
|
424
438
|
case '<=':
|
|
425
439
|
if (isMeasurement(left) && isMeasurement(right))
|
|
426
|
-
return left
|
|
440
|
+
return measurementPhysicalValue(left) <= measurementPhysicalValue(right);
|
|
427
441
|
return toNumber(left, node.left) <= toNumber(right, node.right);
|
|
428
442
|
// Logical (short-circuit not required for simple impl; evaluate both then apply)
|
|
429
443
|
case 'and':
|
|
@@ -629,15 +643,17 @@ function evalFunctionCall(node, env, file) {
|
|
|
629
643
|
return evaluateStandardLibraryCall(node.callee, args, node.source, file);
|
|
630
644
|
return measurements.reduce((best, current) => {
|
|
631
645
|
const better = node.callee === 'min'
|
|
632
|
-
? current
|
|
633
|
-
: current
|
|
646
|
+
? measurementPhysicalValue(current) < measurementPhysicalValue(best)
|
|
647
|
+
: measurementPhysicalValue(current) > measurementPhysicalValue(best);
|
|
634
648
|
return better ? current : best;
|
|
635
649
|
});
|
|
636
650
|
}
|
|
637
|
-
const physicalSum = measurements.reduce((sum, current) => sum + current
|
|
638
|
-
const
|
|
639
|
-
|
|
651
|
+
const physicalSum = measurements.reduce((sum, current) => sum + measurementPhysicalValue(current), 0);
|
|
652
|
+
const physicalResult = node.callee === 'mean' ? physicalSum / measurements.length : physicalSum;
|
|
653
|
+
const value = measurementDisplayValue(physicalResult, first.unit);
|
|
654
|
+
if (!Number.isFinite(physicalSum) || !Number.isFinite(physicalResult) || !Number.isFinite(value)) {
|
|
640
655
|
throwMeasurementDomainError(`${node.callee} result must be finite`, node.source, file);
|
|
656
|
+
}
|
|
641
657
|
return measurement(value, first.unit);
|
|
642
658
|
}
|
|
643
659
|
if (node.callee === 'sum' && Array.isArray(values) && values.length === 0
|
|
@@ -653,7 +669,7 @@ function evalFunctionCall(node, env, file) {
|
|
|
653
669
|
if (Object.keys(value.unit.vector).length !== 0) {
|
|
654
670
|
return staticError('AMX3007', `'${node.callee}' requires a Number or dimensionless Measurement`, node.arguments[0]?.source ?? node.source, file);
|
|
655
671
|
}
|
|
656
|
-
return evaluateStandardLibraryCall(node.callee, [value
|
|
672
|
+
return evaluateStandardLibraryCall(node.callee, [measurementPhysicalValue(value)], node.source, file);
|
|
657
673
|
}
|
|
658
674
|
switch (node.callee) {
|
|
659
675
|
case 'abs':
|
|
@@ -667,7 +683,10 @@ function evalFunctionCall(node, env, file) {
|
|
|
667
683
|
return measurement(rounded, value.unit);
|
|
668
684
|
}
|
|
669
685
|
case 'sqrt': {
|
|
670
|
-
|
|
686
|
+
if (isAffineUnit(value.unit)) {
|
|
687
|
+
return staticError('AMX3007', 'Affine measurement sqrt is not supported', node.source, file);
|
|
688
|
+
}
|
|
689
|
+
const physical = measurementPhysicalValue(value);
|
|
671
690
|
if (physical < 0)
|
|
672
691
|
throwMeasurementDomainError('sqrt of a negative measurement value', node.source, file);
|
|
673
692
|
if (!env.dimensionRegistry)
|
|
@@ -678,7 +697,7 @@ function evalFunctionCall(node, env, file) {
|
|
|
678
697
|
throwMeasurementDomainError('sqrt result must be finite', node.source, file);
|
|
679
698
|
}
|
|
680
699
|
if (Object.keys(result.unit.vector).length === 0)
|
|
681
|
-
return result
|
|
700
|
+
return measurementPhysicalValue(result);
|
|
682
701
|
return result;
|
|
683
702
|
}
|
|
684
703
|
catch (error) {
|
|
@@ -689,6 +708,9 @@ function evalFunctionCall(node, env, file) {
|
|
|
689
708
|
}
|
|
690
709
|
}
|
|
691
710
|
case 'pow': {
|
|
711
|
+
if (isAffineUnit(value.unit)) {
|
|
712
|
+
return staticError('AMX3007', 'Affine measurement powers are not supported', node.source, file);
|
|
713
|
+
}
|
|
692
714
|
const exponent = toNumber(args[1], node.arguments[1]);
|
|
693
715
|
if (!Number.isSafeInteger(exponent))
|
|
694
716
|
return staticError('AMX3007', 'A dimensional pow exponent must be an integer literal', node.arguments[1]?.source, file);
|
|
@@ -698,9 +720,10 @@ function evalFunctionCall(node, env, file) {
|
|
|
698
720
|
const result = Math.pow(value.value, exponent);
|
|
699
721
|
if (!Number.isFinite(result) || !Number.isFinite(unit.scale))
|
|
700
722
|
throwMeasurementDomainError('pow result must be finite', node.source, file);
|
|
723
|
+
const powered = measurement(result, unit);
|
|
701
724
|
if (!Object.keys(unit.vector).length)
|
|
702
|
-
return
|
|
703
|
-
return
|
|
725
|
+
return measurementPhysicalValue(powered);
|
|
726
|
+
return powered;
|
|
704
727
|
}
|
|
705
728
|
}
|
|
706
729
|
}
|
|
@@ -774,8 +797,17 @@ function snapshotView(name, env, file, source) {
|
|
|
774
797
|
let labels;
|
|
775
798
|
let headings;
|
|
776
799
|
let measurementDescriptors;
|
|
800
|
+
let showTable = false;
|
|
777
801
|
let viewData;
|
|
778
802
|
if (declaration.type === 'chartDeclaration') {
|
|
803
|
+
const showTableOption = declaration.options.find(option => option.type === 'chartShowTableOption');
|
|
804
|
+
if (showTableOption?.type === 'chartShowTableOption') {
|
|
805
|
+
const resolved = evaluateExpression(showTableOption.expression, env, file);
|
|
806
|
+
if (typeof resolved !== 'boolean') {
|
|
807
|
+
staticError('AMX3002', `Expected Boolean, got ${typeof resolved}`, showTableOption.expression.source, file);
|
|
808
|
+
}
|
|
809
|
+
showTable = resolved;
|
|
810
|
+
}
|
|
779
811
|
const labelsOption = declaration.options.find(option => option.type === 'chartFieldOption' && option.role === 'labels');
|
|
780
812
|
if (labelsOption?.type === 'chartFieldOption') {
|
|
781
813
|
const labelValues = env.get(labelsOption.field, labelsOption.fieldSource, file);
|
|
@@ -810,7 +842,7 @@ function snapshotView(name, env, file, source) {
|
|
|
810
842
|
return declaration.type === 'tableDeclaration'
|
|
811
843
|
? Object.freeze({ kind: 'table', ...emission, declaration })
|
|
812
844
|
: Object.freeze({
|
|
813
|
-
kind: 'chart', ...emission, declaration,
|
|
845
|
+
kind: 'chart', ...emission, declaration, showTable,
|
|
814
846
|
...(labels ? { labels } : {}), ...(headings ? { headings } : {}),
|
|
815
847
|
...(measurementDescriptors?.length ? { measurementDescriptors } : {})
|
|
816
848
|
});
|
|
@@ -893,7 +925,7 @@ function normalizeChartValue(value, target, name, source, declaration, file) {
|
|
|
893
925
|
if (!isMeasurement(value) || !sameMeasurementUnits(value, target)) {
|
|
894
926
|
staticError('AMX3007', `Chart '${name}' contains an incompatible measurement value`, source, file, declaration.source);
|
|
895
927
|
}
|
|
896
|
-
const converted = value
|
|
928
|
+
const converted = measurementDisplayValue(measurementPhysicalValue(value), target.unit);
|
|
897
929
|
if (!Number.isFinite(converted)) {
|
|
898
930
|
staticError('AMX3007', `Chart '${name}' measurement cannot be normalized to its first display unit`, source, file, declaration.source);
|
|
899
931
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { composeUnits, powerUnit, unitFromMetadata } from './measurement';
|
|
1
|
+
import { composeUnits, isAffineUnit, powerUnit, unitFromMetadata } from './measurement';
|
|
2
2
|
export class ExternalUnitTextError extends Error {
|
|
3
3
|
unknownUnit;
|
|
4
4
|
constructor(message, unknownUnit) {
|
|
@@ -9,6 +9,7 @@ export class ExternalUnitTextError extends Error {
|
|
|
9
9
|
}
|
|
10
10
|
const unitless = Object.freeze({
|
|
11
11
|
scale: 1,
|
|
12
|
+
offset: 0,
|
|
12
13
|
vector: Object.freeze({}),
|
|
13
14
|
factors: Object.freeze([]),
|
|
14
15
|
text: '1'
|
|
@@ -32,10 +33,16 @@ export function parseExternalUnitText(text, registry) {
|
|
|
32
33
|
const parseProduct = () => {
|
|
33
34
|
let unit = parseFactor();
|
|
34
35
|
while (true) {
|
|
35
|
-
if (consume('*'))
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
unit = validateUnit(composeUnits(unit,
|
|
36
|
+
if (consume('*')) {
|
|
37
|
+
const right = parseFactor();
|
|
38
|
+
rejectAffineComposition(unit, right);
|
|
39
|
+
unit = validateUnit(composeUnits(unit, right));
|
|
40
|
+
}
|
|
41
|
+
else if (consume('/')) {
|
|
42
|
+
const right = parseFactor();
|
|
43
|
+
rejectAffineComposition(unit, right);
|
|
44
|
+
unit = validateUnit(composeUnits(unit, right, true));
|
|
45
|
+
}
|
|
39
46
|
else
|
|
40
47
|
return unit;
|
|
41
48
|
}
|
|
@@ -63,6 +70,9 @@ export function parseExternalUnitText(text, registry) {
|
|
|
63
70
|
unit = unitFromMetadata(metadata);
|
|
64
71
|
}
|
|
65
72
|
if (consume('^')) {
|
|
73
|
+
if (isAffineUnit(unit)) {
|
|
74
|
+
throw new ExternalUnitTextError('Affine units may not be used in external unit powers', false);
|
|
75
|
+
}
|
|
66
76
|
skipWhitespace();
|
|
67
77
|
const exponent = source.slice(offset).match(/^[+-]?\d+/);
|
|
68
78
|
if (!exponent)
|
|
@@ -81,8 +91,13 @@ export function parseExternalUnitText(text, registry) {
|
|
|
81
91
|
throw new ExternalUnitTextError(`Unexpected '${source[offset]}' in unit expression`, false);
|
|
82
92
|
return Object.freeze({ ...parsed, text: source });
|
|
83
93
|
}
|
|
94
|
+
function rejectAffineComposition(left, right) {
|
|
95
|
+
if (isAffineUnit(left) || isAffineUnit(right)) {
|
|
96
|
+
throw new ExternalUnitTextError('Affine units may not be used in external unit products or divisions', false);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
84
99
|
function validateUnit(unit) {
|
|
85
|
-
if (!Number.isFinite(unit.scale) || unit.scale <= 0
|
|
100
|
+
if (!Number.isFinite(unit.scale) || unit.scale <= 0 || !Number.isFinite(unit.offset)
|
|
86
101
|
|| Object.values(unit.vector).some(exponent => !Number.isSafeInteger(exponent))
|
|
87
102
|
|| unit.factors.some(factor => !Number.isSafeInteger(factor.exponent))) {
|
|
88
103
|
throw new ExternalUnitTextError('Unit expression exceeds supported scale or exponent limits', false);
|
|
@@ -3,7 +3,7 @@ import { inputError, throwInputErrors } from '../diagnostics/errors';
|
|
|
3
3
|
import { Environment } from './environment';
|
|
4
4
|
import { evaluateExpression } from './evaluateExpression';
|
|
5
5
|
import { parseStrictCsvText, parseStrictJsonText } from './dataText';
|
|
6
|
-
import { isMeasurement, measurement } from './measurement';
|
|
6
|
+
import { isMeasurement, measurement, measurementPhysicalValue } from './measurement';
|
|
7
7
|
import { ExternalUnitTextError, parseExternalUnitText } from './externalUnits';
|
|
8
8
|
import { fieldRegistry } from '../typechecker/declarationRegistry';
|
|
9
9
|
function defaultsEnvironment(environment, registry) {
|
|
@@ -398,7 +398,12 @@ function convertMeasurement(value, unitText, expectedVector, unitPath, context,
|
|
|
398
398
|
report(measurementDiagnostic('AMX4005', 'Measurement unit has an incompatible dimension', unitPath, context, declarationSource, fieldSource, formatVector(expectedVector, registry), formatVector(unit.vector, registry)));
|
|
399
399
|
return null;
|
|
400
400
|
}
|
|
401
|
-
|
|
401
|
+
const result = measurement(value, unit);
|
|
402
|
+
if (!Number.isFinite(measurementPhysicalValue(result))) {
|
|
403
|
+
report(measurementDiagnostic('AMX4005', 'Measurement physical value must be finite', unitPath, context, declarationSource, fieldSource, 'finite physical value', String(measurementPhysicalValue(result))));
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
return result;
|
|
402
407
|
}
|
|
403
408
|
function measurementDiagnostic(code, message, dataPath, context, declarationSource, fieldSource, expected, actual) {
|
|
404
409
|
return inputError(code, message, {
|
|
@@ -20,11 +20,27 @@ export function unitFromMetadata(unit) {
|
|
|
20
20
|
const factors = [{ identity: unit.identity, name: unit.name, exponent: 1 }];
|
|
21
21
|
return Object.freeze({
|
|
22
22
|
scale: unit.scale,
|
|
23
|
+
offset: unit.offset,
|
|
23
24
|
vector: vectorRecord(unit.vector),
|
|
24
25
|
factors: Object.freeze(factors),
|
|
25
26
|
text: unit.name
|
|
26
27
|
});
|
|
27
28
|
}
|
|
29
|
+
export function physicalValue(value, unit) {
|
|
30
|
+
return value * unit.scale + unit.offset;
|
|
31
|
+
}
|
|
32
|
+
export function displayValue(physical, unit) {
|
|
33
|
+
return (physical - unit.offset) / unit.scale;
|
|
34
|
+
}
|
|
35
|
+
export function measurementPhysicalValue(value) {
|
|
36
|
+
return physicalValue(value.value, value.unit);
|
|
37
|
+
}
|
|
38
|
+
export function measurementDisplayValue(physical, unit) {
|
|
39
|
+
return displayValue(physical, unit);
|
|
40
|
+
}
|
|
41
|
+
export function isAffineUnit(unit) {
|
|
42
|
+
return unit.offset !== 0;
|
|
43
|
+
}
|
|
28
44
|
function combineFactors(left, right, multiplier) {
|
|
29
45
|
const factors = new Map();
|
|
30
46
|
for (const factor of left.factors)
|
|
@@ -51,20 +67,28 @@ function combineVectorRecords(left, right, multiplier) {
|
|
|
51
67
|
return vector;
|
|
52
68
|
}
|
|
53
69
|
export function composeUnits(left, right, divide = false) {
|
|
70
|
+
if (isAffineUnit(left) || isAffineUnit(right)) {
|
|
71
|
+
throw new Error('Affine measurement units cannot be composed');
|
|
72
|
+
}
|
|
54
73
|
const multiplier = divide ? -1 : 1;
|
|
55
74
|
const factors = combineFactors(left, right, multiplier);
|
|
56
75
|
return Object.freeze({
|
|
57
76
|
scale: divide ? left.scale / right.scale : left.scale * right.scale,
|
|
77
|
+
offset: 0,
|
|
58
78
|
vector: Object.freeze(combineVectorRecords(left, right, multiplier)),
|
|
59
79
|
factors: Object.freeze(factors),
|
|
60
80
|
text: formatFactors(factors)
|
|
61
81
|
});
|
|
62
82
|
}
|
|
63
83
|
export function powerUnit(unit, exponent) {
|
|
84
|
+
if (isAffineUnit(unit)) {
|
|
85
|
+
throw new Error('Affine measurement units may not be powered');
|
|
86
|
+
}
|
|
64
87
|
const factors = unit.factors.map(factor => ({ ...factor, exponent: factor.exponent * exponent }))
|
|
65
88
|
.filter(factor => factor.exponent !== 0);
|
|
66
89
|
return Object.freeze({
|
|
67
90
|
scale: Math.pow(unit.scale, exponent),
|
|
91
|
+
offset: unit.offset,
|
|
68
92
|
vector: Object.freeze(Object.fromEntries(Object.entries(unit.vector)
|
|
69
93
|
.map(([identity, power]) => [identity, power * exponent]).filter(([, power]) => power !== 0))),
|
|
70
94
|
factors: Object.freeze(factors),
|
|
@@ -84,19 +108,22 @@ export function canonicalUnit(vector, registry) {
|
|
|
84
108
|
}
|
|
85
109
|
return Object.freeze({
|
|
86
110
|
scale: 1,
|
|
111
|
+
offset: 0,
|
|
87
112
|
vector: vectorRecord(vector),
|
|
88
113
|
factors: Object.freeze(factors),
|
|
89
114
|
text: formatFactors(factors)
|
|
90
115
|
});
|
|
91
116
|
}
|
|
92
117
|
export function sqrtMeasurement(value, registry) {
|
|
118
|
+
if (isAffineUnit(value.unit))
|
|
119
|
+
throw new Error('Affine measurement sqrt is not supported');
|
|
93
120
|
const vector = new Map();
|
|
94
121
|
for (const [identity, exponent] of Object.entries(value.unit.vector)) {
|
|
95
122
|
if (exponent % 2 !== 0)
|
|
96
123
|
throw new Error('sqrt requires integral measurement dimension exponents');
|
|
97
124
|
vector.set(identity, exponent / 2);
|
|
98
125
|
}
|
|
99
|
-
const physical = value
|
|
126
|
+
const physical = measurementPhysicalValue(value);
|
|
100
127
|
const physicalRoot = Math.sqrt(physical);
|
|
101
128
|
if (!Number.isFinite(physicalRoot))
|
|
102
129
|
return measurement(physicalRoot, canonicalUnit(vector, registry));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import { outputError } from '../diagnostics/errors';
|
|
3
|
-
import { isMeasurement } from './measurement';
|
|
3
|
+
import { isMeasurement, measurementPhysicalValue } from './measurement';
|
|
4
4
|
import { fieldRegistry } from '../typechecker/declarationRegistry';
|
|
5
5
|
function typeName(type) {
|
|
6
6
|
if (type.kind === 'named')
|
|
@@ -149,8 +149,9 @@ function jsonValue(value, type, output, recordTypes, registry) {
|
|
|
149
149
|
return value.map(item => jsonValue(item, type.element, output, recordTypes, registry));
|
|
150
150
|
}
|
|
151
151
|
if (type.kind === 'measurement') {
|
|
152
|
-
if (!isMeasurement(value) || !Number.isFinite(value.value))
|
|
152
|
+
if (!isMeasurement(value) || !Number.isFinite(value.value) || !Number.isFinite(measurementPhysicalValue(value))) {
|
|
153
153
|
return invalidValue(output, 'expected a finite measurement value');
|
|
154
|
+
}
|
|
154
155
|
if (!sameVector(value.unit.vector, type.vector))
|
|
155
156
|
return invalidValue(output, 'measurement has an incompatible dimension');
|
|
156
157
|
return { value: value.value, unit: value.unit.text };
|
|
@@ -198,8 +199,9 @@ function csvCell(value, type, output, registry) {
|
|
|
198
199
|
return csvCell(value, type.element, output, registry);
|
|
199
200
|
}
|
|
200
201
|
if (type.kind === 'measurement') {
|
|
201
|
-
if (!isMeasurement(value) || !Number.isFinite(value.value))
|
|
202
|
+
if (!isMeasurement(value) || !Number.isFinite(value.value) || !Number.isFinite(measurementPhysicalValue(value))) {
|
|
202
203
|
return invalidValue(output, 'expected a finite measurement value');
|
|
204
|
+
}
|
|
203
205
|
if (!sameVector(value.unit.vector, type.vector) || !value.unit.text)
|
|
204
206
|
return invalidValue(output, 'measurement has an incompatible dimension or missing unit text');
|
|
205
207
|
text = `${JSON.stringify(value.value)} ${value.unit.text}`;
|