11/******************************************************************************************************
22 Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3- Version : 1.4.39 .0
3+ Version : 1.4.40 .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
@@ -227,6 +227,12 @@ protected enum TryBlockEvaluatedState
227227 ExpressionOperator . UnaryMinus
228228 } ;
229229
230+ protected IDictionary < string , ExpressionOperator > unaryOperatorsDictionary = new Dictionary < string , ExpressionOperator > ( )
231+ {
232+ { "+" , ExpressionOperator . UnaryPlus } ,
233+ { "-" , ExpressionOperator . UnaryMinus }
234+ } ;
235+
230236 protected virtual IList < ExpressionOperator > LeftOperandOnlyOperatorsEvaluationDictionary => leftOperandOnlyOperatorsEvaluationDictionary ;
231237 protected virtual IList < ExpressionOperator > RightOperandOnlyOperatorsEvaluationDictionary => rightOperandOnlyOperatorsEvaluationDictionary ;
232238 protected virtual IList < IDictionary < ExpressionOperator , Func < dynamic , dynamic , object > > > OperatorsEvaluations => operatorsEvaluations ;
@@ -928,6 +934,18 @@ public IDictionary<string, object> Variables
928934 }
929935 }
930936
937+ /// <summary>
938+ /// Is fired just before a script is evaluate.<para/>
939+ /// Allow to redefine the script to evaluate or to force a result value.
940+ /// </summary>
941+ public event EventHandler < ExpressionEvaluationEventArg > ScriptEvaluating ;
942+
943+ /// <summary>
944+ /// Is fired just before to return the script evaluation.<para/>
945+ /// Allow to modify on the fly the result of the evaluation.
946+ /// </summary>
947+ public event EventHandler < ExpressionEvaluationEventArg > ScriptEvaluated ;
948+
931949 /// <summary>
932950 /// Is fired just before an expression is evaluate.<para/>
933951 /// Allow to redefine the expression to evaluate or to force a result value.
@@ -1085,6 +1103,13 @@ public virtual T ScriptEvaluate<T>(string script)
10851103 public virtual object ScriptEvaluate ( string script )
10861104 {
10871105 inScript = true ;
1106+
1107+ ExpressionEvaluationEventArg expressionEvaluationEventArg = new ExpressionEvaluationEventArg ( script , this ) ;
1108+
1109+ ScriptEvaluating ? . Invoke ( this , expressionEvaluationEventArg ) ;
1110+
1111+ script = expressionEvaluationEventArg . Expression ;
1112+
10881113 try
10891114 {
10901115 bool isReturn = false ;
@@ -1094,11 +1119,26 @@ public virtual object ScriptEvaluate(string script)
10941119 object result = ScriptEvaluate ( script , ref isReturn , ref isBreak , ref isContinue ) ;
10951120
10961121 if ( isBreak )
1122+ {
10971123 throw new ExpressionEvaluatorSyntaxErrorException ( "[break] keyword executed outside a loop" ) ;
1124+ }
10981125 else if ( isContinue )
1126+ {
10991127 throw new ExpressionEvaluatorSyntaxErrorException ( "[continue] keyword executed outside a loop" ) ;
1128+ }
11001129 else
1130+ {
1131+ expressionEvaluationEventArg = new ExpressionEvaluationEventArg ( script , this , result ) ;
1132+
1133+ ScriptEvaluated ? . Invoke ( this , expressionEvaluationEventArg ) ;
1134+
1135+ if ( expressionEvaluationEventArg . HasValue )
1136+ {
1137+ result = expressionEvaluationEventArg . Value ;
1138+ }
1139+
11011140 return result ;
1141+ }
11021142 }
11031143 finally
11041144 {
@@ -2749,12 +2789,11 @@ protected virtual bool EvaluateOperators(string expression, Stack<object> stack,
27492789 {
27502790 string op = match . Value ;
27512791
2752- if ( op . Equals ( "+" ) && ( stack . Count == 0 || ( stack . Peek ( ) is ExpressionOperator previousOp && ! LeftOperandOnlyOperatorsEvaluationDictionary . Contains ( previousOp ) ) ) )
2753- stack . Push ( ExpressionOperator . UnaryPlus ) ;
2754- else if ( op . Equals ( "-" ) && ( stack . Count == 0 || ( stack . Peek ( ) is ExpressionOperator previousOp2 && ! LeftOperandOnlyOperatorsEvaluationDictionary . Contains ( previousOp2 ) ) ) )
2755- stack . Push ( ExpressionOperator . UnaryMinus ) ;
2792+ if ( unaryOperatorsDictionary . ContainsKey ( op ) && ( stack . Count == 0 || ( stack . Peek ( ) is ExpressionOperator previousOp && ! LeftOperandOnlyOperatorsEvaluationDictionary . Contains ( previousOp ) ) ) )
2793+ stack . Push ( unaryOperatorsDictionary [ op ] ) ;
27562794 else
27572795 stack . Push ( operatorsDictionary [ op ] ) ;
2796+
27582797 i += op . Length - 1 ;
27592798 return true ;
27602799 }
0 commit comments