Skip to content

Commit 56ae71c

Browse files
authored
Merge pull request #38 from codingseb/dev
Dev
2 parents 413d3c9 + 7b0086d commit 56ae71c

File tree

6 files changed

+56
-27
lines changed

6 files changed

+56
-27
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,27 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg
18751875
.SetCategory("inherits ExpressionEvaluator")
18761876
.SetCategory("Custom operators");
18771877

1878+
yield return new TestCaseData(xExpressionEvaluator2
1879+
, "Not true", null)
1880+
.Returns(false)
1881+
.SetCategory("ExpressionEvaluator extend")
1882+
.SetCategory("inherits ExpressionEvaluator")
1883+
.SetCategory("Custom operators");
1884+
1885+
yield return new TestCaseData(xExpressionEvaluator2
1886+
, "Not(true)", null)
1887+
.Returns(false)
1888+
.SetCategory("ExpressionEvaluator extend")
1889+
.SetCategory("inherits ExpressionEvaluator")
1890+
.SetCategory("Custom operators");
1891+
1892+
yield return new TestCaseData(xExpressionEvaluator2
1893+
, "Not(1 == 1)", null)
1894+
.Returns(false)
1895+
.SetCategory("ExpressionEvaluator extend")
1896+
.SetCategory("inherits ExpressionEvaluator")
1897+
.SetCategory("Custom operators");
1898+
18781899
#endregion
18791900

18801901
#region Add a complex operator or change the parsing process

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/XExpressionEvaluator2.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ protected override void Init()
3131
{
3232
operatorsDictionary.Add("#", XExpressionOperator2.Sharp);
3333
operatorsDictionary.Add("love", XExpressionOperator2.Love);
34+
operatorsDictionary.Add("Not", ExpressionOperator.LogicalNegation);
3435
}
3536
}
3637

CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj

