@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,124 @@
1
+ import ts from "typescript";
2
+ import { valueSymbol } from "../sdk";
3
+ import { SpecializeError, type Value } from "./values";
4
+
5
+ /** Whether `node` names something the JavaScript runtime provides: its library declares it, and the program doesn't. */
6
+ export const providedByRuntime = (
7
+ checker: ts.TypeChecker,
8
+ node: ts.Node,
9
+ ): boolean => {
10
+ const declaration = checker.getSymbolAtLocation(node)?.valueDeclaration;
11
+ return (
12
+ declaration !== undefined &&
13
+ (declaration.getSourceFile().isDeclarationFile ||
14
+ (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Ambient) !==
15
+ 0)
16
+ );
17
+ };
18
+
19
+ /** Whether `node` is the runtime's global `name`, such as `Math` or `structuredClone`, rather than one the program declares. */
20
+ export const isGlobal = (
21
+ checker: ts.TypeChecker,
22
+ node: ts.Expression,
23
+ name: string,
24
+ ): boolean =>
25
+ ts.isIdentifier(node) &&
26
+ node.text === name &&
27
+ providedByRuntime(checker, node);
28
+
29
+ /** The symbol a declared name binds. */
30
+ export const symbolOf = (
31
+ checker: ts.TypeChecker,
32
+ name: ts.Identifier,
33
+ ): ts.Symbol => {
34
+ const symbol = valueSymbol(checker, name);
35
+ if (!symbol) {
36
+ throw new SpecializeError(name, `${name.text} is not declared.`);
37
+ }
38
+ return symbol;
39
+ };
40
+
41
+ /** A value only the run decides is undefined or not. */
42
+ export type RunTimeOptional =
43
+ | Extract<Value, { kind: "choice" }>
44
+ | (Extract<Value, { kind: "code" }> & { declared: ts.Type });
45
+
46
+ /**
47
+ * `value` when only the run decides whether it is undefined: optional code, or a reference chosen
48
+ * at run time that may be none where TypeScript has not narrowed it to defined.
49
+ */
50
+ export const runTimeOptional = (
51
+ checker: ts.TypeChecker,
52
+ value: Value,
53
+ ): RunTimeOptional | undefined => {
54
+ if (value.kind === "choice") {
55
+ return value.optional && !value.narrowed ? value : undefined;
56
+ }
57
+ if (
58
+ value.kind !== "code" ||
59
+ !value.declared ||
60
+ !(value.declared.flags & ts.TypeFlags.Union) ||
61
+ checker.getNonNullableType(value.declared) === value.declared
62
+ ) {
63
+ return undefined;
64
+ }
65
+ return { ...value, declared: value.declared };
66
+ };
67
+
68
+ /**
69
+ * The default a parameter takes for `arg`: its initializer when the argument is missing or passed
70
+ * as undefined. A run-time argument that may be undefined is refused, since only the run would
71
+ * decide whether the default applies.
72
+ */
73
+ export const parameterDefault = (
74
+ checker: ts.TypeChecker,
75
+ arg: Value,
76
+ parameter: ts.ParameterDeclaration,
77
+ ): ts.Expression | undefined => {
78
+ if (!parameter.initializer) {
79
+ return undefined;
80
+ }
81
+ if (arg.kind === "const") {
82
+ return arg.value === undefined ? parameter.initializer : undefined;
83
+ }
84
+ if (runTimeOptional(checker, arg)) {
85
+ throw new SpecializeError(
86
+ parameter,
87
+ "Pass a defined value or leave the argument out: the default applies to a missing argument.",
88
+ );
89
+ }
90
+ return undefined;
91
+ };
92
+
93
+ /** The elements an array pattern names, each with the index it reads; a rest element is refused. */
94
+ export const namedElements = (
95
+ pattern: ts.ArrayBindingPattern,
96
+ declared: "parameter" | "pattern",
97
+ ): { element: ts.BindingElement; index: number }[] =>
98
+ pattern.elements.flatMap((element, index) => {
99
+ if (ts.isOmittedExpression(element)) {
100
+ return [];
101
+ }
102
+ if (element.dotDotDotToken) {
103
+ throw new SpecializeError(
104
+ element,
105
+ `Name each element of an array ${declared}; a rest element has no length here.`,
106
+ );
107
+ }
108
+ return [{ element, index }];
109
+ });
110
+
111
+ /** The elements an object pattern names, each with the field it reads; a rest element is refused. */
112
+ export const namedFields = (
113
+ pattern: ts.ObjectBindingPattern,
114
+ declared: "parameter" | "pattern",
115
+ ): { element: ts.BindingElement; field: string }[] =>
116
+ pattern.elements.map((element) => {
117
+ if (element.dotDotDotToken) {
118
+ throw new SpecializeError(
119
+ element,
120
+ `Name each field of an object ${declared}; a rest element has no record here.`,
121
+ );
122
+ }
123
+ return { element, field: (element.propertyName ?? element.name).getText() };
124
+ });
@@ -1,12 +1,22 @@
1
1
  import { canAssign, integerType, primitiveTypes } from "@tsplc/compiler/ir";
