@tsplc/compiler 0.2.17 → 0.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.js +920 -943
  3. package/dist/cli.js.map +13 -10
  4. package/dist/codesys.js +3893 -3352
  5. package/dist/codesys.js.map +29 -20
  6. package/dist/elaborate-capture.js +33 -17
  7. package/dist/elaborate-capture.js.map +3 -3
  8. package/dist/index.js +8787 -6855
  9. package/dist/index.js.map +40 -20
  10. package/dist/ir.js +1575 -1072
  11. package/dist/ir.js.map +21 -17
  12. package/dist/math.js.map +2 -2
  13. package/dist/project.js +154 -60
  14. package/dist/project.js.map +5 -5
  15. package/dist/telemetry.js +27 -9
  16. package/dist/telemetry.js.map +5 -5
  17. package/package.json +112 -112
  18. package/src/codesys/config.ts +50 -0
  19. package/src/codesys/debug.ts +670 -0
  20. package/src/codesys/emit.ts +142 -0
  21. package/src/codesys/engineering.ts +109 -0
  22. package/src/codesys/expressions.ts +92 -0
  23. package/src/codesys/global-lists.ts +250 -0
  24. package/src/codesys/global-names.ts +68 -0
  25. package/src/codesys/http.ts +890 -0
  26. package/src/codesys/index.ts +30 -0
  27. package/src/codesys/instrumentation.ts +430 -0
  28. package/src/codesys/io-paths.ts +140 -0
  29. package/src/codesys/manifest.ts +643 -0
  30. package/src/codesys/names.ts +81 -0
  31. package/src/codesys/numeric.ts +409 -0
  32. package/src/codesys/operands.ts +62 -0
  33. package/src/codesys/plan.ts +605 -0
  34. package/src/codesys/programs.ts +53 -0
  35. package/src/codesys/publication.ts +171 -0
  36. package/src/codesys/routines.ts +603 -0
  37. package/src/codesys/sources.ts +71 -0
  38. package/src/codesys/st-files.ts +115 -0
  39. package/src/codesys/storage.ts +97 -0
  40. package/src/codesys/symbols.ts +220 -0
  41. package/src/codesys/task-access.ts +320 -0
  42. package/src/codesys/telemetry.ts +732 -0
  43. package/src/codesys/types.ts +250 -0
  44. package/src/codesys/vendor.ts +150 -0
  45. package/src/frontend/analyzer.ts +794 -0
  46. package/src/frontend/async.ts +317 -1137
  47. package/src/frontend/blocks.ts +537 -403
  48. package/src/frontend/configuration.ts +176 -144
  49. package/src/frontend/const-functions.ts +183 -148
  50. package/src/frontend/cooperative.ts +73 -43
  51. package/src/frontend/coroutines.ts +1032 -0
  52. package/src/frontend/elaborate/calls.ts +535 -0
  53. package/src/frontend/elaborate/capture.ts +157 -101
  54. package/src/frontend/elaborate/dialect.ts +257 -0
  55. package/src/frontend/elaborate/expressions.ts +992 -0
  56. package/src/frontend/elaborate/frame.ts +53 -0
  57. package/src/frontend/elaborate/graph.ts +116 -71
  58. package/src/frontend/elaborate/imports.ts +70 -0
  59. package/src/frontend/elaborate/index.ts +435 -331
  60. package/src/frontend/elaborate/initializers.ts +198 -0
  61. package/src/frontend/elaborate/layouts.ts +591 -384
  62. package/src/frontend/elaborate/model.ts +264 -169
  63. package/src/frontend/elaborate/specialize.ts +343 -2586
  64. package/src/frontend/elaborate/statements.ts +606 -0
  65. package/src/frontend/elaborate/storage.ts +546 -0
  66. package/src/frontend/elaborate/values.ts +58 -0
  67. package/src/frontend/elaborate/writes.ts +100 -0
  68. package/src/frontend/exposure.ts +172 -140
  69. package/src/frontend/expressions.ts +1383 -0
  70. package/src/frontend/intrinsics.ts +337 -0
  71. package/src/frontend/module-graph.ts +89 -59
  72. package/src/frontend/positions.ts +271 -200
  73. package/src/frontend/promise-chains.ts +897 -0
  74. package/src/frontend/promises.ts +390 -747
  75. package/src/frontend/routines.ts +181 -0
  76. package/src/frontend/sdk.ts +82 -0
  77. package/src/frontend/services.ts +506 -372
  78. package/src/frontend/statements.ts +855 -0
  79. package/src/frontend/tasks.ts +471 -0
  80. package/src/frontend/telemetry.ts +174 -0
  81. package/src/frontend/types.ts +845 -551
  82. package/src/frontend/typescript.ts +323 -2543
  83. package/src/index.ts +5 -5
  84. package/src/ir/aggregates.ts +275 -0
  85. package/src/ir/allocation.ts +84 -0
  86. package/src/ir/core-model.ts +146 -0
  87. package/src/ir/declarations.ts +295 -0
  88. package/src/ir/diagnostics.ts +61 -0
  89. package/src/ir/domains.ts +152 -0
  90. package/src/ir/fold.ts +342 -0
  91. package/src/ir/index.ts +90 -0
  92. package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
  93. package/src/ir/layouts.ts +181 -0
  94. package/src/ir/link.ts +354 -0
  95. package/src/ir/model.ts +386 -0
  96. package/{ir → src/ir}/normalization-error.ts +4 -4
  97. package/src/ir/normalize.ts +70 -0
  98. package/src/ir/numeric.ts +171 -0
  99. package/src/ir/ranges.ts +256 -0
  100. package/src/ir/routine.ts +1163 -0
  101. package/src/ir/scalars.ts +321 -0
  102. package/src/ir/types.ts +127 -0
  103. package/src/project/artifacts.ts +342 -0
  104. package/src/project/cli.ts +177 -0
  105. package/src/project/debugger.ts +82 -0
  106. package/src/project/index.ts +6 -0
  107. package/src/project/plant.ts +467 -0
  108. package/src/project/project-commands.ts +372 -0
  109. package/src/project/project.ts +457 -0
  110. package/src/project/release.ts +153 -0
  111. package/src/project/server.ts +150 -0
  112. package/src/project/terminal.ts +32 -0
  113. package/stdlib/math.ts +14 -10
  114. package/stdlib/telemetry.ts +71 -47
  115. package/types/async.d.ts +46 -40
  116. package/types/data-types.d.ts +26 -26
  117. package/types/globals.d.ts +41 -38
  118. package/types/index.d.ts +96 -51
  119. package/types/plc.d.ts +3 -4
  120. package/types/telemetry.d.ts +14 -14
  121. package/backends/codesys/config.ts +0 -54
  122. package/backends/codesys/debug.ts +0 -509
  123. package/backends/codesys/emit.ts +0 -1738
  124. package/backends/codesys/engineering.ts +0 -88
  125. package/backends/codesys/global-names.ts +0 -49
  126. package/backends/codesys/http.ts +0 -675
  127. package/backends/codesys/index.ts +0 -13
  128. package/backends/codesys/io-paths.ts +0 -105
  129. package/backends/codesys/manifest.ts +0 -312
  130. package/backends/codesys/names.ts +0 -76
  131. package/backends/codesys/numeric.ts +0 -356
  132. package/backends/codesys/plan.ts +0 -457
  133. package/backends/codesys/publication.ts +0 -59
  134. package/backends/codesys/storage.ts +0 -70
  135. package/backends/codesys/task-access.ts +0 -293
  136. package/backends/codesys/telemetry.ts +0 -593
  137. package/backends/codesys/types.ts +0 -179
  138. package/backends/codesys/vendor.ts +0 -108
  139. package/ir/aggregates.ts +0 -333
  140. package/ir/allocation.ts +0 -74
  141. package/ir/core-model.ts +0 -129
  142. package/ir/core.ts +0 -64
  143. package/ir/declarations.ts +0 -208
  144. package/ir/diagnostics.ts +0 -43
  145. package/ir/domains.ts +0 -96
  146. package/ir/index.ts +0 -8
  147. package/ir/link.ts +0 -185
  148. package/ir/model.ts +0 -346
  149. package/ir/routine.ts +0 -859
  150. package/ir/scalars.ts +0 -675
  151. package/ir/types.ts +0 -76
  152. package/project/artifacts.ts +0 -277
  153. package/project/cli.ts +0 -395
  154. package/project/index.ts +0 -1
  155. package/project/plant.ts +0 -509
  156. package/project/project.ts +0 -358
  157. package/project/server.ts +0 -113
@@ -0,0 +1,372 @@
1
+ import { resolve } from "node:path";
2
+ import {
3
+ buildTargetFor,
4
+ controllerFor,
5
+ type Environment,
6
+ loadTsplcConfig,
7
+ rawBuildConfig,
8
+ } from "@tsplc/config";
9
+ import {
10
+ buildProject as buildSt,
11
+ configureDocker,
12
+ configureRuntime,
13
+ inspectController,
14
+ installLibraries,
15
+ loadSources,
16
+ rawEngineeringSettings,
17
+ resolveConfig,
18
+ runProject,
19
+ updateController,
20
+ } from "@tsplc/build";
21
+ import { checkHostConfig } from "@tsplc/compiler";
22
+ import { type Diagnostic, hasErrors } from "@tsplc/compiler/ir";
23
+ import { generateProject } from "@tsplc/typegen";
24
+ import { loadHardware, resolveIo } from "@tsplc/typegen/hardware";
25
+ import {
26
+ buildConfigs,
27
+ buildProject,
28
+ checkProject,
29
+ cleanProject,
30
+ projectTargets,
31
+ } from "./project.ts";
32
+ import { buildServer } from "./server.ts";
33
+
34
+ const projectCommands = [
35
+ "install",
36
+ "typegen",
37
+ "check",
38
+ "build",
39
+ "run",
40
+ "download",
41
+ "online-change",
42
+ "inspect",
43
+ "clean",
44
+ "configure",
45
+ ] as const;
46
+
47
+ export type ProjectCommand = (typeof projectCommands)[number];
48
+
49
+ export const projectCommand = (name: string) =>
50
+ projectCommands.find((command) => command === name);
51
+
52
+ export interface ProjectFlags {
53
+ "emit-only"?: boolean;
54
+ format?: string;
55
+ read?: string[];
56
+ "initialize-user"?: boolean;
57
+ "frozen-lockfile"?: boolean;
58
+ check?: boolean;
59
+ plc?: boolean;
60
+ server?: boolean;
61
+ simulate?: boolean;
62
+ runtime?: boolean;
63
+ target?: string;
64
+ }
65
+
66
+ /** Every command result; one whose diagnostics have errors fails the command. */
67
+ export type CommandResult = object & { diagnostics?: readonly Diagnostic[] };
68
+
69
+ /** A result whose diagnostics fail the command; the CLI reports the whole result. */
70
+ export class DiagnosticFailure extends Error {
71
+ constructor(readonly result: CommandResult) {
72
+ super("PLC validation failed.");
73
+ }
74
+ }
75
+
76
+ type LoadedConfig = Awaited<ReturnType<typeof loadTsplcConfig>>;
77
+
78
+ /** Rejects the flags that don't apply to `command`. */
79
+ export const checkProjectFlags = (
80
+ command: ProjectCommand,
81
+ flags: ProjectFlags,
82
+ ) => {
83
+ if (flags.format && !["json", "text"].includes(flags.format)) {
84
+ throw new Error("--format must be json or text.");
85
+ }
86
+ if (flags["emit-only"] && command !== "build") {
87
+ throw new Error("--emit-only is supported by build only.");
88
+ }
89
+ if (flags.target && (command === "configure" || command === "clean")) {
90
+ throw new Error("--target applies only to commands that read the config.");
91
+ }
92
+ if (flags.runtime && command !== "configure") {
93
+ throw new Error("--runtime applies only to configure.");
94
+ }
95
+ if ((flags.plc || flags.server) && command !== "build") {
96
+ throw new Error("--plc and --server apply only to build.");
97
+ }
98
+ // deploy takes --simulate too, but runs as a plant command.
99
+ if (
100
+ flags.simulate &&
101
+ !["run", "download", "online-change"].includes(command)
102
+ ) {
103
+ throw new Error(
104
+ "--simulate applies only to run, download, online-change and deploy.",
105
+ );
106
+ }
107
+ if (flags.plc && flags.server) {
108
+ throw new Error("Choose --plc or --server, or omit both for a full build.");
109
+ }
110
+ if (flags.server && flags["emit-only"]) {
111
+ throw new Error("--emit-only applies to PLC emission only.");
112
+ }
113
+ };
114
+
115
+ export const runProjectCommand = async (
116
+ command: ProjectCommand,
117
+ file: string,
118
+ flags: ProjectFlags,
119
+ env: Environment,
120
+ ): Promise<CommandResult> => {
121
+ const load = () =>
122
+ loadTsplcConfig(file, { env, target: flags.target, forAgent: false });
123
+ switch (command) {
124
+ case "configure":
125
+ return configure(flags);
126
+ case "clean":
127
+ return cleanProject(file);
128
+ case "install":
129
+ return install(await load(), flags);
130
+ case "typegen":
131
+ return typegen(file, await load(), flags);
132
+ case "check":
133
+ return check(file, await loadCheckedConfig(file, load));
134
+ case "build":
135
+ return build(file, await loadCheckedConfig(file, load), flags);
136
+ case "run":
137
+ return run(file, await loadCheckedConfig(file, load), flags);
138
+ case "inspect":
139
+ return inspect(await load(), flags);
140
+ case "download":
141
+ case "online-change":
142
+ return updateProgram(
143
+ command,
144
+ file,
145
+ await loadCheckedConfig(file, load),
146
+ flags,
147
+ );
148
+ }
149
+ };
150
+
151
+ /** A config loaded after its host config check, whose diagnostics the compiler reports. */
152
+ interface CheckedConfig {
153
+ loaded: LoadedConfig;
154
+ hostConfigDiagnostics: readonly Diagnostic[];
155
+ }
156
+
157
+ /** Loads the config after type-checking it, so a type error fails before the config runs. */
158
+ const loadCheckedConfig = async (
159
+ file: string,
160
+ load: () => Promise<LoadedConfig>,
161
+ ): Promise<CheckedConfig> => {
162
+ const hostConfigDiagnostics = checkHostConfig(resolve(file));
163
+ if (hasErrors(hostConfigDiagnostics)) {
164
+ throw new DiagnosticFailure({ diagnostics: hostConfigDiagnostics });
165
+ }
166
+ return { loaded: await load(), hostConfigDiagnostics };
167
+ };
168
+
169
+ /** Builds the Docker image every build uses, or with `--runtime` the local PLC runtime's. */
170
+ const configure = async (flags: Pick<ProjectFlags, "runtime">) => {
171
+ await (flags.runtime ? configureRuntime() : configureDocker());
172
+ return { ok: true };
173
+ };
174
+
175
+ const install = async (
176
+ { config, root }: LoadedConfig,
177
+ flags: Pick<ProjectFlags, "frozen-lockfile">,
178
+ ) => ({
179
+ libraries: await installLibraries(config.dependencies, root, {
180
+ frozen: flags["frozen-lockfile"],
181
+ }),
182
+ });
183
+
184
+ const typegen = async (
185
+ file: string,
186
+ loaded: LoadedConfig,
187
+ flags: Pick<ProjectFlags, "check">,
188
+ ) => {
189
+ const targets = await projectTargets(file, { loaded });
190
+ if (loaded.config.compiler && !flags.check) {
191
+ await generateProject(file, { hardwareOnly: true, loaded, targets });
192
+ const emitted = await buildProject(file, { emitOnly: true, loaded });
193
+ if (hasErrors(emitted.diagnostics)) {
194
+ throw new DiagnosticFailure(emitted);
195
+ }
196
+ }
197
+ return generateProject(file, { check: flags.check, loaded, targets });
198
+ };
199
+
200
+ const check = async (
201
+ file: string,
202
+ { loaded, hostConfigDiagnostics }: CheckedConfig,
203
+ ) => {
204
+ const { config, configFile, root } = loaded;
205
+ if (config.compiler) {
206
+ return checkProject(file, { loaded, hostConfigDiagnostics });
207
+ }
208
+ const sources = await loadSources(
209
+ resolveConfig(
210
+ {
211
+ ...rawBuildConfig(config),
212
+ io: resolveIo(config, await loadHardware(config, root)),
213
+ },
214
+ configFile,
215
+ ),
216
+ );
217
+ return { objects: sources.objects.length };
218
+ };
219
+
220
+ /**
221
+ * The PLC program `build` and `run` compile: TypeScript through the compiler, or raw
222
+ * Structured Text straight to the vendor build. Either refreshes the generated types.
223
+ */
224
+ const buildPlc = async (
225
+ command: "build" | "run",
226
+ file: string,
227
+ { loaded, hostConfigDiagnostics }: CheckedConfig,
228
+ flags: Pick<ProjectFlags, "emit-only" | "frozen-lockfile" | "simulate">,
229
+ ) => {
230
+ const { config } = loaded;
231
+ if (config.compiler) {
232
+ const built = await buildProject(file, {
233
+ emitOnly: flags["emit-only"],
234
+ frozenLockfile: flags["frozen-lockfile"],
235
+ ...buildConfigs(loaded, flags),
236
+ hostConfigDiagnostics,
237
+ controller: controllerFor(config, command),
238
+ buildTarget: buildTargetFor(command),
239
+ });
240
+ if (hasErrors(built.diagnostics)) {
241
+ throw new DiagnosticFailure(built);
242
+ }
243
+ await generateProject(file, {
244
+ loaded,
245
+ targets: await projectTargets(file, { loaded }),
246
+ });
247
+ return built;
248
+ }
249
+ if (flags["emit-only"]) {
250
+ throw new Error("Raw Structured Text is already emitted.");
251
+ }
252
+ const built = await buildSt(
253
+ await rawEngineeringSettings(loaded, {
254
+ command,
255
+ frozenLockfile: flags["frozen-lockfile"],
256
+ simulate: flags.simulate,
257
+ }),
258
+ );
259
+ await generateProject(file, { hardwareOnly: !config.typegen, loaded });
260
+ return built;
261
+ };
262
+
263
+ /** The PLC program and, unless `--plc` or `--emit-only` restricts it, the configured server. */
264
+ const build = async (
265
+ file: string,
266
+ checked: CheckedConfig,
267
+ flags: Pick<
268
+ ProjectFlags,
269
+ "emit-only" | "frozen-lockfile" | "plc" | "server" | "simulate"
270
+ >,
271
+ ) => {
272
+ const { config, root } = checked.loaded;
273
+ if (flags.server) {
274
+ return buildServer(config, root);
275
+ }
276
+ const plc = await buildPlc("build", file, checked, flags);
277
+ if (flags.plc || flags["emit-only"] || !config.server?.runtime) {
278
+ return plc;
279
+ }
280
+ return { ...plc, ...(await buildServer(config, root)) };
281
+ };
282
+
283
+ const run = async (
284
+ file: string,
285
+ checked: CheckedConfig,
286
+ flags: Pick<ProjectFlags, "frozen-lockfile" | "simulate">,
287
+ ) => {
288
+ const { config, configFile } = checked.loaded;
289
+ const built = await buildPlc("run", file, checked, flags);
290
+ if (!config.build) {
291
+ throw new Error("Configure build to run this project.");
292
+ }
293
+ await runProject(
294
+ resolveConfig(
295
+ config.compiler ? config.build : rawBuildConfig(config),
296
+ configFile,
297
+ ),
298
+ config.name,
299
+ );
300
+ return built;
301
+ };
302
+
303
+ const inspect = async (
304
+ { config, configFile }: LoadedConfig,
305
+ flags: Pick<ProjectFlags, "read">,
306
+ ) => {
307
+ if (!config.build) {
308
+ throw new Error("Configure build settings and controller connection.");
309
+ }
310
+ // Inspection needs only the connection/baseline. It must remain usable
311
+ // when local sources, descriptors or dependency caches are unavailable.
312
+ return inspectController(
313
+ resolveConfig(
314
+ { ...config.build, controller: controllerFor(config, "inspect") },
315
+ configFile,
316
+ ),
317
+ flags.read,
318
+ );
319
+ };
320
+
321
+ /** Downloads or online-changes the program on the configured controller. */
322
+ const updateProgram = async (
323
+ operation: "download" | "online-change",
324
+ file: string,
325
+ checked: CheckedConfig,
326
+ flags: Pick<
327
+ ProjectFlags,
328
+ "frozen-lockfile" | "simulate" | "initialize-user" | "read"
329
+ >,
330
+ ) => {
331
+ const { config } = checked.loaded;
332
+ if (!config.build) {
333
+ throw new Error("Configure build settings and controller connection.");
334
+ }
335
+ const settings = config.compiler
336
+ ? await emittedControllerSettings(operation, file, checked, flags)
337
+ : await rawEngineeringSettings(checked.loaded, {
338
+ command: operation,
339
+ frozenLockfile: flags["frozen-lockfile"],
340
+ simulate: flags.simulate,
341
+ });
342
+ return updateController(settings, operation, {
343
+ initializeUser: flags["initialize-user"],
344
+ read: flags.read,
345
+ });
346
+ };
347
+
348
+ const emittedControllerSettings = async (
349
+ operation: "download" | "online-change",
350
+ file: string,
351
+ { loaded, hostConfigDiagnostics }: CheckedConfig,
352
+ flags: Pick<ProjectFlags, "frozen-lockfile" | "simulate">,
353
+ ) => {
354
+ const emitted = await buildProject(file, {
355
+ emitOnly: true,
356
+ forController: true,
357
+ frozenLockfile: flags["frozen-lockfile"],
358
+ ...buildConfigs(loaded, flags),
359
+ hostConfigDiagnostics,
360
+ controller: controllerFor(loaded.config, operation),
361
+ buildTarget: buildTargetFor(operation),
362
+ });
363
+ if (hasErrors(emitted.diagnostics)) {
364
+ throw new DiagnosticFailure(emitted);
365
+ }
366
+ if (!emitted.controllerConfig) {
367
+ throw new Error("Configure build settings for deployment.");
368
+ }
369
+ // Emission already resolves the generated entry/task paths. Passing
370
+ // this through the input schema again would reject its configFile.
371
+ return { ...emitted.controllerConfig, io: {} };
372
+ };