1
1
/******************************************************************************************************
2
2
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3
- Version : 1.3.6.1
3
+ Version : 1.3.6.2
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
@@ -70,7 +70,7 @@ public class ExpressionEvaluator
70
70
71
71
// For script only
72
72
private static readonly Regex blockKeywordsBeginningRegex = new Regex ( @"^(?>\s*)(?<keyword>while|for|foreach|if|else(?>\s*)if|catch)(?>\s*)[(]" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
73
- private static readonly Regex foreachParenthisEvaluationRegex = new Regex ( @"^(?>\s*)(?<variableName>[" + diactiticsKeywordsRegexPattern + "](?>[" + diactiticsKeywordsRegexPattern + @"0-9]*))(?>\s*)(?<in>in)(?>\s*)(?<collection>.*)" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
73
+ private static readonly Regex foreachParenthisEvaluationRegex = new Regex ( @"^(?>\s*)(?<variableName>[" + diactiticsKeywordsRegexPattern + "](?>[" + diactiticsKeywordsRegexPattern + @"0-9]*))(?>\s*)(?<in>in)(?>\s*)(?<collection>.*)" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
74
74
private static readonly Regex blockKeywordsWithoutParenthesesBeginningRegex = new Regex ( @"^(?>\s*)(?<keyword>else|do|try|finally)(?![" + diactiticsKeywordsRegexPattern + "0-9])" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
75
75
private static readonly Regex blockBeginningRegex = new Regex ( @"^(?>\s*)[{]" , RegexOptions . Compiled ) ;
76
76
private static readonly Regex returnKeywordRegex = new Regex ( @"^return((?>\s*)|\()" , RegexOptions . IgnoreCase | RegexOptions . Singleline | RegexOptions . Compiled ) ;
@@ -411,16 +411,25 @@ private enum TryBlockEvaluatedState
411
411
} ,
412
412
{ "Round" , ( self , args ) =>
413
413
{
414
- if ( args . Count == 3 ) { return Math . Round ( Convert . ToDouble ( self . Evaluate ( args [ 0 ] ) ) , ( int ) self . Evaluate ( args [ 1 ] ) , ( MidpointRounding ) self . Evaluate ( args [ 2 ] ) ) ; } else if ( args . Count == 2 )
414
+ if ( args . Count == 3 )
415
+ {
416
+ return Math . Round ( Convert . ToDouble ( self . Evaluate ( args [ 0 ] ) ) , Convert . ToInt32 ( self . Evaluate ( args [ 1 ] ) ) , ( MidpointRounding ) self . Evaluate ( args [ 2 ] ) ) ;
417
+ }
418
+ else if ( args . Count == 2 )
415
419
{
416
420
object arg2 = self . Evaluate ( args [ 1 ] ) ;
417
421
418
422
if ( arg2 is MidpointRounding midpointRounding )
419
423
return Math . Round ( Convert . ToDouble ( self . Evaluate ( args [ 0 ] ) ) , midpointRounding ) ;
420
424
else
421
- return Math . Round ( Convert . ToDouble ( self . Evaluate ( args [ 0 ] ) ) , ( int ) arg2 ) ;
425
+ return Math . Round ( Convert . ToDouble ( self . Evaluate ( args [ 0 ] ) ) , Convert . ToInt32 ( arg2 ) ) ;
426
+ }
427
+ else if ( args . Count == 1 ) { return Math . Round ( Convert . ToDouble ( self . Evaluate ( args [ 0 ] ) ) ) ; }
428
+ else
429
+ {
430
+ throw new ArgumentException ( ) ;
422
431
}
423
- else if ( args . Count == 1 ) { return Math . Round ( Convert . ToDouble ( self . Evaluate ( args [ 0 ] ) ) ) ; } else { throw new ArgumentException ( ) ; } }
432
+ }
424
433
} ,
425
434
{ "Sign" , ( self , args ) => Math . Sign ( Convert . ToDouble ( self . Evaluate ( args [ 0 ] ) ) ) } ,
426
435
{ "sizeof" , ( self , args ) =>
@@ -918,7 +927,7 @@ object ManageJumpStatementsOrExpressionEval(string expression)
918
927
return lastResult ;
919
928
}
920
929
921
- if ( expression . StartsWith ( "throw " , StringComparisonForCasing ) )
930
+ if ( expression . StartsWith ( "throw " , StringComparisonForCasing ) )
922
931
{
923
932
throw Evaluate ( expression . Remove ( 0 , 6 ) ) as Exception ;
924
933
}
@@ -992,9 +1001,9 @@ void ExecuteIfList()
992
1001
993
1002
void ExecuteTryList ( )
994
1003
{
995
- if ( tryStatementsList . Count > 0 )
1004
+ if ( tryStatementsList . Count > 0 )
996
1005
{
997
- if ( tryStatementsList . Count == 1 )
1006
+ if ( tryStatementsList . Count == 1 )
998
1007
{
999
1008
throw new ExpressionEvaluatorSyntaxErrorException ( "a try statement need at least one catch or one finally statement." ) ;
1000
1009
}
@@ -1003,7 +1012,7 @@ void ExecuteTryList()
1003
1012
{
1004
1013
lastResult = ScriptEvaluate ( tryStatementsList [ 0 ] [ 0 ] , ref isReturn , ref isBreak , ref isContinue ) ;
1005
1014
}
1006
- catch ( Exception exception )
1015
+ catch ( Exception exception )
1007
1016
{
1008
1017
bool atLeasOneCatch = false ;
1009
1018
@@ -1031,14 +1040,14 @@ void ExecuteTryList()
1031
1040
break ;
1032
1041
}
1033
1042
1034
- if ( ! atLeasOneCatch )
1043
+ if ( ! atLeasOneCatch )
1035
1044
{
1036
1045
throw ;
1037
1046
}
1038
1047
}
1039
1048
finally
1040
1049
{
1041
- if ( tryStatementsList . Last ( ) [ 0 ] . Equals ( "finally" ) )
1050
+ if ( tryStatementsList . Last ( ) [ 0 ] . Equals ( "finally" ) )
1042
1051
{
1043
1052
lastResult = ScriptEvaluate ( tryStatementsList . Last ( ) [ 1 ] , ref isReturn , ref isBreak , ref isContinue ) ;
1044
1053
}
@@ -1167,7 +1176,7 @@ void ExecuteBlocksStacks()
1167
1176
ifBlockEvaluatedState = IfBlockEvaluatedState . If ;
1168
1177
tryBlockEvaluatedState = TryBlockEvaluatedState . NoBlockEvaluated ;
1169
1178
}
1170
- else if ( keyword . Equals ( "try" , StringComparisonForCasing ) )
1179
+ else if ( keyword . Equals ( "try" , StringComparisonForCasing ) )
1171
1180
{
1172
1181
tryStatementsList . Add ( new List < string > ( ) { subScript } ) ;
1173
1182
ifBlockEvaluatedState = IfBlockEvaluatedState . NoBlockEvaluated ;
@@ -1499,7 +1508,7 @@ private bool EvaluateNumber(string restOfExpression, Stack<object> stack, ref in
1499
1508
{
1500
1509
if ( OptionForceIntegerNumbersEvaluationsAsDoubleByDefault || numberMatch . Groups [ "hasdecimal" ] . Success )
1501
1510
{
1502
- stack . Push ( double . Parse ( numberMatch . Value . Replace ( "_" , "" ) , NumberStyles . Any , CultureInfoForNumberParsing ) ) ;
1511
+ stack . Push ( double . Parse ( numberMatch . Value . Replace ( "_" , "" ) , NumberStyles . Any , CultureInfoForNumberParsing ) ) ;
1503
1512
}
1504
1513
else
1505
1514
{
@@ -1654,7 +1663,7 @@ void Init(object element, List<string> initArgs)
1654
1663
1655
1664
if ( arrayArgs . Count > 0 )
1656
1665
{
1657
- array = Array . CreateInstance ( type , arrayArgs . ConvertAll ( subExpression => ( int ) Evaluate ( subExpression ) ) . ToArray ( ) ) ;
1666
+ array = Array . CreateInstance ( type , arrayArgs . ConvertAll ( subExpression => Convert . ToInt32 ( Evaluate ( subExpression ) ) ) . ToArray ( ) ) ;
1658
1667
}
1659
1668
1660
1669
Match initInNewBeginningMatch = initInNewBeginningRegex . Match ( expr . Substring ( i ) ) ;
@@ -1685,7 +1694,7 @@ void Init(object element, List<string> initArgs)
1685
1694
{
1686
1695
return false ;
1687
1696
}
1688
- }
1697
+ }
1689
1698
1690
1699
private bool EvaluateVarOrFunc ( string expr , string restOfExpression , Stack < object > stack , ref int i )
1691
1700
{
@@ -1761,7 +1770,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
1761
1770
else
1762
1771
stack . Push ( ( dictionaryObject [ varFuncName ] as Delegate ) . DynamicInvoke ( oArgs . ToArray ( ) ) ) ;
1763
1772
}
1764
- else if ( objType . GetProperty ( varFuncName , InstanceBindingFlag ) is PropertyInfo instancePropertyInfo
1773
+ else if ( objType . GetProperty ( varFuncName , InstanceBindingFlag ) is PropertyInfo instancePropertyInfo
1765
1774
&& ( instancePropertyInfo . PropertyType . IsSubclassOf ( typeof ( Delegate ) ) || instancePropertyInfo . PropertyType == typeof ( Delegate ) ) )
1766
1775
{
1767
1776
stack . Push ( ( instancePropertyInfo . GetValue ( obj ) as Delegate ) . DynamicInvoke ( oArgs . ToArray ( ) ) ) ;
@@ -2090,7 +2099,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
2090
2099
string typeName = $ "{ varFuncName } { ( ( i < expr . Length && expr . Substring ( i ) [ 0 ] == '?' ) ? "?" : "" ) } ";
2091
2100
Type staticType = GetTypeByFriendlyName ( typeName , genericsTypes ) ;
2092
2101
2093
- if ( staticType == null && OptionInlineNamespacesEvaluationActive )
2102
+ if ( staticType == null && OptionInlineNamespacesEvaluationActive )
2094
2103
{
2095
2104
int subIndex = 0 ;
2096
2105
Match namespaceMatch = varOrFunctionRegEx . Match ( expr . Substring ( i + subIndex ) ) ;
@@ -2109,7 +2118,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
2109
2118
2110
2119
staticType = GetTypeByFriendlyName ( typeName , namespaceMatch . Groups [ "isgeneric" ] . Value ) ;
2111
2120
2112
- if ( staticType != null )
2121
+ if ( staticType != null )
2113
2122
{
2114
2123
i += subIndex ;
2115
2124
break ;
@@ -2776,7 +2785,7 @@ private BindingFlags DetermineInstanceOrStatic(ref Type objType, ref object obj,
2776
2785
{
2777
2786
valueTypeNestingTrace = obj as ValueTypeNestingTrace ;
2778
2787
2779
- if ( valueTypeNestingTrace != null )
2788
+ if ( valueTypeNestingTrace != null )
2780
2789
{
2781
2790
obj = valueTypeNestingTrace . Value ;
2782
2791
}
@@ -3000,7 +3009,7 @@ private Type GetTypeByFriendlyName(string typeName, string genericTypes = "", bo
3000
3009
}
3001
3010
}
3002
3011
}
3003
- catch ( ExpressionEvaluatorSyntaxErrorException )
3012
+ catch ( ExpressionEvaluatorSyntaxErrorException )
3004
3013
{
3005
3014
throw ;
3006
3015
}
@@ -3125,7 +3134,7 @@ private class ValueTypeNestingTrace
3125
3134
3126
3135
public void AssignValue ( )
3127
3136
{
3128
- if ( Container is ValueTypeNestingTrace valueTypeNestingTrace )
3137
+ if ( Container is ValueTypeNestingTrace valueTypeNestingTrace )
3129
3138
{
3130
3139
( ( dynamic ) Member ) . SetValue ( valueTypeNestingTrace . Value , Value ) ;
3131
3140
valueTypeNestingTrace . AssignValue ( ) ;
0 commit comments