@tsplc/compiler 0.5.5 → 0.5.7

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 (80) hide show
  1. package/dist/bun-runtime.js +152 -31
  2. package/dist/bun-runtime.js.map +10 -9
  3. package/dist/cli.js +8 -5
  4. package/dist/cli.js.map +3 -3
  5. package/dist/codesys.js +260 -85
  6. package/dist/codesys.js.map +9 -9
  7. package/dist/collections.js +395 -0
  8. package/dist/collections.js.map +10 -0
  9. package/dist/elaborate-capture.js +62 -4
  10. package/dist/elaborate-capture.js.map +4 -4
  11. package/dist/index.js +5322 -1058
  12. package/dist/index.js.map +30 -23
  13. package/dist/ir.js +122 -1
  14. package/dist/ir.js.map +6 -6
  15. package/dist/project.js +8 -5
  16. package/dist/project.js.map +3 -3
  17. package/package.json +14 -9
  18. package/src/codesys/emit.ts +8 -2
  19. package/src/codesys/http.ts +114 -72
  20. package/src/codesys/instrumentation.ts +30 -6
  21. package/src/codesys/numeric.ts +61 -0
  22. package/src/codesys/routines.ts +39 -9
  23. package/src/codesys/st-files.ts +73 -6
  24. package/src/codesys/telemetry.ts +70 -35
  25. package/src/frontend/control-flow.ts +752 -0
  26. package/src/frontend/coroutines.ts +171 -7
  27. package/src/frontend/elaborate/array-searches.ts +216 -0
  28. package/src/frontend/elaborate/assignments.ts +352 -0
  29. package/src/frontend/elaborate/calls.ts +467 -42
  30. package/src/frontend/elaborate/capture-protocol.ts +19 -0
  31. package/src/frontend/elaborate/capture.ts +80 -2
  32. package/src/frontend/elaborate/dialect.ts +116 -2
  33. package/src/frontend/elaborate/expressions.ts +927 -80
  34. package/src/frontend/elaborate/filtered-arrays.ts +607 -0
  35. package/src/frontend/elaborate/frame.ts +44 -1
  36. package/src/frontend/elaborate/generics.ts +137 -0
  37. package/src/frontend/elaborate/graph.ts +91 -0
  38. package/src/frontend/elaborate/index.ts +30 -5
  39. package/src/frontend/elaborate/initializers.ts +15 -1
  40. package/src/frontend/elaborate/iterators.ts +1677 -0
  41. package/src/frontend/elaborate/layouts.ts +41 -19
  42. package/src/frontend/elaborate/model.ts +64 -8
  43. package/src/frontend/elaborate/references.ts +749 -0
  44. package/src/frontend/elaborate/specialize.ts +459 -29
  45. package/src/frontend/elaborate/statements.ts +552 -133
  46. package/src/frontend/elaborate/storage.ts +151 -10
  47. package/src/frontend/elaborate/values.ts +72 -0
  48. package/src/frontend/elaborate/writes.ts +302 -1
  49. package/src/frontend/intrinsics.ts +32 -0
  50. package/src/frontend/module-graph.ts +2 -1
  51. package/src/frontend/positions.ts +12 -5
  52. package/src/frontend/typescript.ts +5 -1
  53. package/src/ir/core-model.ts +2 -6
  54. package/src/ir/fold.ts +30 -1
  55. package/src/ir/index.ts +5 -0
  56. package/src/ir/model.ts +53 -2
  57. package/src/ir/scalars.ts +126 -0
  58. package/src/runtime/.runtime-VaUSOu/server/router.ts +17 -0
  59. package/src/runtime/.runtime-VaUSOu/src/cell.ts +45 -0
  60. package/src/runtime/.runtime-VaUSOu/src/controller.ts +58 -0
  61. package/src/runtime/.runtime-VaUSOu/src/lane.ts +6 -0
  62. package/src/runtime/.runtime-VaUSOu/src/main.ts +11 -0
  63. package/src/runtime/.runtime-VaUSOu/src/services.ts +8 -0
  64. package/src/runtime/.runtime-VaUSOu/tsconfig.json +13 -0
  65. package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +31 -0
  66. package/src/runtime/.runtime-ybIxYh/server/router.ts +17 -0
  67. package/src/runtime/.runtime-ybIxYh/src/cell.ts +45 -0
  68. package/src/runtime/.runtime-ybIxYh/src/controller.ts +58 -0
  69. package/src/runtime/.runtime-ybIxYh/src/lane.ts +6 -0
  70. package/src/runtime/.runtime-ybIxYh/src/main.ts +11 -0
  71. package/src/runtime/.runtime-ybIxYh/src/services.ts +8 -0
  72. package/src/runtime/.runtime-ybIxYh/tsconfig.json +13 -0
  73. package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +31 -0
  74. package/src/runtime/async.ts +15 -12
  75. package/src/runtime/launch.ts +11 -4
  76. package/src/runtime/runtime.ts +26 -23
  77. package/src/runtime/telemetry.ts +14 -6
  78. package/src/runtime/transport.ts +172 -0
  79. package/stdlib/collections.ts +514 -0
  80. package/types/index.d.ts +18 -0
