Skip to content

Commit 67618d6

Browse files
committed
Merge branch 'chainedUnaryOperators' into dev
2 parents 1307602 + 3565d9b commit 67618d6

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 54 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");
@@ -2146,6 +2151,55 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg
21462151
.SetCategory("inherits ExpressionEvaluator")
21472152
.SetCategory("Custom operators");
21482153

2154+
yield return new TestCaseData(xExpressionEvaluator2
2155+
, "2##", null)
2156+
.Returns(1.4142135623730952d)
2157+
.SetCategory("ExpressionEvaluator extend")
2158+
.SetCategory("inherits ExpressionEvaluator")
2159+
.SetCategory("Custom operators");
2160+
2161+
yield return new TestCaseData(xExpressionEvaluator2
2162+
, "2## + 1", null)
2163+
.Returns(2.4142135623730949d)
2164+
.SetCategory("ExpressionEvaluator extend")
2165+
.SetCategory("inherits ExpressionEvaluator")
2166+
.SetCategory("Custom operators");
2167+
2168+
yield return new TestCaseData(xExpressionEvaluator2
2169+
, "2## + +-+-~+1", null)
2170+
.Returns(-0.58578643762690485d)
2171+
.SetCategory("ExpressionEvaluator extend")
2172+
.SetCategory("inherits ExpressionEvaluator")
2173+
.SetCategory("Custom operators");
2174+
2175+
yield return new TestCaseData(xExpressionEvaluator2
2176+
, "2#°", null)
2177+
.Returns(0.70710678118654757d)
2178+
.SetCategory("ExpressionEvaluator extend")
2179+
.SetCategory("inherits ExpressionEvaluator")
2180+
.SetCategory("Custom operators");
2181+
2182+
yield return new TestCaseData(xExpressionEvaluator2
2183+
, "2°#", null)
2184+
.Returns(0.00390625d)
2185+
.SetCategory("ExpressionEvaluator extend")
2186+
.SetCategory("inherits ExpressionEvaluator")
2187+
.SetCategory("Custom operators");
2188+
2189+
yield return new TestCaseData(xExpressionEvaluator2
2190+
, "2#° + +-+-~+1", null)
2191+
.Returns(-1.2928932188134525d)
2192+
.SetCategory("ExpressionEvaluator extend")
2193+
.SetCategory("inherits ExpressionEvaluator")
2194+
.SetCategory("Custom operators");
2195+
2196+
yield return new TestCaseData(xExpressionEvaluator2
2197+
, "2°# + +-+-~+1", null)
2198+
.Returns(-1.99609375d)
2199+
.SetCategory("ExpressionEvaluator extend")
2200+
.SetCategory("inherits ExpressionEvaluator")
2201+
.SetCategory("Custom operators");
2202+
21492203
yield return new TestCaseData(xExpressionEvaluator2
21502204
, "1 love 2", null)
21512205
.Returns(6)

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/XExpressionEvaluator2.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public class XExpressionEvaluator2 : ExpressionEvaluator
99
protected new static readonly IList<ExpressionOperator> leftOperandOnlyOperatorsEvaluationDictionary =
1010
ExpressionEvaluator.leftOperandOnlyOperatorsEvaluationDictionary
1111
.ToList()
12-
.FluidAdd(XExpressionOperator2.Sharp);
12+
.FluidAdd(XExpressionOperator2.Sharp)
13+
.FluidAdd(XExpressionOperator2.Degree);
1314

1415
//protected new static readonly IList<ExpressionOperator> rightOperandOnlyOperatorsEvaluationDictionary =
1516
// ExpressionEvaluator.rightOperandOnlyOperatorsEvaluationDictionary
@@ -18,7 +19,8 @@ public class XExpressionEvaluator2 : ExpressionEvaluator
1819
protected new static readonly IList<IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>> operatorsEvaluations =
1920
ExpressionEvaluator.operatorsEvaluations
2021
.Copy()
21-
.AddOperatorEvaluationAtNewLevelAfter(XExpressionOperator2.Sharp, (left, _) => Math.Pow(left, -left), ExpressionOperator.UnaryPlus)
22+
.AddOperatorEvaluationAtNewLevelAfter(XExpressionOperator2.Sharp, (left, _) => Math.Pow(left, -left), ExpressionOperator.Cast)
23+
.AddOperatorEvaluationAtNewLevelAfter(XExpressionOperator2.Degree, (left, _) => Math.Pow(left, left), ExpressionOperator.Cast)
2224
.AddOperatorEvaluationAtLevelOf(XExpressionOperator2.Love, (left, right) => (left | right) << 1, ExpressionOperator.ShiftBitsLeft);
2325

2426
protected override IList<ExpressionOperator> LeftOperandOnlyOperatorsEvaluationDictionary => leftOperandOnlyOperatorsEvaluationDictionary;
@@ -30,6 +32,7 @@ public class XExpressionEvaluator2 : ExpressionEvaluator
3032
protected override void Init()
3133
{
3234
operatorsDictionary.Add("#", XExpressionOperator2.Sharp);
35+
operatorsDictionary.Add("°", XExpressionOperator2.Degree);
3336
operatorsDictionary.Add("love", XExpressionOperator2.Love);
3437
operatorsDictionary.Add("Not", ExpressionOperator.LogicalNegation);
3538
}
@@ -38,6 +41,7 @@ protected override void Init()
3841
public class XExpressionOperator2 : ExpressionOperator
3942
{
4043
public static readonly ExpressionOperator Sharp = new XExpressionOperator2();
44+
public static readonly ExpressionOperator Degree = new XExpressionOperator2();
4145
public static readonly ExpressionOperator Love = new XExpressionOperator2();
4246
}
4347
}

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 36 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 previousOp && !LeftOperandOnlyOperatorsEvaluationDictionary.Contains(previousOp))))
2582+
stack.Push(ExpressionOperator.UnaryPlus);
2583+
else if (op.Equals("-") && (stack.Count == 0 || (stack.Peek() is ExpressionOperator previousOp2 && !LeftOperandOnlyOperatorsEvaluationDictionary.Contains(previousOp2))))
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)
@@ -2978,6 +2999,20 @@ protected virtual object ProcessStack(Stack<object> stack)
29782999
{
29793000
try
29803001
{
3002+
void EvaluateFirstPreviousUnaryOp(int j)
3003+
{
3004+
if (j < list.Count - 1 && list[j] is ExpressionOperator previousOp && LeftOperandOnlyOperatorsEvaluationDictionary.Contains(previousOp))
3005+
{
3006+
EvaluateFirstPreviousUnaryOp(j + 1);
3007+
3008+
list[j] = OperatorsEvaluations.FirstOrDefault(od => od.ContainsKey(previousOp))?[previousOp]((dynamic)list[j + 1], null);
3009+
3010+
list.RemoveAt(j + 1);
3011+
}
3012+
}
3013+
3014+
EvaluateFirstPreviousUnaryOp(i + 1);
3015+
29813016
list[i] = operatorEvalutationsDict[eOp]((dynamic)list[i + 1], null);
29823017
}
29833018
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)