@nova-energy/openamx 0.0.0-stage → 0.10.1

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.
Files changed (42) hide show
  1. package/COMMERCIAL-LICENSE.md +74 -0
  2. package/LICENSE.md +660 -0
  3. package/README.md +513 -2
  4. package/dist/ast/types.js +2 -0
  5. package/dist/cli.js +186 -0
  6. package/dist/diagnostics/errors.js +94 -0
  7. package/dist/editor/completion.js +313 -0
  8. package/dist/editor/highlighting.js +224 -0
  9. package/dist/editor/moduleAnalysis.js +206 -0
  10. package/dist/editor/refactoring.js +46 -0
  11. package/dist/editor/sourceRanges.js +50 -0
  12. package/dist/editor/symbols.js +203 -0
  13. package/dist/formatter/formatAmx.js +79 -0
  14. package/dist/index.js +10 -0
  15. package/dist/parser/parseDocument.js +114 -0
  16. package/dist/parser/parseExpression.js +768 -0
  17. package/dist/parser/parseFor.js +53 -0
  18. package/dist/parser/parseFrontMatter.js +65 -0
  19. package/dist/parser/parseStatements.js +667 -0
  20. package/dist/parser/stringScanner.js +71 -0
  21. package/dist/renderer/chartModel.js +476 -0
  22. package/dist/renderer/renderHtml.js +383 -0
  23. package/dist/renderer/reportDocx.js +163 -0
  24. package/dist/renderer/reportPdf.js +202 -0
  25. package/dist/renderer/reportPreparation.js +208 -0
  26. package/dist/runtime/csvTextSerialization.js +8 -0
  27. package/dist/runtime/dataText.js +191 -0
  28. package/dist/runtime/docxDestination.js +55 -0
  29. package/dist/runtime/environment.js +105 -0
  30. package/dist/runtime/evaluateDocument.js +32 -0
  31. package/dist/runtime/evaluateExpression.js +888 -0
  32. package/dist/runtime/externalUnits.js +91 -0
  33. package/dist/runtime/inputData.js +508 -0
  34. package/dist/runtime/measurement.js +110 -0
  35. package/dist/runtime/moduleLoader.js +279 -0
  36. package/dist/runtime/outputData.js +303 -0
  37. package/dist/runtime/pdfDestination.js +57 -0
  38. package/dist/runtime/standardLibrary.js +145 -0
  39. package/dist/typechecker/checkDocument.js +1282 -0
  40. package/dist/typechecker/declarationRegistry.js +7 -0
  41. package/dist/typechecker/dimensionTypes.js +1 -0
  42. package/package.json +33 -4
