@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
@@ -0,0 +1,104 @@
1
+ import { type Diagnostic, error } from "@tsplc/compiler/ir";
2
+ import ts from "typescript";
3
+ import { plcSources } from "./module-graph";
4
+ import type { Positions } from "./positions";
5
+ import { dialectFor, inSdk, sdkModules, unaliased } from "./sdk";
6
+
7
+ /** The SDK module of timers and edges, plain source a program imports like its own. */
8
+ const signals = "@tsplc/compiler/signals";
9
+
10
+ /** Dialect names a program has no use for, with what to write instead. */
11
+ const unsupported: Readonly<Record<string, string>> = {
12
+ FunctionBlock:
13
+ "write a plain class, whose instances keep their fields from one scan to the next",
14
+ Input:
15
+ "keep the input as a field of the program's state and bind its path in tsplc.config.ts io",
16
+ Output:
17
+ "keep the output as a field of the program's state and bind its path in tsplc.config.ts io",
18
+ InOut:
19
+ "pass the object that holds the value; a callee changes it through that object",
20
+ ref: "pass the object that holds the value; a callee changes it through that object",
21
+ OnDelayTimer: `import OnDelayTimer from ${signals}`,
22
+ OffDelayTimer: `import OffDelayTimer from ${signals}`,
23
+ PulseTimer: `import PulseTimer from ${signals}`,
24
+ RisingEdge: `import RisingEdge from ${signals}`,
25
+ FallingEdge: `import FallingEdge from ${signals}`,
26
+ Blinker:
27
+ "write it as a class, as the timers of @tsplc/compiler/signals are written",
28
+ UpCounter: "count in a field of a class",
29
+ DownCounter: "count in a field of a class",
30
+ UpDownCounter: "count in a field of a class",
31
+ };
32
+
33
+ /** The SDK's internal exports, which the compiler's own generated code calls. */
34
+ const internal = new Set(["cooperativeTasks", "opcuaCall"]);
35
+
36
+ /** The module a program imports a dialect global from, when it may use it at all. */
37
+ const importedFrom = (name: string): string =>
38
+ name === "retain" || name === "persist"
39
+ ? sdkModules.retain
40
+ : sdkModules.types;
41
+
42
+ /**
43
+ * Where a program's own source uses the tsPLC dialect: its function blocks, IO declarations,
44
+ * references and standard blocks, or a global that a plain program imports instead. The compiler
45
+ * lowers programs into the dialect; a program writes plain TypeScript.
46
+ */
47
+ export const dialectUses = (
48
+ program: ts.Program,
49
+ sdk: string,
50
+ positions: Positions,
51
+ ): Diagnostic[] => {
52
+ const checker = program.getTypeChecker();
53
+ const dialect = dialectFor(sdk);
54
+ const diagnostics: Diagnostic[] = [];
55
+ for (const source of plcSources(program)) {
56
+ if (inSdk(sdk, source.fileName)) {
57
+ continue;
58
+ }
59
+ const reported = new Set<string>();
60
+ const visit = (node: ts.Node): void => {
61
+ if (ts.isIdentifier(node) && !reported.has(node.text)) {
62
+ const message = dialectUse(checker, sdk, dialect, node);
63
+ if (message) {
64
+ reported.add(node.text);
65
+ diagnostics.push(error("PLC1015", message, positions.node(node)));
66
+ }
67
+ }
68
+ ts.forEachChild(node, visit);
69
+ };
70
+ visit(source);
71
+ }
72
+ return diagnostics;
73
+ };
74
+
75
+ const dialectUse = (
76
+ checker: ts.TypeChecker,
77
+ sdk: string,
78
+ dialect: string,
79
+ node: ts.Identifier,
80
+ ): string | undefined => {
81
+ const symbol = checker.getSymbolAtLocation(node);
82
+ const declaration = symbol?.declarations?.[0];
83
+ if (!symbol || !declaration) {
84
+ return undefined;
85
+ }
86
+ const ambient = declaration.getSourceFile().fileName === dialect;
87
+ const named = unaliased(checker, symbol).declarations?.[0];
88
+ const fromSdk =
89
+ ambient ||
90
+ (named !== undefined && inSdk(sdk, named.getSourceFile().fileName));
91
+ if (!fromSdk) {
92
+ return undefined;
93
+ }
94
+ const instead = unsupported[node.text];
95
+ if (instead) {
96
+ return `${node.text} is not part of PLC TypeScript: ${instead}.`;
97
+ }
98
+ if (internal.has(node.text)) {
99
+ return `${node.text} is the compiler's own; a program uses tasks, backgroundTasks and createServiceClient.`;
100
+ }
101
+ return ambient
102
+ ? `${node.text} is not a global: import it from ${importedFrom(node.text)}.`
103
+ : undefined;
104
+ };
@@ -1,6 +1,5 @@
1
1
  import type ts from "typescript";
