@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
package/src/ir/declarations.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
type Variable,
|
|
11
11
|
} from "./model";
|
|
12
12
|
import { reject } from "./normalization-error";
|
|
13
|
-
import { instancedBlock, type PlcType } from "./types";
|
|
13
|
+
import { instancedBlock, isValueType, type PlcType } from "./types";
|
|
14
14
|
|
|
15
15
|
const matchesPorts = (
|
|
16
16
|
variables: readonly Variable[],
|
|
@@ -23,7 +23,7 @@ const matchesPorts = (
|
|
|
23
23
|
variable.name === name &&
|
|
24
24
|
variable.type === type &&
|
|
25
25
|
!variable.mode &&
|
|
26
|
-
|
|
26
|
+
variable.retention === "none",
|
|
27
27
|
),
|
|
28
28
|
);
|
|
29
29
|
|
|
@@ -32,11 +32,8 @@ const checkStandardBlock = (
|
|
|
32
32
|
block: FunctionBlock,
|
|
33
33
|
standard: StandardBlock,
|
|
34
34
|
): void => {
|
|
35
|
-
const expected =
|
|
36
|
-
? standardBlocks[standard]
|
|
37
|
-
: undefined;
|
|
35
|
+
const expected = standardBlocks[standard];
|
|
38
36
|
if (
|
|
39
|
-
!expected ||
|
|
40
37
|
block.name !== standard ||
|
|
41
38
|
block.body.length ||
|
|
42
39
|
block.state.length ||
|
|
@@ -101,19 +98,18 @@ export class ModuleDeclarations {
|
|
|
101
98
|
if (
|
|
102
99
|
fn.owner &&
|
|
103
100
|
(!this.blocks.has(fn.owner.block) ||
|
|
104
|
-
this.blocks.get(fn.owner.block)?.standard
|
|
105
|
-
!["public", "private", "protected"].includes(fn.owner.access))
|
|
101
|
+
this.blocks.get(fn.owner.block)?.standard)
|
|
106
102
|
) {
|
|
107
103
|
reject(
|
|
108
104
|
"Methods require a user block owner and a valid access modifier.",
|
|
109
105
|
fn.span,
|
|
110
106
|
);
|
|
111
107
|
}
|
|
112
|
-
if (fn.result && !
|
|
108
|
+
if (fn.result && !isValueType(fn.result, this.types)) {
|
|
113
109
|
reject("Unknown or non-value function result type.", fn.span);
|
|
114
110
|
}
|
|
115
111
|
for (const parameter of fn.parameters) {
|
|
116
|
-
this.checkVariable(parameter, ["parameter"], true);
|
|
112
|
+
this.checkVariable(parameter, ["parameter"], { allowBorrow: true });
|
|
117
113
|
if (parameter.mutable && !parameter.mode) {
|
|
118
114
|
reject("Value parameters are readonly.", parameter.span);
|
|
119
115
|
}
|
|
@@ -126,7 +122,7 @@ export class ModuleDeclarations {
|
|
|
126
122
|
for (const block of module.blocks) {
|
|
127
123
|
this.claimId(block.id, block.span);
|
|
128
124
|
for (const input of block.inputs) {
|
|
129
|
-
this.checkVariable(input, ["parameter"], true);
|
|
125
|
+
this.checkVariable(input, ["parameter"], { allowBorrow: true });
|
|
130
126
|
if (!input.mode && input.mutable) {
|
|
131
127
|
reject("Value block inputs are readonly.", input.span);
|
|
132
128
|
}
|
|
@@ -136,6 +132,13 @@ export class ModuleDeclarations {
|
|
|
136
132
|
}
|
|
137
133
|
for (const state of block.state) {
|
|
138
134
|
this.checkVariable(state, ["field", "instance"]);
|
|
135
|
+
// A controller has one persistent list, and it holds globals: an instance's state is the instance's to persist.
|
|
136
|
+
if (state.retention === "persist") {
|
|
137
|
+
reject(
|
|
138
|
+
"Only module state may persist; persist the module binding that holds the instance.",
|
|
139
|
+
state.span,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
139
142
|
}
|
|
140
143
|
for (const local of block.locals) {
|
|
141
144
|
this.checkVariable(local, ["local"]);
|
|
@@ -154,7 +157,7 @@ export class ModuleDeclarations {
|
|
|
154
157
|
if (
|
|
155
158
|
block.outputs.length
|
|
156
159
|
? result?.kind !== "record" ||
|
|
157
|
-
!
|
|
160
|
+
!isValueType(result.id, this.types) ||
|
|
158
161
|
result.fields.length !== block.outputs.length ||
|
|
159
162
|
result.fields.some(
|
|
160
163
|
(field) =>
|
|
@@ -196,27 +199,10 @@ export class ModuleDeclarations {
|
|
|
196
199
|
this.ids.add(id);
|
|
197
200
|
}
|
|
198
201
|
|
|
199
|
-
public isValueType(id: string, seen = new Set<string>()): boolean {
|
|
200
|
-
if (seen.has(id)) {
|
|
201
|
-
return true;
|
|
202
|
-
}
|
|
203
|
-
seen.add(id);
|
|
204
|
-
const type = this.types.get(id);
|
|
205
|
-
return Boolean(
|
|
206
|
-
type &&
|
|
207
|
-
type.kind !== "block" &&
|
|
208
|
-
(type.kind !== "record" ||
|
|
209
|
-
type.fields.every(
|
|
210
|
-
(field) => !field.mode && this.isValueType(field.type, seen),
|
|
211
|
-
)) &&
|
|
212
|
-
(type.kind !== "array" || this.isValueType(type.element, seen)),
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
202
|
private checkVariable(
|
|
217
203
|
item: Variable,
|
|
218
204
|
storage: readonly Variable["storage"][],
|
|
219
|
-
allowBorrow =
|
|
205
|
+
{ allowBorrow }: { allowBorrow?: true } = {},
|
|
220
206
|
): void {
|
|
221
207
|
this.claimId(item.id, item.span);
|
|
222
208
|
const type = this.types.get(item.type);
|
|
@@ -237,7 +223,7 @@ export class ModuleDeclarations {
|
|
|
237
223
|
type.kind === "array" ||
|
|
238
224
|
(type.kind === "record" && type.fields.some((field) => !field.readonly));
|
|
239
225
|
if (
|
|
240
|
-
item.
|
|
226
|
+
item.retention !== "none" &&
|
|
241
227
|
!(
|
|
242
228
|
item.storage === "instance" ||
|
|
243
229
|
(["module", "field"].includes(item.storage) && mutableState)
|
|
@@ -262,7 +248,7 @@ export class ModuleDeclarations {
|
|
|
262
248
|
);
|
|
263
249
|
}
|
|
264
250
|
} else if (
|
|
265
|
-
!
|
|
251
|
+
!isValueType(item.type, this.types) &&
|
|
266
252
|
!(allowBorrow && item.mode === "inOut" && type.kind === "block")
|
|
267
253
|
) {
|
|
268
254
|
reject(
|
package/src/ir/diagnostics.ts
CHANGED
|
@@ -40,22 +40,29 @@ export function formatDiagnostic(
|
|
|
40
40
|
): string {
|
|
41
41
|
const { span } = diagnostic;
|
|
42
42
|
const location = span ? `${span.file}:${span.line}:${span.column}: ` : "";
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
for (const related of diagnostic.related ?? []) {
|
|
55
|
-
result += `\n ${related.span.file}:${related.span.line}:${related.span.column}: ${related.message}`;
|
|
56
|
-
}
|
|
57
|
-
for (const note of diagnostic.notes ?? []) {
|
|
58
|
-
result += `\n ${note}`;
|
|
59
|
-
}
|
|
60
|
-
return result;
|
|
43
|
+
return [
|
|
44
|
+
`${location}${diagnostic.code}: ${diagnostic.message}`,
|
|
45
|
+
...(source !== undefined && span ? excerpt(span, source) : []),
|
|
46
|
+
...(diagnostic.related ?? []).map(
|
|
47
|
+
(related) =>
|
|
48
|
+
` ${related.span.file}:${related.span.line}:${related.span.column}: ${related.message}`,
|
|
49
|
+
),
|
|
50
|
+
...(diagnostic.notes ?? []).map((note) => ` ${note}`),
|
|
51
|
+
].join("\n");
|
|
61
52
|
}
|
|
53
|
+
|
|
54
|
+
/** The source line a span starts on, underlined from the span's column. */
|
|
55
|
+
const excerpt = (span: SourceSpan, source: string): string[] => {
|
|
56
|
+
const line = source.split(/\r?\n/)[span.line - 1];
|
|
57
|
+
if (line === undefined) {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
const width = Math.max(
|
|
61
|
+
1,
|
|
62
|
+
Math.min(span.end - span.start, line.length - span.column + 2),
|
|
63
|
+
);
|
|
64
|
+
return [
|
|
65
|
+
`${span.line} | ${line}`,
|
|
66
|
+
`${" ".repeat(String(span.line).length + 3 + span.column - 1)}${"^".repeat(width)}`,
|
|
67
|
+
];
|
|
68
|
+
};
|
package/src/ir/domains.ts
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
latestCalendarSecond,
|
|
3
|
+
latestTimeNanoseconds,
|
|
4
|
+
nanosecondsPerMillisecond,
|
|
5
|
+
} from "@tsplc/iec";
|
|
6
|
+
import { isComparison } from "@tsplc/iec/arithmetic";
|
|
7
|
+
import type { Constant, Expression } from "./model";
|
|
3
8
|
import { canonicalInteger, exactDecimal } from "./numeric";
|
|
4
9
|
import type { PlcType } from "./types";
|
|
5
10
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// TIME stores unsigned milliseconds.
|
|
9
|
-
const [, latestMillisecond] = integerRange({
|
|
10
|
-
bits: elementaryType("TIME").bits,
|
|
11
|
-
signed: false,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
// DATE and DATE_AND_TIME store unsigned seconds since 1970-01-01T00:00:00 UTC.
|
|
15
|
-
const [, latestSecond] = integerRange({
|
|
16
|
-
bits: elementaryType("DATE_AND_TIME").bits,
|
|
17
|
-
signed: false,
|
|
18
|
-
});
|
|
19
|
-
const latestDateTime = new Date(Number(latestSecond) * 1000)
|
|
11
|
+
const latestDateTime = new Date(Number(latestCalendarSecond) * 1000)
|
|
20
12
|
.toISOString()
|
|
21
13
|
.slice(0, "YYYY-MM-DDTHH:mm:ss".length);
|
|
22
14
|
const latestDate = latestDateTime.slice(0, "YYYY-MM-DD".length);
|
|
@@ -46,7 +38,7 @@ export function durationLiteral(
|
|
|
46
38
|
function validDuration(nanoseconds: bigint): boolean {
|
|
47
39
|
return (
|
|
48
40
|
nanoseconds >= 0n &&
|
|
49
|
-
nanoseconds <=
|
|
41
|
+
nanoseconds <= latestTimeNanoseconds &&
|
|
50
42
|
nanoseconds % nanosecondsPerMillisecond === 0n
|
|
51
43
|
);
|
|
52
44
|
}
|
|
@@ -135,14 +127,25 @@ export function domainIssue(
|
|
|
135
127
|
) {
|
|
136
128
|
return undefined;
|
|
137
129
|
}
|
|
138
|
-
if (value.kind === "binary" &&
|
|
139
|
-
|
|
140
|
-
return value.type === "BOOL" &&
|
|
141
|
-
left &&
|
|
142
|
-
["duration", "calendar"].includes(left.kind) &&
|
|
143
|
-
value.left.type === value.right.type
|
|
130
|
+
if (value.kind === "binary" && isComparison(value.operator)) {
|
|
131
|
+
return comparedType(value, types, ["duration", "calendar"])
|
|
144
132
|
? undefined
|
|
145
133
|
: "Time comparisons require matching domains.";
|
|
146
134
|
}
|
|
147
135
|
return "TIME and calendar domains permit storage, typed copies and comparisons; implicit numeric arithmetic is unsupported.";
|
|
148
136
|
}
|
|
137
|
+
|
|
138
|
+
/** The operands' type of a BOOL comparison of two operands of one type, when that type is one of `kinds`. */
|
|
139
|
+
export function comparedType(
|
|
140
|
+
value: Extract<Expression, { kind: "binary" }>,
|
|
141
|
+
types: ReadonlyMap<string, PlcType>,
|
|
142
|
+
kinds: readonly PlcType["kind"][],
|
|
143
|
+
): PlcType | undefined {
|
|
144
|
+
const left = types.get(value.left.type);
|
|
145
|
+
return value.type === "BOOL" &&
|
|
146
|
+
left &&
|
|
147
|
+
kinds.includes(left.kind) &&
|
|
148
|
+
value.left.type === value.right.type
|
|
149
|
+
? left
|
|
150
|
+
: undefined;
|
|
151
|
+
}
|
package/src/ir/fold.ts
CHANGED
|
@@ -2,24 +2,28 @@ import {
|
|
|
2
2
|
applyIntegerPolicy,
|
|
3
3
|
booleanOperation,
|
|
4
4
|
compare,
|
|
5
|
-
elementaryValue,
|
|
6
5
|
floatArithmetic,
|
|
6
|
+
floatUnary,
|
|
7
7
|
integerArithmetic,
|
|
8
|
-
|
|
8
|
+
integerUnary,
|
|
9
9
|
roundFloat,
|
|
10
10
|
saturateFloat,
|
|
11
|
+
saturateFloatToInteger,
|
|
11
12
|
} from "@tsplc/iec/arithmetic";
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
import {
|
|
14
|
+
type Constant,
|
|
15
|
+
type Expression,
|
|
16
|
+
isLogicalIo,
|
|
17
|
+
type Variable,
|
|
18
|
+
} from "./model";
|
|
19
|
+
import { integerType, type PlcType } from "./types";
|
|
16
20
|
|
|
17
|
-
type Types = ReadonlyMap<string, PlcType
|
|
21
|
+
type Types = ReadonlyMap<string, PlcType>;
|
|
18
22
|
|
|
19
23
|
export const fold = (
|
|
20
24
|
expression: Expression,
|
|
21
25
|
variables: ReadonlyMap<string, Variable>,
|
|
22
|
-
types
|
|
26
|
+
types: Types,
|
|
23
27
|
): Constant | undefined => {
|
|
24
28
|
switch (expression.kind) {
|
|
25
29
|
case "constant":
|
|
@@ -49,19 +53,19 @@ export const fold = (
|
|
|
49
53
|
return foldConvert(
|
|
50
54
|
expression,
|
|
51
55
|
fold(expression.operand, variables, types),
|
|
52
|
-
types
|
|
56
|
+
types.get(expression.type),
|
|
53
57
|
);
|
|
54
58
|
case "unary":
|
|
55
59
|
return foldUnary(
|
|
56
60
|
expression,
|
|
57
61
|
fold(expression.operand, variables, types),
|
|
58
|
-
types
|
|
62
|
+
types.get(expression.type),
|
|
59
63
|
);
|
|
60
64
|
case "binary": {
|
|
61
65
|
const left = fold(expression.left, variables, types);
|
|
62
66
|
const right = fold(expression.right, variables, types);
|
|
63
67
|
return left && right
|
|
64
|
-
? foldBinary(expression, left, right, types
|
|
68
|
+
? foldBinary(expression, left, right, types.get(expression.type))
|
|
65
69
|
: undefined;
|
|
66
70
|
}
|
|
67
71
|
default:
|
|
@@ -74,7 +78,7 @@ const foldLength = (
|
|
|
74
78
|
variables: ReadonlyMap<string, Variable>,
|
|
75
79
|
types: Types,
|
|
76
80
|
): Constant | undefined => {
|
|
77
|
-
const type = types
|
|
81
|
+
const type = types.get(expression.operand.type);
|
|
78
82
|
const receiver =
|
|
79
83
|
expression.operand.kind === "load"
|
|
80
84
|
? variables.get(expression.operand.place.root)
|
|
@@ -84,11 +88,7 @@ const foldLength = (
|
|
|
84
88
|
expression.operand.place.path.every(
|
|
85
89
|
(segment) => segment.kind === "field",
|
|
86
90
|
) &&
|
|
87
|
-
Boolean(
|
|
88
|
-
receiver?.initial ||
|
|
89
|
-
receiver?.storage === "input" ||
|
|
90
|
-
receiver?.storage === "output",
|
|
91
|
-
);
|
|
91
|
+
Boolean(receiver && (receiver.initial || isLogicalIo(receiver)));
|
|
92
92
|
if (
|
|
93
93
|
type?.kind === "array" &&
|
|
94
94
|
(pure || fold(expression.operand, variables, types))
|
|
@@ -133,6 +133,17 @@ const foldConvert = (
|
|
|
133
133
|
if (value?.kind === "integer" && target?.kind === "float") {
|
|
134
134
|
return { kind: "float", value: roundFloat(Number(value.value), target) };
|
|
135
135
|
}
|
|
136
|
+
if (
|
|
137
|
+
value?.kind === "float" &&
|
|
138
|
+
target &&
|
|
139
|
+
integerType(target) &&
|
|
140
|
+
expression.policy === "saturate"
|
|
141
|
+
) {
|
|
142
|
+
return {
|
|
143
|
+
kind: "integer",
|
|
144
|
+
value: String(saturateFloatToInteger(value.value, target)),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
136
147
|
if (value?.kind === "float" && target?.kind === "float") {
|
|
137
148
|
return {
|
|
138
149
|
kind: "float",
|
|
@@ -146,48 +157,42 @@ const foldConvert = (
|
|
|
146
157
|
};
|
|
147
158
|
|
|
148
159
|
const foldUnary = (
|
|
149
|
-
|
|
160
|
+
{ operator }: Extract<Expression, { kind: "unary" }>,
|
|
150
161
|
value: Constant | undefined,
|
|
151
162
|
target: PlcType | undefined,
|
|
152
163
|
): Constant | undefined => {
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return {
|
|
158
|
-
kind: "integer",
|
|
159
|
-
value: String(BigInt(value.nanoseconds) / nanosecondsPerMillisecond),
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
if (value?.kind === "float" && isElementary(expression.operator)) {
|
|
163
|
-
return finiteFloat(
|
|
164
|
-
elementaryValue[expression.operator](value.value),
|
|
165
|
-
target,
|
|
166
|
-
);
|
|
164
|
+
if (value?.kind === "bool") {
|
|
165
|
+
return operator === "not"
|
|
166
|
+
? { kind: "bool", value: !value.value }
|
|
167
|
+
: undefined;
|
|
167
168
|
}
|
|
168
|
-
if (value?.kind === "float"
|
|
169
|
-
|
|
169
|
+
if (value?.kind === "float") {
|
|
170
|
+
const result = floatUnary(operator, value.value);
|
|
171
|
+
return result === undefined ? undefined : finiteFloat(result, target);
|
|
170
172
|
}
|
|
171
|
-
if (value?.kind === "
|
|
172
|
-
|
|
173
|
+
if (value?.kind === "duration") {
|
|
174
|
+
const result = integerUnary(operator, BigInt(value.nanoseconds));
|
|
175
|
+
return operator === "timeToMilliseconds" && result !== undefined
|
|
176
|
+
? { kind: "integer", value: String(result) }
|
|
177
|
+
: undefined;
|
|
173
178
|
}
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
expression.operator === "bitNot" &&
|
|
177
|
-
target?.kind === "bits"
|
|
178
|
-
) {
|
|
179
|
-
return {
|
|
180
|
-
kind: "integer",
|
|
181
|
-
value: String(applyIntegerPolicy(~BigInt(value.value), target, "wrap")),
|
|
182
|
-
};
|
|
179
|
+
if (value?.kind !== "integer" || operator === "timeToMilliseconds") {
|
|
180
|
+
return undefined;
|
|
183
181
|
}
|
|
184
|
-
|
|
185
|
-
|
|
182
|
+
const result = integerUnary(operator, BigInt(value.value));
|
|
183
|
+
if (result === undefined) {
|
|
184
|
+
return undefined;
|
|
186
185
|
}
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
// Negation is left unwrapped: its proof requires the result to fit.
|
|
187
|
+
if (operator !== "bitNot") {
|
|
188
|
+
return { kind: "integer", value: String(result) };
|
|
189
189
|
}
|
|
190
|
-
return
|
|
190
|
+
return target?.kind === "bits"
|
|
191
|
+
? {
|
|
192
|
+
kind: "integer",
|
|
193
|
+
value: String(applyIntegerPolicy(result, target, "wrap")),
|
|
194
|
+
}
|
|
195
|
+
: undefined;
|
|
191
196
|
};
|
|
192
197
|
|
|
193
198
|
type Ordered = Exclude<Constant, { kind: "bool" | "array" | "record" }>;
|
package/src/ir/identity.ts
CHANGED
|
@@ -7,3 +7,13 @@ import { createHash } from "node:crypto";
|
|
|
7
7
|
*/
|
|
8
8
|
export const identityDigest = (identity: string): string =>
|
|
9
9
|
createHash("sha256").update(identity).digest("hex").slice(0, 8);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Text in PascalCase, as generated names use it: `bin_full` and `bin full` become `BinFull`.
|
|
13
|
+
* Capitals inside a word are kept, so `PlantIO` stays as written while `plant-io` is `PlantIo`.
|
|
14
|
+
*/
|
|
15
|
+
export const pascalCase = (text: string): string =>
|
|
16
|
+
text
|
|
17
|
+
.split(/[^A-Za-z0-9]+/)
|
|
18
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
19
|
+
.join("");
|
package/src/ir/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { nanosecondsPerMillisecond } from "@tsplc/iec";
|
|
1
2
|
export {
|
|
2
3
|
aggregate,
|
|
3
4
|
aggregateIssue,
|
|
@@ -15,6 +16,7 @@ export {
|
|
|
15
16
|
type Reconstruction,
|
|
16
17
|
type RoutineConditions,
|
|
17
18
|
type StatementConditions,
|
|
19
|
+
staticNames,
|
|
18
20
|
} from "./conditions";
|
|
19
21
|
export {
|
|
20
22
|
type CheckedPlace,
|
|
@@ -23,7 +25,13 @@ export {
|
|
|
23
25
|
type CoreFunctionBlock,
|
|
24
26
|
type CorePath,
|
|
25
27
|
type CoreStatement,
|
|
28
|
+
constantIndex,
|
|
26
29
|
directWrites,
|
|
30
|
+
freeze,
|
|
31
|
+
instanceFields,
|
|
32
|
+
isBlockRoutine,
|
|
33
|
+
isTemporary,
|
|
34
|
+
isVerifiedCore,
|
|
27
35
|
nestedBodies,
|
|
28
36
|
type VerifiedCoreModule,
|
|
29
37
|
} from "./core-model";
|
|
@@ -39,18 +47,26 @@ export {
|
|
|
39
47
|
domainConstantFits,
|
|
40
48
|
domainIssue,
|
|
41
49
|
durationLiteral,
|
|
42
|
-
nanosecondsPerMillisecond,
|
|
43
50
|
} from "./domains";
|
|
44
51
|
export { fold } from "./fold";
|
|
45
|
-
export { identityDigest } from "./identity";
|
|
52
|
+
export { identityDigest, pascalCase } from "./identity";
|
|
46
53
|
export {
|
|
47
54
|
errorName,
|
|
55
|
+
failureCode,
|
|
56
|
+
failureName,
|
|
48
57
|
httpErrors,
|
|
49
58
|
jobError,
|
|
50
59
|
jobErrors,
|
|
51
60
|
reservedErrors,
|
|
52
61
|
} from "./job-errors";
|
|
53
|
-
export {
|
|
62
|
+
export {
|
|
63
|
+
type LayoutBudget,
|
|
64
|
+
layoutIssue,
|
|
65
|
+
maxNestingDepth,
|
|
66
|
+
maxScalarElements,
|
|
67
|
+
maxScalarElementsText,
|
|
68
|
+
maxStringCapacity,
|
|
69
|
+
} from "./layouts";
|
|
54
70
|
export {
|
|
55
71
|
type Artifact,
|
|
56
72
|
type Constant,
|
|
@@ -60,12 +76,16 @@ export {
|
|
|
60
76
|
type Expression,
|
|
61
77
|
type FunctionBlock,
|
|
62
78
|
type GeneratedObject,
|
|
63
|
-
type
|
|
79
|
+
type HttpMethod,
|
|
80
|
+
httpMethods,
|
|
81
|
+
isLogicalIo,
|
|
64
82
|
type MethodOwner,
|
|
83
|
+
optionalRecord,
|
|
65
84
|
type Place,
|
|
66
85
|
type PlcFunction,
|
|
67
86
|
type PlcModule,
|
|
68
87
|
type PlcProgram,
|
|
88
|
+
type Retention,
|
|
69
89
|
type StandardBlock,
|
|
70
90
|
type Statement,
|
|
71
91
|
standardBlocks,
|
|
@@ -74,12 +94,15 @@ export {
|
|
|
74
94
|
telemetryOperands,
|
|
75
95
|
type Variable,
|
|
76
96
|
} from "./model";
|
|
77
|
-
export {
|
|
97
|
+
export { normalizeModule } from "./normalize";
|
|
78
98
|
export {
|
|
79
99
|
canAssign,
|
|
80
100
|
constantFits,
|
|
101
|
+
exactIntegerLimit,
|
|
81
102
|
type IntegerInterval,
|
|
82
|
-
|
|
103
|
+
isRadixLiteral,
|
|
104
|
+
literalOutsideType,
|
|
105
|
+
parseFloatLiteral,
|
|
83
106
|
parseInteger,
|
|
84
107
|
wrapsNatively,
|
|
85
108
|
} from "./numeric";
|
|
@@ -93,7 +116,6 @@ export {
|
|
|
93
116
|
loopBounds,
|
|
94
117
|
maxLoopIterations,
|
|
95
118
|
narrowRanges,
|
|
96
|
-
valueRange,
|
|
97
119
|
} from "./ranges";
|
|
98
120
|
export { scalarIssue } from "./scalars";
|
|
99
121
|
export {
|
|
@@ -101,11 +123,13 @@ export {
|
|
|
101
123
|
type SharedRoutines,
|
|
102
124
|
shareRoutines,
|
|
103
125
|
} from "./share";
|
|
126
|
+
export { type CoreTables, coreTables } from "./tables";
|
|
104
127
|
export { forwardTemporaries } from "./temporaries";
|
|
105
128
|
export {
|
|
106
129
|
type Field,
|
|
107
130
|
instancedBlock,
|
|
108
131
|
integerRange,
|
|
132
|
+
integerType,
|
|
109
133
|
type PlcType,
|
|
110
134
|
primitiveTypes,
|
|
111
135
|
type SymbolId,
|
package/src/ir/job-errors.ts
CHANGED
|
@@ -15,14 +15,22 @@ export const jobError = {
|
|
|
15
15
|
codeChanged: 11001,
|
|
16
16
|
} as const;
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const httpBase = 10000;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* An HTTP response fails a job with `base` + its status, up to `last`. `refused` is a 400: the
|
|
22
|
+
* server can't take the request, so sending it again cannot succeed.
|
|
23
|
+
*/
|
|
24
|
+
export const httpErrors = {
|
|
25
|
+
base: httpBase,
|
|
26
|
+
last: 10999,
|
|
27
|
+
refused: httpBase + 400,
|
|
28
|
+
};
|
|
20
29
|
|
|
21
30
|
/** Codes tsPLC itself reports from 11000 on, which a program's own `throw` may not use. */
|
|
22
31
|
export const reservedErrors = { first: 11000, last: 11099 };
|
|
23
32
|
|
|
24
|
-
|
|
25
|
-
export const jobErrors: Record<string, string> = {
|
|
33
|
+
const errorMeanings: Record<number, string> = {
|
|
26
34
|
[jobError.cancelled]: "cancelled",
|
|
27
35
|
[jobError.timeout]: "timeout",
|
|
28
36
|
[jobError.tooLarge]: "request or response too large to encode",
|
|
@@ -32,7 +40,12 @@ export const jobErrors: Record<string, string> = {
|
|
|
32
40
|
[jobError.noRequestSlot]: "no free request slot",
|
|
33
41
|
[jobError.codeChanged]:
|
|
34
42
|
"an online change replaced the job's code while it ran",
|
|
35
|
-
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Job error codes, as every generated async block and service call reports them. */
|
|
46
|
+
export const jobErrors: Record<string, string> = {
|
|
47
|
+
...errorMeanings,
|
|
48
|
+
[httpErrors.base]: `HTTP status: ${httpErrors.base} + status code`,
|
|
36
49
|
};
|
|
37
50
|
|
|
38
51
|
/** What a telemetry exception record calls an error code: its meaning when it is a job error. */
|
|
@@ -40,8 +53,17 @@ export const errorName = (code: number): string | undefined => {
|
|
|
40
53
|
if (code > httpErrors.base && code <= httpErrors.last) {
|
|
41
54
|
return `HTTP ${code - httpErrors.base}`;
|
|
42
55
|
}
|
|
43
|
-
|
|
44
|
-
|
|
56
|
+
return errorMeanings[code];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** The code a failure reports: its job error code, or 0 for anything else thrown, such as an Error under Bun. */
|
|
60
|
+
export const failureCode = (reason: unknown): number =>
|
|
61
|
+
typeof reason === "number" ? reason : 0;
|
|
62
|
+
|
|
63
|
+
/** What an exception record calls a failure: a job error's meaning, or a thrown Error's message. */
|
|
64
|
+
export const failureName = (reason: unknown): string | undefined => {
|
|
65
|
+
if (typeof reason === "number") {
|
|
66
|
+
return errorName(reason);
|
|
45
67
|
}
|
|
46
|
-
return
|
|
68
|
+
return reason instanceof Error ? reason.message : String(reason);
|
|
47
69
|
};
|