2
// <copyright see="prj:///doc/copyright.txt"/>
3
// <license see="prj:///doc/license.txt"/>
4
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
5
// <version>$Revision: 975 $</version>
9
using System.Collections;
11
namespace ICSharpCode.NRefactory.Parser.AST
13
public abstract class Expression : AbstractNode, INullable
15
public static NullExpression Null {
17
return NullExpression.Instance;
21
public virtual bool IsNull {
27
public static Expression CheckNull(Expression expression)
29
return expression == null ? NullExpression.Instance : expression;
33
/// Returns the existing expression plus the specified integer value.
34
/// WARNING: This method modifies <paramref name="expr"/> and possibly returns <paramref name="expr"/>
35
/// again, but it might also create a new expression around <paramref name="expr"/>.
37
public static Expression AddInteger(Expression expr, int value)
39
PrimitiveExpression pe = expr as PrimitiveExpression;
40
if (pe != null && pe.Value is int) {
41
int newVal = (int)pe.Value + value;
42
return new PrimitiveExpression(newVal, newVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
44
BinaryOperatorExpression boe = expr as BinaryOperatorExpression;
45
if (boe != null && boe.Op == BinaryOperatorType.Add) {
46
boe.Right = AddInteger(boe.Right, value);
47
if (boe.Right is PrimitiveExpression && ((PrimitiveExpression)boe.Right).Value is int) {
48
int newVal = (int)((PrimitiveExpression)boe.Right).Value;
51
} else if (newVal < 0) {
52
((PrimitiveExpression)boe.Right).Value = -newVal;
53
boe.Op = BinaryOperatorType.Subtract;
58
if (boe != null && boe.Op == BinaryOperatorType.Subtract) {
59
pe = boe.Right as PrimitiveExpression;
60
if (pe != null && pe.Value is int) {
61
int newVal = (int)pe.Value - value;
66
boe.Op = BinaryOperatorType.Add;
68
boe.Right = new PrimitiveExpression(newVal, newVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
72
BinaryOperatorType opType = BinaryOperatorType.Add;
75
opType = BinaryOperatorType.Subtract;
77
return new BinaryOperatorExpression(expr, opType, new PrimitiveExpression(value, value.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)));
81
public class NullExpression : Expression
83
static NullExpression nullExpression = new NullExpression();
85
public override bool IsNull {
91
public static NullExpression Instance {
93
return nullExpression;
101
public override object AcceptVisitor(IAstVisitor visitor, object data)
106
public override string ToString()
108
return String.Format("[NullExpression]");