@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
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "url": "git+https://github.com/rip-technologies/tsplc.git",
18
18
  "directory": "packages/compiler"
19
19
  },
20
- "version": "0.5.4",
20
+ "version": "0.5.6",
21
21
  "type": "module",
22
22
  "exports": {
23
23
  ".": {
@@ -60,6 +60,11 @@
60
60
  "bun": "./stdlib/math.ts",
61
61
  "default": "./dist/math.js"
62
62
  },
63
+ "./collections": {
64
+ "types": "./stdlib/collections.ts",
65
+ "bun": "./stdlib/collections.ts",
66
+ "default": "./dist/collections.js"
67
+ },
63
68
  "./types": {
64
69
  "types": "./types/index.d.ts",
65
70
  "default": "./types/index.d.ts"
@@ -101,17 +106,17 @@
101
106
  "bun": ">=1.4.3"
102
107
  },
103
108
  "dependencies": {
104
- "@tsplc/agent": "0.5.4",
105
- "@tsplc/build": "0.5.4",
106
- "@tsplc/config": "0.5.4",
107
- "@tsplc/debug": "0.5.4",
108
- "@tsplc/opcua": "0.5.4",
109
- "@tsplc/typegen": "0.5.4",
109
+ "@tsplc/agent": "0.5.6",
110
+ "@tsplc/build": "0.5.6",
111
+ "@tsplc/config": "0.5.6",
112
+ "@tsplc/debug": "0.5.6",
113
+ "@tsplc/opcua": "0.5.6",
114
+ "@tsplc/typegen": "0.5.6",
110
115
  "typescript": "6.0.3",
111
116
  "zod": "4.3.6",
112
117
  "@types/bun": "1.4.2",
113
- "@tsplc/server": "0.5.4",
114
- "@tsplc/iec": "0.5.4"
118
+ "@tsplc/server": "0.5.6",
119
+ "@tsplc/iec": "0.5.6"
115
120
  },
116
121
  "bin": {
117
122
  "tsplc": "./dist/cli.js"
@@ -2,10 +2,12 @@ import {
2
2
  type Constant,
3
3
  type CoreExpression,
4
4
  type CoreStatement,
5
+ type ElementaryFunction,
5
6
  floatMaximum,
6
7
  type GeneratedObject,
7
8
  integerRange,
8
9
  integerType,
10
+ isElementary,
9
11
  nestedBodies,
10
12
  type PlcType,
11
13
  pathIndexes,
@@ -13,6 +15,20 @@ import {
13
15
  subexpressions,
14
16
  } from "@tsplc/compiler/ir";
15
17
 
18
+ /** The Structured Text function each elementary function is. */
19
+ const elementaryNames: Readonly<Record<ElementaryFunction, string>> = {
20
+ sqrt: "SQRT",
21
+ ln: "LN",
22
+ log10: "LOG",
23
+ exp: "EXP",
24
+ sin: "SIN",
25
+ cos: "COS",
26
+ tan: "TAN",
27
+ asin: "ASIN",
28
+ acos: "ACOS",
29
+ atan: "ATAN",
30
+ };
31
+
16
32
  const operatorTokens: Readonly<Record<string, string>> = {
17
33
  add: "+",
18
34
  subtract: "-",
@@ -66,6 +82,9 @@ export class NumericLowering {
66
82
  if (value.kind === "convert" && value.policy === "saturate") {
67
83
  this.saturation(value.operand.type, value.type);
68
84
  }
85
+ if (value.kind === "unary" && value.operator === "truncate") {
86
+ this.truncation(value.type);
87
+ }
69
88
  }
70
89
 
71
90
  prepareStatements(items: readonly CoreStatement[]): void {
@@ -100,6 +119,12 @@ export class NumericLowering {
100
119
  if (value.operator === "clock") {
101
120
  return "TO_DINT(TIME_TO_UDINT(TIME()))";
102
121
  }
122
+ if (isElementary(value.operator)) {
123
+ return `${elementaryNames[value.operator]}(${operands[0]})`;
124
+ }
125
+ if (value.operator === "truncate") {
126
+ return `${this.truncation(value.type)}(${operands[0]})`;
127
+ }
103
128
  return value.operator === "not"
104
129
  ? `(NOT ${operands[0]})`
105
130
  : `TO_${value.type}(${value.operator === "negate" ? "-" : "NOT "}${operands[0]})`;
@@ -110,6 +135,9 @@ export class NumericLowering {
110
135
  if (value.policy) {
111
136
  return `${this.arithmetic(value.type, value.operator, value.policy)}(${operands.join(", ")})`;
112
137
  }
138
+ if (value.operator === "power") {
139
+ return `EXPT(${operands.join(", ")})`;
140
+ }
113
141
  if (value.operator === "shiftLeft" || value.operator === "shiftRight") {
114
142
  const shift = value.operator === "shiftLeft" ? "SHL" : "SHR";
115
143
  return `TO_${value.type}(${shift}(${operands.join(", ")}))`;
@@ -346,6 +374,39 @@ END_IF;
346
374
  );
347
375
  }
348
376
 
377
+ /**
378
+ * A float toward zero, as a float. TRUNC returns DINT, so the whole part goes through LINT, which
379
+ * holds every float below the size where each float is already whole; those stay as they are.
380
+ */
381
+ private truncation(typeId: string): string {
382
+ const type = this.types.get(typeId);
383
+ if (type?.kind !== "float") {
384
+ throw new Error("Expected verified float truncation");
385
+ }
386
+ const whole = scalarLiteral(
387
+ { kind: "float", value: 2 ** (type.bits === 32 ? 23 : 52) },
388
+ typeId,
389
+ );
390
+ return this.helper(
391
+ `truncate_${typeId}`,
392
+ typeId,
393
+ [["value", typeId]],
394
+ (name) => `IF ABS(value) >= ${whole} THEN
395
+ ${name} := value;
396
+ RETURN;
397
+ END_IF;
398
+ nearest := TO_LINT(value);
399
+ IF value >= 0 AND TO_${typeId}(nearest) > value THEN
400
+ nearest := nearest - 1;
401
+ ELSIF value < 0 AND TO_${typeId}(nearest) < value THEN
402
+ nearest := nearest + 1;
403
+ END_IF;
404
+ ${name} := TO_${typeId}(nearest);
405
+ `,
406
+ " nearest : LINT;",
407
+ );
408
+ }
409
+
349
410
  private saturation(sourceId: string, targetId: string): string {
350
411
  const source = this.types.get(sourceId);
351
412
  const target = this.types.get(targetId);