The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
TypeTable.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: TypeTable.cs //
6 // Authors: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Francisco Ortin - francisco.ortin@gmail.com //
8 // Description: //
9 // Implementation of a table of types. //
10 // Implements Singleton pattern. //
11 // -------------------------------------------------------------------------- //
12 // Create date: 15-10-2006 //
13 // Modification date: 06-04-2007 //
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Text;
19 
20 using ErrorManagement;
21 
22 namespace TypeSystem
23 {
30  public class TypeTable
31  {
32  #region Fields
33 
37  private static TypeTable instance = new TypeTable();
38 
42  private Dictionary<string, TypeExpression> table;
43 
47  private Dictionary<string, System.Type> map;
48 
49  #endregion
50 
51  #region Properties
52 
56  public static TypeTable Instance
57  {
58  get { return instance; }
59  }
60 
61  #endregion
62 
63  #region Constructors
64 
68  private TypeTable()
69  {
70  this.table = new Dictionary<string, TypeExpression>();
71  // Add predefined types
72  this.table.Add(IntType.Instance.ToString(), IntType.Instance);
73  this.table.Add(DoubleType.Instance.ToString(), DoubleType.Instance);
74  this.table.Add(CharType.Instance.ToString(), CharType.Instance);
75  this.table.Add(VoidType.Instance.ToString(), VoidType.Instance);
76  this.table.Add(StringType.Instance.ToString(), StringType.Instance);
77  this.table.Add(BoolType.Instance.ToString(), BoolType.Instance);
78  // mapping with mscorlib.dll
79  this.table.Add("System.Int32", IntType.Instance);
80  this.table.Add("System.Double", DoubleType.Instance);
81  this.table.Add("System.Char", CharType.Instance);
82  this.table.Add("System.String", StringType.Instance);
83  this.table.Add("System.Boolean", BoolType.Instance);
84  this.table.Add("System.Void", VoidType.Instance);
85  this.table.Add("object", new BCLClassType("System.Object", Type.GetType("System.Object")));
86 
87  // Auxiliar mapping
88  this.map = new Dictionary<string, Type>(5);
89  this.map.Add(IntType.Instance.ToString(), Type.GetType("System.Int32"));
90  this.map.Add(DoubleType.Instance.ToString(), Type.GetType("System.Double"));
91  this.map.Add(CharType.Instance.ToString(), Type.GetType("System.Char"));
92  this.map.Add(StringType.Instance.ToString(), Type.GetType("System.String"));
93  this.map.Add(BoolType.Instance.ToString(), Type.GetType("System.Boolean"));
94  this.map.Add(VoidType.Instance.ToString(), Type.GetType("System.Void"));
95  }
96  #endregion
97 
98  #region Clear()
99  public void Clear() {
103  instance = new TypeTable();
104  }
105  #endregion
106 
107  #region GetType()
108 
117  public TypeExpression GetType(string name, Location location)
118  {
119  if (name == null)
120  return null;
121 
122  // If tmpName exists, return the type
123  if (this.table.ContainsKey(name))
124  return this.table[name];
125 
126  // Try to find the type (class or interface) in the BCL (mscorlib.dll)
127  Type type = Type.GetType(name, false);
128  if (type != null) {
129  TypeExpression te = Introspection.createBCLUserType(name, type, location);
130  AddType(name, te, location);
131  return te;
132  }
133 
134  return null;
135  }
136 
137  #endregion
138 
139  #region TypeExpressionToType()
140 
146  public Type[] TypeExpressionToType(TypeExpression[] typ, Location location)
147  {
148  Type[] aux = new Type[typ.GetLength(0)];
149 
150  for (int i = 0; i < typ.GetLength(0); i++)
151  {
152  if (this.map.ContainsKey(typ[i].FullName))
153  aux[i] = this.map[typ[i].FullName];
154  else
155  {
156  if (typ[i] is IBCLUserType)
157  aux[i] = ((IBCLUserType)typ[i]).TypeInfo;
158  else
159  ErrorManager.Instance.NotifyError(new UnknownTypeError(typ[i].FullName, location));
160  }
161  }
162  return aux;
163  }
164 
165  #endregion
166 
167  #region ContainsType()
168 
174  public bool ContainsType(string key)
175  {
176  return this.table.ContainsKey(key);
177  }
178 
179  #endregion
180 
181  #region AddType()
182 
191  public void AddType(string name, TypeExpression type, Location location)
192  {
193  if (!this.table.ContainsKey(name))
194  this.table.Add(name, type);
195  else
196  ErrorManager.Instance.NotifyError(new DefinedTypeError(name, location));
197  }
198 
203  public void AddVarType(TypeVariable type)
204  {
205  if (!this.table.ContainsKey(type.FullName))
206  this.table.Add(type.FullName, type);
207  }
208 
209  #endregion
210 
211  #region ObtainNewType()
212 
213  public static string ObtainNewType(string type)
214  {
215  if (type.StartsWith("Var("))
216  {
217  string aux = Convert.ToString(TypeSystem.TypeVariable.NewTypeVariable);
218  int index = type.IndexOf(')', 3) + 1;
219  type = type.Substring(index, type.Length - index);
220  return aux + type;
221  }
222  else
223  return type;
224  }
225 
226  #endregion
227 
228  #region ToString()
229 
234  public override string ToString()
235  {
236  StringBuilder aux = new StringBuilder();
237  Dictionary<string, TypeExpression>.KeyCollection keys = this.table.Keys;
238  foreach (string key in keys)
239  {
240  aux.AppendFormat("{0}\t\t\t{1}", key, this.table[key].ToString());
241  aux.AppendLine();
242  }
243  return aux.ToString();
244  }
245 
246  #endregion
247  }
248 }
static StringType Instance
Gets the unique instance of StringType
Definition: StringType.cs:57
static CharType Instance
Gets the unique instance of CharType
Definition: CharType.cs:58
override string FullName
The full name in type variables is calculated just in time
static DoubleType Instance
Gets the unique instance of DoubleType
Definition: DoubleType.cs:57
static TypeTable Instance
Gets the unique instance of TypeTable
Definition: TypeTable.cs:57
static string ObtainNewType(string type)
Definition: TypeTable.cs:213
Represents a error produced when the used type is not defined.
Represent a string type.
Definition: StringType.cs:36
This class encapsulates a location in a specific file. Implements an Inmutable pattern. So it can be used in any context, that is his internal fields never change.
Definition: Location.cs:24
Represents a void type.
Definition: VoidType.cs:34
Represent a character type.
Definition: CharType.cs:38
Abstract class that represents all different types.
Represents a error produced when the defined type already exists.
Represents a generic type expression
Definition: TypeVariable.cs:36
TypeExpression GetType(string name, Location location)
Gets the type associated to the name specified in the argument.
Definition: TypeTable.cs:117
System.Text.StringBuilder StringBuilder
Definition: CSharpParser.cs:4
virtual string FullName
Gets the full name of the type Note: WriteType expression is the longest recursive representation of ...
Represent a integer type.
Definition: IntType.cs:37
Type[] TypeExpressionToType(TypeExpression[] typ, Location location)
Gets the type of the type expression.
Definition: TypeTable.cs:146
Represents a double type.
Definition: DoubleType.cs:36
bool ContainsType(string key)
Returns if the type table contains the specified tmpName.
Definition: TypeTable.cs:174
Implementation of a table of types.
Definition: TypeTable.cs:30
void AddType(string name, TypeExpression type, Location location)
Adds a new type into the type table
Definition: TypeTable.cs:191
static VoidType Instance
Gets the unique instance of VoidType
Definition: VoidType.cs:51
static IntType Instance
Gets the unique instance of IntType
Definition: IntType.cs:57
static BoolType Instance
Gets the unique instance of BoolType
Definition: BoolType.cs:57
void AddVarType(TypeVariable type)
Adds a new variable type
Definition: TypeTable.cs:203
Represent a bool type.
Definition: BoolType.cs:36
override string ToString()
Dumps the information stores in the table of types.
Definition: TypeTable.cs:234
void Clear()
Clears the type table (debug purposes)
Definition: TypeTable.cs:102