@tsplc/compiler 0.2.17 → 0.2.19
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/README.md +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
import { from, mapped, type Mapped, type Part } from "../positions.ts";
|
|
3
|
+
import { unusedName } from "./layouts.ts";
|
|
4
|
+
import type { ModelNode } from "./model.ts";
|
|
5
|
+
import type { Env } from "./values.ts";
|
|
6
|
+
|
|
7
|
+
/** Indented lines of one generated routine, with the local names it has used. */
|
|
8
|
+
export class Emitter {
|
|
9
|
+
readonly lines: Mapped[] = [];
|
|
10
|
+
private depth = 1;
|
|
11
|
+
private readonly used = new Set<string>();
|
|
12
|
+
private temporaries = 0;
|
|
13
|
+
|
|
14
|
+
/** @param origin The source the next lines come from: the statement being specialized. */
|
|
15
|
+
constructor(public origin: ts.Node) {}
|
|
16
|
+
|
|
17
|
+
line(text: Part): void {
|
|
18
|
+
this.lines.push(
|
|
19
|
+
from(this.origin, mapped`${" ".repeat(this.depth)}${text}`),
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
open(text: Part): void {
|
|
23
|
+
this.line(text);
|
|
24
|
+
this.depth++;
|
|
25
|
+
}
|
|
26
|
+
close(text: Part = "}"): void {
|
|
27
|
+
this.depth--;
|
|
28
|
+
this.line(text);
|
|
29
|
+
}
|
|
30
|
+
/** Ends one block and starts the next on the same line, as `} else {` does. */
|
|
31
|
+
reopen(text: Part): void {
|
|
32
|
+
this.depth--;
|
|
33
|
+
this.line(text);
|
|
34
|
+
this.depth++;
|
|
35
|
+
}
|
|
36
|
+
local(name: string): string {
|
|
37
|
+
const unused = unusedName(name, (candidate) => this.used.has(candidate));
|
|
38
|
+
this.used.add(unused);
|
|
39
|
+
return unused;
|
|
40
|
+
}
|
|
41
|
+
temporary(): string {
|
|
42
|
+
return this.local(`tsplcTemp${++this.temporaries}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface Frame {
|
|
47
|
+
emitter: Emitter;
|
|
48
|
+
env: Env;
|
|
49
|
+
self?: ModelNode;
|
|
50
|
+
/** The routine's declared return type, for literal and optional results. */
|
|
51
|
+
returns?: ts.Type;
|
|
52
|
+
async: boolean;
|
|
53
|
+
}
|
|
@@ -7,88 +7,133 @@ import { fileURLToPath } from "node:url";
|
|
|
7
7
|
|
|
8
8
|
/** What capture.ts needs: the program's runtime files and the bindings each declares. */
|
|
9
9
|
export interface CaptureRequest {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
entry: string;
|
|
11
|
+
files: string[];
|
|
12
|
+
bindings: Record<
|
|
13
|
+
string,
|
|
14
|
+
{ values: string[]; classes: string[]; functions: string[] }
|
|
15
|
+
>;
|
|
13
16
|
}
|
|
14
17
|
|
|
15
18
|
export type CapturedValue =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
| { kind: "undefined" }
|
|
20
|
+
| { kind: "number"; value: number }
|
|
21
|
+
| { kind: "boolean"; value: boolean }
|
|
22
|
+
| { kind: "string"; value: string }
|
|
23
|
+
| { kind: "bigint"; value: string }
|
|
24
|
+
| { kind: "node"; id: number };
|
|
25
|
+
|
|
26
|
+
/** A captured value that is not an object setup built. */
|
|
27
|
+
export type CapturedPrimitive = Exclude<CapturedValue, { kind: "node" }>;
|
|
22
28
|
|
|
23
29
|
export type CapturedNode =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
| { kind: "object"; fields: Record<string, CapturedValue> }
|
|
31
|
+
| {
|
|
32
|
+
kind: "instance";
|
|
33
|
+
file: string;
|
|
34
|
+
name: string;
|
|
35
|
+
fields: Record<string, CapturedValue>;
|
|
36
|
+
}
|
|
37
|
+
| { kind: "array"; items: CapturedValue[] }
|
|
38
|
+
| { kind: "function"; file: string; name: string }
|
|
39
|
+
| { kind: "service"; path: string };
|
|
29
40
|
|
|
30
41
|
export interface CapturedGraph {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
42
|
+
nodes: CapturedNode[];
|
|
43
|
+
/** Every module-level value binding, in module evaluation order. */
|
|
44
|
+
bindings: { file: string; name: string; value: CapturedValue }[];
|
|
34
45
|
}
|
|
35
46
|
|
|
36
47
|
const names = (source: ts.SourceFile) => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
48
|
+
const found: CaptureRequest["bindings"][string] = {
|
|
49
|
+
values: [],
|
|
50
|
+
classes: [],
|
|
51
|
+
functions: [],
|
|
52
|
+
};
|
|
53
|
+
const bind = (name: ts.BindingName): void => {
|
|
54
|
+
if (ts.isIdentifier(name)) {
|
|
55
|
+
found.values.push(name.text);
|
|
56
|
+
} else {
|
|
57
|
+
for (const element of name.elements) {
|
|
58
|
+
if (!ts.isOmittedExpression(element)) {
|
|
59
|
+
bind(element.name);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
for (const statement of source.statements) {
|
|
65
|
+
if (ts.isVariableStatement(statement)) {
|
|
66
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
67
|
+
bind(declaration.name);
|
|
68
|
+
}
|
|
69
|
+
} else if (ts.isClassDeclaration(statement) && statement.name) {
|
|
70
|
+
found.classes.push(statement.name.text);
|
|
71
|
+
} else if (
|
|
72
|
+
ts.isFunctionDeclaration(statement) &&
|
|
73
|
+
statement.name &&
|
|
74
|
+
statement.body
|
|
75
|
+
) {
|
|
76
|
+
found.functions.push(statement.name.text);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Arrow functions bound to consts are functions too: a stored callback names its binding.
|
|
80
|
+
for (const statement of source.statements) {
|
|
81
|
+
if (ts.isVariableStatement(statement)) {
|
|
82
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
83
|
+
if (
|
|
84
|
+
ts.isIdentifier(declaration.name) &&
|
|
85
|
+
declaration.initializer &&
|
|
86
|
+
(ts.isArrowFunction(declaration.initializer) ||
|
|
87
|
+
ts.isFunctionExpression(declaration.initializer))
|
|
88
|
+
) {
|
|
89
|
+
found.values = found.values.filter(
|
|
90
|
+
(name) => name !== declaration.name.getText(),
|
|
91
|
+
);
|
|
92
|
+
found.functions.push(declaration.name.text);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return found;
|
|
65
98
|
};
|
|
66
99
|
|
|
67
100
|
/** Runs the program's setup in a Bun subprocess and returns the object graph it built. */
|
|
68
|
-
export const captureGraph = (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
101
|
+
export const captureGraph = (
|
|
102
|
+
entry: string,
|
|
103
|
+
sources: readonly ts.SourceFile[],
|
|
104
|
+
): CapturedGraph => {
|
|
105
|
+
const files = sources.filter((source) => !source.isDeclarationFile);
|
|
106
|
+
const request: CaptureRequest = {
|
|
107
|
+
entry,
|
|
108
|
+
files: files.map((source) => source.fileName),
|
|
109
|
+
bindings: Object.fromEntries(
|
|
110
|
+
files.map((source) => [source.fileName, names(source)]),
|
|
111
|
+
),
|
|
112
|
+
};
|
|
113
|
+
const directory = mkdtempSync(join(tmpdir(), "tsplc-capture-"));
|
|
114
|
+
try {
|
|
115
|
+
const file = join(directory, "request.json");
|
|
116
|
+
writeFileSync(file, JSON.stringify(request));
|
|
117
|
+
// The source tree runs capture.ts; a built package ships it beside the bundle.
|
|
118
|
+
const script = ["./capture.ts", "./elaborate-capture.js"]
|
|
119
|
+
.map((path) => fileURLToPath(new URL(path, import.meta.url)))
|
|
120
|
+
.find((path) => existsSync(path));
|
|
121
|
+
if (!script) {
|
|
122
|
+
throw new Error(
|
|
123
|
+
"The elaboration capture script is missing from this build.",
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
const result = spawnSync(process.execPath, [script, file], {
|
|
127
|
+
encoding: "utf8",
|
|
128
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
129
|
+
});
|
|
130
|
+
if (result.status !== 0) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`Running the program's setup failed:\n${result.stderr.trim()}`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
return JSON.parse(result.stdout) as CapturedGraph;
|
|
136
|
+
} finally {
|
|
137
|
+
rmSync(directory, { recursive: true, force: true });
|
|
138
|
+
}
|
|
94
139
|
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { dirname, relative } from "node:path";
|
|
2
|
+
|
|
3
|
+
/** The specifier a generated module imports `to` by: relative, dot-prefixed and without its extension. */
|
|
4
|
+
export const importPath = (from: string, to: string): string => {
|
|
5
|
+
const path = relative(dirname(from), to)
|
|
6
|
+
.replace(/\\/g, "/")
|
|
7
|
+
.replace(/\.tsx?$/, "");
|
|
8
|
+
return path.startsWith("./") || path.startsWith("../") ? path : `./${path}`;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** Names generated code imports from other modules, by module file. */
|
|
12
|
+
export class ModuleImports {
|
|
13
|
+
private readonly names = new Map<string, Set<string>>();
|
|
14
|
+
|
|
15
|
+
/** Records an import of `name` from `file`, and returns the name to read it by. */
|
|
16
|
+
add(file: string, name: string): string {
|
|
17
|
+
const names = this.names.get(file) ?? new Set<string>();
|
|
18
|
+
names.add(name);
|
|
19
|
+
this.names.set(file, names);
|
|
20
|
+
return name;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** One import statement per module, as `importer` reaches it. */
|
|
24
|
+
lines(importer: string, keyword: "import" | "import type"): string[] {
|
|
25
|
+
return [...this.names].map(
|
|
26
|
+
([file, names]) =>
|
|
27
|
+
`${keyword} { ${[...names].join(", ")} } from "${importPath(importer, file)}";`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* What the generated entry module imports besides the generated types: the storage modules,
|
|
34
|
+
* kept modules' bindings and the SDK functions scan code calls.
|
|
35
|
+
*/
|
|
36
|
+
export class EntryImports {
|
|
37
|
+
/** Storage modules generated code reads through a namespace import, by import name. */
|
|
38
|
+
readonly namespaces = new Map<string, string>();
|
|
39
|
+
/** Bindings of kept modules, read by their own names. */
|
|
40
|
+
readonly kept = new ModuleImports();
|
|
41
|
+
/** `@tsplc/compiler/clock` functions scan code calls. */
|
|
42
|
+
readonly clock = new Set<string>();
|
|
43
|
+
/** Conversions generated code calls from `@tsplc/compiler/types`. */
|
|
44
|
+
readonly conversions = new Set<string>();
|
|
45
|
+
/** Whether scan code calls `telemetry`. */
|
|
46
|
+
telemetry = false;
|
|
47
|
+
|
|
48
|
+
lines(entryFile: string): string[] {
|
|
49
|
+
return [
|
|
50
|
+
...[...this.namespaces].map(
|
|
51
|
+
([name, file]) =>
|
|
52
|
+
`import * as ${name} from "${importPath(entryFile, file)}";`,
|
|
53
|
+
),
|
|
54
|
+
...this.kept.lines(entryFile, "import"),
|
|
55
|
+
...(this.clock.size
|
|
56
|
+
? [
|
|
57
|
+
`import { ${[...this.clock].join(", ")} } from "@tsplc/compiler/clock";`,
|
|
58
|
+
]
|
|
59
|
+
: []),
|
|
60
|
+
...(this.telemetry
|
|
61
|
+
? [`import { telemetry } from "@tsplc/compiler/telemetry";`]
|
|
62
|
+
: []),
|
|
63
|
+
...(this.conversions.size
|
|
64
|
+
? [
|
|
65
|
+
`import { ${[...this.conversions].join(", ")} } from "@tsplc/compiler/types";`,
|
|
66
|
+
]
|
|
67
|
+
: []),
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
}
|