Skip to content

Commit 3641b0b

Browse files
author
Andrin Meier
committed
move to expression ast for easier maintenance
Each IExpression represents a cc-expression. An expression produces an IValue. Each IValue knows how to let coerce itself to the target type.
1 parent e8f6b04 commit 3641b0b

File tree

77 files changed

+3160
-1875
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+3160
-1875
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
3+
namespace Rubberduck.Parsing.Preprocessing
4+
{
5+
public sealed class AbsLibraryFunctionExpression : Expression
6+
{
7+
private readonly IExpression _expression;
8+
9+
public AbsLibraryFunctionExpression(IExpression expression)
10+
{
11+
_expression = expression;
12+
}
13+
14+
public override IValue Evaluate()
15+
{
16+
var expr = _expression.Evaluate();
17+
if (expr == null)
18+
{
19+
return null;
20+
}
21+
if (expr.ValueType == ValueType.Date)
22+
{
23+
decimal exprValue = expr.AsDecimal;
24+
exprValue = Math.Abs(exprValue);
25+
try
26+
{
27+
return new DateValue(new DecimalValue(exprValue).AsDate);
28+
}
29+
catch
30+
{
31+
return new DecimalValue(exprValue);
32+
}
33+
}
34+
else
35+
{
36+
return new DecimalValue(Math.Abs(expr.AsDecimal));
37+
}
38+
}
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Rubberduck.Parsing.Preprocessing
2+
{
3+
public sealed class BinaryDivisionExpression : Expression
4+
{
5+
private readonly IExpression _left;
6+
private readonly IExpression _right;
7+
8+
public BinaryDivisionExpression(IExpression left, IExpression right)
9+
{
10+
_left = left;
11+
_right = right;
12+
}
13+
14+
public override IValue Evaluate()
15+
{
16+
var left = _left.Evaluate();
17+
var right = _right.Evaluate();
18+
if (left == null || right == null)
19+
{
20+
return null;
21+
}
22+
var leftValue = left.AsDecimal;
23+
var rightValue = right.AsDecimal;
24+
return new DecimalValue(leftValue / rightValue);
25+
}
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace Rubberduck.Parsing.Preprocessing
4+
{
5+
public sealed class BinaryIntDivExpression : Expression
6+
{
7+
private readonly IExpression _left;
8+
private readonly IExpression _right;
9+
10+
public BinaryIntDivExpression(IExpression left, IExpression right)
11+
{
12+
_left = left;
13+
_right = right;
14+
}
15+
16+
public override IValue Evaluate()
17+
{
18+
var left = _left.Evaluate();
19+
var right = _right.Evaluate();
20+
if (left == null || right == null)
21+
{
22+
return null;
23+
}
24+
var leftValue = Convert.ToInt64(left.AsDecimal);
25+
var rightValue = Convert.ToInt64(right.AsDecimal);
26+
return new DecimalValue(Math.Truncate((decimal)leftValue / rightValue));
27+
}
28+
}
29+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace Rubberduck.Parsing.Preprocessing
2+
{
3+
public sealed class BinaryMinusExpression : Expression
4+
{
5+
private readonly IExpression _left;
6+
private readonly IExpression _right;
7+
8+
public BinaryMinusExpression(IExpression left, IExpression right)
9+
{
10+
_left = left;
11+
_right = right;
12+
}
13+
14+
public override IValue Evaluate()
15+
{
16+
var left = _left.Evaluate();
17+
var right = _right.Evaluate();
18+
if (left == null || right == null)
19+
{
20+
return null;
21+
}
22+
else if (left.ValueType == ValueType.Date && right.ValueType == ValueType.Date)
23+
{
24+
// 5.6.9.3.3 - Effective value type exception.
25+
// If left + right are both Date then effective value type is double.
26+
decimal leftValue = left.AsDecimal;
27+
decimal rightValue = right.AsDecimal;
28+
decimal difference = leftValue - rightValue;
29+
return new DecimalValue(difference);
30+
}
31+
else if (left.ValueType == ValueType.Date || right.ValueType == ValueType.Date)
32+
{
33+
decimal leftValue = left.AsDecimal;
34+
decimal rightValue = right.AsDecimal;
35+
decimal difference = leftValue - rightValue;
36+
try
37+
{
38+
return new DateValue(new DecimalValue(difference).AsDate);
39+
}
40+
catch
41+
{
42+
return new DecimalValue(difference);
43+
}
44+
}
45+
else
46+
{
47+
decimal leftValue = left.AsDecimal;
48+
decimal rightValue = right.AsDecimal;
49+
return new DecimalValue(leftValue - rightValue);
50+
}
51+
}
52+
}
53+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace Rubberduck.Parsing.Preprocessing
4+
{
5+
public sealed class BinaryMultiplicationExpression : Expression
6+
{
7+
private readonly IExpression _left;
8+
private readonly IExpression _right;
9+
10+
public BinaryMultiplicationExpression(IExpression left, IExpression right)
11+
{
12+
_left = left;
13+
_right = right;
14+
}
15+
16+
public override IValue Evaluate()
17+
{
18+
var left = _left.Evaluate();
19+
var right = _right.Evaluate();
20+
if (left == null || right == null)
21+
{
22+
return null;
23+
}
24+
var leftValue = left.AsDecimal;
25+
var rightValue = right.AsDecimal;
26+
return new DecimalValue(leftValue * rightValue);
27+
}
28+
}
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace Rubberduck.Parsing.Preprocessing
2+
{
3+
public sealed class BinaryPlusExpression : Expression
4+
{
5+
private readonly IExpression _left;
6+
private readonly IExpression _right;
7+
8+
public BinaryPlusExpression(IExpression left, IExpression right)
9+
{
10+
_left = left;
11+
_right = right;
12+
}
13+
14+
public override IValue Evaluate()
15+
{
16+
var left = _left.Evaluate();
17+
var right = _right.Evaluate();
18+
if (left == null || right == null)
19+
{
20+
return null;
21+
}
22+
if (left.ValueType == ValueType.String)
23+
{
24+
return new StringValue(left.AsString+ right.AsString);
25+
}
26+
else if (left.ValueType == ValueType.Date || right.ValueType == ValueType.Date)
27+
{
28+
decimal leftValue = left.AsDecimal;
29+
decimal rightValue = right.AsDecimal;
30+
decimal sum = leftValue + rightValue;
31+
try
32+
{
33+
return new DateValue(new DecimalValue(sum).AsDate);
34+
}
35+
catch
36+
{
37+
return new DecimalValue(sum);
38+
}
39+
}
40+
else
41+
{
42+
var leftNumber = left.AsDecimal;
43+
var rightNumber = right.AsDecimal;
44+
return new DecimalValue(leftNumber + rightNumber);
45+
}
46+
}
47+
}
48+
}

Rubberduck.Parsing/Preprocessing/BoolLetCoercion.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
3+
namespace Rubberduck.Parsing.Preprocessing
4+
{
5+
public sealed class BoolValue : IValue
6+
{
7+
private readonly bool _value;
8+
9+
public BoolValue(bool value)
10+
{
11+
_value = value;
12+
}
13+
14+
public ValueType ValueType
15+
{
16+
get
17+
{
18+
return ValueType.Bool;
19+
}
20+
}
21+
22+
public bool AsBool
23+
{
24+
get
25+
{
26+
return _value;
27+
}
28+
}
29+
30+
public byte AsByte
31+
{
32+
get
33+
{
34+
if (_value)
35+
{
36+
return 255;
37+
}
38+
return 0;
39+
}
40+
}
41+
42+
public DateTime AsDate
43+
{
44+
get
45+
{
46+
return new DecimalValue(AsDecimal).AsDate;
47+
}
48+
}
49+
50+
public decimal AsDecimal
51+
{
52+
get
53+
{
54+
if (_value)
55+
{
56+
return -1;
57+
}
58+
else
59+
{
60+
return 0;
61+
}
62+
}
63+
}
64+
65+
public string AsString
66+
{
67+
get
68+
{
69+
if (_value)
70+
{
71+
return "True";
72+
}
73+
else
74+
{
75+
return "False";
76+
}
77+
}
78+
}
79+
80+
public override string ToString()
81+
{
82+
return _value.ToString();
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)