@nova-energy/openamx 0.11.0 → 0.14.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 +42 -10
- package/dist/cli.js +5 -5
- package/dist/editor/completion.js +199 -6
- package/dist/editor/highlighting.js +63 -4
- package/dist/editor/moduleAnalysis.js +8 -2
- package/dist/editor/refactoring.js +5 -2
- package/dist/editor/sourceRanges.js +3 -1
- package/dist/editor/symbols.js +195 -30
- package/dist/formatter/formatAmx.js +83 -7
- package/dist/parser/parseExpression.js +82 -3
- package/dist/parser/parseIf.js +180 -0
- package/dist/parser/parseStatements.js +232 -7
- package/dist/renderer/diagramBrowserEntry.js +255 -0
- package/dist/renderer/diagramBrowserRuntime.js +2 -0
- package/dist/renderer/diagramHtml.js +47 -0
- package/dist/renderer/narrativeModel.js +36 -11
- package/dist/renderer/renderHtml.js +95 -41
- package/dist/renderer/reportDocx.js +38 -16
- package/dist/renderer/reportPdf.js +122 -35
- package/dist/renderer/reportPreparation.js +97 -23
- package/dist/renderer/reportSequence.js +29 -0
- package/dist/renderer/staticDiagram.js +94 -0
- package/dist/runtime/diagramModel.js +455 -0
- package/dist/runtime/environment.js +8 -0
- package/dist/runtime/evaluateExpression.js +194 -10
- package/dist/runtime/inputData.js +20 -18
- package/dist/runtime/moduleLoader.js +76 -4
- package/dist/runtime/outputData.js +18 -12
- package/dist/runtime/standardLibrary.js +208 -1
- package/dist/runtime/valueSerialization.js +12 -0
- package/dist/typechecker/checkDocument.js +496 -16
- package/package.json +1 -1
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { AmxError, staticError } from '../diagnostics/errors';
|
|
2
|
+
import { fieldRegistry, setDeclaringRegistry } from '../typechecker/declarationRegistry';
|
|
3
|
+
import { evaluateExpression } from './evaluateExpression';
|
|
4
|
+
import { Environment } from './environment';
|
|
5
|
+
const capturedContexts = new WeakMap();
|
|
6
|
+
function serializeValue(value, ancestors = new Set()) {
|
|
7
|
+
if (value === undefined)
|
|
8
|
+
return ['undefined'];
|
|
9
|
+
if (value === null)
|
|
10
|
+
return ['null'];
|
|
11
|
+
if (typeof value === 'boolean')
|
|
12
|
+
return ['boolean', value];
|
|
13
|
+
if (typeof value === 'number') {
|
|
14
|
+
return ['number', Number.isNaN(value) ? 'nan'
|
|
15
|
+
: value === Infinity ? 'positive-infinity'
|
|
16
|
+
: value === -Infinity ? 'negative-infinity' : value];
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === 'string')
|
|
19
|
+
return ['string', value];
|
|
20
|
+
if (typeof value !== 'object')
|
|
21
|
+
throw new Error(`Unsupported diagram context value: ${typeof value}`);
|
|
22
|
+
if (ancestors.has(value))
|
|
23
|
+
throw new Error('Diagram context contains a cyclic value and cannot be serialized.');
|
|
24
|
+
ancestors.add(value);
|
|
25
|
+
try {
|
|
26
|
+
if (Array.isArray(value))
|
|
27
|
+
return ['array', value.map(item => serializeValue(item, ancestors))];
|
|
28
|
+
if (value instanceof Map) {
|
|
29
|
+
return ['map', [...value].map(([key, item]) => [serializeValue(key, ancestors), serializeValue(item, ancestors)])];
|
|
30
|
+
}
|
|
31
|
+
if (value instanceof Set)
|
|
32
|
+
return ['set', [...value].map(item => serializeValue(item, ancestors))];
|
|
33
|
+
return ['object', Object.entries(value).map(([key, item]) => [key, serializeValue(item, ancestors)])];
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
ancestors.delete(value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function deserializeValue(value) {
|
|
40
|
+
if (!Array.isArray(value) || typeof value[0] !== 'string')
|
|
41
|
+
throw new Error('Invalid serialized diagram value.');
|
|
42
|
+
switch (value[0]) {
|
|
43
|
+
case 'undefined':
|
|
44
|
+
if (value.length !== 1)
|
|
45
|
+
throw new Error('Invalid serialized undefined diagram value.');
|
|
46
|
+
return undefined;
|
|
47
|
+
case 'null':
|
|
48
|
+
if (value.length !== 1)
|
|
49
|
+
throw new Error('Invalid serialized null diagram value.');
|
|
50
|
+
return null;
|
|
51
|
+
case 'boolean':
|
|
52
|
+
if (value.length !== 2 || typeof value[1] !== 'boolean')
|
|
53
|
+
throw new Error('Invalid serialized boolean diagram value.');
|
|
54
|
+
return value[1];
|
|
55
|
+
case 'number':
|
|
56
|
+
if (value.length !== 2)
|
|
57
|
+
throw new Error('Invalid serialized number diagram value.');
|
|
58
|
+
if (value[1] === 'nan')
|
|
59
|
+
return NaN;
|
|
60
|
+
if (value[1] === 'positive-infinity')
|
|
61
|
+
return Infinity;
|
|
62
|
+
if (value[1] === 'negative-infinity')
|
|
63
|
+
return -Infinity;
|
|
64
|
+
if (typeof value[1] !== 'number')
|
|
65
|
+
throw new Error('Invalid serialized number diagram value.');
|
|
66
|
+
return value[1];
|
|
67
|
+
case 'string':
|
|
68
|
+
if (value.length !== 2 || typeof value[1] !== 'string')
|
|
69
|
+
throw new Error('Invalid serialized string diagram value.');
|
|
70
|
+
return value[1];
|
|
71
|
+
case 'array':
|
|
72
|
+
if (value.length !== 2 || !Array.isArray(value[1]))
|
|
73
|
+
throw new Error('Invalid serialized array diagram value.');
|
|
74
|
+
return value[1].map(deserializeValue);
|
|
75
|
+
case 'object': {
|
|
76
|
+
if (value.length !== 2 || !Array.isArray(value[1]))
|
|
77
|
+
throw new Error('Invalid serialized object diagram value.');
|
|
78
|
+
const entries = value[1].map(entry => {
|
|
79
|
+
if (!Array.isArray(entry) || entry.length !== 2 || typeof entry[0] !== 'string') {
|
|
80
|
+
throw new Error('Invalid serialized object entry in diagram value.');
|
|
81
|
+
}
|
|
82
|
+
return [entry[0], deserializeValue(entry[1])];
|
|
83
|
+
});
|
|
84
|
+
return Object.fromEntries(entries);
|
|
85
|
+
}
|
|
86
|
+
case 'map':
|
|
87
|
+
if (value.length !== 2 || !Array.isArray(value[1]))
|
|
88
|
+
throw new Error('Invalid serialized map diagram value.');
|
|
89
|
+
return new Map(value[1].map(entry => {
|
|
90
|
+
if (!Array.isArray(entry) || entry.length !== 2)
|
|
91
|
+
throw new Error('Invalid serialized map entry in diagram value.');
|
|
92
|
+
return [deserializeValue(entry[0]), deserializeValue(entry[1])];
|
|
93
|
+
}));
|
|
94
|
+
case 'set':
|
|
95
|
+
if (value.length !== 2 || !Array.isArray(value[1]))
|
|
96
|
+
throw new Error('Invalid serialized set diagram value.');
|
|
97
|
+
return new Set(value[1].map(deserializeValue));
|
|
98
|
+
default:
|
|
99
|
+
throw new Error('Unknown serialized diagram value type.');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function mapValue(value, field) {
|
|
103
|
+
if (!(value instanceof Map))
|
|
104
|
+
throw new Error(`Invalid serialized diagram context map '${field}'.`);
|
|
105
|
+
for (const key of value.keys())
|
|
106
|
+
if (typeof key !== 'string')
|
|
107
|
+
throw new Error(`Invalid key in serialized diagram context map '${field}'.`);
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
export function serializeDiagramSceneModel(model) {
|
|
111
|
+
const env = capturedContexts.get(model);
|
|
112
|
+
if (!env)
|
|
113
|
+
throw new Error('Diagram model has no captured AMX evaluation context');
|
|
114
|
+
const context = {
|
|
115
|
+
values: env.toObject(),
|
|
116
|
+
immutableValues: Object.entries(env.toObject()).filter(([, value]) => env.isImmutable(value)).map(([name]) => name),
|
|
117
|
+
recordTypes: env.recordTypes,
|
|
118
|
+
enumDeclarations: env.enumDeclarations,
|
|
119
|
+
enumValues: env.enumValues,
|
|
120
|
+
functions: env.functions,
|
|
121
|
+
bindingTypes: env.bindingTypes,
|
|
122
|
+
dimensionRegistry: env.dimensionRegistry,
|
|
123
|
+
validationMode: env.validationMode,
|
|
124
|
+
declarationRegistries: {
|
|
125
|
+
records: [...env.recordTypes].map(([name, declaration]) => ({
|
|
126
|
+
name,
|
|
127
|
+
declaration: fieldRegistry(declaration, env.dimensionRegistry),
|
|
128
|
+
fields: (declaration.declaredFields ?? declaration.fields)
|
|
129
|
+
.map(field => fieldRegistry(field, env.dimensionRegistry))
|
|
130
|
+
})),
|
|
131
|
+
functions: [...env.functions].map(([name, declaration]) => ({
|
|
132
|
+
name,
|
|
133
|
+
declaration: fieldRegistry(declaration, env.dimensionRegistry)
|
|
134
|
+
}))
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const payload = {
|
|
138
|
+
version: 1,
|
|
139
|
+
model: serializeValue(model),
|
|
140
|
+
context: serializeValue(context)
|
|
141
|
+
};
|
|
142
|
+
return JSON.stringify(payload);
|
|
143
|
+
}
|
|
144
|
+
export function deserializeDiagramSceneModel(serialized) {
|
|
145
|
+
const payload = JSON.parse(serialized);
|
|
146
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)
|
|
147
|
+
|| !('version' in payload) || payload.version !== 1
|
|
148
|
+
|| !('model' in payload) || !('context' in payload)) {
|
|
149
|
+
throw new Error('Invalid serialized diagram model envelope.');
|
|
150
|
+
}
|
|
151
|
+
const modelValue = deserializeValue(payload.model);
|
|
152
|
+
const contextValue = deserializeValue(payload.context);
|
|
153
|
+
if (!modelValue || typeof modelValue !== 'object' || Array.isArray(modelValue)
|
|
154
|
+
|| !contextValue || typeof contextValue !== 'object' || Array.isArray(contextValue)) {
|
|
155
|
+
throw new Error('Invalid serialized diagram model or evaluation context.');
|
|
156
|
+
}
|
|
157
|
+
const model = freezeData(modelValue);
|
|
158
|
+
const context = contextValue;
|
|
159
|
+
if (!context.values || typeof context.values !== 'object' || Array.isArray(context.values)
|
|
160
|
+
|| !Array.isArray(context.immutableValues) || !context.declarationRegistries
|
|
161
|
+
|| !Array.isArray(context.declarationRegistries.records) || !Array.isArray(context.declarationRegistries.functions)
|
|
162
|
+
|| (context.validationMode !== 'aggregate' && context.validationMode !== 'fail-fast')) {
|
|
163
|
+
throw new Error('Invalid serialized diagram evaluation context.');
|
|
164
|
+
}
|
|
165
|
+
const recordTypes = mapValue(context.recordTypes, 'recordTypes');
|
|
166
|
+
const functions = mapValue(context.functions, 'functions');
|
|
167
|
+
const env = new Environment(recordTypes, functions, context.dimensionRegistry);
|
|
168
|
+
env.validationMode = context.validationMode;
|
|
169
|
+
for (const [name, value] of Object.entries(context.values)) {
|
|
170
|
+
env.set(name, value);
|
|
171
|
+
if (context.immutableValues.includes(name))
|
|
172
|
+
env.markImmutable(value);
|
|
173
|
+
}
|
|
174
|
+
for (const [name, value] of mapValue(context.enumDeclarations, 'enumDeclarations')) {
|
|
175
|
+
env.enumDeclarations.set(name, value);
|
|
176
|
+
}
|
|
177
|
+
for (const [name, value] of mapValue(context.enumValues, 'enumValues')) {
|
|
178
|
+
env.enumValues.set(name, value);
|
|
179
|
+
}
|
|
180
|
+
for (const [name, value] of mapValue(context.bindingTypes, 'bindingTypes')) {
|
|
181
|
+
env.bindingTypes.set(name, value);
|
|
182
|
+
}
|
|
183
|
+
for (const item of context.declarationRegistries.records) {
|
|
184
|
+
const declaration = recordTypes.get(item.name);
|
|
185
|
+
if (!declaration || !Array.isArray(item.fields))
|
|
186
|
+
throw new Error('Invalid serialized record declaration registry.');
|
|
187
|
+
if (item.declaration)
|
|
188
|
+
setDeclaringRegistry(declaration, item.declaration);
|
|
189
|
+
const fields = declaration.declaredFields ?? declaration.fields;
|
|
190
|
+
if (fields.length !== item.fields.length)
|
|
191
|
+
throw new Error('Serialized record field registry does not match its declaration.');
|
|
192
|
+
item.fields.forEach((registry, index) => {
|
|
193
|
+
if (registry)
|
|
194
|
+
setDeclaringRegistry(fields[index], registry);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
for (const item of context.declarationRegistries.functions) {
|
|
198
|
+
const declaration = functions.get(item.name);
|
|
199
|
+
if (!declaration)
|
|
200
|
+
throw new Error('Invalid serialized function declaration registry.');
|
|
201
|
+
if (item.declaration)
|
|
202
|
+
setDeclaringRegistry(declaration, item.declaration);
|
|
203
|
+
}
|
|
204
|
+
capturedContexts.set(model, env);
|
|
205
|
+
return model;
|
|
206
|
+
}
|
|
207
|
+
function cloneData(value, seen = new Map()) {
|
|
208
|
+
if (!value || typeof value !== 'object')
|
|
209
|
+
return value;
|
|
210
|
+
const existing = seen.get(value);
|
|
211
|
+
if (existing)
|
|
212
|
+
return existing;
|
|
213
|
+
if (Array.isArray(value)) {
|
|
214
|
+
const copy = [];
|
|
215
|
+
seen.set(value, copy);
|
|
216
|
+
for (const item of value)
|
|
217
|
+
copy.push(cloneData(item, seen));
|
|
218
|
+
return copy;
|
|
219
|
+
}
|
|
220
|
+
if (value instanceof Map) {
|
|
221
|
+
const copy = new Map();
|
|
222
|
+
seen.set(value, copy);
|
|
223
|
+
for (const [key, item] of value)
|
|
224
|
+
copy.set(cloneData(key, seen), cloneData(item, seen));
|
|
225
|
+
return copy;
|
|
226
|
+
}
|
|
227
|
+
if (value instanceof Set) {
|
|
228
|
+
const copy = new Set();
|
|
229
|
+
seen.set(value, copy);
|
|
230
|
+
for (const item of value)
|
|
231
|
+
copy.add(cloneData(item, seen));
|
|
232
|
+
return copy;
|
|
233
|
+
}
|
|
234
|
+
const copy = {};
|
|
235
|
+
seen.set(value, copy);
|
|
236
|
+
for (const [key, item] of Object.entries(value))
|
|
237
|
+
copy[key] = cloneData(item, seen);
|
|
238
|
+
return copy;
|
|
239
|
+
}
|
|
240
|
+
function freezeData(value, seen = new Set()) {
|
|
241
|
+
if (!value || typeof value !== 'object' || seen.has(value))
|
|
242
|
+
return value;
|
|
243
|
+
seen.add(value);
|
|
244
|
+
if (Array.isArray(value))
|
|
245
|
+
value.forEach(item => freezeData(item, seen));
|
|
246
|
+
else if (!(value instanceof Map) && !(value instanceof Set)) {
|
|
247
|
+
Object.values(value).forEach(item => freezeData(item, seen));
|
|
248
|
+
}
|
|
249
|
+
return Object.freeze(value);
|
|
250
|
+
}
|
|
251
|
+
function cloneEnvironment(source) {
|
|
252
|
+
const recordTypes = cloneData(source.recordTypes);
|
|
253
|
+
const functions = cloneData(source.functions);
|
|
254
|
+
const registry = source.dimensionRegistry ? cloneData(source.dimensionRegistry) : undefined;
|
|
255
|
+
const copy = new Environment(recordTypes, functions, registry);
|
|
256
|
+
copy.validationMode = source.validationMode;
|
|
257
|
+
for (const [name, declaration] of source.recordTypes) {
|
|
258
|
+
const cloned = recordTypes.get(name);
|
|
259
|
+
const declarationRegistry = fieldRegistry(declaration, source.dimensionRegistry);
|
|
260
|
+
if (declarationRegistry)
|
|
261
|
+
setDeclaringRegistry(cloned, cloneData(declarationRegistry));
|
|
262
|
+
const originalFields = declaration.declaredFields ?? declaration.fields;
|
|
263
|
+
const copiedFields = cloned.declaredFields ?? cloned.fields;
|
|
264
|
+
for (let index = 0; index < originalFields.length; index++) {
|
|
265
|
+
const fieldRegistryValue = fieldRegistry(originalFields[index], source.dimensionRegistry);
|
|
266
|
+
if (fieldRegistryValue && copiedFields[index]) {
|
|
267
|
+
setDeclaringRegistry(copiedFields[index], cloneData(fieldRegistryValue));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
for (const [name, declaration] of source.functions) {
|
|
272
|
+
const declarationRegistry = fieldRegistry(declaration, source.dimensionRegistry);
|
|
273
|
+
if (declarationRegistry)
|
|
274
|
+
setDeclaringRegistry(functions.get(name), cloneData(declarationRegistry));
|
|
275
|
+
}
|
|
276
|
+
for (const [name, value] of source.enumDeclarations)
|
|
277
|
+
copy.enumDeclarations.set(name, cloneData(value));
|
|
278
|
+
for (const [name, value] of source.enumValues)
|
|
279
|
+
copy.enumValues.set(name, cloneData(value));
|
|
280
|
+
for (const [name, type] of source.bindingTypes)
|
|
281
|
+
copy.bindingTypes.set(name, cloneData(type));
|
|
282
|
+
for (const [name, value] of Object.entries(source.toObject())) {
|
|
283
|
+
const cloned = cloneData(value);
|
|
284
|
+
copy.set(name, cloned);
|
|
285
|
+
if (source.isImmutable(value))
|
|
286
|
+
copy.markImmutable(cloned);
|
|
287
|
+
}
|
|
288
|
+
return copy;
|
|
289
|
+
}
|
|
290
|
+
function property(element, name) {
|
|
291
|
+
const found = element.properties.find(item => item.name === name);
|
|
292
|
+
if (!found)
|
|
293
|
+
throw new Error(`Type checker accepted missing diagram property '${name}'`);
|
|
294
|
+
return found;
|
|
295
|
+
}
|
|
296
|
+
function requiredNumber(element, name, env, file, timeline, time) {
|
|
297
|
+
const item = property(element, name);
|
|
298
|
+
const value = evaluateExpression(item.expression, env, file);
|
|
299
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
300
|
+
if (value === Infinity && time === timeline.to) {
|
|
301
|
+
staticError('AMXD010', 'Infinity value calculated for time upper bound', item.valueSource ?? item.source, file);
|
|
302
|
+
}
|
|
303
|
+
if (value === Infinity && time > timeline.from && time < timeline.to) {
|
|
304
|
+
staticError('AMXD011', 'Expression produces an infinite result', item.valueSource ?? item.source, file);
|
|
305
|
+
}
|
|
306
|
+
staticError('AMXD008', 'Invalid primitive geometry', item.valueSource ?? item.source, file);
|
|
307
|
+
}
|
|
308
|
+
return value;
|
|
309
|
+
}
|
|
310
|
+
function evaluateFrame(declaration, timeline, time, sourceEnvironment, file) {
|
|
311
|
+
if (typeof time !== 'number' || !Number.isFinite(time) || time < timeline.from || time > timeline.to) {
|
|
312
|
+
staticError('AMXD004', 'Invalid time bounds', declaration.times[0]?.source ?? declaration.source, file);
|
|
313
|
+
}
|
|
314
|
+
const env = cloneEnvironment(sourceEnvironment);
|
|
315
|
+
env.set(timeline.variable, time);
|
|
316
|
+
const elements = declaration.elements.map((element) => {
|
|
317
|
+
const readNumber = (name) => requiredNumber(element, name, env, file, timeline, time);
|
|
318
|
+
const readStyle = (name) => {
|
|
319
|
+
const value = property(element, name).expression;
|
|
320
|
+
if (value.type !== 'stringLiteral')
|
|
321
|
+
throw new Error('Type checker accepted a non-literal diagram style');
|
|
322
|
+
return value.value;
|
|
323
|
+
};
|
|
324
|
+
const readLineWidth = () => {
|
|
325
|
+
const value = property(element, 'lineWidth').expression;
|
|
326
|
+
if (value.type !== 'numberLiteral')
|
|
327
|
+
throw new Error('Type checker accepted a non-literal line width');
|
|
328
|
+
return value.value;
|
|
329
|
+
};
|
|
330
|
+
try {
|
|
331
|
+
switch (element.primitive) {
|
|
332
|
+
case 'line': {
|
|
333
|
+
const arrow = element.properties.find(item => item.name === 'arrow')?.expression;
|
|
334
|
+
const arrowValue = arrow?.type === 'stringLiteral' ? arrow.value : 'none';
|
|
335
|
+
return Object.freeze({
|
|
336
|
+
primitive: 'line', name: element.name, source: element.source,
|
|
337
|
+
x1: readNumber('x1'), y1: readNumber('y1'), x2: readNumber('x2'), y2: readNumber('y2'),
|
|
338
|
+
stroke: readStyle('stroke'),
|
|
339
|
+
lineWidth: readLineWidth(),
|
|
340
|
+
arrow: arrowValue
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
case 'rectangle': {
|
|
344
|
+
const width = readNumber('width');
|
|
345
|
+
const height = readNumber('height');
|
|
346
|
+
if (width <= 0)
|
|
347
|
+
staticError('AMXD008', 'Invalid primitive geometry', property(element, 'width').valueSource, file);
|
|
348
|
+
if (height <= 0)
|
|
349
|
+
staticError('AMXD008', 'Invalid primitive geometry', property(element, 'height').valueSource, file);
|
|
350
|
+
const fill = element.properties.some(item => item.name === 'fill') ? readStyle('fill') : undefined;
|
|
351
|
+
return Object.freeze({
|
|
352
|
+
primitive: 'rectangle', name: element.name, source: element.source,
|
|
353
|
+
x: readNumber('x'), y: readNumber('y'), width, height, stroke: readStyle('stroke'),
|
|
354
|
+
...(fill === undefined ? {} : { fill }),
|
|
355
|
+
lineWidth: readLineWidth()
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
case 'circle': {
|
|
359
|
+
const r = readNumber('r');
|
|
360
|
+
if (r <= 0)
|
|
361
|
+
staticError('AMXD008', 'Invalid primitive geometry', property(element, 'r').valueSource, file);
|
|
362
|
+
const fill = element.properties.some(item => item.name === 'fill') ? readStyle('fill') : undefined;
|
|
363
|
+
return Object.freeze({
|
|
364
|
+
primitive: 'circle', name: element.name, source: element.source,
|
|
365
|
+
cx: readNumber('cx'), cy: readNumber('cy'), r, stroke: readStyle('stroke'),
|
|
366
|
+
...(fill === undefined ? {} : { fill }),
|
|
367
|
+
lineWidth: readLineWidth()
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
case 'text': {
|
|
371
|
+
const label = property(element, 'value');
|
|
372
|
+
const value = evaluateExpression(label.expression, env, file);
|
|
373
|
+
if (typeof value !== 'string')
|
|
374
|
+
staticError('AMX3002', 'Expected String, got non-String value', label.valueSource, file);
|
|
375
|
+
return Object.freeze({
|
|
376
|
+
primitive: 'text', name: element.name, source: element.source,
|
|
377
|
+
x: readNumber('x'), y: readNumber('y'), value, fill: readStyle('fill')
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
default:
|
|
381
|
+
throw new Error(`Type checker accepted unsupported diagram primitive '${element.primitive}'`);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
if (error instanceof AmxError && time !== timeline.from && error.code !== 'AMXD008'
|
|
386
|
+
&& error.code !== 'AMXD010' && error.code !== 'AMXD011') {
|
|
387
|
+
throw new AmxError({
|
|
388
|
+
code: 'AMXD009',
|
|
389
|
+
message: `Frame evaluation leads to invalid results. The underlying error is: ${error.message}`,
|
|
390
|
+
file: error.file ?? file,
|
|
391
|
+
line: error.line ?? element.source?.line,
|
|
392
|
+
column: error.column ?? element.source?.column
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
throw error;
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
return Object.freeze({ time, elements: Object.freeze(elements) });
|
|
399
|
+
}
|
|
400
|
+
function evaluateDocumentNumber(expression, env, file, code, message, source) {
|
|
401
|
+
const value = evaluateExpression(expression, env, file);
|
|
402
|
+
if (typeof value !== 'number' || !Number.isFinite(value)
|
|
403
|
+
|| (code === 'AMXD003' || code === 'AMXD014' || code === 'AMXD005') && value <= 0) {
|
|
404
|
+
staticError(code, message, source, file);
|
|
405
|
+
}
|
|
406
|
+
return value;
|
|
407
|
+
}
|
|
408
|
+
export function createDiagramModel(sourceDeclaration, sourceEnvironment, file, documentNodeIndex, statementIndex) {
|
|
409
|
+
const declaration = freezeData(cloneData(sourceDeclaration));
|
|
410
|
+
const env = cloneEnvironment(sourceEnvironment);
|
|
411
|
+
const getDiagramProperty = (name) => {
|
|
412
|
+
const item = declaration.properties.find(propertyItem => propertyItem.name === name);
|
|
413
|
+
if (!item)
|
|
414
|
+
throw new Error(`Type checker accepted missing diagram property '${name}'`);
|
|
415
|
+
return item;
|
|
416
|
+
};
|
|
417
|
+
const widthProperty = getDiagramProperty('width');
|
|
418
|
+
const heightProperty = getDiagramProperty('height');
|
|
419
|
+
const width = evaluateDocumentNumber(widthProperty.expression, env, file, 'AMXD003', 'Canvas width is invalid', widthProperty.valueSource);
|
|
420
|
+
const height = evaluateDocumentNumber(heightProperty.expression, env, file, 'AMXD014', 'Canvas height is invalid', heightProperty.valueSource);
|
|
421
|
+
const titleExpression = getDiagramProperty('title').expression;
|
|
422
|
+
const descriptionExpression = getDiagramProperty('description').expression;
|
|
423
|
+
if (titleExpression.type !== 'stringLiteral' || descriptionExpression.type !== 'stringLiteral') {
|
|
424
|
+
throw new Error('Type checker accepted a non-literal diagram title or description');
|
|
425
|
+
}
|
|
426
|
+
const time = declaration.times[0];
|
|
427
|
+
const from = evaluateDocumentNumber(time.from, env, file, 'AMXD004', 'Invalid time bounds', time.fromSource ?? time.source);
|
|
428
|
+
const to = evaluateDocumentNumber(time.to, env, file, 'AMXD004', 'Invalid time bounds', time.toSource ?? time.source);
|
|
429
|
+
if (from >= to)
|
|
430
|
+
staticError('AMXD004', 'Invalid time bounds', time.source ?? declaration.source, file);
|
|
431
|
+
const duration = evaluateDocumentNumber(time.duration, env, file, 'AMXD005', 'Invalid playback duration value', time.durationSource ?? time.source);
|
|
432
|
+
const timeline = Object.freeze({ variable: time.name, from, to, duration });
|
|
433
|
+
const initialFrame = evaluateFrame(declaration, timeline, from, env, file);
|
|
434
|
+
const model = Object.freeze({
|
|
435
|
+
name: declaration.name,
|
|
436
|
+
title: titleExpression.value,
|
|
437
|
+
description: descriptionExpression.value,
|
|
438
|
+
canvas: Object.freeze({ width, height, coordinates: 'bottom-left-y-up' }),
|
|
439
|
+
timeline,
|
|
440
|
+
declaration,
|
|
441
|
+
initialFrame,
|
|
442
|
+
source: declaration.source,
|
|
443
|
+
file,
|
|
444
|
+
documentNodeIndex,
|
|
445
|
+
statementIndex
|
|
446
|
+
});
|
|
447
|
+
capturedContexts.set(model, env);
|
|
448
|
+
return model;
|
|
449
|
+
}
|
|
450
|
+
export function evaluateDiagramFrame(model, time, file = model.file) {
|
|
451
|
+
const env = capturedContexts.get(model);
|
|
452
|
+
if (!env)
|
|
453
|
+
throw new Error('Diagram model has no captured AMX evaluation context');
|
|
454
|
+
return evaluateFrame(model.declaration, model.timeline, time, env, file);
|
|
455
|
+
}
|
|
@@ -7,11 +7,14 @@ import { throwUndefinedIdentifier } from '../diagnostics/errors';
|
|
|
7
7
|
export class Environment {
|
|
8
8
|
store = new Map();
|
|
9
9
|
recordTypes;
|
|
10
|
+
enumDeclarations = new Map();
|
|
11
|
+
enumValues = new Map();
|
|
10
12
|
functions;
|
|
11
13
|
bindingTypes = new Map();
|
|
12
14
|
dimensionRegistry;
|
|
13
15
|
viewDefinitions = new Map();
|
|
14
16
|
viewEmissions = [];
|
|
17
|
+
diagramEmissions = [];
|
|
15
18
|
immutableValues = new WeakSet();
|
|
16
19
|
currentDocumentNodeIndex = -1;
|
|
17
20
|
currentStatementIndex = -1;
|
|
@@ -31,6 +34,10 @@ export class Environment {
|
|
|
31
34
|
frame.validationMode = this.validationMode;
|
|
32
35
|
for (const [name, type] of this.bindingTypes)
|
|
33
36
|
frame.bindingTypes.set(name, type);
|
|
37
|
+
for (const [name, declaration] of this.enumDeclarations)
|
|
38
|
+
frame.enumDeclarations.set(name, declaration);
|
|
39
|
+
for (const [name, values] of this.enumValues)
|
|
40
|
+
frame.enumValues.set(name, values);
|
|
34
41
|
for (const [name, value] of Object.entries(parameters)) {
|
|
35
42
|
frame.set(name, value);
|
|
36
43
|
if (this.isImmutable(value))
|
|
@@ -101,5 +108,6 @@ export class Environment {
|
|
|
101
108
|
this.immutableValues = new WeakSet();
|
|
102
109
|
this.viewDefinitions.clear();
|
|
103
110
|
this.viewEmissions.length = 0;
|
|
111
|
+
this.diagramEmissions.length = 0;
|
|
104
112
|
}
|
|
105
113
|
}
|