@tsplc/compiler 0.2.16 → 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
package/ir/types.ts DELETED
@@ -1,76 +0,0 @@
1
- export type TypeId = string;
2
- export type SymbolId = string;
3
-
4
- export interface Field {
5
- name: string;
6
- type: TypeId;
7
- readonly: boolean;
8
- mode?: "inOut";
9
- }
10
-
11
- export type PlcType =
12
- | { id: TypeId; kind: "bool" }
13
- | { id: TypeId; kind: "integer"; bits: 8 | 16 | 32 | 64; signed: boolean }
14
- | { id: TypeId; kind: "bits"; bits: 8 | 16 | 32 | 64 }
15
- | { id: TypeId; kind: "float"; bits: 32 | 64 }
16
- | { id: TypeId; kind: "duration" }
17
- | { id: TypeId; kind: "calendar"; domain: "date" | "timeOfDay" | "dateTime" }
18
- | { id: TypeId; kind: "string"; capacity: number; encoding: "utf8" | "utf16" }
19
- | {
20
- id: TypeId;
21
- kind: "array";
22
- element: TypeId;
23
- length: number;
24
- lowerBound: number;
25
- /** `Record<Enum, T>`: the enum whose contiguous member values are the bounds and the only indexes. */
26
- index?: TypeId;
27
- }
28
- | { id: TypeId; kind: "record"; fields: readonly Field[] }
29
- | {
30
- id: TypeId;
31
- kind: "enum";
32
- base: TypeId;
33
- /** `text` is the string a plain TypeScript program's literal union member stands for. */
34
- members: readonly { name: string; value: string; text?: string }[];
35
- }
36
- | { id: TypeId; kind: "block"; block: SymbolId; output?: TypeId };
37
-
38
- /** The block allocated by a type: a block itself, or a fixed array of one. */
39
- export const instancedBlock = (
40
- id: TypeId,
41
- types: ReadonlyMap<TypeId, PlcType>,
42
- ): Extract<PlcType, { kind: "block" }> | undefined => {
43
- const type = types.get(id);
44
- const element = type?.kind === "array" ? types.get(type.element) : type;
45
- return element?.kind === "block" ? element : undefined;
46
- };
47
-
48
- export const primitiveTypes: readonly PlcType[] = [
49
- { id: "BOOL", kind: "bool" },
50
- ...(
51
- [
52
- ["SINT", "USINT", "BYTE", 8],
53
- ["INT", "UINT", "WORD", 16],
54
- ["DINT", "UDINT", "DWORD", 32],
55
- ["LINT", "ULINT", "LWORD", 64],
56
- ] as const
57
- ).flatMap(([signed, unsigned, bitstring, bits]) => [
58
- { id: signed, kind: "integer" as const, bits, signed: true },
59
- { id: unsigned, kind: "integer" as const, bits, signed: false },
60
- { id: bitstring, kind: "bits" as const, bits },
61
- ]),
62
- { id: "REAL", kind: "float", bits: 32 },
63
- { id: "LREAL", kind: "float", bits: 64 },
64
- { id: "TIME", kind: "duration" },
65
- { id: "DATE", kind: "calendar", domain: "date" },
66
- { id: "TIME_OF_DAY", kind: "calendar", domain: "timeOfDay" },
67
- { id: "DATE_AND_TIME", kind: "calendar", domain: "dateTime" },
68
- ];
69
-
70
- export function integerRange(
71
- type: Extract<PlcType, { kind: "integer" | "bits" }>,
72
- ): readonly [bigint, bigint] {
73
- const signed = type.kind === "integer" && type.signed;
74
- const limit = 1n << BigInt(type.bits - (signed ? 1 : 0));
75
- return signed ? [-limit, limit - 1n] : [0n, limit - 1n];
76
- }
@@ -1,277 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { lstat, mkdir, readdir, readFile, rename, rm, writeFile } from "node:fs/promises";
3
- import { dirname, join } from "node:path";
4
- import type { buildCodesys } from "@tsplc/compiler/codesys";
5
-
6
- const missing = (cause: unknown): boolean =>
7
- cause instanceof Error && "code" in cause && cause.code === "ENOENT";
8
- const digest = (value: string | Uint8Array): string =>
9
- createHash("sha256").update(value).digest("hex");
10
-
11
- // Ownership is an explicit boundary: generated files must match their manifest,
12
- // and engineering trees must belong to this configuration before vendor writes.
13
- interface Manifest {
14
- kind: "tsplc-artifacts";
15
- version: 1;
16
- configFile: string;
17
- files: { path: string; sha256: string }[];
18
- }
19
-
20
- function manifest(value: unknown, configFile: string): Manifest {
21
- if (
22
- typeof value !== "object" ||
23
- value === null ||
24
- !("kind" in value) ||
25
- value.kind !== "tsplc-artifacts" ||
26
- !("version" in value) ||
27
- value.version !== 1 ||
28
- !("configFile" in value) ||
29
- value.configFile !== configFile ||
30
- !("files" in value) ||
31
- !Array.isArray(value.files)
32
- ) {
33
- throw new Error("Output directory has no matching tsPLC ownership manifest.");
34
- }
35
-
36
- const files = value.files.map((item: unknown) => {
37
- if (
38
- typeof item !== "object" ||
39
- item === null ||
40
- !("path" in item) ||
41
- typeof item.path !== "string" ||
42
- !/^[A-Za-z0-9_ .-]+(?:\/[A-Za-z0-9_ .-]+)*$/.test(item.path) ||
43
- item.path.split("/").some((part) => part === "." || part === "..") ||
44
- ["manifest.json", "receipt.json"].includes(item.path) ||
45
- !("sha256" in item) ||
46
- typeof item.sha256 !== "string" ||
47
- !/^[0-9a-f]{64}$/.test(item.sha256)
48
- ) {
49
- throw new Error("Invalid generated file ownership entry.");
50
- }
51
- return { path: item.path, sha256: item.sha256 };
52
- });
53
- if (new Set(files.map((item) => item.path)).size !== files.length) {
54
- throw new Error("Duplicate generated file ownership entry.");
55
- }
56
- return { kind: "tsplc-artifacts", version: 1, configFile, files };
57
- }
58
-
59
- async function directory(path: string): Promise<boolean> {
60
- try {
61
- const entry = await lstat(path);
62
- if (!entry.isDirectory() || entry.isSymbolicLink()) {
63
- throw new Error(`Generated directory must be a real directory: ${path}`);
64
- }
65
- return true;
66
- } catch (cause) {
67
- if (missing(cause)) {
68
- return false;
69
- }
70
- throw cause;
71
- }
72
- }
73
-
74
- async function filesIn(path: string, prefix = ""): Promise<string[]> {
75
- const result: string[] = [];
76
- for (const entry of await readdir(path, { withFileTypes: true })) {
77
- const name = prefix + entry.name;
78
- if (entry.isDirectory() && !entry.isSymbolicLink()) {
79
- result.push(...(await filesIn(join(path, entry.name), `${name}/`)));
80
- } else {
81
- result.push(name);
82
- }
83
- }
84
- return result;
85
- }
86
-
87
- export async function engineeringDirectory(path: string, configFile: string): Promise<void> {
88
- const marker = join(path, "ownership.json");
89
- const exists = await directory(path);
90
- if (!exists || !(await readdir(path)).length) {
91
- await mkdir(path, { recursive: true });
92
- await writeFile(
93
- marker,
94
- `${JSON.stringify({ kind: "tsplc-engineering", version: 1, configFile }, null, 2)}\n`,
95
- { flag: "wx" },
96
- );
97
- } else {
98
- const entry = await lstat(marker).catch((cause: unknown) => {
99
- if (missing(cause)) {
100
- throw new Error(`Engineering cache has no matching tsPLC ownership marker: ${path}`);
101
- }
102
- throw cause;
103
- });
104
- if (!entry.isFile() || entry.isSymbolicLink()) {
105
- throw new Error("Engineering ownership marker must be a real file.");
106
- }
107
- const value = JSON.parse(await readFile(marker, "utf8"));
108
- if (
109
- value?.kind !== "tsplc-engineering" ||
110
- value.version !== 1 ||
111
- value.configFile !== configFile
112
- ) {
113
- throw new Error(`Engineering cache has no matching tsPLC ownership marker: ${path}`);
114
- }
115
- }
116
-
117
- const inspect = async (directory: string): Promise<void> => {
118
- for (const entry of await readdir(directory, { withFileTypes: true })) {
119
- const child = join(directory, entry.name);
120
- if (entry.isSymbolicLink()) {
121
- throw new Error(`Engineering cache must not contain symlinks: ${child}`);
122
- }
123
- if (entry.isDirectory()) {
124
- await inspect(child);
125
- }
126
- }
127
- };
128
- await inspect(path);
129
- }
130
-
131
- export async function owned(
132
- configFile: string,
133
- withdrawReceipt = false,
134
- ): Promise<{ root: string; output: string; ownership?: Manifest; unknown: string[] }> {
135
- const root = join(dirname(configFile), ".tsplc");
136
- const output = join(root, "artifacts");
137
- if (!(await directory(root)) || !(await directory(output))) {
138
- return { root, output, unknown: [] };
139
- }
140
-
141
- const manifestPath = join(output, "manifest.json");
142
- const manifestEntry = await lstat(manifestPath);
143
- if (!manifestEntry.isFile() || manifestEntry.isSymbolicLink()) {
144
- throw new Error("Ownership manifest must be a real file.");
145
- }
146
- const ownership = manifest(JSON.parse(await readFile(manifestPath, "utf8")), configFile);
147
- const receipt = join(output, "receipt.json");
148
- try {
149
- const entry = await lstat(receipt);
150
- if (!entry.isFile() || entry.isSymbolicLink()) {
151
- throw new Error("Build receipt must be a real file.");
152
- }
153
- const value = JSON.parse(await readFile(receipt, "utf8"));
154
- if (
155
- value?.kind !== "tsplc-build" ||
156
- value.version !== 1 ||
157
- value.configFile !== configFile ||
158
- !["emitted", "vendor-compiled"].includes(value.status)
159
- ) {
160
- throw new Error("Build receipt is not owned by this configuration.");
161
- }
162
- if (withdrawReceipt) {
163
- await rm(receipt);
164
- }
165
- } catch (cause) {
166
- if (!missing(cause)) {
167
- throw cause;
168
- }
169
- }
170
-
171
- const names = new Set([
172
- ...ownership.files.map((file) => file.path),
173
- "manifest.json",
174
- "receipt.json",
175
- ]);
176
- for (const file of ownership.files) {
177
- let parent = output;
178
- for (const part of file.path.split("/").slice(0, -1)) {
179
- parent = join(parent, part);
180
- if (!(await directory(parent))) {
181
- throw new Error(`Missing generated directory: ${parent}`);
182
- }
183
- }
184
- const path = join(output, file.path);
185
- const entry = await lstat(path);
186
- if (!entry.isFile() || entry.isSymbolicLink() || digest(await readFile(path)) !== file.sha256) {
187
- throw new Error(
188
- `Generated file was replaced or edited; preserve it before rebuilding/cleaning: ${path}`,
189
- );
190
- }
191
- }
192
- return {
193
- root,
194
- output,
195
- ownership,
196
- unknown: (await filesIn(output)).filter((name) => !names.has(name)),
197
- };
198
- }
199
-
200
- interface Receipt {
201
- configFile: string;
202
- status: "emitted" | "vendor-compiled";
203
- vendor?: Pick<Awaited<ReturnType<typeof buildCodesys>>, "application" | "tasks">;
204
- }
205
-
206
- // Compile in a separate staging tree. Recheck ownership immediately before the
207
- // swap, because the vendor build may have given the user time to edit old files.
208
- export async function publishArtifacts(
209
- staging: string,
210
- previous: Awaited<ReturnType<typeof owned>>,
211
- receipt: Receipt,
212
- ): Promise<void> {
213
- const configFile = receipt.configFile;
214
- const generated = (await filesIn(staging)).sort();
215
- const ownership: Manifest = { kind: "tsplc-artifacts", version: 1, configFile, files: [] };
216
- for (const path of generated) {
217
- ownership.files.push({ path, sha256: digest(await readFile(join(staging, path))) });
218
- }
219
- await writeFile(join(staging, "manifest.json"), `${JSON.stringify(ownership, null, 2)}\n`);
220
- await writeFile(
221
- join(staging, "receipt.json"),
222
- `${JSON.stringify(
223
- { kind: "tsplc-build", version: 1, generatedAt: new Date().toISOString(), ...receipt },
224
- null,
225
- 2,
226
- )}\n`,
227
- );
228
-
229
- const current = await owned(configFile);
230
- if (JSON.stringify(current.ownership) !== JSON.stringify(previous.ownership)) {
231
- throw new Error("Output ownership changed during the build; preserve it before rebuilding.");
232
- }
233
- if (current.unknown.length) {
234
- throw new Error(`Output contains unrelated files: ${current.unknown.join(", ")}`);
235
- }
236
-
237
- const backup = `${staging}-previous`;
238
- if (previous.ownership) {
239
- await rename(previous.output, backup);
240
- }
241
- try {
242
- await rename(staging, previous.output);
243
- } catch (cause) {
244
- if (previous.ownership) {
245
- await rename(backup, previous.output);
246
- }
247
- throw cause;
248
- }
249
- if (previous.ownership) {
250
- await rm(backup, { recursive: true });
251
- }
252
- }
253
-
254
- export async function cleanArtifacts(configFile: string): Promise<void> {
255
- const generated = await owned(configFile);
256
- if (!generated.ownership) {
257
- return;
258
- }
259
- for (const entry of generated.ownership.files) {
260
- await rm(join(generated.output, entry.path));
261
- }
262
- await rm(join(generated.output, "receipt.json"), { force: true });
263
- await rm(join(generated.output, "manifest.json"));
264
-
265
- // Remove empty generated directories while preserving unrelated files.
266
- const removeEmpty = async (path: string): Promise<void> => {
267
- for (const entry of await readdir(path, { withFileTypes: true })) {
268
- if (entry.isDirectory() && !entry.isSymbolicLink()) {
269
- await removeEmpty(join(path, entry.name));
270
- }
271
- }
272
- if (!(await readdir(path)).length) {
273
- await rm(path, { recursive: true });
274
- }
275
- };
276
- await removeEmpty(generated.output);
277
- }