From 289013febb4a2cd7f356656c69991dbe6d5726d7 Mon Sep 17 00:00:00 2001 From: Yazwh0 <90315107+Yazwh0@users.noreply.github.com> Date: Sat, 15 Apr 2023 16:59:33 +0100 Subject: [PATCH 1/5] Update ExpressionEvaluator.cs Add unary operators so they can be added to --- .../ExpressionEvaluator.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs index da6caa4..bb5f400 100644 --- a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs +++ b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs @@ -226,6 +226,12 @@ protected enum TryBlockEvaluatedState ExpressionOperator.UnaryMinus }; + protected IDictionary unaryOperatorsDictionary = new Dictionary() + { + { "+", ExpressionOperator.UnaryPlus }, + { "-", ExpressionOperator.UnaryMinus } + }; + protected virtual IList LeftOperandOnlyOperatorsEvaluationDictionary => leftOperandOnlyOperatorsEvaluationDictionary; protected virtual IList RightOperandOnlyOperatorsEvaluationDictionary => rightOperandOnlyOperatorsEvaluationDictionary; protected virtual IList>> OperatorsEvaluations => operatorsEvaluations; @@ -2748,12 +2754,11 @@ protected virtual bool EvaluateOperators(string expression, Stack stack, { string op = match.Value; - if (op.Equals("+") && (stack.Count == 0 || (stack.Peek() is ExpressionOperator previousOp && !LeftOperandOnlyOperatorsEvaluationDictionary.Contains(previousOp)))) - stack.Push(ExpressionOperator.UnaryPlus); - else if (op.Equals("-") && (stack.Count == 0 || (stack.Peek() is ExpressionOperator previousOp2 && !LeftOperandOnlyOperatorsEvaluationDictionary.Contains(previousOp2)))) - stack.Push(ExpressionOperator.UnaryMinus); + if (unaryOperatorsDictionary.ContainsKey(op) && (stack.Count == 0 || (stack.Peek() is ExpressionOperator previousOp && !LeftOperandOnlyOperatorsEvaluationDictionary.Contains(previousOp)))) + stack.Push(unaryOperatorsDictionary[op]); else stack.Push(operatorsDictionary[op]); + i += op.Length - 1; return true; } From f7f99e470d5024b9d945eda51888ebb59c5611ec Mon Sep 17 00:00:00 2001 From: Arc <87562566+Arc-huangjingtong@users.noreply.github.com> Date: Sun, 13 Aug 2023 22:18:04 +0800 Subject: [PATCH 2/5] Add a new event Add a new event was fired in ScriptEvaluate --- .../ExpressionEvaluator.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs index da6caa4..3b9c78d 100644 --- a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs +++ b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs @@ -927,6 +927,13 @@ public IDictionary Variables } } + /// + /// Is fired just before an scriptExpression is evaluate. + /// Allow to redefine the scriptExpression to evaluate or to force a result value. + /// + public event EventHandler ScriptExpressionEvaluating; + + /// /// Is fired just before an expression is evaluate. /// Allow to redefine the expression to evaluate or to force a result value. @@ -1084,6 +1091,13 @@ public virtual T ScriptEvaluate(string script) public virtual object ScriptEvaluate(string script) { inScript = true; + + ExpressionEvaluationEventArg expressionEvaluationEventArg = new ExpressionEvaluationEventArg(script, this); + + ExpressionEvaluating?.Invoke(this, expressionEvaluationEventArg); + + script = expressionEvaluationEventArg.Expression; + try { bool isReturn = false; From 7bde9be953e2e876562253c94b6bd32ac85db428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Geiser?= Date: Fri, 22 Sep 2023 08:59:49 +0200 Subject: [PATCH 3/5] Correct Script Event name and add Script Evaluated event --- .../ExpressionEvaluator.cs | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs index 85015b7..ce058ce 100644 --- a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs +++ b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs @@ -929,12 +929,17 @@ public IDictionary Variables } /// - /// Is fired just before an scriptExpression is evaluate. - /// Allow to redefine the scriptExpression to evaluate or to force a result value. + /// Is fired just before a script is evaluate. + /// Allow to redefine the script to evaluate or to force a result value. /// - public event EventHandler ScriptExpressionEvaluating; - - + public event EventHandler ScriptEvaluating; + + /// + /// Is fired just before to return the script evaluation. + /// Allow to modify on the fly the result of the evaluation. + /// + public event EventHandler ScriptEvaluated; + /// /// Is fired just before an expression is evaluate. /// Allow to redefine the expression to evaluate or to force a result value. @@ -1095,7 +1100,7 @@ public virtual object ScriptEvaluate(string script) ExpressionEvaluationEventArg expressionEvaluationEventArg = new ExpressionEvaluationEventArg(script, this); - ExpressionEvaluating?.Invoke(this, expressionEvaluationEventArg); + ScriptEvaluating?.Invoke(this, expressionEvaluationEventArg); script = expressionEvaluationEventArg.Expression; @@ -1108,11 +1113,26 @@ public virtual object ScriptEvaluate(string script) object result = ScriptEvaluate(script, ref isReturn, ref isBreak, ref isContinue); if (isBreak) + { throw new ExpressionEvaluatorSyntaxErrorException("[break] keyword executed outside a loop"); + } else if (isContinue) + { throw new ExpressionEvaluatorSyntaxErrorException("[continue] keyword executed outside a loop"); + } else + { + expressionEvaluationEventArg = new ExpressionEvaluationEventArg(script, this, result); + + ScriptEvaluated?.Invoke(this, expressionEvaluationEventArg); + + if (expressionEvaluationEventArg.HasValue) + { + result = expressionEvaluationEventArg.Value; + } + return result; + } } finally { From 02ffd854856bfe88d1c5efdf1d50e1a75b1bcc5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Geiser?= Date: Fri, 22 Sep 2023 09:20:15 +0200 Subject: [PATCH 4/5] Version 1.4.40.0 --- .../CodingSeb.ExpressionEvaluator.csproj | 11 ++++++----- CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj b/CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj index 41d2e4b..d0abc48 100644 --- a/CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj +++ b/CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj @@ -5,9 +5,9 @@ CodingSeb.ExpressionEvaluator A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts Copyright © Coding Seb 2017 - 1.4.39.0 - 1.4.39.0 - 1.4.39.0 + 1.4.40.0 + 1.4.40.0 + 1.4.40.0 bin\$(Configuration)\ Coding Seb CodingSeb.ExpressionEvaluator @@ -20,8 +20,9 @@ https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true Icon.png false - * net45 target is now net462 because (net45 is not supported anymore) -* Match function arguments considering implicit casts + * Make shared cache for types resolution thread safe +* Add ScriptEvaluating and ScriptEvaluated events +* Add unaryOperatorsDictionary to manage custom operators that are both unaries and binaries better LICENSE.md https://github.com/codingseb/ExpressionEvaluator true diff --git a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs index 807804f..dfa1fd9 100644 --- a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs +++ b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs @@ -1,6 +1,6 @@ /****************************************************************************************************** Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator) - Version : 1.4.39.0 + Version : 1.4.40.0 (if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable) Author : Coding Seb From 48294673774e75feed46f9b9b80b9e18ca5e3093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Geiser?= Date: Fri, 22 Sep 2023 09:38:25 +0200 Subject: [PATCH 5/5] Update packages --- .../CodingSeb.ExpressionEvaluator.Tests.csproj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CodingSeb.ExpressionEvaluator.Tests/CodingSeb.ExpressionEvaluator.Tests.csproj b/CodingSeb.ExpressionEvaluator.Tests/CodingSeb.ExpressionEvaluator.Tests.csproj index 65291f4..85a6120 100644 --- a/CodingSeb.ExpressionEvaluator.Tests/CodingSeb.ExpressionEvaluator.Tests.csproj +++ b/CodingSeb.ExpressionEvaluator.Tests/CodingSeb.ExpressionEvaluator.Tests.csproj @@ -9,13 +9,13 @@ disable - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - +