@@ -44,7 +44,11 @@ export class LuaTranspiler {
4444 case ts . SyntaxKind . ReturnStatement :
4545 return indent + this . transpileReturn ( < ts . ReturnStatement > node ) + "\n" ;
4646 case ts . SyntaxKind . ForStatement :
47- return this . transpileFor ( < ts . ForStatement > node , indent ) + "\n" ;
47+ return this . transpileFor ( < ts . ForStatement > node , indent ) ;
48+ case ts . SyntaxKind . ForOfStatement :
49+ return this . transpileForOf ( < ts . ForOfStatement > node , indent ) ;
50+ case ts . SyntaxKind . ForInStatement :
51+ return this . transpileForIn ( < ts . ForInStatement > node , indent ) ;
4852 case ts . SyntaxKind . EndOfFileToken :
4953 return "" ;
5054 default :
@@ -142,14 +146,47 @@ export class LuaTranspiler {
142146 let result = indent + `for ${ identifier . escapedText } =${ start } ,${ end } ,${ step } do\n` ;
143147
144148 // Add body
145- const block = tsEx . getFirstChildOfType < ts . Block > ( node , ts . isBlock ) ;
146- result += this . transpileBlock ( block , indent + " " ) ;
149+ result += this . transpileBlock ( node . statement , indent + " " ) ;
150+
151+ return result + indent + "end\n" ;
152+ }
153+
154+ static transpileForOf ( node : ts . ForOfStatement , indent : string ) : string {
155+ // Get variable identifier
156+ const variable = < ts . VariableDeclaration > ( < ts . VariableDeclarationList > node . initializer ) . declarations [ 0 ] ;
157+ const identifier = < ts . Identifier > variable . name ;
158+
159+ // Transpile expression
160+ const expression = this . transpileExpression ( node . expression ) ;
161+
162+ // Make header
163+ let result = indent + `for _, ${ identifier . escapedText } in pairs(${ expression } ) do\n` ;
164+
165+ // For body
166+ result += this . transpileBlock ( node . statement , indent + " " ) ;
167+
168+ return result + indent + "end\n" ;
169+ }
170+
171+ static transpileForIn ( node : ts . ForInStatement , indent : string ) : string {
172+ // Get variable identifier
173+ const variable = < ts . VariableDeclaration > ( < ts . VariableDeclarationList > node . initializer ) . declarations [ 0 ] ;
174+ const identifier = < ts . Identifier > variable . name ;
175+
176+ // Transpile expression
177+ const expression = this . transpileExpression ( node . expression ) ;
178+
179+ // Make header
180+ let result = indent + `for ${ identifier . escapedText } , _ in pairs(${ expression } ) do\n` ;
181+
182+ // For body
183+ result += this . transpileBlock ( node . statement , indent + " " ) ;
147184
148185 return result + indent + "end\n" ;
149186 }
150187
151188 static transpileReturn ( node : ts . ReturnStatement ) : string {
152- return "return " + this . transpileExpression ( tsEx . getChildren ( node ) [ 0 ] ) ;
189+ return "return " + this . transpileExpression ( node . expression ) ;
153190 }
154191
155192 static transpileExpression ( node : ts . Node ) : string {
0 commit comments