Skip to content

Commit 6dca417

Browse files
committed
Integrated transformer into compile & test pipeline
1 parent a5d2c22 commit 6dca417

33 files changed

+588
-3770
lines changed

src/Compiler.ts

Lines changed: 13 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import * as path from "path";
33
import * as ts from "typescript";
44

55
import { parseCommandLine } from "./CommandLineParser";
6-
import { CompilerOptions } from "./CompilerOptions";
7-
import { LuaLibImportKind, LuaTarget } from "./Transpiler";
6+
import { CompilerOptions, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
7+
import { createTransformer } from "./TransformerFactory";
8+
import { LuaTranspiler } from "./LuaTranspiler";
89

9-
import { createTranspiler } from "./TranspilerFactory";
1010

1111
export function compile(argv: string[]): void {
1212
const commandLine = parseCommandLine(argv);
@@ -40,7 +40,9 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions):
4040
}
4141

4242
host.afterProgramCreate = program => {
43-
const status = emitFilesAndReportErrors(program.getProgram());
43+
const transpiler = new LuaTranspiler(program.getProgram());
44+
45+
const status = transpiler.emitFilesAndReportErrors();
4446
const errorDiagnostic: ts.Diagnostic = {
4547
category: undefined,
4648
code: 6194,
@@ -72,85 +74,9 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions):
7274
export function compileFilesWithOptions(fileNames: string[], options: CompilerOptions): void {
7375
const program = ts.createProgram(fileNames, options);
7476

75-
emitFilesAndReportErrors(program);
76-
}
77-
78-
function emitFilesAndReportErrors(program: ts.Program): number {
79-
const options = program.getCompilerOptions() as CompilerOptions;
80-
81-
const checker = program.getTypeChecker();
82-
83-
// Get all diagnostics, ignore unsupported extension
84-
const diagnostics = ts.getPreEmitDiagnostics(program).filter(diag => diag.code !== 6054);
85-
diagnostics.forEach(reportDiagnostic);
86-
87-
// If there are errors dont emit
88-
if (diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error).length > 0) {
89-
if (!options.watch) {
90-
process.exit(1);
91-
} else {
92-
return 1;
93-
}
94-
}
95-
96-
program.getSourceFiles().forEach(sourceFile => {
97-
98-
if (!sourceFile.isDeclarationFile) {
99-
try {
100-
const rootDir = options.rootDir;
101-
102-
// Transpile AST
103-
const lua = createTranspiler(checker, options, sourceFile).transpileSourceFile();
104-
105-
let outPath = sourceFile.fileName;
106-
if (options.outDir !== options.rootDir) {
107-
const relativeSourcePath = path.resolve(sourceFile.fileName)
108-
.replace(path.resolve(rootDir), "");
109-
outPath = path.join(options.outDir, relativeSourcePath);
110-
}
111-
112-
// change extension or rename to outFile
113-
if (options.outFile) {
114-
if (path.isAbsolute(options.outFile)) {
115-
outPath = options.outFile;
116-
} else {
117-
// append to workingDir or outDir
118-
outPath = path.resolve(options.outDir, options.outFile);
119-
}
120-
} else {
121-
const fileNameLua = path.basename(outPath, path.extname(outPath)) + ".lua";
122-
outPath = path.join(path.dirname(outPath), fileNameLua);
123-
}
124-
125-
// Write output
126-
ts.sys.writeFile(outPath, lua);
127-
} catch (exception) {
128-
/* istanbul ignore else: Testing else part would require to add a bug/exception to our code */
129-
if (exception.node) {
130-
const pos = ts.getLineAndCharacterOfPosition(sourceFile, exception.node.pos);
131-
// Graciously handle transpilation errors
132-
console.error("Encountered error parsing file: " + exception.message);
133-
console.error(
134-
sourceFile.fileName + " line: " + (1 + pos.line) + " column: " + pos.character + "\n" +
135-
exception.stack
136-
);
137-
process.exit(1);
138-
} else {
139-
throw exception;
140-
}
141-
}
142-
}
143-
});
77+
const transpiler = new LuaTranspiler(program);
14478

145-
// Copy lualib to target dir
146-
if (options.luaLibImport === LuaLibImportKind.Require || options.luaLibImport === LuaLibImportKind.Always) {
147-
fs.copyFileSync(
148-
path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"),
149-
path.join(options.outDir, "lualib_bundle.lua")
150-
);
151-
}
152-
153-
return 0;
79+
transpiler.emitFilesAndReportErrors();
15480
}
15581

15682
const libSource = fs.readFileSync(path.join(path.dirname(require.resolve("typescript")), "lib.es6.d.ts")).toString();
@@ -187,23 +113,9 @@ export function transpileString(str: string,
187113
};
188114
const program = ts.createProgram(["file.ts"], options, compilerHost);
189115

