1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version>$Revision: 975 $</version>
// </file>
using System;
using System.Collections;
namespace ICSharpCode.NRefactory.Parser.AST
{
public abstract class Expression : AbstractNode, INullable
{
public static NullExpression Null {
get {
return NullExpression.Instance;
}
}
public virtual bool IsNull {
get {
return false;
}
}
public static Expression CheckNull(Expression expression)
{
return expression == null ? NullExpression.Instance : expression;
}
/// <summary>
/// Returns the existing expression plus the specified integer value.
/// WARNING: This method modifies <paramref name="expr"/> and possibly returns <paramref name="expr"/>
/// again, but it might also create a new expression around <paramref name="expr"/>.
/// </summary>
public static Expression AddInteger(Expression expr, int value)
{
PrimitiveExpression pe = expr as PrimitiveExpression;
if (pe != null && pe.Value is int) {
int newVal = (int)pe.Value + value;
return new PrimitiveExpression(newVal, newVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
}
BinaryOperatorExpression boe = expr as BinaryOperatorExpression;
if (boe != null && boe.Op == BinaryOperatorType.Add) {
boe.Right = AddInteger(boe.Right, value);
if (boe.Right is PrimitiveExpression && ((PrimitiveExpression)boe.Right).Value is int) {
int newVal = (int)((PrimitiveExpression)boe.Right).Value;
if (newVal == 0) {
return boe.Left;
} else if (newVal < 0) {
((PrimitiveExpression)boe.Right).Value = -newVal;
boe.Op = BinaryOperatorType.Subtract;
}
}
return boe;
}
if (boe != null && boe.Op == BinaryOperatorType.Subtract) {
pe = boe.Right as PrimitiveExpression;
if (pe != null && pe.Value is int) {
int newVal = (int)pe.Value - value;
if (newVal == 0)
return boe.Left;
if (newVal < 0) {
newVal = -newVal;
boe.Op = BinaryOperatorType.Add;
}
boe.Right = new PrimitiveExpression(newVal, newVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
return boe;
}
}
BinaryOperatorType opType = BinaryOperatorType.Add;
if (value < 0) {
value = -value;
opType = BinaryOperatorType.Subtract;
}
return new BinaryOperatorExpression(expr, opType, new PrimitiveExpression(value, value.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)));
}
}
public class NullExpression : Expression
{
static NullExpression nullExpression = new NullExpression();
public override bool IsNull {
get {
return true;
}
}
public static NullExpression Instance {
get {
return nullExpression;
}
}
NullExpression()
{
}
public override object AcceptVisitor(IAstVisitor visitor, object data)
{
return null;
}
public override string ToString()
{
return String.Format("[NullExpression]");
}
}
}
|