@@ -191,10 +191,7 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
191
191
var numberNode = symbolNode . Next ;
192
192
if ( numberNode ? . Token is FloatToken )
193
193
{
194
- // Number must not be used in a following math operation of a different category.
195
- if ( numberNode . Next ? . Token is not SymbolOperatorToken nextMath ||
196
- nextMath . GetMathSymbolType ( ) == symbol . GetMathSymbolType ( ) ||
197
- nextMath . GetMathSymbolType ( ) == TokenExtensions . MathSymbolType . AddSubtract )
194
+ if ( IsSafeToPerformMath ( vectorNode , symbol , numberNode ) )
198
195
{
199
196
// Perform math on each bracketed value.
200
197
var newCsv =
@@ -231,8 +228,7 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
231
228
var numberNode = symbolNode . Previous ;
232
229
if ( numberNode ? . Token is FloatToken )
233
230
{
234
- // Number must not be used in a preceding math operation.
235
- if ( numberNode . Previous ? . Token is not SymbolOperatorToken nextMath || nextMath . GetMathSymbolType ( ) != TokenExtensions . MathSymbolType . MultiplyDivide )
231
+ if ( IsSafeToPerformMath ( numberNode , symbol , vectorNode ) )
236
232
{
237
233
// Perform math on each bracketed value.
238
234
var newCsv =
@@ -270,11 +266,7 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
270
266
if ( rhsVectorNode ? . Token is TypeToken t && t . IsVector ( ) )
271
267
{
272
268
var rhsVectorBrackets = rhsVectorNode . Next as RoundBracketSyntaxNode ;
273
-
274
- // RHS vector must not be used in a following math operation of a different category.
275
- if ( rhsVectorBrackets ? . Next ? . Token is not SymbolOperatorToken nextMath ||
276
- nextMath . GetMathSymbolType ( ) == symbol . GetMathSymbolType ( ) ||
277
- nextMath . GetMathSymbolType ( ) == TokenExtensions . MathSymbolType . AddSubtract )
269
+ if ( IsSafeToPerformMath ( vectorNode , symbol , rhsVectorNode ) )
278
270
{
279
271
// Ensure both brackets have the same number of elements.
280
272
var lhsCsv = brackets . GetCsv ( ) . ToList ( ) ;
@@ -314,22 +306,10 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
314
306
. ToList ( ) )
315
307
{
316
308
var symbolNode = numNodeA . Next ;
317
- var symbolType = symbolNode . Token . GetMathSymbolType ( ) ;
318
-
319
- if ( symbolType != TokenExtensions . MathSymbolType . MultiplyDivide &&
320
- numNodeA . Previous != null &&
321
- numNodeA . Previous . Token is not CommaToken &&
322
- numNodeA . Previous . Token ? . GetMathSymbolType ( ) != symbolType )
323
- {
324
- continue ;
325
- }
326
-
327
309
var numNodeB = symbolNode . Next ;
328
- if ( numNodeB ? . Next ? . Token is SymbolOperatorToken &&
329
- numNodeB . Next . Token . GetMathSymbolType ( ) != TokenExtensions . MathSymbolType . AddSubtract )
330
- {
310
+
311
+ if ( ! IsSafeToPerformMath ( numNodeA , symbolNode . Token as SymbolOperatorToken , numNodeB ) )
331
312
continue ;
332
- }
333
313
334
314
var c = DoNodeMath ( numNodeA , symbolNode , numNodeB ) ;
335
315
@@ -355,6 +335,32 @@ numNodeA.Previous.Token is not CommaToken &&
355
335
return repeatSimplifications ;
356
336
}
357
337
338
+ private static bool IsSafeToPerformMath ( SyntaxNode lhsNumNode , SymbolOperatorToken symbol , SyntaxNode rhsNumNode )
339
+ {
340
+ if ( lhsNumNode == null || symbol == null || rhsNumNode == null )
341
+ return false ;
342
+
343
+ var symbolType = symbol . GetMathSymbolType ( ) ;
344
+ switch ( symbolType )
345
+ {
346
+ case TokenExtensions . MathSymbolType . Unknown :
347
+ return false ;
348
+
349
+ // If main symbol is * or /, that's fine - Any further symbols in the expression won't affect things.
350
+ case TokenExtensions . MathSymbolType . MultiplyDivide :
351
+ return true ;
352
+
353
+ // Only ok to perform math on the two nodes if the surrounding symbols are + or - (or don't exist).
354
+ case TokenExtensions . MathSymbolType . AddSubtract :
355
+ var prevSymbol = ( lhsNumNode . Previous as GenericSyntaxNode ) ? . Token as SymbolOperatorToken ;
356
+ var nextSymbol = ( rhsNumNode . Next is RoundBracketSyntaxNode ? rhsNumNode . Next . Next : rhsNumNode . Next ) ? . Token as SymbolOperatorToken ;
357
+ return new [ ] { prevSymbol , nextSymbol } . Where ( o => o != null ) . All ( o => o . GetMathSymbolType ( ) == symbolType ) ;
358
+
359
+ default :
360
+ throw new ArgumentOutOfRangeException ( ) ;
361
+ }
362
+ }
363
+
358
364
private static double DoNodeMath ( SyntaxNode numNodeA , SyntaxNode symbolNode , SyntaxNode numNodeB )
359
365
{
360
366
var a = double . Parse ( numNodeA . Token . Content ) ;
0 commit comments