Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Instance.cs
Go to the documentation of this file.
1 using Mercury.Nucleus.Trees;
2 using Mercury.Syntax;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 namespace Mercury.Interpreting
8 {
9 //=============================================================================
10 // Instance
11 //=============================================================================
12 
17  public class Instance : IEquatable<Instance>
18  {
19  //--[ Private constructors ]-----------------------------------------
20 
21  private Instance(Variable var, object value)
22  {
23  if (var == null) throw new ArgumentNullException("var");
24  if (value == null) throw new ArgumentNullException("value");
25 
26  Var = var;
27  Value = value;
28  }
29 
30  //--[ Constructors ]-------------------------------------------------
31 
32  // used by Wildcard
33  internal Instance(Variable var, Tree<Symbol>[] wildmatch)
34  : this(var, wildmatch as object)
35  { }
36 
37  internal Instance(Variable var, Tree<Symbol> tree)
38  : this(var, tree as object)
39  { }
40 
41  //--[ Properties ]---------------------------------------------------
42 
44  public Variable Var { get; private set; }
45 
47  public object Value { get; private set; }
48 
49  //--[ Overriden methods ]--------------------------------------------
50 
51  #region [ Object ]
52 
53  public override bool Equals(object obj)
54  {
55  Instance other = obj as Instance;
56  return other == null ? false : Equals(other);
57  }
58 
59  public override int GetHashCode()
60  { return Var.GetHashCode(); }
61 
62  #endregion
63 
64  //--[ Interface implementation ]-------------------------------------
65 
66  #region [ IEquatable ]
67 
68  public bool Equals(Instance other)
69  {
70  if ((other == null) || !Var.Equals(other.Var))
71  return false;
72 
73  if (Var is Wildcard)
74  {
75  var obja = Value as Tree<Symbol>[];
76  var objb = other.Value as Tree<Symbol>[];
77 
78  return (obja.Length == objb.Length)
79  && (obja.Zip(objb, (a, b) => a.Equals(b)).All(x => x));
80  }
81 
82  return Value.Equals(other.Value);
83  }
84 
85  #endregion
86 
87  }
88 
89 //=============================================================================
90 // InstanceList
91 //=============================================================================
92 
94  public class InstanceList : IEnumerable<Instance>
95  {
96  //--[ Private fields ]-----------------------------------------------
97 
98  private List<Instance> data;
99  private Dictionary<string, Instance> dict;
100 
101  //--[ Constructors ]-------------------------------------------------
102 
103  internal InstanceList()
104  {
105  data = new List<Instance>();
106  dict = new Dictionary<string, Instance>();
107  }
108 
109  //--[ Properties ]---------------------------------------------------
110 
112  public int Count { get { return data.Count; } }
113 
114  //--[ Indexer ]------------------------------------------------------
115 
120  public Instance this[string name] { get { return dict[name]; } }
121 
122  //--[ Methods ]------------------------------------------------------
123 
130  internal void Add(Instance instance)
131  {
132  if (instance == null) throw new ArgumentNullException("instance");
133 
134  dict.Add(instance.Var.Name, instance);
135  data.Add(instance);
136  }
137 
139  internal void Clear()
140  {
141  dict.Clear();
142  data.Clear();
143  }
144 
150  internal void RemoveLast(int count = 1)
151  {
152  for(int ix = 0; (ix < count) && (data.Count > 0); ++ix)
153  {
154  Instance value = data[data.Count - 1];
155  data.RemoveAt(data.Count - 1);
156  dict.Remove(value.Var.Name);
157  }
158  }
159 
160  //--[ Interface implementation ]-------------------------------------
161 
162  #region [ IEnumerable ]
163 
164  public IEnumerator<Instance> GetEnumerator()
165  { return data.GetEnumerator(); }
166 
167  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
168  { return data.GetEnumerator(); }
169 
170  #endregion
171 
172  }
173 
174 }
Represents a binding between a Mercury.Interpreting.Variable and its value
Definition: Instance.cs:17
override int GetHashCode()
Definition: Instance.cs:59
Maps token to symbol, keyword, number and string
Variable Var
The variable
Definition: Instance.cs:44
Represents a variable element that captures value and can be used on the RHS of the RewriteRule as an...
Definition: Element.cs:261
bool Equals(Instance other)
Definition: Instance.cs:68
An element that matches zero or more values. Can be used as a variable, but the actioncall must be of...
Definition: Element.cs:370
Represents the list of instances
Definition: Instance.cs:94
IEnumerator< Instance > GetEnumerator()
Definition: Instance.cs:164
override bool Equals(object obj)
Definition: Instance.cs:53