2
- import { joined, type Mapped, mapped } from "../positions";
3
- import type { DialectText } from "./dialect";
2
+ import { mapped } from "../positions";
4
3
  import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
5
4
  import type { Frame } from "./frame";
6
5
  import { constant, SpecializeError, type Value } from "./values";
@@ -10,44 +9,13 @@ import { constant, SpecializeError, type Value } from "./values";
10
9
  * length, so each runs as one test or store per element, in the order JavaScript visits them.
11
10
  */
12
11
  export class ArraySearches {
13
- private readonly checker: ts.TypeChecker;
14
- private readonly dialect: DialectText;
15
-
16
12
  constructor(
17
- context: SpecializeContext,
18
- private readonly expressions: ExpressionSpecializer,
19
- ) {
20
- this.checker = context.checker;
21
- this.dialect = context.dialect;
22
- }
23
-
24
- /** `includes(target)`: whether any element equals it. */
25
- includes(
26
- items: readonly Value[],
27
- node: ts.CallExpression,
28
- frame: Frame,
29
- ): Value {
30
- const target = this.target(node, frame);
31
- const tests: (string | Mapped)[] = [];
32
- for (const item of items) {
33
- const test = this.expressions.equality(item, target, { negate: false });
34
- if (test.kind === "const") {
35
- if (test.value) {
36
- return constant(true);
37
- }
38
- continue;
39
- }
40
- tests.push(this.dialect.condition(test, node));
41
- }
42
- if (!tests.length) {
43
- return constant(false);
44
- }
45
- return {
46
- kind: "code",
47
- text: mapped`(${joined(tests, " || ")})`,
48
- declared: this.checker.getBooleanType(),
49
- };
50
- }
13
+ private readonly context: SpecializeContext,
14
+ private readonly expressions: Pick<
15
+ ExpressionSpecializer,
16
+ "expression" | "held" | "localType" | "equality" | "assignments"
17
+ >,
18
+ ) {}
51
19
 
52
20
  /** `indexOf(target)` and `lastIndexOf(target)`: the first or last index of an equal element, or -1. */
