Skip to content

Dev #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 16, 2020
Merged

Dev #54

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ public void TypeTesting(string expression, Type type)
[TestCase("Abs(-4) > 10 / 2 ? Abs(-3) : (Abs(-4) + 4) / 2", ExpectedResult = 4, Category = "Conditional Operator t ? x : y")]
[TestCase("Abs(-4) < 10 / 2 ? (true ? 6 : 3+2) : (false ? Abs(-18) : 100 / 2)", ExpectedResult = 6, Category = "Conditional Operator t ? x : y")]
[TestCase("Abs(-4) > 10 / 2 ? (true ? 6 : 3+2) : (false ? Abs(-18) : 100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")]
[TestCase("Abs(-4) > 10 / 2?(true ? 6 : 3+2):(false?Abs(-18):100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")]
[TestCase("1==1?true:false", ExpectedResult = true, Category = "Conditional Operator t ? x : y")]
#endregion

#region Math Constants
Expand Down Expand Up @@ -1773,12 +1775,13 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg
.Returns("Hello Bob")
.SetCategory("Context object");

ExpressionEvaluator evaluatorWithPersonContext = new ExpressionEvaluator();

evaluatorWithPersonContext.Context = new Person1()
ExpressionEvaluator evaluatorWithPersonContext = new ExpressionEvaluator
{
name = "John",
LastName = "Smith"
Context = new Person1()
{
name = "John",
LastName = "Smith"
}
};

