Mercury library 1.0 Translation of CFG grammars into an object model (Bachelor's thesis).

- Main Page
- Packages
- Classes
- Files

- File List

All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
FormalParameter.cs
Go to the documentation of this file.
1 using System;
2 using System.Globalization;
3
4 namespace Mercury.Interpreting
5 {
6 //==<< ParameterType >>======================================================
7
11 public enum ParameterType
12 {
14 IntegralConstant,
16 RealConstant,
18 StringConstant,
20 BooleanConstant,
22 Variable,
24 ActionCall
25 }
26
27 //=============================================================================
28 // IFormalParameter
29 //=============================================================================
30
32 public interface IFormalParameter
33 {
35 ParameterType Type { get; }
36 }
37
38 //=============================================================================
39 // ConstantParameter
40 //=============================================================================
41
43 public class ConstantParameter : IFormalParameter, IEquatable<ConstantParameter>
44 {
45 //--[ Constructors ]-------------------------------------------------
46
53 public ConstantParameter(object value, ParameterType type)
54 {
55 if (value == null) throw new ArgumentNullException("value");
56
57 Value = value;
58 Type = type;
59 }
60
61 //--[ Properties ]---------------------------------------------------
62
64 public object Value { get; protected set; }
65
66 //--[ Methods ]------------------------------------------------------
67
68
72 public static ConstantParameter IntegralParameter(int value)
73 { return new ConstantParameter(value, ParameterType.IntegralConstant); }
74
78 public static ConstantParameter RealParameter(double value)
79 { return new ConstantParameter(value, ParameterType.RealConstant); }
80
84 public static ConstantParameter BooleanParameter(bool value)
85 { return new ConstantParameter(value, ParameterType.BooleanConstant); }
86
90 public static ConstantParameter StringParameter(string value)
91 { return new ConstantParameter(value, ParameterType.StringConstant); }
92
98 public static ConstantParameter AutoCreate(string value)
99 {
100 int ip; double dp; bool bp;
101
102 var culture = CultureInfo.InvariantCulture;
103 if ( int.TryParse(value, NumberStyles.Integer, culture, out ip))
104 return IntegralParameter(ip);
105 else if (double.TryParse(value, NumberStyles.Float, culture, out dp))
106 return RealParameter(dp);
107 else if ( bool.TryParse(value, out bp))
108 return BooleanParameter(bp);
109 else
110 return StringParameter(value);
111 }
112
113 //--[ Overriden methods ]--------------------------------------------
114
115 #region [ Object ]
116
117 public override bool Equals(object obj)
118 {
119 ConstantParameter cp = obj as ConstantParameter;
120 return (cp == null) ? false : Equals(cp);
121 }
122
123 public override int GetHashCode()
124 { return (((int) Type) + 1) * Value.GetHashCode(); }
125
126 public override string ToString()
127 { return Type == ParameterType.StringConstant ? "\"" + Value.ToString() + "\"" : Value.ToString(); }
128
129 #endregion
130
131 //--[ Interface implementation ]-------------------------------------
132
133 #region [ IFormalParameter ]
134
135 public ParameterType Type { get; protected set; }
136
137 #endregion
138
139 #region [ IEquatable ]
140
141 public bool Equals(ConstantParameter other)
142 {
143 return (other != null)
144 && (Type == other.Type)
145 && (Value.Equals(other.Value));
146 }
147
148 #endregion
149
150 }
151
152 //=============================================================================
153 // VariableParameter
154 //=============================================================================
155
160 public class VariableParameter : IFormalParameter, IEquatable<VariableParameter>
161 {
162 //--[ Constructors ]-------------------------------------------------
163
169 public VariableParameter(string name)
170 {
171 if (!Variable.IsValidName(name))
172 throw new ArgumentException(string.Format("Invalid variable name '{0}'", name));
173
174 Name = name;
175 Type = ParameterType.Variable;
176 }
177
178 //--[ Properties ]---------------------------------------------------
179
181 public string Name { get; private set; }
182
183 //--[ Overriden methods ]--------------------------------------------
184
185 #region [ Object ]
186
187 public override bool Equals(object obj)
188 {
189 VariableParameter vp = obj as VariableParameter;
190 return vp == null ? false : Equals(vp);
191 }
192
193 public override int GetHashCode()
194 { return Name.GetHashCode(); }
195
196 public override string ToString()
197 { return "#" + Name; }
198
199 #endregion
200
201 //--[ Interface implementation ]-------------------------------------
202
203 #region [ IFormalParameter ]
204
205 public ParameterType Type { get; private set; }
206
207 #endregion
208
209 #region [ IEquatable ]
210
211 public bool Equals(VariableParameter other)
212 { return (other != null) && (Name.Equals(other.Name)); }
213
214 #endregion
215
216 }
217 }
Mercury.Interpreting.VariableParameter.Equals
bool Equals(VariableParameter other)
Definition: FormalParameter.cs:211
Mercury.Interpreting.VariableParameter.GetHashCode
override int GetHashCode()
Definition: FormalParameter.cs:193
Mercury.Interpreting.StringConstant
String constant
Mercury.Interpreting.ConstantParameter.ToString
override string ToString()
Definition: FormalParameter.cs:126
Mercury.Interpreting.ConstantParameter.GetHashCode
override int GetHashCode()
Definition: FormalParameter.cs:123
Mercury.Interpreting.VariableParameter
Represents a named parameter that will be replaced by an actual value of a variable captured by inter...
Definition: FormalParameter.cs:160
Mercury.Interpreting.ConstantParameter.Equals
bool Equals(ConstantParameter other)
Definition: FormalParameter.cs:141
Mercury.Interpreting.ConstantParameter
Constant parameter.
Definition: FormalParameter.cs:43
Mercury.Interpreting.RealConstant
Real (double) constant
Mercury.Interpreting.ConstantParameter.ConstantParameter
ConstantParameter(object value, ParameterType type)
Creates a new ConstantParameter instance.
Definition: FormalParameter.cs:53
Mercury.Interpreting.Variable.IsValidName
static bool IsValidName(string name)
Determines whether the specified string is a valid variable name.
Definition: Element.cs:307
Mercury.Interpreting.ConstantParameter.IntegralParameter
static ConstantParameter IntegralParameter(int value)
Creates a new integral constant parameter.
Definition: FormalParameter.cs:72
Mercury.Interpreting.ConstantParameter.StringParameter
static ConstantParameter StringParameter(string value)
Creates a new string constant parameter.
Definition: FormalParameter.cs:90
Mercury.Interpreting.BooleanConstant
Boolean constant
Mercury.Interpreting.Variable
Represents a variable element that captures value and can be used on the RHS of the RewriteRule as an...
Definition: Element.cs:261
Mercury.Interpreting.VariableParameter.Equals
override bool Equals(object obj)
Definition: FormalParameter.cs:187
Mercury.Interpreting.VariableParameter.VariableParameter
VariableParameter(string name)
Creates a new Mercury.Interpreting.VariableParameter instance.
Definition: FormalParameter.cs:169
Mercury.Interpreting.ConstantParameter.AutoCreate
static ConstantParameter AutoCreate(string value)
Tries to parse the string value and infers its type.
Definition: FormalParameter.cs:98
Mercury.Interpreting.VariableParameter.ToString
override string ToString()
Definition: FormalParameter.cs:196
Mercury.Interpreting.ConstantParameter.BooleanParameter
static ConstantParameter BooleanParameter(bool value)
Creates a new boolean constant parameter
Definition: FormalParameter.cs:84
Mercury.Interpreting.ParameterType
ParameterType
Formal Parameter Type
Definition: FormalParameter.cs:11
Mercury.Interpreting.ConstantParameter.RealParameter
static ConstantParameter RealParameter(double value)
Creates a new real constant parameter.
Definition: FormalParameter.cs:78
Mercury.Interpreting.ActionCall
Action call result
Mercury.Interpreting.IFormalParameter
Represents formal parameters.
Definition: FormalParameter.cs:32
Mercury.Interpreting.ConstantParameter.Equals
override bool Equals(object obj)
Definition: FormalParameter.cs:117
Mercury.Interpreting.ConstantParameter.Value
object Value
Value of this constant
Definition: FormalParameter.cs:64
Mercury.Interpreting.IntegralConstant
Integral constant