@@ -3123,11 +3123,7 @@ export class LuaTransformer {
31233123 const ownerType = this . checker . getTypeAtLocation ( node . expression . expression ) ;
31243124
31253125 if ( ownerType . symbol && ownerType . symbol . escapedName === "Math" ) {
3126- return tstl . createCallExpression (
3127- this . transformMathExpression ( node . expression . name ) ,
3128- this . transformArguments ( node . arguments ) ,
3129- node
3130- ) ;
3126+ return this . transformMathCallExpression ( node ) ;
31313127 }
31323128
31333129 if ( ownerType . symbol && ownerType . symbol . escapedName === "StringConstructor" ) {
@@ -3320,34 +3316,99 @@ export class LuaTransformer {
33203316 }
33213317
33223318 // Transpile a Math._ property
3323- public transformMathExpression ( identifier : ts . Identifier ) : tstl . TableIndexExpression {
3324- const translation = {
3325- PI : "pi" ,
3326- abs : "abs" ,
3327- acos : "acos" ,
3328- asin : "asin" ,
3329- atan : "atan" ,
3330- ceil : "ceil" ,
3331- cos : "cos" ,
3332- exp : "exp" ,
3333- floor : "floor" ,
3334- log : "log" ,
3335- max : "max" ,
3336- min : "min" ,
3337- pow : "pow" ,
3338- random : "random" ,
3339- round : "round" ,
3340- sin : "sin" ,
3341- sqrt : "sqrt" ,
3342- tan : "tan" ,
3343- } ;
3319+ public transformMathExpression ( identifier : ts . Identifier ) : tstl . Expression {
3320+ const name = identifier . escapedText as string ;
3321+ switch ( name ) {
3322+ case "PI" :
3323+ const property = tstl . createStringLiteral ( "pi" ) ;
3324+ const math = tstl . createIdentifier ( "math" ) ;
3325+ return tstl . createTableIndexExpression ( math , property , identifier ) ;
3326+
3327+ case "E" :
3328+ case "LN10" :
3329+ case "LN2" :
3330+ case "LOG10E" :
3331+ case "LOG2E" :
3332+ case "SQRT1_2" :
3333+ case "SQRT2" :
3334+ return tstl . createNumericLiteral ( Math [ name ] , identifier ) ;
33443335
3345- if ( translation [ identifier . escapedText as string ] ) {
3346- const property = tstl . createStringLiteral ( translation [ identifier . escapedText as string ] ) ;
3347- const math = tstl . createIdentifier ( "math" ) ;
3348- return tstl . createTableIndexExpression ( math , property , identifier ) ;
3349- } else {
3350- throw TSTLErrors . UnsupportedProperty ( "math" , identifier . escapedText as string , identifier ) ;
3336+ default :
3337+ throw TSTLErrors . UnsupportedProperty ( "math" , name , identifier ) ;
3338+ }
3339+ }
3340+
3341+ // Transpile a Math._ property
3342+ public transformMathCallExpression ( node : ts . CallExpression ) : tstl . Expression {
3343+ const expression = node . expression as ts . PropertyAccessExpression ;
3344+ const params = this . transformArguments ( node . arguments ) ;
3345+ const expressionName = expression . name . escapedText as string ;
3346+ switch ( expressionName ) {
3347+ // math.tan(x / y)
3348+ case "atan2" :
3349+ {
3350+ const math = tstl . createIdentifier ( "math" ) ;
3351+ const atan = tstl . createStringLiteral ( "atan" ) ;
3352+ const div = tstl . createBinaryExpression ( params [ 0 ] , params [ 1 ] , tstl . SyntaxKind . DivisionOperator ) ;
3353+ return tstl . createCallExpression ( tstl . createTableIndexExpression ( math , atan ) , [ div ] , node ) ;
3354+ }
3355+
3356+ // (math.log(x) / Math.LNe)
3357+ case "log10" :
3358+ case "log2" :
3359+ {
3360+ const math = tstl . createIdentifier ( "math" ) ;
3361+ const log1 = tstl . createTableIndexExpression ( math , tstl . createStringLiteral ( "log" ) ) ;
3362+ const logCall1 = tstl . createCallExpression ( log1 , params ) ;
3363+ const e = tstl . createNumericLiteral ( expressionName === "log10" ? Math . LN10 : Math . LN2 ) ;
3364+ const div = tstl . createBinaryExpression ( logCall1 , e , tstl . SyntaxKind . DivisionOperator ) ;
3365+ return tstl . createParenthesizedExpression ( div , node ) ;
3366+ }
3367+
3368+ // math.log(1 + x)
3369+ case "log1p" :
3370+ {
3371+ const math = tstl . createIdentifier ( "math" ) ;
3372+ const log = tstl . createStringLiteral ( "log" ) ;
3373+ const one = tstl . createNumericLiteral ( 1 ) ;
3374+ const add = tstl . createBinaryExpression ( one , params [ 0 ] , tstl . SyntaxKind . AdditionOperator ) ;
3375+ return tstl . createCallExpression ( tstl . createTableIndexExpression ( math , log ) , [ add ] , node ) ;
3376+ }
3377+
3378+ // math.floor(x + 0.5)
3379+ case "round" :
3380+ {
3381+ const math = tstl . createIdentifier ( "math" ) ;
3382+ const floor = tstl . createStringLiteral ( "floor" ) ;
3383+ const half = tstl . createNumericLiteral ( 0.5 ) ;
3384+ const add = tstl . createBinaryExpression ( params [ 0 ] , half , tstl . SyntaxKind . AdditionOperator ) ;
3385+ return tstl . createCallExpression ( tstl . createTableIndexExpression ( math , floor ) , [ add ] , node ) ;
3386+ }
3387+
3388+ case "abs" :
3389+ case "acos" :
3390+ case "asin" :
3391+ case "atan" :
3392+ case "ceil" :
3393+ case "cos" :
3394+ case "exp" :
3395+ case "floor" :
3396+ case "log" :
3397+ case "max" :
3398+ case "min" :
3399+ case "pow" :
3400+ case "random" :
3401+ case "sin" :
3402+ case "sqrt" :
3403+ case "tan" :
3404+ {
3405+ const math = tstl . createIdentifier ( "math" ) ;
3406+ const method = tstl . createStringLiteral ( expressionName ) ;
3407+ return tstl . createCallExpression ( tstl . createTableIndexExpression ( math , method ) , params , node ) ;
3408+ }
3409+
3410+ default :
3411+ throw TSTLErrors . UnsupportedProperty ( "math" , name , expression ) ;
33513412 }
33523413 }
33533414
0 commit comments