Skip to content

Commit

Permalink
Merge pull request #164 from codingseb/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
codingseb authored Sep 22, 2023
2 parents d5b780a + 4829467 commit b266cc5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NUnit" Version="3.13.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0">
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" Version="4.0.3" />
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup><ItemGroup>
<Compile Update="OthersTests.cs" />
<Compile Update="Resources.Designer.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<Product>CodingSeb.ExpressionEvaluator</Product>
<Description>A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts</Description>
<Copyright>Copyright © Coding Seb 2017</Copyright>
<Version>1.4.39.0</Version>
<AssemblyVersion>1.4.39.0</AssemblyVersion>
<FileVersion>1.4.39.0</FileVersion>
<Version>1.4.40.0</Version>
<AssemblyVersion>1.4.40.0</AssemblyVersion>
<FileVersion>1.4.40.0</FileVersion>
<OutputPath>bin\$(Configuration)\</OutputPath>
<Authors>Coding Seb</Authors>
<PackageId>CodingSeb.ExpressionEvaluator</PackageId>
Expand All @@ -20,8 +20,9 @@
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
<PackageIcon>Icon.png</PackageIcon>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>* net45 target is now net462 because (net45 is not supported anymore)
* Match function arguments considering implicit casts</PackageReleaseNotes>
<PackageReleaseNotes>* 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</PackageReleaseNotes>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<RepositoryUrl>https://github.com/codingseb/ExpressionEvaluator</RepositoryUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
49 changes: 44 additions & 5 deletions CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -227,6 +227,12 @@ protected enum TryBlockEvaluatedState
ExpressionOperator.UnaryMinus
};

protected IDictionary<string, ExpressionOperator> unaryOperatorsDictionary = new Dictionary<string, ExpressionOperator>()
{
{ "+", ExpressionOperator.UnaryPlus },
{ "-", ExpressionOperator.UnaryMinus }
};

protected virtual IList<ExpressionOperator> LeftOperandOnlyOperatorsEvaluationDictionary => leftOperandOnlyOperatorsEvaluationDictionary;
protected virtual IList<ExpressionOperator> RightOperandOnlyOperatorsEvaluationDictionary => rightOperandOnlyOperatorsEvaluationDictionary;
protected virtual IList<IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>> OperatorsEvaluations => operatorsEvaluations;
Expand Down Expand Up @@ -928,6 +934,18 @@ public IDictionary<string, object> Variables
}
}

/// <summary>
/// Is fired just before a script is evaluate.<para/>
/// Allow to redefine the script to evaluate or to force a result value.
/// </summary>
public event EventHandler<ExpressionEvaluationEventArg> ScriptEvaluating;

/// <summary>
/// Is fired just before to return the script evaluation.<para/>
/// Allow to modify on the fly the result of the evaluation.
/// </summary>
public event EventHandler<ExpressionEvaluationEventArg> ScriptEvaluated;

/// <summary>
/// Is fired just before an expression is evaluate.<para/>
/// Allow to redefine the expression to evaluate or to force a result value.
Expand Down Expand Up @@ -1085,6 +1103,13 @@ public virtual T ScriptEvaluate<T>(string script)
public virtual object ScriptEvaluate(string script)
{
inScript = true;

ExpressionEvaluationEventArg expressionEvaluationEventArg = new ExpressionEvaluationEventArg(script, this);

ScriptEvaluating?.Invoke(this, expressionEvaluationEventArg);

script = expressionEvaluationEventArg.Expression;

try
{
bool isReturn = false;
Expand All @@ -1094,11 +1119,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
{
Expand Down Expand Up @@ -2749,12 +2789,11 @@ protected virtual bool EvaluateOperators(string expression, Stack<object> 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;
}
Expand Down

0 comments on commit b266cc5

Please sign in to comment.