53
21
  indexOf(
@@ -56,7 +24,7 @@ export class ArraySearches {
56
24
  frame: Frame,
57
25
  { last }: { last: boolean },
58
26
  ): Value {
59
- const target = this.target(node, frame);
27
+ const target = searchTarget(node, frame, this.expressions);
60
28
  return this.firstIndex(items, frame, { last }, (item) =>
61
29
  this.expressions.equality(item, target, { negate: false }),
62
30
  );
@@ -74,7 +42,10 @@ export class ArraySearches {
74
42
  );
75
43
  }
76
44
 
77
- /** `find(test)` and `findLast(test)` over values: the element that passed, or undefined. */
45
+ /**
46
+ * `find(test)` and `findLast(test)`: the element that passed, or undefined. Among objects setup
47
+ * built the one that passes is chosen now; among values, at run time.
48
+ */
78
49
  find(
79
50
  items: readonly Value[],
80
51
  node: ts.CallExpression,
@@ -82,29 +53,29 @@ export class ArraySearches {
82
53
  { last }: { last: boolean },
83
54
  apply: (args: Value[]) => Value,
84
55
  ): Value {
85
- const declared = this.checker.getTypeAtLocation(node);
86
- const element = this.checker.getNonNullableType(declared);
56
+ if (
57
+ items.some((item) => item.kind === "static" || item.kind === "function")
58
+ ) {
59
+ return decidedFind(items, node, { last }, apply);
60
+ }
61
+ const { checker, dialect } = this.context;
62
+ const declared = checker.getTypeAtLocation(node);
63
+ const element = checker.getNonNullableType(declared);
87
64
  const found = frame.emitter.temporary();
88
65
  const result = frame.emitter.temporary();
89
66
  frame.emitter.line(`let ${found}: boolean = false;`);
90
67
  frame.emitter.line(
91
- mapped`let ${result}: ${this.expressions.localType(declared)} = ${this.dialect.assignmentText(constant(undefined), declared)};`,
68
+ mapped`let ${result}: ${this.expressions.localType(declared)} = ${dialect.assignmentText(constant(undefined), declared, undefined)};`,
92
69
  );
93
70
  for (const [index, item] of ordered(items, { last })) {
94
- if (item.kind === "static" || item.kind === "function") {
95
- throw new SpecializeError(
96
- node,
97
- "find picks one of the objects setup built, which scan code can't hold; use findIndex and index the array.",
98
- );
99
- }
100
71
  frame.emitter.open(`if (!${found}) {`);
101
72
  const test = apply([item, constant(index)]);
102
73
  if (test.kind !== "const" || test.value) {
103
74
  const passed =
104
- test.kind === "const" ? "true" : this.dialect.condition(test, node);
75
+ test.kind === "const" ? "true" : dialect.condition(test, node);
105
76
  frame.emitter.open(mapped`if (${passed}) {`);
106
77
  frame.emitter.line(
107
- mapped`${result} = ${this.dialect.wrapDefined(this.dialect.code(item, element), declared)};`,
78
+ mapped`${result} = ${dialect.wrapDefined(dialect.code(item, element), declared)};`,
108
79
  );
109
80
  frame.emitter.line(`${found} = true;`);
110
81
  frame.emitter.close();
@@ -166,7 +137,7 @@ export class ArraySearches {
166
137
  }
167
138
  } else {
168
139
  frame.emitter.line(
169
- mapped`if (${this.dialect.condition(passed, frame.emitter.origin)}) { ${result} = ${index}; }`,
140
+ mapped`if (${this.context.dialect.condition(passed, frame.emitter.origin)}) { ${result} = ${index}; }`,
170
141
  );
171
142
  }
172
143
  frame.emitter.close();
@@ -174,22 +145,11 @@ export class ArraySearches {
174
145
  return {
175
146
  kind: "code",
176
147
  text: result,
177
- declared: this.checker.getNumberType(),
148
+ declared: this.context.checker.getNumberType(),
178
149
  place: true,
179
150
  };
180
151
  }
181
152
 
182
- private target(node: ts.CallExpression, frame: Frame): Value {
183
- const [argument] = node.arguments;
184
- if (!argument) {
185
- throw new SpecializeError(node, "Pass the value to look for.");
186
- }
187
- return this.expressions.held(
188
- this.expressions.expression(argument, frame),
189
- frame,
190
- );
191
- }
192
-
193
153
  private constantIndex(
194
154
  node: ts.Expression,
195
155
  length: number,
@@ -206,11 +166,59 @@ export class ArraySearches {
206
166
  }
207
167
  }
208
168
 
209
- /** Elements with their indexes, first to last or, for the `last` searches, last to first. */
210
- const ordered = (
211
- items: readonly Value[],
169
+ /** Elements with their indexes, first to last or, for a search from the end, last to first. */
170
+ export const ordered = <T>(
171
+ items: readonly T[],
212
172
  { last }: { last: boolean },
213
- ): [number, Value][] => {
173
+ ): [number, T][] => {
214
174
  const indexed = [...items.entries()];
215
175
  return last ? indexed.reverse() : indexed;
216
176
  };
177
+
178
+ /** An array method's callback: its first argument, the test or step it runs on elements. */
179
+ export const callbackArgument = (
180
+ node: ts.CallExpression,
181
+ frame: Frame,
182
+ expressions: Pick<ExpressionSpecializer, "expression">,
183
+ ): Value => {
184
+ const [argument] = node.arguments;
185
+ if (!argument) {
186
+ throw new SpecializeError(node, "Pass a callback.");
187
+ }
188
+ return expressions.expression(argument, frame);
189
+ };
190
+
191
+ /** What `includes` and `indexOf` look for, held so each element is compared with the value it had at the call. */
192
+ export const searchTarget = (
193
+ node: ts.CallExpression,
194
+ frame: Frame,
195
+ expressions: Pick<ExpressionSpecializer, "expression" | "held">,
196
+ ): Value => {
197
+ const [argument] = node.arguments;
198
+ if (!argument) {
199
+ throw new SpecializeError(node, "Pass the value to look for.");
200
+ }
201
+ return expressions.held(expressions.expression(argument, frame), frame);
202
+ };
203
+
204
+ /** `find` and `findLast` among objects setup built, whose tests are known now: the object that passes, or undefined. */
205
+ const decidedFind = (
206
+ items: readonly Value[],
207
+ node: ts.CallExpression,
208
+ { last }: { last: boolean },
209
+ apply: (args: Value[]) => Value,
210
+ ): Value => {
211
+ for (const [index, item] of ordered(items, { last })) {
212
+ const test = apply([item, constant(index)]);
213
+ if (test.kind !== "const") {
214
+ throw new SpecializeError(
215
+ node,
216
+ "find picks one of the objects setup built, which scan code can't hold; use findIndex and index the array.",
217
+ );
218
+ }
219
+ if (test.value) {
220
+ return item;
221
+ }
222
+ }
223
+ return constant(undefined);
224
+ };
@@ -4,7 +4,7 @@ import type { DialectText } from "./dialect";
4
4
  import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
5
5
  import type { Frame } from "./frame";
6
6
  import { constant, SpecializeError, type Value } from "./values";
7
- import { isAssignment, isUpdate } from "./writes";
7
+ import { isAssignment, isUpdate, occursOutsideFunctions } from "./writes";
8
8
 
9
9
  /** The operator each compound assignment applies. */
10
10
  const compoundOperators = new Map<ts.SyntaxKind, string>([
@@ -17,24 +17,34 @@ const compoundOperators = new Map<ts.SyntaxKind, string>([
17
17
 
18
18
  /** Whether evaluating the expression writes a place: an assignment or `++`/`--` in it, outside functions it declares. */
19
19
  export const containsWrite = (node: ts.Node): boolean =>
20
- (ts.isBinaryExpression(node) && isAssignment(node.operatorToken.kind)) ||
21
- isUpdate(node) ||
22
- (!ts.isFunctionLike(node) && (ts.forEachChild(node, containsWrite) ?? false));
20
+ occursOutsideFunctions(
21
+ node,
22
+ (inner) =>
23
+ (ts.isBinaryExpression(inner) &&
24
+ isAssignment(inner.operatorToken.kind)) ||
25
+ isUpdate(inner),
26
+ );
27
+
28
+ /** A statement storing `value` into `place`. */
29
+ export const storeStatement = (place: Part, value: Part): Mapped =>
30
+ mapped`${place} = ${value};`;
23
31
 
24
32
  /**
25
33
  * Assignments and steps. Structured Text assigns only in statements, so one used as a value is a
26
34
  * statement first, and the expression reads the place afterwards.
27
35
  */
28
36
  export class AssignmentSpecializer {
29
- private readonly checker: ts.TypeChecker;
30
- private readonly dialect: DialectText;
31
-
32
37
  constructor(
33
- context: SpecializeContext,
38
+ private readonly context: SpecializeContext,
34
39
  private readonly expressions: ExpressionSpecializer,
35
- ) {
36
- this.checker = context.checker;
37
- this.dialect = context.dialect;
40
+ ) {}
41
+
42
+ private get checker(): ts.TypeChecker {
43
+ return this.context.checker;
44
+ }
45
+
46
+ private get dialect(): DialectText {
47
+ return this.context.dialect;
38
48
  }
39
49
 
40
50
  /** Stores the assignment and returns what the target then holds. */
@@ -94,16 +104,7 @@ export class AssignmentSpecializer {
94
104
  }
95
105
  const value = this.expressions.expression(node.right, frame, declared);
96
106
  if (operator === ts.SyntaxKind.EqualsToken) {
97
- frame.emitter.line(
98
- this.store(
99
- target,
100
- this.dialect.assignmentText(
101
- value,
102
- declared,
103
- target.kind === "code" ? target.iec : undefined,
104
- ),
105
- ),
106
- );
107
+ frame.emitter.line(this.stored(target, value));
107
108
  return target;
108
109
  }
109
110
  const binary = compoundOperators.get(operator);
@@ -209,14 +210,16 @@ export class AssignmentSpecializer {
209
210
  property,
210
211
  frame,
211
212
  );
212
- const target = property.objectAssignmentInitializer
213
- ? this.withDefault(
214
- part,
215
- property.objectAssignmentInitializer,
216
- frame,
217
- )
218
- : part;
219
- this.collectTarget(property.name, target, frame, writes);
213
+ this.collectTarget(
214
+ property.name,
215
+ this.expressions.defaulted(
216
+ part,
217
+ property.objectAssignmentInitializer,
218
+ frame,
219
+ ),
220
+ frame,
221
+ writes,
222
+ );
220
223
  continue;
221
224
  }
222
225
  if (!ts.isPropertyAssignment(property)) {
@@ -255,7 +258,7 @@ export class AssignmentSpecializer {
255
258
  ) {
256
259
  this.collectTarget(
257
260
  target.left,
258
- this.withDefault(part, target.right, frame),
261
+ this.expressions.defaulted(part, target.right, frame),
259
262
  frame,
260
263
  writes,
261
264
  );
@@ -270,68 +273,39 @@ export class AssignmentSpecializer {
270
273
  }
271
274
  const read = this.expressions.held(part, frame);
272
275
  writes.push(() => {
273
- const place = this.expressions.expression(target, frame);
274
- const declared = place.kind === "code" ? place.declared : undefined;
275
- frame.emitter.line(
276
- this.store(
277
- place,
278
- this.dialect.assignmentText(
279
- read,
280
- declared,
281
- place.kind === "code" ? place.iec : undefined,
282
- ),
283
- ),
276
+ this.storeValue(
277
+ this.expressions.expression(target, frame),
278
+ read,
279
+ target,
280
+ frame,
284
281
  );
285
282
  });
286
283
  }
287
284
 
288
- private withDefault(
289
- part: Value,
290
- fallback: ts.Expression,
291
- frame: Frame,
292
- ): Value {
293
- if (part.kind === "const" && part.value === undefined) {
294
- return this.expressions.expression(fallback, frame);
295
- }
296
- if (part.kind !== "code") {
297
- return part;
298
- }
299
- const declared = part.declared;
300
- if (!declared || !(declared.flags & ts.TypeFlags.Union)) {
301
- return part;
302
- }
303
- const defined = this.checker.getNonNullableType(declared);
304
- const otherwise = this.expressions.expression(fallback, frame, defined);
305
- return {
306
- kind: "code",
307
- text: mapped`(${this.dialect.isUndefinedText(part.text, declared)} ? ${this.dialect.code(otherwise, defined)} : ${this.dialect.code(part, undefined)}${this.expressions.optionalValueSuffix(declared)})`,
308
- declared: defined,
309
- };
310
- }
311
-
312
285
  /** Stores a value into a place, converting it as an IEC assignment does. */
313
286
  storeValue(place: Value, value: Value, at: ts.Node, frame: Frame): void {
314
287
  this.placeText(place, at);
315
- frame.emitter.line(
316
- this.store(
317
- place,
318
- this.dialect.assignmentText(
319
- value,
320
- place.kind === "code" ? place.declared : undefined,
321
- place.kind === "code" ? place.iec : undefined,
322
- ),
288
+ frame.emitter.line(this.stored(place, value));
289
+ }
290
+
291
+ /** A statement storing a value into a place, converted as an IEC assignment does. */
292
+ private stored(place: Value, value: Value): Mapped {
293
+ return this.store(
294
+ place,
295
+ this.dialect.assignmentText(
296
+ value,
297
+ place.kind === "code" ? place.declared : undefined,
298
+ place.kind === "code" ? place.iec : undefined,
323
299
  ),
324
300
  );
325
301
  }
326
302
 
327
- /** A statement storing `text` into a place, through its module's setter when it has one. */
303
+ /** A statement storing `text` into a place. */
328
304
  private store(target: Value, text: Part): Mapped {
329
305
  if (target.kind !== "code") {
330
306
  throw new Error("Not a place.");
331
307
  }
332
- return target.setter
333
- ? mapped`${target.setter}(${text});`
334
- : mapped`${target.text} = ${text};`;
308
+ return storeStatement(target.text, text);
335
309
  }
336
310
 
337
311
  private placeText(value: Value, node: ts.Node): string | Mapped {
@@ -341,9 +315,26 @@ export class AssignmentSpecializer {
341
315
  `This changes a copy: ${value.copyOf} returns stored state, which TypeScript would change in place but the PLC copies. Change it where it is stored, as a collection's update does.`,
342
316
  );
343
317
  }
318
+ if (value.kind === "code" && value.input) {
319
+ throw new SpecializeError(
320
+ node,
321
+ `${value.input.path} is bound to ${value.input.address}, and the hardware overwrites it every scan: keep what scan code computes in a field of its own.`,
322
+ );
323
+ }
344
324
  if (value.kind === "code" && value.place) {
345
325
  return value.text;
346
326
  }
327
+ if (
328
+ value.kind === "function" ||
329
+ (value.kind === "static" &&
330
+ (value.node.captured.kind === "closure" ||
331
+ value.node.captured.kind === "function"))
332
+ ) {
333
+ throw new SpecializeError(
334
+ node,
335
+ "This assigns a function, and the PLC calls only functions known when the program compiles: keep one function, and choose what it does with an if.",
336
+ );
337
+ }
347
338
  throw new SpecializeError(
348
339
  node,
349
340
  "Assign to stored state: a field scan code owns, or a let.",