11import * as ts from "typescript" ;
2- import * as tstl from "../../LuaAST" ;
2+ import * as lua from "../../LuaAST" ;
33import { TransformationContext } from "../context" ;
44import { PropertyCallExpression , transformArguments } from "../transformers/call" ;
55import { UnsupportedProperty } from "../utils/errors" ;
66
7- export function transformMathProperty ( node : ts . PropertyAccessExpression ) : tstl . Expression {
7+ export function transformMathProperty ( node : ts . PropertyAccessExpression ) : lua . Expression {
88 const name = node . name . text ;
99 switch ( name ) {
1010 case "PI" :
11- const property = tstl . createStringLiteral ( "pi" , node . name ) ;
12- const math = tstl . createIdentifier ( "math" , node . expression ) ;
13- return tstl . createTableIndexExpression ( math , property , node ) ;
11+ const property = lua . createStringLiteral ( "pi" , node . name ) ;
12+ const math = lua . createIdentifier ( "math" , node . expression ) ;
13+ return lua . createTableIndexExpression ( math , property , node ) ;
1414
1515 case "E" :
1616 case "LN10" :
@@ -19,14 +19,14 @@ export function transformMathProperty(node: ts.PropertyAccessExpression): tstl.E
1919 case "LOG2E" :
2020 case "SQRT1_2" :
2121 case "SQRT2" :
22- return tstl . createNumericLiteral ( Math [ name ] , node ) ;
22+ return lua . createNumericLiteral ( Math [ name ] , node ) ;
2323
2424 default :
2525 throw UnsupportedProperty ( "Math" , name , node ) ;
2626 }
2727}
2828
29- export function transformMathCall ( context : TransformationContext , node : PropertyCallExpression ) : tstl . Expression {
29+ export function transformMathCall ( context : TransformationContext , node : PropertyCallExpression ) : lua . Expression {
3030 const expression = node . expression ;
3131 const signature = context . checker . getResolvedSignature ( node ) ;
3232 const params = transformArguments ( context , node . arguments , signature ) ;
@@ -35,39 +35,39 @@ export function transformMathCall(context: TransformationContext, node: Property
3535 switch ( expressionName ) {
3636 // math.tan(x / y)
3737 case "atan2" : {
38- const math = tstl . createIdentifier ( "math" ) ;
39- const atan = tstl . createStringLiteral ( "atan" ) ;
40- const div = tstl . createBinaryExpression ( params [ 0 ] , params [ 1 ] , tstl . SyntaxKind . DivisionOperator ) ;
41- return tstl . createCallExpression ( tstl . createTableIndexExpression ( math , atan ) , [ div ] , node ) ;
38+ const math = lua . createIdentifier ( "math" ) ;
39+ const atan = lua . createStringLiteral ( "atan" ) ;
40+ const div = lua . createBinaryExpression ( params [ 0 ] , params [ 1 ] , lua . SyntaxKind . DivisionOperator ) ;
41+ return lua . createCallExpression ( lua . createTableIndexExpression ( math , atan ) , [ div ] , node ) ;
4242 }
4343
4444 // (math.log(x) / Math.LNe)
4545 case "log10" :
4646 case "log2" : {
47- const math = tstl . createIdentifier ( "math" ) ;
48- const log1 = tstl . createTableIndexExpression ( math , tstl . createStringLiteral ( "log" ) ) ;
49- const logCall1 = tstl . createCallExpression ( log1 , params ) ;
50- const e = tstl . createNumericLiteral ( expressionName === "log10" ? Math . LN10 : Math . LN2 ) ;
51- const div = tstl . createBinaryExpression ( logCall1 , e , tstl . SyntaxKind . DivisionOperator ) ;
52- return tstl . createParenthesizedExpression ( div , node ) ;
47+ const math = lua . createIdentifier ( "math" ) ;
48+ const log1 = lua . createTableIndexExpression ( math , lua . createStringLiteral ( "log" ) ) ;
49+ const logCall1 = lua . createCallExpression ( log1 , params ) ;
50+ const e = lua . createNumericLiteral ( expressionName === "log10" ? Math . LN10 : Math . LN2 ) ;
51+ const div = lua . createBinaryExpression ( logCall1 , e , lua . SyntaxKind . DivisionOperator ) ;
52+ return lua . createParenthesizedExpression ( div , node ) ;
5353 }
5454
5555 // math.log(1 + x)
5656 case "log1p" : {
57- const math = tstl . createIdentifier ( "math" ) ;
58- const log = tstl . createStringLiteral ( "log" ) ;
59- const one = tstl . createNumericLiteral ( 1 ) ;
60- const add = tstl . createBinaryExpression ( one , params [ 0 ] , tstl . SyntaxKind . AdditionOperator ) ;
61- return tstl . createCallExpression ( tstl . createTableIndexExpression ( math , log ) , [ add ] , node ) ;
57+ const math = lua . createIdentifier ( "math" ) ;
58+ const log = lua . createStringLiteral ( "log" ) ;
59+ const one = lua . createNumericLiteral ( 1 ) ;
60+ const add = lua . createBinaryExpression ( one , params [ 0 ] , lua . SyntaxKind . AdditionOperator ) ;
61+ return lua . createCallExpression ( lua . createTableIndexExpression ( math , log ) , [ add ] , node ) ;
6262 }
6363
6464 // math.floor(x + 0.5)
6565 case "round" : {
66- const math = tstl . createIdentifier ( "math" ) ;
67- const floor = tstl . createStringLiteral ( "floor" ) ;
68- const half = tstl . createNumericLiteral ( 0.5 ) ;
69- const add = tstl . createBinaryExpression ( params [ 0 ] , half , tstl . SyntaxKind . AdditionOperator ) ;
70- return tstl . createCallExpression ( tstl . createTableIndexExpression ( math , floor ) , [ add ] , node ) ;
66+ const math = lua . createIdentifier ( "math" ) ;
67+ const floor = lua . createStringLiteral ( "floor" ) ;
68+ const half = lua . createNumericLiteral ( 0.5 ) ;
69+ const add = lua . createBinaryExpression ( params [ 0 ] , half , lua . SyntaxKind . AdditionOperator ) ;
70+ return lua . createCallExpression ( lua . createTableIndexExpression ( math , floor ) , [ add ] , node ) ;
7171 }
7272
7373 case "abs" :
@@ -86,9 +86,9 @@ export function transformMathCall(context: TransformationContext, node: Property
8686 case "sin" :
8787 case "sqrt" :
8888 case "tan" : {
89- const math = tstl . createIdentifier ( "math" ) ;
90- const method = tstl . createStringLiteral ( expressionName ) ;
91- return tstl . createCallExpression ( tstl . createTableIndexExpression ( math , method ) , params , node ) ;
89+ const math = lua . createIdentifier ( "math" ) ;
90+ const method = lua . createStringLiteral ( expressionName ) ;
91+ return lua . createCallExpression ( lua . createTableIndexExpression ( math , method ) , params , node ) ;
9292 }
9393
9494 default :
0 commit comments