@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.
Files changed (157) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.js +920 -943
  3. package/dist/cli.js.map +13 -10
  4. package/dist/codesys.js +3893 -3352
  5. package/dist/codesys.js.map +29 -20
  6. package/dist/elaborate-capture.js +33 -17
  7. package/dist/elaborate-capture.js.map +3 -3
  8. package/dist/index.js +8787 -6855
  9. package/dist/index.js.map +40 -20
  10. package/dist/ir.js +1575 -1072
  11. package/dist/ir.js.map +21 -17
  12. package/dist/math.js.map +2 -2
  13. package/dist/project.js +154 -60
  14. package/dist/project.js.map +5 -5
  15. package/dist/telemetry.js +27 -9
  16. package/dist/telemetry.js.map +5 -5
  17. package/package.json +112 -112
  18. package/src/codesys/config.ts +50 -0
  19. package/src/codesys/debug.ts +670 -0
  20. package/src/codesys/emit.ts +142 -0
  21. package/src/codesys/engineering.ts +109 -0
  22. package/src/codesys/expressions.ts +92 -0
  23. package/src/codesys/global-lists.ts +250 -0
  24. package/src/codesys/global-names.ts +68 -0
  25. package/src/codesys/http.ts +890 -0
  26. package/src/codesys/index.ts +30 -0
  27. package/src/codesys/instrumentation.ts +430 -0
  28. package/src/codesys/io-paths.ts +140 -0
  29. package/src/codesys/manifest.ts +643 -0
  30. package/src/codesys/names.ts +81 -0
  31. package/src/codesys/numeric.ts +409 -0
  32. package/src/codesys/operands.ts +62 -0
  33. package/src/codesys/plan.ts +605 -0
  34. package/src/codesys/programs.ts +53 -0
  35. package/src/codesys/publication.ts +171 -0
  36. package/src/codesys/routines.ts +603 -0
  37. package/src/codesys/sources.ts +71 -0
  38. package/src/codesys/st-files.ts +115 -0
  39. package/src/codesys/storage.ts +97 -0
  40. package/src/codesys/symbols.ts +220 -0
  41. package/src/codesys/task-access.ts +320 -0
  42. package/src/codesys/telemetry.ts +732 -0
  43. package/src/codesys/types.ts +250 -0
  44. package/src/codesys/vendor.ts +150 -0
  45. package/src/frontend/analyzer.ts +794 -0
  46. package/src/frontend/async.ts +317 -1137
  47. package/src/frontend/blocks.ts +537 -403
  48. package/src/frontend/configuration.ts +176 -144
  49. package/src/frontend/const-functions.ts +183 -148
  50. package/src/frontend/cooperative.ts +73 -43
  51. package/src/frontend/coroutines.ts +1032 -0
  52. package/src/frontend/elaborate/calls.ts +535 -0
  53. package/src/frontend/elaborate/capture.ts +157 -101
  54. package/src/frontend/elaborate/dialect.ts +257 -0
  55. package/src/frontend/elaborate/expressions.ts +992 -0
  56. package/src/frontend/elaborate/frame.ts +53 -0
  57. package/src/frontend/elaborate/graph.ts +116 -71
  58. package/src/frontend/elaborate/imports.ts +70 -0
  59. package/src/frontend/elaborate/index.ts +435 -331
  60. package/src/frontend/elaborate/initializers.ts +198 -0
  61. package/src/frontend/elaborate/layouts.ts +591 -384
  62. package/src/frontend/elaborate/model.ts +264 -169
  63. package/src/frontend/elaborate/specialize.ts +343 -2586
  64. package/src/frontend/elaborate/statements.ts +606 -0
  65. package/src/frontend/elaborate/storage.ts +546 -0
  66. package/src/frontend/elaborate/values.ts +58 -0
  67. package/src/frontend/elaborate/writes.ts +100 -0
  68. package/src/frontend/exposure.ts +172 -140
  69. package/src/frontend/expressions.ts +1383 -0
  70. package/src/frontend/intrinsics.ts +337 -0
  71. package/src/frontend/module-graph.ts +89 -59
  72. package/src/frontend/positions.ts +271 -200
  73. package/src/frontend/promise-chains.ts +897 -0
  74. package/src/frontend/promises.ts +390 -747
  75. package/src/frontend/routines.ts +181 -0
  76. package/src/frontend/sdk.ts +82 -0
  77. package/src/frontend/services.ts +506 -372
  78. package/src/frontend/statements.ts +855 -0
  79. package/src/frontend/tasks.ts +471 -0
  80. package/src/frontend/telemetry.ts +174 -0
  81. package/src/frontend/types.ts +845 -551
  82. package/src/frontend/typescript.ts +323 -2543
  83. package/src/index.ts +5 -5
  84. package/src/ir/aggregates.ts +275 -0
  85. package/src/ir/allocation.ts +84 -0
  86. package/src/ir/core-model.ts +146 -0
  87. package/src/ir/declarations.ts +295 -0
  88. package/src/ir/diagnostics.ts +61 -0
  89. package/src/ir/domains.ts +152 -0
  90. package/src/ir/fold.ts +342 -0
  91. package/src/ir/index.ts +90 -0
  92. package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
  93. package/src/ir/layouts.ts +181 -0
  94. package/src/ir/link.ts +354 -0
  95. package/src/ir/model.ts +386 -0
  96. package/{ir → src/ir}/normalization-error.ts +4 -4
  97. package/src/ir/normalize.ts +70 -0
  98. package/src/ir/numeric.ts +171 -0
  99. package/src/ir/ranges.ts +256 -0
  100. package/src/ir/routine.ts +1163 -0
  101. package/src/ir/scalars.ts +321 -0
  102. package/src/ir/types.ts +127 -0
  103. package/src/project/artifacts.ts +342 -0
  104. package/src/project/cli.ts +177 -0
  105. package/src/project/debugger.ts +82 -0
  106. package/src/project/index.ts +6 -0
  107. package/src/project/plant.ts +467 -0
  108. package/src/project/project-commands.ts +372 -0
  109. package/src/project/project.ts +457 -0
  110. package/src/project/release.ts +153 -0
  111. package/src/project/server.ts +150 -0
  112. package/src/project/terminal.ts +32 -0
  113. package/stdlib/math.ts +14 -10
  114. package/stdlib/telemetry.ts +71 -47
  115. package/types/async.d.ts +46 -40
  116. package/types/data-types.d.ts +26 -26
  117. package/types/globals.d.ts +41 -38
  118. package/types/index.d.ts +96 -51
  119. package/types/plc.d.ts +3 -4
  120. package/types/telemetry.d.ts +14 -14
  121. package/backends/codesys/config.ts +0 -54
  122. package/backends/codesys/debug.ts +0 -509
  123. package/backends/codesys/emit.ts +0 -1738
  124. package/backends/codesys/engineering.ts +0 -88
  125. package/backends/codesys/global-names.ts +0 -49
  126. package/backends/codesys/http.ts +0 -675
  127. package/backends/codesys/index.ts +0 -13
  128. package/backends/codesys/io-paths.ts +0 -105
  129. package/backends/codesys/manifest.ts +0 -312
  130. package/backends/codesys/names.ts +0 -76
  131. package/backends/codesys/numeric.ts +0 -356
  132. package/backends/codesys/plan.ts +0 -457
  133. package/backends/codesys/publication.ts +0 -59
  134. package/backends/codesys/storage.ts +0 -70
  135. package/backends/codesys/task-access.ts +0 -293
  136. package/backends/codesys/telemetry.ts +0 -593
  137. package/backends/codesys/types.ts +0 -179
  138. package/backends/codesys/vendor.ts +0 -108
  139. package/ir/aggregates.ts +0 -333
  140. package/ir/allocation.ts +0 -74
  141. package/ir/core-model.ts +0 -129
  142. package/ir/core.ts +0 -64
  143. package/ir/declarations.ts +0 -208
  144. package/ir/diagnostics.ts +0 -43
  145. package/ir/domains.ts +0 -96
  146. package/ir/index.ts +0 -8
  147. package/ir/link.ts +0 -185
  148. package/ir/model.ts +0 -346
  149. package/ir/routine.ts +0 -859
  150. package/ir/scalars.ts +0 -675
  151. package/ir/types.ts +0 -76
  152. package/project/artifacts.ts +0 -277
  153. package/project/cli.ts +0 -395
  154. package/project/index.ts +0 -1
  155. package/project/plant.ts +0 -509
  156. package/project/project.ts +0 -358
  157. package/project/server.ts +0 -113
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export {
2
- analyze,
3
- typeCheckOnly,
4
- checkHostConfig,
5
- type FrontendOptions,
6
- type FrontendResult,
2
+ analyze,
3
+ typeCheckOnly,
4
+ checkHostConfig,
5
+ type FrontendOptions,
6
+ type FrontendResult,
7
7
  } from "./frontend/typescript.ts";
