@tsplc/compiler 0.5.4 → 0.5.6

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 (67) hide show
  1. package/dist/bun-runtime.js +14 -2
  2. package/dist/bun-runtime.js.map +4 -4
  3. package/dist/codesys.js +46 -2
  4. package/dist/codesys.js.map +3 -3
  5. package/dist/collections.js +395 -0
  6. package/dist/collections.js.map +10 -0
  7. package/dist/elaborate-capture.js +64 -5
  8. package/dist/elaborate-capture.js.map +4 -4
  9. package/dist/index.js +5345 -1074
  10. package/dist/index.js.map +32 -25
  11. package/dist/ir.js +122 -1
  12. package/dist/ir.js.map +6 -6
  13. package/package.json +14 -9
  14. package/src/codesys/numeric.ts +61 -0
  15. package/src/frontend/control-flow.ts +752 -0
  16. package/src/frontend/coroutines.ts +171 -7
  17. package/src/frontend/elaborate/array-searches.ts +216 -0
  18. package/src/frontend/elaborate/assignments.ts +352 -0
  19. package/src/frontend/elaborate/calls.ts +488 -49
  20. package/src/frontend/elaborate/capture-protocol.ts +19 -0
  21. package/src/frontend/elaborate/capture.ts +82 -3
  22. package/src/frontend/elaborate/dialect.ts +118 -4
  23. package/src/frontend/elaborate/expressions.ts +927 -80
  24. package/src/frontend/elaborate/filtered-arrays.ts +607 -0
  25. package/src/frontend/elaborate/frame.ts +44 -1
  26. package/src/frontend/elaborate/generics.ts +137 -0
  27. package/src/frontend/elaborate/graph.ts +91 -0
  28. package/src/frontend/elaborate/imports.ts +14 -15
  29. package/src/frontend/elaborate/index.ts +30 -5
  30. package/src/frontend/elaborate/initializers.ts +15 -1
  31. package/src/frontend/elaborate/iterators.ts +1677 -0
  32. package/src/frontend/elaborate/layouts.ts +41 -19
  33. package/src/frontend/elaborate/model.ts +64 -8
  34. package/src/frontend/elaborate/references.ts +749 -0
  35. package/src/frontend/elaborate/specialize.ts +459 -29
  36. package/src/frontend/elaborate/statements.ts +552 -133
  37. package/src/frontend/elaborate/storage.ts +151 -10
  38. package/src/frontend/elaborate/values.ts +72 -0
  39. package/src/frontend/elaborate/writes.ts +302 -1
  40. package/src/frontend/intrinsics.ts +32 -0
  41. package/src/frontend/module-graph.ts +2 -1
  42. package/src/frontend/positions.ts +12 -5
  43. package/src/frontend/sdk.ts +7 -1
  44. package/src/frontend/typescript.ts +5 -1
  45. package/src/ir/core-model.ts +2 -6
  46. package/src/ir/fold.ts +30 -1
  47. package/src/ir/index.ts +5 -0
  48. package/src/ir/model.ts +53 -2
  49. package/src/ir/scalars.ts +126 -0
  50. package/src/runtime/.runtime-VaUSOu/server/router.ts +17 -0
  51. package/src/runtime/.runtime-VaUSOu/src/cell.ts +45 -0
  52. package/src/runtime/.runtime-VaUSOu/src/controller.ts +58 -0
  53. package/src/runtime/.runtime-VaUSOu/src/lane.ts +6 -0
  54. package/src/runtime/.runtime-VaUSOu/src/main.ts +11 -0
  55. package/src/runtime/.runtime-VaUSOu/src/services.ts +8 -0
  56. package/src/runtime/.runtime-VaUSOu/tsconfig.json +13 -0
  57. package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +31 -0
  58. package/src/runtime/.runtime-ybIxYh/server/router.ts +17 -0
  59. package/src/runtime/.runtime-ybIxYh/src/cell.ts +45 -0
  60. package/src/runtime/.runtime-ybIxYh/src/controller.ts +58 -0
  61. package/src/runtime/.runtime-ybIxYh/src/lane.ts +6 -0
  62. package/src/runtime/.runtime-ybIxYh/src/main.ts +11 -0
  63. package/src/runtime/.runtime-ybIxYh/src/services.ts +8 -0
  64. package/src/runtime/.runtime-ybIxYh/tsconfig.json +13 -0
  65. package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +31 -0
  66. package/stdlib/collections.ts +514 -0
  67. package/types/index.d.ts +18 -0
