@tsplc/tmp-compiler 0.0.0-stage → 0.6.3

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 (185) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +41 -2
  3. package/dist/cli.js +2325 -0
  4. package/dist/cli.js.map +26 -0
  5. package/dist/clock.js +11 -0
  6. package/dist/clock.js.map +11 -0
  7. package/dist/codesys.js +6563 -0
  8. package/dist/codesys.js.map +51 -0
  9. package/dist/collections.js +399 -0
  10. package/dist/collections.js.map +10 -0
  11. package/dist/index.js +16198 -0
  12. package/dist/index.js.map +62 -0
  13. package/dist/ir.js +3948 -0
  14. package/dist/ir.js.map +35 -0
  15. package/dist/retain.js +10 -0
  16. package/dist/retain.js.map +10 -0
  17. package/dist/signals.js +115 -0
  18. package/dist/signals.js.map +10 -0
  19. package/dist/telemetry.js +121 -0
  20. package/dist/telemetry.js.map +12 -0
  21. package/package.json +122 -4
  22. package/src/codesys/config.ts +50 -0
  23. package/src/codesys/debug.ts +655 -0
  24. package/src/codesys/emit.ts +185 -0
  25. package/src/codesys/endpoints.ts +111 -0
  26. package/src/codesys/engineering.ts +96 -0
  27. package/src/codesys/explain/expressions.ts +445 -0
  28. package/src/codesys/explain/flags.ts +134 -0
  29. package/src/codesys/explain/parameters.ts +53 -0
  30. package/src/codesys/explain/rows.ts +182 -0
  31. package/src/codesys/explain/tables.ts +79 -0
  32. package/src/codesys/explain/tasks.ts +68 -0
  33. package/src/codesys/explain/writes.ts +497 -0
  34. package/src/codesys/explain.ts +149 -0
  35. package/src/codesys/expressions.ts +100 -0
  36. package/src/codesys/global-lists.ts +284 -0
  37. package/src/codesys/global-names.ts +66 -0
  38. package/src/codesys/http.ts +1090 -0
  39. package/src/codesys/index.ts +13 -0
  40. package/src/codesys/instrumentation.ts +574 -0
  41. package/src/codesys/io-paths.ts +149 -0
  42. package/src/codesys/loops.ts +101 -0
  43. package/src/codesys/manifest.ts +684 -0
  44. package/src/codesys/names.ts +58 -0
  45. package/src/codesys/numeric.ts +516 -0
  46. package/src/codesys/plan.ts +688 -0
  47. package/src/codesys/programs.ts +138 -0
  48. package/src/codesys/publication.ts +99 -0
  49. package/src/codesys/required.ts +8 -0
  50. package/src/codesys/routines.ts +247 -0
  51. package/src/codesys/scan-clock.ts +103 -0
  52. package/src/codesys/scheduling.ts +96 -0
  53. package/src/codesys/service-channels.ts +24 -0
  54. package/src/codesys/sources.ts +118 -0
  55. package/src/codesys/st-files.ts +219 -0
  56. package/src/codesys/statements.ts +409 -0
  57. package/src/codesys/storage.ts +150 -0
  58. package/src/codesys/symbols.ts +240 -0
  59. package/src/codesys/task-access.ts +186 -0
  60. package/src/codesys/telemetry-settings.ts +19 -0
  61. package/src/codesys/telemetry.ts +852 -0
  62. package/src/codesys/types.ts +310 -0
  63. package/src/codesys/vendor.ts +113 -0
  64. package/src/frontend/analyzer.ts +779 -0
  65. package/src/frontend/async.ts +530 -0
  66. package/src/frontend/blocks.ts +578 -0
  67. package/src/frontend/configuration.ts +83 -0
  68. package/src/frontend/const-enums.ts +196 -0
  69. package/src/frontend/const-functions.ts +204 -0
  70. package/src/frontend/control-flow.ts +787 -0
  71. package/src/frontend/cooperative.ts +114 -0
  72. package/src/frontend/coroutines.ts +1344 -0
  73. package/src/frontend/dialect-names.ts +104 -0
  74. package/src/frontend/elaborate/array-searches.ts +224 -0
  75. package/src/frontend/elaborate/assignments.ts +343 -0
  76. package/src/frontend/elaborate/calls.ts +1342 -0
  77. package/src/frontend/elaborate/capture-protocol.ts +237 -0
  78. package/src/frontend/elaborate/capture.ts +388 -0
  79. package/src/frontend/elaborate/declarations.ts +124 -0
  80. package/src/frontend/elaborate/dialect.ts +473 -0
  81. package/src/frontend/elaborate/expressions.ts +2177 -0
  82. package/src/frontend/elaborate/filtered-arrays.ts +643 -0
  83. package/src/frontend/elaborate/frame.ts +145 -0
  84. package/src/frontend/elaborate/generics.ts +123 -0
  85. package/src/frontend/elaborate/graph.ts +141 -0
  86. package/src/frontend/elaborate/imports.ts +105 -0
  87. package/src/frontend/elaborate/index.ts +851 -0
  88. package/src/frontend/elaborate/initializers.ts +235 -0
  89. package/src/frontend/elaborate/iterators.ts +1541 -0
  90. package/src/frontend/elaborate/layouts.ts +1025 -0
  91. package/src/frontend/elaborate/model.ts +478 -0
  92. package/src/frontend/elaborate/names.ts +134 -0
  93. package/src/frontend/elaborate/reduce.ts +900 -0
  94. package/src/frontend/elaborate/references.ts +727 -0
  95. package/src/frontend/elaborate/retention.ts +284 -0
  96. package/src/frontend/elaborate/specialize.ts +1137 -0
  97. package/src/frontend/elaborate/statements.ts +1220 -0
  98. package/src/frontend/elaborate/storage.ts +836 -0
  99. package/src/frontend/elaborate/time-constructors.ts +34 -0
  100. package/src/frontend/elaborate/values.ts +200 -0
  101. package/src/frontend/elaborate/writes.ts +415 -0
  102. package/src/frontend/expressions.ts +1382 -0
  103. package/src/frontend/generated-names.ts +95 -0
  104. package/src/frontend/intrinsics.ts +382 -0
  105. package/src/frontend/lowering-key.ts +23 -0
  106. package/src/frontend/module-graph.ts +156 -0
  107. package/src/frontend/positions.ts +359 -0
  108. package/src/frontend/promise-chains.ts +872 -0
  109. package/src/frontend/promises.ts +419 -0
  110. package/src/frontend/routines.ts +239 -0
  111. package/src/frontend/sdk.ts +187 -0
  112. package/src/frontend/services.ts +612 -0
  113. package/src/frontend/statements.ts +871 -0
  114. package/src/frontend/tasks.ts +526 -0
  115. package/src/frontend/telemetry.ts +174 -0
  116. package/src/frontend/types.ts +1073 -0
  117. package/src/frontend/typescript.ts +659 -0
  118. package/src/index.ts +9 -0
  119. package/src/ir/aggregates.ts +291 -0
  120. package/src/ir/allocation.ts +96 -0
  121. package/src/ir/conditions.ts +855 -0
  122. package/src/ir/core-model.ts +289 -0
  123. package/src/ir/declarations.ts +281 -0
  124. package/src/ir/diagnostics.ts +68 -0
  125. package/src/ir/domains.ts +151 -0
  126. package/src/ir/fold.ts +279 -0
  127. package/src/ir/identity.ts +19 -0
  128. package/src/ir/index.ts +137 -0
  129. package/src/ir/job-errors.ts +69 -0
  130. package/src/ir/layouts.ts +211 -0
  131. package/src/ir/link.ts +302 -0
  132. package/src/ir/model.ts +394 -0
  133. package/src/ir/normalization-error.ts +10 -0
  134. package/src/ir/normalize.ts +52 -0
  135. package/src/ir/numeric.ts +163 -0
  136. package/src/ir/operands.ts +160 -0
  137. package/src/ir/ranges.ts +420 -0
  138. package/src/ir/routine.ts +1116 -0
  139. package/src/ir/scalars.ts +453 -0
  140. package/src/ir/share.ts +790 -0
  141. package/src/ir/short-circuit.ts +140 -0
  142. package/src/ir/tables.ts +111 -0
  143. package/src/ir/temporaries.ts +138 -0
  144. package/src/ir/types.ts +151 -0
  145. package/src/project/artifacts.ts +424 -0
  146. package/src/project/cli.ts +211 -0
  147. package/src/project/debugger.ts +111 -0
  148. package/src/project/deployments.ts +387 -0
  149. package/src/project/flags.ts +14 -0
  150. package/src/project/git.ts +35 -0
  151. package/src/project/plant.ts +629 -0
  152. package/src/project/project-commands.ts +507 -0
  153. package/src/project/project.ts +559 -0
  154. package/src/project/release.ts +152 -0
  155. package/src/project/server-check.ts +54 -0
  156. package/src/project/server.ts +165 -0
  157. package/src/project/terminal.ts +45 -0
  158. package/src/runtime/async.ts +207 -0
  159. package/src/runtime/launch.ts +59 -0
  160. package/src/runtime/main.ts +59 -0
  161. package/src/runtime/missing-file.ts +3 -0
  162. package/src/runtime/process.ts +177 -0
  163. package/src/runtime/program.ts +109 -0
  164. package/src/runtime/retain.ts +108 -0
  165. package/src/runtime/runtime.ts +324 -0
  166. package/src/runtime/settings.ts +27 -0
  167. package/src/runtime/storage.ts +456 -0
  168. package/src/runtime/symbols.ts +97 -0
  169. package/src/runtime/telemetry.ts +154 -0
  170. package/src/runtime/transport.ts +168 -0
  171. package/stdlib/clock.ts +8 -0
  172. package/stdlib/collections.ts +534 -0
  173. package/stdlib/plc-time.ts +2 -0
  174. package/stdlib/retain.ts +4 -0
  175. package/stdlib/signals.ts +142 -0
  176. package/stdlib/telemetry.ts +89 -0
  177. package/types/async.d.ts +87 -0
  178. package/types/clock.d.ts +8 -0
  179. package/types/config.d.ts +2 -0
  180. package/types/data-types.d.ts +31 -0
  181. package/types/dialect.d.ts +49 -0
  182. package/types/globals.d.ts +10 -0
  183. package/types/index.d.ts +212 -0
  184. package/types/retain.d.ts +12 -0
  185. package/types/telemetry.d.ts +47 -0
