1010// -----------------------------------------------------------------------
1111
1212using System ;
13+ using System . Collections . Generic ;
1314using System . Linq ;
1415using Shrinker . Lexer ;
1516using Shrinker . Parser . SyntaxNodes ;
@@ -123,8 +124,8 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
123124 }
124125
125126 // pow(1.1, 2.2) => <the result>
126- foreach ( var powNode in rootNode . TheTree
127- . OfType < GlslFunctionCallSyntaxNode > ( )
127+ var functionCalls = rootNode . TheTree . OfType < GlslFunctionCallSyntaxNode > ( ) . ToList ( ) ;
128+ foreach ( var powNode in functionCalls
128129 . Where ( o => o . Name == "pow" && o . Params . IsSimpleCsv ( ) )
129130 . ToList ( ) )
130131 {
@@ -137,32 +138,31 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
137138 }
138139 }
139140
140- // abs(-1.1) => <the result>
141- foreach ( var absNode in rootNode . TheTree
142- . OfType < GlslFunctionCallSyntaxNode > ( )
143- . Where ( o => o . Name == "abs" && o . Params . IsSimpleCsv ( ) )
144- . ToList ( ) )
141+ // Constant math/trig functions => <the result>
142+ var mathOp = new List < Tuple < string , Func < double , double > > >
145143 {
146- var x = absNode . Params . Children . Where ( o => o . Token is FloatToken ) . Select ( o => ( ( FloatToken ) o . Token ) . Number ) . ToList ( ) ;
147- if ( x . Count == 1 )
148- {
149- absNode . Params . Remove ( ) ;
150- absNode . ReplaceWith ( new GenericSyntaxNode ( FloatToken . From ( Math . Abs ( x [ 0 ] ) , MaxDp ) ) ) ;
151- didChange = true ;
152- }
153- }
154-
155- // sqrt(-1.1) => <the result>
156- foreach ( var sqrtNode in rootNode . TheTree
157- . OfType < GlslFunctionCallSyntaxNode > ( )
158- . Where ( o => o . Name == "sqrt" && o . Params . IsSimpleCsv ( ) )
144+ new Tuple < string , Func < double , double > > ( "abs" , Math . Abs ) ,
145+ new Tuple < string , Func < double , double > > ( "sqrt" , d => Math . Sqrt ( Math . Abs ( d ) ) ) ,
146+ new Tuple < string , Func < double , double > > ( "sin" , Math . Sin ) ,
147+ new Tuple < string , Func < double , double > > ( "cos" , Math . Cos ) ,
148+ new Tuple < string , Func < double , double > > ( "tan" , Math . Tan ) ,
149+ new Tuple < string , Func < double , double > > ( "asin" , Math . Asin ) ,
150+ new Tuple < string , Func < double , double > > ( "acos" , Math . Acos ) ,
151+ new Tuple < string , Func < double , double > > ( "atan" , Math . Atan ) ,
152+ new Tuple < string , Func < double , double > > ( "radians" , x => x / 180.0 * Math . PI ) ,
153+ new Tuple < string , Func < double , double > > ( "degrees" , x => x / Math . PI * 180.0 )
154+ } ;
155+
156+ foreach ( var mathNode in functionCalls
157+ . Where ( o => mathOp . Select ( op => op . Item1 ) . Contains ( o . Name ) && o . Params . IsSimpleCsv ( ) )
159158 . ToList ( ) )
160159 {
161- var x = sqrtNode . Params . Children . Where ( o => o . Token is FloatToken ) . Select ( o => ( ( FloatToken ) o . Token ) . Number ) . ToList ( ) ;
160+ var x = mathNode . Params . Children . Where ( o => o . Token is FloatToken ) . Select ( o => ( ( FloatToken ) o . Token ) . Number ) . ToList ( ) ;
162161 if ( x . Count == 1 )
163162 {
164- sqrtNode . Params . Remove ( ) ;
165- sqrtNode . ReplaceWith ( new GenericSyntaxNode ( FloatToken . From ( Math . Sqrt ( Math . Abs ( x [ 0 ] ) ) , MaxDp ) ) ) ;
163+ mathNode . Params . Remove ( ) ;
164+ var mathFunc = mathOp . Single ( o => o . Item1 == mathNode . Name ) . Item2 ;
165+ mathNode . ReplaceWith ( new GenericSyntaxNode ( FloatToken . From ( mathFunc ( x [ 0 ] ) , MaxDp ) ) ) ;
166166 didChange = true ;
167167 }
168168 }
0 commit comments