Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 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  {
22  Variable,
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  {
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  {
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 }
bool Equals(VariableParameter other)
Represents a named parameter that will be replaced by an actual value of a variable captured by inter...
bool Equals(ConstantParameter other)
Real (double) constant
ConstantParameter(object value, ParameterType type)
Creates a new ConstantParameter instance.
static bool IsValidName(string name)
Determines whether the specified string is a valid variable name.
Definition: Element.cs:307
static ConstantParameter IntegralParameter(int value)
Creates a new integral constant parameter.
static ConstantParameter StringParameter(string value)
Creates a new string constant parameter.
Represents a variable element that captures value and can be used on the RHS of the RewriteRule as an...
Definition: Element.cs:261
VariableParameter(string name)
Creates a new Mercury.Interpreting.VariableParameter instance.
static ConstantParameter AutoCreate(string value)
Tries to parse the string value and infers its type.
static ConstantParameter BooleanParameter(bool value)
Creates a new boolean constant parameter
ParameterType
Formal Parameter Type
static ConstantParameter RealParameter(double value)
Creates a new real constant parameter.
Represents formal parameters.
object Value
Value of this constant