11/******************************************************************************************************
22 Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3- Version : 1.3.7.3
3+ Version : 1.4.0.0
44 (if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
55
66 Author : Coding Seb
@@ -883,6 +883,8 @@ protected virtual void Init()
883883
884884 #region Main evaluate methods (Expressions and scripts ==> public)
885885
886+ #region Scripts
887+
886888 protected bool inScript ;
887889
888890 /// <summary>
@@ -1370,6 +1372,10 @@ void forAction(int index)
13701372 throw new ExpressionEvaluatorSyntaxErrorException ( "No [return] keyword found" ) ;
13711373 }
13721374
1375+ #endregion
1376+
1377+ #region Expressions
1378+
13731379 /// <summary>
13741380 /// Evaluate the specified math or pseudo C# expression
13751381 /// </summary>
@@ -1381,71 +1387,43 @@ public T Evaluate<T>(string expression)
13811387 return ( T ) Evaluate ( expression ) ;
13821388 }
13831389
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+
13841406 /// <summary>
13851407 /// Evaluate the specified math or pseudo C# expression
13861408 /// </summary>
13871409 /// <param name="expression">the math or pseudo C# expression to evaluate</param>
13881410 /// <returns>The result of the operation if syntax is correct</returns>
13891411 public object Evaluate ( string expression )
13901412 {
1391- bool continueEvaluation = true ;
1392-
13931413 expression = expression . Trim ( ) ;
13941414
13951415 Stack < object > stack = new Stack < object > ( ) ;
13961416
13971417 if ( GetLambdaExpression ( expression , stack ) )
13981418 return stack . Pop ( ) ;
13991419
1400- for ( int i = 0 ; i < expression . Length && continueEvaluation ; i ++ )
1420+ for ( int i = 0 ; i < expression . Length ; i ++ )
14011421 {
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 ) ) )
14111423 {
14121424 string s = expression . Substring ( i , 1 ) ;
14131425
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 ) )
14491427 {
14501428 throw new ExpressionEvaluatorSyntaxErrorException ( $ "Invalid character [{ ( int ) s [ 0 ] } :{ s } ]") ;
14511429 }
@@ -1457,6 +1435,8 @@ public object Evaluate(string expression)
14571435
14581436 #endregion
14591437
1438+ #endregion
1439+
14601440 #region Sub parts evaluate methods (protected virtual)
14611441
14621442 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,
23252305 return false ;
23262306 }
23272307
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+
23282348 protected virtual bool EvaluateParenthis ( string expression , Stack < object > stack , ref int i )
23292349 {
23302350 string s = expression . Substring ( i , 1 ) ;
@@ -3770,4 +3790,4 @@ public FunctionPreEvaluationEventArg(string name, Func<string, object> evaluateF
37703790 }
37713791
37723792 #endregion
3773- }
3793+ }
0 commit comments