@@ -0,0 +1,105 @@
1
+ import { throwUndefinedIdentifier } from '../diagnostics/errors';
2
+ /**
3
+ * Runtime environment for variable storage during evaluation.
4
+ * Variables are stored in declaration order (source order).
5
+ * get() throws AMX1004 for undefined identifiers.
6
+ */
7
+ export class Environment {
8
+ store = new Map();
9
+ recordTypes;
10
+ functions;
11
+ bindingTypes = new Map();
12
+ dimensionRegistry;
13
+ viewDefinitions = new Map();
14
+ viewEmissions = [];
15
+ immutableValues = new WeakSet();
16
+ currentDocumentNodeIndex = -1;
17
+ currentStatementIndex = -1;
18
+ validationMode = 'aggregate';
19
+ constructor(recordTypes = new Map(), functions = new Map(), dimensionRegistry) {
20
+ this.recordTypes = recordTypes;
21
+ this.functions = functions;
22
+ this.dimensionRegistry = dimensionRegistry;
23
+ }
24
+ /**
25
+ * Create an isolated call frame sharing this module's record types and functions
26
+ * but containing only the supplied parameter bindings. Used to evaluate pure
27
+ * function bodies without exposing document/module state.
28
+ */
29
+ createCallFrame(parameters) {
30
+ const frame = new Environment(this.recordTypes, this.functions, this.dimensionRegistry);
31
+ frame.validationMode = this.validationMode;
32
+ for (const [name, type] of this.bindingTypes)
33
+ frame.bindingTypes.set(name, type);
34
+ for (const [name, value] of Object.entries(parameters)) {
35
+ frame.set(name, value);
36
+ if (this.isImmutable(value))
37
+ frame.markImmutable(value);
38
+ }
39
+ return frame;
40
+ }
41
+ /**
42
+ * Set or overwrite a variable value.
43
+ */
44
+ set(name, value) {
45
+ this.store.set(name, value);
46
+ }
47
+ markImmutable(value) {
48
+ const visit = (item) => {
49
+ if (!item || typeof item !== 'object' || this.immutableValues.has(item))
50
+ return;
51
+ this.immutableValues.add(item);
52
+ if (Array.isArray(item))
53
+ item.forEach(visit);
54
+ else
55
+ Object.values(item).forEach(visit);
56
+ };
57
+ visit(value);
58
+ }
59
+ isImmutable(value) {
60
+ return !!value && typeof value === 'object' && this.immutableValues.has(value);
61
+ }
62
+ update(name, value, source, file) {
63
+ if (!this.store.has(name)) {
64
+ throwUndefinedIdentifier(name, source, file);
65
+ }
66
+ this.store.set(name, value);
67
+ }
68
+ delete(name) {
69
+ this.store.delete(name);
70
+ }
71
+ /**
72
+ * Get a variable value. Throws AMX1004 if not defined.
73
+ */
74
+ get(name, source, file) {
75
+ if (!this.store.has(name)) {
76
+ throwUndefinedIdentifier(name, source, file);
77
+ }
78
+ return this.store.get(name);
79
+ }
80
+ /**
81
+ * Check existence (used internally by evaluator for forward-ref detection).
82
+ */
83
+ has(name) {
84
+ return this.store.has(name);
85
+ }
86
+ /**
87
+ * Return a plain object snapshot of current bindings (for evaluateDocument result).
88
+ */
89
+ toObject() {
90
+ const obj = {};
91
+ for (const [k, v] of this.store.entries()) {
92
+ obj[k] = v;
93
+ }
94
+ return obj;
95
+ }
96
+ /**
97
+ * Clear all bindings (useful for tests).
98
+ */
99
+ clear() {
100
+ this.store.clear();
101
+ this.immutableValues = new WeakSet();
102
+ this.viewDefinitions.clear();
103
+ this.viewEmissions.length = 0;
104
+ }
105
+ }
@@ -0,0 +1,32 @@
1
+ import { Environment } from './environment';
2
+ import { evaluateStatements } from './evaluateExpression';
3
+ import { checkDocument } from '../typechecker/checkDocument';
4
+ /**
5
+ * Evaluate an OpenAmxDocument in source order.
6
+ * - Only executable code-block statements are processed.
7
+ * - All blocks share one environment and execute in document order.
8
+ * - Narrative nodes and legacy top-level declarations are ignored.
9
+ * Returns a plain object map of final variable bindings.
10
+ */
11
+ export function evaluateDocument(doc, file) {
12
+ return evaluateDocumentEnvironment(doc, file).toObject();
13
+ }
14
+ /** Evaluate executable blocks once and return their shared final environment. */
15
+ export function evaluateDocumentEnvironment(doc, file) {
16
+ const checked = checkDocument(doc, file);
17
+ const env = new Environment(new Map(), new Map(), {
18
+ dimensions: checked.dimensions,
19
+ units: checked.units,
20
+ baseUnits: checked.baseUnits
21
+ });
22
+ for (const [name, type] of checked.bindingTypes)
23
+ env.bindingTypes.set(name, type);
24
+ for (let nodeIndex = 0; nodeIndex < doc.nodes.length; nodeIndex++) {
25
+ const node = doc.nodes[nodeIndex];
26
+ if (node.type === 'executableCodeBlock') {
27
+ env.currentDocumentNodeIndex = nodeIndex;
28
+ evaluateStatements(node.statements, env, file);
29
+ }
30
+ }
31
+ return env;
32
+ }