Skip to content

Commit 47abd15

Browse files
committed
OK for unary left operand. But test failed when right operator is in the place :(
1 parent 1307602 commit 47abd15

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
10221022
Dictionary<string, object> variablesForSimpleVariablesInjection = new Dictionary<string, object>()
10231023
{
10241024
{ "hello", "Test" },
1025+
{ "a", 0 },
10251026
{ "x", 5 },
10261027
{ "y", 20 },
10271028
{ "isThisReal", true },
@@ -1056,6 +1057,10 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
10561057
yield return new TestCaseData("-x + +y", variablesForSimpleVariablesInjection, true).SetCategory("SimpleVariablesInjection,Unary both +-").Returns(15);
10571058
yield return new TestCaseData("(-x + +y)", variablesForSimpleVariablesInjection, true).SetCategory("SimpleVariablesInjection,Unary both +-,Parenthis").Returns(15);
10581059

1060+
yield return new TestCaseData("-~a", variablesForSimpleVariablesInjection, true).SetCategory("SimpleVariablesInjection,MultipleUnary").Returns(1);
1061+
yield return new TestCaseData("+-+-+-+-+a", variablesForSimpleVariablesInjection, true).SetCategory("SimpleVariablesInjection,MultipleUnary").Returns(0);
1062+
yield return new TestCaseData("a >> +-+-+-+2 << +-+-+-+-2 >> +-+-+-+-+2 << +-+-+-+-+2", variablesForSimpleVariablesInjection, true).SetCategory("SimpleVariablesInjection,MultipleUnary").Returns(0);
1063+
10591064
yield return new TestCaseData("ISTHISREAL", variablesForSimpleVariablesInjection, false).SetCategory("SimpleVariablesInjection,IgnoreCase").Returns(true).SetCategory("Options, OptionCaseSensitiveEvaluationActive");
10601065
yield return new TestCaseData("isthisreal", variablesForSimpleVariablesInjection, false).SetCategory("SimpleVariablesInjection,IgnoreCase").Returns(true).SetCategory("Options, OptionCaseSensitiveEvaluationActive");
10611066
yield return new TestCaseData("iStHISrEAL", variablesForSimpleVariablesInjection, false).SetCategory("SimpleVariablesInjection,IgnoreCase").Returns(true).SetCategory("Options, OptionCaseSensitiveEvaluationActive");

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2577,7 +2577,13 @@ protected virtual bool EvaluateOperators(string expression, Stack<object> stack,
25772577
if (match.Success)
25782578
{
25792579
string op = match.Value;
2580-
stack.Push(operatorsDictionary[op]);
2580+
2581+
if (op.Equals("+") && (stack.Count == 0 || stack.Peek() is ExpressionOperator))
2582+
stack.Push(ExpressionOperator.UnaryPlus);
2583+
else if (op.Equals("-") && (stack.Count == 0 || stack.Peek() is ExpressionOperator))
2584+
stack.Push(ExpressionOperator.UnaryMinus);
2585+
else
2586+
stack.Push(operatorsDictionary[op]);
25812587
i += op.Length - 1;
25822588
return true;
25832589
}
@@ -2957,6 +2963,21 @@ protected virtual object ProcessStack(Stack<object> stack)
29572963
{
29582964
try
29592965
{
2966+
void EvaluateFirstNextUnaryOp(int j, ref int parentIndex)
2967+
{
2968+
if (j > 0 && list[j] is ExpressionOperator nextOp && RightOperandOnlyOperatorsEvaluationDictionary.Contains(nextOp))
2969+
{
2970+
EvaluateFirstNextUnaryOp(j - 1, ref j);
2971+
2972+
list[j] = OperatorsEvaluations.FirstOrDefault(od => od.ContainsKey(nextOp))?[nextOp](null, (dynamic)list[j - 1]);
2973+
2974+
list.RemoveAt(j - 1);
2975+
parentIndex=j;
2976+
}
2977+
}
2978+
2979+
EvaluateFirstNextUnaryOp(i - 1, ref i);
2980+
29602981
list[i] = operatorEvalutationsDict[eOp](null, (dynamic)list[i - 1]);
29612982
}
29622983
catch (Exception ex)

TryWindow/MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class MainWindow : Window
1919
private readonly string persistCodeFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "code.cs");
2020
private readonly string persistIterationFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "iterations");
2121

22-
private CancellationTokenSource cancellationTokenSource = null;
22+
private CancellationTokenSource cancellationTokenSource;
2323

2424
public MainWindow()
2525
{

0 commit comments

Comments
 (0)