1
1
/******************************************************************************************************
2
2
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3
- Version : 1.3.7.3
3
+ Version : 1.4.0.0
4
4
(if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
5
5
6
6
Author : Coding Seb
@@ -883,6 +883,8 @@ protected virtual void Init()
883
883
884
884
#region Main evaluate methods (Expressions and scripts ==> public)
885
885
886
+ #region Scripts
887
+
886
888
protected bool inScript ;
887
889
888
890
/// <summary>
@@ -1370,6 +1372,10 @@ void forAction(int index)
1370
1372
throw new ExpressionEvaluatorSyntaxErrorException ( "No [return] keyword found" ) ;
1371
1373
}
1372
1374
1375
+ #endregion
1376
+
1377
+ #region Expressions
1378
+
1373
1379
/// <summary>
1374
1380
/// Evaluate the specified math or pseudo C# expression
1375
1381
/// </summary>
@@ -1381,71 +1387,43 @@ public T Evaluate<T>(string expression)
1381
1387
return ( T ) Evaluate ( expression ) ;
1382
1388
}
1383
1389
1390
+ private IList < ParsingMethodDelegate > parsingMethods ;
1391
+
1392
+ protected virtual IList < ParsingMethodDelegate > ParsingMethods => parsingMethods ?? ( parsingMethods = new List < ParsingMethodDelegate > ( )
1393
+ {
1394
+ EvaluateCast ,
1395
+ EvaluateNumber ,
1396
+ EvaluateInstanceCreationWithNewKeyword ,
1397
+ EvaluateVarOrFunc ,
1398
+ EvaluateOperators ,
1399
+ EvaluateChar ,
1400
+ EvaluateParenthis ,
1401
+ EvaluateIndexing ,
1402
+ EvaluateString ,
1403
+ EvaluateTernaryConditionalOperator ,
1404
+ } ) ;
1405
+
1384
1406
/// <summary>
1385
1407
/// Evaluate the specified math or pseudo C# expression
1386
1408
/// </summary>
1387
1409
/// <param name="expression">the math or pseudo C# expression to evaluate</param>
1388
1410
/// <returns>The result of the operation if syntax is correct</returns>
1389
1411
public object Evaluate ( string expression )
1390
1412
{
1391
- bool continueEvaluation = true ;
1392
-
1393
1413
expression = expression . Trim ( ) ;
1394
1414
1395
1415
Stack < object > stack = new Stack < object > ( ) ;
1396
1416
1397
1417
if ( GetLambdaExpression ( expression , stack ) )
1398
1418
return stack . Pop ( ) ;
1399
1419
1400
- for ( int i = 0 ; i < expression . Length && continueEvaluation ; i ++ )
1420
+ for ( int i = 0 ; i < expression . Length ; i ++ )
1401
1421
{
1402
- if ( ! ( EvaluateCast ( expression , stack , ref i )
1403
- || EvaluateNumber ( expression , stack , ref i )
1404
- || EvaluateInstanceCreationWithNewKeyword ( expression , stack , ref i )
1405
- || EvaluateVarOrFunc ( expression , stack , ref i )
1406
- || EvaluateOperators ( expression , stack , ref i )
1407
- || EvaluateChar ( expression , stack , ref i )
1408
- || EvaluateParenthis ( expression , stack , ref i )
1409
- || EvaluateIndexing ( expression , stack , ref i )
1410
- || EvaluateString ( expression , stack , ref i ) ) )
1422
+ if ( ! ParsingMethods . Any ( m => m ( expression , stack , ref i ) ) )
1411
1423
{
1412
1424
string s = expression . Substring ( i , 1 ) ;
1413
1425
1414
- if ( s . Equals ( "?" ) )
1415
- {
1416
- bool condition = ( bool ) ProcessStack ( stack ) ;
1417
-
1418
- string restOfExpression = expression . Substring ( i ) ;
1419
-
1420
- for ( int j = 1 ; j < restOfExpression . Length ; j ++ )
1421
- {
1422
- string s2 = restOfExpression . Substring ( j , 1 ) ;
1423
-
1424
- Match internalStringMatch = stringBeginningRegex . Match ( restOfExpression . Substring ( j ) ) ;
1425
-
1426
- if ( internalStringMatch . Success )
1427
- {
1428
- string innerString = internalStringMatch . Value + GetCodeUntilEndOfString ( restOfExpression . Substring ( j + internalStringMatch . Length ) , internalStringMatch ) ;
1429
- j += innerString . Length - 1 ;
1430
- }
1431
- else if ( s2 . Equals ( "(" ) )
1432
- {
1433
- j ++ ;
1434
- GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( restOfExpression , ref j , false ) ;
1435
- }
1436
- else if ( s2 . Equals ( ":" ) )
1437
- {
1438
- stack . Clear ( ) ;
1439
-
1440
- stack . Push ( condition ? Evaluate ( restOfExpression . Substring ( 1 , j - 1 ) ) : Evaluate ( restOfExpression . Substring ( j + 1 ) ) ) ;
1441
-
1442
- continueEvaluation = false ;
1443
-
1444
- break ;
1445
- }
1446
- }
1447
- }
1448
- else if ( ! s . Trim ( ) . Equals ( string . Empty ) )
1426
+ if ( ! s . Trim ( ) . Equals ( string . Empty ) )
1449
1427
{
1450
1428
throw new ExpressionEvaluatorSyntaxErrorException ( $ "Invalid character [{ ( int ) s [ 0 ] } :{ s } ]") ;
1451
1429
}
@@ -1457,6 +1435,8 @@ public object Evaluate(string expression)
1457
1435
1458
1436
#endregion
1459
1437
1438
+ #endregion
1439
+
1460
1440
#region Sub parts evaluate methods (protected virtual)
1461
1441
1462
1442
protected virtual bool EvaluateCast ( string expression , Stack < object > stack , ref int i )
@@ -2325,6 +2305,46 @@ protected virtual bool EvaluateOperators(string expression, Stack<object> stack,
2325
2305
return false ;
2326
2306
}
2327
2307
2308
+ protected virtual bool EvaluateTernaryConditionalOperator ( string expression , Stack < object > stack , ref int i )
2309
+ {
2310
+ if ( expression . Substring ( i , 1 ) . Equals ( "?" ) )
2311
+ {
2312
+ bool condition = ( bool ) ProcessStack ( stack ) ;
2313
+
2314
+ string restOfExpression = expression . Substring ( i + 1 ) ;
2315
+
2316
+ for ( int j = 0 ; j < restOfExpression . Length ; j ++ )
2317
+ {
2318
+ string s2 = restOfExpression . Substring ( j , 1 ) ;
2319
+
2320
+ Match internalStringMatch = stringBeginningRegex . Match ( restOfExpression . Substring ( j ) ) ;
2321
+
2322
+ if ( internalStringMatch . Success )
2323
+ {
2324
+ string innerString = internalStringMatch . Value + GetCodeUntilEndOfString ( restOfExpression . Substring ( j + internalStringMatch . Length ) , internalStringMatch ) ;
2325
+ j += innerString . Length - 1 ;
2326
+ }
2327
+ else if ( s2 . Equals ( "(" ) )
2328
+ {
2329
+ j ++ ;
2330
+ GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( restOfExpression , ref j , false ) ;
2331
+ }
2332
+ else if ( s2 . Equals ( ":" ) )
2333
+ {
2334
+ stack . Clear ( ) ;
2335
+
2336
+ stack . Push ( condition ? Evaluate ( restOfExpression . Substring ( 1 , j - 1 ) ) : Evaluate ( restOfExpression . Substring ( j + 1 ) ) ) ;
2337
+
2338
+ i = expression . Length ;
2339
+
2340
+ return true ;
2341
+ }
2342
+ }
2343
+ }
2344
+
2345
+ return false ;
2346
+ }
2347
+
2328
2348
protected virtual bool EvaluateParenthis ( string expression , Stack < object > stack , ref int i )
2329
2349
{
2330
2350
string s = expression . Substring ( i , 1 ) ;
@@ -3770,4 +3790,4 @@ public FunctionPreEvaluationEventArg(string name, Func<string, object> evaluateF
3770
3790
}
3771
3791
3772
3792
#endregion
3773
- }
3793
+ }
0 commit comments