190-
const result = createTranspiler(program.getTypeChecker(),
191-
options,
192-
program.getSourceFile("file.ts")).transpileSourceFile();
193-
return result.trim();
194-
}
116+
const transpiler = new LuaTranspiler(program);
195117

196-
function reportDiagnostic(diagnostic: ts.Diagnostic): void {
197-
if (diagnostic.file) {
198-
const { line, character } =
199-
diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
200-
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
201-
console.log(
202-
`${diagnostic.code}: ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`
203-
);
204-
} else {
205-
console.log(
206-
`${diagnostic.code}: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`
207-
);
208-
}
209-
}
118+
const result = transpiler.transpileSourceFile(program.getSourceFile("file.ts"));
119+
120+
return result.trim();
121+
}

src/CompilerOptions.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ export interface CompilerOptions extends ts.CompilerOptions {
55
luaTarget?: string;
66
luaLibImport?: string;
77
}
8+
9+
export enum LuaLibImportKind {
10+
None = "none",
11+
Always = "always",
12+
Inline = "inline",
13+
Require = "require",
14+
}
15+
16+
export enum LuaTarget {
17+
Lua51 = "5.1",
18+
Lua52 = "5.2",
19+
Lua53 = "5.3",
20+
LuaJIT = "jit",
21+
}

