@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
@@ -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
- !variable.retain,
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 = Object.hasOwn(standardBlocks, standard)
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 && !this.isValueType(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
- !this.isValueType(result.id) ||
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 = false,
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.retain &&
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
- !this.isValueType(item.type) &&
251
+ !isValueType(item.type, this.types) &&
266
252
  !(allowBorrow && item.mode === "inOut" && type.kind === "block")
267
253
  ) {
268
254
  reject(
@@ -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
- let result = `${location}${diagnostic.code}: ${diagnostic.message}`;
44
- if (source !== undefined && span) {
45
- const line = source.split(/\r?\n/)[span.line - 1];
46
- if (line !== undefined) {
47
- const width = Math.max(
48
- 1,
49
- Math.min(span.end - span.start, line.length - span.column + 2),
50
- );
51
- result += `\n${span.line} | ${line}\n${" ".repeat(String(span.line).length + 3 + span.column - 1)}${"^".repeat(width)}`;
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 { elementaryType, integerRange } from "@tsplc/iec";
2
- import { type Constant, comparisonOperators, type Expression } from "./model";
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
- export const nanosecondsPerMillisecond = 1_000_000n;
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 <= latestMillisecond * nanosecondsPerMillisecond &&
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" && comparisonOperators.has(value.operator)) {
139
- const left = types.get(value.left.type);
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
- isElementary,
8
+ integerUnary,
9
9
  roundFloat,
10
10
  saturateFloat,
11
+ saturateFloatToInteger,
11
12
  } from "@tsplc/iec/arithmetic";
12
- import { nanosecondsPerMillisecond } from "./domains";
13
- import type { Constant, Expression, Variable } from "./model";
14
- import { integerType } from "./numeric";
15
- import type { PlcType } from "./types";
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> | undefined;
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?: ReadonlyMap<string, PlcType>,
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?.get(expression.type),
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?.get(expression.type),
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?.get(expression.type))
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?.get(expression.operand.type);
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
- expression: Extract<Expression, { kind: "unary" }>,
160
+ { operator }: Extract<Expression, { kind: "unary" }>,
150
161
  value: Constant | undefined,
151
162
  target: PlcType | undefined,
152
163
  ): Constant | undefined => {
153
- if (
154
- value?.kind === "duration" &&
155
- expression.operator === "timeToMilliseconds"
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" && expression.operator === "truncate") {
169
- return finiteFloat(Math.trunc(value.value), target);
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 === "bool" && expression.operator === "not") {
172
- return { kind: "bool", value: !value.value };
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
- value?.kind === "integer" &&
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
- if (value?.kind === "integer" && expression.operator === "negate") {
185
- return { kind: "integer", value: String(-BigInt(value.value)) };
182
+ const result = integerUnary(operator, BigInt(value.value));
183
+ if (result === undefined) {
184
+ return undefined;
186
185
  }
187
- if (value?.kind === "float" && expression.operator === "negate") {
188
- return { kind: "float", value: -value.value };
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 undefined;
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" }>;
@@ -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 { layoutIssue, maxNestingDepth, maxScalarElements } from "./layouts";
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 MathHelper,
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 { isVerifiedCore, normalizeModule } from "./normalize";
97
+ export { normalizeModule } from "./normalize";
78
98
  export {
79
99
  canAssign,
80
100
  constantFits,
101
+ exactIntegerLimit,
81
102
  type IntegerInterval,
82
- integerType,
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,
@@ -15,14 +15,22 @@ export const jobError = {
15
15
  codeChanged: 11001,
16
16
  } as const;
17
17
 
18
- /** An HTTP response fails a job with `base` + its status, up to `last`. */
19
- export const httpErrors = { base: 10000, last: 10999 };
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
- /** Job error codes, as every generated async block and service call reports them. */
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
- [httpErrors.base]: "HTTP status: 10000 + status code",
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
- if (code >= reservedErrors.first && code <= reservedErrors.last) {
44
- return jobErrors[code];
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 code < httpErrors.base ? jobErrors[code] : undefined;
68
+ return reason instanceof Error ? reason.message : String(reason);
47
69
  };