@@ -0,0 +1,352 @@
1
+ import ts from "typescript";
2
+ import { type Mapped, mapped, type Part } from "../positions";
3
+ import type { DialectText } from "./dialect";
4
+ import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
5
+ import type { Frame } from "./frame";
6
+ import { constant, SpecializeError, type Value } from "./values";
7
+ import { isAssignment, isUpdate } from "./writes";
8
+
9
+ /** The operator each compound assignment applies. */
10
+ const compoundOperators = new Map<ts.SyntaxKind, string>([
11
+ [ts.SyntaxKind.PlusEqualsToken, "+"],
12
+ [ts.SyntaxKind.MinusEqualsToken, "-"],
13
+ [ts.SyntaxKind.AsteriskEqualsToken, "*"],
14
+ [ts.SyntaxKind.BarEqualsToken, "|"],
15
+ [ts.SyntaxKind.AmpersandEqualsToken, "&"],
16
+ ]);
17
+
18
+ /** Whether evaluating the expression writes a place: an assignment or `++`/`--` in it, outside functions it declares. */
19
+ export const containsWrite = (node: ts.Node): boolean =>
20
+ (ts.isBinaryExpression(node) && isAssignment(node.operatorToken.kind)) ||
21
+ isUpdate(node) ||
22
+ (!ts.isFunctionLike(node) && (ts.forEachChild(node, containsWrite) ?? false));
23
+
24
+ /**
25
+ * Assignments and steps. Structured Text assigns only in statements, so one used as a value is a
26
+ * statement first, and the expression reads the place afterwards.
27
+ */
28
+ export class AssignmentSpecializer {
29
+ private readonly checker: ts.TypeChecker;
30
+ private readonly dialect: DialectText;
31
+
32
+ constructor(
33
+ context: SpecializeContext,
34
+ private readonly expressions: ExpressionSpecializer,
35
+ ) {
36
+ this.checker = context.checker;
37
+ this.dialect = context.dialect;
38
+ }
39
+
40
+ /** Stores the assignment and returns what the target then holds. */
41
+ assign(node: ts.BinaryExpression, frame: Frame): Value {
42
+ if (
43
+ ts.isArrayLiteralExpression(node.left) ||
44
+ ts.isObjectLiteralExpression(node.left)
45
+ ) {
46
+ const value = this.expressions.expression(node.right, frame);
47
+ this.destructure(node.left, value, frame);
48
+ return value;
49
+ }
50
+ const target = this.expressions.references.target(node.left, frame);
51
+ if (target.kind === "each") {
52
+ this.expressions.references.each(target.choice, frame, (option) => {
53
+ this.assignTo(target.place(option), node, frame);
54
+ });
55
+ return constant(undefined);
56
+ }
57
+ return this.assignTo(target, node, frame);
58
+ }
59
+
60
+ private assignTo(
61
+ target: Value,
62
+ node: ts.BinaryExpression,
63
+ frame: Frame,
64
+ ): Value {
65
+ if (target.kind === "choice") {
66
+ return this.expressions.references.assign(target, node, frame);
67
+ }
68
+ const text = this.placeText(target, node.left);
69
+ const declared = target.kind === "code" ? target.declared : undefined;
70
+ const operator = node.operatorToken.kind;
71
+ if (operator === ts.SyntaxKind.QuestionQuestionEqualsToken) {
72
+ const value = this.expressions.expression(
73
+ node.right,
74
+ frame,
75
+ declared && this.checker.getNonNullableType(declared),
76
+ );
77
+ frame.emitter.open(
78
+ mapped`if (${this.dialect.isUndefinedText(text, declared)}) {`,
79
+ );
80
+ frame.emitter.line(
81
+ this.store(
82
+ target,
83
+ this.dialect.wrapDefined(
84
+ this.dialect.code(
85
+ value,
86
+ declared && this.checker.getNonNullableType(declared),
87
+ ),
88
+ declared,
89
+ ),
90
+ ),
91
+ );
92
+ frame.emitter.close();
93
+ return target;
94
+ }
95
+ const value = this.expressions.expression(node.right, frame, declared);
96
+ if (operator === ts.SyntaxKind.EqualsToken) {
97
+ frame.emitter.line(
98
+ this.store(
99
+ target,
100
+ this.dialect.assignmentText(
101
+ value,
102
+ declared,
103
+ target.kind === "code" ? target.iec : undefined,
104
+ ),
105
+ ),
106
+ );
107
+ return target;
108
+ }
109
+ const binary = compoundOperators.get(operator);
110
+ if (!binary) {
111
+ throw new SpecializeError(
112
+ node,
113
+ `${node.operatorToken.getText()} is not supported.`,
114
+ );
115
+ }
116
+ frame.emitter.line(
117
+ this.store(
118
+ target,
119
+ mapped`${text} ${binary} ${this.dialect.code(value, declared)}`,
120
+ ),
121
+ );
122
+ return target;
123
+ }
124
+
125
+ /** Steps the place by one; `x++` is worth what the place held before, `++x` what it holds after. */
126
+ step(
127
+ node: ts.PrefixUnaryExpression | ts.PostfixUnaryExpression,
128
+ frame: Frame,
129
+ { used }: { used: boolean },
130
+ ): Value {
131
+ const target = this.expressions.references.target(node.operand, frame);
132
+ if (target.kind === "each") {
133
+ this.expressions.references.each(target.choice, frame, (option) => {
134
+ this.stepOn(target.place(option), node, frame, { used: false });
135
+ });
136
+ return constant(undefined);
137
+ }
138
+ return this.stepOn(target, node, frame, { used });
139
+ }
140
+
141
+ private stepOn(
142
+ target: Value,
143
+ node: ts.PrefixUnaryExpression | ts.PostfixUnaryExpression,
144
+ frame: Frame,
145
+ { used }: { used: boolean },
146
+ ): Value {
147
+ const text = this.placeText(target, node.operand);
148
+ const before =
149
+ used && ts.isPostfixUnaryExpression(node)
150
+ ? this.expressions.held(target, frame)
151
+ : target;
152
+ frame.emitter.line(
153
+ this.store(
154
+ target,
155
+ mapped`${text} ${node.operator === ts.SyntaxKind.PlusPlusToken ? "+" : "-"} 1`,
156
+ ),
157
+ );
158
+ return before;
159
+ }
160
+
161
+ /**
162
+ * Assigns each target of a pattern its part of `value`. Every part is read before any target is
163
+ * written, so `[a, b] = [b, a]` swaps.
164
+ */
165
+ private destructure(
166
+ pattern: ts.ArrayLiteralExpression | ts.ObjectLiteralExpression,
167
+ value: Value,
168
+ frame: Frame,
169
+ ): void {
170
+ const writes: (() => void)[] = [];
171
+ this.collect(pattern, value, frame, writes);
172
+ for (const write of writes) {
173
+ write();
174
+ }
175
+ }
176
+
177
+ private collect(
178
+ pattern: ts.Expression,
179
+ value: Value,
180
+ frame: Frame,
181
+ writes: (() => void)[],
182
+ ): void {
183
+ if (ts.isArrayLiteralExpression(pattern)) {
184
+ pattern.elements.forEach((element, index) => {
185
+ if (ts.isOmittedExpression(element)) {
186
+ return;
187
+ }
188
+ if (ts.isSpreadElement(element)) {
189
+ throw new SpecializeError(
190
+ element,
191
+ "Assign the rest of an array element by element.",
192
+ );
193
+ }
194
+ this.collectTarget(
195
+ element,
196
+ this.expressions.element(value, constant(index), element, frame),
197
+ frame,
198
+ writes,
199
+ );
200
+ });
201
+ return;
202
+ }
203
+ if (ts.isObjectLiteralExpression(pattern)) {
204
+ for (const property of pattern.properties) {
205
+ if (ts.isShorthandPropertyAssignment(property)) {
206
+ const part = this.expressions.member(
207
+ value,
208
+ property.name.text,
209
+ property,
210
+ frame,
211
+ );
212
+ const target = property.objectAssignmentInitializer
213
+ ? this.withDefault(
214
+ part,
215
+ property.objectAssignmentInitializer,
216
+ frame,
217
+ )
218
+ : part;
219
+ this.collectTarget(property.name, target, frame, writes);
220
+ continue;
221
+ }
222
+ if (!ts.isPropertyAssignment(property)) {
223
+ throw new SpecializeError(
224
+ property,
225
+ "Assign each field of a pattern by name.",
226
+ );
227
+ }
228
+ this.collectTarget(
229
+ property.initializer,
230
+ this.expressions.member(
231
+ value,
232
+ property.name.getText(),
233
+ property,
234
+ frame,
235
+ ),
236
+ frame,
237
+ writes,
238
+ );
239
+ }
240
+ return;
241
+ }
242
+ throw new SpecializeError(pattern, "Not a pattern.");
243
+ }
244
+
245
+ /** One target of a pattern, with the default it takes when its part is undefined. */
246
+ private collectTarget(
247
+ target: ts.Expression,
248
+ part: Value,
249
+ frame: Frame,
250
+ writes: (() => void)[],
251
+ ): void {
252
+ if (
253
+ ts.isBinaryExpression(target) &&
254
+ target.operatorToken.kind === ts.SyntaxKind.EqualsToken
255
+ ) {
256
+ this.collectTarget(
257
+ target.left,
258
+ this.withDefault(part, target.right, frame),
259
+ frame,
260
+ writes,
261
+ );
262
+ return;
263
+ }
264
+ if (
265
+ ts.isArrayLiteralExpression(target) ||
266
+ ts.isObjectLiteralExpression(target)
267
+ ) {
268
+ this.collect(target, part, frame, writes);
269
+ return;
270
+ }
271
+ const read = this.expressions.held(part, frame);
272
+ writes.push(() => {
273
+ const place = this.expressions.expression(target, frame);
274
+ const declared = place.kind === "code" ? place.declared : undefined;
275
+ frame.emitter.line(
276
+ this.store(
277
+ place,
278
+ this.dialect.assignmentText(
279
+ read,
280
+ declared,
281
+ place.kind === "code" ? place.iec : undefined,
282
+ ),
283
+ ),
284
+ );
285
+ });
286
+ }
287
+
288
+ private withDefault(
289
+ part: Value,
290
+ fallback: ts.Expression,
291
+ frame: Frame,
292
+ ): Value {
293
+ if (part.kind === "const" && part.value === undefined) {
294
+ return this.expressions.expression(fallback, frame);
295
+ }
296
+ if (part.kind !== "code") {
297
+ return part;
298
+ }
299
+ const declared = part.declared;
300
+ if (!declared || !(declared.flags & ts.TypeFlags.Union)) {
301
+ return part;
302
+ }
303
+ const defined = this.checker.getNonNullableType(declared);
304
+ const otherwise = this.expressions.expression(fallback, frame, defined);
305
+ return {
306
+ kind: "code",
307
+ text: mapped`(${this.dialect.isUndefinedText(part.text, declared)} ? ${this.dialect.code(otherwise, defined)} : ${this.dialect.code(part, undefined)}${this.expressions.optionalValueSuffix(declared)})`,
308
+ declared: defined,
309
+ };
310
+ }
311
+
312
+ /** Stores a value into a place, converting it as an IEC assignment does. */
313
+ storeValue(place: Value, value: Value, at: ts.Node, frame: Frame): void {
314
+ this.placeText(place, at);
315
+ frame.emitter.line(
316
+ this.store(
317
+ place,
318
+ this.dialect.assignmentText(
319
+ value,
320
+ place.kind === "code" ? place.declared : undefined,
321
+ place.kind === "code" ? place.iec : undefined,
322
+ ),
323
+ ),
324
+ );
325
+ }
326
+
327
+ /** A statement storing `text` into a place, through its module's setter when it has one. */
328
+ private store(target: Value, text: Part): Mapped {
329
+ if (target.kind !== "code") {
330
+ throw new Error("Not a place.");
331
+ }
332
+ return target.setter
333
+ ? mapped`${target.setter}(${text});`
334
+ : mapped`${target.text} = ${text};`;
335
+ }
336
+
337
+ private placeText(value: Value, node: ts.Node): string | Mapped {
338
+ if (value.kind === "code" && value.copyOf) {
339
+ throw new SpecializeError(
340
+ node,
341
+ `This changes a copy: ${value.copyOf} returns stored state, which TypeScript would change in place but the PLC copies. Change it where it is stored, as a collection's update does.`,
342
+ );
343
+ }
344
+ if (value.kind === "code" && value.place) {
345
+ return value.text;
346
+ }
347
+ throw new SpecializeError(
348
+ node,
349
+ "Assign to stored state: a field scan code owns, or a let.",
350
+ );
351
+ }
352
+ }