@@ -0,0 +1,275 @@
1
+ import {
2
+ comparisonOperators,
3
+ type Constant,
4
+ type Expression,
5
+ type Variable,
6
+ type Place,
7
+ } from "./model.ts";
8
+ import { constantFits, type IntegerInterval } from "./numeric.ts";
9
+ import { interval } from "./ranges.ts";
10
+ import { scalarIssue } from "./scalars.ts";
11
+ import { calendarZero, domainConstantFits, domainIssue } from "./domains.ts";
12
+ import type { PlcType } from "./types.ts";
13
+
14
+ export function aggregate(type: PlcType | undefined): boolean {
15
+ return type?.kind === "record" || type?.kind === "array";
16
+ }
17
+ export function valueFits(
18
+ value: Constant,
19
+ type: PlcType,
20
+ types: ReadonlyMap<string, PlcType>,
21
+ ): boolean {
22
+ if (type.kind === "duration" || type.kind === "calendar") {
23
+ return domainConstantFits(value, type);
24
+ }
25
+ if (type.kind === "enum") {
26
+ return (
27
+ value.kind === "integer" &&
28
+ type.members.some((member) => member.value === value.value)
29
+ );
30
+ }
31
+ if (type.kind === "string") {
32
+ return (
33
+ value.kind === "string" &&
34
+ !value.value.includes("\0") &&
35
+ (type.encoding === "utf8"
36
+ ? new TextEncoder().encode(value.value).length <= type.capacity
37
+ : value.value.length <= type.capacity)
38
+ );
39
+ }
40
+ if (type.kind === "array") {
41
+ return (
42
+ value.kind === "array" &&
43
+ value.items.length === type.length &&
44
+ value.items.every((item) =>
45
+ valueFits(item, types.get(type.element)!, types),
46
+ )
47
+ );
48
+ }
49
+ if (type.kind === "record") {
50
+ return (
51
+ value.kind === "record" &&
52
+ Object.keys(value.fields).length === type.fields.length &&
53
+ type.fields.every(
54
+ (field) =>
55
+ Object.hasOwn(value.fields, field.name) &&
56
+ valueFits(value.fields[field.name]!, types.get(field.type)!, types),
57
+ )
58
+ );
59
+ }
60
+ return constantFits(value, type);
61
+ }
62
+ export function defaultValue(
63
+ typeId: string,
64
+ types: ReadonlyMap<string, PlcType>,
65
+ ): Constant {
66
+ const type = types.get(typeId);
67
+ if (!type) {
68
+ throw new Error("Unknown default-value type");
69
+ }
70
+ if (type.kind === "bool") {
71
+ return { kind: "bool", value: false };
72
+ }
73
+ if (type.kind === "float") {
74
+ return { kind: "float", value: 0 };
75
+ }
76
+ if (type.kind === "string") {
77
+ return { kind: "string", value: "" };
78
+ }
79
+ if (type.kind === "enum") {
80
+ return {
81
+ kind: "integer",
82
+ value:
83
+ type.members.find((member) => member.value === "0")?.value ??
84
+ type.members[0]!.value,
85
+ };
86
+ }
87
+ if (type.kind === "array") {
88
+ return {
89
+ kind: "array",
90
+ items: Array.from({ length: type.length }, () =>
91
+ defaultValue(type.element, types),
92
+ ),
93
+ };
94
+ }
95
+ if (type.kind === "record") {
96
+ return {
97
+ kind: "record",
98
+ fields: Object.fromEntries(
99
+ type.fields.map((field) => [
100
+ field.name,
101
+ defaultValue(field.type, types),
102
+ ]),
103
+ ),
104
+ };
105
+ }
106
+ if (type.kind === "duration") {
107
+ return { kind: "duration", nanoseconds: "0" };
108
+ }
109
+ if (type.kind === "calendar") {
110
+ return { kind: "calendar", value: calendarZero[type.domain] };
111
+ }
112
+ if (type.kind === "integer" || type.kind === "bits") {
113
+ return { kind: "integer", value: "0" };
114
+ }
115
+ throw new Error("Unsupported default-value type");
116
+ }
117
+ export function placeType(
118
+ place: Place,
119
+ types: ReadonlyMap<string, PlcType>,
120
+ variables: ReadonlyMap<string, Variable>,
121
+ ranges: ReadonlyMap<string, IntegerInterval> = new Map(),
122
+ writable = false,
123
+ ): { type?: string; issue?: string } {
124
+ const root = variables.get(place.root);
125
+ if (!root) {
126
+ return { issue: "Unknown storage root." };
127
+ }
128
+ if (
129
+ writable &&
130
+ (root.storage === "input" ||
131
+ root.storage === "constant" ||
132
+ root.storage === "blockOutput" ||
133
+ (root.storage === "parameter" && root.mode !== "inOut") ||
134
+ (!place.path.length && !root.mutable))
135
+ ) {
136
+ return { issue: "This storage is read-only." };
137
+ }
138
+ let id = root.type;
139
+ for (const segment of place.path) {
140
+ const type = types.get(id);
141
+ if (segment.kind === "field") {
142
+ if (type?.kind === "block" && segment.name === "out" && type.output) {
143
+ if (writable) {
144
+ return { issue: "Function block output views are read-only." };
145
+ }
146
+ id = type.output;
147
+ continue;
148
+ }
149
+ if (type?.kind !== "record") {
150
+ return { issue: "Field access requires a record." };
151
+ }
152
+ const field = type.fields.find((field) => field.name === segment.name);
153
+ if (!field) {
154
+ return { issue: "Unknown record field." };
155
+ }
156
+ if (writable && field.readonly) {
157
+ return { issue: "This field is read-only." };
158
+ }
159
+ id = field.type;
160
+ } else {
161
+ if (type?.kind !== "array") {
162
+ return { issue: "Index access requires a fixed array." };
163
+ }
164
+ // Enum values are closed, and the layout check below proves its members are the bounds.
165
+ // An integer, such as a for…of counter, still needs its bounds proof.
166
+ if (type.index && segment.value.type === type.index) {
167
+ id = type.element;
168
+ continue;
169
+ }
170
+ const indexType = types.get(segment.value.type);
171
+ const bounds = interval(segment.value, types, variables, ranges);
172
+ if (
173
+ indexType?.kind !== "integer" ||
174
+ !bounds ||
175
+ bounds[0] < BigInt(type.lowerBound) ||
176
+ bounds[1] >= BigInt(type.lowerBound + type.length)
177
+ ) {
178
+ return {
179
+ issue: "Array index must be proved within the declared bounds.",
180
+ };
181
+ }
182
+ id = type.element;
183
+ }
184
+ }
185
+ return { type: id };
186
+ }
187
+ export function aggregateIssue(
188
+ value: Expression,
189
+ types: ReadonlyMap<string, PlcType>,
190
+ ): string | undefined {
191
+ const type = types.get(value.type);
192
+ if (!type) {
193
+ return "Unknown expression type.";
194
+ }
195
+ if (value.kind === "constant") {
196
+ return valueFits(value.value, type, types)
197
+ ? undefined
198
+ : "Constant does not fit its closed PLC layout.";
199
+ }
200
+ if (value.kind === "record") {
201
+ return type.kind === "record" &&
202
+ Object.keys(value.fields).length === type.fields.length &&
203
+ type.fields.every(
204
+ (field) =>
205
+ Object.hasOwn(value.fields, field.name) &&
206
+ value.fields[field.name]?.type === field.type,
207
+ )
208
+ ? undefined
209
+ : "Record expression differs from its layout.";
210
+ }
211
+ if (value.kind === "array") {
212
+ return type.kind === "array" &&
213
+ value.items.length === type.length &&
214
+ value.items.every((item) => item.type === type.element)
215
+ ? undefined
216
+ : "Array expression differs from its layout.";
217
+ }
218
+ if (value.kind === "copy") {
219
+ return aggregate(type) && value.operand.type === value.type
220
+ ? undefined
221
+ : "Copy requires one matching fixed aggregate.";
222
+ }
223
+ if (value.kind === "convert") {
224
+ const source = types.get(value.operand.type);
225
+ return type.kind === "string" &&
226
+ source?.kind === "string" &&
227
+ source.encoding === type.encoding &&
228
+ source.capacity <= type.capacity &&
229
+ value.policy === "exact"
230
+ ? undefined
231
+ : "Unsupported aggregate/domain conversion.";
232
+ }
233
+ if (
234
+ value.kind === "load" ||
235
+ value.kind === "call" ||
236
+ value.kind === "conditional"
237
+ ) {
238
+ return undefined;
239
+ }
240
+ if (value.kind === "binary" && comparisonOperators.has(value.operator)) {
241
+ const left = types.get(value.left.type);
242
+ return value.type === "BOOL" &&
243
+ left &&
244
+ ["enum", "string"].includes(left.kind) &&
245
+ value.left.type === value.right.type &&
246
+ (left.kind !== "enum" || ["equal", "notEqual"].includes(value.operator))
247
+ ? undefined
248
+ : "Comparison requires compatible scalar domains; object identity is unsupported.";
249
+ }
250
+ return "Operation is unsupported for this PLC domain.";
251
+ }
252
+
253
+ // Route a reduced expression to the checker that owns its PLC domain. A binary
254
+ // operation belongs to its operand domain, e.g. a TIME comparison yields BOOL.
255
+ export function expressionIssue(
256
+ value: Expression,
257
+ types: ReadonlyMap<string, PlcType>,
258
+ variables: ReadonlyMap<string, Variable>,
259
+ ranges: ReadonlyMap<string, IntegerInterval>,
260
+ ): string | undefined {
261
+ const kinds = [
262
+ value.type,
263
+ ...(value.kind === "binary" ? [value.left.type] : []),
264
+ ].map((id) => types.get(id)?.kind ?? "");
265
+ if (kinds.some((kind) => ["duration", "calendar"].includes(kind))) {
266
+ return domainIssue(value, types);
267
+ }
268
+ if (
269
+ ["copy", "record", "array"].includes(value.kind) ||
270
+ kinds.some((kind) => ["record", "array", "enum", "string"].includes(kind))
271
+ ) {
272
+ return aggregateIssue(value, types);
273
+ }
274
+ return scalarIssue(value, types, variables, ranges);
275
+ }
@@ -0,0 +1,84 @@
1
+ import type { PlcModule } from "./model.ts";
2
+ import type { ModuleDeclarations } from "./declarations.ts";
3
+ import { maxNestingDepth, maxScalarElements } from "./layouts.ts";
4
+ import { reject } from "./normalization-error.ts";
5
+ import { instancedBlock } from "./types.ts";
6
+
7
+ export function checkStaticAllocation(
8
+ module: PlcModule,
9
+ declarations: ModuleDeclarations,
10
+ ): void {
11
+ const { types, blocks, layoutSizes } = declarations;
12
+
13
+ // Declaration admission closes these registries over every referenced type
14
+ // and block before allocation starts; a missing entry cannot be authored here.
15
+ // Static allocation is a separate graph from scan calls: even an unused child
16
+ // consumes memory, and a recursive child cannot be allocated by value.
17
+ const payload = (id: string): number => layoutSizes.get(id)!;
18
+
19
+ const instanceSize = (id: string): number => {
20
+ const type = types.get(id)!;
21
+ return type.kind === "array"
22
+ ? type.length * instanceSize(type.element)
23
+ : allocation(type.kind === "block" ? type.block : "").size;
24
+ };
25
+ const allocations = new Map<string, { size: number; height: number }>();
26
+ const allocating = new Set<string>();
27
+ const allocation = (id: string): { size: number; height: number } => {
28
+ if (allocating.size >= maxNestingDepth) {
29
+ reject(
30
+ `Static block allocation nesting exceeds ${maxNestingDepth} levels.`,
31
+ blocks.get(id)?.span,
32
+ );
33
+ }
34
+ if (allocating.has(id)) {
35
+ reject("Recursive static block allocation.", blocks.get(id)?.span);
36
+ }
37
+ const cached = allocations.get(id);
38
+ if (cached) {
39
+ return cached;
40
+ }
41
+ allocating.add(id);
42
+ const block = blocks.get(id)!;
43
+ const children = block.state
44
+ .filter((item) => item.storage === "instance")
45
+ .map((item) => ({
46
+ height: allocation(instancedBlock(item.type, types)!.block).height,
47
+ size: instanceSize(item.type),
48
+ }));
49
+ const own = [
50
+ ...block.inputs,
51
+ ...block.outputs,
52
+ ...block.state.filter((item) => item.storage !== "instance"),
53
+ ].reduce(
54
+ (sum, item) => sum + (item.mode === "inOut" ? 1 : payload(item.type)),
55
+ 1,
56
+ );
57
+ const size = own + children.reduce((sum, child) => sum + child.size, 0);
58
+ const height = children.length
59
+ ? 1 + Math.max(...children.map((child) => child.height))
60
+ : 1;
61
+ if (size > maxScalarElements || height > maxNestingDepth) {
62
+ reject("Static block allocation exceeds its finite budget.", block.span);
63
+ }
64
+ allocating.delete(id);
65
+ const result = { size, height };
66
+ allocations.set(id, result);
67
+ return result;
68
+ };
69
+
70
+ for (const block of module.blocks) {
71
+ allocation(block.id);
72
+ }
73
+ const staticPayload = module.variables.reduce((sum, variable) => {
74
+ return (
75
+ sum +
76
+ (instancedBlock(variable.type, types)
77
+ ? instanceSize(variable.type)
78
+ : payload(variable.type))
79
+ );
80
+ }, 0);
81
+ if (staticPayload > maxScalarElements) {
82
+ reject("Static module storage exceeds one million scalar elements.");
83
+ }
84
+ }
@@ -0,0 +1,146 @@
1
+ import type { SourceSpan } from "./diagnostics.ts";
2
+ import type {
3
+ Variable,
4
+ Constant,
5
+ BinaryOperator,
6
+ FunctionBlock,
7
+ PlcProgram,
8
+ PlcFunction,
9
+ PlcModule,
10
+ MethodOwner,
11
+ TelemetryCall,
12
+ } from "./model.ts";
13
+ import type { PlcType } from "./types.ts";
14
+
15
+ // Core expressions contain no source-level calls. Their effects have already
16
+ // become ordered statements, and every place has passed storage/bounds checks.
17
+ export type CorePath = readonly (
18
+ | { readonly kind: "field"; readonly name: string }
19
+ | { readonly kind: "index"; readonly value: CoreExpression }
20
+ )[];
21
+ export interface CheckedPlace {
22
+ readonly root: string;
23
+ readonly path: CorePath;
24
+ }
25
+ export type CoreExpression = {
26
+ readonly type: string;
27
+ readonly span: SourceSpan;
28
+ } & (
29
+ | { readonly kind: "borrow"; readonly place: CheckedPlace }
30
+ | { readonly kind: "constant"; readonly value: Constant }
31
+ | { readonly kind: "load"; readonly root: string; readonly path?: CorePath }
32
+ | {
33
+ readonly kind: "unary";
34
+ readonly operator:
35
+ "not" | "negate" | "bitNot" | "timeToMilliseconds" | "clock";
36
+ readonly operand: CoreExpression;
37
+ }
38
+ | {
39
+ readonly kind: "binary";
40
+ readonly operator: BinaryOperator;
41
+ readonly left: CoreExpression;
42
+ readonly right: CoreExpression;
43
+ readonly policy?: "wrap" | "saturate";
44
+ }
45
+ | {
46
+ readonly kind: "convert";
47
+ readonly operand: CoreExpression;
48
+ readonly policy: "exact" | "wrap" | "saturate";
49
+ }
50
+ );
51
+ export type CoreStatement = { readonly span: SourceSpan } & (
52
+ | {
53
+ readonly kind: "assign";
54
+ readonly root: string;
55
+ readonly path?: CorePath;
56
+ readonly value: CoreExpression;
57
+ }
58
+ | {
59
+ readonly kind: "if";
60
+ readonly condition: CoreExpression;
61
+ readonly then: readonly CoreStatement[];
62
+ readonly else: readonly CoreStatement[];
63
+ }
64
+ | {
65
+ readonly kind: "switch";
66
+ readonly value: CoreExpression;
67
+ readonly cases: readonly {
68
+ values: readonly Constant[];
69
+ body: readonly CoreStatement[];
70
+ }[];
71
+ readonly fallback: readonly CoreStatement[];
72
+ }
73
+ | {
74
+ readonly kind: "for";
75
+ readonly counter: string;
76
+ readonly counterScope?: "existing";
77
+ readonly start: Constant;
78
+ readonly limit: Constant;
79
+ readonly step: string;
80
+ readonly dynamic?: {
81
+ readonly start: CoreExpression;
82
+ readonly limit: CoreExpression;
83
+ readonly exclusive?: true;
84
+ };
85
+ readonly maxIterations: number;
86
+ readonly body: readonly CoreStatement[];
87
+ }
88
+ | {
89
+ readonly kind: "invoke";
90
+ readonly function: string;
91
+ readonly instance?: string;
92
+ readonly index?: CoreExpression;
93
+ readonly arguments: readonly CoreExpression[];
94
+ readonly destination?: string;
95
+ }
96
+ | {
97
+ readonly kind: "blockUpdate";
98
+ readonly instance: string;
99
+ readonly index?: CoreExpression;
100
+ readonly inputs: Readonly<Record<string, CoreExpression>>;
101
+ }
102
+ | { readonly kind: "return"; readonly value?: CoreExpression }
103
+ | { readonly kind: "break" | "continue" }
104
+ | ({ readonly kind: "telemetry" } & TelemetryCall<CoreExpression>)
105
+ );
106
+ /** The statement lists nested directly inside a statement. */
107
+ export const nestedBodies = (
108
+ item: CoreStatement,
109
+ ): readonly (readonly CoreStatement[])[] => {
110
+ if (item.kind === "if") {
111
+ return [item.then, item.else];
112
+ }
113
+ if (item.kind === "switch") {
114
+ return [...item.cases.map((branch) => branch.body), item.fallback];
115
+ }
116
+ if (item.kind === "for") {
117
+ return [item.body];
118
+ }
119
+ return [];
120
+ };
121
+ export interface CoreFunction {
122
+ readonly owner?: MethodOwner;
123
+ readonly origin?: PlcFunction["origin"];
124
+ readonly id: string;
125
+ readonly name: string;
126
+ readonly parameters: readonly Variable[];
127
+ readonly result?: string;
128
+ readonly locals: readonly Variable[];
129
+ readonly body: readonly CoreStatement[];
130
+ readonly span: SourceSpan;
131
+ }
132
+ export interface CoreFunctionBlock extends Readonly<
133
+ Omit<FunctionBlock, "body">
134
+ > {
135
+ readonly body: readonly CoreStatement[];
136
+ }
137
+ export const verified: unique symbol = Symbol("verified core");
138
+ export interface VerifiedCoreModule {
139
+ readonly [verified]: true;
140
+ readonly folded?: PlcModule["folded"];
141
+ readonly types: readonly PlcType[];
142
+ readonly variables: readonly Variable[];
143
+ readonly functions: readonly CoreFunction[];
144
+ readonly blocks: readonly CoreFunctionBlock[];
145
+ readonly programs: readonly PlcProgram[];
146
+ }