Skip to content

Commit 7f39d57

Browse files
InfiniteRainlolleko
authored andcommitted
add support for continue statement (#112)
* add support for continue statement * make us of lua goto; decrease code reusage; add tests * implement requested changes * add continue translation tests; minor requested changes * fix tupleArrayUses translation test
1 parent 0cf96b0 commit 7f39d57

22 files changed

+343
-53
lines changed

src/Transpiler.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class LuaTranspiler {
6161
public importCount: number;
6262
public isModule: boolean;
6363
public sourceFile: ts.SourceFile;
64+
public loopStack: number[];
6465

6566
constructor(checker: ts.TypeChecker, options: ts.CompilerOptions, sourceFile: ts.SourceFile) {
6667
this.indent = "";
@@ -72,6 +73,7 @@ export class LuaTranspiler {
7273
this.importCount = 0;
7374
this.sourceFile = sourceFile;
7475
this.isModule = tsHelper.isFileModule(sourceFile);
76+
this.loopStack = [];
7577
}
7678

7779
public pushIndent(): void {
@@ -194,8 +196,7 @@ export class LuaTranspiler {
194196
case ts.SyntaxKind.ThrowStatement:
195197
return this.transpileThrow(node as ts.ThrowStatement);
196198
case ts.SyntaxKind.ContinueStatement:
197-
// Disallow continue
198-
throw new TranspileError("Continue is not supported in Lua", node);
199+
return this.transpileContinue();
199200
case ts.SyntaxKind.TypeAliasDeclaration:
200201
case ts.SyntaxKind.InterfaceDeclaration:
201202
case ts.SyntaxKind.EndOfFileToken:
@@ -317,6 +318,10 @@ export class LuaTranspiler {
317318
}
318319
}
319320

321+
public transpileContinue(): string {
322+
return this.indent + `goto __continue${this.loopStack[this.loopStack.length - 1]}\n`;
323+
}
324+
320325
public transpileIf(node: ts.IfStatement): string {
321326
const condition = this.transpileExpression(node.expression);
322327

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

343+
public transpileLoopBody(
344+
node: ts.WhileStatement
345+
| ts.DoStatement
346+
| ts.ForStatement
347+
| ts.ForOfStatement
348+
| ts.ForInStatement
349+
): string {
350+
this.loopStack.push(this.genVarCounter);
351+
this.genVarCounter++;
352+
let result = this.indent + "do\n";
353+
this.pushIndent();
354+
result += this.transpileStatement(node.statement);
355+
this.popIndent();
356+
result += this.indent + "end\n";
357+
result += this.indent + `::__continue${this.loopStack.pop()}::\n`;
358+
return result;
359+
}
360+
338361
public transpileWhile(node: ts.WhileStatement): string {
339362
const condition = this.transpileExpression(node.expression);
340363

341364
let result = this.indent + `while ${condition} do\n`;
342365
this.pushIndent();
343-
result += this.transpileStatement(node.statement);
366+
result += this.transpileLoopBody(node);
344367
this.popIndent();
345368
return result + this.indent + "end\n";
346369
}
@@ -349,7 +372,7 @@ export class LuaTranspiler {
349372
let result = this.indent + `repeat\n`;
350373

351374
this.pushIndent();
352-
result += this.transpileStatement(node.statement);
375+
result += this.transpileLoopBody(node);
353376
this.popIndent();
354377

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

369392
// Add body
370393
this.pushIndent();
371-
result += this.transpileStatement(node.statement);
394+
result += this.transpileLoopBody(node);
372395
result += this.indent + this.transpileExpression(node.incrementor) + "\n";
373396
this.popIndent();
374397

@@ -394,7 +417,7 @@ export class LuaTranspiler {
394417

395418
// For body
396419
this.pushIndent();
397-
result += this.transpileStatement(node.statement);
420+
result += this.transpileLoopBody(node);
398421
this.popIndent();
399422

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

418441
// For body
419442
this.pushIndent();
420-
result += this.transpileStatement(node.statement);
443+
result += this.transpileLoopBody(node);
421444
this.popIndent();
422445

423446
return result + this.indent + "end\n";

test/translation/lua/continue.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local i = 0
2+
while(i<10) do
3+
do
4+
if i<5 then
5+
goto __continue0
6+
end
7+
end
8+
::__continue0::
9+
i=i+1
10+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local i = 0
2+
while(i<10) do
3+
do
4+
if i<5 then
5+
goto __continue0
6+
end
7+
if i==7 then
8+
goto __continue0
9+
end
10+
end
11+
::__continue0::
12+
i=i+1
13+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local i = 0
2+
while(i<5) do
3+
do
4+
if (i%2)==0 then
5+
goto __continue0
6+
end
7+
local j = 0
8+
while(j<2) do
9+
do
10+
if j==1 then
11+
goto __continue1
12+
end
13+
end
14+
::__continue1::
15+
j=j+1
16+
end
17+
end
18+
::__continue0::
19+
i=i+1
20+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
local i = 0
2+
while(i<5) do
3+
do
4+
if (i%2)==0 then
5+
goto __continue0
6+
end
7+
local j = 0
8+
while(j<2) do
9+
do
10+
if j==1 then
11+
goto __continue1
12+
end
13+
end
14+
::__continue1::
15+
j=j+1
16+
end
17+
if i==4 then
18+
goto __continue0
19+
end
20+
end
21+
::__continue0::
22+
i=i+1
23+
end

test/translation/lua/do.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local e = 10
2+
3+
repeat
4+
do
5+
e=e-1
6+
end
7+
::__continue0::
8+
until not (e>0)

test/translation/lua/for.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local i = 1
2+
while(i<=100) do
3+
do
4+
end
5+
::__continue0::
6+
i=i+1
7+
end

test/translation/lua/forIn.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
for i, _ in pairs({a = 1,b = 2,c = 3,d = 4}) do
2+
do
3+
end
4+
::__continue0::
5+
end

test/translation/lua/forOf.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
for _, i in ipairs({1,2,3,4,5,6,7,8,9,10}) do
2+
do
3+
end
4+
::__continue0::
5+
end

test/translation/lua/tupleArrayUses.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
for _, value in ipairs(tuple) do
2+
do
3+
end
4+
::__continue0::
25
end
36
TS_forEach(tuple, function(v)
47
end

0 commit comments

Comments
 (0)