Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class LuaTranspiler {
public importCount: number;
public isModule: boolean;
public sourceFile: ts.SourceFile;
public loopStack: number[];

constructor(checker: ts.TypeChecker, options: ts.CompilerOptions, sourceFile: ts.SourceFile) {
this.indent = "";
Expand All @@ -72,6 +73,7 @@ export class LuaTranspiler {
this.importCount = 0;
this.sourceFile = sourceFile;
this.isModule = tsHelper.isFileModule(sourceFile);
this.loopStack = [];
}

public pushIndent(): void {
Expand Down Expand Up @@ -194,8 +196,7 @@ export class LuaTranspiler {
case ts.SyntaxKind.ThrowStatement:
return this.transpileThrow(node as ts.ThrowStatement);
case ts.SyntaxKind.ContinueStatement:
// Disallow continue
throw new TranspileError("Continue is not supported in Lua", node);
return this.transpileContinue();
case ts.SyntaxKind.TypeAliasDeclaration:
case ts.SyntaxKind.InterfaceDeclaration:
case ts.SyntaxKind.EndOfFileToken:
Expand Down Expand Up @@ -317,6 +318,10 @@ export class LuaTranspiler {
}
}

public transpileContinue(): string {
return this.indent + `goto __continue${this.loopStack[this.loopStack.length - 1]}\n`;
}

public transpileIf(node: ts.IfStatement): string {
const condition = this.transpileExpression(node.expression);

Expand All @@ -335,12 +340,30 @@ export class LuaTranspiler {
return result + this.indent + "end\n";
}

public transpileLoopBody(
node: ts.WhileStatement
| ts.DoStatement
| ts.ForStatement
| ts.ForOfStatement
| ts.ForInStatement
): string {
this.loopStack.push(this.genVarCounter);
this.genVarCounter++;
let result = this.indent + "do\n";
this.pushIndent();
result += this.transpileStatement(node.statement);
this.popIndent();
result += this.indent + "end\n";
result += this.indent + `::__continue${this.loopStack.pop()}::\n`;
return result;
}

public transpileWhile(node: ts.WhileStatement): string {
const condition = this.transpileExpression(node.expression);

let result = this.indent + `while ${condition} do\n`;
this.pushIndent();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can push the indent inside the transpileLoopBody function, this will reduce code reuse even more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with this. If you look at the regular for loop it translates the incrementor after the loop body but inside the indent. If you move the indent inside the loop body method this no longer works. You could ofcourse move only the push inside but I think that is bad form, push and pop should be obvious in the same context.

result += this.transpileStatement(node.statement);
result += this.transpileLoopBody(node);
this.popIndent();
return result + this.indent + "end\n";
}
Expand All @@ -349,7 +372,7 @@ export class LuaTranspiler {
let result = this.indent + `repeat\n`;

this.pushIndent();
result += this.transpileStatement(node.statement);
result += this.transpileLoopBody(node);
this.popIndent();

// Negate the expression because we translate from do-while to repeat-until (repeat-while-not)
Expand All @@ -368,7 +391,7 @@ export class LuaTranspiler {

// Add body
this.pushIndent();
result += this.transpileStatement(node.statement);
result += this.transpileLoopBody(node);
result += this.indent + this.transpileExpression(node.incrementor) + "\n";
this.popIndent();

Expand All @@ -394,7 +417,7 @@ export class LuaTranspiler {

// For body
this.pushIndent();
result += this.transpileStatement(node.statement);
result += this.transpileLoopBody(node);
this.popIndent();

return result + this.indent + "end\n";
Expand All @@ -417,7 +440,7 @@ export class LuaTranspiler {

// For body
this.pushIndent();
result += this.transpileStatement(node.statement);
result += this.transpileLoopBody(node);
this.popIndent();

return result + this.indent + "end\n";
Expand Down
10 changes: 10 additions & 0 deletions test/translation/lua/continue.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local i = 0
while(i<10) do
do
if i<5 then
goto __continue0
end
end
::__continue0::
i=i+1
end
13 changes: 13 additions & 0 deletions test/translation/lua/continueConcurrent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local i = 0
while(i<10) do
do
if i<5 then
goto __continue0
end
if i==7 then
goto __continue0
end
end
::__continue0::
i=i+1
end
20 changes: 20 additions & 0 deletions test/translation/lua/continueNested.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local i = 0
while(i<5) do
do
if (i%2)==0 then
goto __continue0
end
local j = 0
while(j<2) do
do
if j==1 then
goto __continue1
end
end
::__continue1::
j=j+1
end
end
::__continue0::
i=i+1
end
23 changes: 23 additions & 0 deletions test/translation/lua/continueNestedConcurrent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local i = 0
while(i<5) do
do
if (i%2)==0 then
goto __continue0
end
local j = 0
while(j<2) do
do
if j==1 then
goto __continue1
end
end
::__continue1::
j=j+1
end
if i==4 then
goto __continue0
end
end
::__continue0::
i=i+1
end
8 changes: 8 additions & 0 deletions test/translation/lua/do.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local e = 10

repeat
do
e=e-1
end
::__continue0::
until not (e>0)
7 changes: 7 additions & 0 deletions test/translation/lua/for.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local i = 1
while(i<=100) do
do
end
::__continue0::
i=i+1
end
5 changes: 5 additions & 0 deletions test/translation/lua/forIn.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
for i, _ in pairs({a = 1,b = 2,c = 3,d = 4}) do
do
end
::__continue0::
end
5 changes: 5 additions & 0 deletions test/translation/lua/forOf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
for _, i in ipairs({1,2,3,4,5,6,7,8,9,10}) do
do
end
::__continue0::
end
3 changes: 3 additions & 0 deletions test/translation/lua/tupleArrayUses.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for _, value in ipairs(tuple) do
do
end
::__continue0::
end
TS_forEach(tuple, function(v)
end
Expand Down
8 changes: 8 additions & 0 deletions test/translation/lua/while.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local d = 10

while d>0 do
do
d=d-1
end
::__continue0::
end
5 changes: 5 additions & 0 deletions test/translation/ts/continue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
for (let i = 0; i < 10; i++) {
if (i < 5) {
continue;
}
}
9 changes: 9 additions & 0 deletions test/translation/ts/continueConcurrent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
for (let i = 0; i < 10; i++) {
if (i < 5) {
continue;
}

if (i === 7) {
continue;
}
}
11 changes: 11 additions & 0 deletions test/translation/ts/continueNested.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
for (let i = 0; i < 5; i++) {
if (i % 2 === 0) {
continue;
}

for (let j = 0; j < 2; j++) {
if (j === 1) {
continue;
}
}
}
15 changes: 15 additions & 0 deletions test/translation/ts/continueNestedConcurrent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
for (let i = 0; i < 5; i++) {
if (i % 2 === 0) {
continue;
}

for (let j = 0; j < 2; j++) {
if (j === 1) {
continue;
}
}

if (i === 4) {
continue;
}
}
4 changes: 4 additions & 0 deletions test/translation/ts/do.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let e = 10
do {
e--;
} while (e > 0)
2 changes: 2 additions & 0 deletions test/translation/ts/for.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
for (let i = 1; i <= 100; i++) {
}
7 changes: 7 additions & 0 deletions test/translation/ts/forIn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
for (let i in {
a: 1,
b: 2,
c: 3,
d: 4
}) {
}
2 changes: 2 additions & 0 deletions test/translation/ts/forOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
for (let i of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) {
}
4 changes: 4 additions & 0 deletions test/translation/ts/while.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let d = 10;
while (d > 0) {
d--;
}
Loading