@tsplc/compiler 0.6.1 → 0.6.2
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/dist/codesys.js +2 -2
- package/dist/codesys.js.map +1 -1
- package/dist/index.js +1182 -628
- package/dist/index.js.map +14 -12
- package/package.json +9 -9
- package/src/frontend/analyzer.ts +32 -6
- package/src/frontend/elaborate/calls.ts +5 -5
- package/src/frontend/elaborate/frame.ts +2 -1
- package/src/frontend/elaborate/imports.ts +54 -13
- package/src/frontend/elaborate/index.ts +191 -361
- package/src/frontend/elaborate/layouts.ts +57 -30
- package/src/frontend/elaborate/model.ts +20 -1
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/specialize.ts +28 -4
- package/src/frontend/elaborate/storage.ts +7 -7
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"url": "git+https://github.com/rip-technologies/tsplc.git",
|
|
18
18
|
"directory": "packages/compiler"
|
|
19
19
|
},
|
|
20
|
-
"version": "0.6.
|
|
20
|
+
"version": "0.6.2",
|
|
21
21
|
"type": "module",
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|
|
@@ -93,17 +93,17 @@
|
|
|
93
93
|
"bun": ">=1.4.3"
|
|
94
94
|
},
|
|
95
95
|
"dependencies": {
|
|
96
|
-
"@tsplc/agent": "0.6.
|
|
97
|
-
"@tsplc/build": "0.6.
|
|
98
|
-
"@tsplc/config": "0.6.
|
|
99
|
-
"@tsplc/debug": "0.6.
|
|
100
|
-
"@tsplc/opcua": "0.6.
|
|
101
|
-
"@tsplc/typegen": "0.6.
|
|
96
|
+
"@tsplc/agent": "0.6.2",
|
|
97
|
+
"@tsplc/build": "0.6.2",
|
|
98
|
+
"@tsplc/config": "0.6.2",
|
|
99
|
+
"@tsplc/debug": "0.6.2",
|
|
100
|
+
"@tsplc/opcua": "0.6.2",
|
|
101
|
+
"@tsplc/typegen": "0.6.2",
|
|
102
102
|
"typescript": "6.0.3",
|
|
103
103
|
"zod": "4.3.6",
|
|
104
104
|
"@types/bun": "1.4.2",
|
|
105
|
-
"@tsplc/server": "0.6.
|
|
106
|
-
"@tsplc/iec": "0.6.
|
|
105
|
+
"@tsplc/server": "0.6.2",
|
|
106
|
+
"@tsplc/iec": "0.6.2"
|
|
107
107
|
},
|
|
108
108
|
"bin": {
|
|
109
109
|
"tsplc": "./dist/cli.js"
|
package/src/frontend/analyzer.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
} from "@tsplc/compiler/ir";
|
|
11
11
|
import ts from "typescript";
|
|
12
12
|
import { BlockResolver, storageInitializer } from "./blocks";
|
|
13
|
+
import type { Uncompiled } from "./elaborate/reduce";
|
|
13
14
|
import {
|
|
14
15
|
type AnalysisContext,
|
|
15
16
|
ExpressionLowering,
|
|
@@ -99,8 +100,8 @@ export class Analyzer {
|
|
|
99
100
|
private readonly diagnostics: Diagnostic[],
|
|
100
101
|
private readonly positions: Positions,
|
|
101
102
|
serviceRecord: ServiceRecord | null,
|
|
102
|
-
/**
|
|
103
|
-
reduced:
|
|
103
|
+
/** What of each module elaboration rewrote is PLC code, where that is not all of it. */
|
|
104
|
+
private readonly reduced: ReadonlyMap<string, Uncompiled>,
|
|
104
105
|
) {
|
|
105
106
|
this.checker = program.getTypeChecker();
|
|
106
107
|
this.resolver = new TypeResolver(
|
|
@@ -144,7 +145,6 @@ export class Analyzer {
|
|
|
144
145
|
this.sources = runtimeSources(program).filter(
|
|
145
146
|
(source) =>
|
|
146
147
|
source.fileName !== sdk &&
|
|
147
|
-
!reduced.has(source.fileName) &&
|
|
148
148
|
!inSdk(sdk, source.fileName) &&
|
|
149
149
|
!program.isSourceFileDefaultLibrary(source) &&
|
|
150
150
|
(!source.isDeclarationFile || roots.has(source.fileName)),
|
|
@@ -187,9 +187,35 @@ export class Analyzer {
|
|
|
187
187
|
};
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
+
/**
|
|
191
|
+
* A module's statements that are PLC code. A module elaboration cut down is there for
|
|
192
|
+
* TypeScript, but for the constants a module of constants reads; the entry module it wrote also
|
|
193
|
+
* declares, for other modules to import, names that are no code of its own.
|
|
194
|
+
*/
|
|
195
|
+
private statements(source: ts.SourceFile): readonly ts.Statement[] {
|
|
196
|
+
const part = this.reduced.get(source.fileName);
|
|
197
|
+
if (!part) {
|
|
198
|
+
return source.statements;
|
|
199
|
+
}
|
|
200
|
+
const names = (statement: ts.Statement): string[] => {
|
|
201
|
+
if (ts.isVariableStatement(statement)) {
|
|
202
|
+
return statement.declarationList.declarations.map((declaration) =>
|
|
203
|
+
declaration.name.getText(),
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return ts.isEnumDeclaration(statement) ? [statement.name.text] : [];
|
|
207
|
+
};
|
|
208
|
+
return source.statements.filter((statement) =>
|
|
209
|
+
"compiled" in part
|
|
210
|
+
? ts.isVariableStatement(statement) &&
|
|
211
|
+
names(statement).some((name) => part.compiled.has(name))
|
|
212
|
+
: !names(statement).some((name) => part.declared.has(name)),
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
190
216
|
private collectDeclarations(): void {
|
|
191
217
|
const classes = this.sources.flatMap((source) =>
|
|
192
|
-
|
|
218
|
+
this.statements(source).filter(ts.isClassDeclaration),
|
|
193
219
|
);
|
|
194
220
|
this.blocks.index(classes);
|
|
195
221
|
for (const node of classes) {
|
|
@@ -208,7 +234,7 @@ export class Analyzer {
|
|
|
208
234
|
}
|
|
209
235
|
for (const source of this.sources) {
|
|
210
236
|
this.diagnostics.push(...suppressionDiagnostics(source, this.positions));
|
|
211
|
-
for (const statement of
|
|
237
|
+
for (const statement of this.statements(source)) {
|
|
212
238
|
this.guard(() => this.declaration(statement));
|
|
213
239
|
}
|
|
214
240
|
}
|
|
@@ -216,7 +242,7 @@ export class Analyzer {
|
|
|
216
242
|
|
|
217
243
|
private initializeStorage(): void {
|
|
218
244
|
for (const source of this.sources) {
|
|
219
|
-
for (const statement of
|
|
245
|
+
for (const statement of this.statements(source)) {
|
|
220
246
|
if (ts.isVariableStatement(statement) && !source.isDeclarationFile) {
|
|
221
247
|
this.guard(() => {
|
|
222
248
|
for (const declaration of statement.declarationList.declarations) {
|
|
@@ -715,7 +715,7 @@ export class CallSpecializer {
|
|
|
715
715
|
if (work) {
|
|
716
716
|
this.refuseStateInWork(work);
|
|
717
717
|
}
|
|
718
|
-
imports.kept.add(root.file, root.name);
|
|
718
|
+
const local = imports.kept.add(root.file, root.name);
|
|
719
719
|
const args = node.arguments.map((argument) => {
|
|
720
720
|
const contextual = checker.getContextualType(argument);
|
|
721
721
|
return dialect.code(
|
|
@@ -728,8 +728,8 @@ export class CallSpecializer {
|
|
|
728
728
|
kind: "code",
|
|
729
729
|
text:
|
|
730
730
|
name === null
|
|
731
|
-
? mapped`${
|
|
732
|
-
: mapped`${
|
|
731
|
+
? mapped`${local}(${joined(args, ", ")})`
|
|
732
|
+
: mapped`${local}.${name}(${joined(args, ", ")})`,
|
|
733
733
|
declared:
|
|
734
734
|
name === null
|
|
735
735
|
? checker.getAwaitedType(checker.getTypeAtLocation(node))
|
|
@@ -800,7 +800,7 @@ export class CallSpecializer {
|
|
|
800
800
|
"Create the service client in a module-level const, and import it where scan code calls it: the PLC calls the server through that one client, with the types the server's router gives each call.",
|
|
801
801
|
);
|
|
802
802
|
}
|
|
803
|
-
imports.kept.add(client.file, client.name);
|
|
803
|
+
const local = imports.kept.add(client.file, client.name);
|
|
804
804
|
const args = node.arguments.map((argument) => {
|
|
805
805
|
const contextual = checker.getContextualType(argument);
|
|
806
806
|
return dialect.code(
|
|
@@ -810,7 +810,7 @@ export class CallSpecializer {
|
|
|
810
810
|
});
|
|
811
811
|
return {
|
|
812
812
|
kind: "code",
|
|
813
|
-
text: mapped`${
|
|
813
|
+
text: mapped`${local}.${path}(${joined(args, ", ")})`,
|
|
814
814
|
declared: checker.getTypeAtLocation(node),
|
|
815
815
|
};
|
|
816
816
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type ts from "typescript";
|
|
2
2
|
import { from, type Mapped, mapped, type Part } from "../positions";
|
|
3
|
-
import {
|
|
3
|
+
import type { ArrayLayout } from "./layouts";
|
|
4
4
|
import type { ModelNode } from "./model";
|
|
5
|
+
import { unusedName } from "./names";
|
|
5
6
|
import type { Env, Value } from "./values";
|
|
6
7
|
|
|
7
8
|
/** Indented lines of one generated routine, with the local names it has used. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { dirname, relative } from "node:path";
|
|
2
|
+
import type { Names } from "./names";
|
|
2
3
|
|
|
3
4
|
/** The specifier a generated module imports `to` by: relative, dot-prefixed and without its extension. */
|
|
4
5
|
export const importPath = (from: string, to: string): string => {
|
|
@@ -10,22 +11,47 @@ export const importPath = (from: string, to: string): string => {
|
|
|
10
11
|
|
|
11
12
|
/** Names generated code imports from other modules, by module file. */
|
|
12
13
|
export class ModuleImports {
|
|
13
|
-
|
|
14
|
+
/** Each module's imported names, with the name generated code reads each by. */
|
|
15
|
+
private readonly imported = new Map<string, Map<string, string>>();
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
constructor(
|
|
18
|
+
private readonly names: Names,
|
|
19
|
+
/** Names the modules that import these declare themselves, which an import gives way to. */
|
|
20
|
+
private readonly avoided: ReadonlySet<string> = new Set(),
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
/** Whether `name` of `file` is imported. */
|
|
24
|
+
has(file: string, name: string): boolean {
|
|
25
|
+
return this.imported.get(file)?.has(name) ?? false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Records an import of `name` from `file`, and returns the name to read it by: its own, unless
|
|
30
|
+
* the generated modules already use that name for something else.
|
|
31
|
+
*/
|
|
16
32
|
add(file: string, name: string): string {
|
|
17
|
-
const names = this.
|
|
18
|
-
|
|
19
|
-
this.names.
|
|
20
|
-
|
|
33
|
+
const names = this.imported.get(file) ?? new Map<string, string>();
|
|
34
|
+
this.imported.set(file, names);
|
|
35
|
+
const local = this.names.import(`${file}\0${name}`, name, this.avoided);
|
|
36
|
+
names.set(name, local);
|
|
37
|
+
return local;
|
|
21
38
|
}
|
|
22
39
|
|
|
23
40
|
/** One import statement per module, as `importer` reaches it. */
|
|
24
41
|
lines(importer: string, keyword: "import" | "import type"): string[] {
|
|
25
|
-
return [...this.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
return [...this.imported].flatMap(([file, names]) => {
|
|
43
|
+
const list = [...names]
|
|
44
|
+
// A module already has what it declares itself, under its own name.
|
|
45
|
+
.filter(([name, local]) => file !== importer || name !== local)
|
|
46
|
+
.map(([name, local]) =>
|
|
47
|
+
name === local ? name : `${name} as ${local}`,
|
|
48
|
+
);
|
|
49
|
+
return list.length
|
|
50
|
+
? [
|
|
51
|
+
`${keyword} { ${list.join(", ")} } from "${importPath(importer, file)}";`,
|
|
52
|
+
]
|
|
53
|
+
: [];
|
|
54
|
+
});
|
|
29
55
|
}
|
|
30
56
|
}
|
|
31
57
|
|
|
@@ -35,12 +61,27 @@ export class ModuleImports {
|
|
|
35
61
|
*/
|
|
36
62
|
export class EntryImports {
|
|
37
63
|
/** 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
|
|
64
|
+
private readonly namespaces = new Map<string, string>();
|
|
65
|
+
/** Bindings of kept modules, read by their own names unless something else has them. */
|
|
66
|
+
readonly kept: ModuleImports;
|
|
41
67
|
/** SDK functions by the module that declares them, such as `@tsplc/compiler/clock`. */
|
|
42
68
|
private readonly sdk = new Map<string, Set<string>>();
|
|
43
69
|
|
|
70
|
+
constructor(private readonly names: Names) {
|
|
71
|
+
this.kept = new ModuleImports(names);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Records a namespace import of a storage module, and returns the name to read it by: `name`,
|
|
76
|
+
* unless the generated modules already use that for something else, as two roots whose names
|
|
77
|
+
* differ only in their separators would.
|
|
78
|
+
*/
|
|
79
|
+
namespace(file: string, name: string): string {
|
|
80
|
+
const local = this.names.import(file, name);
|
|
81
|
+
this.namespaces.set(local, file);
|
|
82
|
+
return local;
|
|
83
|
+
}
|
|
84
|
+
|
|
44
85
|
/** Records a call of `name`, which `module` of the SDK declares. */
|
|
45
86
|
addSdk(module: string, name: string): void {
|
|
46
87
|
const names = this.sdk.get(module) ?? new Set<string>();
|