@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.
Files changed (263) hide show
  1. package/README.md +33 -8
  2. package/dist/cli.js +794 -621
  3. package/dist/cli.js.map +19 -16
  4. package/dist/clock.js +5 -2
  5. package/dist/clock.js.map +5 -4
  6. package/dist/codesys.js +4162 -4157
  7. package/dist/codesys.js.map +43 -29
  8. package/dist/index.js +7852 -6549
  9. package/dist/index.js.map +53 -50
  10. package/dist/ir.js +2482 -2277
  11. package/dist/ir.js.map +28 -25
  12. package/dist/retain.js +3 -1
  13. package/dist/retain.js.map +3 -3
  14. package/dist/signals.js +115 -0
  15. package/dist/signals.js.map +10 -0
  16. package/dist/telemetry.js +25 -20
  17. package/dist/telemetry.js.map +6 -6
  18. package/package.json +19 -39
  19. package/src/codesys/config.ts +3 -3
  20. package/src/codesys/debug.ts +92 -108
  21. package/src/codesys/emit.ts +100 -88
  22. package/src/codesys/endpoints.ts +111 -0
  23. package/src/codesys/engineering.ts +30 -54
  24. package/src/codesys/explain/expressions.ts +445 -0
  25. package/src/codesys/explain/flags.ts +134 -0
  26. package/src/codesys/explain/parameters.ts +53 -0
  27. package/src/codesys/explain/rows.ts +182 -0
  28. package/src/codesys/explain/tables.ts +79 -0
  29. package/src/codesys/explain/tasks.ts +68 -0
  30. package/src/codesys/explain/writes.ts +497 -0
  31. package/src/codesys/explain.ts +85 -1401
  32. package/src/codesys/expressions.ts +41 -33
  33. package/src/codesys/global-lists.ts +114 -94
  34. package/src/codesys/global-names.ts +12 -14
  35. package/src/codesys/http.ts +428 -388
  36. package/src/codesys/instrumentation.ts +142 -148
  37. package/src/codesys/io-paths.ts +31 -23
  38. package/src/codesys/loops.ts +101 -0
  39. package/src/codesys/manifest.ts +269 -269
  40. package/src/codesys/names.ts +10 -33
  41. package/src/codesys/numeric.ts +89 -42
  42. package/src/codesys/plan.ts +202 -190
  43. package/src/codesys/programs.ts +23 -16
  44. package/src/codesys/publication.ts +13 -86
  45. package/src/codesys/required.ts +8 -0
  46. package/src/codesys/routines.ts +103 -489
  47. package/src/codesys/scheduling.ts +96 -0
  48. package/src/codesys/service-channels.ts +23 -0
  49. package/src/codesys/sources.ts +34 -28
  50. package/src/codesys/st-files.ts +112 -105
  51. package/src/codesys/statements.ts +409 -0
  52. package/src/codesys/storage.ts +41 -23
  53. package/src/codesys/symbols.ts +130 -119
  54. package/src/codesys/task-access.ts +65 -221
  55. package/src/codesys/telemetry-settings.ts +19 -0
  56. package/src/codesys/telemetry.ts +185 -148
  57. package/src/codesys/types.ts +69 -109
  58. package/src/codesys/vendor.ts +13 -50
  59. package/src/frontend/analyzer.ts +66 -111
  60. package/src/frontend/async.ts +316 -128
  61. package/src/frontend/blocks.ts +178 -154
  62. package/src/frontend/configuration.ts +49 -153
  63. package/src/frontend/const-functions.ts +50 -40
  64. package/src/frontend/control-flow.ts +164 -129
  65. package/src/frontend/cooperative.ts +17 -9
  66. package/src/frontend/coroutines.ts +539 -441
  67. package/src/frontend/dialect-names.ts +104 -0
  68. package/src/frontend/elaborate/array-searches.ts +77 -69
  69. package/src/frontend/elaborate/assignments.ts +68 -77
  70. package/src/frontend/elaborate/calls.ts +878 -512
  71. package/src/frontend/elaborate/capture-protocol.ts +118 -21
  72. package/src/frontend/elaborate/capture.ts +196 -109
  73. package/src/frontend/elaborate/declarations.ts +124 -0
  74. package/src/frontend/elaborate/dialect.ts +119 -46
  75. package/src/frontend/elaborate/expressions.ts +763 -442
  76. package/src/frontend/elaborate/filtered-arrays.ts +127 -91
  77. package/src/frontend/elaborate/frame.ts +50 -2
  78. package/src/frontend/elaborate/generics.ts +20 -34
  79. package/src/frontend/elaborate/graph.ts +13 -87
  80. package/src/frontend/elaborate/imports.ts +0 -5
  81. package/src/frontend/elaborate/index.ts +416 -169
  82. package/src/frontend/elaborate/initializers.ts +64 -42
  83. package/src/frontend/elaborate/iterators.ts +228 -364
  84. package/src/frontend/elaborate/layouts.ts +477 -116
  85. package/src/frontend/elaborate/model.ts +113 -55
  86. package/src/frontend/elaborate/references.ts +116 -138
  87. package/src/frontend/elaborate/retention.ts +137 -59
  88. package/src/frontend/elaborate/specialize.ts +617 -308
  89. package/src/frontend/elaborate/statements.ts +436 -247
  90. package/src/frontend/elaborate/storage.ts +280 -213
  91. package/src/frontend/elaborate/time-constructors.ts +34 -0
  92. package/src/frontend/elaborate/values.ts +77 -7
  93. package/src/frontend/elaborate/writes.ts +98 -84
  94. package/src/frontend/expressions.ts +149 -151
  95. package/src/frontend/generated-names.ts +3 -3
  96. package/src/frontend/intrinsics.ts +31 -17
  97. package/src/frontend/lowering-key.ts +23 -0
  98. package/src/frontend/module-graph.ts +105 -43
  99. package/src/frontend/positions.ts +43 -35
  100. package/src/frontend/promise-chains.ts +226 -266
  101. package/src/frontend/promises.ts +88 -67
  102. package/src/frontend/routines.ts +122 -65
  103. package/src/frontend/sdk.ts +104 -5
  104. package/src/frontend/services.ts +149 -102
  105. package/src/frontend/statements.ts +60 -64
  106. package/src/frontend/tasks.ts +151 -96
  107. package/src/frontend/telemetry.ts +1 -1
  108. package/src/frontend/types.ts +366 -181
  109. package/src/frontend/typescript.ts +239 -112
  110. package/src/index.ts +2 -1
  111. package/src/ir/aggregates.ts +90 -75
  112. package/src/ir/allocation.ts +29 -18
  113. package/src/ir/conditions.ts +38 -78
  114. package/src/ir/core-model.ts +128 -11
  115. package/src/ir/declarations.ts +18 -32
  116. package/src/ir/diagnostics.ts +25 -18
  117. package/src/ir/domains.ts +26 -23
  118. package/src/ir/fold.ts +55 -50
  119. package/src/ir/identity.ts +10 -0
  120. package/src/ir/index.ts +31 -7
  121. package/src/ir/job-errors.ts +30 -8
  122. package/src/ir/layouts.ts +63 -34
  123. package/src/ir/link.ts +11 -46
  124. package/src/ir/model.ts +25 -6
  125. package/src/ir/normalize.ts +3 -28
  126. package/src/ir/numeric.ts +45 -18
  127. package/src/ir/operands.ts +102 -0
  128. package/src/ir/ranges.ts +242 -67
  129. package/src/ir/routine.ts +126 -183
  130. package/src/ir/scalars.ts +29 -25
  131. package/src/ir/share.ts +323 -343
  132. package/src/ir/short-circuit.ts +140 -0
  133. package/src/ir/tables.ts +111 -0
  134. package/src/ir/temporaries.ts +15 -113
  135. package/src/ir/types.ts +32 -8
  136. package/src/project/artifacts.ts +143 -71
  137. package/src/project/cli.ts +104 -87
  138. package/src/project/debugger.ts +48 -34
  139. package/src/project/deployments.ts +12 -68
  140. package/src/project/flags.ts +14 -0
  141. package/src/project/plant.ts +293 -225
  142. package/src/project/project-commands.ts +254 -192
  143. package/src/project/project.ts +184 -141
  144. package/src/project/release.ts +22 -28
  145. package/src/project/server.ts +22 -26
  146. package/src/project/terminal.ts +13 -2
  147. package/src/runtime/async.ts +17 -13
  148. package/src/runtime/launch.ts +5 -2
  149. package/src/runtime/main.ts +8 -3
  150. package/src/runtime/missing-file.ts +3 -0
  151. package/src/runtime/process.ts +24 -19
  152. package/src/runtime/program.ts +20 -53
  153. package/src/runtime/retain.ts +18 -10
  154. package/src/runtime/runtime.ts +47 -60
  155. package/src/runtime/settings.ts +27 -0
  156. package/src/runtime/storage.ts +49 -74
  157. package/src/runtime/symbols.ts +19 -75
  158. package/src/runtime/telemetry.ts +37 -14
  159. package/src/runtime/transport.ts +26 -30
  160. package/stdlib/clock.ts +4 -2
  161. package/stdlib/plc-time.ts +2 -0
  162. package/stdlib/retain.ts +2 -0
  163. package/stdlib/signals.ts +142 -0
  164. package/stdlib/telemetry.ts +5 -12
  165. package/types/dialect.d.ts +49 -0
  166. package/types/globals.d.ts +2 -42
  167. package/types/index.d.ts +3 -1
  168. package/types/retain.d.ts +8 -2
  169. package/types/telemetry.d.ts +3 -0
  170. package/dist/bun-runtime.js +0 -1472
  171. package/dist/bun-runtime.js.map +0 -26
  172. package/dist/elaborate-capture.js +0 -304
  173. package/dist/elaborate-capture.js.map +0 -11
  174. package/dist/math.js +0 -24
  175. package/dist/math.js.map +0 -10
  176. package/dist/project.js +0 -626
  177. package/dist/project.js.map +0 -12
  178. package/dist/server-check.js +0 -59
  179. package/dist/server-check.js.map +0 -11
  180. package/src/frontend/exposure.ts +0 -181
  181. package/src/project/index.ts +0 -6
  182. package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
  183. package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
  184. package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
  185. package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
  186. package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
  187. package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
  188. package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
  189. package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
  190. package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
  191. package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
  192. package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
  193. package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
  194. package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
  195. package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
  196. package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
  197. package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
  198. package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
  199. package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
  200. package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
  201. package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
  202. package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
  203. package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
  204. package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
  205. package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
  206. package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
  207. package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
  208. package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
  209. package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
  210. package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
  211. package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
  212. package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
  213. package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
  214. package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
  215. package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
  216. package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
  217. package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
  218. package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
  219. package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
  220. package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
  221. package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
  222. package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
  223. package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
  224. package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
  225. package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
  226. package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
  227. package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
  228. package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
  229. package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
  230. package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
  231. package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
  232. package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
  233. package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
  234. package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
  235. package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
  236. package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
  237. package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
  238. package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
  239. package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
  240. package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
  241. package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
  242. package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
  243. package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
  244. package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
  245. package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
  246. package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
  247. package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
  248. package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
  249. package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
  250. package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
  251. package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
  252. package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
  253. package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
  254. package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
  255. package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
  256. package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
  257. package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
  258. package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
  259. package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
  260. package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
  261. package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
  262. package/stdlib/math.ts +0 -23
  263. 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 { type BinaryOperator, roundFloat } from "@tsplc/iec/arithmetic";
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: Map<ts.Symbol, Variable>;
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 = valueRange(
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(): Map<string, Variable> {
233
- return new Map(
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
- if (
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
- return this.checked({
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
- ? floatConstant(text, target)
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: Record<string, Expression> = Object.create(null);
555
- const written = new Set<string>();
556
- for (const [position, property] of node.properties.entries()) {
557
- if (ts.isSpreadAssignment(property) && !position) {
558
- for (const [field, value] of this.spreadFields(
559
- property.expression,
560
- type.fields,
561
- )) {
562
- // TypeScript's spread shares a nested record or array, so a later write to it diverges.
563
- this.recordCopySource(value);
564
- fields[field.name] = this.implicitCopy(value);
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
- const field = type.fields.find((field) => field.name === named.name);
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 right = this.expression(
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
- return this.expression(node.left, this.expression(node.right).type);
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 wider = this.expression(node.right).type;
799
+ const right = this.expression(node.right);
826
800
  const from = this.resolver.types.get(left.type);
827
- const to = this.resolver.types.get(wider);
828
- return from && to && !canAssign(to, from) && canAssign(from, to)
829
- ? this.coerce(left, wider)
830
- : left;
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: Record<string, Expression> = Object.create(null);
869
- const written = new Set<string>();
870
- for (const [position, property] of record.properties.entries()) {
871
- if (ts.isSpreadAssignment(property) && !position) {
872
- for (const [parameter, value] of this.spreadFields(
873
- property.expression,
874
- definition.inputs,
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
- const parameter = definition.inputs.find(
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 fn.parameters.map((parameter, index) =>
1049
- this.inputArgument(node.arguments[index]!, parameter),
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
- target,
1181
- {
1182
- kind: "constant",
1183
- type: target.type,
1184
- value: value.fields[name]!,
1185
- span: base.span,
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(node, "PLC3002", issue ?? "This PLC storage is read-only.");
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 = relative(this.root, source.fileName).replace(/\\/g, "/");
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
- // TIME constructor scales in nanoseconds, and calendar constructor result types.
21
- const durationScales = new Map<string, bigint>([
22
- ["ms", 1_000_000n],
23
- ["seconds", 1_000_000_000n],
24
- ["minutes", 60_000_000_000n],
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
- const conversions = new Map<string, Conversion>([
32
- ["exact", "exact"],
33
- ["wrap", "wrap"],
34
- ["saturate", "saturate"],
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
- const conversion = conversions.get(name);
76
- if (conversion !== undefined) {
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
- if (text === undefined) {
152
- return undefined;
153
- }
154
- const decimal = /^-?0[xob]/i.test(text) ? String(parseInteger(text)) : text;
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) });