@@ -13,15 +13,17 @@ export class TranspileError extends Error {
1313
1414export class LuaTranspiler {
1515 // Transpile a source file
16- static transpileSourceFile ( node : ts . SourceFile ) : string {
17- let transpiler = new LuaTranspiler ( ) ;
16+ static transpileSourceFile ( node : ts . SourceFile , checker : ts . TypeChecker ) : string {
17+ let transpiler = new LuaTranspiler ( checker ) ;
1818 return transpiler . transpileBlock ( node ) ;
1919 }
2020
2121 indent : string ;
22+ checker : ts . TypeChecker ;
2223
23- constructor ( ) {
24+ constructor ( checker : ts . TypeChecker ) {
2425 this . indent = "" ;
26+ this . checker = checker ;
2527 }
2628
2729 pushIndent ( ) : void {
@@ -138,8 +140,12 @@ export class LuaTranspiler {
138140 // Transpile expression
139141 const expression = this . transpileExpression ( node . expression ) ;
140142
143+ // Use ipairs for array types, pairs otherwise
144+ const isArray = this . checker . getTypeAtLocation ( node . expression ) . symbol . escapedName == "Array" ;
145+ const pairs = isArray ? "ipairs" : "pairs" ;
146+
141147 // Make header
142- let result = this . indent + `for _, ${ identifier . escapedText } in pairs(${ expression } ) do\n` ;
148+ let result = this . indent + `for _, ${ identifier . escapedText } in ${ pairs } (${ expression } ) do\n` ;
143149
144150 // For body
145151 this . pushIndent ( ) ;
@@ -157,8 +163,12 @@ export class LuaTranspiler {
157163 // Transpile expression
158164 const expression = this . transpileExpression ( node . expression ) ;
159165
166+ // Use ipairs for array types, pairs otherwise
167+ const isArray = this . checker . getTypeAtLocation ( node . expression ) . symbol . escapedName == "Array" ;
168+ const pairs = isArray ? "ipairs" : "pairs" ;
169+
160170 // Make header
161- let result = this . indent + `for ${ identifier . escapedText } , _ in pairs(${ expression } ) do\n` ;
171+ let result = this . indent + `for ${ identifier . escapedText } , _ in ${ pairs } (${ expression } ) do\n` ;
162172
163173 // For body
164174 this . pushIndent ( ) ;
@@ -329,7 +339,13 @@ export class LuaTranspiler {
329339 const element = this . transpileExpression ( node . expression ) ;
330340 const index = this . transpileExpression ( node . argumentExpression ) ;
331341
332- return `${ element } [${ index } ]` ;
342+ const isArray = this . checker . getTypeAtLocation ( node . expression ) . symbol . escapedName == "Array" ;
343+
344+ if ( isArray ) {
345+ return `${ element } [${ index } +1]` ;
346+ } else {
347+ return `${ element } [${ index } ]` ;
348+ }
333349 }
334350
335351 // Transpile a variable statement
@@ -503,6 +519,6 @@ export class LuaTranspiler {
503519 this . pushIndent ( ) ;
504520 result += this . transpileBlock ( node . body ) ;
505521 this . popIndent ( ) ;
506- return result + this . indent + "end\n " ;
522+ return result + this . indent + "end " ;
507523 }
508524}
0 commit comments