@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.
@@ -1,4 +1,5 @@
1
1
  import { moduleError, staticError } from '../diagnostics/errors';
2
+ import { physicalValue } from '../runtime/measurement';
2
3
  import { fieldRegistry, setDeclaringRegistry } from './declarationRegistry';
3
4
  const named = (name) => ({ kind: 'named', name });
4
5
  const list = (element) => ({ kind: 'list', element });
@@ -402,51 +403,159 @@ export function checkDocument(doc, file, context) {
402
403
  }
403
404
  return failDeclaration('Dimension expressions may use only visible dimensions, *, /, and integer powers', expression.source);
404
405
  }
406
+ function validateUnitExpressionScalar(value, source) {
407
+ if (!Number.isFinite(value))
408
+ failDeclaration('Unit expression scalar must be finite', source);
409
+ return value;
410
+ }
405
411
  function validateUnitScale(scale, source) {
406
412
  if (!Number.isFinite(scale) || scale <= 0)
407
413
  failDeclaration('Unit scale must be finite and greater than zero', source);
408
414
  return scale;
409
415
  }
416
+ function validateUnitOffset(offset, source) {
417
+ if (!Number.isFinite(offset))
418
+ failDeclaration('Unit offset must be finite', source);
419
+ return offset;
420
+ }
421
+ function scaledUnit(unit, factor, source) {
422
+ const scale = validateUnitScale(unit.scale * factor, source);
423
+ const offset = validateUnitOffset(unit.offset * factor, source);
424
+ return { kind: 'unit', vector: unit.vector, scale, offset };
425
+ }
410
426
  function resolveUnitExpression(expression) {
427
+ const operatorSource = expression.type === 'binaryExpression' ? expression.operatorSource ?? expression.source : expression.source;
411
428
  if (expression.type === 'numberLiteral') {
412
- return { vector: new Map(), scale: validateUnitScale(expression.value, expression.source), hasUnit: false };
429
+ return { kind: 'scalar', value: validateUnitExpressionScalar(expression.value, expression.source) };
413
430
  }
414
431
  if (expression.type === 'identifier') {
432
+ if (expression.name === 'PI')
433
+ return { kind: 'scalar', value: Math.PI };
434
+ if (expression.name === 'E')
435
+ return { kind: 'scalar', value: Math.E };
415
436
  const unit = units.get(expression.name);
416
437
  if (!unit)
417
438
  return failDeclaration(`Unknown or not-yet-declared unit '${expression.name}'`, expression.source);
418
- return { vector: unit.vector, scale: unit.scale, hasUnit: true };
419
- }
420
- if (expression.type === 'binaryExpression' && (expression.operator === '*' || expression.operator === '/')) {
421
- const left = resolveUnitExpression(expression.left);
422
- const right = resolveUnitExpression(expression.right);
423
- const divide = expression.operator === '/';
424
439
  return {
425
- vector: combineVectors(left.vector, right.vector, divide ? -1 : 1, expression.source),
426
- scale: validateUnitScale(divide ? left.scale / right.scale : left.scale * right.scale, expression.source),
427
- hasUnit: left.hasUnit || right.hasUnit
440
+ kind: 'unit',
441
+ vector: unit.vector,
442
+ scale: validateUnitScale(unit.scale, expression.source),
443
+ offset: validateUnitOffset(unit.offset, expression.source)
428
444
  };
429
445
  }
430
- if (expression.type === 'binaryExpression' && expression.operator === '^') {
431
- const exponent = integerExponent(expression.right);
432
- if (exponent === undefined)
433
- return failDeclaration('Unit powers must be signed integer literals', expression.right.source ?? expression.source);
434
- const base = resolveUnitExpression(expression.left);
435
- const vector = new Map();
436
- for (const [identity, value] of base.vector) {
437
- const powered = value * exponent;
438
- if (!Number.isSafeInteger(powered))
439
- failDeclaration('Dimension exponent is outside the supported integer range', expression.source);
440
- if (powered !== 0)
441
- vector.set(identity, powered);
446
+ if (expression.type === 'unaryExpression') {
447
+ if (expression.operator !== '-')
448
+ return failDeclaration('Unit expressions may use only finite scalars, visible units, PI, E, +, -, *, /, and signed integer powers', expression.source);
449
+ const argument = resolveUnitExpression(expression.argument);
450
+ if (argument.kind !== 'scalar')
451
+ return failDeclaration('Unit declaration unary minus requires a dimensionless scalar', expression.source);
452
+ return { kind: 'scalar', value: validateUnitExpressionScalar(-argument.value, expression.source) };
453
+ }
454
+ if (expression.type === 'binaryExpression') {
455
+ const left = resolveUnitExpression(expression.left);
456
+ const right = resolveUnitExpression(expression.right);
457
+ if (expression.operator === '+' || expression.operator === '-') {
458
+ if (left.kind === 'scalar' && right.kind === 'scalar') {
459
+ return {
460
+ kind: 'scalar',
461
+ value: validateUnitExpressionScalar(expression.operator === '+' ? left.value + right.value : left.value - right.value, operatorSource)
462
+ };
463
+ }
464
+ if (left.kind === 'unit' && right.kind === 'unit') {
465
+ return failDeclaration('Unit declaration addition and subtraction require one affine unit term and one dimensionless scalar', operatorSource);
466
+ }
467
+ if (left.kind === 'scalar' && right.kind === 'unit') {
468
+ if (expression.operator === '-') {
469
+ return failDeclaration('Unit declaration subtraction requires the unit term on the left', operatorSource);
470
+ }
471
+ return {
472
+ kind: 'unit',
473
+ vector: right.vector,
474
+ scale: right.scale,
475
+ offset: validateUnitOffset(right.offset + left.value, operatorSource)
476
+ };
477
+ }
478
+ if (left.kind === 'unit' && right.kind === 'scalar') {
479
+ return {
480
+ kind: 'unit',
481
+ vector: left.vector,
482
+ scale: left.scale,
483
+ offset: validateUnitOffset(expression.operator === '+' ? left.offset + right.value : left.offset - right.value, operatorSource)
484
+ };
485
+ }
486
+ return failDeclaration('Unit declaration addition and subtraction require one affine unit term and one dimensionless scalar', operatorSource);
487
+ }
488
+ if (expression.operator === '*' || expression.operator === '/') {
489
+ const divide = expression.operator === '/';
490
+ if (left.kind === 'scalar' && right.kind === 'scalar') {
491
+ if (divide && right.value === 0) {
492
+ return failDeclaration('Unit expression scalar division requires a non-zero denominator', operatorSource);
493
+ }
494
+ return {
495
+ kind: 'scalar',
496
+ value: validateUnitExpressionScalar(divide ? left.value / right.value : left.value * right.value, operatorSource)
497
+ };
498
+ }
499
+ if (left.kind === 'unit' && right.kind === 'scalar') {
500
+ if (right.value <= 0)
501
+ return failDeclaration('Unit scale must be finite and greater than zero', operatorSource);
502
+ return scaledUnit(left, divide ? 1 / right.value : right.value, operatorSource);
503
+ }
504
+ if (left.kind === 'scalar' && right.kind === 'unit') {
505
+ if (!divide)
506
+ return scaledUnit(right, left.value, operatorSource);
507
+ if (right.offset !== 0) {
508
+ return failDeclaration('Affine units may not be multiplied, divided, or raised to non-identity powers in declarations', operatorSource);
509
+ }
510
+ return {
511
+ kind: 'unit',
512
+ vector: combineVectors(new Map(), right.vector, -1, operatorSource),
513
+ scale: validateUnitScale(left.value / right.scale, operatorSource),
514
+ offset: 0
515
+ };
516
+ }
517
+ if (left.kind === 'unit' && right.kind === 'unit') {
518
+ if (left.offset !== 0 || right.offset !== 0) {
519
+ return failDeclaration('Affine units may not be multiplied, divided, or raised to non-identity powers in declarations', operatorSource);
520
+ }
521
+ return {
522
+ kind: 'unit',
523
+ vector: combineVectors(left.vector, right.vector, divide ? -1 : 1, operatorSource),
524
+ scale: validateUnitScale(divide ? left.scale / right.scale : left.scale * right.scale, operatorSource),
525
+ offset: 0
526
+ };
527
+ }
528
+ return failDeclaration('Unit declaration multiplication and division require a supported scalar or affine unit combination', operatorSource);
529
+ }
530
+ if (expression.operator === '^') {
531
+ const exponent = integerExponent(expression.right);
532
+ if (exponent === undefined)
533
+ return failDeclaration('Unit powers must be signed integer literals', expression.right.source ?? operatorSource);
534
+ if (left.kind === 'scalar') {
535
+ return { kind: 'scalar', value: validateUnitExpressionScalar(Math.pow(left.value, exponent), operatorSource) };
536
+ }
537
+ if (exponent === 1)
538
+ return left;
539
+ if (left.offset !== 0) {
540
+ return failDeclaration('Affine units may not be multiplied, divided, or raised to non-identity powers in declarations', operatorSource);
541
+ }
542
+ const vector = new Map();
543
+ for (const [identity, value] of left.vector) {
544
+ const powered = value * exponent;
545
+ if (!Number.isSafeInteger(powered))
546
+ failDeclaration('Dimension exponent is outside the supported integer range', operatorSource);
547
+ if (powered !== 0)
548
+ vector.set(identity, powered);
549
+ }
550
+ return {
551
+ kind: 'unit',
552
+ vector: normalizedVector(vector),
553
+ scale: validateUnitScale(Math.pow(left.scale, exponent), operatorSource),
554
+ offset: 0
555
+ };
442
556
  }
443
- return {
444
- vector: normalizedVector(vector),
445
- scale: validateUnitScale(Math.pow(base.scale, exponent), expression.source),
446
- hasUnit: base.hasUnit
447
- };
448
557
  }
449
- return failDeclaration('Unit expressions may use only positive finite numbers, visible units, *, /, and integer powers', expression.source);
558
+ return failDeclaration('Unit expressions may use only finite scalars, visible units, PI, E, +, -, *, /, and signed integer powers', expression.source);
450
559
  }
451
560
  function checkDimension(statement) {
452
561
  if (loopDepth > 0)
@@ -482,6 +591,7 @@ export function checkDocument(doc, file, context) {
482
591
  }
483
592
  let vector;
484
593
  let scale;
594
+ let offset;
485
595
  if (statement.dimension) {
486
596
  const dimension = dimensions.get(statement.dimension);
487
597
  if (!dimension)
@@ -494,13 +604,15 @@ export function checkDocument(doc, file, context) {
494
604
  failDeclaration(`Base dimension '${statement.dimension}' already has independent unit '${existing.name}'`, statement.dimensionSource ?? statement.source, existing.source);
495
605
  vector = dimension.vector;
496
606
  scale = 1;
607
+ offset = 0;
497
608
  }
498
609
  else if (statement.expression) {
499
610
  const resolved = resolveUnitExpression(statement.expression);
500
- if (!resolved.hasUnit)
501
- failDeclaration('A derived unit must reference at least one visible unit', statement.expression.source);
611
+ if (resolved.kind === 'scalar')
612
+ return failDeclaration('A derived unit must reference at least one visible unit', statement.expression.source);
502
613
  vector = resolved.vector;
503
614
  scale = validateUnitScale(resolved.scale, statement.expression.source);
615
+ offset = validateUnitOffset(resolved.offset, statement.expression.source);
504
616
  }
505
617
  else {
506
618
  return failDeclaration('Unit declaration requires a base dimension or a unit expression', statement.source);
@@ -512,6 +624,7 @@ export function checkDocument(doc, file, context) {
512
624
  identity: JSON.stringify([moduleIdentity, statement.name]),
513
625
  vector: normalizedVector(new Map(vector)),
514
626
  scale,
627
+ offset,
515
628
  source: statement.nameSource ?? statement.source
516
629
  };
517
630
  units.set(statement.name, metadata);
@@ -576,7 +689,7 @@ export function checkDocument(doc, file, context) {
576
689
  if (expression.type === 'measurementAttachment') {
577
690
  const value = constantValue(expression.value);
578
691
  const unit = units.get(expression.unit);
579
- return typeof value === 'number' && unit ? value * unit.scale : undefined;
692
+ return typeof value === 'number' && unit ? physicalValue(value, unit) : undefined;
580
693
  }
581
694
  if (expression.type === 'measurementConversion')
582
695
  return literalMeasurementValue(expression.value);
@@ -685,7 +798,7 @@ export function checkDocument(doc, file, context) {
685
798
  fail('AMX3010', 'Measurement value must be finite', expression.source);
686
799
  }
687
800
  const value = constantValue(expression.value);
688
- if (typeof value === 'number' && !Number.isFinite(value * unit.scale)) {
801
+ if (typeof value === 'number' && !Number.isFinite(physicalValue(value, unit))) {
689
802
  fail('AMX3010', 'Measurement physical value must be finite', expression.source);
690
803
  }
691
804
  return { kind: 'measurement', vector: unit.vector };
@@ -1339,10 +1452,16 @@ export function checkDocument(doc, file, context) {
1339
1452
  checkViewName(view.name, view.source, view.exported);
1340
1453
  oneOption(view, 'viewTitleOption', 'title', true);
1341
1454
  oneOption(view, 'viewDescriptionOption', 'description', true);
1342
- const allowed = new Set(['viewTitleOption', 'viewDescriptionOption', 'chartFieldOption', 'chartSeriesOption']);
1455
+ const showTable = oneOption(view, 'chartShowTableOption', "'showTable'", false);
1456
+ const allowed = new Set(['viewTitleOption', 'viewDescriptionOption', 'chartFieldOption', 'chartSeriesOption', 'chartShowTableOption']);
1343
1457
  const unsupported = view.options.find(option => !allowed.has(option.type));
1344
1458
  if (unsupported)
1345
1459
  fail('AMX3003', 'Option is not valid for a chart', unsupported.source);
1460
+ if (showTable) {
1461
+ const actual = infer(showTable.expression);
1462
+ if (!equal(actual, named('Boolean')))
1463
+ fail('AMX3002', `Expected Boolean, got ${format(actual)}`, showTable.expression.source);
1464
+ }
1346
1465
  const input = viewListElement(view.binding, view.bindingSource);
1347
1466
  const fields = optionsOf(view, 'chartFieldOption');
1348
1467
  const series = optionsOf(view, 'chartSeriesOption');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nova-energy/openamx",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "description": "OpenAMX is an open-source, text-based, computable document format for engineering knowledge.",
5
5
  "type": "module",
6
6
  "bin": {