@tsplc/compiler 0.2.17 → 0.2.19

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,181 @@
1
+ import ts from "typescript";
2
+ import {
3
+ aggregate,
4
+ type MethodOwner,
5
+ type PlcFunction,
6
+ } from "@tsplc/compiler/ir";
7
+ import type { Positions } from "./positions.ts";
8
+ import {
9
+ hasModifier,
10
+ type Fail,
11
+ type TypeEnvironment,
12
+ type TypeResolver,
13
+ } from "./types.ts";
14
+
15
+ /** A routine whose signature is admitted and whose body is still to be lowered. */
16
+ interface PendingRoutine {
17
+ node: ts.FunctionDeclaration | ts.MethodDeclaration;
18
+ fn: PlcFunction;
19
+ environment: TypeEnvironment;
20
+ }
21
+
22
+ /**
23
+ * The routines a module contains: helpers and block methods by symbol, every
24
+ * instance by id, and the worklist of bodies still to lower. Lowering a body can
25
+ * instantiate more generic helpers, which join the worklist.
26
+ */
27
+ export class RoutineTable {
28
+ readonly functions = new Map<ts.Symbol, PlcFunction>();
29
+ readonly methods = new Map<ts.Symbol, PlcFunction>();
30
+ readonly functionDeclarations = new Map<ts.Symbol, ts.FunctionDeclaration>();
31
+ readonly instances = new Map<string, PlcFunction>();
32
+ readonly pending: PendingRoutine[] = [];
33
+
34
+ constructor(
35
+ private readonly checker: ts.TypeChecker,
36
+ private readonly resolver: TypeResolver,
37
+ private readonly positions: Positions,
38
+ private readonly fail: Fail,
39
+ ) {}
40
+
41
+ instantiate(
42
+ node: ts.FunctionDeclaration | ts.MethodDeclaration,
43
+ arguments_: readonly ts.TypeNode[],
44
+ call: ts.Node,
45
+ outer: TypeEnvironment,
46
+ ): PlcFunction {
47
+ if (!node.name || !ts.isIdentifier(node.name)) {
48
+ this.fail(node, "PLC1001", "PLC routines need identifier names.");
49
+ }
50
+ const routineName = node.name.text;
51
+ const environment = this.resolver.bindings(
52
+ node.typeParameters,
53
+ arguments_,
54
+ outer,
55
+ call,
56
+ );
57
+ const suffix = (node.typeParameters ?? [])
58
+ .map((parameter) =>
59
+ this.resolver.key(
60
+ environment.get(this.resolver.symbol(parameter.name))!,
61
+ ),
62
+ )
63
+ .join(",");
64
+ const id = `${this.resolver.id(node, routineName)}${suffix ? `<${suffix}>` : ""}`;
65
+ const existing = this.instances.get(id);
66
+ if (existing) {
67
+ return existing;
68
+ }
69
+ // Bounds generic expansion; plain helpers each instantiate once.
70
+ if (
71
+ suffix &&
72
+ [...this.instances.keys()].filter((key) => key.endsWith(">")).length >=
73
+ 256
74
+ ) {
75
+ this.fail(
76
+ call,
77
+ "PLC2001",
78
+ "Generic specialization exceeds 256 concrete helpers; recursive specialization is unsupported.",
79
+ );
80
+ }
81
+ const parameters = node.parameters.map((parameter) => {
82
+ if (
83
+ !ts.isIdentifier(parameter.name) ||
84
+ !parameter.type ||
85
+ parameter.initializer ||
86
+ parameter.questionToken ||
87
+ parameter.dotDotDotToken
88
+ ) {
89
+ this.fail(
90
+ parameter,
91
+ "PLC2001",
92
+ "PLC parameters need names and explicit types; optional/rest/default parameters are unsupported.",
93
+ );
94
+ }
95
+ const type = this.resolver.resolve(parameter.type, environment);
96
+ const block = this.resolver.types.get(type.type)?.kind === "block";
97
+ if (type.mode !== "inOut" && !block && !this.resolver.isValue(type)) {
98
+ this.fail(
99
+ parameter,
100
+ "PLC2001",
101
+ "Parameters require value types or InOut borrows.",
102
+ );
103
+ }
104
+ if (type.io) {
105
+ this.fail(
106
+ parameter,
107
+ "PLC2001",
108
+ "Parameters cannot use logical I/O wrappers.",
109
+ );
110
+ }
111
+ // Records, arrays and blocks pass by reference, as objects do in TypeScript.
112
+ const mode =
113
+ type.mode ??
114
+ (block || aggregate(this.resolver.types.get(type.type))
115
+ ? "inOut"
116
+ : undefined);
117
+ return {
118
+ id: `${this.resolver.id(parameter, parameter.name.text)}@${id}`,
119
+ name: parameter.name.text,
120
+ type: type.type,
121
+ storage: "parameter" as const,
122
+ mutable: mode === "inOut",
123
+ ...(mode && { mode }),
124
+ retain: false,
125
+ span: this.positions.node(parameter),
126
+ };
127
+ });
128
+ let result: string | undefined;
129
+ if (node.type && node.type.kind !== ts.SyntaxKind.VoidKeyword) {
130
+ const type = this.resolver.resolve(node.type, environment);
131
+ if (!this.resolver.isValue(type)) {
132
+ this.fail(node.type, "PLC2001", "Helper results require value types.");
133
+ }
134
+ if (type.io) {
135
+ this.fail(
136
+ node.type,
137
+ "PLC4002",
138
+ "Helper results cannot use I/O wrappers.",
139
+ );
140
+ }
141
+ result = type.type;
142
+ } else if (!node.type) {
143
+ const signature = this.checker.getSignatureFromDeclaration(node);
144
+ if (
145
+ !signature ||
146
+ !(
147
+ this.checker.getReturnTypeOfSignature(signature).flags &
148
+ ts.TypeFlags.Void
149
+ )
150
+ ) {
151
+ this.fail(node, "PLC2001", "Annotate helper results explicitly.");
152
+ }
153
+ }
154
+ const bounds = this.resolver.mathHelper(node, routineName);
155
+ const fn: PlcFunction = {
156
+ id,
157
+ name: `${routineName}${suffix ? `_${suffix}` : ""}`,
158
+ parameters,
159
+ locals: [],
160
+ body: [],
161
+ span: this.positions.node(node),
162
+ ...(result && { result }),
163
+ ...(bounds && { bounds }),
164
+ };
165
+ this.instances.set(id, fn);
166
+ this.pending.push({ node, fn, environment });
167
+ return fn;
168
+ }
169
+ }
170
+
171
+ export const methodAccess = (
172
+ method: ts.MethodDeclaration,
173
+ ): MethodOwner["access"] => {
174
+ if (hasModifier(method, ts.SyntaxKind.PrivateKeyword)) {
175
+ return "private";
176
+ }
177
+ if (hasModifier(method, ts.SyntaxKind.ProtectedKeyword)) {
178
+ return "protected";
179
+ }
180
+ return "public";
181
+ };
@@ -0,0 +1,82 @@
1
+ import ts from "typescript";
2
+ import { createRequire } from "node:module";
3
+ import { realpathSync } from "node:fs";
4
+ import { dirname } from "node:path";
5
+
6
+ /** The `@tsplc/compiler/globals` declaration file a project compiles against. */
7
+ export const sdkFor = (
8
+ project: string,
9
+ options: ts.CompilerOptions,
10
+ ): string => {
11
+ const name = "@tsplc/compiler/globals";
12
+ const resolveSdk = (from: string) =>
13
+ createRequire(from).resolve(name).replaceAll("\\", "/");
14
+ try {
15
+ const installed = resolveSdk(project);
16
+ const resolved = ts.resolveModuleName(
17
+ name,
18
+ project,
19
+ options,
20
+ ts.sys,
21
+ ).resolvedModule;
22
+ // TypeScript may preserve a workspace symlink. A path alias may only stand in
23
+ // for the SDK if it resolves to the same installed declaration.
24
+ if (
25
+ resolved &&
26
+ realpathSync(resolved.resolvedFileName) === realpathSync(installed)
27
+ ) {
28
+ return resolved.resolvedFileName;
29
+ }
30
+ return installed;
31
+ } catch (cause) {
32
+ if (
33
+ !(cause instanceof Error) ||
34
+ !("code" in cause) ||
35
+ cause.code !== "MODULE_NOT_FOUND"
36
+ ) {
37
+ throw cause;
38
+ }
39
+ return resolveSdk(import.meta.url);
40
+ }
41
+ };
42
+
43
+ /** Whether `fileName` is one of the SDK declaration files, which sit beside `sdk`. */
44
+ export const inSdk = (sdk: string, fileName: string): boolean =>
45
+ fileName.startsWith(`${dirname(sdk)}/`);
46
+
47
+ /** A symbol followed through import and export aliases to what it names. */
48
+ export const unaliased = (
49
+ checker: ts.TypeChecker,
50
+ symbol: ts.Symbol,
51
+ ): ts.Symbol =>
52
+ symbol.flags & ts.SymbolFlags.Alias
53
+ ? unaliased(checker, checker.getAliasedSymbol(symbol))
54
+ : symbol;
55
+
56
+ /** The symbol `node` names, followed through import and export aliases. */
57
+ export const symbolAt = (
58
+ checker: ts.TypeChecker,
59
+ node: ts.Node,
60
+ ): ts.Symbol | undefined => {
61
+ const symbol = checker.getSymbolAtLocation(node);
62
+ return symbol && unaliased(checker, symbol);
63
+ };
64
+
65
+ /** Whether the SDK declares `symbol`. */
66
+ export const declaredBySdk = (sdk: string, symbol: ts.Symbol): boolean =>
67
+ Boolean(
68
+ symbol.declarations?.some((declaration) =>
69
+ inSdk(sdk, declaration.getSourceFile().fileName),
70
+ ),
71
+ );
72
+
73
+ /** Whether `node` names the SDK's `name`, such as `sleep`, `expose` or `env`. */
74
+ export const namesSdk = (
75
+ checker: ts.TypeChecker,
76
+ sdk: string,
77
+ node: ts.Node,
78
+ name: string,
79
+ ): boolean => {
80
+ const symbol = symbolAt(checker, node);
81
+ return symbol?.name === name && declaredBySdk(sdk, symbol);
82
+ };