2
+ import { elementaryType } from "@tsplc/iec";
2
3
  import ts from "typescript";
3
4
  import { joined, type Mapped, mapped } from "../positions";
5
+ import { sdkModules } from "../sdk";
6
+ import { defaultNumericTypes } from "../types";
4
7
  import type { EntryImports } from "./imports";
5
8
  import type { Initializers } from "./initializers";
6
- import type { Layouts } from "./layouts";
9
+ import {
10
+ definedOptional,
11
+ emptyOptional,
12
+ type Layouts,
13
+ optionalHas,
14
+ optionalValue,
15
+ } from "./layouts";
7
16
  import type { Model } from "./model";
8
- import { referenceIndex, referenceType } from "./references";
17
+ import { holds, present, referenceIndex, referenceType } from "./references";
9
18
  import type { Storage } from "./storage";
19
+ import { calendarConstructor } from "./time-constructors";
10
20
  import type { Value } from "./values";
11
21
 
12
22
  /**
@@ -39,7 +49,7 @@ export class DialectText {
39
49
  this.layouts.isOptionalRecord(value.declared) &&
40
50
  !this.layouts.isOptionalRecord(expected)
41
51
  ) {
42
- return mapped`${value.text}.value`;
52
+ return optionalValue(value.text);
43
53
  }
44
54
  return value.text;
45
55
  case "const": {
@@ -55,23 +65,24 @@ export class DialectText {
55
65
  this.layouts.isOptionalRecord(expected) &&
56
66
  value.value === undefined
57
67
  ) {
58
- return `defaultValue<${this.layouts.text(expected)}>()`;
68
+ return this.emptyOptional(expected);
59
69
  }
60
- if (
61
- expected &&
62
- this.layouts.enumDeclaration(expected) &&
63
- typeof value.value === "number"
64
- ) {
65
- const member = this.layouts.enumMemberName(expected, value.value);
70
+ if (expected && typeof value.value === "number") {
71
+ const member = this.layouts.enumMember(expected, value.value);
66
72
  if (member) {
67
- return `${this.layouts.text(expected)}.${member}`;
73
+ return member;
74
+ }
75
+ // Setup's TIME is a number of milliseconds; the dialect spells a TIME constant as a duration.
76
+ if (expected.aliasSymbol?.name === "TIME") {
77
+ this.imports.addSdk(sdkModules.types, "ms");
78
+ return `ms(${value.value})`;
68
79
  }
69
80
  }
70
81
  if (value.value === undefined) {
71
82
  throw new Error("undefined has no PLC value here.");
72
83
  }
73
84
  if (typeof value.value === "string") {
74
- return JSON.stringify(value.value);
85
+ return this.stringText(value.value, expected);
75
86
  }
76
87
  if (typeof value.value === "bigint") {
77
88
  return `${value.value}n`;
@@ -129,7 +140,7 @@ export class DialectText {
129
140
  expected: ts.Type,
130
141
  ): string | Mapped {
131
142
  const copies = choice.options.map((option) => ({
132
- id: option.id,
143
+ test: holds(choice, option),
133
144
  text: this.code({ kind: "static", node: option }, expected),
134
145
  }));
135
146
  const last = copies.pop();
@@ -137,8 +148,7 @@ export class DialectText {
137
148
  throw new Error("A reference to no object has no PLC value.");
138
149
  }
139
150
  return copies.reduceRight<string | Mapped>(
140
- (otherwise, copy) =>
141
- mapped`(${choice.index} === ${copy.id} ? ${copy.text} : ${otherwise})`,
151
+ (otherwise, copy) => mapped`(${copy.test} ? ${copy.text} : ${otherwise})`,
142
152
  last.text,
143
153
  );
144
154
  }
@@ -167,7 +177,9 @@ export class DialectText {
167
177
  if (!value) {
168
178
  return `${name}: ${this.initializers.defaultOf(fieldType)}`;
169
179
  }
170
- const property = this.checker.getPropertyOfType(type, name);
180
+ const property = tagged
181
+ ? this.layouts.taggedField(type, tagged, name)
182
+ : this.checker.getPropertyOfType(type, name);
171
183
  // A record field holds exactly its type, so a narrower integer widens explicitly, as a result does.
172
184
  return mapped`${name}: ${this.returnText(value, fieldType, this.layouts.iecName(property?.valueDeclaration))}`;
173
185
  }),
@@ -184,9 +196,7 @@ export class DialectText {
184
196
  declared: ts.Type | undefined,
185
197
  iec: string | undefined,
186
198
  ): string | Mapped {
187
- const target =
188
- iec ??
189
- (declared && declared.flags & ts.TypeFlags.Number ? "DINT" : undefined);
199
+ const target = storedWidth(declared, iec);
190
200
  const from = value.kind === "code" ? value.iec : undefined;
191
201
  if (
192
202
  target &&
@@ -196,38 +206,57 @@ export class DialectText {
196
206
  integerTypes.has(target) &&
197
207
  !needsConversion(from, target)
198
208
  ) {
199
- this.imports.addSdk("@tsplc/compiler/types", "exact");
209
+ this.imports.addSdk(sdkModules.types, "exact");
200
210
  return mapped`exact<${target}>(${this.code(value, undefined)})`;
201
211
  }
202
212
  return this.assignmentText(value, declared, iec);
203
213
  }
204
214
 
215
+ /** Whether storing `value` into a place of `declared` type converts it, as IEC assignment narrows. */
216
+ converts(
217
+ value: Value,
218
+ declared: ts.Type | undefined,
219
+ iec: string | undefined,
220
+ ): boolean {
221
+ const target = storedWidth(declared, iec);
222
+ return (
223
+ target !== undefined &&
224
+ value.kind === "code" &&
225
+ needsConversion(value.iec ?? defaultNumericTypes.number, target)
226
+ );
227
+ }
228
+
205
229
  /** The text stored into a place of `declared` type, wrapping an optional's value. */
