@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,4 @@
1
+ /** `retain` as a program running under Bun sees it: retention is the PLC's, so the value passes through. */
2
+ export const retain = <T>(value: T): T => value;
3
+ /** `persist` as a program running under Bun sees it: as `retain`, the value passes through. */
4
+ export const persist = <T>(value: T): T => value;
@@ -0,0 +1,142 @@
1
+ // PLC implementation source: timers and edge detectors for a plain TypeScript program. Each is an
2
+ // ordinary class the compiler specializes like a program's own, and the same classes run under Bun
3
+ // for tests and simulation. Every update takes the scan's `now()`, so one scan reads one time.
4
+
5
+ /**
6
+ * Milliseconds since `last`, the previous update's `now()`. The PLC clock is a DINT that wraps
7
+ * after 24.8 days: on the PLC the subtraction wraps with it, and under Bun the one negative step
8
+ * across the wrap counts as none, so a timer measures scan by scan instead of from a start time.
9
+ */
10
+ const elapsedSince = (last: number | undefined, now: number): number =>
11
+ last === undefined ? 0 : Math.max(now - last, 0);
12
+
13
+ /** Turns its output on once the input has been on for `durationMs`, and off as soon as the input goes off. IEC `TON`. */
14
+ export class OnDelayTimer {
15
+ private heldMs = 0;
16
+ private previous = false;
17
+ private lastNow: number | undefined;
18
+
19
+ constructor(readonly durationMs: number) {}
20
+
21
+ /** How long the input has been on, up to `durationMs`. */
22
+ get elapsedMs(): number {
23
+ return this.heldMs;
24
+ }
25
+
26
+ get output(): boolean {
27
+ return this.previous && this.heldMs >= this.durationMs;
28
+ }
29
+
30
+ /** Takes this scan's input and `now()`, and returns the output. */
31
+ update(now: number, input: boolean): boolean {
32
+ const step = elapsedSince(this.lastNow, now);
33
+ this.lastNow = now;
34
+ this.heldMs =
35
+ input && this.previous
36
+ ? Math.min(this.heldMs + step, this.durationMs)
37
+ : 0;
38
+ this.previous = input;
39
+ return this.output;
40
+ }
41
+ }
42
+
43
+ /** Turns its output on with the input, and off once the input has been off for `durationMs`. IEC `TOF`. */
44
+ export class OffDelayTimer {
45
+ private offMs = 0;
46
+ private previous = false;
47
+ private on = false;
48
+ private lastNow: number | undefined;
49
+
50
+ constructor(readonly durationMs: number) {}
51
+
52
+ /** How long the input has been off since the output last turned on, up to `durationMs`. */
53
+ get elapsedMs(): number {
54
+ return this.offMs;
55
+ }
56
+
57
+ get output(): boolean {
58
+ return this.on;
59
+ }
60
+
61
+ /** Takes this scan's input and `now()`, and returns the output. */
62
+ update(now: number, input: boolean): boolean {
63
+ const step = elapsedSince(this.lastNow, now);
64
+ this.lastNow = now;
65
+ if (input) {
66
+ this.on = true;
67
+ this.offMs = 0;
68
+ } else if (this.on) {
69
+ this.offMs = this.previous
70
+ ? 0
71
+ : Math.min(this.offMs + step, this.durationMs);
72
+ this.on = this.offMs < this.durationMs;
73
+ }
74
+ this.previous = input;
75
+ return this.on;
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Turns its output on for `durationMs` when the input rises, whatever the input does meanwhile. A
81
+ * new pulse needs the input to rise again after the last one ends. IEC `TP`.
82
+ */
83
+ export class PulseTimer {
84
+ private onMs = 0;
85
+ private previous = false;
86
+ private on = false;
87
+ private lastNow: number | undefined;
88
+
89
+ constructor(readonly durationMs: number) {}
90
+
91
+ /** How long the current pulse has lasted, up to `durationMs`. */
92
+ get elapsedMs(): number {
93
+ return this.onMs;
94
+ }
95
+
96
+ get output(): boolean {
97
+ return this.on;
98
+ }
99
+
100
+ /** Takes this scan's input and `now()`, and returns the output. */
101
+ update(now: number, input: boolean): boolean {
102
+ const step = elapsedSince(this.lastNow, now);
103
+ this.lastNow = now;
104
+ if (this.on) {
105
+ this.onMs = Math.min(this.onMs + step, this.durationMs);
106
+ this.on = this.onMs < this.durationMs;
107
+ } else if (input && !this.previous) {
108
+ this.on = true;
109
+ this.onMs = 0;
110
+ }
111
+ this.previous = input;
112
+ return this.on;
113
+ }
114
+ }
115
+
116
+ /** True for the one scan in which its input goes from off to on, including an input already on at the first scan. IEC `R_TRIG`. */
117
+ export class RisingEdge {
118
+ private previous = false;
119
+ /** Whether the last update saw the input rise. */
120
+ rose = false;
121
+
122
+ /** Takes this scan's input, and returns whether it rose. */
123
+ update(input: boolean): boolean {
124
+ this.rose = input && !this.previous;
125
+ this.previous = input;
126
+ return this.rose;
127
+ }
128
+ }
129
+
130
+ /** True for the one scan in which its input goes from on to off. An input off from the first scan never falls. */
131
+ export class FallingEdge {
132
+ private previous = false;
133
+ /** Whether the last update saw the input fall. */
134
+ fell = false;
135
+
136
+ /** Takes this scan's input, and returns whether it fell. */
137
+ update(input: boolean): boolean {
138
+ this.fell = this.previous && !input;
139
+ this.previous = input;
140
+ return this.fell;
141
+ }
142
+ }
@@ -0,0 +1,89 @@
1
+ import { relative } from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+ import { failureCode, failureName } from "../src/ir/job-errors";
4
+ import { plcTime } from "./plc-time";
5
+
6
+ type Reported = Readonly<Record<string, unknown>>;
7
+ class Text {
8
+ constructor(readonly value: string) {}
9
+ }
10
+ type Value = boolean | number | bigint | string | Text;
11
+ type Properties = Readonly<Record<string, Value>>;
12
+
13
+ const module = fileURLToPath(import.meta.url);
14
+ let collector: ((record: Reported) => void) | undefined;
15
+
16
+ /** The program's call: the first stack frame outside this module, relative to the working directory. */
17
+ const source = () => {
18
+ for (const line of new Error().stack?.split("\n") ?? []) {
19
+ const [, file, lineNumber] =
20
+ /\(?((?:\/|[A-Za-z]:\\)[^():]+):(\d+):\d+\)?$/.exec(line) ?? [];
21
+ if (file && file !== module) {
22
+ return `${relative(process.cwd(), file).replaceAll("\\", "/")}:${lineNumber}`;
23
+ }
24
+ }
25
+ return "";
26
+ };
27
+
28
+ const reportedValue = (value: Value) => {
29
+ if (value instanceof Text) {
30
+ return value.value;
31
+ }
32
+ if (typeof value === "bigint") {
33
+ return Number(value);
34
+ }
35
+ return value;
36
+ };
37
+
38
+ const report = (record: Reported, properties: Properties = {}) =>
39
+ collector?.({
40
+ ...record,
41
+ properties: Object.fromEntries(
42
+ Object.entries(properties).map(([name, value]) => [
43
+ name,
44
+ reportedValue(value),
45
+ ]),
46
+ ),
47
+ source: source(),
48
+ scan: 0,
49
+ time: plcTime(),
50
+ });
51
+
52
+ /** What a PLC program's `telemetry` calls do under Bun; see `types/telemetry.d.ts`. */
53
+ export const telemetry = {
54
+ capture: (event: string, properties?: Properties) =>
55
+ report({ kind: "event", event }, properties),
56
+ info: (message: string, properties?: Properties) =>
57
+ report({ kind: "log", level: "info", message }, properties),
58
+ warn: (message: string, properties?: Properties) =>
59
+ report({ kind: "log", level: "warn", message }, properties),
60
+ error: (message: string, properties?: Properties) =>
61
+ report({ kind: "log", level: "error", message }, properties),
62
+ // Under Bun a failure can be a thrown Error rather than a code; it is sent as code 0 with its message.
63
+ captureException: (error: unknown, properties?: Properties) => {
64
+ const code = failureCode(error);
65
+ const name = failureName(error);
66
+ report(
67
+ { kind: "exception", code, ...(name && { name }), handled: true },
68
+ properties,
69
+ );
70
+ },
71
+ text: (bytes: readonly number[], length: number) => {
72
+ const sent = bytes.slice(0, Math.max(0, Math.min(length, bytes.length)));
73
+ const end = sent.indexOf(0);
74
+ return new Text(
75
+ new TextDecoder().decode(
76
+ Uint8Array.from(end < 0 ? sent : sent.slice(0, end)),
77
+ ),
78
+ );
79
+ },
80
+ };
81
+
82
+ export const onTelemetry = (next: (record: Reported) => void) => {
83
+ collector = next;
84
+ return () => {
85
+ if (collector === next) {
86
+ collector = undefined;
87
+ }
88
+ };
89
+ };
@@ -0,0 +1,87 @@
1
+ import type { BOOL, TIME, UDINT } from "./index.d.ts";
2
+
3
+ export interface BackgroundSnapshot<T> {
4
+ valid: BOOL;
5
+ busy: BOOL;
6
+ done: BOOL;
7
+ cancelRequested: BOOL;
8
+ error: UDINT;
9
+ result: T;
10
+ }
11
+ export interface BackgroundTask<A extends unknown[], R> {
12
+ (...arguments_: A): Promise<R>;
13
+ start(...arguments_: A): BOOL;
14
+ snapshot(): BackgroundSnapshot<R>;
15
+ cancel(): BOOL;
16
+ reset(): BOOL;
17
+ }
18
+ export declare const backgroundTasks: {
19
+ define<A extends unknown[], R>(
20
+ work: (...arguments_: A) => Promise<R>,
21
+ options: { interval: TIME; priority: number },
22
+ ): BackgroundTask<A, R>;
23
+ };
24
+ export type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
25
+ export interface FetchOptions<Request> {
26
+ method?: HttpMethod;
27
+ json?: Request;
28
+ }
29
+ /** Fetch a bounded JSON response. URL and method must be compile-time values. */
30
+ export declare function fetch<Response, Request = unknown>(
31
+ url: string,
32
+ options?: FetchOptions<Request>,
33
+ ): Promise<Response>;
34
+ export declare function sleep(duration: TIME): Promise<void>;
35
+
36
+ /** The PLC's view of a tRPC router: each procedure is an async call. */
37
+ export type ServiceClient<Procedures> = {
38
+ [Key in keyof Procedures]: Procedures[Key] extends {
39
+ _def: { $types: { input: infer Input; output: infer Output } };
40
+ }
41
+ ? (input: Input) => Promise<Output>
42
+ : ServiceClient<Procedures[Key]>;
43
+ };
44
+ /**
45
+ * Calls the server router's `meta({ plc: true })` procedures, like a tRPC client. Import the
46
+ * router's type with `import type`; tsPLC reads each call's wire layout from it.
47
+ */
48
+ export declare function createServiceClient<
49
+ Router extends { _def: { record: object } },
50
+ >(): ServiceClient<Router["_def"]["record"]>;
51
+
52
+ /** Cancel pending continuations in a controller or the current module. */
53
+ export declare function cancelAsync(owner?: object): void;
54
+ export declare function asyncDiagnostics(): {
55
+ valid: BOOL;
56
+ count: UDINT;
57
+ lastError: UDINT;
58
+ };
59
+ export declare function errorCode(reason: unknown): UDINT;
60
+ export interface Task<A extends unknown[], R> {
61
+ /** Starts the work. False while the task is busy or holds an unread result. */
62
+ start(...arguments_: A): BOOL;
63
+ /** The current state. After `cancel` it completes with error 1 and no result. */
64
+ snapshot(): BackgroundSnapshot<R>;
65
+ /** Discards the work or its unread result. False when the task is idle. */
66
+ cancel(): BOOL;
67
+ /** Clears a finished task so it can start again. False while it is busy. */
68
+ reset(): BOOL;
69
+ }
70
+ /** One named, cancellable invocation of async work, advanced in the owner's scan task. */
71
+ export declare const tasks: {
72
+ define<A extends unknown[], R>(
73
+ work: (...arguments_: A) => Promise<R>,
74
+ ): Task<A, R>;
75
+ };
76
+ /** @internal Compiler-owned, bounded continuation slots. */
77
+ export declare const cooperativeTasks: {
78
+ define<A extends unknown[], R>(
79
+ work: (...arguments_: A) => Promise<R>,
80
+ ): { start(...arguments_: A): void; cancel(): void };
81
+ };
82
+
83
+ /** @internal Generated OPC UA service calls use the same async continuation model. */
84
+ export declare function opcuaCall<Response, Request>(
85
+ path: string,
86
+ request: Request,
87
+ ): Promise<Response>;
@@ -0,0 +1,8 @@
1
+ import type { DINT } from "./index.d.ts";
2
+
3
+ /**
4
+ * The scan's clock: milliseconds since the PLC runtime started, read once when the scan began, so
5
+ * every call during one scan returns the same value. It wraps after 24.8 days, so compare times by
6
+ * subtracting: `now() - since >= 500`.
7
+ */
8
+ export declare function now(): DINT;
@@ -0,0 +1,2 @@
1
+ /** Resolve a required string environment value when compiling module configuration. */
2
+ export declare const env: <T extends string>(name: string) => T;
@@ -0,0 +1,31 @@
1
+ export {};
2
+
3
+ // The IEC data types alone, for host code that reads PLC declarations but must not see names that exist only in PLC programs.
4
+ declare global {
5
+ type BOOL = import("./index").BOOL;
6
+ type SINT = import("./index").SINT;
7
+ type USINT = import("./index").USINT;
8
+ type INT = import("./index").INT;
9
+ type UINT = import("./index").UINT;
10
+ type DINT = import("./index").DINT;
11
+ type UDINT = import("./index").UDINT;
12
+ type LINT = import("./index").LINT;
13
+ type ULINT = import("./index").ULINT;
14
+ type BYTE = import("./index").BYTE;
15
+ type WORD = import("./index").WORD;
16
+ type DWORD = import("./index").DWORD;
17
+ type LWORD = import("./index").LWORD;
18
+ type REAL = import("./index").REAL;
19
+ type LREAL = import("./index").LREAL;
20
+ type TIME = import("./index").TIME;
21
+ type DATE = import("./index").DATE;
22
+ type TIME_OF_DAY = import("./index").TIME_OF_DAY;
23
+ type DATE_AND_TIME = import("./index").DATE_AND_TIME;
24
+ type FixedArray<
25
+ T,
26
+ N extends number,
27
+ Lower extends number = 0,
28
+ > = import("./index").FixedArray<T, N, Lower>;
29
+ type STRING<N extends number> = import("./index").STRING<N>;
30
+ type WSTRING<N extends number> = import("./index").WSTRING<N>;
31
+ }
@@ -0,0 +1,49 @@
1
+ import "./data-types";
2
+
3
+ // The tsPLC dialect's globals: the language the compiler lowers a program into, whose generated
4
+ // code calls these ambiently. A program's own source can't use them (the frontend rejects them),
5
+ // and no package entry exports this file, so editors don't offer them. Ambient spellings point to
6
+ // the canonical SDK types; the frontend checks this declaration identity rather than trusting a
7
+ // same-named user alias.
8
+ declare global {
9
+ type Input<T> = import("./index").Input<T>;
10
+ type Output<T> = import("./index").Output<T>;
11
+
12
+ type InOut<T> = import("./index").InOut<T>;
13
+ type FunctionBlock<
14
+ I,
15
+ O = Record<never, never>,
16
+ > = import("./index").FunctionBlock<I, O>;
17
+ const FunctionBlock: typeof import("./index").FunctionBlock;
18
+ type Blinker = import("./index").Blinker;
19
+ const Blinker: typeof import("./index").Blinker;
20
+ type OnDelayTimer = import("./index").OnDelayTimer;
21
+ const OnDelayTimer: typeof import("./index").OnDelayTimer;
22
+ type OffDelayTimer = import("./index").OffDelayTimer;
23
+ const OffDelayTimer: typeof import("./index").OffDelayTimer;
24
+ type PulseTimer = import("./index").PulseTimer;
25
+ const PulseTimer: typeof import("./index").PulseTimer;
26
+ type RisingEdge = import("./index").RisingEdge;
27
+ const RisingEdge: typeof import("./index").RisingEdge;
28
+ type FallingEdge = import("./index").FallingEdge;
29
+ const FallingEdge: typeof import("./index").FallingEdge;
30
+ type UpCounter = import("./index").UpCounter;
31
+ const UpCounter: typeof import("./index").UpCounter;
32
+ type DownCounter = import("./index").DownCounter;
33
+ const DownCounter: typeof import("./index").DownCounter;
34
+ type UpDownCounter = import("./index").UpDownCounter;
35
+ const UpDownCounter: typeof import("./index").UpDownCounter;
36
+ const ref: typeof import("./index").ref;
37
+ const retain: typeof import("./index").retain;
38
+ const ms: typeof import("./index").ms;
39
+ const seconds: typeof import("./index").seconds;
40
+ const minutes: typeof import("./index").minutes;
41
+ const timeToMilliseconds: typeof import("./index").timeToMilliseconds;
42
+ const date: typeof import("./index").date;
43
+ const timeOfDay: typeof import("./index").timeOfDay;
44
+ const dateAndTime: typeof import("./index").dateAndTime;
45
+
46
+ const defaultValue: typeof import("./index").defaultValue;
47
+ const copy: typeof import("./index").copy;
48
+ const entries: typeof import("./index").entries;
49
+ }
@@ -0,0 +1,10 @@
1
+ import "./data-types";
2
+
3
+ // What every PLC source sees without an import: the IEC type names (from data-types) and
4
+ // VoidFunction. The SDK's functions and classes are imported from their modules.
5
+ declare global {
6
+ interface VoidFunction {
7
+ // biome-ignore lint/style/useShorthandFunctionType: This declaration must merge with lib.dom's interface.
8
+ (): void;
9
+ }
10
+ }
@@ -0,0 +1,212 @@
1
+ // Canonical PLC declarations: TypeScript supplies editor/checker compatibility;
2
+ // the compiler preserves scalar domains and lowers intrinsics without JS execution.
3
+ export type BOOL = boolean;
4
+ export type SINT = number;
5
+ export type USINT = number;
6
+ export type INT = number;
7
+ export type UINT = number;
8
+ export type DINT = number;
9
+ export type UDINT = number;
10
+ export type LINT = bigint;
11
+ export type ULINT = bigint;
12
+ export type BYTE = number;
13
+ export type WORD = number;
14
+ export type DWORD = number;
15
+ export type LWORD = bigint;
16
+ export type REAL = number;
17
+ export type LREAL = number;
18
+ export type Input<T> = T;
19
+ export type Output<T> = T;
20
+
21
+ // Conversions and arithmetic state their overflow policy explicitly.
22
+ export declare function exact<T extends number | bigint>(
23
+ value: number | bigint,
24
+ ): T;
25
+ export declare function wrap<T extends number | bigint>(
26
+ value: number | bigint,
27
+ ): T;
28
+ // Float-to-float saturation clips to finite target endpoints, then rounds to its width. A float
29
+ // saturated to an integer type drops its fraction toward zero, then clips; NaN becomes 0.
30
+ export declare function saturate<T extends number | bigint>(
31
+ value: number | bigint,
32
+ ): T;
33
+ export declare function wrapAdd<T extends number | bigint>(
34
+ left: T,
35
+ right: T,
36
+ ): T;
37
+ export declare function wrapSubtract<T extends number | bigint>(
38
+ left: T,
39
+ right: T,
40
+ ): T;
41
+ export declare function wrapMultiply<T extends number | bigint>(
42
+ left: T,
43
+ right: T,
44
+ ): T;
45
+ export declare function saturatingAdd<T extends number | bigint>(
46
+ left: T,
47
+ right: T,
48
+ ): T;
49
+ export declare function saturatingSubtract<T extends number | bigint>(
50
+ left: T,
51
+ right: T,
52
+ ): T;
53
+ export declare function saturatingMultiply<T extends number | bigint>(
54
+ left: T,
55
+ right: T,
56
+ ): T;
57
+ export declare function div<T extends number | bigint>(left: T, right: T): T;
58
+
59
+ // Math on REAL or LREAL, named by the type argument. A result must be proved finite: sin, cos, tan
60
+ // and atan always are; the others need their operand's domain proved, as a constant or an integer
61
+ // converted exactly is.
62
+ export declare function sqrt<T extends number>(value: T): T;
63
+ export declare function ln<T extends number>(value: T): T;
64
+ export declare function log10<T extends number>(value: T): T;
65
+ export declare function exp<T extends number>(value: T): T;
66
+ export declare function sin<T extends number>(value: T): T;
67
+ export declare function cos<T extends number>(value: T): T;
68
+ export declare function tan<T extends number>(value: T): T;
69
+ export declare function asin<T extends number>(value: T): T;
70
+ export declare function acos<T extends number>(value: T): T;
71
+ export declare function atan<T extends number>(value: T): T;
72
+ /** Toward zero, keeping the floating type. */
73
+ export declare function trunc<T extends number>(value: T): T;
74
+ /** `base` to the power `exponent`, which must be a constant. */
75
+ export declare function expt<T extends number>(base: T, exponent: T): T;
76
+
77
+ // Capacity and bounds are compiler metadata, erased by the TypeScript checker.
78
+ export type FixedArray<T, _N extends number, _Lower extends number = 0> = T[];
79
+ export type STRING<_N extends number> = string;
80
+ export type WSTRING<_N extends number> = string;
81
+ export declare function defaultValue<T>(): T;
82
+ export declare function copy<T>(value: T): T;
83
+ // `for (const [key, element] of entries(record))` iterates an enum-keyed Record by key.
84
+ export declare function entries<K extends number, T>(
85
+ record: Record<K, T>,
86
+ ): [K, T][];
87
+
88
+ declare const timeBrand: unique symbol;
89
+ declare const calendarBrand: unique symbol;
90
+ export type TIME = number & { readonly [timeBrand]: "TIME" };
91
+ export type DATE = string & { readonly [calendarBrand]: "DATE" };
92
+ export type TIME_OF_DAY = string & { readonly [calendarBrand]: "TIME_OF_DAY" };
93
+ export type DATE_AND_TIME = string & {
94
+ readonly [calendarBrand]: "DATE_AND_TIME";
95
+ };
96
+
97
+ // Borrowing and retained storage are checked by the compiler, not JS wrappers.
98
+ export type InOut<T> = T;
99
+ export declare function ref<T>(value: T): InOut<T>;
100
+ export declare function retain<T>(value: T): T;
101
+ export declare function persist<T>(value: T): T;
102
+
103
+ export declare function ms(value: number): TIME;
104
+ export declare function seconds(value: number): TIME;
105
+ export declare function minutes(value: number): TIME;
106
+ // TIME stores unsigned 32-bit milliseconds; this preserves that complete range.
107
+ export declare function timeToMilliseconds(value: TIME): UDINT;
108
+ export declare function date(value: string): DATE;
109
+ export declare function timeOfDay(value: string): TIME_OF_DAY;
110
+ export declare function dateAndTime(value: string): DATE_AND_TIME;
111
+
112
+ // The empty output schema makes execute return void for outputless blocks.
113
+ export declare abstract class FunctionBlock<I, O = Record<never, never>> {
114
+ readonly out: Readonly<O>;
115
+ update(input: I): void;
116
+ protected abstract execute(input: I): keyof O extends never ? void : O;
117
+ }
118
+
119
+ /** Starts on, then alternates `onTime` and `offTime`. Keeps its last `on` when disabled. CODESYS Util.BLINK. */
120
+ export declare class Blinker extends FunctionBlock<
121
+ { enabled: BOOL; offTime: TIME; onTime: TIME },
122
+ { on: BOOL }
123
+ > {
124
+ protected execute(input: { enabled: BOOL; offTime: TIME; onTime: TIME }): {
125
+ on: BOOL;
126
+ };
127
+ }
128
+
129
+ /** `done` once `enabled` has held for `duration`; resets when `enabled` drops. `elapsed` stops at `duration`. IEC TON. */
130
+ export declare class OnDelayTimer extends FunctionBlock<
131
+ { enabled: BOOL; duration: TIME },
132
+ { done: BOOL; elapsed: TIME }
133
+ > {
134
+ protected execute(input: { enabled: BOOL; duration: TIME }): {
135
+ done: BOOL;
136
+ elapsed: TIME;
137
+ };
138
+ }
139
+
140
+ /** `active` while `enabled`, and for `duration` after it drops. IEC TOF. */
141
+ export declare class OffDelayTimer extends FunctionBlock<
142
+ { enabled: BOOL; duration: TIME },
143
+ { active: BOOL; elapsed: TIME }
144
+ > {
145
+ protected execute(input: { enabled: BOOL; duration: TIME }): {
146
+ active: BOOL;
147
+ elapsed: TIME;
148
+ };
149
+ }
150
+
151
+ /** `active` for `duration` from a rising `start`; `start` is ignored while active. IEC TP. */
152
+ export declare class PulseTimer extends FunctionBlock<
153
+ { start: BOOL; duration: TIME },
154
+ { active: BOOL; elapsed: TIME }
155
+ > {
156
+ protected execute(input: { start: BOOL; duration: TIME }): {
157
+ active: BOOL;
158
+ elapsed: TIME;
159
+ };
160
+ }
161
+
162
+ /** `rose` for the one update where `signal` goes from false to true. IEC R_TRIG. */
163
+ export declare class RisingEdge extends FunctionBlock<
164
+ { signal: BOOL },
165
+ { rose: BOOL }
166
+ > {
167
+ protected execute(input: { signal: BOOL }): { rose: BOOL };
168
+ }
169
+
170
+ /** `fell` for the one update where `signal` goes from true to false. IEC F_TRIG. */
171
+ export declare class FallingEdge extends FunctionBlock<
172
+ { signal: BOOL },
173
+ { fell: BOOL }
174
+ > {
175
+ protected execute(input: { signal: BOOL }): { fell: BOOL };
176
+ }
177
+
178
+ /** Counts rising `countUp` edges. `reset` clears `value`. `reachedPreset` indicates `value >= preset`. CODESYS CTU. */
179
+ export declare class UpCounter extends FunctionBlock<
180
+ { countUp: BOOL; reset: BOOL; preset: WORD },
181
+ { reachedPreset: BOOL; value: WORD }
182
+ > {
183
+ protected execute(input: { countUp: BOOL; reset: BOOL; preset: WORD }): {
184
+ reachedPreset: BOOL;
185
+ value: WORD;
186
+ };
187
+ }
188
+
189
+ /** Counts down on rising `countDown` edges. `load` sets `value` to `preset`. `atZero` indicates `value = 0`. CODESYS CTD. */
190
+ export declare class DownCounter extends FunctionBlock<
191
+ { countDown: BOOL; load: BOOL; preset: WORD },
192
+ { atZero: BOOL; value: WORD }
193
+ > {
194
+ protected execute(input: { countDown: BOOL; load: BOOL; preset: WORD }): {
195
+ atZero: BOOL;
196
+ value: WORD;
197
+ };
198
+ }
199
+
200
+ /** Counts rising edges in either direction. `reset` clears `value`, and `load` sets `value` to `preset`. CODESYS CTUD. */
201
+ export declare class UpDownCounter extends FunctionBlock<
202
+ { countUp: BOOL; countDown: BOOL; reset: BOOL; load: BOOL; preset: WORD },
203
+ { reachedPreset: BOOL; atZero: BOOL; value: WORD }
204
+ > {
205
+ protected execute(input: {
206
+ countUp: BOOL;
207
+ countDown: BOOL;
208
+ reset: BOOL;
209
+ load: BOOL;
210
+ preset: WORD;
211
+ }): { reachedPreset: BOOL; atZero: BOOL; value: WORD };
212
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Keeps state through a warm restart: `retain(initializer)` returns its argument, and the PLC
3
+ * declares the storage that holds it `RETAIN`. A program imports it from here, because its setup
4
+ * and unit tests run under Bun (docs/02-language/03-state-and-io.md).
5
+ */
6
+ export { retain } from "./index";
7
+ /**
8
+ * Keeps a module binding's state through a download and a cold reset too: `persist(initializer)`
9
+ * returns its argument, and the PLC declares the storage that holds it `PERSISTENT RETAIN`, in the
10
+ * application's one persistent list (docs/02-language/03-state-and-io.md).
11
+ */
12
+ export { persist } from "./index";