@@ -132,7 +132,7 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
132
132
if ( xy . Count == 2 && xy . All ( o => o > 0.0 ) )
133
133
{
134
134
powNode . Params . Remove ( ) ;
135
- powNode . ReplaceWith ( new GenericSyntaxNode ( new FloatToken ( $ " { Math . Pow ( xy [ 0 ] , xy [ 1 ] ) : F } " ) . Simplify ( ) ) ) ;
135
+ powNode . ReplaceWith ( new GenericSyntaxNode ( FloatToken . From ( Math . Pow ( xy [ 0 ] , xy [ 1 ] ) , MaxDp ) ) ) ;
136
136
didChange = true ;
137
137
}
138
138
}
@@ -147,7 +147,64 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
147
147
if ( x . Count == 1 )
148
148
{
149
149
absNode . Params . Remove ( ) ;
150
- absNode . ReplaceWith ( new GenericSyntaxNode ( new FloatToken ( $ "{ Math . Abs ( x [ 0 ] ) : F} ") . Simplify ( ) ) ) ;
150
+ absNode . ReplaceWith ( new GenericSyntaxNode ( FloatToken . From ( Math . Abs ( x [ 0 ] ) , MaxDp ) ) ) ;
151
+ didChange = true ;
152
+ }
153
+ }
154
+
155
+ // vecN(...) + <float>
156
+ foreach ( var vectorNode in rootNode . TheTree
157
+ . OfType < GenericSyntaxNode > ( )
158
+ . Where ( o => o . Token is TypeToken t && t . Content ? . StartsWith ( "vec" ) == true ) )
159
+ {
160
+ // Find the brackets.
161
+ if ( vectorNode . Next is not RoundBracketSyntaxNode brackets )
162
+ continue ;
163
+
164
+ // Brackets must be filled with floats.
165
+ if ( ! brackets . IsSimpleCsv ( ) )
166
+ continue ;
167
+
168
+ // Find the math symbol.
169
+ var symbolNode = brackets . Next ;
170
+ if ( symbolNode ? . Token is not SymbolOperatorToken symbol )
171
+ continue ;
172
+ switch ( symbol . GetMathSymbolType ( ) )
173
+ {
174
+ case TokenExtensions . MathSymbolType . AddSubtract :
175
+ case TokenExtensions . MathSymbolType . MultiplyDivide :
176
+ break ; // Supported.
177
+
178
+ default :
179
+ continue ; // Not supported - Continue search.
180
+ }
181
+
182
+ // Find the float number.
183
+ var numberNode = symbolNode . Next ;
184
+ if ( numberNode ? . Token is not FloatToken )
185
+ continue ;
186
+
187
+ // Number must not be used in a following math operation.
188
+ if ( numberNode . Next ? . Token is SymbolOperatorToken nextMath && nextMath . GetMathSymbolType ( ) == TokenExtensions . MathSymbolType . MultiplyDivide )
189
+ continue ;
190
+
191
+ // Perform math on each bracketed value.
192
+ var newCsv =
193
+ brackets
194
+ . GetCsv ( )
195
+ . Select ( o => DoNodeMath ( o . Single ( ) , symbolNode , numberNode ) )
196
+ . Select ( o => new GenericSyntaxNode ( FloatToken . From ( o , MaxDp ) ) ) ;
197
+
198
+ // Replace bracket content and sum (if it shrinks the code).
199
+ var newBrackets = new RoundBracketSyntaxNode ( newCsv . ToCsv ( ) ) ;
200
+ var oldSize = brackets . ToCode ( ) . Length + 1 + numberNode . ToCode ( ) . Length ;
201
+ var newSize = newBrackets . ToCode ( ) . Length ;
202
+ if ( newSize <= oldSize )
203
+ {
204
+ brackets . ReplaceWith ( newBrackets ) ;
205
+ symbolNode . Remove ( ) ;
206
+ numberNode . Remove ( ) ;
207
+
151
208
didChange = true ;
152
209
}
153
210
}
@@ -179,66 +236,17 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
179
236
continue ;
180
237
}
181
238
182
- var a = double . Parse ( numNodeA . Token . Content ) ;
183
- var b = double . Parse ( numNodeB . Token . Content ) ;
184
-
185
- var symbol = symbolNode . Token . Content [ 0 ] ;
186
-
187
- // Invert * or / if preceded by a /.
188
- // E.g. 1.2 / 2.3 * 4.5 = 1.2 / (2.3 / 4.5)
189
- // = 1.2 / 0.51111
190
- // = 2.3478
191
- if ( numNodeA . Previous ? . Token ? . GetMathSymbolType ( ) == TokenExtensions . MathSymbolType . MultiplyDivide &&
192
- numNodeA . Previous . HasNodeContent ( "/" ) )
193
- {
194
- symbol = symbol == '*' ? '/' : '*' ;
195
- }
196
-
197
- // Invert + or - if preceded by a -.
198
- // E.g. -3.0 + 0.1 = - (3.0 - 0.1)
199
- // = - (2.9)
200
- // = -2.9
201
- if ( numNodeA . Previous . HasNodeContent ( "-" ) &&
202
- symbolNode . Token . GetMathSymbolType ( ) == TokenExtensions . MathSymbolType . AddSubtract )
203
- {
204
- symbol = symbol == '+' ? '-' : '+' ;
205
- }
206
-
207
- double c ;
208
- switch ( symbol )
209
- {
210
- case '+' :
211
- c = a + b ;
212
- break ;
213
- case '-' :
214
- c = a - b ;
215
- break ;
216
- case '*' :
217
- c = a * b ;
218
- break ;
219
- case '/' :
220
- c = a / b ;
221
- if ( double . IsInfinity ( c ) )
222
- c = 0.0 ;
223
- break ;
224
- default :
225
- throw new InvalidOperationException ( $ "Unrecognized math operation '{ symbol } '.") ;
226
- }
239
+ var c = DoNodeMath ( numNodeA , symbolNode , numNodeB ) ;
227
240
228
241
var isFloatResult = numNodeA . Token is FloatToken || numNodeB . Token is FloatToken ;
229
242
numNodeB . Remove ( ) ;
230
243
symbolNode . Remove ( ) ;
231
244
232
245
SyntaxNode newNode ;
233
246
if ( isFloatResult )
234
- {
235
- var numberToken = Math . Abs ( c ) < 0.0001 && Math . Abs ( c ) > 0.0 ? new FloatToken ( c . ToString ( $ ".#{ new string ( '#' , MaxDp - 1 ) } e0") ) : new FloatToken ( c . ToString ( $ "F{ MaxDp } ") ) ;
236
- newNode = numNodeA . ReplaceWith ( new GenericSyntaxNode ( numberToken . Simplify ( ) ) ) ;
237
- }
247
+ newNode = numNodeA . ReplaceWith ( new GenericSyntaxNode ( FloatToken . From ( c , MaxDp ) ) ) ;
238
248
else
239
- {
240
249
newNode = numNodeA . ReplaceWith ( new GenericSyntaxNode ( new IntToken ( ( int ) c ) ) ) ;
241
- }
242
250
243
251
// If new node is the sole child of a group, promote it.
244
252
if ( newNode . IsOnlyChild ( ) && newNode . Parent . GetType ( ) == typeof ( GroupSyntaxNode ) )
@@ -255,5 +263,50 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
255
263
256
264
return repeatSimplifications ;
257
265
}
266
+
267
+ private static double DoNodeMath ( SyntaxNode numNodeA , SyntaxNode symbolNode , SyntaxNode numNodeB )
268
+ {
269
+ var a = double . Parse ( numNodeA . Token . Content ) ;
270
+ var b = double . Parse ( numNodeB . Token . Content ) ;
271
+
272
+ var symbol = symbolNode . Token . Content [ 0 ] ;
273
+
274
+ // Invert * or / if preceded by a /.
275
+ // E.g. 1.2 / 2.3 * 4.5 = 1.2 / (2.3 / 4.5)
276
+ // = 1.2 / 0.51111
277
+ // = 2.3478
278
+ if ( numNodeA . Previous ? . Token ? . GetMathSymbolType ( ) == TokenExtensions . MathSymbolType . MultiplyDivide &&
279
+ numNodeA . Previous . HasNodeContent ( "/" ) )
280
+ {
281
+ symbol = symbol == '*' ? '/' : '*' ;
282
+ }
283
+
284
+ // Invert + or - if preceded by a -.
285
+ // E.g. -3.0 + 0.1 = - (3.0 - 0.1)
286
+ // = - (2.9)
287
+ // = -2.9
288
+ if ( numNodeA . Previous . HasNodeContent ( "-" ) &&
289
+ symbolNode . Token . GetMathSymbolType ( ) == TokenExtensions . MathSymbolType . AddSubtract )
290
+ {
291
+ symbol = symbol == '+' ? '-' : '+' ;
292
+ }
293
+
294
+ switch ( symbol )
295
+ {
296
+ case '+' :
297
+ return a + b ;
298
+ case '-' :
299
+ return a - b ;
300
+ case '*' :
301
+ return a * b ;
302
+ case '/' :
303
+ var c = a / b ;
304
+ if ( double . IsInfinity ( c ) )
305
+ c = 0.0 ;
306
+ return c ;
307
+ default :
308
+ throw new InvalidOperationException ( $ "Unrecognized math operation '{ symbol } '.") ;
309
+ }
310
+ }
258
311
}
259
312
}
0 commit comments