206
230
  assignmentText(
207
231
  value: Value,
208
232
  declared: ts.Type | undefined,
209
- iec?: string,
233
+ iec: string | undefined,
210
234
  ): string | Mapped {
211
235
  const index =
212
236
  declared && referenceType(declared) ? referenceIndex(value) : undefined;
213
237
  if (index !== undefined) {
214
238
  return index;
215
239
  }
216
- const target =
217
- iec ??
218
- (declared && declared.flags & ts.TypeFlags.Number ? "DINT" : undefined);
240
+ const target = storedWidth(declared, iec);
241
+ // A whole float fits an integer as TypeScript's number holds it, unless it is out of range.
242
+ const family = target && elementaryType(target)?.family;
219
243
  if (
220
244
  target &&
245
+ (family === "integer" || family === "bits") &&
221
246
  value.kind === "code" &&
222
- needsConversion(value.iec ?? "DINT", target)
247
+ value.whole
223
248
  ) {
249
+ this.imports.addSdk(sdkModules.types, "saturate");
250
+ return mapped`saturate<${target}>(${this.code(value, undefined)})`;
251
+ }
252
+ if (target && this.converts(value, declared, iec)) {
224
253
  // A TypeScript number holds any of these; in the PLC a value converts as an IEC assignment does.
225
- this.imports.addSdk("@tsplc/compiler/types", "wrap");
254
+ this.imports.addSdk(sdkModules.types, "wrap");
226
255
  return mapped`wrap<${target}>(${this.code(value, undefined)})`;
227
256
  }
228
257
  if (declared && this.layouts.isOptionalRecord(declared)) {
229
258
  if (value.kind === "const" && value.value === undefined) {
230
- return `defaultValue<${this.layouts.text(declared)}>()`;
259
+ return this.emptyOptional(declared);
231
260
  }
232
261
  if (
233
262
  value.kind === "code" &&
@@ -242,18 +271,28 @@ export class DialectText {
242
271
  );
243
272
  // A record chosen at run time may be none, which the optional says.
244
273
  return value.kind === "choice" && value.optional
245
- ? mapped`(${value.index} >= 0 ? ${defined} : defaultValue<${this.layouts.text(declared)}>())`
274
+ ? mapped`(${present(value.index)} ? ${defined} : ${this.emptyOptional(declared)})`
246
275
  : defined;
247
276
  }
248
277
  return this.code(value, declared);
249
278
  }
250
279
 
280
+ /**
281
+ * An optional of `declared` type holding nothing. A record literal takes the place's own optional
282
+ * record, whose value may have the IEC width the place's declaration names.
283
+ */
284
+ private emptyOptional(declared: ts.Type): Mapped {
285
+ return emptyOptional(
286
+ this.initializers.defaultOf(this.checker.getNonNullableType(declared)),
287
+ );
288
+ }
289
+
251
290
  wrapDefined(
252
291
  text: string | Mapped,
253
292
  declared: ts.Type | undefined,
254
293
  ): string | Mapped {
255
294
  return declared && this.layouts.isOptionalRecord(declared)
256
- ? mapped`{ has: true, value: ${text} }`
295
+ ? definedOptional(text)
257
296
  : text;
258
297
  }
259
298
 
@@ -262,7 +301,7 @@ export class DialectText {
262
301
  declared: ts.Type | undefined,
263
302
  ): Mapped {
264
303
  if (declared && this.layouts.isOptionalRecord(declared)) {
265
- return mapped`!${text}.has`;
304
+ return mapped`!${optionalHas(text)}`;
266
305
  }
267
306
  const literal = declared && this.layouts.literalEnum(declared);
268
307
  if (literal) {
@@ -274,7 +313,7 @@ export class DialectText {
274
313
  /** A boolean condition, including JavaScript truthiness of optionals and enums. */
275
314
  condition(value: Value, at: ts.Node): string | Mapped {
276
315
  if (value.kind === "choice") {
277
- return value.optional ? mapped`(${value.index} >= 0)` : "true";
316
+ return value.optional ? mapped`(${present(value.index)})` : "true";
278
317
  }
279
318
  if (value.kind === "code" && value.declared) {
280
319
  const declared = value.declared;
@@ -284,7 +323,7 @@ export class DialectText {
284
323
  ) {
285
324
  const type = this.checker.getTypeAtLocation(at);
286
325
  if (!(type.flags & ts.TypeFlags.BooleanLike)) {
287
- return mapped`${value.text}.has`;
326
+ return optionalHas(value.text);
288
327
  }
289
328
  }
290
329
  const literal = this.layouts.literalEnum(declared);
@@ -295,6 +334,16 @@ export class DialectText {
295
334
  return this.code(value, undefined);
296
335
  }
297
336
 
337
+ /** A string setup gave, as a calendar constant where a calendar type is expected. */
338
+ private stringText(text: string, expected: ts.Type | undefined): string {
339
+ const calendar = calendarConstructor(expected?.aliasSymbol?.name ?? "");
340
+ if (!calendar) {
341
+ return JSON.stringify(text);
342
+ }
343
+ this.imports.addSdk(sdkModules.types, calendar);
344
+ return `${calendar}(${JSON.stringify(text)})`;
345
+ }
346
+
298
347
  /**
299
348
  * Both sides of a comparison as text. Integers of different signedness, which TypeScript
300
349
  * compares as numbers, both widen to LINT so the PLC compares them exactly.
@@ -304,17 +353,24 @@ export class DialectText {
304
353
  right: Value,
305
354
  declared: ts.Type | undefined,
306
355
  ): [string | Mapped, string | Mapped] {
307
- const leftText = this.code(left, declared);
308
- const rightText = this.code(right, declared);
356
+ // A constant compared with code takes the code's type, as a calendar constant needs.
357
+ const leftText = this.code(
358
+ left,
359
+ right.kind === "code" ? (right.declared ?? declared) : declared,
360
+ );
361
+ const rightText = this.code(
362
+ right,
363
+ left.kind === "code" ? (left.declared ?? declared) : declared,
364
+ );
309
365
  if (left.kind !== "code" || right.kind !== "code") {
310
366
  return [leftText, rightText];
311
367
  }
312
- const from = left.iec ?? "DINT";
313
- const to = right.iec ?? "DINT";
368
+ const from = left.iec ?? defaultNumericTypes.number;
369
+ const to = right.iec ?? defaultNumericTypes.number;
314
370
  if (!needsConversion(from, to) || !needsConversion(to, from)) {
315
371
  return [leftText, rightText];
316
372
  }
317
- this.imports.addSdk("@tsplc/compiler/types", "wrap");
373
+ this.imports.addSdk(sdkModules.types, "wrap");
318
374
  return [mapped`wrap<LINT>(${leftText})`, mapped`wrap<LINT>(${rightText})`];
319
375
  }
320
376
 
@@ -336,22 +392,18 @@ export class DialectText {
336
392
  iec: operand.kind === "code" ? operand.iec : undefined,
337
393
  };
338
394
  }
339
- const from = left.iec ?? "DINT";
340
- const to = right.iec ?? "DINT";
395
+ const from = left.iec ?? defaultNumericTypes.number;
396
+ const to = right.iec ?? defaultNumericTypes.number;
341
397
  if (from === to || !integerTypes.has(from) || !integerTypes.has(to)) {
342
398
  return { texts: [leftText, rightText], iec: left.iec };
343
399
  }
344
- const common = !needsConversion(from, to)
345
- ? to
346
- : !needsConversion(to, from)
347
- ? from
348
- : "LINT";
349
- this.imports.addSdk("@tsplc/compiler/types", "exact");
400
+ const common = commonWidth(from, to);
401
+ this.imports.addSdk(sdkModules.types, "exact");
350
402
  const widened = (text: string | Mapped, width: string) =>
351
403
  width === common ? text : mapped`exact<${common}>(${text})`;
352
404
  return {
353
405
  texts: [widened(leftText, from), widened(rightText, to)],
354
- iec: common === "DINT" ? undefined : common,
406
+ iec: common === defaultNumericTypes.number ? undefined : common,
355
407
  };
356
408
  }
357
409
  }
@@ -361,6 +413,27 @@ const integerTypes = new Map(
361
413
  primitiveTypes.filter(integerType).map((type) => [type.id, type]),
362
414
  );
363
415
 
416
+ /** The IEC width a value is stored at: the one its place declares, or a TypeScript number's. */
417
+ const storedWidth = (
418
+ declared: ts.Type | undefined,
419
+ iec: string | undefined,
420
+ ): string | undefined =>
421
+ iec ??
422
+ (declared && declared.flags & ts.TypeFlags.Number
423
+ ? defaultNumericTypes.number
424
+ : undefined);
425
+
426
+ /** The width two integer widths widen to: the wider, or LINT when neither holds the other. */
427
+ const commonWidth = (from: string, to: string): string => {
428
+ if (!needsConversion(from, to)) {
429
+ return to;
430
+ }
431
+ if (!needsConversion(to, from)) {
432
+ return from;
433
+ }
434
+ return "LINT";
435
+ };
436
+
364
437
  /** Whether IEC needs an explicit conversion from one integer type to another; widening is implicit. */
365
438
  const needsConversion = (from: string, to: string): boolean => {
366
439
  const source = integerTypes.get(from);