@tsplc/compiler 0.2.17 → 0.2.19
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 +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
package/src/ir/fold.ts
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { nanosecondsPerMillisecond } from "./domains.ts";
|
|
2
|
+
import type {
|
|
3
|
+
BinaryOperator,
|
|
4
|
+
Constant,
|
|
5
|
+
Expression,
|
|
6
|
+
Variable,
|
|
7
|
+
} from "./model.ts";
|
|
8
|
+
import {
|
|
9
|
+
applyIntegerPolicy,
|
|
10
|
+
integerType,
|
|
11
|
+
roundFloat,
|
|
12
|
+
saturateFloat,
|
|
13
|
+
} from "./numeric.ts";
|
|
14
|
+
import type { PlcType } from "./types.ts";
|
|
15
|
+
|
|
16
|
+
type Types = ReadonlyMap<string, PlcType> | undefined;
|
|
17
|
+
|
|
18
|
+
export const fold = (
|
|
19
|
+
expression: Expression,
|
|
20
|
+
variables: ReadonlyMap<string, Variable>,
|
|
21
|
+
types?: ReadonlyMap<string, PlcType>,
|
|
22
|
+
): Constant | undefined => {
|
|
23
|
+
switch (expression.kind) {
|
|
24
|
+
case "constant":
|
|
25
|
+
return expression.value;
|
|
26
|
+
case "copy":
|
|
27
|
+
return fold(expression.operand, variables, types);
|
|
28
|
+
case "length":
|
|
29
|
+
return foldLength(expression, variables, types);
|
|
30
|
+
case "record":
|
|
31
|
+
return foldRecord(expression, variables, types);
|
|
32
|
+
case "array": {
|
|
33
|
+
const items = expression.items.map((item) =>
|
|
34
|
+
fold(item, variables, types),
|
|
35
|
+
);
|
|
36
|
+
return items.every((item): item is Constant => item !== undefined)
|
|
37
|
+
? { kind: "array", items }
|
|
38
|
+
: undefined;
|
|
39
|
+
}
|
|
40
|
+
case "load": {
|
|
41
|
+
if (expression.place.path.length) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const variable = variables.get(expression.place.root);
|
|
45
|
+
return variable?.storage === "constant" ? variable.initial : undefined;
|
|
46
|
+
}
|
|
47
|
+
case "convert":
|
|
48
|
+
return foldConvert(
|
|
49
|
+
expression,
|
|
50
|
+
fold(expression.operand, variables, types),
|
|
51
|
+
types?.get(expression.type),
|
|
52
|
+
);
|
|
53
|
+
case "unary":
|
|
54
|
+
return foldUnary(
|
|
55
|
+
expression,
|
|
56
|
+
fold(expression.operand, variables, types),
|
|
57
|
+
types?.get(expression.type),
|
|
58
|
+
);
|
|
59
|
+
case "binary": {
|
|
60
|
+
const left = fold(expression.left, variables, types);
|
|
61
|
+
const right = fold(expression.right, variables, types);
|
|
62
|
+
return left && right
|
|
63
|
+
? foldBinary(expression, left, right, types?.get(expression.type))
|
|
64
|
+
: undefined;
|
|
65
|
+
}
|
|
66
|
+
default:
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const foldLength = (
|
|
72
|
+
expression: Extract<Expression, { kind: "length" }>,
|
|
73
|
+
variables: ReadonlyMap<string, Variable>,
|
|
74
|
+
types: Types,
|
|
75
|
+
): Constant | undefined => {
|
|
76
|
+
const type = types?.get(expression.operand.type);
|
|
77
|
+
const receiver =
|
|
78
|
+
expression.operand.kind === "load"
|
|
79
|
+
? variables.get(expression.operand.place.root)
|
|
80
|
+
: undefined;
|
|
81
|
+
const pure =
|
|
82
|
+
expression.operand.kind === "load" &&
|
|
83
|
+
expression.operand.place.path.every(
|
|
84
|
+
(segment) => segment.kind === "field",
|
|
85
|
+
) &&
|
|
86
|
+
Boolean(
|
|
87
|
+
receiver?.initial ||
|
|
88
|
+
receiver?.storage === "input" ||
|
|
89
|
+
receiver?.storage === "output",
|
|
90
|
+
);
|
|
91
|
+
if (
|
|
92
|
+
type?.kind === "array" &&
|
|
93
|
+
(pure || fold(expression.operand, variables, types))
|
|
94
|
+
) {
|
|
95
|
+
return { kind: "integer", value: String(type.length) };
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const foldRecord = (
|
|
101
|
+
expression: Extract<Expression, { kind: "record" }>,
|
|
102
|
+
variables: ReadonlyMap<string, Variable>,
|
|
103
|
+
types: Types,
|
|
104
|
+
): Constant | undefined => {
|
|
105
|
+
const fields: Record<string, Constant> = Object.create(null);
|
|
106
|
+
for (const [name, operand] of Object.entries(expression.fields)) {
|
|
107
|
+
const value = fold(operand, variables, types);
|
|
108
|
+
if (!value) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
fields[name] = value;
|
|
112
|
+
}
|
|
113
|
+
return { kind: "record", fields };
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const foldConvert = (
|
|
117
|
+
expression: Extract<Expression, { kind: "convert" }>,
|
|
118
|
+
value: Constant | undefined,
|
|
119
|
+
target: PlcType | undefined,
|
|
120
|
+
): Constant | undefined => {
|
|
121
|
+
if (value?.kind === "integer" && target && integerType(target)) {
|
|
122
|
+
const integer = BigInt(value.value);
|
|
123
|
+
return {
|
|
124
|
+
kind: "integer",
|
|
125
|
+
value: String(
|
|
126
|
+
expression.policy === "exact"
|
|
127
|
+
? integer
|
|
128
|
+
: applyIntegerPolicy(integer, target, expression.policy),
|
|
129
|
+
),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (value?.kind === "integer" && target?.kind === "float") {
|
|
133
|
+
return { kind: "float", value: roundFloat(Number(value.value), target) };
|
|
134
|
+
}
|
|
135
|
+
if (value?.kind === "float" && target?.kind === "float") {
|
|
136
|
+
return {
|
|
137
|
+
kind: "float",
|
|
138
|
+
value:
|
|
139
|
+
expression.policy === "saturate"
|
|
140
|
+
? saturateFloat(value.value, target)
|
|
141
|
+
: roundFloat(value.value, target),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
return undefined;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const foldUnary = (
|
|
148
|
+
expression: Extract<Expression, { kind: "unary" }>,
|
|
149
|
+
value: Constant | undefined,
|
|
150
|
+
target: PlcType | undefined,
|
|
151
|
+
): Constant | undefined => {
|
|
152
|
+
if (
|
|
153
|
+
value?.kind === "duration" &&
|
|
154
|
+
expression.operator === "timeToMilliseconds"
|
|
155
|
+
) {
|
|
156
|
+
return {
|
|
157
|
+
kind: "integer",
|
|
158
|
+
value: String(BigInt(value.nanoseconds) / nanosecondsPerMillisecond),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (value?.kind === "bool" && expression.operator === "not") {
|
|
162
|
+
return { kind: "bool", value: !value.value };
|
|
163
|
+
}
|
|
164
|
+
if (
|
|
165
|
+
value?.kind === "integer" &&
|
|
166
|
+
expression.operator === "bitNot" &&
|
|
167
|
+
target?.kind === "bits"
|
|
168
|
+
) {
|
|
169
|
+
return {
|
|
170
|
+
kind: "integer",
|
|
171
|
+
value: String(applyIntegerPolicy(~BigInt(value.value), target, "wrap")),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (value?.kind === "integer" && expression.operator === "negate") {
|
|
175
|
+
return { kind: "integer", value: String(-BigInt(value.value)) };
|
|
176
|
+
}
|
|
177
|
+
if (value?.kind === "float" && expression.operator === "negate") {
|
|
178
|
+
return { kind: "float", value: -value.value };
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
type Ordered = Exclude<Constant, { kind: "bool" | "array" | "record" }>;
|
|
184
|
+
|
|
185
|
+
const ordered = (value: Constant): value is Ordered =>
|
|
186
|
+
value.kind !== "bool" && value.kind !== "array" && value.kind !== "record";
|
|
187
|
+
|
|
188
|
+
/** The value two constants of one kind compare as; durations compare by nanoseconds. */
|
|
189
|
+
const comparable = (value: Ordered): bigint | number | string => {
|
|
190
|
+
switch (value.kind) {
|
|
191
|
+
case "integer":
|
|
192
|
+
return BigInt(value.value);
|
|
193
|
+
case "duration":
|
|
194
|
+
return BigInt(value.nanoseconds);
|
|
195
|
+
default:
|
|
196
|
+
return value.value;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const foldBinary = (
|
|
201
|
+
expression: Extract<Expression, { kind: "binary" }>,
|
|
202
|
+
left: Constant,
|
|
203
|
+
right: Constant,
|
|
204
|
+
target: PlcType | undefined,
|
|
205
|
+
): Constant | undefined => {
|
|
206
|
+
if (left.kind === "bool" && right.kind === "bool") {
|
|
207
|
+
return foldLogical(expression.operator, left.value, right.value);
|
|
208
|
+
}
|
|
209
|
+
if (left.kind !== right.kind || !ordered(left) || !ordered(right)) {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
const compared = compare(
|
|
213
|
+
expression.operator,
|
|
214
|
+
comparable(left),
|
|
215
|
+
comparable(right),
|
|
216
|
+
);
|
|
217
|
+
if (compared !== undefined) {
|
|
218
|
+
return { kind: "bool", value: compared };
|
|
219
|
+
}
|
|
220
|
+
if (left.kind === "integer" && right.kind === "integer") {
|
|
221
|
+
const value = integerArithmetic(
|
|
222
|
+
expression.operator,
|
|
223
|
+
BigInt(left.value),
|
|
224
|
+
BigInt(right.value),
|
|
225
|
+
);
|
|
226
|
+
if (value === undefined) {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
const wrapped =
|
|
230
|
+
target &&
|
|
231
|
+
integerType(target) &&
|
|
232
|
+
(expression.policy || target.kind === "bits")
|
|
233
|
+
? applyIntegerPolicy(value, target, expression.policy ?? "wrap")
|
|
234
|
+
: value;
|
|
235
|
+
return { kind: "integer", value: String(wrapped) };
|
|
236
|
+
}
|
|
237
|
+
if (left.kind === "float" && right.kind === "float") {
|
|
238
|
+
const value = floatArithmetic(expression.operator, left.value, right.value);
|
|
239
|
+
if (value === undefined) {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
kind: "float",
|
|
244
|
+
value: target?.kind === "float" ? roundFloat(value, target) : value,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
return undefined;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const foldLogical = (
|
|
251
|
+
operator: BinaryOperator,
|
|
252
|
+
left: boolean,
|
|
253
|
+
right: boolean,
|
|
254
|
+
): Constant | undefined => {
|
|
255
|
+
switch (operator) {
|
|
256
|
+
case "and":
|
|
257
|
+
return { kind: "bool", value: left && right };
|
|
258
|
+
case "or":
|
|
259
|
+
return { kind: "bool", value: left || right };
|
|
260
|
+
case "equal":
|
|
261
|
+
return { kind: "bool", value: left === right };
|
|
262
|
+
case "notEqual":
|
|
263
|
+
return { kind: "bool", value: left !== right };
|
|
264
|
+
default:
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const compare = <Value extends bigint | number | string>(
|
|
270
|
+
operator: BinaryOperator,
|
|
271
|
+
left: Value,
|
|
272
|
+
right: Value,
|
|
273
|
+
): boolean | undefined => {
|
|
274
|
+
switch (operator) {
|
|
275
|
+
case "equal":
|
|
276
|
+
return left === right;
|
|
277
|
+
case "notEqual":
|
|
278
|
+
return left !== right;
|
|
279
|
+
case "less":
|
|
280
|
+
return left < right;
|
|
281
|
+
case "lessEqual":
|
|
282
|
+
return left <= right;
|
|
283
|
+
case "greater":
|
|
284
|
+
return left > right;
|
|
285
|
+
case "greaterEqual":
|
|
286
|
+
return left >= right;
|
|
287
|
+
default:
|
|
288
|
+
return undefined;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const integerArithmetic = (
|
|
293
|
+
operator: BinaryOperator,
|
|
294
|
+
left: bigint,
|
|
295
|
+
right: bigint,
|
|
296
|
+
): bigint | undefined => {
|
|
297
|
+
switch (operator) {
|
|
298
|
+
case "add":
|
|
299
|
+
return left + right;
|
|
300
|
+
case "subtract":
|
|
301
|
+
return left - right;
|
|
302
|
+
case "multiply":
|
|
303
|
+
return left * right;
|
|
304
|
+
case "divide":
|
|
305
|
+
return right === 0n ? undefined : left / right;
|
|
306
|
+
case "remainder":
|
|
307
|
+
return right === 0n ? undefined : left % right;
|
|
308
|
+
case "bitAnd":
|
|
309
|
+
return left & right;
|
|
310
|
+
case "bitOr":
|
|
311
|
+
return left | right;
|
|
312
|
+
case "bitXor":
|
|
313
|
+
return left ^ right;
|
|
314
|
+
case "shiftLeft":
|
|
315
|
+
return right < 0n || right > 63n ? undefined : left << right;
|
|
316
|
+
case "shiftRight":
|
|
317
|
+
return right < 0n || right > 63n ? undefined : left >> right;
|
|
318
|
+
default:
|
|
319
|
+
return undefined;
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const floatArithmetic = (
|
|
324
|
+
operator: BinaryOperator,
|
|
325
|
+
left: number,
|
|
326
|
+
right: number,
|
|
327
|
+
): number | undefined => {
|
|
328
|
+
switch (operator) {
|
|
329
|
+
case "add":
|
|
330
|
+
return left + right;
|
|
331
|
+
case "subtract":
|
|
332
|
+
return left - right;
|
|
333
|
+
case "multiply":
|
|
334
|
+
return left * right;
|
|
335
|
+
case "divide":
|
|
336
|
+
return left / right;
|
|
337
|
+
case "remainder":
|
|
338
|
+
return left % right;
|
|
339
|
+
default:
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
};
|
package/src/ir/index.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
export { isVerifiedCore, normalizeModule } from "./normalize.ts";
|
|
2
|
+
export {
|
|
3
|
+
type Diagnostic,
|
|
4
|
+
type SourceSpan,
|
|
5
|
+
error,
|
|
6
|
+
formatDiagnostic,
|
|
7
|
+
hasErrors,
|
|
8
|
+
} from "./diagnostics.ts";
|
|
9
|
+
export {
|
|
10
|
+
type Field,
|
|
11
|
+
type PlcType,
|
|
12
|
+
type SymbolId,
|
|
13
|
+
type TypeId,
|
|
14
|
+
instancedBlock,
|
|
15
|
+
integerRange,
|
|
16
|
+
primitiveTypes,
|
|
17
|
+
} from "./types.ts";
|
|
18
|
+
export {
|
|
19
|
+
type Artifact,
|
|
20
|
+
type BinaryOperator,
|
|
21
|
+
type Constant,
|
|
22
|
+
type Deployment,
|
|
23
|
+
type EmitResult,
|
|
24
|
+
type Expression,
|
|
25
|
+
type FunctionBlock,
|
|
26
|
+
type GeneratedObject,
|
|
27
|
+
type MathHelper,
|
|
28
|
+
type MethodOwner,
|
|
29
|
+
type Place,
|
|
30
|
+
type PlcFunction,
|
|
31
|
+
type PlcModule,
|
|
32
|
+
type PlcProgram,
|
|
33
|
+
type StandardBlock,
|
|
34
|
+
type Statement,
|
|
35
|
+
type Task,
|
|
36
|
+
type TelemetryCall,
|
|
37
|
+
type Variable,
|
|
38
|
+
comparisonOperators,
|
|
39
|
+
standardBlocks,
|
|
40
|
+
telemetryOperands,
|
|
41
|
+
} from "./model.ts";
|
|
42
|
+
export {
|
|
43
|
+
type CheckedPlace,
|
|
44
|
+
type CoreExpression,
|
|
45
|
+
type CoreFunction,
|
|
46
|
+
type CoreFunctionBlock,
|
|
47
|
+
type CorePath,
|
|
48
|
+
type CoreStatement,
|
|
49
|
+
type VerifiedCoreModule,
|
|
50
|
+
nestedBodies,
|
|
51
|
+
} from "./core-model.ts";
|
|
52
|
+
export {
|
|
53
|
+
type IntegerInterval,
|
|
54
|
+
applyIntegerPolicy,
|
|
55
|
+
canAssign,
|
|
56
|
+
constantFits,
|
|
57
|
+
floatMaximum,
|
|
58
|
+
integerType,
|
|
59
|
+
parseInteger,
|
|
60
|
+
roundFloat,
|
|
61
|
+
saturateFloat,
|
|
62
|
+
wrapsNatively,
|
|
63
|
+
} from "./numeric.ts";
|
|
64
|
+
export { fold } from "./fold.ts";
|
|
65
|
+
export {
|
|
66
|
+
interval,
|
|
67
|
+
loopBounds,
|
|
68
|
+
loopIterations,
|
|
69
|
+
maxLoopIterations,
|
|
70
|
+
narrowRanges,
|
|
71
|
+
valueRange,
|
|
72
|
+
} from "./ranges.ts";
|
|
73
|
+
export { scalarIssue } from "./scalars.ts";
|
|
74
|
+
export {
|
|
75
|
+
aggregate,
|
|
76
|
+
aggregateIssue,
|
|
77
|
+
defaultValue,
|
|
78
|
+
expressionIssue,
|
|
79
|
+
placeType,
|
|
80
|
+
valueFits,
|
|
81
|
+
} from "./aggregates.ts";
|
|
82
|
+
export { layoutIssue, maxNestingDepth, maxScalarElements } from "./layouts.ts";
|
|
83
|
+
export {
|
|
84
|
+
calendarLiteral,
|
|
85
|
+
domainConstantFits,
|
|
86
|
+
domainIssue,
|
|
87
|
+
durationLiteral,
|
|
88
|
+
nanosecondsPerMillisecond,
|
|
89
|
+
} from "./domains.ts";
|
|
90
|
+
export { errorName, httpErrors, jobErrors } from "./job-errors.ts";
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/** Job error codes, as every generated async block and service call reports them. */
|
|
2
2
|
export const jobErrors: Record<string, string> = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
"1": "cancelled",
|
|
4
|
+
"2": "timeout",
|
|
5
|
+
"3": "request or response too large to encode",
|
|
6
|
+
"4": "malformed response",
|
|
7
|
+
"5": "network error or server restarted",
|
|
8
|
+
"6": "TLS error",
|
|
9
|
+
"7": "no free request slot",
|
|
10
|
+
"10000": "HTTP status: 10000 + status code",
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
/** An HTTP response fails a job with `base` + its status, up to `last`. */
|
|
@@ -15,6 +15,8 @@ export const httpErrors = { base: 10000, last: 10999 };
|
|
|
15
15
|
|
|
16
16
|
/** What a telemetry exception record calls an error code: its meaning when it is a job error. */
|
|
17
17
|
export const errorName = (code: number): string | undefined => {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
if (code > httpErrors.base && code <= httpErrors.last) {
|
|
19
|
+
return `HTTP ${code - httpErrors.base}`;
|
|
20
|
+
}
|
|
21
|
+
return code < httpErrors.base ? jobErrors[code] : undefined;
|
|
20
22
|
};
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { elementaryType, integerRange } from "@tsplc/iec";
|
|
2
|
+
import { constantFits } from "./numeric.ts";
|
|
3
|
+
import { InvalidCore, reject } from "./normalization-error.ts";
|
|
4
|
+
import { primitiveTypes, type PlcType } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
/** The finite storage budget: scalar elements in one layout or allocation, and nesting depth. */
|
|
7
|
+
export const maxScalarElements = 1_000_000;
|
|
8
|
+
export const maxNestingDepth = 64;
|
|
9
|
+
|
|
10
|
+
// Array bounds and indexes are DINT.
|
|
11
|
+
const [firstIndex, lastIndex] = integerRange(elementaryType("DINT"));
|
|
12
|
+
|
|
13
|
+
const definitionIssue = (
|
|
14
|
+
type: PlcType,
|
|
15
|
+
types: ReadonlyMap<string, PlcType>,
|
|
16
|
+
): string | undefined => {
|
|
17
|
+
const canonical = primitiveTypes.find((item) => item.id === type.id);
|
|
18
|
+
if (canonical) {
|
|
19
|
+
return type.kind !== canonical.kind ||
|
|
20
|
+
("bits" in canonical &&
|
|
21
|
+
(!("bits" in type) || type.bits !== canonical.bits)) ||
|
|
22
|
+
("signed" in canonical &&
|
|
23
|
+
(!("signed" in type) || type.signed !== canonical.signed)) ||
|
|
24
|
+
("domain" in canonical &&
|
|
25
|
+
(!("domain" in type) || type.domain !== canonical.domain))
|
|
26
|
+
? `Conflicting primitive type ${type.id}`
|
|
27
|
+
: undefined;
|
|
28
|
+
}
|
|
29
|
+
switch (type.kind) {
|
|
30
|
+
case "record":
|
|
31
|
+
return !type.fields.length ||
|
|
32
|
+
new Set(type.fields.map((field) => field.name)).size !==
|
|
33
|
+
type.fields.length ||
|
|
34
|
+
type.fields.some(
|
|
35
|
+
(field) =>
|
|
36
|
+
!field.name ||
|
|
37
|
+
typeof field.readonly !== "boolean" ||
|
|
38
|
+
(field.mode && field.mode !== "inOut") ||
|
|
39
|
+
!types.has(field.type),
|
|
40
|
+
)
|
|
41
|
+
? `Invalid closed record layout ${type.id}`
|
|
42
|
+
: undefined;
|
|
43
|
+
case "array": {
|
|
44
|
+
if (
|
|
45
|
+
!types.has(type.element) ||
|
|
46
|
+
!Number.isSafeInteger(type.length) ||
|
|
47
|
+
type.length < 1 ||
|
|
48
|
+
type.length > maxScalarElements ||
|
|
49
|
+
!Number.isInteger(type.lowerBound) ||
|
|
50
|
+
BigInt(type.lowerBound) < firstIndex ||
|
|
51
|
+
BigInt(type.lowerBound + type.length - 1) > lastIndex
|
|
52
|
+
) {
|
|
53
|
+
return `Invalid fixed array layout ${type.id}`;
|
|
54
|
+
}
|
|
55
|
+
if (type.index === undefined) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const index = types.get(type.index);
|
|
59
|
+
return index?.kind !== "enum" ||
|
|
60
|
+
index.members.length !== type.length ||
|
|
61
|
+
index.members.some(
|
|
62
|
+
(member) =>
|
|
63
|
+
Number(member.value) < type.lowerBound ||
|
|
64
|
+
Number(member.value) >= type.lowerBound + type.length,
|
|
65
|
+
)
|
|
66
|
+
? `Invalid enum-keyed array layout ${type.id}`
|
|
67
|
+
: undefined;
|
|
68
|
+
}
|
|
69
|
+
case "enum": {
|
|
70
|
+
const base = types.get(type.base);
|
|
71
|
+
return type.base !== "DINT" ||
|
|
72
|
+
base?.kind !== "integer" ||
|
|
73
|
+
!type.members.length ||
|
|
74
|
+
new Set(type.members.map((member) => member.name)).size !==
|
|
75
|
+
type.members.length ||
|
|
76
|
+
new Set(type.members.map((member) => member.value)).size !==
|
|
77
|
+
type.members.length ||
|
|
78
|
+
type.members.some(
|
|
79
|
+
(member) =>
|
|
80
|
+
!member.name ||
|
|
81
|
+
!constantFits({ kind: "integer", value: member.value }, base),
|
|
82
|
+
)
|
|
83
|
+
? `Invalid enum layout ${type.id}`
|
|
84
|
+
: undefined;
|
|
85
|
+
}
|
|
86
|
+
case "string":
|
|
87
|
+
return !Number.isInteger(type.capacity) ||
|
|
88
|
+
type.capacity < 1 ||
|
|
89
|
+
type.capacity > 65535 ||
|
|
90
|
+
!["utf8", "utf16"].includes(type.encoding)
|
|
91
|
+
? `Invalid bounded string layout ${type.id}`
|
|
92
|
+
: undefined;
|
|
93
|
+
case "block":
|
|
94
|
+
return !type.block ||
|
|
95
|
+
(type.output && types.get(type.output)?.kind !== "record")
|
|
96
|
+
? `Invalid block type ${type.id}`
|
|
97
|
+
: undefined;
|
|
98
|
+
default:
|
|
99
|
+
return `Unsupported core type definition ${type.id}`;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
interface Layout {
|
|
104
|
+
size: number;
|
|
105
|
+
height: number;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Validates every layout edge once, before constants or places can traverse them, and measures
|
|
110
|
+
* each by-value layout against the storage budget. Returns each type's size in scalar elements.
|
|
111
|
+
*/
|
|
112
|
+
export const admitLayouts = (
|
|
113
|
+
types: ReadonlyMap<string, PlcType>,
|
|
114
|
+
): ReadonlyMap<string, number> => {
|
|
115
|
+
for (const type of types.values()) {
|
|
116
|
+
const issue = definitionIssue(type, types);
|
|
117
|
+
if (issue) {
|
|
118
|
+
reject(issue);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const active = new Set<string>();
|
|
122
|
+
const layouts = new Map<string, Layout>();
|
|
123
|
+
const measure = (id: string, depth: number): Layout => {
|
|
124
|
+
if (active.has(id) || depth > maxNestingDepth) {
|
|
125
|
+
reject(`Recursive or excessively nested by-value layout ${id}`);
|
|
126
|
+
}
|
|
127
|
+
const cached = layouts.get(id);
|
|
128
|
+
if (cached) {
|
|
129
|
+
if (depth + cached.height > maxNestingDepth) {
|
|
130
|
+
reject(`Layout nesting exceeds ${maxNestingDepth} levels ${id}`);
|
|
131
|
+
}
|
|
132
|
+
return cached;
|
|
133
|
+
}
|
|
134
|
+
active.add(id);
|
|
135
|
+
// Every edge was validated above.
|
|
136
|
+
const layout = composite(types.get(id)!, depth);
|
|
137
|
+
active.delete(id);
|
|
138
|
+
if (layout.size > maxScalarElements) {
|
|
139
|
+
reject(`Layout exceeds one million scalar elements ${id}`);
|
|
140
|
+
}
|
|
141
|
+
if (depth + layout.height > maxNestingDepth) {
|
|
142
|
+
reject(`Layout nesting exceeds ${maxNestingDepth} levels ${id}`);
|
|
143
|
+
}
|
|
144
|
+
layouts.set(id, layout);
|
|
145
|
+
return layout;
|
|
146
|
+
};
|
|
147
|
+
const composite = (type: PlcType, depth: number): Layout => {
|
|
148
|
+
if (type.kind === "array") {
|
|
149
|
+
const element = measure(type.element, depth + 1);
|
|
150
|
+
return { size: type.length * element.size, height: 1 + element.height };
|
|
151
|
+
}
|
|
152
|
+
if (type.kind === "record") {
|
|
153
|
+
const fields = type.fields.map((field) => measure(field.type, depth + 1));
|
|
154
|
+
return {
|
|
155
|
+
size: fields.reduce((sum, field) => sum + field.size, 0),
|
|
156
|
+
height:
|
|
157
|
+
1 +
|
|
158
|
+
fields.reduce((maximum, field) => Math.max(maximum, field.height), 0),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return { size: 1, height: 0 };
|
|
162
|
+
};
|
|
163
|
+
for (const id of types.keys()) {
|
|
164
|
+
measure(id, 0);
|
|
165
|
+
}
|
|
166
|
+
return new Map([...layouts].map(([id, layout]) => [id, layout.size]));
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export const layoutIssue = (
|
|
170
|
+
types: ReadonlyMap<string, PlcType>,
|
|
171
|
+
): string | undefined => {
|
|
172
|
+
try {
|
|
173
|
+
admitLayouts(types);
|
|
174
|
+
return undefined;
|
|
175
|
+
} catch (cause) {
|
|
176
|
+
if (cause instanceof InvalidCore) {
|
|
177
|
+
return cause.diagnostic.message;
|
|
178
|
+
}
|
|
179
|
+
throw cause;
|
|
180
|
+
}
|
|
181
|
+
};
|