@@ -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" ;
0 commit comments