@tsplc/compiler 0.2.17 → 0.2.18
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/model.ts
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import type { SourceSpan, Diagnostic } from "./diagnostics.ts";
|
|
2
|
+
import type { PlcType, SymbolId, TypeId } from "./types.ts";
|
|
3
|
+
|
|
4
|
+
export type Constant =
|
|
5
|
+
| { kind: "bool"; value: boolean }
|
|
6
|
+
| { kind: "integer"; value: string }
|
|
7
|
+
| { kind: "float"; value: number }
|
|
8
|
+
| { kind: "duration"; nanoseconds: string }
|
|
9
|
+
| { kind: "calendar"; value: string }
|
|
10
|
+
| { kind: "string"; value: string }
|
|
11
|
+
| { kind: "array"; items: readonly Constant[] }
|
|
12
|
+
| { kind: "record"; fields: Readonly<Record<string, Constant>> };
|
|
13
|
+
|
|
14
|
+
export interface Variable {
|
|
15
|
+
id: SymbolId;
|
|
16
|
+
name: string;
|
|
17
|
+
type: TypeId;
|
|
18
|
+
storage:
|
|
19
|
+
| "module"
|
|
20
|
+
| "local"
|
|
21
|
+
| "parameter"
|
|
22
|
+
| "input"
|
|
23
|
+
| "output"
|
|
24
|
+
| "field"
|
|
25
|
+
| "instance"
|
|
26
|
+
| "constant"
|
|
27
|
+
| "blockOutput";
|
|
28
|
+
mutable: boolean;
|
|
29
|
+
retain: boolean;
|
|
30
|
+
exported?: true;
|
|
31
|
+
exposure?: { name: string; version: string };
|
|
32
|
+
mode?: "inOut";
|
|
33
|
+
initial?: Constant;
|
|
34
|
+
span: SourceSpan;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface Place {
|
|
38
|
+
root: SymbolId;
|
|
39
|
+
path: readonly (
|
|
40
|
+
{ kind: "field"; name: string } | { kind: "index"; value: Expression }
|
|
41
|
+
)[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type BinaryOperator =
|
|
45
|
+
| "add"
|
|
46
|
+
| "subtract"
|
|
47
|
+
| "multiply"
|
|
48
|
+
| "divide"
|
|
49
|
+
| "remainder"
|
|
50
|
+
| "equal"
|
|
51
|
+
| "notEqual"
|
|
52
|
+
| "less"
|
|
53
|
+
| "lessEqual"
|
|
54
|
+
| "greater"
|
|
55
|
+
| "greaterEqual"
|
|
56
|
+
| "and"
|
|
57
|
+
| "or"
|
|
58
|
+
| "bitAnd"
|
|
59
|
+
| "bitOr"
|
|
60
|
+
| "bitXor"
|
|
61
|
+
| "shiftLeft"
|
|
62
|
+
| "shiftRight";
|
|
63
|
+
|
|
64
|
+
export const comparisonOperators: ReadonlySet<BinaryOperator> = new Set([
|
|
65
|
+
"equal",
|
|
66
|
+
"notEqual",
|
|
67
|
+
"less",
|
|
68
|
+
"lessEqual",
|
|
69
|
+
"greater",
|
|
70
|
+
"greaterEqual",
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
export type Expression = { type: TypeId; span: SourceSpan } & (
|
|
74
|
+
| { kind: "constant"; value: Constant }
|
|
75
|
+
| { kind: "load"; place: Place }
|
|
76
|
+
| {
|
|
77
|
+
kind: "unary";
|
|
78
|
+
operator: "not" | "negate" | "bitNot" | "timeToMilliseconds" | "clock";
|
|
79
|
+
operand: Expression;
|
|
80
|
+
}
|
|
81
|
+
| {
|
|
82
|
+
kind: "binary";
|
|
83
|
+
operator: BinaryOperator;
|
|
84
|
+
left: Expression;
|
|
85
|
+
right: Expression;
|
|
86
|
+
policy?: "wrap" | "saturate";
|
|
87
|
+
}
|
|
88
|
+
| {
|
|
89
|
+
kind: "call";
|
|
90
|
+
function: SymbolId;
|
|
91
|
+
instance?: SymbolId;
|
|
92
|
+
index?: Expression;
|
|
93
|
+
arguments: readonly Expression[];
|
|
94
|
+
}
|
|
95
|
+
| {
|
|
96
|
+
kind: "conditional";
|
|
97
|
+
condition: Expression;
|
|
98
|
+
then: Expression;
|
|
99
|
+
else: Expression;
|
|
100
|
+
}
|
|
101
|
+
| {
|
|
102
|
+
kind: "convert";
|
|
103
|
+
operand: Expression;
|
|
104
|
+
policy: "exact" | "wrap" | "saturate";
|
|
105
|
+
}
|
|
106
|
+
| { kind: "borrow"; place: Place }
|
|
107
|
+
| { kind: "length"; operand: Expression }
|
|
108
|
+
| { kind: "copy"; operand: Expression }
|
|
109
|
+
| { kind: "record"; fields: Readonly<Record<string, Expression>> }
|
|
110
|
+
| { kind: "array"; items: readonly Expression[] }
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
export type Statement = { span: SourceSpan } & (
|
|
114
|
+
| { kind: "assign"; destination: Place; value: Expression }
|
|
115
|
+
| {
|
|
116
|
+
kind: "if";
|
|
117
|
+
condition: Expression;
|
|
118
|
+
then: readonly Statement[];
|
|
119
|
+
else: readonly Statement[];
|
|
120
|
+
}
|
|
121
|
+
| {
|
|
122
|
+
kind: "switch";
|
|
123
|
+
value: Expression;
|
|
124
|
+
cases: readonly {
|
|
125
|
+
values: readonly Constant[];
|
|
126
|
+
body: readonly Statement[];
|
|
127
|
+
}[];
|
|
128
|
+
fallback: readonly Statement[];
|
|
129
|
+
}
|
|
130
|
+
| {
|
|
131
|
+
kind: "for";
|
|
132
|
+
counter: SymbolId;
|
|
133
|
+
/** Existing storage remains observable after the loop exits. */
|
|
134
|
+
counterScope?: "existing";
|
|
135
|
+
start: Expression;
|
|
136
|
+
limit: Expression;
|
|
137
|
+
step: string;
|
|
138
|
+
exclusive?: true;
|
|
139
|
+
maxIterations: number;
|
|
140
|
+
body: readonly Statement[];
|
|
141
|
+
}
|
|
142
|
+
| {
|
|
143
|
+
kind: "invoke";
|
|
144
|
+
function: SymbolId;
|
|
145
|
+
instance?: SymbolId;
|
|
146
|
+
index?: Expression;
|
|
147
|
+
arguments: readonly Expression[];
|
|
148
|
+
destination?: Place;
|
|
149
|
+
}
|
|
150
|
+
| {
|
|
151
|
+
kind: "blockUpdate";
|
|
152
|
+
instance: SymbolId;
|
|
153
|
+
index?: Expression;
|
|
154
|
+
inputs: Readonly<Record<string, Expression>>;
|
|
155
|
+
}
|
|
156
|
+
| { kind: "return"; value?: Expression }
|
|
157
|
+
| { kind: "break" | "continue" }
|
|
158
|
+
| ({ kind: "telemetry" } & TelemetryCall<Expression>)
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* A `@tsplc/compiler/telemetry` call. `record` is what the record says besides its properties;
|
|
163
|
+
* a property with a `length` is a byte array, borrowed, sent as text up to that length.
|
|
164
|
+
*/
|
|
165
|
+
export interface TelemetryCall<E> {
|
|
166
|
+
record:
|
|
167
|
+
| { kind: "event"; event: string }
|
|
168
|
+
| { kind: "log"; level: "info" | "warn" | "error"; message: string }
|
|
169
|
+
| { kind: "exception"; code: E };
|
|
170
|
+
properties: readonly { name: string; value: E; length?: E }[];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Every value a telemetry call reads, in evaluation order. */
|
|
174
|
+
export const telemetryOperands = <E>({
|
|
175
|
+
record,
|
|
176
|
+
properties,
|
|
177
|
+
}: TelemetryCall<E>): E[] => [
|
|
178
|
+
...(record.kind === "exception" ? [record.code] : []),
|
|
179
|
+
...properties.flatMap(({ value, length }) =>
|
|
180
|
+
length ? [value, length] : [value],
|
|
181
|
+
),
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
export interface MethodOwner {
|
|
185
|
+
block: SymbolId;
|
|
186
|
+
access: "public" | "private" | "protected";
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface PlcFunction {
|
|
190
|
+
owner?: MethodOwner;
|
|
191
|
+
id: SymbolId;
|
|
192
|
+
name: string;
|
|
193
|
+
parameters: readonly Variable[];
|
|
194
|
+
locals: readonly Variable[];
|
|
195
|
+
result?: TypeId;
|
|
196
|
+
/** A `@tsplc/compiler/math` helper, whose result range follows from its arguments' ranges. */
|
|
197
|
+
bounds?: MathHelper;
|
|
198
|
+
/** The source method this function was specialized from, as `Class.method`, and the TypeScript path of its `this`. */
|
|
199
|
+
origin?: { name: string; instance?: string };
|
|
200
|
+
body: readonly Statement[];
|
|
201
|
+
span: SourceSpan;
|
|
202
|
+
}
|
|
203
|
+
export type MathHelper = "min" | "max" | "clamp";
|
|
204
|
+
|
|
205
|
+
// Native vendor blocks under their TypeScript names. Ports are [name, native name, type];
|
|
206
|
+
// every output starts at its type's default, matching the vendor library blocks.
|
|
207
|
+
const timer = (input: string, output: string) =>
|
|
208
|
+
({
|
|
209
|
+
inputs: [
|
|
210
|
+
[input, "IN", "BOOL"],
|
|
211
|
+
["duration", "PT", "TIME"],
|
|
212
|
+
],
|
|
213
|
+
outputs: [
|
|
214
|
+
[output, "Q", "BOOL"],
|
|
215
|
+
["elapsed", "ET", "TIME"],
|
|
216
|
+
],
|
|
217
|
+
}) as const;
|
|
218
|
+
export const standardBlocks = {
|
|
219
|
+
OnDelayTimer: { native: "TON", ...timer("enabled", "done") },
|
|
220
|
+
OffDelayTimer: { native: "TOF", ...timer("enabled", "active") },
|
|
221
|
+
PulseTimer: { native: "TP", ...timer("start", "active") },
|
|
222
|
+
RisingEdge: {
|
|
223
|
+
native: "R_TRIG",
|
|
224
|
+
inputs: [["signal", "CLK", "BOOL"]],
|
|
225
|
+
outputs: [["rose", "Q", "BOOL"]],
|
|
226
|
+
},
|
|
227
|
+
FallingEdge: {
|
|
228
|
+
native: "F_TRIG",
|
|
229
|
+
inputs: [["signal", "CLK", "BOOL"]],
|
|
230
|
+
outputs: [["fell", "Q", "BOOL"]],
|
|
231
|
+
},
|
|
232
|
+
UpCounter: {
|
|
233
|
+
native: "CTU",
|
|
234
|
+
inputs: [
|
|
235
|
+
["countUp", "CU", "BOOL"],
|
|
236
|
+
["reset", "RESET", "BOOL"],
|
|
237
|
+
["preset", "PV", "WORD"],
|
|
238
|
+
],
|
|
239
|
+
outputs: [
|
|
240
|
+
["reachedPreset", "Q", "BOOL"],
|
|
241
|
+
["value", "CV", "WORD"],
|
|
242
|
+
],
|
|
243
|
+
},
|
|
244
|
+
DownCounter: {
|
|
245
|
+
native: "CTD",
|
|
246
|
+
inputs: [
|
|
247
|
+
["countDown", "CD", "BOOL"],
|
|
248
|
+
["load", "LOAD", "BOOL"],
|
|
249
|
+
["preset", "PV", "WORD"],
|
|
250
|
+
],
|
|
251
|
+
outputs: [
|
|
252
|
+
["atZero", "Q", "BOOL"],
|
|
253
|
+
["value", "CV", "WORD"],
|
|
254
|
+
],
|
|
255
|
+
},
|
|
256
|
+
UpDownCounter: {
|
|
257
|
+
native: "CTUD",
|
|
258
|
+
inputs: [
|
|
259
|
+
["countUp", "CU", "BOOL"],
|
|
260
|
+
["countDown", "CD", "BOOL"],
|
|
261
|
+
["reset", "RESET", "BOOL"],
|
|
262
|
+
["load", "LOAD", "BOOL"],
|
|
263
|
+
["preset", "PV", "WORD"],
|
|
264
|
+
],
|
|
265
|
+
outputs: [
|
|
266
|
+
["reachedPreset", "QU", "BOOL"],
|
|
267
|
+
["atZero", "QD", "BOOL"],
|
|
268
|
+
["value", "CV", "WORD"],
|
|
269
|
+
],
|
|
270
|
+
},
|
|
271
|
+
Blinker: {
|
|
272
|
+
native: "Util.BLINK",
|
|
273
|
+
inputs: [
|
|
274
|
+
["enabled", "ENABLE", "BOOL"],
|
|
275
|
+
["offTime", "TIMELOW", "TIME"],
|
|
276
|
+
["onTime", "TIMEHIGH", "TIME"],
|
|
277
|
+
],
|
|
278
|
+
outputs: [["on", "OUT", "BOOL"]],
|
|
279
|
+
},
|
|
280
|
+
} as const;
|
|
281
|
+
export type StandardBlock = keyof typeof standardBlocks;
|
|
282
|
+
|
|
283
|
+
export interface FunctionBlock {
|
|
284
|
+
id: SymbolId;
|
|
285
|
+
name: string;
|
|
286
|
+
inputs: readonly Variable[];
|
|
287
|
+
outputs: readonly Variable[];
|
|
288
|
+
state: readonly Variable[];
|
|
289
|
+
locals: readonly Variable[];
|
|
290
|
+
body: readonly Statement[];
|
|
291
|
+
result?: TypeId;
|
|
292
|
+
standard?: StandardBlock;
|
|
293
|
+
runtime?:
|
|
294
|
+
| { kind: "mailbox" }
|
|
295
|
+
| { kind: "backgroundHost" }
|
|
296
|
+
| { kind: "rpc"; path: string }
|
|
297
|
+
| {
|
|
298
|
+
kind: "http";
|
|
299
|
+
url: string;
|
|
300
|
+
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
301
|
+
json: boolean;
|
|
302
|
+
};
|
|
303
|
+
/** The async function lowered to this block, and the source each `step` value stands for. */
|
|
304
|
+
job?: {
|
|
305
|
+
name: string;
|
|
306
|
+
/** The TypeScript path of `this`, for a method specialized to one instance. */
|
|
307
|
+
instance?: string;
|
|
308
|
+
span: SourceSpan;
|
|
309
|
+
steps: readonly {
|
|
310
|
+
step: number;
|
|
311
|
+
kind: "await" | "return" | "throw" | "statement";
|
|
312
|
+
span: SourceSpan;
|
|
313
|
+
text: string;
|
|
314
|
+
}[];
|
|
315
|
+
};
|
|
316
|
+
span: SourceSpan;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export interface PlcProgram {
|
|
320
|
+
id: SymbolId;
|
|
321
|
+
name: string;
|
|
322
|
+
entry: SymbolId;
|
|
323
|
+
continuation?: { instance: string; file: string };
|
|
324
|
+
background?: {
|
|
325
|
+
intervalNanoseconds: string;
|
|
326
|
+
priority: number;
|
|
327
|
+
mailbox: string;
|
|
328
|
+
worker: string;
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export interface PlcModule {
|
|
333
|
+
/**
|
|
334
|
+
* Fields the elaborator folded into constants: no storage, but a known value at each TypeScript
|
|
335
|
+
* path. `type` is the scalar's PLC type; strings, often enum members, have none.
|
|
336
|
+
*/
|
|
337
|
+
folded?: readonly {
|
|
338
|
+
path: string;
|
|
339
|
+
value: Constant;
|
|
340
|
+
type?: TypeId;
|
|
341
|
+
span: SourceSpan;
|
|
342
|
+
}[];
|
|
343
|
+
types: readonly PlcType[];
|
|
344
|
+
variables: readonly Variable[];
|
|
345
|
+
functions: readonly PlcFunction[];
|
|
346
|
+
blocks: readonly FunctionBlock[];
|
|
347
|
+
programs: readonly PlcProgram[];
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export interface Task {
|
|
351
|
+
name: string;
|
|
352
|
+
intervalNanoseconds: string;
|
|
353
|
+
priority: number;
|
|
354
|
+
programs: readonly string[];
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export interface Deployment {
|
|
358
|
+
tasks: readonly Task[];
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export interface Artifact {
|
|
362
|
+
path: string;
|
|
363
|
+
content: string;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export interface GeneratedObject {
|
|
367
|
+
parent?: SymbolId;
|
|
368
|
+
sourcePath?: string;
|
|
369
|
+
id: SymbolId;
|
|
370
|
+
kind:
|
|
371
|
+
| "program"
|
|
372
|
+
| "function"
|
|
373
|
+
| "functionBlock"
|
|
374
|
+
| "method"
|
|
375
|
+
| "globalList"
|
|
376
|
+
| "dataType";
|
|
377
|
+
name: string;
|
|
378
|
+
declaration: string;
|
|
379
|
+
implementation?: string;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export interface EmitResult {
|
|
383
|
+
artifacts: readonly Artifact[];
|
|
384
|
+
objects: readonly GeneratedObject[];
|
|
385
|
+
diagnostics: readonly Diagnostic[];
|
|
386
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { error, type Diagnostic, type SourceSpan } from "./diagnostics.ts";
|
|
2
2
|
|
|
3
3
|
export class InvalidCore extends Error {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
constructor(readonly diagnostic: Diagnostic) {
|
|
5
|
+
super(diagnostic.message);
|
|
6
|
+
}
|
|
7
7
|
}
|
|
8
8
|
export function reject(message: string, span?: SourceSpan): never {
|
|
9
|
-
|
|
9
|
+
throw new InvalidCore(error("PLC6001", message, span));
|
|
10
10
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Diagnostic } from "./diagnostics.ts";
|
|
2
|
+
import type { PlcModule } from "./model.ts";
|
|
3
|
+
import {
|
|
4
|
+
verified,
|
|
5
|
+
type VerifiedCoreModule,
|
|
6
|
+
type CoreFunction,
|
|
7
|
+
type CoreFunctionBlock,
|
|
8
|
+
} from "./core-model.ts";
|
|
9
|
+
import { ModuleDeclarations } from "./declarations.ts";
|
|
10
|
+
import { checkStaticAllocation } from "./allocation.ts";
|
|
11
|
+
import { RoutineNormalizer } from "./routine.ts";
|
|
12
|
+
import { linkModule } from "./link.ts";
|
|
13
|
+
import { InvalidCore } from "./normalization-error.ts";
|
|
14
|
+
|
|
15
|
+
const verifiedModules = new WeakSet<object>();
|
|
16
|
+
export function isVerifiedCore(value: unknown): value is VerifiedCoreModule {
|
|
17
|
+
return (
|
|
18
|
+
typeof value === "object" && value !== null && verifiedModules.has(value)
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function freeze(value: unknown): void {
|
|
23
|
+
if (typeof value !== "object" || value === null || Object.isFrozen(value)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
for (const child of Object.values(value)) {
|
|
27
|
+
freeze(child);
|
|
28
|
+
}
|
|
29
|
+
Object.freeze(value);
|
|
30
|
+
}
|
|
31
|
+
// The module crosses the trust boundary only after admission, lowering and linking.
|
|
32
|
+
export function normalizeModule(module: PlcModule): {
|
|
33
|
+
core?: VerifiedCoreModule;
|
|
34
|
+
diagnostics: readonly Diagnostic[];
|
|
35
|
+
} {
|
|
36
|
+
try {
|
|
37
|
+
const declarations = new ModuleDeclarations(module);
|
|
38
|
+
checkStaticAllocation(module, declarations);
|
|
39
|
+
|
|
40
|
+
const functions: CoreFunction[] = [];
|
|
41
|
+
const blocks: CoreFunctionBlock[] = [];
|
|
42
|
+
const edges = new Map<string, ReadonlySet<string>>();
|
|
43
|
+
for (const source of [...module.functions, ...module.blocks]) {
|
|
44
|
+
const normalized = new RoutineNormalizer(
|
|
45
|
+
source,
|
|
46
|
+
declarations,
|
|
47
|
+
).normalize();
|
|
48
|
+
edges.set(source.id, normalized.callees);
|
|
49
|
+
if ("inputs" in normalized.routine) {
|
|
50
|
+
blocks.push(normalized.routine);
|
|
51
|
+
} else {
|
|
52
|
+
functions.push(normalized.routine);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const linked = linkModule(module, declarations, functions, blocks, edges);
|
|
57
|
+
const core: VerifiedCoreModule = {
|
|
58
|
+
...structuredClone(linked),
|
|
59
|
+
[verified]: true,
|
|
60
|
+
};
|
|
61
|
+
freeze(core);
|
|
62
|
+
verifiedModules.add(core);
|
|
63
|
+
return { core, diagnostics: [] };
|
|
64
|
+
} catch (cause) {
|
|
65
|
+
if (cause instanceof InvalidCore) {
|
|
66
|
+
return { diagnostics: [cause.diagnostic] };
|
|
67
|
+
}
|
|
68
|
+
throw cause;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type { Constant } from "./model.ts";
|
|
2
|
+
import { integerRange, type PlcType } from "./types.ts";
|
|
3
|
+
|
|
4
|
+
type IntegerType = Extract<PlcType, { kind: "integer" | "bits" }>;
|
|
5
|
+
type FloatType = Extract<PlcType, { kind: "float" }>;
|
|
6
|
+
|
|
7
|
+
export type IntegerInterval = readonly [bigint, bigint];
|
|
8
|
+
|
|
9
|
+
export const lowest = (a: bigint, b: bigint): bigint => (a < b ? a : b);
|
|
10
|
+
export const highest = (a: bigint, b: bigint): bigint => (a > b ? a : b);
|
|
11
|
+
|
|
12
|
+
/** Whether every value of `inner` lies in `outer`. */
|
|
13
|
+
export const within = (
|
|
14
|
+
inner: IntegerInterval,
|
|
15
|
+
outer: IntegerInterval,
|
|
16
|
+
): boolean => inner[0] >= outer[0] && inner[1] <= outer[1];
|
|
17
|
+
|
|
18
|
+
/** Whether `interval` admits `value`, such as a divisor that may be zero. */
|
|
19
|
+
export const admits = (interval: IntegerInterval, value: bigint): boolean =>
|
|
20
|
+
interval[0] <= value && value <= interval[1];
|
|
21
|
+
|
|
22
|
+
/** Integer constants are canonical decimal text: no leading zeros and no negative zero. */
|
|
23
|
+
export const canonicalInteger = /^(?:0|-?[1-9][0-9]*)$/;
|
|
24
|
+
|
|
25
|
+
export const integerType = (type: PlcType): type is IntegerType =>
|
|
26
|
+
type.kind === "integer" || type.kind === "bits";
|
|
27
|
+
|
|
28
|
+
// IEEE 754 binary32 (REAL) and binary64 (LREAL): the largest finite value, and the significand
|
|
29
|
+
// precision, which bounds the integers the format holds exactly.
|
|
30
|
+
const floatFormats = {
|
|
31
|
+
32: { maximum: Math.fround((2 - 2 ** -23) * 2 ** 127), precision: 24 },
|
|
32
|
+
64: { maximum: Number.MAX_VALUE, precision: 53 },
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
export const floatMaximum = (type: FloatType): number =>
|
|
36
|
+
floatFormats[type.bits].maximum;
|
|
37
|
+
|
|
38
|
+
/** Every integer from minus this limit to this limit converts to the float type exactly. */
|
|
39
|
+
export const exactIntegerLimit = (type: FloatType): bigint =>
|
|
40
|
+
1n << BigInt(floatFormats[type.bits].precision);
|
|
41
|
+
|
|
42
|
+
/** The nearest value of the float type, as each typed REAL result rounds. */
|
|
43
|
+
export const roundFloat = (value: number, type: FloatType): number =>
|
|
44
|
+
type.bits === 32 ? Math.fround(value) : value;
|
|
45
|
+
|
|
46
|
+
export const saturateFloat = (value: number, type: FloatType): number => {
|
|
47
|
+
// Clamp before target rounding so a finite source cannot round to infinity.
|
|
48
|
+
// Comparisons leave signed zero intact; min/max substitution is unnecessary.
|
|
49
|
+
const maximum = floatMaximum(type);
|
|
50
|
+
if (value < -maximum) {
|
|
51
|
+
return -maximum;
|
|
52
|
+
}
|
|
53
|
+
if (value > maximum) {
|
|
54
|
+
return maximum;
|
|
55
|
+
}
|
|
56
|
+
return roundFloat(value, type);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
type DecimalIssue = "malformed" | "exponent" | "fraction";
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A signed decimal with an optional fraction and exponent, times `scale`, exactly; it must come
|
|
63
|
+
* to a whole number. The exponent is capped so a literal cannot demand a vast power of ten.
|
|
64
|
+
*/
|
|
65
|
+
export const exactDecimal = (
|
|
66
|
+
text: string,
|
|
67
|
+
scale: bigint,
|
|
68
|
+
): bigint | DecimalIssue => {
|
|
69
|
+
const match = /^(-?\d+)(?:\.(\d*))?(?:e([+-]?\d+))?$/i.exec(text);
|
|
70
|
+
if (!match) {
|
|
71
|
+
return "malformed";
|
|
72
|
+
}
|
|
73
|
+
const [, whole, fraction = "", exponentText = "0"] = match;
|
|
74
|
+
const exponent = Number(exponentText) - fraction.length;
|
|
75
|
+
if (!Number.isSafeInteger(exponent) || Math.abs(exponent) > 400) {
|
|
76
|
+
return "exponent";
|
|
77
|
+
}
|
|
78
|
+
// The pattern requires the whole digits, so the group always matched.
|
|
79
|
+
const digits = BigInt(whole! + fraction) * scale;
|
|
80
|
+
if (exponent >= 0) {
|
|
81
|
+
return digits * 10n ** BigInt(exponent);
|
|
82
|
+
}
|
|
83
|
+
const divisor = 10n ** BigInt(-exponent);
|
|
84
|
+
return digits % divisor ? "fraction" : digits / divisor;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const integerLiteralIssues: Record<DecimalIssue, string> = {
|
|
88
|
+
malformed: "Expected an exact integer literal.",
|
|
89
|
+
exponent: "Integer literal exponent is out of range.",
|
|
90
|
+
fraction: "Integer literals cannot have a fractional part.",
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const parseInteger = (text: string): bigint => {
|
|
94
|
+
const clean = text.replaceAll("_", "").replace(/n$/, "");
|
|
95
|
+
if (/^-?0[xob]/i.test(clean)) {
|
|
96
|
+
return clean.startsWith("-") ? -BigInt(clean.slice(1)) : BigInt(clean);
|
|
97
|
+
}
|
|
98
|
+
const value = exactDecimal(clean, 1n);
|
|
99
|
+
if (typeof value !== "bigint") {
|
|
100
|
+
throw new Error(integerLiteralIssues[value]);
|
|
101
|
+
}
|
|
102
|
+
return value;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const constantFits = (value: Constant, type: PlcType): boolean => {
|
|
106
|
+
if (type.kind === "bool") {
|
|
107
|
+
return value.kind === "bool" && typeof value.value === "boolean";
|
|
108
|
+
}
|
|
109
|
+
if (integerType(type)) {
|
|
110
|
+
return (
|
|
111
|
+
value.kind === "integer" &&
|
|
112
|
+
canonicalInteger.test(value.value) &&
|
|
113
|
+
admits(integerRange(type), BigInt(value.value))
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
if (type.kind === "float") {
|
|
117
|
+
return (
|
|
118
|
+
value.kind === "float" &&
|
|
119
|
+
Number.isFinite(value.value) &&
|
|
120
|
+
Object.is(roundFloat(value.value, type), value.value)
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export const canAssign = (
|
|
127
|
+
source: PlcType,
|
|
128
|
+
destination: PlcType,
|
|
129
|
+
value?: Constant,
|
|
130
|
+
): boolean => {
|
|
131
|
+
if (value) {
|
|
132
|
+
return source.kind === destination.kind && constantFits(value, destination);
|
|
133
|
+
}
|
|
134
|
+
if (source.id === destination.id) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
if (source.kind === "integer" && destination.kind === "integer") {
|
|
138
|
+
return within(integerRange(source), integerRange(destination));
|
|
139
|
+
}
|
|
140
|
+
return (
|
|
141
|
+
source.kind === "float" &&
|
|
142
|
+
destination.kind === "float" &&
|
|
143
|
+
source.bits <= destination.bits
|
|
144
|
+
);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/** Plain integer +, - and * are native IEC arithmetic, which wraps on overflow as hand-written ST does. */
|
|
148
|
+
export const wrapsNatively = (expression: {
|
|
149
|
+
kind: string;
|
|
150
|
+
operator?: string;
|
|
151
|
+
policy?: string;
|
|
152
|
+
}): boolean =>
|
|
153
|
+
expression.kind === "binary" &&
|
|
154
|
+
!expression.policy &&
|
|
155
|
+
(expression.operator === "add" ||
|
|
156
|
+
expression.operator === "subtract" ||
|
|
157
|
+
expression.operator === "multiply");
|
|
158
|
+
|
|
159
|
+
export const applyIntegerPolicy = (
|
|
160
|
+
value: bigint,
|
|
161
|
+
type: IntegerType,
|
|
162
|
+
policy: "wrap" | "saturate",
|
|
163
|
+
): bigint => {
|
|
164
|
+
if (policy === "saturate") {
|
|
165
|
+
const [minimum, maximum] = integerRange(type);
|
|
166
|
+
return highest(minimum, lowest(value, maximum));
|
|
167
|
+
}
|
|
168
|
+
return type.kind === "integer" && type.signed
|
|
169
|
+
? BigInt.asIntN(type.bits, value)
|
|
170
|
+
: BigInt.asUintN(type.bits, value);
|
|
171
|
+
};
|