Skip to content

Commit b266cc5

Browse files
authored
Merge pull request #164 from codingseb/dev
Dev
2 parents d5b780a + 4829467 commit b266cc5

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

CodingSeb.ExpressionEvaluator.Tests/CodingSeb.ExpressionEvaluator.Tests.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
<Nullable>disable</Nullable>
1010
</PropertyGroup>
1111
<ItemGroup>
12-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
13-
<PackageReference Include="NUnit" Version="3.13.0" />
14-
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0">
12+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
13+
<PackageReference Include="NUnit" Version="3.13.3" />
14+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>
18-
<PackageReference Include="Shouldly" Version="4.0.3" />
18+
<PackageReference Include="Shouldly" Version="4.2.1" />
1919
</ItemGroup><ItemGroup>
2020
<Compile Update="OthersTests.cs" />
2121
<Compile Update="Resources.Designer.cs">

CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<Product>CodingSeb.ExpressionEvaluator</Product>
66
<Description>A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts</Description>
77
<Copyright>Copyright © Coding Seb 2017</Copyright>
8-
<Version>1.4.39.0</Version>
9-
<AssemblyVersion>1.4.39.0</AssemblyVersion>
10-
<FileVersion>1.4.39.0</FileVersion>
8+
<Version>1.4.40.0</Version>
9+
<AssemblyVersion>1.4.40.0</AssemblyVersion>
10+
<FileVersion>1.4.40.0</FileVersion>
1111
<OutputPath>bin\$(Configuration)\</OutputPath>
1212
<Authors>Coding Seb</Authors>
1313
<PackageId>CodingSeb.ExpressionEvaluator</PackageId>
@@ -20,8 +20,9 @@
2020
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
2121
<PackageIcon>Icon.png</PackageIcon>
2222
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
23-
<PackageReleaseNotes>* net45 target is now net462 because (net45 is not supported anymore)
24-
* Match function arguments considering implicit casts</PackageReleaseNotes>
23+
<PackageReleaseNotes>* Make shared cache for types resolution thread safe
24+
* Add ScriptEvaluating and ScriptEvaluated events
25+
* Add unaryOperatorsDictionary to manage custom operators that are both unaries and binaries better</PackageReleaseNotes>
2526
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
2627
<RepositoryUrl>https://github.com/codingseb/ExpressionEvaluator</RepositoryUrl>
2728
<GenerateDocumentationFile>true</GenerateDocumentationFile>

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

+44-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

Comments
 (0)