@nova-energy/openamx 0.16.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 });
@@ -137,6 +138,74 @@ function expressionReferencesIdentifier(expression, name) {
137
138
  default: return false;
138
139
  }
139
140
  }
141
+ function identifierSource(expression, name) {
142
+ if (expression.type === 'identifier')
143
+ return expression.name === name ? expression.source : undefined;
144
+ switch (expression.type) {
145
+ case 'binaryExpression': return identifierSource(expression.left, name) ?? identifierSource(expression.right, name);
146
+ case 'typeTestExpression':
147
+ case 'measurementAttachment':
148
+ case 'measurementConversion':
149
+ return identifierSource(expression.value, name);
150
+ case 'unaryExpression': return identifierSource(expression.argument, name);
151
+ case 'conditionalExpression': return identifierSource(expression.test, name)
152
+ ?? identifierSource(expression.consequent, name) ?? identifierSource(expression.alternate, name);
153
+ case 'bracedIfExpression': return identifierSource(expression.test, name)
154
+ ?? bodyIdentifierSource(expression.consequent, name) ?? bodyIdentifierSource(expression.alternate, name);
155
+ case 'stringInterpolation':
156
+ return expression.parts.find((part) => typeof part !== 'string')
157
+ ? expression.parts.reduce((source, part) => source ?? (typeof part === 'string' ? undefined : identifierSource(part, name)), undefined)
158
+ : undefined;
159
+ case 'matchExpression': return identifierSource(expression.expression, name)
160
+ ?? expression.cases.reduce((source, item) => source ?? identifierSource(item.expression, name), undefined)
161
+ ?? identifierSource(expression.defaultExpression, name);
162
+ case 'rangeExpression': return identifierSource(expression.start, name) ?? identifierSource(expression.end, name);
163
+ case 'functionCall': return expression.arguments.reduce((source, argument) => source ?? identifierSource(argument, name), undefined);
164
+ case 'listLiteral': return expression.elements.reduce((source, item) => source ?? identifierSource(item, name), undefined);
165
+ case 'forExpression': return identifierSource(expression.iterable, name) ?? bodyIdentifierSource(expression.body, name);
166
+ case 'recordConstructor': return expression.fields.reduce((source, field) => source ?? identifierSource(field.expression, name), undefined);
167
+ case 'fieldAccess': return identifierSource(expression.receiver, name);
168
+ case 'listAccess': return identifierSource(expression.receiver, name) ?? identifierSource(expression.index, name);
169
+ default: return undefined;
170
+ }
171
+ }
172
+ function bodyIdentifierSource(statements, name) {
173
+ for (const statement of statements) {
174
+ let source;
175
+ switch (statement.type) {
176
+ case 'variableDeclaration':
177
+ source = identifierSource(statement.expression, name);
178
+ break;
179
+ case 'assignmentStatement':
180
+ case 'compoundAssignmentStatement':
181
+ source = identifierSource(statement.expression, name);
182
+ break;
183
+ case 'returnStatement':
184
+ source = identifierSource(statement.expression, name);
185
+ break;
186
+ case 'forStatement':
187
+ source = identifierSource(statement.iterable, name) ?? bodyIdentifierSource(statement.body, name);
188
+ break;
189
+ case 'bracedIfStatement':
190
+ source = identifierSource(statement.test, name)
191
+ ?? bodyIdentifierSource(statement.consequent, name)
192
+ ?? (statement.alternate ? bodyIdentifierSource(statement.alternate, name) : undefined);
193
+ break;
194
+ case 'addStatement':
195
+ source = identifierSource(statement.value, name)
196
+ ?? (statement.index ? identifierSource(statement.index, name) : undefined);
197
+ break;
198
+ case 'removeStatement':
199
+ source = identifierSource(statement.count, name)
200
+ ?? (statement.index ? identifierSource(statement.index, name) : undefined);
201
+ break;
202
+ default: source = undefined;
203
+ }
204
+ if (source)
205
+ return source;
206
+ }
207
+ return undefined;
208
+ }
140
209
  function bodyReferencesIdentifier(statements, name) {
141
210
  return statements.some(statement => {
142
211
  switch (statement.type) {
@@ -334,51 +403,159 @@ export function checkDocument(doc, file, context) {
334
403
  }
335
404
  return failDeclaration('Dimension expressions may use only visible dimensions, *, /, and integer powers', expression.source);
336
405
  }
406
+ function validateUnitExpressionScalar(value, source) {
407
+ if (!Number.isFinite(value))
408
+ failDeclaration('Unit expression scalar must be finite', source);
409
+ return value;
410
+ }
337
411
  function validateUnitScale(scale, source) {
338
412
  if (!Number.isFinite(scale) || scale <= 0)
339
413
  failDeclaration('Unit scale must be finite and greater than zero', source);
340
414
  return scale;
341
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
+ }
342
426
  function resolveUnitExpression(expression) {
427
+ const operatorSource = expression.type === 'binaryExpression' ? expression.operatorSource ?? expression.source : expression.source;
343
428
  if (expression.type === 'numberLiteral') {
344
- return { vector: new Map(), scale: validateUnitScale(expression.value, expression.source), hasUnit: false };
429
+ return { kind: 'scalar', value: validateUnitExpressionScalar(expression.value, expression.source) };
345
430
  }
346
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 };
347
436
  const unit = units.get(expression.name);
348
437
  if (!unit)
349
438
  return failDeclaration(`Unknown or not-yet-declared unit '${expression.name}'`, expression.source);
350
- return { vector: unit.vector, scale: unit.scale, hasUnit: true };
351
- }
352
- if (expression.type === 'binaryExpression' && (expression.operator === '*' || expression.operator === '/')) {
353
- const left = resolveUnitExpression(expression.left);
354
- const right = resolveUnitExpression(expression.right);
355
- const divide = expression.operator === '/';
356
439
  return {
357
- vector: combineVectors(left.vector, right.vector, divide ? -1 : 1, expression.source),
358
- scale: validateUnitScale(divide ? left.scale / right.scale : left.scale * right.scale, expression.source),
359
- 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)
360
444
  };
361
445
  }
362
- if (expression.type === 'binaryExpression' && expression.operator === '^') {
363
- const exponent = integerExponent(expression.right);
364
- if (exponent === undefined)
365
- return failDeclaration('Unit powers must be signed integer literals', expression.right.source ?? expression.source);
366
- const base = resolveUnitExpression(expression.left);
367
- const vector = new Map();
368
- for (const [identity, value] of base.vector) {
369
- const powered = value * exponent;
370
- if (!Number.isSafeInteger(powered))
371
- failDeclaration('Dimension exponent is outside the supported integer range', expression.source);
372
- if (powered !== 0)
373
- 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
+ };
374
556
  }
375
- return {
376
- vector: normalizedVector(vector),
377
- scale: validateUnitScale(Math.pow(base.scale, exponent), expression.source),
378
- hasUnit: base.hasUnit
379
- };
380
557
  }
381
- 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);
382
559
  }
383
560
  function checkDimension(statement) {
384
561
  if (loopDepth > 0)
@@ -414,6 +591,7 @@ export function checkDocument(doc, file, context) {
414
591
  }
415
592
  let vector;
416
593
  let scale;
594
+ let offset;
417
595
  if (statement.dimension) {
418
596
  const dimension = dimensions.get(statement.dimension);
419
597
  if (!dimension)
@@ -426,13 +604,15 @@ export function checkDocument(doc, file, context) {
426
604
  failDeclaration(`Base dimension '${statement.dimension}' already has independent unit '${existing.name}'`, statement.dimensionSource ?? statement.source, existing.source);
427
605
  vector = dimension.vector;
428
606
  scale = 1;
607
+ offset = 0;
429
608
  }
430
609
  else if (statement.expression) {
431
610
  const resolved = resolveUnitExpression(statement.expression);
432
- if (!resolved.hasUnit)
433
- 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);
434
613
  vector = resolved.vector;
435
614
  scale = validateUnitScale(resolved.scale, statement.expression.source);
615
+ offset = validateUnitOffset(resolved.offset, statement.expression.source);
436
616
  }
437
617
  else {
438
618
  return failDeclaration('Unit declaration requires a base dimension or a unit expression', statement.source);
@@ -444,6 +624,7 @@ export function checkDocument(doc, file, context) {
444
624
  identity: JSON.stringify([moduleIdentity, statement.name]),
445
625
  vector: normalizedVector(new Map(vector)),
446
626
  scale,
627
+ offset,
447
628
  source: statement.nameSource ?? statement.source
448
629
  };
449
630
  units.set(statement.name, metadata);
@@ -508,7 +689,7 @@ export function checkDocument(doc, file, context) {
508
689
  if (expression.type === 'measurementAttachment') {
509
690
  const value = constantValue(expression.value);
510
691
  const unit = units.get(expression.unit);
511
- return typeof value === 'number' && unit ? value * unit.scale : undefined;
692
+ return typeof value === 'number' && unit ? physicalValue(value, unit) : undefined;
512
693
  }
513
694
  if (expression.type === 'measurementConversion')
514
695
  return literalMeasurementValue(expression.value);
@@ -617,7 +798,7 @@ export function checkDocument(doc, file, context) {
617
798
  fail('AMX3010', 'Measurement value must be finite', expression.source);
618
799
  }
619
800
  const value = constantValue(expression.value);
620
- if (typeof value === 'number' && !Number.isFinite(value * unit.scale)) {
801
+ if (typeof value === 'number' && !Number.isFinite(physicalValue(value, unit))) {
621
802
  fail('AMX3010', 'Measurement physical value must be finite', expression.source);
622
803
  }
623
804
  return { kind: 'measurement', vector: unit.vector };
@@ -1271,10 +1452,16 @@ export function checkDocument(doc, file, context) {
1271
1452
  checkViewName(view.name, view.source, view.exported);
1272
1453
  oneOption(view, 'viewTitleOption', 'title', true);
1273
1454
  oneOption(view, 'viewDescriptionOption', 'description', true);
1274
- 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']);
1275
1457
  const unsupported = view.options.find(option => !allowed.has(option.type));
1276
1458
  if (unsupported)
1277
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
+ }
1278
1465
  const input = viewListElement(view.binding, view.bindingSource);
1279
1466
  const fields = optionsOf(view, 'chartFieldOption');
1280
1467
  const series = optionsOf(view, 'chartSeriesOption');
@@ -1405,7 +1592,44 @@ export function checkDocument(doc, file, context) {
1405
1592
  localBindings.set(time.name, named('Number'));
1406
1593
  bindings = localBindings;
1407
1594
  try {
1408
- for (const element of diagram.elements) {
1595
+ function checkDiagramContent(content) {
1596
+ for (const node of content) {
1597
+ if (node.type === 'diagramElement') {
1598
+ checkDiagramElement(node);
1599
+ continue;
1600
+ }
1601
+ if (node.type === 'diagramForBlock') {
1602
+ if (node.variable === time.name) {
1603
+ fail('AMXD6001', `Variable [${time.name}] is already used to control the diagrams timeline. Use a different variable name.`, node.variableSource ?? node.source);
1604
+ }
1605
+ if (expressionReferencesIdentifier(node.iterable, time.name)) {
1606
+ fail('AMXD6001', `Variable [${time.name}] is already used to control the diagrams timeline. Use a different variable name.`, identifierSource(node.iterable, time.name) ?? node.iterableSource ?? node.source);
1607
+ }
1608
+ const iterable = infer(node.iterable);
1609
+ if (iterable.kind !== 'list') {
1610
+ fail(iterable.kind === 'measurement' ? 'AMX3007' : 'AMX3003', 'For loop needs a list or range', node.iterableSource ?? node.source);
1611
+ }
1612
+ const outerBindings = bindings;
1613
+ bindings = new Map(outerBindings);
1614
+ bindings.set(node.variable, iterable.element);
1615
+ try {
1616
+ checkDiagramContent(node.content);
1617
+ }
1618
+ finally {
1619
+ bindings = outerBindings;
1620
+ }
1621
+ continue;
1622
+ }
1623
+ if (expressionReferencesIdentifier(node.test, time.name)) {
1624
+ fail('AMXD6001', `Variable [${time.name}] is already used to control the diagrams timeline. Use a different variable name.`, identifierSource(node.test, time.name) ?? node.testSource ?? node.source);
1625
+ }
1626
+ requireType(infer(node.test), named('Boolean'), node.testSource ?? node.source);
1627
+ checkDiagramContent(node.consequent);
1628
+ if (node.alternate)
1629
+ checkDiagramContent(node.alternate);
1630
+ }
1631
+ }
1632
+ function checkDiagramElement(element) {
1409
1633
  diagramElementCount++;
1410
1634
  if (diagramElementCount > 512) {
1411
1635
  fail('AMXD013', 'A single report can have up to 512 elements maximum.', element.source);
@@ -1465,6 +1689,7 @@ export function checkDocument(doc, file, context) {
1465
1689
  }
1466
1690
  }
1467
1691
  }
1692
+ checkDiagramContent(diagram.content);
1468
1693
  }
1469
1694
  finally {
1470
1695
  bindings = outerBindings;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nova-energy/openamx",
3
- "version": "0.16.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": {