Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
ActionCall.cs
Go to the documentation of this file.
1 using Mercury.Exceptions;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 
7 namespace Mercury.Interpreting
8 {
9 //=============================================================================
10 // ActionCall
11 //=============================================================================
12 
18  public class ActionCall<T> : IFormalParameter, IEquatable<ActionCall<T>>
19  where T : class
20  {
21  //--[ Private fields ]-----------------------------------------------
22 
23  private int? memHash = null;
24 
25  //--[ Constructors ]-------------------------------------------------
26 
34  public ActionCall(InterpreterAction<T> action, IEnumerable<IFormalParameter> formalparams)
35  {
36  if (action == null) throw new ArgumentNullException("action");
37  if (formalparams == null) throw new ArgumentNullException("formalparams");
38 
39  Action = action;
40  FormalParameters = formalparams.ToArray();
41  Type = ParameterType.ActionCall;
42 
43  if ((Action.Arity.Item2 < FormalParameters.Count) || (FormalParameters.Count < Action.Arity.Item1))
44  throw new InvalidActionCallException(this.ToString(), "invalid number of formal parameters");
45  }
46 
47  //--[ Properties ]---------------------------------------------------
48 
51  public IReadOnlyList<IFormalParameter> FormalParameters { get; private set; }
52 
55  public InterpreterAction<T> Action { get; private set; }
56 
57  //--[ Overriden methods ]--------------------------------------------
58 
59  #region [ Object ]
60 
61  public override bool Equals(object obj)
62  {
63  ActionCall<T> other = obj as ActionCall<T>;
64  return obj == null ? false : Equals(other);
65  }
66 
67  public override int GetHashCode()
68  {
69  if (!memHash.HasValue)
70  {
71  memHash = 19 * Action.Name.GetHashCode();
72 
73  for (int ix = 0; ix < FormalParameters.Count; ++ix)
74  memHash = 17 * memHash + FormalParameters[ix].GetHashCode();
75  }
76 
77  return memHash.Value;
78  }
79 
80  public override string ToString()
81  {
82  StringBuilder sb = new StringBuilder("(").Append(Action.Name);
83 
84  if (FormalParameters.Count != 0)
85  {
86  sb.Append(' ').Append(FormalParameters[0]);
87 
88  for (int ix = 1; ix < FormalParameters.Count; ++ix)
89  sb.Append(' ').Append(FormalParameters[ix]);
90  }
91 
92  return sb.Append(")").ToString();
93  }
94 
95  #endregion
96 
97  //--[ Interface implementation ]-------------------------------------
98 
99  #region [ IFormalParameter ]
100 
101  public ParameterType Type { get; private set; }
102 
103  #endregion
104 
105  #region [ IEquatable ]
106 
107  public bool Equals(ActionCall<T> other)
108  {
109  return (other != null)
110  && (Action.Name == other.Action.Name)
111  && (FormalParameters.Count == other.FormalParameters.Count)
112  && (FormalParameters.Zip(other.FormalParameters, (x, y) => x.Equals(y)).All(x => x));
113  }
114 
115  #endregion
116 
117  }
118 
119 }
bool Equals(ActionCall< T > other)
Definition: ActionCall.cs:107
An exception representing a problem with Mercury.Interpreting.ActionCall{T}
Function that wraps constant values in the RHS
ParameterType
Formal Parameter Type
This class represents the application of Mercury.Interpreting.InterpreterAction{T} on the Mercury...
Definition: ActionCall.cs:18
Represents formal parameters.
ActionCall(InterpreterAction< T > action, IEnumerable< IFormalParameter > formalparams)
Initializes a new instance of the ActionCall{T} class.
Definition: ActionCall.cs:34
IReadOnlyList< IFormalParameter > FormalParameters
The list of formal parameters.
Definition: ActionCall.cs:51
override bool Equals(object obj)
Definition: ActionCall.cs:61