src/LuaAST.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ export function createFunctionExpression(
570570
body: Block,
571571
params?: Identifier[],
572572
dots?: DotsLiteral,
573-
restParamName?: Identifier;
573+
restParamName?: Identifier,
574574
parent?: Node,
575575
tsOriginal?: ts.Node): FunctionExpression {
576576

src/LuaNode.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/LuaPrinter.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export class LuaPrinter {
3434

3535
private currentIndent: string;
3636

37+
public print(block: tstl.Block): string {
38+
return this.printBlock(block);
39+
}
40+
3741
private pushIndent(): void {
3842
this.currentIndent = this.currentIndent + " ";
3943
}
@@ -80,6 +84,7 @@ export class LuaPrinter {
8084
return this.printExpressionStatement(statement as tstl.ExpressionStatement);
8185
}
8286
}
87+
8388
private printDoStatement(statement: tstl.DoStatement): string {
8489
let result = this.indent("do\n");
8590
this.pushIndent();
@@ -89,14 +94,17 @@ export class LuaPrinter {
8994

9095
return result;
9196
}
97+
9298
private printVariableDeclarationStatement(statement: tstl.VariableDeclarationStatement): string {
9399
return this.indent(`"local" ${statement.left.map(e => this.printExpression(e)).join(", ")} =` +
94100
`${statement.right.map(e => this.printExpression(e)).join(", ")};\n`);
95101
}
102+
96103
private printVariableAssignmentStatement(statement: tstl.VariableAssignmentStatement): string {
97104
return this.indent(`${statement.left.map(e => this.printExpression(e)).join(", ")} =` +
98105
`${statement.right.map(e => this.printExpression(e)).join(", ")};\n`);
99106
}
107+
100108
private printIfStatement(statement: tstl.IfStatement): string {
101109
let result = this.indent(`if ${this.printExpression(statement.condtion)} then\n`);
102110
this.pushIndent();
@@ -116,6 +124,7 @@ export class LuaPrinter {
116124

117125
return result;
118126
}
127+
119128
private printWhileStatement(statement: tstl.WhileStatement): string {
120129
let result = this.indent(`while ${this.printExpression(statement.condtion)} do\n`);
121130
this.pushIndent();
@@ -125,6 +134,7 @@ export class LuaPrinter {
125134

126135
return result;
127136
}
137+
128138
private printRepeatStatement(statement: tstl.RepeatStatement): string {
129139
let result = this.indent(`repeat\n`);
130140
this.pushIndent();
@@ -134,6 +144,7 @@ export class LuaPrinter {
134144

135145
return result;
136146
}
147+
137148
private printForStatement(statement: tstl.ForStatement): string {
138149
const ctrlVar = this.printExpression(statement.controlVariable);
139150
const ctrlVarInit = this.printExpression(statement.controlVariableInitializer);
@@ -153,6 +164,7 @@ export class LuaPrinter {
153164

154165
return result;
155166
}
167+
156168
private printForInStatement(statement: tstl.ForInStatement): string {
157169
const names = statement.names.map(i => this.printIdentifier(i)).join(", ");
158170
const expressions = statement.names.map(e => this.printExpression(e)).join(", ");
@@ -165,24 +177,30 @@ export class LuaPrinter {
165177

166178
return result;
167179
}
180+
168181
private printGotoStatement(statement: tstl.GotoStatement): string {
169182
return this.indent(`goto ${statement.label};\n`);
170183
}
184+
171185
private printLabelStatement(statement: tstl.LabelStatement): string {
172186
return this.indent(`::${statement.name}::\n`);
173187
}
188+
174189
private printReturnStatement(statement: tstl.ReturnStatement): string {
175190
if (!statement.expressions) {
176191
return this.indent(`return;\n`);
177192
}
178193
return this.indent(`return ${statement.expressions.map(e => this.printExpression(e)).join(", ")};\n`);
179194
}
195+
180196
private printBreakStatement(statement: tstl.BreakStatement): string {
181197
return this.indent("break;\n");
182198
}
199+
183200
private printExpressionStatement(statement: tstl.ExpressionStatement): string {
184201
return this.indent(`${this.printExpression(statement.expression)};\n`);
185202
}
203+
186204
// Expressions
187205
private printExpression(expression: tstl.Expression): string {
188206
switch (expression.kind) {
@@ -219,25 +237,31 @@ export class LuaPrinter {
219237
return this.printTableIndexExpression(expression as tstl.TableIndexExpression);
220238
}
221239
}
240+
222241
private printStringLiteral(expression: tstl.StringLiteral): string {
223242
return `"${expression.value}"`;
224243
}
244+
225245
private printNumericLiteral(expression: tstl.NumericLiteral): string {
226246
return `${expression.value}`;
227247
}
248+
228249
private printNilLiteral(expression: tstl.NilLiteral): string {
229250
return "nil";
230251
}
252+
231253
private printDotsLiteral(expression: tstl.DotsLiteral): string {
232254
return "...";
233255
}
256+
234257
private printBooleanLiteral(expression: tstl.BooleanLiteral): string {
235258
if (expression.kind === tstl.SyntaxKind.TrueKeyword) {
236259
return "true";
237260
} else {
238261
return "false";
239262
}
240263
}
264+
241265
private printFunctionExpression(expression: tstl.FunctionExpression): string {
242266
const paramterArr: string[] = expression.params.map(i => this.printIdentifier(i));
243267
if (expression.dots) {
@@ -252,6 +276,7 @@ export class LuaPrinter {
252276

253277
return result;
254278
}
279+
255280
private printTableFieldExpression(expression: tstl.TableFieldExpression): string {
256281
const value = this.printExpression(expression.value);
257282

@@ -261,41 +286,50 @@ export class LuaPrinter {
261286
return value;
262287
}
263288
}
289+
264290
private printTableExpression(expression: tstl.TableExpression): string {
265291
return `{${expression.fields.map(f => this.printTableFieldExpression(f)).join(", ")}}`;
266292
}
293+
267294
private printUnaryExpression(expression: tstl.UnaryExpression): string {
268295
return `${this.printOperator(expression.operator)}${this.printExpression(expression.operand)}`;
269296
}
297+
270298
private printBinaryExpression(expression: tstl.BinaryExpression): string {
271299
const left = this.printExpression(expression.left);
272300
const operator = this.printOperator(expression.operator);
273301
const right = this.printExpression(expression.right);
274302
return `${left} ${operator} ${right}`;
275303
}
304+
276305
private printParenthesizedExpression(expression: tstl.ParenthesizedExpression): string {
277306
return `(${this.printExpression(expression.innerEpxression)})`;
278307
}
308+
279309
private printCallExpression(expression: tstl.CallExpression): string {
280310
const params = expression.params.map(e => this.printExpression(e)).join(", ");
281311
return `${this.printExpression(expression.expression)}(${params}))`;
282312
}
313+
283314
private printMethodCallExpression(expression: tstl.MethodCallExpression): string {
284315
const params = expression.params.map(e => this.printExpression(e)).join(", ");
285316
const prefix = this.printExpression(expression.prefixExpression);
286317
const name = this.printIdentifier(expression.name);
287318
return `${prefix}:${name}(${params}))`;
288319
}
320+
289321
private printIdentifier(expression: tstl.Identifier): string {
290322
return expression.text;
291323
}
324+
292325
private printTableIndexExpression(expression: tstl.TableIndexExpression): string {
293326
const table = this.printExpression(expression.table);
294327
if (tstl.isIdentifier(expression.index)) {
295328
return `${table}.${this.printIdentifier(expression.index)}`;
296329
}
297330
return `${table}[${this.printExpression(expression.index)}]`;
298331
}
332+
299333
private printOperator(kind: tstl.Operator): string {
300334
return LuaPrinter.operatorMap[kind];
301335
}

0 commit comments

Comments
 (0)