@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/dist/ir.js CHANGED
@@ -2,6 +2,31 @@
2
2
  import { elementaryType, integerRange as integerRange2 } from "@tsplc/iec";
3
3
 
4
4
  // packages/compiler/src/ir/model.ts
5
+ var elementaryFunctions = [
6
+ "sqrt",
7
+ "ln",
8
+ "log10",
9
+ "exp",
10
+ "sin",
11
+ "cos",
12
+ "tan",
13
+ "asin",
14
+ "acos",
15
+ "atan"
16
+ ];
17
+ var elementaryValue = {
18
+ sqrt: Math.sqrt,
19
+ ln: Math.log,
20
+ log10: Math.log10,
21
+ exp: Math.exp,
22
+ sin: Math.sin,
23
+ cos: Math.cos,
24
+ tan: Math.tan,
25
+ asin: Math.asin,
26
+ acos: Math.acos,
27
+ atan: Math.atan
28
+ };
29
+ var isElementary = (operator) => elementaryFunctions.includes(operator);
5
30
  var comparisonOperators = new Set([
6
31
  "equal",
7
32
  "notEqual",
@@ -396,6 +421,12 @@ var foldUnary = (expression, value, target) => {
396
421
  value: String(BigInt(value.nanoseconds) / nanosecondsPerMillisecond)
397
422
  };
398
423
  }
424
+ if (value?.kind === "float" && isElementary(expression.operator)) {
425
+ return finiteFloat(elementaryValue[expression.operator](value.value), target);
426
+ }
427
+ if (value?.kind === "float" && expression.operator === "truncate") {
428
+ return finiteFloat(Math.trunc(value.value), target);
429
+ }
399
430
  if (value?.kind === "bool" && expression.operator === "not") {
400
431
  return { kind: "bool", value: !value.value };
401
432
  }
@@ -525,10 +556,16 @@ var floatArithmetic = (operator, left, right) => {
525
556
  return left / right;
526
557
  case "remainder":
527
558
  return left % right;
559
+ case "power":
560
+ return left ** right;
528
561
  default:
529
562
  return;
530
563
  }
531
564
  };
565
+ var finiteFloat = (value, target) => {
566
+ const rounded = target?.kind === "float" ? roundFloat(value, target) : value;
567
+ return Number.isFinite(rounded) ? { kind: "float", value: rounded } : undefined;
568
+ };
532
569
 
533
570
  // packages/compiler/src/ir/ranges.ts
