Skip to content

Commit 547b293

Browse files
committed
refactor evaluator objects, much simpler evaluation now
1 parent bd0b551 commit 547b293

32 files changed

+140
-192
lines changed

JankSQL/Engines/TestTable.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{
33
using System.Collections.Immutable;
44

5+
using JankSQL.Expressions;
6+
57
public class TestTable
68
{
79
private readonly Tuple[] rows;

JankSQL/Engines/TestTableBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace JankSQL.Engines
22
{
3+
using JankSQL.Expressions;
4+
35
public class TestTableBuilder
46
{
57
private readonly List<FullColumnName> columnNames = new ();

JankSQL/Expressions/Expression.cs

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -65,87 +65,7 @@ internal ExpressionOperand Evaluate(IRowValueAccessor? accessor, Engines.IEngine
6565
do
6666
{
6767
foreach (ExpressionNode n in this)
68-
{
69-
if (n is ExpressionOperand expressionOperand)
70-
stack.Push(expressionOperand);
71-
else if (n is ExpressionOperator expressionOperator)
72-
{
73-
// it's an operator
74-
ExpressionOperand r = expressionOperator.Evaluate(stack);
75-
stack.Push(r);
76-
}
77-
else if (n is ExpressionFunction expressionFunction)
78-
{
79-
// a function to evaluate
80-
ExpressionOperand r = expressionFunction.Evaluate(stack);
81-
stack.Push(r);
82-
}
83-
else if (n is ExpressionOperandFromColumn columnOperand)
84-
{
85-
// value from a column
86-
if (accessor == null)
87-
throw new ExecutionException($"Not in a row context to evaluate {this}");
88-
stack.Push(accessor.GetValue(columnOperand.ColumnName));
89-
}
90-
else if (n is ExpressionComparisonOperator comparisonOperator)
91-
{
92-
// comparison operator
93-
ExpressionOperand r = comparisonOperator.Evaluate(stack);
94-
stack.Push(r);
95-
}
96-
else if (n is ExpressionIsNullOperator nullOperator)
97-
{
98-
// nullness operator
99-
ExpressionOperand r = nullOperator.Evaluate(stack);
100-
stack.Push(r);
101-
}
102-
else if (n is ExpressionBooleanOperator booleanOperator)
103-
{
104-
// AND, OR, ...
105-
ExpressionOperand r = booleanOperator.Evaluate(stack);
106-
stack.Push(r);
107-
}
108-
else if (n is ExpressionBetweenOperator betweenOperator)
109-
{
110-
// a [NOT] BETWEEN b AND c
111-
ExpressionOperand r = betweenOperator.Evaluate(stack);
112-
stack.Push(r);
113-
}
114-
else if (n is ExpressionCaseOperator caseOperator)
115-
{
116-
// CASE (input) WHEN (expr) THEN (expr) ... [ELSE (expr)]
117-
// or
118-
// CASE WHEN (predicate) THEN (expr) ... [ELSE (expr)]
119-
if (accessor == null)
120-
throw new ExecutionException($"Not in a row context to evaluate {this}");
121-
ExpressionOperand r = caseOperator.Evaluate(engine, accessor, stack, bindValues);
122-
stack.Push(r);
123-
}
124-
else if (n is ExpressionSubselectOperator subselectOperator)
125-
{
126-
if (accessor == null)
127-
throw new ExecutionException($"Not in a row context to evaluate {this}");
128-
ExpressionOperand r = subselectOperator.Evaluate(engine, accessor, stack, bindValues);
129-
stack.Push(r);
130-
}
131-
else if (n is ExpressionInOperator inOperator)
132-
{
133-
if (accessor == null)
134-
throw new ExecutionException($"Not in a row context to evaluate {this}");
135-
ExpressionOperand r = inOperator.Evaluate(engine, accessor, stack, bindValues);
136-
stack.Push(r);
137-
}
138-
else if (n is ExpressionBindOperator bindOperator)
139-
{
140-
ExpressionOperand r = bindOperator.Evaluate(stack, bindValues);
141-
stack.Push(r);
142-
}
143-
else
144-
{
145-
throw new InvalidOperationException($"Not prepared for ExpressionNode {n}");
146-
}
147-
148-
}
68+
n.Evaluate(engine, accessor, stack, bindValues);
14969
}
15070
while (stack.Count > 1);
15171

JankSQL/Expressions/ExpressionAssignmentOperator.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

JankSQL/Expressions/ExpressionBetweenOperator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public override string ToString()
1414
return isNotBetween ? "NOT BETWEEN" : "BETWEEN";
1515
}
1616

17-
internal ExpressionOperand Evaluate(Stack<ExpressionOperand> stack)
17+
internal override void Evaluate(Engines.IEngine engine, IRowValueAccessor? accessor, Stack<ExpressionOperand> stack, Dictionary<string, ExpressionOperand> bindValues)
1818
{
1919
bool result;
2020
ExpressionOperand right = stack.Pop();
@@ -26,7 +26,7 @@ internal ExpressionOperand Evaluate(Stack<ExpressionOperand> stack)
2626
if (!isNotBetween)
2727
result = !result;
2828

29-
return new ExpressionOperandBoolean(result);
29+
stack.Push(new ExpressionOperandBoolean(result));
3030
}
3131
}
3232
}

JankSQL/Expressions/ExpressionBindOperator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public override string ToString()
1515
return $"BindValue({targetName}";
1616
}
1717

18-
internal ExpressionOperand Evaluate(Stack<ExpressionOperand> stack, Dictionary<string, ExpressionOperand> bindValues)
18+
internal override void Evaluate(Engines.IEngine engine, IRowValueAccessor? accessor, Stack<ExpressionOperand> stack, Dictionary<string, ExpressionOperand> bindValues)
1919
{
2020
if (!bindValues.TryGetValue(targetName, out bindValue))
2121
throw new SemanticErrorException($"Bind target {targetName} was not bound");
2222

23-
return bindValue;
23+
stack.Push(bindValue);
2424
}
2525
}
2626
}