yield return new TestCaseData(evaluatorWithPersonContext
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.10.0</Version>
<AssemblyVersion>1.4.10.0</AssemblyVersion>
<FileVersion>1.4.10.0</FileVersion>
<Version>1.4.11.0</Version>
<AssemblyVersion>1.4.11.0</AssemblyVersion>
<FileVersion>1.4.11.0</FileVersion>
<OutputPath>bin\$(Configuration)\</OutputPath>
<Authors>Coding Seb</Authors>
<PackageId>CodingSeb.ExpressionEvaluator</PackageId>
Expand All @@ -18,7 +18,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>* Correction of some bugs linked to nullconditional operator</PackageReleaseNotes>
<PackageReleaseNotes>* Correction of Ternary conditional operator bug</PackageReleaseNotes>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<RepositoryUrl>https://github.com/codingseb/ExpressionEvaluator</RepositoryUrl>
</PropertyGroup>
Expand Down
35 changes: 17 additions & 18 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.10.0
Version : 1.4.11.0
(if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)

Author : Coding Seb
Expand All @@ -22,7 +22,7 @@
namespace CodingSeb.ExpressionEvaluator
{
/// <summary>
/// This class allow to evaluate a string math or pseudo C# expression
/// This class allow to evaluate a string math or pseudo C# expression
/// </summary>
public partial class ExpressionEvaluator
{
Expand Down Expand Up @@ -653,8 +653,8 @@ public string OptionNumberParsingThousandSeparator
public bool OptionFluidPrefixingActive { get; set; } = true;

/// <summary>
/// if <c>true</c> allow the use of inline namespace (Can be slow, and is less secure).
/// if <c>false</c> unactive inline namespace (only namespaces in Namespaces list are available).
/// if <c>true</c> allow the use of inline namespace (Can be slow, and is less secure).
/// if <c>false</c> unactive inline namespace (only namespaces in Namespaces list are available).
/// By default : true
/// </summary>
public bool OptionInlineNamespacesEvaluationActive { get; set; } = true;
Expand Down Expand Up @@ -744,7 +744,7 @@ public bool OptionNewFunctionEvaluationActive

/// <summary>
/// If <c>true</c> Evaluate function is callables in an expression. If <c>false</c> Evaluate is not callable.
/// By default : true
/// By default : true
/// if set to false for security (also ensure that ExpressionEvaluator type is in TypesToBlock list)
/// </summary>
public bool OptionEvaluateFunctionActive { get; set; } = true;
Expand Down Expand Up @@ -772,7 +772,7 @@ public bool OptionNewFunctionEvaluationActive

/// <summary>
/// If <c>true</c> ScriptEvaluate function is callables in an expression. If <c>false</c> Evaluate is not callable.
/// By default : true
/// By default : true
/// if set to false for security (also ensure that ExpressionEvaluator type is in TypesToBlock list)
/// </summary>
public bool OptionScriptEvaluateFunctionActive { get; set; } = true;
Expand Down Expand Up @@ -1394,7 +1394,7 @@ void forAction(int index)

bool executed = false;

if (TryParseStringAndParenthisAndCurlyBrackets(ref i)){}
if (TryParseStringAndParenthisAndCurlyBrackets(ref i)) { }
else if (script.Length - i > 2 && script.Substring(i, 3).Equals("';'"))
{
i += 2;
Expand Down Expand Up @@ -1454,7 +1454,7 @@ public T Evaluate<T>(string expression)

private IList<ParsingMethodDelegate> parsingMethods;

protected virtual IList<ParsingMethodDelegate> ParsingMethods => parsingMethods ?? (parsingMethods = new List<ParsingMethodDelegate>()
protected virtual IList<ParsingMethodDelegate> ParsingMethods => parsingMethods ?? (parsingMethods = new List<ParsingMethodDelegate>()
{
EvaluateCast,
EvaluateNumber,
Expand Down Expand Up @@ -1809,7 +1809,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,

try
{
if(obj is NullConditionalNullValue)
if (obj is NullConditionalNullValue)
{
stack.Push(obj);
}
Expand Down Expand Up @@ -1851,7 +1851,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
{
if (dictionaryObject[varFuncName] is InternalDelegate internalDelegate)
stack.Push(internalDelegate(oArgs.ToArray()));
else if(dictionaryObject[varFuncName] is Delegate del)
else if (dictionaryObject[varFuncName] is Delegate del)
stack.Push(del.DynamicInvoke(oArgs.ToArray()));
}
else if (objType.GetProperty(varFuncName, InstanceBindingFlag) is PropertyInfo instancePropertyInfo
Expand Down Expand Up @@ -2250,7 +2250,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
{
if (Variables.ContainsKey(varFuncName) && Variables[varFuncName] is StronglyTypedVariable stronglyTypedVariable)
{
if(cusVarValueToPush == null && stronglyTypedVariable.Type.IsValueType && Nullable.GetUnderlyingType(stronglyTypedVariable.Type) == null)
if (cusVarValueToPush == null && stronglyTypedVariable.Type.IsValueType && Nullable.GetUnderlyingType(stronglyTypedVariable.Type) == null)
{
throw new ExpressionEvaluatorSyntaxErrorException($"Can not cast null to {stronglyTypedVariable.Type} because it's not a nullable valueType");
}
Expand Down Expand Up @@ -2417,11 +2417,11 @@ protected virtual bool EvaluateOperators(string expression, Stack<object> stack,

Match match = Regex.Match(expression.Substring(i), regexPattern, optionCaseSensitiveEvaluationActive ? RegexOptions.None : RegexOptions.IgnoreCase);

if(match.Success)
if (match.Success)
{
string op = match.Value;
stack.Push(operatorsDictionary[op]);
i+= op.Length - 1;
i += op.Length - 1;
return true;
}

Expand Down Expand Up @@ -2456,7 +2456,7 @@ protected virtual bool EvaluateTernaryConditionalOperator(string expression, Sta
{
stack.Clear();

stack.Push(condition ? Evaluate(restOfExpression.Substring(1, j - 1)) : Evaluate(restOfExpression.Substring(j + 1)));
stack.Push(condition ? Evaluate(restOfExpression.Substring(0, j)) : Evaluate(restOfExpression.Substring(j + 1)));

i = expression.Length;

Expand Down Expand Up @@ -2572,7 +2572,6 @@ protected virtual bool EvaluateIndexing(string expression, Stack<object> stack,

dynamic right = Evaluate(innerExp.ToString());
ExpressionOperator op = indexingBeginningMatch.Length == 2 ? ExpressionOperator.IndexingWithNullConditional : ExpressionOperator.Indexing;


if (OptionForceIntegerNumbersEvaluationsAsDoubleByDefault && right is double && Regex.IsMatch(innerExp.ToString(), @"^\d+$"))
right = (int)right;
Expand Down Expand Up @@ -2995,7 +2994,7 @@ protected virtual MethodInfo GetRealMethod(ref Type type, ref object obj, string
.MakeGenericMethod(parameterType.GetGenericArguments());
modifiedArgs[a] = Delegate.CreateDelegate(parameterType, de, encapsMethod);
}
else if(paramTypeName.StartsWith("Action")
else if (paramTypeName.StartsWith("Action")
&& modifiedArgs[a] is InternalDelegate)
{
InternalDelegate led = modifiedArgs[a] as InternalDelegate;
Expand Down Expand Up @@ -3047,7 +3046,7 @@ protected virtual MethodInfo MakeConcreteMethodIfGeneric(MethodInfo methodInfo,
{
if (genericsTypes.Equals(string.Empty))
{
if(inferedGenericsTypes != null && inferedGenericsTypes.Length == methodInfo.GetGenericArguments().Length)
if (inferedGenericsTypes != null && inferedGenericsTypes.Length == methodInfo.GetGenericArguments().Length)
{
return methodInfo.MakeGenericMethod(inferedGenericsTypes);
}
Expand Down Expand Up @@ -3722,7 +3721,7 @@ public override int GetHashCode()

public bool Equals(ExpressionOperator otherOperator)
{
return otherOperator!= null && OperatorValue == otherOperator.OperatorValue;
return otherOperator != null && OperatorValue == otherOperator.OperatorValue;
}
}

Expand Down
10 changes: 4 additions & 6 deletions TryWindow/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace TryWindow
/// </summary>
public partial class MainWindow : Window
{
private string persistCodeFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "code.cs");
private string persistIterationFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "iterations");
private readonly string persistCodeFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "code.cs");
private readonly string persistIterationFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "iterations");

private CancellationTokenSource cancellationTokenSource = null;

Expand Down Expand Up @@ -88,7 +88,7 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e)
{
stopWatch.Stop();
}
}, cancellationTokenSource.Token);
}, cancellationTokenSource.Token).ConfigureAwait(true);

if (exception == null)
ResultTextBlock.Text = result;
Expand All @@ -110,7 +110,6 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e)

private void Evaluator_EvaluateFunction(object sender, FunctionEvaluationEventArg e)
{

}

private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventArg e)
Expand Down Expand Up @@ -138,8 +137,7 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
if (cancellationTokenSource != null)
cancellationTokenSource.Cancel();
cancellationTokenSource?.Cancel();
}

private void ScriptTextBox_TextChanged(object sender, EventArgs e)
Expand Down