~ubuntu-branches/ubuntu/feisty/monodevelop/feisty

1.1.3 by Sebastian Dröge
Import upstream version 0.11+svn20060818
1
// <file>
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>
6
// </file>
7
8
using System;
9
using System.Collections;
10
11
namespace ICSharpCode.NRefactory.Parser.AST
12
{
13
	public abstract class Expression : AbstractNode, INullable
14
	{
15
		public static NullExpression Null {
16
			get {
17
				return NullExpression.Instance;
18
			}
19
		}
20
		
21
		public virtual bool IsNull {
22
			get {
23
				return false;
24
			}
25
		}
26
		
27
		public static Expression CheckNull(Expression expression)
28
		{
29
			return expression == null ? NullExpression.Instance : expression;
30
		}
31
		
32
		/// <summary>
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"/>.
36
		/// </summary>
37
		public static Expression AddInteger(Expression expr, int value)
38
		{
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));
43
			}
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;
49
					if (newVal == 0) {
50
						return boe.Left;
51
					} else if (newVal < 0) {
52
						((PrimitiveExpression)boe.Right).Value = -newVal;
53
						boe.Op = BinaryOperatorType.Subtract;
54
					}
55
				}
56
				return boe;
57
			}
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;
62
					if (newVal == 0)
63
						return boe.Left;
64
					if (newVal < 0) {
65
						newVal = -newVal;
66
						boe.Op = BinaryOperatorType.Add;
67
					}
68
					boe.Right = new PrimitiveExpression(newVal, newVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
69
					return boe;
70
				}
71
			}
72
			BinaryOperatorType opType = BinaryOperatorType.Add;
73
			if (value < 0) {
74
				value = -value;
75
				opType = BinaryOperatorType.Subtract;
76
			}
77
			return new BinaryOperatorExpression(expr, opType, new PrimitiveExpression(value, value.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)));
78
		}
79
	}
80
	
81
	public class NullExpression : Expression
82
	{
83
		static NullExpression nullExpression = new NullExpression();
84
		
85
		public override bool IsNull {
86
			get {
87
				return true;
88
			}
89
		}
90
		
91
		public static NullExpression Instance {
92
			get {
93
				return nullExpression;
94
			}
95
		}
96
		
97
		NullExpression()
98
		{
99
		}
100
		
101
		public override object AcceptVisitor(IAstVisitor visitor, object data)
102
		{
103
			return null;
104
		}
105
		
106
		public override string ToString()
107
		{
108
			return String.Format("[NullExpression]");
109
		}
110
	}
111
}