Lines changed: 5 additions & 13 deletions
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.0.0</Version>
9-
<AssemblyVersion>1.4.0.0</AssemblyVersion>
10-
<FileVersion>1.4.0.0</FileVersion>
8+
<Version>1.4.1.0</Version>
9+
<AssemblyVersion>1.4.1.0</AssemblyVersion>
10+
<FileVersion>1.4.1.0</FileVersion>
1111
<OutputPath>bin\$(Configuration)\</OutputPath>
1212
<Authors>Coding Seb</Authors>
1313
<PackageId>CodingSeb.ExpressionEvaluator</PackageId>
@@ -18,16 +18,8 @@
1818
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1919
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
2020
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
21-
<PackageReleaseNotes>Warning some breaking changes in the on the fly variables and function evaluations.
22-
Deep changes to allow more customization
23-
24-
* Custom operators or custom expression parsing with inheritance
25-
* Manage &lt;&gt; in on the fly evaluations to evaluate generic types
26-
* 2 additional on the fly evaluation (Preevaluation)
27-
* optionally use var before variable assignation (For better copy paste with C# code)
28-
* option to globally cache namespaces and types resolution (to speed up multiple evaluation)
29-
* Correction of Array/collection indexing when using OptionForceIntegernumbersEvaluationsAsDoubleDyDefault=true
30-
* Some other corrections and refactorings</PackageReleaseNotes>
21+
<PackageReleaseNotes>Correction of bug :
22+
* Added a righthandOperator 'Not' does not work</PackageReleaseNotes>
3123
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
3224
</PropertyGroup>
3325
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 17 additions & 12 deletions
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.0.0
3+
Version : 1.4.1.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
@@ -382,7 +382,7 @@ protected enum TryBlockEvaluatedState
382382
{ "new", (self, args) =>
383383
{
384384
List<object> cArgs = args.ConvertAll(self.Evaluate);
385-
return Activator.CreateInstance((cArgs[0] as ClassOrEnumType).Type, cArgs.Skip(1).ToArray());
385+
return cArgs[0] is ClassOrEnumType classOrEnumType ? Activator.CreateInstance(classOrEnumType.Type, cArgs.Skip(1).ToArray()) : null;
386386
}
387387
},
388388
{ "Round", (self, args) =>
@@ -429,11 +429,11 @@ protected enum TryBlockEvaluatedState
429429

430430
/// <summary>
431431
/// if set to <c>true</c> use a cache for types that were resolved to resolve faster next time.
432-
/// if set to <c>false</c> the cach of types resolution is not use for this instance of ExpressionEvaluator.
432+
/// if set to <c>false</c> the cache of types resolution is not use for this instance of ExpressionEvaluator.
433433
/// Default : false
434434
/// the cache is the static Dictionary TypesResolutionCaching (so it is shared by all instances of ExpressionEvaluator that have CacheTypesResolutions enabled)
435435
/// </summary>
436-
public bool CacheTypesResolutions { get; set; } = false;
436+
public bool CacheTypesResolutions { get; set; }
437437

438438
/// <summary>
439439
/// A shared cache for types resolution.
@@ -1713,7 +1713,8 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
17131713
&& (!varFuncMatch.Groups["sign"].Success
17141714
|| stack.Count == 0
17151715
|| stack.Peek() is ExpressionOperator)
1716-
&& !operatorsDictionary.ContainsKey(varFuncMatch.Value.Trim()))
1716+
&& !operatorsDictionary.ContainsKey(varFuncMatch.Value.Trim())
1717+
&& (!operatorsDictionary.ContainsKey(varFuncMatch.Groups["name"].Value) || varFuncMatch.Groups["inObject"].Success))
17171718
{
17181719
string varFuncName = varFuncMatch.Groups["name"].Value;
17191720
string genericsTypes = varFuncMatch.Groups["isgeneric"].Value;
@@ -1777,17 +1778,20 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
17771778
MethodInfo methodInfo = GetRealMethod(ref objType, ref obj, varFuncName, flag, oArgs, genericsTypes);
17781779

17791780
// if not found check if obj is an expandoObject or similar
1780-
if (obj is IDynamicMetaObjectProvider && obj is IDictionary<string, object> dictionaryObject && (dictionaryObject[varFuncName] is InternalDelegate || dictionaryObject[varFuncName] is Delegate))
1781+
if (obj is IDynamicMetaObjectProvider
1782+
&& obj is IDictionary<string, object> dictionaryObject
1783+
&& (dictionaryObject[varFuncName] is InternalDelegate || dictionaryObject[varFuncName] is Delegate))
17811784
{
17821785
if (dictionaryObject[varFuncName] is InternalDelegate internalDelegate)
17831786
stack.Push(internalDelegate(oArgs.ToArray()));
1784-
else
1785-
stack.Push((dictionaryObject[varFuncName] as Delegate).DynamicInvoke(oArgs.ToArray()));
1787+
else if(dictionaryObject[varFuncName] is Delegate del)
1788+
stack.Push(del.DynamicInvoke(oArgs.ToArray()));
17861789
}
17871790
else if (objType.GetProperty(varFuncName, InstanceBindingFlag) is PropertyInfo instancePropertyInfo
1788-
&& (instancePropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || instancePropertyInfo.PropertyType == typeof(Delegate)))
1791+
&& (instancePropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || instancePropertyInfo.PropertyType == typeof(Delegate))
1792+
&& instancePropertyInfo.GetValue(obj) is Delegate del)
17891793
{
1790-
stack.Push((instancePropertyInfo.GetValue(obj) as Delegate).DynamicInvoke(oArgs.ToArray()));
1794+
stack.Push(del.DynamicInvoke(oArgs.ToArray()));
17911795
}
17921796
else
17931797
{
@@ -1813,9 +1817,10 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
18131817
stack.Push(methodInfo.Invoke(isExtention ? null : obj, oArgs.ToArray()));
18141818
}
18151819
else if (objType.GetProperty(varFuncName, StaticBindingFlag) is PropertyInfo staticPropertyInfo
1816-
&& (staticPropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || staticPropertyInfo.PropertyType == typeof(Delegate)))
1820+
&& (staticPropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || staticPropertyInfo.PropertyType == typeof(Delegate))
1821+
&& staticPropertyInfo.GetValue(obj) is Delegate del2)
18171822
{
1818-
stack.Push((staticPropertyInfo.GetValue(obj) as Delegate).DynamicInvoke(oArgs.ToArray()));
1823+
stack.Push(del2.DynamicInvoke(oArgs.ToArray()));
18191824
}
18201825
else
18211826
{

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ It is largely based on and inspired by the following resources [this post on st
5252
* [DynamicExpresso](https://github.com/davideicardi/DynamicExpresso/)
5353
* [Flee](https://github.com/mparlak/Flee)
5454
* [CS-Script](https://github.com/oleg-shilo/cs-script) Best alternative (I use it some times) -> Real C# scripts better than ExpressionEvaluator (But everything is compiled. Read the doc. Execution is faster but compilation can make it very slow. And if not done the right way, it can lead to [memory leaks](https://en.wikipedia.org/wiki/Memory_leak))
55-
* [Roslyn](https://github.com/dotnet/roslyn) The Microsoft official solution
55+
* [Roslyn](https://github.com/dotnet/roslyn) The Microsoft official solution (For scripting [see](https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples))
5656

5757
### Commercial
5858
* [Eval Expression.NET](http://eval-expression.net/)

TryWindow/MainWindow.xaml.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e)
4545
evaluator.Namespaces.Add("System.Diagnostics");
4646

4747
evaluator.EvaluateVariable += Evaluator_EvaluateVariable;
48+
evaluator.EvaluateFunction += Evaluator_EvaluateFunction;
4849

4950
Stopwatch stopWatch = new Stopwatch();
5051

@@ -55,7 +56,7 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e)
5556
Exception exception = null;
5657
cancellationTokenSource = new CancellationTokenSource();
5758
cancellationTokenSource.Token.ThrowIfCancellationRequested();
58-
string result = await Task.Run<string>(() =>
59+
string result = await Task.Run(() =>
5960
{
6061
if (!int.TryParse(sIteration, out int iterations))
6162
iterations = 1;
@@ -104,6 +105,11 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e)
104105
CancelButton.IsEnabled = false;
105106
}
106107

108+
private void Evaluator_EvaluateFunction(object sender, FunctionEvaluationEventArg e)
109+
{
110+
111+
}
112+
107113
private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventArg e)
108114
{
109115
if (e.This != null)
@@ -120,6 +126,10 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr
120126
{
121127
e.Value = JsonConvert.SerializeObject(e.This.GetType().GetProperties().Select(m => m.Name));
122128
}
129+
else if (e.Name.Equals("FieldsNames"))
130+
{
131+
e.Value = JsonConvert.SerializeObject(e.This.GetType().GetFields().Select(m => m.Name));
132+
}
123133
}
124134
}
125135

0 commit comments

Comments
 (0)