534
571
  var interval = (expression, types, variables, ranges = new Map) => {
@@ -771,7 +808,15 @@ var exactConversionIssue = (expression, source, type, proof) => {
771
808
  return "Exact conversion cannot be proved lossless.";
772
809
  };
773
810
  var unaryIssue = (expression, source, type, proof) => {
811
+ if (isElementary(expression.operator)) {
812
+ if (source.kind !== "float" || source.id !== type.id) {
813
+ return "Elementary functions take and return one floating type.";
814
+ }
815
+ return domainIssue2(expression.operator, expression.operand, type, proof);
816
+ }
774
817
  switch (expression.operator) {
818
+ case "truncate":
819
+ return source.kind === "float" && source.id === type.id ? undefined : "trunc takes and returns one floating type.";
775
820
  case "clock":
776
821
  return type.id === "DINT" ? undefined : "now() returns DINT milliseconds.";
777
822
  case "timeToMilliseconds":
@@ -804,6 +849,12 @@ var binaryIssue = (expression, left, right, type, proof) => {
804
849
  }
805
850
  return right.id === type.id ? undefined : "Bit-string widths differ.";
806
851
  }
852
+ if (expression.operator === "power") {
853
+ if (type.kind !== "float" || left.id !== type.id || right.id !== type.id) {
854
+ return "A power takes and returns one floating type.";
855
+ }
856
+ return powerIssue(expression, type, proof);
857
+ }
807
858
  if (left.id !== type.id || right.id !== type.id || type.kind !== "integer" && type.kind !== "float") {
808
859
  return "Arithmetic operands need one integer or floating type.";
809
860
  }
@@ -823,6 +874,73 @@ var binaryIssue = (expression, left, right, type, proof) => {
823
874
  }
824
875
  return resultIssue(expression, type, proof);
825
876
  };
877
+ var floatBounds = (expression, proof) => {
878
+ const value = proof.fold(expression);
879
+ if (value?.kind === "float") {
880
+ return [value.value, value.value];
881
+ }
882
+ if (value?.kind === "integer") {
883
+ return [Number(value.value), Number(value.value)];
884
+ }
885
+ if (expression.kind === "convert" && expression.policy === "exact") {
886
+ const proved = proof.interval(expression.operand);
887
+ return proved && [Number(proved[0]), Number(proved[1])];
888
+ }
889
+ return;
890
+ };
891
+ var domains = {
892
+ sqrt: { holds: (low) => low >= 0, needs: "not negative" },
893
+ ln: { holds: (low) => low > 0, needs: "greater than zero" },
894
+ log10: { holds: (low) => low > 0, needs: "greater than zero" },
895
+ exp: {
896
+ holds: (_, high, type) => type.kind === "float" && Number.isFinite(roundFloat(Math.exp(high), type)),
897
+ needs: "small enough for a finite result"
898
+ },
899
+ asin: {
900
+ holds: (low, high) => low >= -1 && high <= 1,
901
+ needs: "between -1 and 1"
902
+ },
903
+ acos: {
904
+ holds: (low, high) => low >= -1 && high <= 1,
905
+ needs: "between -1 and 1"
906
+ },
907
+ sin: undefined,
908
+ cos: undefined,
909
+ tan: undefined,
910
+ atan: undefined
911
+ };
912
+ var domainIssue2 = (operator, operand, type, proof) => {
913
+ const domain = domains[operator];
914
+ if (!domain) {
915
+ return;
916
+ }
917
+ const bounds = floatBounds(operand, proof);
918
+ if (bounds && domain.holds(bounds[0], bounds[1], type)) {
919
+ return;
920
+ }
921
+ return `${operator} needs its operand proved ${domain.needs}, as a constant or an integer converted exactly is.`;
922
+ };
923
+ var powerIssue = (expression, type, proof) => {
924
+ const folded = proof.fold(expression);
925
+ if (folded) {
926
+ return constantFits(folded, type) ? undefined : "Floating result is not finite or representable.";
927
+ }
928
+ const exponent = proof.fold(expression.right);
929
+ const base = floatBounds(expression.left, proof);
930
+ if (exponent?.kind !== "float" || !base || type.kind !== "float") {
931
+ return "A power needs a constant exponent and a base proved within bounds, as a constant or an integer converted exactly is.";
932
+ }
933
+ const [low, high] = base;
934
+ if (!Number.isInteger(exponent.value) && low < 0) {
935
+ return "A fractional power needs its base proved not negative.";
936
+ }
937
+ if (exponent.value < 0 && low <= 0 && high >= 0) {
938
+ return "A negative power needs its base proved not zero.";
939
+ }
940
+ const magnitudes = [Math.abs(low), Math.abs(high)];
941
+ const largest = exponent.value >= 0 ? Math.max(...magnitudes) ** exponent.value : Math.min(...magnitudes) ** exponent.value;
942
+ return Number.isFinite(roundFloat(largest, type)) ? undefined : "The power may not be finite.";
943
+ };
826
944
  var resultIssue = (expression, type, proof) => {
827
945
  if (integerType(type)) {
828
946
  const result = proof.interval(expression);
@@ -3283,6 +3401,8 @@ export {
3283
3401
  domainConstantFits,
3284
3402
  domainIssue,
3285
3403
  durationLiteral,
3404
+ elementaryFunctions,
3405
+ elementaryValue,
3286
3406
  error,
3287
3407
  errorName,
3288
3408
  expressionIssue,
@@ -3297,6 +3417,7 @@ export {
3297
3417
  integerRange,
3298
3418
  integerType,
3299
3419
  interval,
3420
+ isElementary,
3300
3421
  isVerifiedCore,
3301
3422
  jobError,
3302
3423
  jobErrors,
@@ -3328,5 +3449,5 @@ export {
3328
3449
  wrapsNatively
3329
3450
  };
3330
3451
 
3331
- //# debugId=EED404D57725C8B464756E2164756E21
3452
+ //# debugId=6862F809FA52070464756E2164756E21
3332
3453
  //# sourceMappingURL=ir.js.map