@@ -0,0 +1,235 @@
1
+ import ts from "typescript";
2
+ import type { CapturedValue } from "./capture-protocol";
3
+ import {
4
+ definedOptional,
5
+ holdsInstances,
6
+ type Layouts,
7
+ recordTypes,
8
+ } from "./layouts";
9
+ import type { Model, ModelNode } from "./model";
10
+ import { noReference } from "./references";
11
+ import { mixedStateText, type Storage } from "./storage";
12
+ import { calendarConstructor } from "./time-constructors";
13
+
14
+ /** Values setup captured as dialect text: storage initializers, and fixed data scan code reads. */
15
+ export class Initializers {
16
+ /** SDK functions the text written since `takeSdkCalls` last ran calls, which its module imports. */
17
+ private readonly sdkCalls = new Set<string>();
18
+
19
+ constructor(
20
+ private readonly model: Model,
21
+ private readonly checker: ts.TypeChecker,
22
+ private readonly layouts: Layouts,
23
+ private readonly storage: Storage,
24
+ ) {}
25
+
26
+ /** A captured value as dialect text of its declared type. */
27
+ valueText(value: CapturedValue, declared: ts.Type, layout?: string): string {
28
+ const node = this.model.node(value);
29
+ const literal = this.layouts.literalEnum(declared);
30
+ if (literal) {
31
+ return this.layouts.member(
32
+ literal,
33
+ value.kind === "string" ? value.value : undefined,
34
+ );
35
+ }
36
+ const optional = this.layouts.isOptionalRecord(declared);
37
+ if (optional) {
38
+ const inner = this.checker.getNonNullableType(declared);
39
+ const name = layout ?? this.layouts.text(declared, value);
40
+ return value.kind === "undefined"
41
+ ? `defaultValue<${name}>()`
42
+ : definedOptional(this.valueText(value, inner)).text;
43
+ }
44
+ if (this.layouts.enumDeclaration(declared) && value.kind === "number") {
45
+ return (
46
+ this.layouts.enumMember(declared, value.value) ?? String(value.value)
47
+ );
48
+ }
49
+ // Setup's TIME is a number of milliseconds; the dialect spells a TIME constant as a duration.
50
+ if (
51
+ value.kind === "number" &&
52
+ (layout ?? declared.aliasSymbol?.name) === "TIME"
53
+ ) {
54
+ this.sdkCalls.add("ms");
55
+ return `ms(${value.value})`;
56
+ }
57
+ // Setup's calendar value is the text it was written in, which its constructor takes.
58
+ const calendar = calendarConstructor(
59
+ layout ?? declared.aliasSymbol?.name ?? "",
60
+ );
61
+ if (value.kind === "string" && calendar) {
62
+ this.sdkCalls.add(calendar);
63
+ return `${calendar}(${JSON.stringify(value.value)})`;
64
+ }
65
+ if (!node) {
66
+ return this.literal(value);
67
+ }
68
+ if (node.captured.kind === "array") {
69
+ const element = this.model.elementType(declared);
70
+ // Instances of different classes are stored side by side, a field each.
71
+ const mixed = this.storage.mixedStates(node);
72
+ const items = node.captured.items.map((item) =>
73
+ element ? this.valueText(item, element) : this.literal(item),
74
+ );
75
+ if (mixed) {
76
+ return mixedStateText(items);
77
+ }
78
+ if (!items.length) {
79
+ return `defaultValue<${layout ?? this.layouts.text(declared, value)}>()`;
80
+ }
81
+ return `[${items.join(", ")}]`;
82
+ }
83
+ if (node.captured.kind === "object") {
84
+ return this.recordInitializer(node, declared);
85
+ }
86
+ if (node.captured.kind === "instance") {
87
+ const text = this.storage.storageType(node);
88
+ return text ? this.instanceInitializer(node) : "{}";
89
+ }
90
+ throw new Error("A function has no stored value.");
91
+ }
92
+
93
+ /** The SDK functions of `@tsplc/compiler/types` the text written since the last call calls. */
94
+ takeSdkCalls(): string[] {
95
+ const names = [...this.sdkCalls];
96
+ this.sdkCalls.clear();
97
+ return names;
98
+ }
99
+
100
+ private instanceInitializer(node: ModelNode): string {
101
+ return `{ ${this.storage
102
+ .instanceFields(node)
103
+ .map(
104
+ ([name, layout]) =>
105
+ `${name}: ${this.instanceFieldInitializer(node, name, layout)}`,
106
+ )
107
+ .join(", ")} }`;
108
+ }
109
+
110
+ /** The initial value of one of an instance's stored fields. */
111
+ instanceFieldInitializer(
112
+ node: ModelNode,
113
+ name: string,
114
+ layout: string,
115
+ ): string {
116
+ const captured = node.captured;
117
+ if (captured.kind !== "instance") {
118
+ throw new Error("Not an instance.");
119
+ }
120
+ const value = this.model.field(node, name);
121
+ const inner = this.model.node(value);
122
+ if (this.storage.referenceField(node, name)) {
123
+ return inner ? String(inner.reference) : noReference;
124
+ }
125
+ if (inner?.captured.kind === "instance") {
126
+ return this.instanceInitializer(inner);
127
+ }
128
+ if (inner?.captured.kind === "array" && holdsInstances(this.model, inner)) {
129
+ const states = inner.captured.items.map((item) => {
130
+ const instance = this.model.node(item);
131
+ if (!instance) {
132
+ throw new Error(`${name} holds a primitive among its instances.`);
133
+ }
134
+ return this.instanceInitializer(instance);
135
+ });
136
+ return this.storage.mixedStates(inner)
137
+ ? mixedStateText(states)
138
+ : `[${states.join(", ")}]`;
139
+ }
140
+ const declared = this.model.capturedFieldType(node, name);
141
+ if (!declared) {
142
+ throw new Error(`${name} has no declared type.`);
143
+ }
144
+ return this.valueText(value, declared, layout);
145
+ }
146
+
147
+ private recordInitializer(node: ModelNode, declared: ts.Type): string {
148
+ const captured = node.captured;
149
+ if (captured.kind !== "object") {
150
+ throw new Error("Not a record.");
151
+ }
152
+ const tagged = this.layouts.tagged(declared);
153
+ const record = recordTypes(declared);
154
+ const recordType = record
155
+ ? declared
156
+ : this.model.variant(declared, captured);
157
+ const properties = tagged
158
+ ? this.layouts.taggedFields(tagged)
159
+ : this.checker
160
+ .getPropertiesOfType(recordType)
161
+ .map((property) => property.name);
162
+ const fields = properties.map((name): [string, string] => {
163
+ const field = captured.fields[name];
164
+ if (tagged && name === tagged.tag) {
165
+ const tag = this.layouts.tagEnum(declared, tagged);
166
+ return [
167
+ name,
168
+ this.layouts.member(
169
+ tag,
170
+ field?.kind === "string" ? field.value : undefined,
171
+ ),
172
+ ];
173
+ }
174
+ const fieldType =
175
+ this.model.fieldType(recordType, name, captured) ??
176
+ (tagged && this.layouts.taggedFieldType(declared, tagged, name));
177
+ if (!fieldType) {
178
+ throw new Error(`${name} has no declared type.`);
179
+ }
180
+ return [
181
+ name,
182
+ field
183
+ ? this.valueText(field, fieldType)
184
+ : `defaultValue<${this.layouts.text(fieldType)}>()`,
185
+ ];
186
+ });
187
+ if (!record) {
188
+ return `{ ${fields.map(([name, value]) => `${name}: ${value}`).join(", ")} }`;
189
+ }
190
+ const literal = this.layouts.literalEnum(record.key);
191
+ const entries = fields.map(([name, value]) => {
192
+ // A string member of a mixed enum has no number; its key stays as written for verification to reject.
193
+ const key = literal
194
+ ? this.layouts.member(literal, name)
195
+ : (this.layouts.enumMember(record.key, Number(name)) ??
196
+ JSON.stringify(name));
197
+ return `[${key}]: ${value}`;
198
+ });
199
+ // An enum that is also optional elsewhere has an Undefined member the table must cover.
200
+ if (literal?.optional) {
201
+ entries.unshift(
202
+ `[${this.layouts.member(literal, undefined)}]: ${this.defaultOf(record.value)}`,
203
+ );
204
+ }
205
+ return `{ ${entries.join(", ")} }`;
206
+ }
207
+
208
+ private literal(value: CapturedValue): string {
209
+ switch (value.kind) {
210
+ case "number":
211
+ case "boolean":
212
+ return String(value.value);
213
+ case "string":
214
+ return JSON.stringify(value.value);
215
+ case "bigint":
216
+ return `${value.value}n`;
217
+ default:
218
+ throw new Error(`A captured ${value.kind} has no literal.`);
219
+ }
220
+ }
221
+
222
+ /** The value a place of `type` starts with, as dialect text; nothing for void. */
223
+ defaultOf(type: ts.Type | undefined): string {
224
+ if (!type || type.flags & ts.TypeFlags.Void) {
225
+ return "";
226
+ }
227
+ if (type.flags & ts.TypeFlags.BooleanLike) {
228
+ return "false";
229
+ }
230
+ if (type.flags & ts.TypeFlags.NumberLike) {
231
+ return "0";
232
+ }
233
+ return `defaultValue<${this.layouts.text(type)}>()`;
234
+ }
235
+ }