11import * as ts from "typescript" ;
22
33import { TSHelper as tsEx } from "./TSHelper" ;
4+ import { ForHelper } from "./ForHelper" ;
45
5- class TranspileError extends Error {
6+ export class TranspileError extends Error {
67 node : ts . Node ;
78 constructor ( message : string , node : ts . Node ) {
89 super ( message ) ;
@@ -109,91 +110,14 @@ export class LuaTranspiler {
109110 }
110111
111112 transpileFor ( node : ts . ForStatement ) : string {
112- // Goal: Populate three for components:
113- let start = "0" ;
114- let end = "0" ;
115- let step = "1" ;
116-
117- // Get variable
113+ // Get iterator variable
118114 const variable = ( < ts . VariableDeclarationList > node . initializer ) . declarations [ 0 ] ;
119115 const identifier = < ts . Identifier > variable . name ;
120116
121- // Get initial value
122- start = this . transpileExpression ( variable . initializer ) ;
123-
124- // Get ending value
125- if ( ts . isBinaryExpression ( node . condition ) ) {
126- if ( ts . isIdentifier ( node . condition . left ) ) {
127- // Account for lua 1 indexing
128- switch ( node . condition . operatorToken . kind ) {
129- case ts . SyntaxKind . LessThanEqualsToken :
130- case ts . SyntaxKind . GreaterThanEqualsToken :
131- end = this . transpileExpression ( node . condition . right ) ;
132- break ;
133- case ts . SyntaxKind . LessThanToken :
134- end = this . transpileExpression ( node . condition . right ) + "-1" ;
135- break ;
136- case ts . SyntaxKind . GreaterThanToken :
137- end = this . transpileExpression ( node . condition . right ) + "+1" ;
138- break ;
139- default :
140- throw new TranspileError ( "Unsupported for-loop condition operator: " + tsEx . enumName ( node . condition . operatorToken , ts . SyntaxKind ) , node ) ;
141- }
142- } else {
143- this . transpileExpression ( node . condition . left ) ;
144- // Account for lua 1 indexing
145- switch ( node . condition . operatorToken . kind ) {
146- case ts . SyntaxKind . LessThanEqualsToken :
147- case ts . SyntaxKind . GreaterThanEqualsToken :
148- end = this . transpileExpression ( node . condition . right ) ;
149- break ;
150- case ts . SyntaxKind . LessThanToken :
151- end = this . transpileExpression ( node . condition . right ) + "+1" ;
152- break ;
153- case ts . SyntaxKind . GreaterThanToken :
154- end = this . transpileExpression ( node . condition . right ) + "-1" ;
155- break ;
156- default :
157- throw new TranspileError ( "Unsupported for-loop condition operator: " + tsEx . enumName ( node . condition . operatorToken , ts . SyntaxKind ) , node ) ;
158- }
159- }
160- } else {
161- throw new TranspileError ( "Unsupported for-loop condition type: " + tsEx . enumName ( node . condition . kind , ts . SyntaxKind ) , node ) ;
162- }
163-
164- // Get increment step
165- switch ( node . incrementor . kind ) {
166- case ts . SyntaxKind . PostfixUnaryExpression :
167- case ts . SyntaxKind . PrefixUnaryExpression :
168- switch ( ( < ts . PostfixUnaryExpression | ts . PrefixUnaryExpression > node . incrementor ) . operator ) {
169- case ts . SyntaxKind . PlusPlusToken :
170- step = "1" ;
171- break ;
172- case ts . SyntaxKind . MinusMinusToken :
173- step = "-1" ;
174- break ;
175- default :
176- throw new TranspileError ( "Unsupported for-loop increment step: " + tsEx . enumName ( node . incrementor . kind , ts . SyntaxKind ) , node ) ;
177- }
178- break ;
179- case ts . SyntaxKind . BinaryExpression :
180- let value = ts . isIdentifier ( ( < ts . BinaryExpression > node . incrementor ) . left ) ?
181- this . transpileExpression ( ( < ts . BinaryExpression > node . incrementor ) . right ) :
182- this . transpileExpression ( ( < ts . BinaryExpression > node . incrementor ) . left ) ;
183- switch ( ( < ts . BinaryExpression > node . incrementor ) . operatorToken . kind ) {
184- case ts . SyntaxKind . PlusEqualsToken :
185- step = value ;
186- break ;
187- case ts . SyntaxKind . MinusEqualsToken :
188- step = "-" + value ;
189- break ;
190- default :
191- throw new TranspileError ( "Unsupported for-loop increment step: " + tsEx . enumName ( node . incrementor . kind , ts . SyntaxKind ) , node ) ;
192- }
193- break ;
194- default :
195- throw new TranspileError ( "Unsupported for-loop increment step: " + tsEx . enumName ( node . incrementor . kind , ts . SyntaxKind ) , node ) ;
196- }
117+ // Populate three components of lua numeric for loop:
118+ let start = this . transpileExpression ( variable . initializer ) ;
119+ let end = ForHelper . GetForEnd ( node . condition , this ) ;
120+ let step = ForHelper . GetForStep ( node . incrementor , this ) ;
197121
198122 // Add header
199123 let result = this . indent + `for ${ identifier . escapedText } =${ start } ,${ end } ,${ step } do\n` ;
@@ -290,8 +214,8 @@ export class LuaTranspiler {
290214 // Transpile operands
291215 const lhs = this . transpileExpression ( node . left , true ) ;
292216 const rhs = this . transpileExpression ( node . right , true ) ;
217+
293218 // Rewrite some non-existant binary operators
294-
295219 let result = "" ;
296220 switch ( node . operatorToken . kind ) {
297221 case ts . SyntaxKind . PlusEqualsToken :
@@ -316,6 +240,7 @@ export class LuaTranspiler {
316240 result = lhs + this . transpileOperator ( node . operatorToken ) + rhs ;
317241 }
318242
243+ // Optionally put brackets around result
319244 if ( brackets ) {
320245 return `(${ result } )` ;
321246 } else {
0 commit comments