@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
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import {
|
|
2
|
+
comparisonOperators,
|
|
3
|
+
type Constant,
|
|
4
|
+
type Expression,
|
|
5
|
+
type Variable,
|
|
6
|
+
} from "./model.ts";
|
|
7
|
+
import { fold } from "./fold.ts";
|
|
8
|
+
import {
|
|
9
|
+
admits,
|
|
10
|
+
canAssign,
|
|
11
|
+
constantFits,
|
|
12
|
+
exactIntegerLimit,
|
|
13
|
+
floatMaximum,
|
|
14
|
+
integerType,
|
|
15
|
+
roundFloat,
|
|
16
|
+
within,
|
|
17
|
+
wrapsNatively,
|
|
18
|
+
type IntegerInterval,
|
|
19
|
+
} from "./numeric.ts";
|
|
20
|
+
import { interval } from "./ranges.ts";
|
|
21
|
+
import { integerRange, type PlcType } from "./types.ts";
|
|
22
|
+
|
|
23
|
+
/** An operand's constant value and proved integer range, over the storage in scope. */
|
|
24
|
+
interface Proof {
|
|
25
|
+
fold: (value: Expression) => Constant | undefined;
|
|
26
|
+
interval: (value: Expression) => IntegerInterval | undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const scalarKinds: ReadonlySet<PlcType["kind"]> = new Set([
|
|
30
|
+
"bool",
|
|
31
|
+
"integer",
|
|
32
|
+
"bits",
|
|
33
|
+
"float",
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
export const scalarIssue = (
|
|
37
|
+
expression: Expression,
|
|
38
|
+
types: ReadonlyMap<string, PlcType>,
|
|
39
|
+
variables: ReadonlyMap<string, Variable>,
|
|
40
|
+
ranges: ReadonlyMap<string, IntegerInterval> = new Map(),
|
|
41
|
+
): string | undefined => {
|
|
42
|
+
const proof: Proof = {
|
|
43
|
+
fold: (value) => fold(value, variables, types),
|
|
44
|
+
interval: (value) => interval(value, types, variables, ranges),
|
|
45
|
+
};
|
|
46
|
+
const type = types.get(expression.type);
|
|
47
|
+
if (!type || !scalarKinds.has(type.kind)) {
|
|
48
|
+
return "Unknown scalar expression type.";
|
|
49
|
+
}
|
|
50
|
+
if (expression.kind === "constant") {
|
|
51
|
+
return constantFits(expression.value, type)
|
|
52
|
+
? undefined
|
|
53
|
+
: "Literal is outside its PLC type or has the wrong domain.";
|
|
54
|
+
}
|
|
55
|
+
if (expression.kind === "load" || expression.kind === "call") {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
if (expression.kind === "convert" || expression.kind === "unary") {
|
|
59
|
+
const source = types.get(expression.operand.type);
|
|
60
|
+
if (!source) {
|
|
61
|
+
return "Unknown operand type.";
|
|
62
|
+
}
|
|
63
|
+
return expression.kind === "convert"
|
|
64
|
+
? conversionIssue(expression, source, type, proof)
|
|
65
|
+
: unaryIssue(expression, source, type, proof);
|
|
66
|
+
}
|
|
67
|
+
if (expression.kind === "binary") {
|
|
68
|
+
const left = types.get(expression.left.type);
|
|
69
|
+
const right = types.get(expression.right.type);
|
|
70
|
+
if (!left || !right) {
|
|
71
|
+
return "Unknown operand type.";
|
|
72
|
+
}
|
|
73
|
+
return binaryIssue(expression, left, right, type, proof);
|
|
74
|
+
}
|
|
75
|
+
if ("operand" in expression && !types.has(expression.operand.type)) {
|
|
76
|
+
return "Unknown operand type.";
|
|
77
|
+
}
|
|
78
|
+
return "Expression has not been reduced to a scalar operation.";
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const conversionIssue = (
|
|
82
|
+
expression: Extract<Expression, { kind: "convert" }>,
|
|
83
|
+
source: PlcType,
|
|
84
|
+
type: PlcType,
|
|
85
|
+
proof: Proof,
|
|
86
|
+
): string | undefined => {
|
|
87
|
+
if (!["exact", "wrap", "saturate"].includes(expression.policy)) {
|
|
88
|
+
return "Unknown numeric conversion policy.";
|
|
89
|
+
}
|
|
90
|
+
if (source.kind === "bool" || type.kind === "bool") {
|
|
91
|
+
return "Numeric conversions cannot use BOOL.";
|
|
92
|
+
}
|
|
93
|
+
if (expression.policy === "exact") {
|
|
94
|
+
return exactConversionIssue(expression, source, type, proof);
|
|
95
|
+
}
|
|
96
|
+
if (
|
|
97
|
+
expression.policy === "saturate" &&
|
|
98
|
+
source.kind === "float" &&
|
|
99
|
+
type.kind === "float"
|
|
100
|
+
) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
if (integerType(source) && integerType(type)) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
return expression.policy === "saturate"
|
|
107
|
+
? "Saturate conversions require two integer/bit-string operands or two floating operands."
|
|
108
|
+
: "Wrap conversions require integer or bit-string operands.";
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const exactConversionIssue = (
|
|
112
|
+
expression: Extract<Expression, { kind: "convert" }>,
|
|
113
|
+
source: PlcType,
|
|
114
|
+
type: PlcType,
|
|
115
|
+
proof: Proof,
|
|
116
|
+
): string | undefined => {
|
|
117
|
+
const value = proof.fold(expression.operand);
|
|
118
|
+
if (
|
|
119
|
+
canAssign(source, type, value) ||
|
|
120
|
+
(value &&
|
|
121
|
+
integerType(source) &&
|
|
122
|
+
integerType(type) &&
|
|
123
|
+
constantFits(value, type))
|
|
124
|
+
) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
if (!integerType(source)) {
|
|
128
|
+
return "Exact conversion cannot be proved lossless.";
|
|
129
|
+
}
|
|
130
|
+
if (type.kind === "float" && value?.kind === "integer") {
|
|
131
|
+
const converted = roundFloat(Number(value.value), type);
|
|
132
|
+
if (
|
|
133
|
+
Number.isFinite(converted) &&
|
|
134
|
+
Number.isInteger(converted) &&
|
|
135
|
+
BigInt(converted) === BigInt(value.value)
|
|
136
|
+
) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const proved = proof.interval(expression.operand) ?? integerRange(source);
|
|
141
|
+
if (type.kind === "float") {
|
|
142
|
+
const limit = exactIntegerLimit(type);
|
|
143
|
+
if (within(proved, [-limit, limit])) {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (integerType(type) && within(proved, integerRange(type))) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
return "Exact conversion cannot be proved lossless.";
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const unaryIssue = (
|
|
154
|
+
expression: Extract<Expression, { kind: "unary" }>,
|
|
155
|
+
source: PlcType,
|
|
156
|
+
type: PlcType,
|
|
157
|
+
proof: Proof,
|
|
158
|
+
): string | undefined => {
|
|
159
|
+
switch (expression.operator) {
|
|
160
|
+
case "clock":
|
|
161
|
+
return type.id === "DINT"
|
|
162
|
+
? undefined
|
|
163
|
+
: "now() returns DINT milliseconds.";
|
|
164
|
+
case "timeToMilliseconds":
|
|
165
|
+
return source.id === "TIME" &&
|
|
166
|
+
source.kind === "duration" &&
|
|
167
|
+
type.id === "UDINT"
|
|
168
|
+
? undefined
|
|
169
|
+
: "timeToMilliseconds requires TIME and returns UDINT.";
|
|
170
|
+
case "not":
|
|
171
|
+
return type.kind === "bool" && source.kind === "bool"
|
|
172
|
+
? undefined
|
|
173
|
+
: "Logical negation requires BOOL.";
|
|
174
|
+
case "bitNot":
|
|
175
|
+
return type.kind === "bits" && source.id === type.id
|
|
176
|
+
? undefined
|
|
177
|
+
: "Bitwise complement requires a bit string.";
|
|
178
|
+
case "negate":
|
|
179
|
+
if (
|
|
180
|
+
source.id !== type.id ||
|
|
181
|
+
!(type.kind === "integer" || type.kind === "float")
|
|
182
|
+
) {
|
|
183
|
+
return "Numeric negation requires one arithmetic type.";
|
|
184
|
+
}
|
|
185
|
+
return resultIssue(expression, type, proof);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const binaryIssue = (
|
|
190
|
+
expression: Extract<Expression, { kind: "binary" }>,
|
|
191
|
+
left: PlcType,
|
|
192
|
+
right: PlcType,
|
|
193
|
+
type: PlcType,
|
|
194
|
+
proof: Proof,
|
|
195
|
+
): string | undefined => {
|
|
196
|
+
if (expression.operator === "and" || expression.operator === "or") {
|
|
197
|
+
return type.kind === "bool" && left.kind === "bool" && right.kind === "bool"
|
|
198
|
+
? undefined
|
|
199
|
+
: "Logical operators require BOOL.";
|
|
200
|
+
}
|
|
201
|
+
if (comparisonOperators.has(expression.operator)) {
|
|
202
|
+
return type.kind === "bool" &&
|
|
203
|
+
left.id === right.id &&
|
|
204
|
+
scalarKinds.has(left.kind) &&
|
|
205
|
+
(left.kind !== "bool" ||
|
|
206
|
+
["equal", "notEqual"].includes(expression.operator))
|
|
207
|
+
? undefined
|
|
208
|
+
: "Comparison operands need the same PLC domain.";
|
|
209
|
+
}
|
|
210
|
+
if (
|
|
211
|
+
["bitAnd", "bitOr", "bitXor", "shiftLeft", "shiftRight"].includes(
|
|
212
|
+
expression.operator,
|
|
213
|
+
)
|
|
214
|
+
) {
|
|
215
|
+
if (type.kind !== "bits" || left.id !== type.id) {
|
|
216
|
+
return "Bitwise operators require bit strings.";
|
|
217
|
+
}
|
|
218
|
+
if (
|
|
219
|
+
expression.operator === "shiftLeft" ||
|
|
220
|
+
expression.operator === "shiftRight"
|
|
221
|
+
) {
|
|
222
|
+
const count = proof.fold(expression.right);
|
|
223
|
+
return count?.kind === "integer" &&
|
|
224
|
+
BigInt(count.value) >= 0n &&
|
|
225
|
+
BigInt(count.value) < BigInt(type.bits)
|
|
226
|
+
? undefined
|
|
227
|
+
: "Shift count must be a compile-time integer within the bit width.";
|
|
228
|
+
}
|
|
229
|
+
return right.id === type.id ? undefined : "Bit-string widths differ.";
|
|
230
|
+
}
|
|
231
|
+
if (
|
|
232
|
+
left.id !== type.id ||
|
|
233
|
+
right.id !== type.id ||
|
|
234
|
+
(type.kind !== "integer" && type.kind !== "float")
|
|
235
|
+
) {
|
|
236
|
+
return "Arithmetic operands need one integer or floating type.";
|
|
237
|
+
}
|
|
238
|
+
if (expression.policy) {
|
|
239
|
+
return type.kind === "integer" &&
|
|
240
|
+
["add", "subtract", "multiply"].includes(expression.operator)
|
|
241
|
+
? undefined
|
|
242
|
+
: "Explicit arithmetic policies require integer addition, subtraction or multiplication.";
|
|
243
|
+
}
|
|
244
|
+
if (
|
|
245
|
+
(expression.operator === "divide" || expression.operator === "remainder") &&
|
|
246
|
+
type.kind === "integer"
|
|
247
|
+
) {
|
|
248
|
+
const divisor = proof.interval(expression.right);
|
|
249
|
+
if (!divisor || admits(divisor, 0n)) {
|
|
250
|
+
return "Division requires a proved nonzero divisor.";
|
|
251
|
+
}
|
|
252
|
+
const dividend = proof.interval(expression.left);
|
|
253
|
+
const [minimum] = integerRange(type);
|
|
254
|
+
if (
|
|
255
|
+
type.signed &&
|
|
256
|
+
dividend &&
|
|
257
|
+
dividend[0] <= minimum &&
|
|
258
|
+
admits(divisor, -1n)
|
|
259
|
+
) {
|
|
260
|
+
return "Signed minimum divided by -1 cannot be represented.";
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return resultIssue(expression, type, proof);
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/** Whether an arithmetic result is proved to stay in its integer type, or finite in its float type. */
|
|
267
|
+
const resultIssue = (
|
|
268
|
+
expression: Extract<Expression, { kind: "unary" | "binary" }>,
|
|
269
|
+
type: PlcType,
|
|
270
|
+
proof: Proof,
|
|
271
|
+
): string | undefined => {
|
|
272
|
+
if (integerType(type)) {
|
|
273
|
+
const result = proof.interval(expression);
|
|
274
|
+
if (
|
|
275
|
+
!(wrapsNatively(expression) && !proof.fold(expression)) &&
|
|
276
|
+
(!result || !within(result, integerRange(type)))
|
|
277
|
+
) {
|
|
278
|
+
return "Arithmetic may overflow; widen the result or use an explicit wrap/saturate operation.";
|
|
279
|
+
}
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
if (type.kind !== "float") {
|
|
283
|
+
return undefined;
|
|
284
|
+
}
|
|
285
|
+
const result = proof.fold(expression);
|
|
286
|
+
if (result) {
|
|
287
|
+
return constantFits(result, type)
|
|
288
|
+
? undefined
|
|
289
|
+
: "Floating result is not finite or representable.";
|
|
290
|
+
}
|
|
291
|
+
if (expression.kind === "unary") {
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
const lhs = proof.fold(expression.left);
|
|
295
|
+
const rhs = proof.fold(expression.right);
|
|
296
|
+
if (
|
|
297
|
+
["add", "subtract"].includes(expression.operator) &&
|
|
298
|
+
(lhs?.kind === "float" || rhs?.kind === "float")
|
|
299
|
+
) {
|
|
300
|
+
// Addition/subtraction and nearest rounding are monotone. Checking both
|
|
301
|
+
// finite endpoints proves every finite input without changing its arithmetic.
|
|
302
|
+
const maximum = floatMaximum(type);
|
|
303
|
+
const finite = [-maximum, maximum].every((endpoint) => {
|
|
304
|
+
const a = lhs?.kind === "float" ? lhs.value : endpoint;
|
|
305
|
+
const b = rhs?.kind === "float" ? rhs.value : endpoint;
|
|
306
|
+
const value = expression.operator === "add" ? a + b : a - b;
|
|
307
|
+
return Number.isFinite(roundFloat(value, type));
|
|
308
|
+
});
|
|
309
|
+
if (finite) {
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (
|
|
314
|
+
rhs?.kind === "float" &&
|
|
315
|
+
rhs.value === 1 &&
|
|
316
|
+
["multiply", "divide"].includes(expression.operator)
|
|
317
|
+
) {
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
return "Floating arithmetic must prove a finite result.";
|
|
321
|
+
};
|
package/src/ir/types.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ElementaryType,
|
|
3
|
+
elementaryTypes,
|
|
4
|
+
integerRange as elementaryIntegerRange,
|
|
5
|
+
} from "@tsplc/iec";
|
|
6
|
+
|
|
7
|
+
export type TypeId = string;
|
|
8
|
+
export type SymbolId = string;
|
|
9
|
+
|
|
10
|
+
export interface Field {
|
|
11
|
+
name: string;
|
|
12
|
+
type: TypeId;
|
|
13
|
+
readonly: boolean;
|
|
14
|
+
mode?: "inOut";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type PlcType =
|
|
18
|
+
| { id: TypeId; kind: "bool" }
|
|
19
|
+
| { id: TypeId; kind: "integer"; bits: 8 | 16 | 32 | 64; signed: boolean }
|
|
20
|
+
| { id: TypeId; kind: "bits"; bits: 8 | 16 | 32 | 64 }
|
|
21
|
+
| { id: TypeId; kind: "float"; bits: 32 | 64 }
|
|
22
|
+
| { id: TypeId; kind: "duration" }
|
|
23
|
+
| { id: TypeId; kind: "calendar"; domain: "date" | "timeOfDay" | "dateTime" }
|
|
24
|
+
| { id: TypeId; kind: "string"; capacity: number; encoding: "utf8" | "utf16" }
|
|
25
|
+
| {
|
|
26
|
+
id: TypeId;
|
|
27
|
+
kind: "array";
|
|
28
|
+
element: TypeId;
|
|
29
|
+
length: number;
|
|
30
|
+
lowerBound: number;
|
|
31
|
+
/** `Record<Enum, T>`: the enum whose contiguous member values are the bounds and the only indexes. */
|
|
32
|
+
index?: TypeId;
|
|
33
|
+
}
|
|
34
|
+
| { id: TypeId; kind: "record"; fields: readonly Field[] }
|
|
35
|
+
| {
|
|
36
|
+
id: TypeId;
|
|
37
|
+
kind: "enum";
|
|
38
|
+
base: TypeId;
|
|
39
|
+
/** `text` is the string a plain TypeScript program's literal union member stands for. */
|
|
40
|
+
members: readonly { name: string; value: string; text?: string }[];
|
|
41
|
+
}
|
|
42
|
+
| { id: TypeId; kind: "block"; block: SymbolId; output?: TypeId };
|
|
43
|
+
|
|
44
|
+
/** The block allocated by a type: a block itself, or a fixed array of one. */
|
|
45
|
+
export const instancedBlock = (
|
|
46
|
+
id: TypeId,
|
|
47
|
+
types: ReadonlyMap<TypeId, PlcType>,
|
|
48
|
+
): Extract<PlcType, { kind: "block" }> | undefined => {
|
|
49
|
+
const type = types.get(id);
|
|
50
|
+
const element = type?.kind === "array" ? types.get(type.element) : type;
|
|
51
|
+
return element?.kind === "block" ? element : undefined;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const primitiveType = (type: ElementaryType): PlcType[] => {
|
|
55
|
+
switch (type.family) {
|
|
56
|
+
case "bool":
|
|
57
|
+
return [{ id: type.name, kind: "bool" }];
|
|
58
|
+
case "integer":
|
|
59
|
+
return [
|
|
60
|
+
{
|
|
61
|
+
id: type.name,
|
|
62
|
+
kind: "integer",
|
|
63
|
+
bits: type.bits,
|
|
64
|
+
signed: type.signed,
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
case "bits":
|
|
68
|
+
return [{ id: type.name, kind: "bits", bits: type.bits }];
|
|
69
|
+
case "real":
|
|
70
|
+
return [{ id: type.name, kind: "float", bits: type.bits }];
|
|
71
|
+
case "duration":
|
|
72
|
+
return type.bits === 32 ? [{ id: type.name, kind: "duration" }] : [];
|
|
73
|
+
case "date":
|
|
74
|
+
case "timeOfDay":
|
|
75
|
+
case "dateTime":
|
|
76
|
+
return type.bits === 32
|
|
77
|
+
? [{ id: type.name, kind: "calendar", domain: type.family }]
|
|
78
|
+
: [];
|
|
79
|
+
case "char":
|
|
80
|
+
case "string":
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
type IntegerValued = Extract<PlcType, { kind: "integer" | "bits" }>;
|
|
86
|
+
|
|
87
|
+
const isIntegerValued = (type: PlcType): type is IntegerValued =>
|
|
88
|
+
type.kind === "integer" || type.kind === "bits";
|
|
89
|
+
|
|
90
|
+
const integerKindRank = (type: IntegerValued) => {
|
|
91
|
+
if (type.kind === "bits") {
|
|
92
|
+
return 2;
|
|
93
|
+
}
|
|
94
|
+
return type.signed ? 0 : 1;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const elementaryPrimitives = elementaryTypes.flatMap(primitiveType);
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The elementary types a program uses by name. Date and time types are the 32-bit ones;
|
|
101
|
+
* strings are sized where they are declared, and characters are not modelled.
|
|
102
|
+
*
|
|
103
|
+
* Integers are grouped by width (signed, unsigned, bit string), because the debug manifest
|
|
104
|
+
* lists types in this order and the build id written into the emitted ST hashes it.
|
|
105
|
+
*/
|
|
106
|
+
export const primitiveTypes: readonly PlcType[] = [
|
|
107
|
+
...elementaryPrimitives.filter((type) => type.kind === "bool"),
|
|
108
|
+
...elementaryPrimitives
|
|
109
|
+
.filter(isIntegerValued)
|
|
110
|
+
.sort(
|
|
111
|
+
(left, right) =>
|
|
112
|
+
left.bits - right.bits ||
|
|
113
|
+
integerKindRank(left) - integerKindRank(right),
|
|
114
|
+
),
|
|
115
|
+
...elementaryPrimitives.filter(
|
|
116
|
+
(type) => type.kind !== "bool" && !isIntegerValued(type),
|
|
117
|
+
),
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
export function integerRange(
|
|
121
|
+
type: Extract<PlcType, { kind: "integer" | "bits" }>,
|
|
122
|
+
): readonly [bigint, bigint] {
|
|
123
|
+
return elementaryIntegerRange({
|
|
124
|
+
bits: type.bits,
|
|
125
|
+
signed: type.kind === "integer" && type.signed,
|
|
126
|
+
});
|
|
127
|
+
}
|