@@ -2,7 +2,7 @@ import { primitiveTypes } from "@tsplc/compiler/ir";
2
2
  import ts from "typescript";
3
3
  import type { CapturedValue } from "./capture-protocol";
4
4
  import { ModuleImports } from "./imports";
5
- import type { Model } from "./model";
5
+ import type { Model, ModelNode } from "./model";
6
6
 
7
7
  /** IEC names a program can write as types; they keep their width rather than becoming DINT. */
8
8
  const iecNames = new Set(primitiveTypes.map((type) => type.id));
@@ -72,10 +72,12 @@ export class Layouts {
72
72
  }
73
73
  }
74
74
 
75
+ /** A declaration name no layout has, storage's instance states among them. */
75
76
  private unique(base: string): string {
76
77
  const count = this.names.get(base) ?? 0;
77
78
  this.names.set(base, count + 1);
78
- return count ? `${base}_${count + 1}` : base;
79
+ const name = count ? `${base}_${count + 1}` : base;
80
+ return this.declarations.has(name) ? this.unique(base) : name;
79
81
  }
80
82
 
81
83
  /** Each string-literal union's enum, with the string each member stands for. */
@@ -100,9 +102,7 @@ export class Layouts {
100
102
  type: ts.Type,
101
103
  ): { members: string[]; optional: boolean } | undefined {
102
104
  const parts = type.isUnion() ? type.types : [type];
103
- const defined = parts.filter(
104
- (part) => !(part.flags & ts.TypeFlags.Undefined),
105
- );
105
+ const defined = parts.filter((part) => !isAbsent(part));
106
106
  const members = defined.flatMap((part) =>
107
107
  part.isStringLiteral() ? [part.value] : [],
108
108
  );
@@ -161,9 +161,7 @@ export class Layouts {
161
161
  if (!type.isUnion()) {
162
162
  return undefined;
163
163
  }
164
- const variants = type.types.filter(
165
- (part) => !(part.flags & ts.TypeFlags.Undefined),
166
- );
164
+ const variants = type.types.filter((part) => !isAbsent(part));
167
165
  const [first, ...others] = variants;
168
166
  if (
169
167
  !first ||
@@ -239,10 +237,8 @@ export class Layouts {
239
237
 
240
238
  /** Whether `T | undefined` is stored as `{ has, value }`; a literal union has an Undefined member instead. */
241
239
  isOptionalRecord(type: ts.Type): boolean {
242
- if (
243
- !type.isUnion() ||
244
- !type.types.some((part) => part.flags & ts.TypeFlags.Undefined)
245
- ) {
240
+ bothAbsent(this.checker, type);
241
+ if (!type.isUnion() || !type.types.some((part) => isAbsent(part))) {
246
242
  return false;
247
243
  }
248
244
  return !this.literalEnum(type);
@@ -362,6 +358,11 @@ export class Layouts {
362
358
  if (iec) {
363
359
  return iec;
364
360
  }
361
+ // A class instance in a record stores the state its class holds, as one in a field does.
362
+ const node = value && this.model.node(value);
363
+ if (node?.captured.kind === "instance" && this.instanceState) {
364
+ return this.instanceState(node) ?? "boolean";
365
+ }
365
366
  const elementName = iecTypeName(arrayElement(annotation(declaration)));
366
367
  if (elementName) {
367
368
  const captured = value && this.model.node(value);
@@ -375,6 +376,9 @@ export class Layouts {
375
376
  return this.text(type, value, hint);
376
377
  }
377
378
 
379
+ /** The stored layout of a class instance, which storage, built on these layouts, gives once it exists. */
380
+ instanceState?: (node: ModelNode) => string | undefined;
381
+
378
382
  /** A record field's layout, with the lengths setup gave its arrays. */
379
383
  fieldText(property: ts.Symbol): string {
380
384
  return this.declaredText(
@@ -524,17 +528,18 @@ export class Layouts {
524
528
  return `${property.name}: ${this.declaredText(property.valueDeclaration, this.checker.getTypeOfSymbol(property), captured ?? this.fieldSample(property), pascalCase(property.name))};`;
525
529
  });
526
530
  const body = fields.join(" ");
527
- const base = type.aliasSymbol?.name ?? type.getSymbol()?.name;
528
- const key = `${base}:${body}`;
531
+ const symbolName = type.aliasSymbol?.name ?? type.getSymbol()?.name;
532
+ // A type literal and an object literal with the same fields are one shape, so values pass between them unconverted.
533
+ const base =
534
+ symbolName === "__type" || symbolName === "__object"
535
+ ? undefined
536
+ : symbolName;
537
+ const key = `${base ?? ""}:${body}`;
529
538
  const known = this.records.get(key);
530
539
  if (known) {
531
540
  return known;
532
541
  }
533
- const name = this.unique(
534
- base && base !== "__type" && base !== "__object"
535
- ? base
536
- : (hint ?? "Fields"),
537
- );
542
+ const name = this.unique(base ?? hint ?? "Fields");
538
543
  this.records.set(key, name);
539
544
  this.declarations.set(name, `export interface ${name} { ${body} }`);
540
545
  return name;
@@ -613,3 +618,20 @@ export const enumMember = (value: string): string => {
613
618
  const name = pascalCase(value);
614
619
  return /^[A-Za-z_]/.test(name) ? name : `_${name}`;
615
620
  };
621
+
622
+ /** `null` and `undefined` are one empty value in the PLC: an optional's Undefined member, or `has` false. */
623
+ export const isAbsent = (type: ts.Type): boolean =>
624
+ Boolean(type.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null));
625
+
626
+ /** A type holding both null and undefined would need two empty values, which the PLC's optionals don't have. */
627
+ const bothAbsent = (checker: ts.TypeChecker, type: ts.Type): void => {
628
+ if (
629
+ type.isUnion() &&
630
+ type.types.some((part) => part.flags & ts.TypeFlags.Null) &&
631
+ type.types.some((part) => part.flags & ts.TypeFlags.Undefined)
632
+ ) {
633
+ throw new Error(
634
+ `${checker.typeToString(type)}: null and undefined are one empty value in the PLC; use one of them.`,
635
+ );
636
+ }
637
+ };
@@ -64,7 +64,8 @@ type ClassField = ts.PropertyDeclaration | ts.ParameterPropertyDeclaration;
64
64
  export interface Root {
65
65
  file: string;
66
66
  name: string;
67
- declaration: ts.VariableDeclaration;
67
+ /** The variable, or the element of a destructuring declaration, that names the root. */
68
+ declaration: ts.VariableDeclaration | ts.BindingElement;
68
69
  exported: boolean;
69
70
  /** What setup bound it to. */
70
71
  value: CapturedValue;
@@ -130,16 +131,31 @@ export class Model {
130
131
  return node;
131
132
  }
132
133
 
133
- private binding(file: string, name: string): ts.VariableDeclaration {
134
+ private binding(
135
+ file: string,
136
+ name: string,
137
+ ): ts.VariableDeclaration | ts.BindingElement {
134
138
  const source = this.program.getSourceFile(file);
139
+ const named = (
140
+ declaration: ts.VariableDeclaration | ts.BindingElement,
141
+ ): ts.VariableDeclaration | ts.BindingElement | undefined => {
142
+ if (ts.isIdentifier(declaration.name)) {
143
+ return declaration.name.text === name ? declaration : undefined;
144
+ }
145
+ for (const element of declaration.name.elements) {
146
+ const found = ts.isBindingElement(element) && named(element);
147
+ if (found) {
148
+ return found;
149
+ }
150
+ }
151
+ return undefined;
152
+ };
135
153
  for (const statement of source?.statements ?? []) {
136
154
  if (ts.isVariableStatement(statement)) {
137
155
  for (const declaration of statement.declarationList.declarations) {
138
- if (
139
- ts.isIdentifier(declaration.name) &&
140
- declaration.name.text === name
141
- ) {
142
- return declaration;
156
+ const found = named(declaration);
157
+ if (found) {
158
+ return found;
143
159
  }
144
160
  }
145
161
  }
@@ -156,6 +172,15 @@ export class Model {
156
172
  return value && this.node(value);
157
173
  }
158
174
 
175
+ /** A program source by its file name. */
176
+ sourceFile(file: string): ts.SourceFile {
177
+ const source = this.program.getSourceFile(file);
178
+ if (!source) {
179
+ throw new Error(`${file} is not part of the program.`);
180
+ }
181
+ return source;
182
+ }
183
+
159
184
  /** The class a captured instance belongs to. */
160
185
  classOf(node: ModelNode): ts.ClassDeclaration {
161
186
  const captured = node.captured;
@@ -184,8 +209,33 @@ export class Model {
184
209
  );
185
210
  }
186
211
 
187
- /** A class's fields in declaration order: properties and constructor parameter properties. */
212
+ /** The class and the classes it extends, the class first. */
213
+ ancestry(declaration: ts.ClassDeclaration): ts.ClassDeclaration[] {
214
+ const base = declaration.heritageClauses?.find(
215
+ (clause) => clause.token === ts.SyntaxKind.ExtendsKeyword,
216
+ )?.types[0]?.expression;
217
+ const symbol = base && this.checker.getSymbolAtLocation(base);
218
+ const resolved =
219
+ symbol && symbol.flags & ts.SymbolFlags.Alias
220
+ ? this.checker.getAliasedSymbol(symbol)
221
+ : symbol;
222
+ const parent = resolved?.declarations?.find(ts.isClassDeclaration);
223
+ return parent ? [declaration, ...this.ancestry(parent)] : [declaration];
224
+ }
225
+
226
+ /** A class's own and inherited members, nearest first, as a lookup finds an override before what it overrides. */
227
+ members(declaration: ts.ClassDeclaration): ts.ClassElement[] {
228
+ return this.ancestry(declaration).flatMap((owner) => [...owner.members]);
229
+ }
230
+
231
+ /** A class's fields, inherited ones first, each in declaration order: properties and constructor parameter properties. */
188
232
  fields(declaration: ts.ClassDeclaration): ClassField[] {
233
+ return this.ancestry(declaration)
234
+ .reverse()
235
+ .flatMap((owner) => this.ownFields(owner));
236
+ }
237
+
238
+ private ownFields(declaration: ts.ClassDeclaration): ClassField[] {
189
239
  return declaration.members.flatMap((member): ClassField[] => {
190
240
  if (ts.isPropertyDeclaration(member)) {
191
241
  return [member];
@@ -203,6 +253,12 @@ export class Model {
203
253
  /** The declared type of a field of a captured instance or record. */
204
254
  capturedFieldType(holder: ModelNode, name: string): ts.Type | undefined {
205
255
  if (holder.captured.kind === "instance") {
256
+ // An instance of a generic class has its fields at its own type arguments.
257
+ const instantiated =
258
+ holder.type && this.checker.getPropertyOfType(holder.type, name);
259
+ if (instantiated) {
260
+ return this.checker.getTypeOfSymbol(instantiated);
261
+ }
206
262
  const member = this.member(this.classOf(holder), name);
207
263
  return member && this.checker.getTypeAtLocation(member.name);
208
264
  }