JankSQL/Expressions/ExpressionBooleanOperator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal static ExpressionBooleanOperator GetNotOperator()
3636
return new ExpressionBooleanOperator(BooleanOperatorType.NOT);
3737
}
3838

39-
internal ExpressionOperand Evaluate(Stack<ExpressionOperand> stack)
39+
internal override void Evaluate(Engines.IEngine engine, IRowValueAccessor? accessor, Stack<ExpressionOperand> stack, Dictionary<string, ExpressionOperand> bindValues)
4040
{
4141
bool result = true;
4242

@@ -59,7 +59,7 @@ internal ExpressionOperand Evaluate(Stack<ExpressionOperand> stack)
5959
break;
6060
}
6161

62-
return new ExpressionOperandBoolean(result);
62+
stack.Push(new ExpressionOperandBoolean(result));
6363
}
6464
}
6565
}

JankSQL/Expressions/ExpressionCaseOperator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ public override string ToString()
2121
return "CASE Operator";
2222
}
2323

24-
internal ExpressionOperand Evaluate(Engines.IEngine engine, IRowValueAccessor accessor, Stack<ExpressionOperand> stack, Dictionary<string, ExpressionOperand> bindValues)
24+
internal override void Evaluate(Engines.IEngine engine, IRowValueAccessor? accessor, Stack<ExpressionOperand> stack, Dictionary<string, ExpressionOperand> bindValues)
2525
{
26+
if (accessor == null)
27+
throw new ExecutionException($"Not in a row context to evaluate {this}");
28+
2629
ExpressionOperand? result = null;
2730

2831
// evaluate each when to find truth ...
@@ -48,7 +51,7 @@ internal ExpressionOperand Evaluate(Engines.IEngine engine, IRowValueAccessor ac
4851
}
4952

5053
// return what we discovered
51-
return result;
54+
stack.Push(result);
5255
}
5356
}
5457
}

JankSQL/Expressions/ExpressionComparisonOperator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public override string ToString()
1414
return str;
1515
}
1616

17-
internal ExpressionOperand Evaluate(Stack<ExpressionOperand> stack)
17+
internal override void Evaluate(Engines.IEngine engine, IRowValueAccessor? accessor, Stack<ExpressionOperand> stack, Dictionary<string, ExpressionOperand> bindValues)
1818
{
1919
bool result;
2020
ExpressionOperand right = stack.Pop();
@@ -35,7 +35,7 @@ internal ExpressionOperand Evaluate(Stack<ExpressionOperand> stack)
3535
else
3636
throw new NotImplementedException($"ExpressionComparisonOperator: no implementation for {str}");
3737

38-
return new ExpressionOperandBoolean(result);
38+
stack.Push(new ExpressionOperandBoolean(result));
3939
}
4040
}
4141
}

JankSQL/Expressions/ExpressionFunction.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ public bool Equals(ExpressionFunction? other)
5858
var r = FunctionDict[str].Invoke();
5959
return r;
6060
}
61-
62-
internal abstract ExpressionOperand Evaluate(Stack<ExpressionOperand> stack);
6361
}
6462
}
6563

0 commit comments

Comments
 (0)