@nova-energy/openamx 0.11.0 → 0.12.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 +5 -3
- package/dist/editor/completion.js +96 -5
- package/dist/editor/highlighting.js +36 -3
- package/dist/editor/moduleAnalysis.js +8 -2
- package/dist/editor/refactoring.js +1 -1
- package/dist/editor/sourceRanges.js +1 -0
- package/dist/editor/symbols.js +109 -3
- package/dist/parser/parseExpression.js +14 -0
- package/dist/parser/parseIf.js +180 -0
- package/dist/parser/parseStatements.js +85 -7
- package/dist/runtime/environment.js +2 -0
- package/dist/runtime/evaluateExpression.js +116 -8
- package/dist/runtime/inputData.js +20 -18
- package/dist/runtime/moduleLoader.js +16 -2
- package/dist/runtime/outputData.js +18 -12
- package/dist/typechecker/checkDocument.js +254 -13
- package/package.json +1 -1
|
@@ -30,13 +30,16 @@ function supportsJson(type, recordTypes, registry, visiting = new Set()) {
|
|
|
30
30
|
if (!declaration || visiting.has(type.name))
|
|
31
31
|
return false;
|
|
32
32
|
const next = new Set(visiting).add(type.name);
|
|
33
|
-
return declaration.fields.every(field =>
|
|
33
|
+
return declaration.fields.every(field => {
|
|
34
|
+
const fieldScope = fieldRegistry(field, registry);
|
|
35
|
+
return supportsJson(checkedType(field.annotation, fieldScope), recordTypes, fieldScope, next);
|
|
36
|
+
});
|
|
34
37
|
}
|
|
35
38
|
function supportsCsv(type, recordTypes, registry) {
|
|
36
39
|
if (type.kind !== 'list' || type.element.kind !== 'named')
|
|
37
40
|
return false;
|
|
38
41
|
const declaration = recordTypes.get(type.element.name);
|
|
39
|
-
return !!declaration && declaration.fields.every(field => isCsvScalar(checkedType(field.annotation, fieldRegistry(
|
|
42
|
+
return !!declaration && declaration.fields.every(field => isCsvScalar(checkedType(field.annotation, fieldRegistry(field, registry))));
|
|
40
43
|
}
|
|
41
44
|
export function describeOutputSchemas(exportedBindings, recordTypes, registry) {
|
|
42
45
|
return [...exportedBindings].flatMap(([name, type]) => {
|
|
@@ -63,9 +66,10 @@ function assertJsonShape(type, recordTypes, outputName, registry) {
|
|
|
63
66
|
const declaration = recordTypes.get(type.name);
|
|
64
67
|
if (!declaration)
|
|
65
68
|
outputError('AMX6001', `Output '${outputName}' uses unknown record type '${type.name}'`);
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
assertJsonShape(checkedType(field.annotation,
|
|
69
|
+
for (const field of declaration.fields) {
|
|
70
|
+
const fieldScope = fieldRegistry(field, registry);
|
|
71
|
+
assertJsonShape(checkedType(field.annotation, fieldScope), recordTypes, outputName, fieldScope);
|
|
72
|
+
}
|
|
69
73
|
}
|
|
70
74
|
function checkedType(reference, registry) {
|
|
71
75
|
if (reference.type === 'namedType') {
|
|
@@ -82,10 +86,9 @@ function assertCsvShape(type, recordTypes, outputName, registry) {
|
|
|
82
86
|
const declaration = recordTypes.get(type.element.name);
|
|
83
87
|
if (!declaration)
|
|
84
88
|
outputError('AMX6001', `CSV output '${outputName}' uses unknown record type '${type.element.name}'`);
|
|
85
|
-
const
|
|
86
|
-
const unsupported = declaration.fields.find(field => !isCsvScalar(checkedType(field.annotation, recordRegistry)));
|
|
89
|
+
const unsupported = declaration.fields.find(field => !isCsvScalar(checkedType(field.annotation, fieldRegistry(field, registry))));
|
|
87
90
|
if (unsupported) {
|
|
88
|
-
outputError('AMX6001', `CSV output '${outputName}' field '${unsupported.name}' must be a scalar or nullable scalar, not ${typeName(checkedType(unsupported.annotation,
|
|
91
|
+
outputError('AMX6001', `CSV output '${outputName}' field '${unsupported.name}' must be a scalar or nullable scalar, not ${typeName(checkedType(unsupported.annotation, fieldRegistry(unsupported, registry)))}`);
|
|
89
92
|
}
|
|
90
93
|
}
|
|
91
94
|
export function prepareOutputs(mappings, exportedBindings, recordTypes, reservedDestination, registry) {
|
|
@@ -182,7 +185,8 @@ function jsonValue(value, type, output, recordTypes, registry) {
|
|
|
182
185
|
for (const field of declaration.fields) {
|
|
183
186
|
if (!Object.prototype.hasOwnProperty.call(record, field.name))
|
|
184
187
|
return invalidValue(output, `record ${type.name} is missing field '${field.name}'`);
|
|
185
|
-
|
|
188
|
+
const fieldScope = fieldRegistry(field, registry);
|
|
189
|
+
ordered[field.name] = jsonValue(record[field.name], checkedType(field.annotation, fieldScope), output, recordTypes, fieldScope);
|
|
186
190
|
}
|
|
187
191
|
return ordered;
|
|
188
192
|
}
|
|
@@ -244,7 +248,7 @@ function csvContents(value, type, output, recordTypes, registry) {
|
|
|
244
248
|
rows.push(fields.map(field => {
|
|
245
249
|
if (!Object.prototype.hasOwnProperty.call(record, field.name))
|
|
246
250
|
return invalidValue(output, `record ${index} is missing field '${field.name}'`);
|
|
247
|
-
return csvCell(record[field.name], checkedType(field.annotation, fieldRegistry(
|
|
251
|
+
return csvCell(record[field.name], checkedType(field.annotation, fieldRegistry(field, registry)), output, registry);
|
|
248
252
|
}).join(','));
|
|
249
253
|
}
|
|
250
254
|
return `${rows.join('\n')}\n`;
|
|
@@ -274,8 +278,10 @@ function measurementExpectations(type, recordTypes, registry, path = '$', seen =
|
|
|
274
278
|
if (!declaration)
|
|
275
279
|
return [];
|
|
276
280
|
const next = new Set(seen).add(type.name);
|
|
277
|
-
|
|
278
|
-
|
|
281
|
+
return declaration.fields.flatMap(field => {
|
|
282
|
+
const fieldScope = fieldRegistry(field, registry) ?? registry;
|
|
283
|
+
return measurementExpectations(checkedType(field.annotation, fieldScope), recordTypes, fieldScope, `${path}.${field.name}`, next) ?? [];
|
|
284
|
+
});
|
|
279
285
|
}
|
|
280
286
|
function formatDimensionVector(vector, registry) {
|
|
281
287
|
return [...vector].sort(([left], [right]) => left.localeCompare(right)).map(([identity, exponent]) => {
|
|
@@ -64,6 +64,8 @@ function bodyContainsForExpression(expression) {
|
|
|
64
64
|
case 'stringInterpolation': return expression.parts.some(part => typeof part !== 'string' && bodyContainsForExpression(part));
|
|
65
65
|
case 'unaryExpression': return bodyContainsForExpression(expression.argument);
|
|
66
66
|
case 'conditionalExpression': return [expression.test, expression.consequent, expression.alternate].some(bodyContainsForExpression);
|
|
67
|
+
case 'bracedIfExpression': return bodyContainsForExpression(expression.test)
|
|
68
|
+
|| bodyContainsForStatements(expression.consequent) || bodyContainsForStatements(expression.alternate);
|
|
67
69
|
case 'listLiteral': return expression.elements.some(bodyContainsForExpression);
|
|
68
70
|
case 'functionCall': return expression.arguments.some(bodyContainsForExpression);
|
|
69
71
|
case 'rangeExpression': return bodyContainsForExpression(expression.start) || bodyContainsForExpression(expression.end);
|
|
@@ -74,6 +76,25 @@ function bodyContainsForExpression(expression) {
|
|
|
74
76
|
default: return false;
|
|
75
77
|
}
|
|
76
78
|
}
|
|
79
|
+
function bodyContainsForStatements(statements) {
|
|
80
|
+
return statements.some(statement => {
|
|
81
|
+
switch (statement.type) {
|
|
82
|
+
case 'variableDeclaration': return bodyContainsForExpression(statement.expression);
|
|
83
|
+
case 'assignmentStatement':
|
|
84
|
+
case 'compoundAssignmentStatement': return bodyContainsForExpression(statement.expression);
|
|
85
|
+
case 'returnStatement': return bodyContainsForExpression(statement.expression);
|
|
86
|
+
case 'bracedIfStatement': return bodyContainsForExpression(statement.test)
|
|
87
|
+
|| bodyContainsForStatements(statement.consequent)
|
|
88
|
+
|| !!statement.alternate && bodyContainsForStatements(statement.alternate);
|
|
89
|
+
case 'forStatement': return bodyContainsForExpression(statement.iterable) || bodyContainsForStatements(statement.body);
|
|
90
|
+
case 'addStatement': return bodyContainsForExpression(statement.value)
|
|
91
|
+
|| !!statement.index && bodyContainsForExpression(statement.index);
|
|
92
|
+
case 'removeStatement': return bodyContainsForExpression(statement.count)
|
|
93
|
+
|| !!statement.index && bodyContainsForExpression(statement.index);
|
|
94
|
+
default: return false;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
77
98
|
function constantValue(expression) {
|
|
78
99
|
switch (expression.type) {
|
|
79
100
|
case 'numberLiteral': return Number.isFinite(expression.value) ? expression.value : undefined;
|
|
@@ -156,6 +177,7 @@ function literalListLength(expression) {
|
|
|
156
177
|
}
|
|
157
178
|
export function checkDocument(doc, file, context) {
|
|
158
179
|
const types = new Map(context?.types ?? []);
|
|
180
|
+
const enums = new Map(context?.enums ?? []);
|
|
159
181
|
let bindings = new Map(context?.bindings ?? []);
|
|
160
182
|
const functions = new Map(context?.functions ?? []);
|
|
161
183
|
const dimensions = new Map(context?.dimensions ?? []);
|
|
@@ -163,9 +185,10 @@ export function checkDocument(doc, file, context) {
|
|
|
163
185
|
const baseUnits = context?.baseUnits ?? new Map();
|
|
164
186
|
const moduleRegistry = { dimensions, units, baseUnits };
|
|
165
187
|
const moduleIdentity = context?.moduleIdentity ?? file ?? '<memory>';
|
|
166
|
-
const immutableNames = new Set([...(context?.bindings?.keys() ?? []), ...(context?.functions?.keys() ?? []), ...(context?.types?.keys() ?? []), ...(context?.dimensions?.keys() ?? []), ...(context?.units?.keys() ?? [])]);
|
|
188
|
+
const immutableNames = new Set([...(context?.bindings?.keys() ?? []), ...(context?.functions?.keys() ?? []), ...(context?.types?.keys() ?? []), ...(context?.enums?.keys() ?? []), ...(context?.dimensions?.keys() ?? []), ...(context?.units?.keys() ?? [])]);
|
|
167
189
|
const inputNames = new Set();
|
|
168
190
|
const exportedTypes = new Map();
|
|
191
|
+
const exportedEnums = new Map();
|
|
169
192
|
const exportedFunctions = new Map();
|
|
170
193
|
const exportedBindings = new Map();
|
|
171
194
|
const exportedDimensions = new Map();
|
|
@@ -174,10 +197,10 @@ export function checkDocument(doc, file, context) {
|
|
|
174
197
|
const views = new Map();
|
|
175
198
|
let loopDepth = 0;
|
|
176
199
|
let allowListMutation = true;
|
|
177
|
-
const fail = (code, message, source) => staticError(code, message, source, file);
|
|
200
|
+
const fail = (code, message, source, declarationSource) => staticError(code, message, source, file, declarationSource);
|
|
178
201
|
const failDeclaration = (message, source, declarationSource) => staticError('AMX3008', message, source, file, declarationSource);
|
|
179
202
|
function hasName(name) {
|
|
180
|
-
return primitives.has(name) || stdlibNames.has(name) || types.has(name) || functions.has(name)
|
|
203
|
+
return primitives.has(name) || stdlibNames.has(name) || types.has(name) || enums.has(name) || functions.has(name)
|
|
181
204
|
|| bindings.has(name) || views.has(name) || dimensions.has(name) || units.has(name);
|
|
182
205
|
}
|
|
183
206
|
function normalizedVector(vector) {
|
|
@@ -571,7 +594,7 @@ export function checkDocument(doc, file, context) {
|
|
|
571
594
|
const declared = declaration.fields.find(item => item.name === field.name);
|
|
572
595
|
if (!declared)
|
|
573
596
|
fail('AMX3001', `Unknown field '${field.name}'`, field.source);
|
|
574
|
-
const target = resolve(declared.annotation,
|
|
597
|
+
const target = resolve(declared.annotation, declared);
|
|
575
598
|
requireType(infer(field.expression, target), target, field.expression.source ?? field.source);
|
|
576
599
|
}
|
|
577
600
|
for (const field of declaration.fields) {
|
|
@@ -581,6 +604,16 @@ export function checkDocument(doc, file, context) {
|
|
|
581
604
|
return named(expression.name);
|
|
582
605
|
}
|
|
583
606
|
case 'fieldAccess': {
|
|
607
|
+
if (expression.receiver.type === 'identifier') {
|
|
608
|
+
const enumeration = enums.get(expression.receiver.name);
|
|
609
|
+
if (enumeration) {
|
|
610
|
+
const member = enumeration.members.find(item => item.name === expression.field);
|
|
611
|
+
if (!member)
|
|
612
|
+
fail('AMX3001', `Unknown enum member '${enumeration.name}.${expression.field}'`, expression.source);
|
|
613
|
+
const resolvedValue = enumeration.resolvedValues?.[enumeration.members.indexOf(member)];
|
|
614
|
+
return named(typeof resolvedValue === 'number' ? 'Number' : 'String');
|
|
615
|
+
}
|
|
616
|
+
}
|
|
584
617
|
const receiver = infer(expression.receiver);
|
|
585
618
|
const declaration = receiver.kind === 'named' ? types.get(receiver.name) : undefined;
|
|
586
619
|
if (!declaration)
|
|
@@ -588,7 +621,7 @@ export function checkDocument(doc, file, context) {
|
|
|
588
621
|
const field = declaration.fields.find(item => item.name === expression.field);
|
|
589
622
|
if (!field)
|
|
590
623
|
fail('AMX3001', `Unknown field '${expression.field}'`, expression.source);
|
|
591
|
-
return resolve(field.annotation,
|
|
624
|
+
return resolve(field.annotation, field);
|
|
592
625
|
}
|
|
593
626
|
case 'listAccess': {
|
|
594
627
|
const receiver = infer(expression.receiver);
|
|
@@ -748,6 +781,16 @@ export function checkDocument(doc, file, context) {
|
|
|
748
781
|
? 'AMX3007' : 'AMX3002', 'Conditional branches have incompatible types', expression.source);
|
|
749
782
|
return result;
|
|
750
783
|
}
|
|
784
|
+
case 'bracedIfExpression': {
|
|
785
|
+
requireType(infer(expression.test), named('Boolean'), expression.test.source);
|
|
786
|
+
const consequent = checkIfExpressionBranch(expression.consequent, expression.consequentSource, expected, expression.source);
|
|
787
|
+
const alternate = checkIfExpressionBranch(expression.alternate, expression.alternateSource, expected, expression.source);
|
|
788
|
+
const result = common(consequent, alternate);
|
|
789
|
+
if (!result)
|
|
790
|
+
fail(consequent.kind === 'measurement' || alternate.kind === 'measurement'
|
|
791
|
+
? 'AMX3007' : 'AMX3002', 'Conditional branches have incompatible types', expression.source);
|
|
792
|
+
return result;
|
|
793
|
+
}
|
|
751
794
|
case 'rangeExpression':
|
|
752
795
|
requireType(infer(expression.start), named('Number'), expression.start.source);
|
|
753
796
|
requireType(infer(expression.end), named('Number'), expression.end.source);
|
|
@@ -881,6 +924,63 @@ export function checkDocument(doc, file, context) {
|
|
|
881
924
|
}
|
|
882
925
|
return list(returned ?? named('Number'));
|
|
883
926
|
}
|
|
927
|
+
function checkBranchScope(statements, check) {
|
|
928
|
+
const original = bindings;
|
|
929
|
+
bindings = new Map(original);
|
|
930
|
+
try {
|
|
931
|
+
for (const statement of statements)
|
|
932
|
+
checkStatement(statement);
|
|
933
|
+
check();
|
|
934
|
+
}
|
|
935
|
+
finally {
|
|
936
|
+
bindings = original;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
function checkIfExpressionBranch(statements, closingSource, expected, expressionSource) {
|
|
940
|
+
let result;
|
|
941
|
+
let hasFallthrough = true;
|
|
942
|
+
const collect = (type, source) => {
|
|
943
|
+
if (!result)
|
|
944
|
+
result = type;
|
|
945
|
+
else {
|
|
946
|
+
const merged = common(result, type);
|
|
947
|
+
if (!merged)
|
|
948
|
+
fail('AMX3002', 'Conditional branches have incompatible types', expressionSource ?? source);
|
|
949
|
+
result = merged;
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
const walk = (body) => {
|
|
953
|
+
let terminates = false;
|
|
954
|
+
const original = bindings;
|
|
955
|
+
bindings = new Map(original);
|
|
956
|
+
try {
|
|
957
|
+
for (const statement of body) {
|
|
958
|
+
if (statement.type === 'returnStatement') {
|
|
959
|
+
collect(infer(statement.expression, expected), statement.source);
|
|
960
|
+
terminates = true;
|
|
961
|
+
}
|
|
962
|
+
else if (statement.type === 'bracedIfStatement') {
|
|
963
|
+
requireType(infer(statement.test), named('Boolean'), statement.test.source);
|
|
964
|
+
const consequentReturns = walk(statement.consequent);
|
|
965
|
+
const alternateReturns = statement.alternate ? walk(statement.alternate) : false;
|
|
966
|
+
if (consequentReturns && alternateReturns)
|
|
967
|
+
terminates = true;
|
|
968
|
+
}
|
|
969
|
+
else {
|
|
970
|
+
checkStatement(statement);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
finally {
|
|
975
|
+
bindings = original;
|
|
976
|
+
}
|
|
977
|
+
return terminates;
|
|
978
|
+
};
|
|
979
|
+
hasFallthrough = !walk(statements);
|
|
980
|
+
if (hasFallthrough || !result)
|
|
981
|
+
fail('AMX3021', 'Every possible path in an if expression must return a value', closingSource ?? expressionSource);
|
|
982
|
+
return result;
|
|
983
|
+
}
|
|
884
984
|
function checkDefault(expression, target) {
|
|
885
985
|
if (expression.type === 'listLiteral') {
|
|
886
986
|
for (const item of expression.elements)
|
|
@@ -891,7 +991,7 @@ export function checkDocument(doc, file, context) {
|
|
|
891
991
|
const declaration = types.get(expression.name);
|
|
892
992
|
const declared = declaration?.fields.find(item => item.name === field.name);
|
|
893
993
|
if (declared)
|
|
894
|
-
checkDefault(field.expression, resolve(declared.annotation,
|
|
994
|
+
checkDefault(field.expression, resolve(declared.annotation, declared));
|
|
895
995
|
}
|
|
896
996
|
}
|
|
897
997
|
else if (!(expression.type === 'unaryExpression' && expression.operator === '-'
|
|
@@ -946,7 +1046,7 @@ export function checkDocument(doc, file, context) {
|
|
|
946
1046
|
const field = declaration.fields.find(item => item.name === fieldName);
|
|
947
1047
|
if (!field)
|
|
948
1048
|
fail('AMX3001', `Unknown field '${fieldName}' on '${recordName}'`, source);
|
|
949
|
-
return resolve(field.annotation,
|
|
1049
|
+
return resolve(field.annotation, field);
|
|
950
1050
|
}
|
|
951
1051
|
function isScalar(type) {
|
|
952
1052
|
if (type.kind === 'named')
|
|
@@ -1106,23 +1206,117 @@ export function checkDocument(doc, file, context) {
|
|
|
1106
1206
|
if (hasName(statement.name))
|
|
1107
1207
|
fail('AMX3005', `Duplicate type '${statement.name}'`, statement.source);
|
|
1108
1208
|
setDeclaringRegistry(statement, moduleRegistry);
|
|
1209
|
+
const declaredFields = statement.declaredFields ?? statement.fields;
|
|
1210
|
+
statement.declaredFields = declaredFields;
|
|
1211
|
+
const inheritedFields = [];
|
|
1212
|
+
const inheritedPositions = new Map();
|
|
1213
|
+
const inheritedParents = new Map();
|
|
1214
|
+
for (const parent of statement.parents ?? []) {
|
|
1215
|
+
const parentDeclaration = types.get(parent.name);
|
|
1216
|
+
if (!parentDeclaration)
|
|
1217
|
+
return fail('AMX3001', `Unknown type '${parent.name}'`, parent.source);
|
|
1218
|
+
for (const field of parentDeclaration.fields) {
|
|
1219
|
+
const previous = inheritedParents.get(field.name);
|
|
1220
|
+
if (previous && !declaredFields.some(candidate => candidate.name === field.name && candidate.override)) {
|
|
1221
|
+
fail('AMX3011', `Parent ${previous.name} and Parent ${parent.name} have the same property: '${field.name}'`, field.source, previous.field.source);
|
|
1222
|
+
}
|
|
1223
|
+
if (!previous)
|
|
1224
|
+
inheritedParents.set(field.name, { name: parent.name, field });
|
|
1225
|
+
if (!inheritedPositions.has(field.name)) {
|
|
1226
|
+
inheritedPositions.set(field.name, inheritedFields.length);
|
|
1227
|
+
inheritedFields.push(field);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1109
1231
|
const fields = new Set();
|
|
1110
|
-
for (const field of
|
|
1232
|
+
for (const field of declaredFields) {
|
|
1111
1233
|
if (fields.has(field.name))
|
|
1112
1234
|
fail('AMX3005', `Duplicate field '${field.name}'`, field.source);
|
|
1113
1235
|
fields.add(field.name);
|
|
1114
|
-
const
|
|
1236
|
+
const inherited = inheritedParents.get(field.name);
|
|
1237
|
+
if (inherited && !field.override) {
|
|
1238
|
+
fail('AMX3012', `This type declared '${field.name}' which is also declared by "${inherited.name}", use the override keyword to declare this property.`, field.source, inherited.field.source);
|
|
1239
|
+
}
|
|
1240
|
+
if (!inherited && field.override) {
|
|
1241
|
+
fail('AMX3013', `No inherited type includes "${field.name}", override is not necessary`, field.source);
|
|
1242
|
+
}
|
|
1243
|
+
setDeclaringRegistry(field, moduleRegistry);
|
|
1244
|
+
const target = resolve(field.annotation, field);
|
|
1115
1245
|
if (field.optional && !field.defaultExpression && target.kind !== 'nullable')
|
|
1116
1246
|
fail('AMX3005', `Optional field '${field.name}' needs a nullable type or default`, field.source);
|
|
1117
1247
|
}
|
|
1248
|
+
const effectiveFields = [...inheritedFields];
|
|
1249
|
+
for (const field of declaredFields) {
|
|
1250
|
+
const inheritedPosition = inheritedPositions.get(field.name);
|
|
1251
|
+
if (inheritedPosition === undefined) {
|
|
1252
|
+
effectiveFields.push(field);
|
|
1253
|
+
}
|
|
1254
|
+
else {
|
|
1255
|
+
effectiveFields[inheritedPosition] = field;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
statement.fields = effectiveFields;
|
|
1118
1259
|
types.set(statement.name, statement);
|
|
1119
|
-
for (const field of
|
|
1260
|
+
for (const field of declaredFields)
|
|
1120
1261
|
if (field.defaultExpression)
|
|
1121
|
-
checkDefault(field.defaultExpression, resolve(field.annotation));
|
|
1262
|
+
checkDefault(field.defaultExpression, resolve(field.annotation, field));
|
|
1122
1263
|
if (statement.exported)
|
|
1123
1264
|
exportedTypes.set(statement.name, statement);
|
|
1124
1265
|
return;
|
|
1125
1266
|
}
|
|
1267
|
+
case 'enumDeclaration': {
|
|
1268
|
+
if (hasName(statement.name))
|
|
1269
|
+
fail('AMX3005', `Duplicate declaration '${statement.name}'`, statement.nameSource ?? statement.source);
|
|
1270
|
+
if (statement.members.length === 0) {
|
|
1271
|
+
fail('AMX3015', 'Enums need at least one value', statement.closingSource ?? statement.source);
|
|
1272
|
+
}
|
|
1273
|
+
const memberNames = new Set();
|
|
1274
|
+
for (const member of statement.members) {
|
|
1275
|
+
if (memberNames.has(member.name))
|
|
1276
|
+
fail('AMX3016', 'Enum members names have to be unique', member.nameSource ?? statement.source);
|
|
1277
|
+
memberNames.add(member.name);
|
|
1278
|
+
}
|
|
1279
|
+
const hasExplicitValue = statement.members.some(member => member.value !== undefined);
|
|
1280
|
+
if (!hasExplicitValue) {
|
|
1281
|
+
statement.resolvedValues = statement.members.map((_, index) => index + 1);
|
|
1282
|
+
}
|
|
1283
|
+
else {
|
|
1284
|
+
const missing = statement.members.find(member => member.value === undefined);
|
|
1285
|
+
if (missing)
|
|
1286
|
+
fail('AMX3019', 'All members should have explicitly assigned values', missing.nameSource ?? statement.source);
|
|
1287
|
+
const values = [];
|
|
1288
|
+
let valueType;
|
|
1289
|
+
for (const member of statement.members) {
|
|
1290
|
+
const value = member.value;
|
|
1291
|
+
let literal;
|
|
1292
|
+
let literalType;
|
|
1293
|
+
if (value.type === 'numberLiteral') {
|
|
1294
|
+
literal = value.value;
|
|
1295
|
+
literalType = 'Number';
|
|
1296
|
+
}
|
|
1297
|
+
else if (value.type === 'stringLiteral') {
|
|
1298
|
+
literal = value.value;
|
|
1299
|
+
literalType = 'String';
|
|
1300
|
+
}
|
|
1301
|
+
else {
|
|
1302
|
+
return fail('AMX3020', 'Only constant values can be assigned to enum members', member.valueSource ?? value.source ?? member.nameSource ?? statement.source);
|
|
1303
|
+
}
|
|
1304
|
+
if (valueType && valueType !== literalType) {
|
|
1305
|
+
fail('AMX3018', 'Enum members should all have the same value types, all Number or String', member.valueSource ?? value.source ?? member.nameSource ?? statement.source);
|
|
1306
|
+
}
|
|
1307
|
+
valueType = literalType;
|
|
1308
|
+
if (values.includes(literal)) {
|
|
1309
|
+
fail('AMX3017', 'All enum members must have unique values', member.valueSource ?? value.source ?? member.nameSource ?? statement.source);
|
|
1310
|
+
}
|
|
1311
|
+
values.push(literal);
|
|
1312
|
+
}
|
|
1313
|
+
statement.resolvedValues = values;
|
|
1314
|
+
}
|
|
1315
|
+
enums.set(statement.name, statement);
|
|
1316
|
+
if (statement.exported)
|
|
1317
|
+
exportedEnums.set(statement.name, statement);
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1126
1320
|
case 'functionDeclaration': {
|
|
1127
1321
|
if (hasName(statement.name))
|
|
1128
1322
|
fail('AMX3005', `Duplicate declaration '${statement.name}'`, statement.source);
|
|
@@ -1155,7 +1349,7 @@ export function checkDocument(doc, file, context) {
|
|
|
1155
1349
|
return;
|
|
1156
1350
|
}
|
|
1157
1351
|
case 'variableDeclaration': {
|
|
1158
|
-
if (types.has(statement.name) || functions.has(statement.name) || views.has(statement.name) || dimensions.has(statement.name) || units.has(statement.name))
|
|
1352
|
+
if (types.has(statement.name) || enums.has(statement.name) || functions.has(statement.name) || views.has(statement.name) || dimensions.has(statement.name) || units.has(statement.name))
|
|
1159
1353
|
fail('AMX3005', `Binding '${statement.name}' conflicts with another declaration`, statement.source);
|
|
1160
1354
|
if (inputNames.has(statement.name))
|
|
1161
1355
|
fail('AMX3005', `Binding '${statement.name}' conflicts with an input`, statement.source);
|
|
@@ -1241,12 +1435,58 @@ export function checkDocument(doc, file, context) {
|
|
|
1241
1435
|
case 'forStatement':
|
|
1242
1436
|
checkLoop(statement.variable, statement.iterable, statement.body, statement.source, undefined, true);
|
|
1243
1437
|
return;
|
|
1244
|
-
case '
|
|
1438
|
+
case 'bracedIfStatement': {
|
|
1439
|
+
requireType(infer(statement.test), named('Boolean'), statement.test.source);
|
|
1440
|
+
checkBranchScope(statement.consequent, () => { });
|
|
1441
|
+
if (statement.alternate)
|
|
1442
|
+
checkBranchScope(statement.alternate, () => { });
|
|
1443
|
+
return;
|
|
1444
|
+
}
|
|
1445
|
+
case 'returnStatement':
|
|
1446
|
+
if (loopDepth > 0)
|
|
1447
|
+
infer(statement.expression);
|
|
1448
|
+
else
|
|
1449
|
+
fail('AMX3003', 'Return outside expression loop', statement.source);
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
function checkInheritanceCycles() {
|
|
1453
|
+
const declarations = doc.nodes.flatMap(node => node.type === 'executableCodeBlock' ? node.statements : [])
|
|
1454
|
+
.filter((statement) => statement.type === 'typeDeclaration');
|
|
1455
|
+
const declarationsByName = new Map();
|
|
1456
|
+
const duplicateNames = new Set();
|
|
1457
|
+
for (const declaration of declarations) {
|
|
1458
|
+
if (declarationsByName.has(declaration.name))
|
|
1459
|
+
duplicateNames.add(declaration.name);
|
|
1460
|
+
else
|
|
1461
|
+
declarationsByName.set(declaration.name, declaration);
|
|
1462
|
+
}
|
|
1463
|
+
for (const name of duplicateNames)
|
|
1464
|
+
declarationsByName.delete(name);
|
|
1465
|
+
const states = new Map();
|
|
1466
|
+
const visit = (declaration) => {
|
|
1467
|
+
states.set(declaration.name, 'visiting');
|
|
1468
|
+
for (const parent of declaration.parents ?? []) {
|
|
1469
|
+
const parentDeclaration = declarationsByName.get(parent.name);
|
|
1470
|
+
if (!parentDeclaration)
|
|
1471
|
+
continue;
|
|
1472
|
+
const state = states.get(parent.name);
|
|
1473
|
+
if (state === 'visiting') {
|
|
1474
|
+
fail('AMX3014', 'The inherited type causes a circular dependency', parent.source, parentDeclaration.nameSource);
|
|
1475
|
+
}
|
|
1476
|
+
if (!state)
|
|
1477
|
+
visit(parentDeclaration);
|
|
1478
|
+
}
|
|
1479
|
+
states.set(declaration.name, 'visited');
|
|
1480
|
+
};
|
|
1481
|
+
for (const declaration of declarationsByName.values()) {
|
|
1482
|
+
if (!states.has(declaration.name))
|
|
1483
|
+
visit(declaration);
|
|
1245
1484
|
}
|
|
1246
1485
|
}
|
|
1247
1486
|
let sawNonImport = false;
|
|
1248
1487
|
let sawInput = false;
|
|
1249
1488
|
let sawOtherItem = false;
|
|
1489
|
+
checkInheritanceCycles();
|
|
1250
1490
|
for (const node of doc.nodes) {
|
|
1251
1491
|
if (node.type !== 'executableCodeBlock')
|
|
1252
1492
|
continue;
|
|
@@ -1270,6 +1510,7 @@ export function checkDocument(doc, file, context) {
|
|
|
1270
1510
|
}
|
|
1271
1511
|
return {
|
|
1272
1512
|
exportedTypes,
|
|
1513
|
+
exportedEnums,
|
|
1273
1514
|
exportedFunctions,
|
|
1274
1515
|
exportedBindings,
|
|
1275
1516
|
bindingTypes: new Map(bindings),
|