@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
@@ -1,153 +1,185 @@
1
1
  import { runtimeSources } from "./module-graph.ts";
2
2
  import ts from "typescript";
3
- import { dirname } from "node:path";
3
+ import { namesSdk, symbolAt } from "./sdk.ts";
4
4
  import { error, type Diagnostic } from "@tsplc/compiler/ir";
5
5
  import { edited, type Edit, type Mapped, type Positions } from "./positions.ts";
6
+ import type { Environment } from "@tsplc/config";
6
7
 
7
- const symbolAt = (checker: ts.TypeChecker, node: ts.Node) => {
8
- let symbol = checker.getSymbolAtLocation(node);
9
- while (symbol && symbol.flags & ts.SymbolFlags.Alias) symbol = checker.getAliasedSymbol(symbol);
10
- return symbol;
11
- };
12
-
13
- export const lowerEnvironment = (program: ts.Program, sdk: string, positions: Positions) => {
14
- const checker = program.getTypeChecker();
15
- const sources = new Map<string, Mapped>();
16
- const diagnostics: Diagnostic[] = [];
17
- for (const source of runtimeSources(program)) {
18
- if (source.isDeclarationFile) continue;
19
- const edits: Edit[] = [];
20
- const visit = (node: ts.Node): void => {
21
- if (ts.isCallExpression(node)) {
22
- const symbol = symbolAt(checker, node.expression);
23
- if (
24
- symbol?.name === "env" &&
25
- symbol.declarations?.some((declaration) =>
26
- declaration.getSourceFile().fileName.startsWith(dirname(sdk) + "/"),
27
- )
28
- ) {
29
- const name = node.arguments[0];
30
- let owner: ts.Node | undefined = node.parent;
31
- while (owner && !ts.isVariableDeclaration(owner) && !ts.isFunctionLike(owner))
32
- owner = owner.parent;
33
- const moduleConst =
34
- owner &&
35
- ts.isVariableDeclaration(owner) &&
36
- ts.isVariableDeclarationList(owner.parent) &&
37
- Boolean(owner.parent.flags & ts.NodeFlags.Const) &&
38
- ts.isVariableStatement(owner.parent.parent) &&
39
- ts.isSourceFile(owner.parent.parent.parent);
40
- const value = name && ts.isStringLiteral(name) ? process.env[name.text] : undefined;
41
- let message: string | undefined;
42
- if (
43
- !moduleConst ||
44
- node.arguments.length !== 1 ||
45
- !name ||
46
- !ts.isStringLiteral(name) ||
47
- node.typeArguments?.length !== 1
48
- )
49
- message =
50
- "env<STRING<N>> requires a literal environment name in module-level const configuration.";
51
- else if (value === undefined || value.length === 0)
52
- message = `Required environment value ${name.text} is missing.`;
53
- else if (value.includes("\0"))
54
- message = `Environment value ${name.text} contains an unsupported NUL byte.`;
55
- if (message) diagnostics.push(error("PLC1011", message, positions.node(node)));
56
- else if (value !== undefined)
57
- edits.push({
58
- start: node.getStart(source),
59
- end: node.end,
60
- text: JSON.stringify(value),
61
- });
62
- return;
63
- }
64
- }
65
- ts.forEachChild(node, visit);
66
- };
67
- visit(source);
68
- if (edits.length) sources.set(source.fileName, edited(source, edits));
69
- }
70
- return { sources, diagnostics };
8
+ export const lowerEnvironment = (
9
+ program: ts.Program,
10
+ sdk: string,
11
+ positions: Positions,
12
+ env: Environment,
13
+ ) => {
14
+ const checker = program.getTypeChecker();
15
+ const sources = new Map<string, Mapped>();
16
+ const diagnostics: Diagnostic[] = [];
17
+ for (const source of runtimeSources(program)) {
18
+ if (source.isDeclarationFile) {
19
+ continue;
20
+ }
21
+ const edits: Edit[] = [];
22
+ const visit = (node: ts.Node): void => {
23
+ if (ts.isCallExpression(node)) {
24
+ if (namesSdk(checker, sdk, node.expression, "env")) {
25
+ const name = node.arguments[0];
26
+ let owner: ts.Node | undefined = node.parent;
27
+ while (
28
+ owner &&
29
+ !ts.isVariableDeclaration(owner) &&
30
+ !ts.isFunctionLike(owner)
31
+ ) {
32
+ owner = owner.parent;
33
+ }
34
+ const moduleConst =
35
+ owner &&
36
+ ts.isVariableDeclaration(owner) &&
37
+ ts.isVariableDeclarationList(owner.parent) &&
38
+ Boolean(owner.parent.flags & ts.NodeFlags.Const) &&
39
+ ts.isVariableStatement(owner.parent.parent) &&
40
+ ts.isSourceFile(owner.parent.parent.parent);
41
+ const value =
42
+ name && ts.isStringLiteral(name) ? env[name.text] : undefined;
43
+ let message: string | undefined;
44
+ if (
45
+ !moduleConst ||
46
+ node.arguments.length !== 1 ||
47
+ !name ||
48
+ !ts.isStringLiteral(name) ||
49
+ node.typeArguments?.length !== 1
50
+ ) {
51
+ message =
52
+ "env<STRING<N>> requires a literal environment name in module-level const configuration.";
53
+ } else if (value === undefined || value.length === 0) {
54
+ message = `Required environment value ${name.text} is missing.`;
55
+ } else if (value.includes("\0")) {
56
+ message = `Environment value ${name.text} contains an unsupported NUL byte.`;
57
+ }
58
+ if (message) {
59
+ diagnostics.push(error("PLC1011", message, positions.node(node)));
60
+ } else if (value !== undefined) {
61
+ edits.push({
62
+ start: node.getStart(source),
63
+ end: node.end,
64
+ text: JSON.stringify(value),
65
+ });
66
+ }
67
+ return;
68
+ }
69
+ }
70
+ ts.forEachChild(node, visit);
71
+ };
72
+ visit(source);
73
+ if (edits.length) {
74
+ sources.set(source.fileName, edited(source, edits));
75
+ }
76
+ }
77
+ return { sources, diagnostics };
71
78
  };
72
79
 
73
80
  /** URL expressions are compile-time values, never reads from mutable PLC state. */
74
- export const staticString = (checker: ts.TypeChecker, root: ts.Expression): string | undefined => {
75
- const active = new Set<ts.Node>();
76
- const valueNode = (node: ts.Node): ts.Node | undefined => {
77
- if (ts.isParenthesizedExpression(node)) return node.expression;
78
- if (ts.isIdentifier(node)) {
79
- const declaration = symbolAt(checker, node)?.declarations?.find(ts.isVariableDeclaration);
80
- if (
81
- declaration &&
82
- ts.isVariableDeclarationList(declaration.parent) &&
83
- declaration.parent.flags & ts.NodeFlags.Const
84
- )
85
- return declaration.initializer;
86
- }
87
- if (ts.isPropertyAccessExpression(node)) {
88
- const field = symbolAt(checker, node.name);
89
- if (
90
- !field?.declarations?.some(
91
- (declaration) =>
92
- ts.canHaveModifiers(declaration) &&
93
- ts
94
- .getModifiers(declaration)
95
- ?.some((modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword),
96
- )
97
- )
98
- return;
99
- const object = resolve(node.expression);
100
- if (object && ts.isObjectLiteralExpression(object)) {
101
- for (const property of object.properties) {
102
- if (
103
- ts.isPropertyAssignment(property) &&
104
- (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) &&
105
- property.name.text === node.name.text
106
- )
107
- return property.initializer;
108
- }
109
- }
110
- return;
111
- }
112
- return node;
113
- };
114
- const resolve = (node: ts.Node): ts.Node | undefined => {
115
- if (active.has(node)) return;
116
- active.add(node);
117
- try {
118
- const next = valueNode(node);
119
- return next && next !== node ? resolve(next) : next;
120
- } finally {
121
- active.delete(node);
122
- }
123
- };
124
- const read = (node: ts.Node): string | undefined => {
125
- const resolved = resolve(node);
126
- if (!resolved || active.has(resolved)) return;
127
- active.add(resolved);
128
- try {
129
- if (ts.isStringLiteral(resolved) || ts.isNoSubstitutionTemplateLiteral(resolved))
130
- return resolved.text;
131
- if (ts.isTemplateExpression(resolved)) {
132
- let value = resolved.head.text;
133
- for (const part of resolved.templateSpans) {
134
- const item = read(part.expression);
135
- if (item === undefined) return;
136
- value += item + part.literal.text;
137
- }
138
- return value;
139
- }
140
- if (
141
- ts.isBinaryExpression(resolved) &&
142
- resolved.operatorToken.kind === ts.SyntaxKind.PlusToken
143
- ) {
144
- const left = read(resolved.left),
145
- right = read(resolved.right);
146
- if (left !== undefined && right !== undefined) return left + right;
147
- }
148
- } finally {
149
- active.delete(resolved);
150
- }
151
- };
152
- return read(root);
81
+ export const staticString = (
82
+ checker: ts.TypeChecker,
83
+ root: ts.Expression,
84
+ ): string | undefined => {
85
+ const active = new Set<ts.Node>();
86
+ const valueNode = (node: ts.Node): ts.Node | undefined => {
87
+ if (ts.isParenthesizedExpression(node)) {
88
+ return node.expression;
89
+ }
90
+ if (ts.isIdentifier(node)) {
91
+ const declaration = symbolAt(checker, node)?.declarations?.find(
92
+ ts.isVariableDeclaration,
93
+ );
94
+ if (
95
+ declaration &&
96
+ ts.isVariableDeclarationList(declaration.parent) &&
97
+ declaration.parent.flags & ts.NodeFlags.Const
98
+ ) {
99
+ return declaration.initializer;
100
+ }
101
+ }
102
+ if (ts.isPropertyAccessExpression(node)) {
103
+ const field = symbolAt(checker, node.name);
104
+ if (
105
+ !field?.declarations?.some(
106
+ (declaration) =>
107
+ ts.canHaveModifiers(declaration) &&
108
+ ts
109
+ .getModifiers(declaration)
110
+ ?.some(
111
+ (modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword,
112
+ ),
113
+ )
114
+ ) {
115
+ return;
116
+ }
117
+ const object = resolve(node.expression);
118
+ if (object && ts.isObjectLiteralExpression(object)) {
119
+ for (const property of object.properties) {
120
+ if (
121
+ ts.isPropertyAssignment(property) &&
122
+ (ts.isIdentifier(property.name) ||
123
+ ts.isStringLiteral(property.name)) &&
124
+ property.name.text === node.name.text
125
+ ) {
126
+ return property.initializer;
127
+ }
128
+ }
129
+ }
130
+ return;
131
+ }
132
+ return node;
133
+ };
134
+ const resolve = (node: ts.Node): ts.Node | undefined => {
135
+ if (active.has(node)) {
136
+ return;
137
+ }
138
+ active.add(node);
139
+ try {
140
+ const next = valueNode(node);
141
+ return next && next !== node ? resolve(next) : next;
142
+ } finally {
143
+ active.delete(node);
144
+ }
145
+ };
146
+ const read = (node: ts.Node): string | undefined => {
147
+ const resolved = resolve(node);
148
+ if (!resolved || active.has(resolved)) {
149
+ return;
150
+ }
151
+ active.add(resolved);
152
+ try {
153
+ if (
154
+ ts.isStringLiteral(resolved) ||
155
+ ts.isNoSubstitutionTemplateLiteral(resolved)
156
+ ) {
157
+ return resolved.text;
158
+ }
159
+ if (ts.isTemplateExpression(resolved)) {
160
+ let value = resolved.head.text;
161
+ for (const part of resolved.templateSpans) {
162
+ const item = read(part.expression);
163
+ if (item === undefined) {
164
+ return;
165
+ }
166
+ value += item + part.literal.text;
167
+ }
168
+ return value;
169
+ }
170
+ if (
171
+ ts.isBinaryExpression(resolved) &&
172
+ resolved.operatorToken.kind === ts.SyntaxKind.PlusToken
173
+ ) {
174
+ const left = read(resolved.left);
175
+ const right = read(resolved.right);
176
+ if (left !== undefined && right !== undefined) {
177
+ return left + right;
178
+ }
179
+ }
180
+ } finally {
181
+ active.delete(resolved);
182
+ }
183
+ };
184
+ return read(root);
153
185
  };