Skip to content

Commit 55da476

Browse files
authored
Feature/api interface (#561)
* Fixed bug where elseBlock statement did not get parent assigned correctly * Improved printer interface * Cleaned up transformer a little * Renamed isEmptyStatement to isStatementEmpty
1 parent 2ec3234 commit 55da476

File tree

3 files changed

+172
-162
lines changed

3 files changed

+172
-162
lines changed

src/LuaAST.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export function createIfStatement(
310310
statement.condition = condition;
311311
setParent(ifBlock, statement);
312312
statement.ifBlock = ifBlock;
313-
setParent(ifBlock, statement);
313+
setParent(elseBlock, statement);
314314
statement.elseBlock = elseBlock;
315315
return statement;
316316
}

src/LuaPrinter.ts

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22

3-
import {SourceNode, SourceMapGenerator, RawSourceMap, SourceMapConsumer} from "source-map";
3+
import {SourceNode, SourceMapGenerator} from "source-map";
44

55
import * as tstl from "./LuaAST";
66
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
@@ -145,19 +145,19 @@ export class LuaPrinter {
145145
return this.concatNodes(header, fileBlockNode);
146146
}
147147

148-
private pushIndent(): void {
148+
protected pushIndent(): void {
149149
this.currentIndent = this.currentIndent + " ";
150150
}
151151

152-
private popIndent(): void {
152+
protected popIndent(): void {
153153
this.currentIndent = this.currentIndent.slice(4);
154154
}
155155

156-
private indent(input: SourceChunk = ""): SourceChunk {
156+
protected indent(input: SourceChunk = ""): SourceChunk {
157157
return this.concatNodes(this.currentIndent, input);
158158
}
159159

160-
private createSourceNode(node: tstl.Node, chunks: SourceChunk | SourceChunk[]): SourceNode {
160+
protected createSourceNode(node: tstl.Node, chunks: SourceChunk | SourceChunk[]): SourceNode {
161161
const originalPos = tstl.getOriginalPos(node);
162162

163163
return originalPos !== undefined && originalPos.line !== undefined && originalPos.column !== undefined
@@ -166,12 +166,12 @@ export class LuaPrinter {
166166
: new SourceNode(null, null, this.sourceFile, chunks);
167167
}
168168

169-
private concatNodes(...chunks: SourceChunk[]): SourceNode {
169+
protected concatNodes(...chunks: SourceChunk[]): SourceNode {
170170
// tslint:disable-next-line:no-null-keyword
171171
return new SourceNode(null, null, this.sourceFile, chunks);
172172
}
173173

174-
private printBlock(block: tstl.Block): SourceNode {
174+
protected printBlock(block: tstl.Block): SourceNode {
175175
return this.createSourceNode(block, this.printStatementArray(block.statements));
176176
}
177177

@@ -196,7 +196,7 @@ export class LuaPrinter {
196196
return result || false;
197197
}
198198

199-
private printStatementArray(statements: tstl.Statement[]): SourceChunk[] {
199+
protected printStatementArray(statements: tstl.Statement[]): SourceChunk[] {
200200
const statementNodes: SourceNode[] = [];
201201
statements = this.removeDeadAndEmptyStatements(statements);
202202
statements.forEach(
@@ -216,7 +216,7 @@ export class LuaPrinter {
216216
return statementNodes.length > 0 ? [...this.joinChunks("\n", statementNodes), "\n"] : [];
217217
}
218218

219-
private printStatement(statement: tstl.Statement): SourceNode {
219+
public printStatement(statement: tstl.Statement): SourceNode {
220220
switch (statement.kind) {
221221
case tstl.SyntaxKind.DoStatement:
222222
return this.printDoStatement(statement as tstl.DoStatement);
@@ -249,7 +249,7 @@ export class LuaPrinter {
249249
}
250250
}
251251

252-
private printDoStatement(statement: tstl.DoStatement): SourceNode {
252+
public printDoStatement(statement: tstl.DoStatement): SourceNode {
253253
const chunks: SourceChunk[] = [];
254254

255255
chunks.push(this.indent("do\n"));
@@ -261,7 +261,7 @@ export class LuaPrinter {
261261
return this.concatNodes(...chunks);
262262
}
263263

264-
private printVariableDeclarationStatement(statement: tstl.VariableDeclarationStatement): SourceNode {
264+
public printVariableDeclarationStatement(statement: tstl.VariableDeclarationStatement): SourceNode {
265265
const chunks: SourceChunk[] = [];
266266

267267
chunks.push(this.indent("local "));
@@ -282,7 +282,7 @@ export class LuaPrinter {
282282
return this.concatNodes(...chunks);
283283
}
284284

285-
private printVariableAssignmentStatement(statement: tstl.AssignmentStatement): SourceNode {
285+
public printVariableAssignmentStatement(statement: tstl.AssignmentStatement): SourceNode {
286286
const chunks: SourceChunk[] = [];
287287

288288
chunks.push(this.indent());
@@ -305,9 +305,12 @@ export class LuaPrinter {
305305
return this.createSourceNode(statement, chunks);
306306
}
307307

308-
private printIfStatement(statement: tstl.IfStatement, isElseIf?: boolean): SourceNode {
308+
public printIfStatement(statement: tstl.IfStatement): SourceNode {
309309
const chunks: SourceChunk[] = [];
310310

311+
const isElseIf = statement.parent !== undefined
312+
&& tstl.isIfStatement(statement.parent);
313+
311314
const prefix = isElseIf ? "elseif" : "if";
312315

313316
chunks.push(this.indent(prefix + " "), this.printExpression(statement.condition), " then\n");
@@ -318,7 +321,7 @@ export class LuaPrinter {
318321

319322
if (statement.elseBlock) {
320323
if (tstl.isIfStatement(statement.elseBlock)) {
321-
chunks.push(this.printIfStatement(statement.elseBlock, true));
324+
chunks.push(this.printIfStatement(statement.elseBlock));
322325
} else {
323326
chunks.push(this.indent("else\n"));
324327
this.pushIndent();
@@ -333,7 +336,7 @@ export class LuaPrinter {
333336
return this.concatNodes(...chunks);
334337
}
335338

336-
private printWhileStatement(statement: tstl.WhileStatement): SourceNode {
339+
public printWhileStatement(statement: tstl.WhileStatement): SourceNode {
337340
const chunks: SourceChunk[] = [];
338341

339342
chunks.push(this.indent("while "), this.printExpression(statement.condition), " do\n");
@@ -347,7 +350,7 @@ export class LuaPrinter {
347350
return this.concatNodes(...chunks);
348351
}
349352

350-
private printRepeatStatement(statement: tstl.RepeatStatement): SourceNode {
353+
public printRepeatStatement(statement: tstl.RepeatStatement): SourceNode {
351354
const chunks: SourceChunk[] = [];
352355

353356
chunks.push(this.indent(`repeat\n`));
@@ -361,7 +364,7 @@ export class LuaPrinter {
361364
return this.concatNodes(...chunks);
362365
}
363366

364-
private printForStatement(statement: tstl.ForStatement): SourceNode {
367+
public printForStatement(statement: tstl.ForStatement): SourceNode {
365368
const ctrlVar = this.printExpression(statement.controlVariable);
366369
const ctrlVarInit = this.printExpression(statement.controlVariableInitializer);
367370
const limit = this.printExpression(statement.limitExpression);
@@ -384,7 +387,7 @@ export class LuaPrinter {
384387
return this.concatNodes(...chunks);
385388
}
386389

387-
private printForInStatement(statement: tstl.ForInStatement): SourceNode {
390+
public printForInStatement(statement: tstl.ForInStatement): SourceNode {
388391
const names = statement.names.map(i => this.printIdentifier(i)).join(", ");
389392
const expressions = statement.expressions.map(e => this.printExpression(e)).join(", ");
390393

@@ -400,15 +403,15 @@ export class LuaPrinter {
400403
return this.createSourceNode(statement, chunks);
401404
}
402405

403-
private printGotoStatement(statement: tstl.GotoStatement): SourceNode {
406+
public printGotoStatement(statement: tstl.GotoStatement): SourceNode {
404407
return this.createSourceNode(statement, [this.indent("goto "), statement.label]);
405408
}
406409

407-
private printLabelStatement(statement: tstl.LabelStatement): SourceNode {
410+
public printLabelStatement(statement: tstl.LabelStatement): SourceNode {
408411
return this.createSourceNode(statement, [this.indent("::"), statement.name, "::"]);
409412
}
410413

411-
private printReturnStatement(statement: tstl.ReturnStatement): SourceNode {
414+
public printReturnStatement(statement: tstl.ReturnStatement): SourceNode {
412415
if (!statement.expressions || statement.expressions.length === 0) {
413416
return this.createSourceNode(statement, this.indent("return"));
414417
}
@@ -420,16 +423,16 @@ export class LuaPrinter {
420423
return this.createSourceNode(statement, [this.indent(), "return ", ...chunks]);
421424
}
422425

423-
private printBreakStatement(statement: tstl.BreakStatement): SourceNode {
426+
public printBreakStatement(statement: tstl.BreakStatement): SourceNode {
424427
return this.createSourceNode(statement, this.indent("break"));
425428
}
426429

427-
private printExpressionStatement(statement: tstl.ExpressionStatement): SourceNode {
430+
public printExpressionStatement(statement: tstl.ExpressionStatement): SourceNode {
428431
return this.concatNodes(this.indent(), this.printExpression(statement.expression));
429432
}
430433

431434
// Expressions
432-
private printExpression(expression: tstl.Expression): SourceNode {
435+
public printExpression(expression: tstl.Expression): SourceNode {
433436
switch (expression.kind) {
434437
case tstl.SyntaxKind.StringLiteral:
435438
return this.printStringLiteral(expression as tstl.StringLiteral);
@@ -467,23 +470,23 @@ export class LuaPrinter {
467470
}
468471
}
469472

470-
private printStringLiteral(expression: tstl.StringLiteral): SourceNode {
473+
public printStringLiteral(expression: tstl.StringLiteral): SourceNode {
471474
return this.createSourceNode(expression, `"${expression.value}"`);
472475
}
473476

474-
private printNumericLiteral(expression: tstl.NumericLiteral): SourceNode {
477+
public printNumericLiteral(expression: tstl.NumericLiteral): SourceNode {
475478
return this.createSourceNode(expression, String(expression.value));
476479
}
477480

478-
private printNilLiteral(expression: tstl.NilLiteral): SourceNode {
481+
public printNilLiteral(expression: tstl.NilLiteral): SourceNode {
479482
return this.createSourceNode(expression, "nil");
480483
}
481484

482-
private printDotsLiteral(expression: tstl.DotsLiteral): SourceNode {
485+
public printDotsLiteral(expression: tstl.DotsLiteral): SourceNode {
483486
return this.createSourceNode(expression, "...");
484487
}
485488

486-
private printBooleanLiteral(expression: tstl.BooleanLiteral): SourceNode {
489+
public printBooleanLiteral(expression: tstl.BooleanLiteral): SourceNode {
487490
if (expression.kind === tstl.SyntaxKind.TrueKeyword) {
488491
return this.createSourceNode(expression, "true");
489492
} else {
@@ -503,7 +506,7 @@ export class LuaPrinter {
503506
return this.joinChunks(", ", parameterChunks);
504507
}
505508

506-
private printFunctionExpression(expression: tstl.FunctionExpression): SourceNode {
509+
public printFunctionExpression(expression: tstl.FunctionExpression): SourceNode {
507510
const chunks: SourceChunk[] = [];
508511

509512
chunks.push("function(");
@@ -531,7 +534,7 @@ export class LuaPrinter {
531534
return this.createSourceNode(expression, chunks);
532535
}
533536

534-
private printFunctionDefinition(statement: tstl.FunctionDefinition): SourceNode {
537+
public printFunctionDefinition(statement: tstl.FunctionDefinition): SourceNode {
535538
const expression = statement.right[0];
536539
const chunks: SourceChunk[] = [];
537540

@@ -549,7 +552,7 @@ export class LuaPrinter {
549552
return this.createSourceNode(expression, chunks);
550553
}
551554

552-
private printTableFieldExpression(expression: tstl.TableFieldExpression): SourceNode {
555+
public printTableFieldExpression(expression: tstl.TableFieldExpression): SourceNode {
553556
const chunks: SourceChunk[] = [];
554557

555558
const value = this.printExpression(expression.value);
@@ -567,7 +570,7 @@ export class LuaPrinter {
567570
return this.createSourceNode(expression, chunks);
568571
}
569572

570-
private printTableExpression(expression: tstl.TableExpression): SourceNode {
573+
public printTableExpression(expression: tstl.TableExpression): SourceNode {
571574
const chunks: SourceChunk[] = [];
572575

573576
chunks.push("{");
@@ -591,7 +594,7 @@ export class LuaPrinter {
591594
return this.createSourceNode(expression, chunks);
592595
}
593596

594-
private printUnaryExpression(expression: tstl.UnaryExpression): SourceNode {
597+
public printUnaryExpression(expression: tstl.UnaryExpression): SourceNode {
595598
const chunks: SourceChunk[] = [];
596599

597600
chunks.push(this.printOperator(expression.operator));
@@ -600,7 +603,7 @@ export class LuaPrinter {
600603
return this.createSourceNode(expression, chunks);
601604
}
602605

603-
private printBinaryExpression(expression: tstl.BinaryExpression): SourceNode {
606+
public printBinaryExpression(expression: tstl.BinaryExpression): SourceNode {
604607
const chunks: SourceChunk[] = [];
605608

606609
chunks.push(this.printExpression(expression.left));
@@ -610,11 +613,11 @@ export class LuaPrinter {
610613
return this.createSourceNode(expression, chunks);
611614
}
612615

613-
private printParenthesizedExpression(expression: tstl.ParenthesizedExpression): SourceNode {
616+
public printParenthesizedExpression(expression: tstl.ParenthesizedExpression): SourceNode {
614617
return this.createSourceNode(expression, ["(", this.printExpression(expression.innerExpression), ")"]);
615618
}
616619

617-
private printCallExpression(expression: tstl.CallExpression): SourceNode {
620+
public printCallExpression(expression: tstl.CallExpression): SourceNode {
618621
const chunks = [];
619622

620623
const parameterChunks = expression.params !== undefined
@@ -626,7 +629,7 @@ export class LuaPrinter {
626629
return this.concatNodes(...chunks);
627630
}
628631

629-
private printMethodCallExpression(expression: tstl.MethodCallExpression): SourceNode {
632+
public printMethodCallExpression(expression: tstl.MethodCallExpression): SourceNode {
630633
const prefix = this.printExpression(expression.prefixExpression);
631634

632635
const parameterChunks = expression.params !== undefined
@@ -638,11 +641,11 @@ export class LuaPrinter {
638641
return this.concatNodes(prefix, ":", name, "(", ...this.joinChunks(", ", parameterChunks), ")");
639642
}
640643

641-
private printIdentifier(expression: tstl.Identifier): SourceNode {
644+
public printIdentifier(expression: tstl.Identifier): SourceNode {
642645
return this.createSourceNode(expression, expression.text);
643646
}
644647

645-
private printTableIndexExpression(expression: tstl.TableIndexExpression): SourceNode {
648+
public printTableIndexExpression(expression: tstl.TableIndexExpression): SourceNode {
646649
const chunks: SourceChunk[] = [];
647650

648651
chunks.push(this.printExpression(expression.table));
@@ -654,18 +657,15 @@ export class LuaPrinter {
654657
return this.createSourceNode(expression, chunks);
655658
}
656659

657-
private printOperator(kind: tstl.Operator): string {
658-
return LuaPrinter.operatorMap[kind];
659-
}
660-
661-
private isEmptyStatement(statement: tstl.Statement): boolean {
662-
return tstl.isDoStatement(statement) && (!statement.statements || statement.statements.length === 0);
660+
public printOperator(kind: tstl.Operator): SourceNode {
661+
// tslint:disable-next-line:no-null-keyword
662+
return new SourceNode(null, null, this.sourceFile, LuaPrinter.operatorMap[kind]);
663663
}
664664

665-
private removeDeadAndEmptyStatements(statements: tstl.Statement[]): tstl.Statement[] {
665+
protected removeDeadAndEmptyStatements(statements: tstl.Statement[]): tstl.Statement[] {
666666
const aliveStatements = [];
667667
for (const statement of statements) {
668-
if (!this.isEmptyStatement(statement)) {
668+
if (!this.isStatementEmpty(statement)) {
669669
aliveStatements.push(statement);
670670
}
671671
if (tstl.isReturnStatement(statement)) {
@@ -675,7 +675,11 @@ export class LuaPrinter {
675675
return aliveStatements;
676676
}
677677

678-
private joinChunks(separator: string, chunks: SourceChunk[]): SourceChunk[] {
678+
protected isStatementEmpty(statement: tstl.Statement): boolean {
679+
return tstl.isDoStatement(statement) && (!statement.statements || statement.statements.length === 0);
680+
}
681+
682+
protected joinChunks(separator: string, chunks: SourceChunk[]): SourceChunk[] {
679683
const result = [];
680684
for (let i = 0; i < chunks.length; i++) {
681685
result.push(chunks[i]);

0 commit comments

Comments
 (0)