@tsplc/compiler 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +16 -2
- package/backends/codesys/config.ts +54 -0
- package/backends/codesys/emit.ts +1063 -0
- package/backends/codesys/engineering.ts +88 -0
- package/backends/codesys/http.ts +598 -0
- package/backends/codesys/index.ts +12 -0
- package/backends/codesys/io-paths.ts +103 -0
- package/backends/codesys/names.ts +76 -0
- package/backends/codesys/numeric.ts +356 -0
- package/backends/codesys/plan.ts +512 -0
- package/backends/codesys/publication.ts +59 -0
- package/backends/codesys/storage.ts +63 -0
- package/backends/codesys/task-access.ts +118 -0
- package/backends/codesys/types.ts +178 -0
- package/backends/codesys/vendor.ts +108 -0
- package/dist/cli.js +740 -0
- package/dist/cli.js.map +13 -0
- package/dist/clock.js +8 -0
- package/dist/clock.js.map +10 -0
- package/dist/codesys.js +2587 -0
- package/dist/codesys.js.map +22 -0
- package/dist/elaborate-capture.js +118 -0
- package/dist/elaborate-capture.js.map +10 -0
- package/dist/index.js +6859 -0
- package/dist/index.js.map +25 -0
- package/dist/ir.js +1888 -0
- package/dist/ir.js.map +22 -0
- package/dist/math.js +24 -0
- package/dist/math.js.map +10 -0
- package/dist/project.js +423 -0
- package/dist/project.js.map +11 -0
- package/ir/aggregates.ts +333 -0
- package/ir/allocation.ts +74 -0
- package/ir/core-model.ts +116 -0
- package/ir/core.ts +63 -0
- package/ir/declarations.ts +201 -0
- package/ir/diagnostics.ts +43 -0
- package/ir/domains.ts +96 -0
- package/ir/index.ts +7 -0
- package/ir/link.ts +176 -0
- package/ir/model.ts +268 -0
- package/ir/normalization-error.ts +10 -0
- package/ir/routine.ts +826 -0
- package/ir/scalars.ts +675 -0
- package/ir/types.ts +70 -0
- package/package.json +105 -4
- package/project/artifacts.ts +277 -0
- package/project/cli.ts +282 -0
- package/project/index.ts +1 -0
- package/project/project.ts +298 -0
- package/project/server.ts +83 -0
- package/src/frontend/async.ts +1060 -0
- package/src/frontend/blocks.ts +419 -0
- package/src/frontend/configuration.ts +167 -0
- package/src/frontend/const-functions.ts +153 -0
- package/src/frontend/cooperative.ts +75 -0
- package/src/frontend/elaborate/capture.ts +138 -0
- package/src/frontend/elaborate/graph.ts +94 -0
- package/src/frontend/elaborate/index.ts +302 -0
- package/src/frontend/elaborate/layouts.ts +390 -0
- package/src/frontend/elaborate/model.ts +177 -0
- package/src/frontend/elaborate/specialize.ts +2280 -0
- package/src/frontend/exposure.ts +147 -0
- package/src/frontend/module-graph.ts +63 -0
- package/src/frontend/promises.ts +751 -0
- package/src/frontend/services.ts +435 -0
- package/src/frontend/types.ts +578 -0
- package/src/frontend/typescript.ts +2442 -0
- package/src/index.ts +7 -0
- package/stdlib/clock.ts +2 -0
- package/stdlib/math.ts +19 -0
- package/types/async.d.ts +81 -0
- package/types/clock.d.ts +4 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/globals.d.ts +40 -0
- package/types/index.d.ts +111 -0
- package/types/plc.d.ts +5 -0
package/src/index.ts
ADDED
package/stdlib/clock.ts
ADDED
package/stdlib/math.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// PLC implementation source: the frontend specializes these helpers for each
|
|
2
|
+
// concrete scalar domain. The release includes this source for that analysis.
|
|
3
|
+
export function min<T extends number | bigint>(left: T, right: T): T {
|
|
4
|
+
return left < right ? left : right;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function max<T extends number | bigint>(left: T, right: T): T {
|
|
8
|
+
return left > right ? left : right;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function clamp<T extends number | bigint>(value: T, lower: T, upper: T): T {
|
|
12
|
+
if (value < lower) {
|
|
13
|
+
return lower;
|
|
14
|
+
}
|
|
15
|
+
if (value > upper) {
|
|
16
|
+
return upper;
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
}
|
package/types/async.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
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(): { valid: BOOL; count: UDINT; lastError: UDINT };
|
|
55
|
+
export declare function errorCode(reason: unknown): UDINT;
|
|
56
|
+
export interface Task<A extends unknown[], R> {
|
|
57
|
+
/** Starts the work. False while the task is busy or holds an unread result. */
|
|
58
|
+
start(...arguments_: A): BOOL;
|
|
59
|
+
/** The current state. After `cancel` it completes with error 1 and no result. */
|
|
60
|
+
snapshot(): BackgroundSnapshot<R>;
|
|
61
|
+
/** Discards the work or its unread result. False when the task is idle. */
|
|
62
|
+
cancel(): BOOL;
|
|
63
|
+
/** Clears a finished task so it can start again. False while it is busy. */
|
|
64
|
+
reset(): BOOL;
|
|
65
|
+
}
|
|
66
|
+
/** One named, cancellable invocation of async work, advanced in the owner's scan task. */
|
|
67
|
+
export declare const tasks: {
|
|
68
|
+
define<A extends unknown[], R>(work: (...arguments_: A) => Promise<R>): Task<A, R>;
|
|
69
|
+
};
|
|
70
|
+
/** @internal Compiler-owned, bounded continuation slots. */
|
|
71
|
+
export declare const cooperativeTasks: {
|
|
72
|
+
define<A extends unknown[], R>(
|
|
73
|
+
work: (...arguments_: A) => Promise<R>,
|
|
74
|
+
): { start(...arguments_: A): void; cancel(): void };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** @internal Generated OPC UA service calls use the same async continuation model. */
|
|
78
|
+
export declare function opcuaCall<Response, Request>(
|
|
79
|
+
path: string,
|
|
80
|
+
request: Request,
|
|
81
|
+
): Promise<Response>;
|
package/types/clock.d.ts
ADDED
|
@@ -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<T, N extends number, Lower extends number = 0> = import("./index").FixedArray<
|
|
25
|
+
T,
|
|
26
|
+
N,
|
|
27
|
+
Lower
|
|
28
|
+
>;
|
|
29
|
+
type STRING<N extends number> = import("./index").STRING<N>;
|
|
30
|
+
type WSTRING<N extends number> = import("./index").WSTRING<N>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import "./data-types";
|
|
2
|
+
|
|
3
|
+
// Ambient spellings point to the canonical SDK types; the frontend checks this
|
|
4
|
+
// declaration identity rather than trusting a same-named user alias.
|
|
5
|
+
declare global {
|
|
6
|
+
interface VoidFunction {
|
|
7
|
+
(): void;
|
|
8
|
+
}
|
|
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<I, O = Record<never, never>> = import("./index").FunctionBlock<I, O>;
|
|
14
|
+
const FunctionBlock: typeof import("./index").FunctionBlock;
|
|
15
|
+
type Blinker = import("./index").Blinker;
|
|
16
|
+
const Blinker: typeof import("./index").Blinker;
|
|
17
|
+
type OnDelayTimer = import("./index").OnDelayTimer;
|
|
18
|
+
const OnDelayTimer: typeof import("./index").OnDelayTimer;
|
|
19
|
+
type OffDelayTimer = import("./index").OffDelayTimer;
|
|
20
|
+
const OffDelayTimer: typeof import("./index").OffDelayTimer;
|
|
21
|
+
type PulseTimer = import("./index").PulseTimer;
|
|
22
|
+
const PulseTimer: typeof import("./index").PulseTimer;
|
|
23
|
+
type RisingEdge = import("./index").RisingEdge;
|
|
24
|
+
const RisingEdge: typeof import("./index").RisingEdge;
|
|
25
|
+
type FallingEdge = import("./index").FallingEdge;
|
|
26
|
+
const FallingEdge: typeof import("./index").FallingEdge;
|
|
27
|
+
const ref: typeof import("./index").ref;
|
|
28
|
+
const retain: typeof import("./index").retain;
|
|
29
|
+
const ms: typeof import("./index").ms;
|
|
30
|
+
const seconds: typeof import("./index").seconds;
|
|
31
|
+
const minutes: typeof import("./index").minutes;
|
|
32
|
+
const timeToMilliseconds: typeof import("./index").timeToMilliseconds;
|
|
33
|
+
const date: typeof import("./index").date;
|
|
34
|
+
const timeOfDay: typeof import("./index").timeOfDay;
|
|
35
|
+
const dateAndTime: typeof import("./index").dateAndTime;
|
|
36
|
+
|
|
37
|
+
const defaultValue: typeof import("./index").defaultValue;
|
|
38
|
+
const copy: typeof import("./index").copy;
|
|
39
|
+
const entries: typeof import("./index").entries;
|
|
40
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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>(value: number | bigint): T;
|
|
23
|
+
export declare function wrap<T extends number | bigint>(value: number | bigint): T;
|
|
24
|
+
// Float-to-float saturation clips to finite target endpoints, then rounds to its width.
|
|
25
|
+
export declare function saturate<T extends number | bigint>(value: number | bigint): T;
|
|
26
|
+
export declare function wrapAdd<T extends number | bigint>(left: T, right: T): T;
|
|
27
|
+
export declare function wrapSubtract<T extends number | bigint>(left: T, right: T): T;
|
|
28
|
+
export declare function wrapMultiply<T extends number | bigint>(left: T, right: T): T;
|
|
29
|
+
export declare function saturatingAdd<T extends number | bigint>(left: T, right: T): T;
|
|
30
|
+
export declare function saturatingSubtract<T extends number | bigint>(left: T, right: T): T;
|
|
31
|
+
export declare function saturatingMultiply<T extends number | bigint>(left: T, right: T): T;
|
|
32
|
+
export declare function div<T extends number | bigint>(left: T, right: T): T;
|
|
33
|
+
|
|
34
|
+
// Capacity and bounds are compiler metadata, erased by the TypeScript checker.
|
|
35
|
+
export type FixedArray<T, _N extends number, _Lower extends number = 0> = T[];
|
|
36
|
+
export type STRING<_N extends number> = string;
|
|
37
|
+
export type WSTRING<_N extends number> = string;
|
|
38
|
+
export declare function defaultValue<T>(): T;
|
|
39
|
+
export declare function copy<T>(value: T): T;
|
|
40
|
+
// `for (const [key, element] of entries(record))` iterates an enum-keyed Record by key.
|
|
41
|
+
export declare function entries<K extends number, T>(record: Record<K, T>): [K, T][];
|
|
42
|
+
|
|
43
|
+
declare const timeBrand: unique symbol;
|
|
44
|
+
declare const calendarBrand: unique symbol;
|
|
45
|
+
export type TIME = number & { readonly [timeBrand]: "TIME" };
|
|
46
|
+
export type DATE = string & { readonly [calendarBrand]: "DATE" };
|
|
47
|
+
export type TIME_OF_DAY = string & { readonly [calendarBrand]: "TIME_OF_DAY" };
|
|
48
|
+
export type DATE_AND_TIME = string & { readonly [calendarBrand]: "DATE_AND_TIME" };
|
|
49
|
+
|
|
50
|
+
// Borrowing and retained storage are checked by the compiler, not JS wrappers.
|
|
51
|
+
export type InOut<T> = T;
|
|
52
|
+
export declare function ref<T>(value: T): InOut<T>;
|
|
53
|
+
export declare function retain<T>(value: T): T;
|
|
54
|
+
|
|
55
|
+
export declare function ms(value: number): TIME;
|
|
56
|
+
export declare function seconds(value: number): TIME;
|
|
57
|
+
export declare function minutes(value: number): TIME;
|
|
58
|
+
// TIME stores unsigned 32-bit milliseconds; this preserves that complete range.
|
|
59
|
+
export declare function timeToMilliseconds(value: TIME): UDINT;
|
|
60
|
+
export declare function date(value: string): DATE;
|
|
61
|
+
export declare function timeOfDay(value: string): TIME_OF_DAY;
|
|
62
|
+
export declare function dateAndTime(value: string): DATE_AND_TIME;
|
|
63
|
+
|
|
64
|
+
// The empty output schema makes execute return void for outputless blocks.
|
|
65
|
+
export declare abstract class FunctionBlock<I, O = Record<never, never>> {
|
|
66
|
+
readonly out: Readonly<O>;
|
|
67
|
+
update(input: I): void;
|
|
68
|
+
protected abstract execute(input: I): keyof O extends never ? void : O;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Alternates `on` while enabled: off for `offTime`, then on for `onTime`. Keeps its last `on` when disabled. CODESYS Util.BLINK. */
|
|
72
|
+
export declare class Blinker extends FunctionBlock<
|
|
73
|
+
{ enabled: BOOL; offTime: TIME; onTime: TIME },
|
|
74
|
+
{ on: BOOL }
|
|
75
|
+
> {
|
|
76
|
+
protected execute(input: { enabled: BOOL; offTime: TIME; onTime: TIME }): { on: BOOL };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** `done` once `enabled` has held for `duration`; resets when `enabled` drops. `elapsed` stops at `duration`. IEC TON. */
|
|
80
|
+
export declare class OnDelayTimer extends FunctionBlock<
|
|
81
|
+
{ enabled: BOOL; duration: TIME },
|
|
82
|
+
{ done: BOOL; elapsed: TIME }
|
|
83
|
+
> {
|
|
84
|
+
protected execute(input: { enabled: BOOL; duration: TIME }): { done: BOOL; elapsed: TIME };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** `active` while `enabled`, and for `duration` after it drops. IEC TOF. */
|
|
88
|
+
export declare class OffDelayTimer extends FunctionBlock<
|
|
89
|
+
{ enabled: BOOL; duration: TIME },
|
|
90
|
+
{ active: BOOL; elapsed: TIME }
|
|
91
|
+
> {
|
|
92
|
+
protected execute(input: { enabled: BOOL; duration: TIME }): { active: BOOL; elapsed: TIME };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** `active` for `duration` from a rising `start`; `start` is ignored while active. IEC TP. */
|
|
96
|
+
export declare class PulseTimer extends FunctionBlock<
|
|
97
|
+
{ start: BOOL; duration: TIME },
|
|
98
|
+
{ active: BOOL; elapsed: TIME }
|
|
99
|
+
> {
|
|
100
|
+
protected execute(input: { start: BOOL; duration: TIME }): { active: BOOL; elapsed: TIME };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** `rose` for the one update where `signal` goes from false to true. IEC R_TRIG. */
|
|
104
|
+
export declare class RisingEdge extends FunctionBlock<{ signal: BOOL }, { rose: BOOL }> {
|
|
105
|
+
protected execute(input: { signal: BOOL }): { rose: BOOL };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** `fell` for the one update where `signal` goes from true to false. IEC F_TRIG. */
|
|
109
|
+
export declare class FallingEdge extends FunctionBlock<{ signal: BOOL }, { fell: BOOL }> {
|
|
110
|
+
protected execute(input: { signal: BOOL }): { fell: BOOL };
|
|
111
|
+
}
|