@tsplc/compiler 0.5.13 → 0.5.15
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 +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
|
|
2
|
-
|
|
3
1
|
import {
|
|
4
2
|
aggregate,
|
|
5
3
|
type Constant,
|
|
@@ -17,14 +15,14 @@ import {
|
|
|
17
15
|
type Place,
|
|
18
16
|
type PlcFunction,
|
|
19
17
|
type PlcType,
|
|
18
|
+
parseFloatLiteral,
|
|
20
19
|
parseInteger,
|
|
21
20
|
placeType,
|
|
22
21
|
type SourceSpan,
|
|
23
22
|
type Statement,
|
|
24
23
|
type Variable,
|
|
25
|
-
valueRange,
|
|
26
24
|
} from "@tsplc/compiler/ir";
|
|
27
|
-
import {
|
|
25
|
+
import type { BinaryOperator } from "@tsplc/iec/arithmetic";
|
|
28
26
|
import ts from "typescript";
|
|
29
27
|
import type { BlockResolver, BlockSource } from "./blocks";
|
|
30
28
|
import { intrinsicExpression } from "./intrinsics";
|
|
@@ -89,7 +87,7 @@ export interface LoweringScope {
|
|
|
89
87
|
block: BlockSource | undefined;
|
|
90
88
|
environment: TypeEnvironment;
|
|
91
89
|
/** Module storage, with the routine's parameters or block fields over it. */
|
|
92
|
-
variables:
|
|
90
|
+
variables: ReadonlyMap<ts.Symbol, Variable>;
|
|
93
91
|
}
|
|
94
92
|
|
|
95
93
|
/** A call to a helper or block method, with its instance receiver if it has one. */
|
|
@@ -119,6 +117,8 @@ export class ExpressionLowering {
|
|
|
119
117
|
private readonly loopCounters = new Set<string>();
|
|
120
118
|
private readonly aliases = new Map<ts.Symbol, Place>();
|
|
121
119
|
private readonly copySources: Place[] = [];
|
|
120
|
+
/** `byId()`, until the next binding. */
|
|
121
|
+
private storage: ReadonlyMap<string, Variable> | undefined;
|
|
122
122
|
|
|
123
123
|
constructor(context: AnalysisContext, scope: LoweringScope) {
|
|
124
124
|
this.checker = context.checker;
|
|
@@ -127,15 +127,16 @@ export class ExpressionLowering {
|
|
|
127
127
|
this.routines = context.routines;
|
|
128
128
|
this.positions = context.positions;
|
|
129
129
|
this.fail = context.fail;
|
|
130
|
-
this.variables = scope.variables;
|
|
130
|
+
this.variables = new Map(scope.variables);
|
|
131
131
|
this.environment = scope.environment;
|
|
132
132
|
this.activeBlock = scope.block;
|
|
133
133
|
this.activeFunction = scope.fn;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
/** Brings a local, loop counter or loop key into scope. */
|
|
136
|
+
/** Brings a global, parameter, block field, local, loop counter or loop key into scope. */
|
|
137
137
|
bind(symbol: ts.Symbol, variable: Variable): void {
|
|
138
138
|
this.variables.set(symbol, variable);
|
|
139
|
+
this.storage = undefined;
|
|
139
140
|
}
|
|
140
141
|
|
|
141
142
|
/** Brings a const alias into scope; each use re-reads `place`. */
|
|
@@ -145,13 +146,7 @@ export class ExpressionLowering {
|
|
|
145
146
|
|
|
146
147
|
/** A const is assigned once, so its initializer's range holds wherever it is read. */
|
|
147
148
|
holdRange(id: string, value: Expression): void {
|
|
148
|
-
const bound =
|
|
149
|
-
value,
|
|
150
|
-
this.resolver.types,
|
|
151
|
-
this.byId(),
|
|
152
|
-
this.ranges,
|
|
153
|
-
this.routines.instances,
|
|
154
|
-
);
|
|
149
|
+
const bound = this.interval(value);
|
|
155
150
|
if (bound) {
|
|
156
151
|
this.ranges.set(id, bound);
|
|
157
152
|
}
|
|
@@ -228,9 +223,14 @@ export class ExpressionLowering {
|
|
|
228
223
|
return `${this.resolver.id(node, name)}${this.activeFunction ? `@${this.activeFunction.id}` : ""}`;
|
|
229
224
|
}
|
|
230
225
|
|
|
226
|
+
/** The scope's storage by symbol. */
|
|
227
|
+
bySymbol(): ReadonlyMap<ts.Symbol, Variable> {
|
|
228
|
+
return this.variables;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
231
|
/** The scope's storage by id, including the enclosing block's inputs and outputs. */
|
|
232
|
-
byId():
|
|
233
|
-
|
|
232
|
+
byId(): ReadonlyMap<string, Variable> {
|
|
233
|
+
this.storage ??= new Map(
|
|
234
234
|
[
|
|
235
235
|
...this.variables.values(),
|
|
236
236
|
...(this.activeBlock
|
|
@@ -241,6 +241,7 @@ export class ExpressionLowering {
|
|
|
241
241
|
: []),
|
|
242
242
|
].map((variable) => [variable.id, variable]),
|
|
243
243
|
);
|
|
244
|
+
return this.storage;
|
|
244
245
|
}
|
|
245
246
|
|
|
246
247
|
/** Rejects a lowered expression the model cannot hold, at the expression's own span. */
|
|
@@ -279,28 +280,12 @@ export class ExpressionLowering {
|
|
|
279
280
|
);
|
|
280
281
|
}
|
|
281
282
|
const constant = fold(value, this.byId(), this.resolver.types);
|
|
282
|
-
|
|
283
|
+
const widerString =
|
|
283
284
|
source.kind === "string" &&
|
|
284
285
|
target.kind === "string" &&
|
|
285
286
|
source.encoding === target.encoding &&
|
|
286
|
-
source.capacity <= target.capacity
|
|
287
|
-
) {
|
|
288
|
-
return constant
|
|
289
|
-
? {
|
|
290
|
-
kind: "constant",
|
|
291
|
-
type: destination,
|
|
292
|
-
value: constant,
|
|
293
|
-
span: value.span,
|
|
294
|
-
}
|
|
295
|
-
: {
|
|
296
|
-
kind: "convert",
|
|
297
|
-
type: destination,
|
|
298
|
-
operand: value,
|
|
299
|
-
policy: "exact",
|
|
300
|
-
span: value.span,
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
if (!canAssign(source, target, constant)) {
|
|
287
|
+
source.capacity <= target.capacity;
|
|
288
|
+
if (!widerString && !canAssign(source, target, constant)) {
|
|
304
289
|
this.reject(
|
|
305
290
|
value.span,
|
|
306
291
|
"PLC2002",
|
|
@@ -315,13 +300,14 @@ export class ExpressionLowering {
|
|
|
315
300
|
span: value.span,
|
|
316
301
|
};
|
|
317
302
|
}
|
|
318
|
-
|
|
303
|
+
const converted: Expression = {
|
|
319
304
|
kind: "convert",
|
|
320
305
|
type: destination,
|
|
321
306
|
operand: value,
|
|
322
307
|
policy: "exact",
|
|
323
308
|
span: value.span,
|
|
324
|
-
}
|
|
309
|
+
};
|
|
310
|
+
return widerString ? converted : this.checked(converted);
|
|
325
311
|
}
|
|
326
312
|
|
|
327
313
|
constant(expression: Expression): Constant | undefined {
|
|
@@ -446,7 +432,7 @@ export class ExpressionLowering {
|
|
|
446
432
|
type,
|
|
447
433
|
value:
|
|
448
434
|
target.kind === "float"
|
|
449
|
-
?
|
|
435
|
+
? { kind: "float", value: parseFloatLiteral(text, target) }
|
|
450
436
|
: this.integerConstant(node, text),
|
|
451
437
|
span: this.positions.node(node),
|
|
452
438
|
});
|
|
@@ -551,39 +537,21 @@ export class ExpressionLowering {
|
|
|
551
537
|
type: RecordLayout,
|
|
552
538
|
): Expression {
|
|
553
539
|
const span = this.positions.node(node);
|
|
554
|
-
const fields
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
))
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
}
|
|
566
|
-
continue;
|
|
567
|
-
}
|
|
568
|
-
const named = namedProperty(property);
|
|
569
|
-
if (!named) {
|
|
570
|
-
this.fail(
|
|
571
|
-
property,
|
|
572
|
-
"PLC1001",
|
|
540
|
+
const fields = this.namedFields(node, type.fields, {
|
|
541
|
+
spread: (_, value) => {
|
|
542
|
+
// TypeScript's spread shares a nested record or array, so a later write to it diverges.
|
|
543
|
+
this.recordCopySource(value);
|
|
544
|
+
return this.implicitCopy(value);
|
|
545
|
+
},
|
|
546
|
+
named: (field, value) =>
|
|
547
|
+
this.implicitCopy(this.expression(value, field.type)),
|
|
548
|
+
unnamed: {
|
|
549
|
+
code: "PLC1001",
|
|
550
|
+
message:
|
|
573
551
|
"Record literals take named fields after at most one leading spread; methods are unsupported.",
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
if (!field || written.has(field.name)) {
|
|
578
|
-
this.fail(property, "PLC2001", "Unknown or duplicate record field.");
|
|
579
|
-
}
|
|
580
|
-
written.add(field.name);
|
|
581
|
-
// An explicit field overrides the spread and keeps its source evaluation order.
|
|
582
|
-
delete fields[field.name];
|
|
583
|
-
fields[field.name] = this.implicitCopy(
|
|
584
|
-
this.expression(named.value, field.type),
|
|
585
|
-
);
|
|
586
|
-
}
|
|
552
|
+
},
|
|
553
|
+
unknown: "Unknown or duplicate record field.",
|
|
554
|
+
});
|
|
587
555
|
if (this.resolver.unions.has(type.id)) {
|
|
588
556
|
for (const field of type.fields) {
|
|
589
557
|
if (!Object.hasOwn(fields, field.name)) {
|
|
@@ -643,16 +611,17 @@ export class ExpressionLowering {
|
|
|
643
611
|
if (layout?.kind !== "enum") {
|
|
644
612
|
this.fail(node, "PLC2001", "Invalid enum layout.");
|
|
645
613
|
}
|
|
614
|
+
const member = layout.members.find(
|
|
615
|
+
(member) => member.name === declaration.name.getText(),
|
|
616
|
+
);
|
|
617
|
+
if (!member) {
|
|
618
|
+
this.fail(node, "PLC2001", "Invalid enum layout.");
|
|
619
|
+
}
|
|
646
620
|
return {
|
|
647
621
|
span: this.positions.node(node),
|
|
648
622
|
type,
|
|
649
623
|
kind: "constant",
|
|
650
|
-
value: {
|
|
651
|
-
kind: "integer",
|
|
652
|
-
value: layout.members.find(
|
|
653
|
-
(member) => member.name === declaration.name.getText(),
|
|
654
|
-
)!.value,
|
|
655
|
-
},
|
|
624
|
+
value: { kind: "integer", value: member.value },
|
|
656
625
|
};
|
|
657
626
|
}
|
|
658
627
|
|
|
@@ -771,13 +740,16 @@ export class ExpressionLowering {
|
|
|
771
740
|
}
|
|
772
741
|
const logical = operator === "and" || operator === "or";
|
|
773
742
|
const comparison = comparisonOperators.has(operator);
|
|
774
|
-
const left = this.leftOperand(node, operator, expected);
|
|
775
|
-
const
|
|
776
|
-
node.right,
|
|
743
|
+
const { left, right: lowered } = this.leftOperand(node, operator, expected);
|
|
744
|
+
const rightType =
|
|
777
745
|
operator === "shiftLeft" || operator === "shiftRight"
|
|
778
746
|
? "DINT"
|
|
779
|
-
: left.type
|
|
780
|
-
|
|
747
|
+
: left.type;
|
|
748
|
+
// Lowered in the type it already has, an operand lowers the same again.
|
|
749
|
+
const right =
|
|
750
|
+
lowered?.type === rightType
|
|
751
|
+
? lowered
|
|
752
|
+
: this.expression(node.right, rightType);
|
|
781
753
|
if (
|
|
782
754
|
operator === "divide" &&
|
|
783
755
|
this.resolver.types.get(left.type)?.kind !== "float"
|
|
@@ -801,33 +773,39 @@ export class ExpressionLowering {
|
|
|
801
773
|
/**
|
|
802
774
|
* The left operand in the type the operation works in: BOOL for logic, the right
|
|
803
775
|
* operand's type when a literal is compared to it, and the wider side of any other
|
|
804
|
-
* comparison. The right operand then takes the left's type
|
|
776
|
+
* comparison. The right operand then takes the left's type; `right` is how it lowered
|
|
777
|
+
* on its own, when its type decided the left's.
|
|
805
778
|
*/
|
|
806
779
|
private leftOperand(
|
|
807
780
|
node: ts.BinaryExpression,
|
|
808
781
|
operator: BinaryOperator,
|
|
809
782
|
expected: string | undefined,
|
|
810
|
-
): Expression {
|
|
783
|
+
): { left: Expression; right?: Expression } {
|
|
811
784
|
if (operator === "and" || operator === "or") {
|
|
812
|
-
return this.expression(node.left, "BOOL");
|
|
785
|
+
return { left: this.expression(node.left, "BOOL") };
|
|
813
786
|
}
|
|
814
787
|
if (!comparisonOperators.has(operator)) {
|
|
815
|
-
return this.expression(node.left, expected);
|
|
788
|
+
return { left: this.expression(node.left, expected) };
|
|
816
789
|
}
|
|
817
790
|
if (contextualLiteral(node.left)) {
|
|
818
|
-
|
|
791
|
+
const right = this.expression(node.right);
|
|
792
|
+
return { left: this.expression(node.left, right.type), right };
|
|
819
793
|
}
|
|
820
794
|
const left = this.expression(node.left);
|
|
821
795
|
if (contextualLiteral(node.right)) {
|
|
822
|
-
return left;
|
|
796
|
+
return { left };
|
|
823
797
|
}
|
|
824
798
|
// Compare at the wider type; widening cannot change the value.
|
|
825
|
-
const
|
|
799
|
+
const right = this.expression(node.right);
|
|
826
800
|
const from = this.resolver.types.get(left.type);
|
|
827
|
-
const to = this.resolver.types.get(
|
|
828
|
-
return
|
|
829
|
-
|
|
830
|
-
|
|
801
|
+
const to = this.resolver.types.get(right.type);
|
|
802
|
+
return {
|
|
803
|
+
left:
|
|
804
|
+
from && to && !canAssign(to, from) && canAssign(from, to)
|
|
805
|
+
? this.coerce(left, right.type)
|
|
806
|
+
: left,
|
|
807
|
+
right,
|
|
808
|
+
};
|
|
831
809
|
}
|
|
832
810
|
|
|
833
811
|
sdkCall(node: ts.CallExpression): string | undefined {
|
|
@@ -851,7 +829,10 @@ export class ExpressionLowering {
|
|
|
851
829
|
"Update requires a static block instance binding.",
|
|
852
830
|
);
|
|
853
831
|
}
|
|
854
|
-
const definition = this.blocks.definitions.get(receiver.block)
|
|
832
|
+
const definition = this.blocks.definitions.get(receiver.block);
|
|
833
|
+
if (!definition) {
|
|
834
|
+
throw new Error(`Block ${receiver.block} has no definition.`);
|
|
835
|
+
}
|
|
855
836
|
const [record, ...rest] = node.arguments;
|
|
856
837
|
if (
|
|
857
838
|
node.typeArguments?.length ||
|
|
@@ -865,39 +846,17 @@ export class ExpressionLowering {
|
|
|
865
846
|
"Block update requires one complete named input record.",
|
|
866
847
|
);
|
|
867
848
|
}
|
|
868
|
-
const inputs
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
)) {
|
|
876
|
-
inputs[parameter.name] =
|
|
877
|
-
parameter.mode === "inOut"
|
|
878
|
-
? this.reference(value, parameter)
|
|
879
|
-
: value;
|
|
880
|
-
}
|
|
881
|
-
continue;
|
|
882
|
-
}
|
|
883
|
-
const named = namedProperty(property);
|
|
884
|
-
if (!named) {
|
|
885
|
-
this.fail(
|
|
886
|
-
property,
|
|
887
|
-
"PLC2001",
|
|
849
|
+
const inputs = this.namedFields(record, definition.inputs, {
|
|
850
|
+
spread: (parameter, value) =>
|
|
851
|
+
parameter.mode === "inOut" ? this.reference(value, parameter) : value,
|
|
852
|
+
named: (parameter, value) => this.inputArgument(value, parameter),
|
|
853
|
+
unnamed: {
|
|
854
|
+
code: "PLC2001",
|
|
855
|
+
message:
|
|
888
856
|
"Block inputs are named fields after at most one leading spread.",
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
(input) => input.name === named.name,
|
|
893
|
-
);
|
|
894
|
-
if (!parameter || written.has(parameter.name)) {
|
|
895
|
-
this.fail(property, "PLC2001", "Unknown or duplicate block input.");
|
|
896
|
-
}
|
|
897
|
-
written.add(parameter.name);
|
|
898
|
-
delete inputs[parameter.name];
|
|
899
|
-
inputs[parameter.name] = this.inputArgument(named.value, parameter);
|
|
900
|
-
}
|
|
857
|
+
},
|
|
858
|
+
unknown: "Unknown or duplicate block input.",
|
|
859
|
+
});
|
|
901
860
|
if (definition.inputs.some((input) => !Object.hasOwn(inputs, input.name))) {
|
|
902
861
|
this.fail(
|
|
903
862
|
node,
|
|
@@ -929,7 +888,6 @@ export class ExpressionLowering {
|
|
|
929
888
|
block:
|
|
930
889
|
variable && instancedBlock(variable.type, this.resolver.types)?.block,
|
|
931
890
|
index,
|
|
932
|
-
// An instance is named bare, and an instance array element by one index.
|
|
933
891
|
selected: !rest.length && (array ? Boolean(index) : !selector),
|
|
934
892
|
};
|
|
935
893
|
}
|
|
@@ -1045,9 +1003,13 @@ export class ExpressionLowering {
|
|
|
1045
1003
|
"PLC argument count differs from its signature.",
|
|
1046
1004
|
);
|
|
1047
1005
|
}
|
|
1048
|
-
return
|
|
1049
|
-
|
|
1050
|
-
|
|
1006
|
+
return node.arguments.map((argument, index) => {
|
|
1007
|
+
const parameter = fn.parameters[index];
|
|
1008
|
+
if (!parameter) {
|
|
1009
|
+
throw new Error(`${fn.id} takes ${fn.parameters.length} arguments.`);
|
|
1010
|
+
}
|
|
1011
|
+
return this.inputArgument(argument, parameter);
|
|
1012
|
+
});
|
|
1051
1013
|
}
|
|
1052
1014
|
|
|
1053
1015
|
private inputArgument(value: ts.Expression, parameter: Variable): Expression {
|
|
@@ -1117,6 +1079,48 @@ export class ExpressionLowering {
|
|
|
1117
1079
|
: this.implicitCopy(argument);
|
|
1118
1080
|
}
|
|
1119
1081
|
|
|
1082
|
+
/**
|
|
1083
|
+
* The values an object literal gives its targets: named fields after at most one leading
|
|
1084
|
+
* spread, each target written at most once. A named field overrides the spread and keeps its
|
|
1085
|
+
* source evaluation order.
|
|
1086
|
+
*/
|
|
1087
|
+
private namedFields<Target extends { name: string; type: string }>(
|
|
1088
|
+
record: ts.ObjectLiteralExpression,
|
|
1089
|
+
targets: readonly Target[],
|
|
1090
|
+
rules: {
|
|
1091
|
+
spread: (target: Target, value: Expression) => Expression;
|
|
1092
|
+
named: (target: Target, value: ts.Expression) => Expression;
|
|
1093
|
+
unnamed: { code: string; message: string };
|
|
1094
|
+
unknown: string;
|
|
1095
|
+
},
|
|
1096
|
+
): Record<string, Expression> {
|
|
1097
|
+
const values: Record<string, Expression> = Object.create(null);
|
|
1098
|
+
const written = new Set<string>();
|
|
1099
|
+
for (const [position, property] of record.properties.entries()) {
|
|
1100
|
+
if (ts.isSpreadAssignment(property) && !position) {
|
|
1101
|
+
for (const [target, value] of this.spreadFields(
|
|
1102
|
+
property.expression,
|
|
1103
|
+
targets,
|
|
1104
|
+
)) {
|
|
1105
|
+
values[target.name] = rules.spread(target, value);
|
|
1106
|
+
}
|
|
1107
|
+
continue;
|
|
1108
|
+
}
|
|
1109
|
+
const named = namedProperty(property);
|
|
1110
|
+
if (!named) {
|
|
1111
|
+
this.fail(property, rules.unnamed.code, rules.unnamed.message);
|
|
1112
|
+
}
|
|
1113
|
+
const target = targets.find((target) => target.name === named.name);
|
|
1114
|
+
if (!target || written.has(target.name)) {
|
|
1115
|
+
this.fail(property, "PLC2001", rules.unknown);
|
|
1116
|
+
}
|
|
1117
|
+
written.add(target.name);
|
|
1118
|
+
delete values[target.name];
|
|
1119
|
+
values[target.name] = rules.named(target, named.value);
|
|
1120
|
+
}
|
|
1121
|
+
return values;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1120
1124
|
/**
|
|
1121
1125
|
* The fields `{ ...base }` supplies, each with the target it fills: base is a record whose
|
|
1122
1126
|
* fields are all target fields of the same type. A stored base is read field by field;
|
|
@@ -1176,15 +1180,16 @@ export class ExpressionLowering {
|
|
|
1176
1180
|
);
|
|
1177
1181
|
}
|
|
1178
1182
|
const { value } = base;
|
|
1179
|
-
return filled.map(([target, name]) =>
|
|
1180
|
-
|
|
1181
|
-
{
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1183
|
+
return filled.map(([target, name]) => {
|
|
1184
|
+
const field = value.fields[name];
|
|
1185
|
+
if (!field) {
|
|
1186
|
+
throw new Error(`A ${spread.type} constant has no ${name} field.`);
|
|
1187
|
+
}
|
|
1188
|
+
return [
|
|
1189
|
+
target,
|
|
1190
|
+
{ kind: "constant", type: target.type, value: field, span: base.span },
|
|
1191
|
+
];
|
|
1192
|
+
});
|
|
1188
1193
|
}
|
|
1189
1194
|
|
|
1190
1195
|
/** A record or array assigned into storage copies: fixed storage cannot repoint at its source. */
|
|
@@ -1337,7 +1342,12 @@ export class ExpressionLowering {
|
|
|
1337
1342
|
const place = this.place(node);
|
|
1338
1343
|
const { type, issue } = this.writablePlaceType(place);
|
|
1339
1344
|
if (issue || !type || this.loopCounters.has(place.root)) {
|
|
1340
|
-
this.fail(
|
|
1345
|
+
this.fail(
|
|
1346
|
+
node,
|
|
1347
|
+
"PLC3002",
|
|
1348
|
+
issue ??
|
|
1349
|
+
"This storage is read-only here: copy it into a let and change the copy.",
|
|
1350
|
+
);
|
|
1341
1351
|
}
|
|
1342
1352
|
const overlaps = (source: Place) =>
|
|
1343
1353
|
source.root === place.root &&
|
|
@@ -1361,18 +1371,6 @@ export class ExpressionLowering {
|
|
|
1361
1371
|
}
|
|
1362
1372
|
}
|
|
1363
1373
|
|
|
1364
|
-
/** A float literal rounds to its PLC type's width. */
|
|
1365
|
-
function floatConstant(
|
|
1366
|
-
text: string,
|
|
1367
|
-
type: Extract<PlcType, { kind: "float" }>,
|
|
1368
|
-
): Constant {
|
|
1369
|
-
const clean = text.replaceAll("_", "");
|
|
1370
|
-
const number = /^-?0[xob]/i.test(clean)
|
|
1371
|
-
? Number(parseInteger(clean))
|
|
1372
|
-
: Number(clean);
|
|
1373
|
-
return { kind: "float", value: roundFloat(number, type) };
|
|
1374
|
-
}
|
|
1375
|
-
|
|
1376
1374
|
/** A literal whose type comes from the other side of a comparison. */
|
|
1377
1375
|
function contextualLiteral(node: ts.Expression): boolean {
|
|
1378
1376
|
const inner = unparenthesized(node);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { relative } from "node:path";
|
|
2
1
|
import { identityDigest } from "@tsplc/compiler/ir";
|
|
3
2
|
import ts from "typescript";
|
|
3
|
+
import { modulePath } from "./lowering-key";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Names for what lowering generates, each a fixed function of the source node it stands for: its
|
|
@@ -16,9 +16,9 @@ export class GeneratedNames {
|
|
|
16
16
|
return `${base}_${identityDigest(this.identity(node))}`;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
identity(node: ts.Node): string {
|
|
19
|
+
private identity(node: ts.Node): string {
|
|
20
20
|
const source = node.getSourceFile();
|
|
21
|
-
const module =
|
|
21
|
+
const module = modulePath(this.root, source.fileName);
|
|
22
22
|
const scopes = enclosingDeclarations(node);
|
|
23
23
|
const scope = scopes.at(-1)?.node ?? source;
|
|
24
24
|
const key = nodeKey(node, source);
|
|
@@ -4,8 +4,10 @@ import {
|
|
|
4
4
|
defaultValue,
|
|
5
5
|
durationLiteral,
|
|
6
6
|
type Expression,
|
|
7
|
+
isRadixLiteral,
|
|
7
8
|
parseInteger,
|
|
8
9
|
} from "@tsplc/compiler/ir";
|
|
10
|
+
import { nanosecondsPerMillisecond } from "@tsplc/iec";
|
|
9
11
|
import { elementaryFunctions, type UnaryOperator } from "@tsplc/iec/arithmetic";
|
|
10
12
|
import ts from "typescript";
|
|
11
13
|
import type { ExpressionLowering } from "./expressions";
|
|
@@ -17,22 +19,21 @@ type Arithmetic = Pick<
|
|
|
17
19
|
"operator" | "policy"
|
|
18
20
|
>;
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
const durationScales
|
|
22
|
-
["ms",
|
|
23
|
-
["seconds",
|
|
24
|
-
["minutes",
|
|
22
|
+
/** The SDK's TIME constructors, such as `ms`, by the nanoseconds one unit of each is. */
|
|
23
|
+
export const durationScales: ReadonlyMap<string, bigint> = new Map([
|
|
24
|
+
["ms", nanosecondsPerMillisecond],
|
|
25
|
+
["seconds", 1000n * nanosecondsPerMillisecond],
|
|
26
|
+
["minutes", 60_000n * nanosecondsPerMillisecond],
|
|
25
27
|
]);
|
|
26
28
|
const calendarTypes = new Map<string, string>([
|
|
27
29
|
["date", "DATE"],
|
|
28
30
|
["timeOfDay", "TIME_OF_DAY"],
|
|
29
31
|
["dateAndTime", "DATE_AND_TIME"],
|
|
30
32
|
]);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
]);
|
|
33
|
+
/** The SDK's conversions, each named for the policy it applies. */
|
|
34
|
+
const conversions: readonly Conversion[] = ["exact", "wrap", "saturate"];
|
|
35
|
+
const isConversion = (name: string): name is Conversion =>
|
|
36
|
+
conversions.some((conversion) => conversion === name);
|
|
36
37
|
/** The SDK's one-operand math functions, each over the floating type its type argument names. */
|
|
37
38
|
const mathFunctions = new Map<string, UnaryOperator>([
|
|
38
39
|
...elementaryFunctions.map((name): [string, UnaryOperator] => [name, name]),
|
|
@@ -50,6 +51,15 @@ const arithmetic = new Map<string, Arithmetic>([
|
|
|
50
51
|
["saturatingMultiply", { operator: "multiply", policy: "saturate" }],
|
|
51
52
|
]);
|
|
52
53
|
|
|
54
|
+
/** The SDK calls whose value has the PLC type their type argument names, such as `wrap<INT>(x)`. */
|
|
55
|
+
export const typeArgumentIntrinsics: ReadonlySet<string> = new Set([
|
|
56
|
+
...conversions,
|
|
57
|
+
...mathFunctions.keys(),
|
|
58
|
+
...arithmetic.keys(),
|
|
59
|
+
"defaultValue",
|
|
60
|
+
"copy",
|
|
61
|
+
]);
|
|
62
|
+
|
|
53
63
|
/** A call to an SDK intrinsic such as `ms`, `wrap<T>` or `defaultValue<T>`; undefined for any other call. */
|
|
54
64
|
export function intrinsicExpression(
|
|
55
65
|
lowering: ExpressionLowering,
|
|
@@ -72,9 +82,8 @@ export function intrinsicExpression(
|
|
|
72
82
|
calendarConstant(lowering, argument, calendar),
|
|
73
83
|
);
|
|
74
84
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return converted(lowering, node, conversion);
|
|
85
|
+
if (isConversion(name)) {
|
|
86
|
+
return converted(lowering, node, name);
|
|
78
87
|
}
|
|
79
88
|
const operation = arithmetic.get(name);
|
|
80
89
|
if (operation !== undefined) {
|
|
@@ -148,10 +157,15 @@ function durationConstant(
|
|
|
148
157
|
scale: bigint,
|
|
149
158
|
): Constant | undefined {
|
|
150
159
|
const text = durationText(lowering, argument);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
160
|
+
return text === undefined ? undefined : literalDuration(text, scale);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The TIME value a numeric literal's text stands for in units of `scale` nanoseconds. */
|
|
164
|
+
export function literalDuration(
|
|
165
|
+
text: string,
|
|
166
|
+
scale: bigint,
|
|
167
|
+
): Constant | undefined {
|
|
168
|
+
const decimal = isRadixLiteral(text) ? String(parseInteger(text)) : text;
|
|
155
169
|
return durationLiteral(decimal.replace(/n$/, ""), scale);
|
|
156
170
|
}
|
|
157
171
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { relative, resolve } from "node:path";
|
|
2
|
+
|
|
3
|
+
/** A file's path from the project root, with forward slashes, as generated names, digests and declaration ids spell it. */
|
|
4
|
+
export const modulePath = (projectRoot: string, file: string): string =>
|
|
5
|
+
relative(projectRoot, file).replace(/\\/g, "/");
|
|
6
|
+
|
|
7
|
+
/** The key a lowering pass records what it learned about a module's declaration under. */
|
|
8
|
+
export const loweringKey = (file: string, name: string): string =>
|
|
9
|
+
`${file}:${name}`;
|
|
10
|
+
|
|
11
|
+
/** The lowering key of a verified declaration, whose id begins `<modulePath>:<start>:<name>`. */
|
|
12
|
+
export const declarationKey = (
|
|
13
|
+
root: string,
|
|
14
|
+
declaration: { id: string; name: string },
|
|
15
|
+
): string =>
|
|
16
|
+
loweringKey(
|
|
17
|
+
resolve(root, declaration.id.slice(0, declaration.id.indexOf(":"))),
|
|
18
|
+
declaration.name,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
/** The lowering key of a verified type, which carries its name only in its id. */
|
|
22
|
+
export const typeKey = (root: string, id: string): string =>
|
|
23
|
+
declarationKey(root, { id, name: id.slice(id.lastIndexOf(":") + 1) });
|