The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
ILStatementsCodeGeneration.cs
Go to the documentation of this file.
1 using System.IO;
2 using System.Text;
3 using TypeSystem;
4 using System;
5 namespace CodeGeneration {
6 
8  protected TextWriter output;
10  public ILStatementsCodeGeneration(TextWriter output) {
11  this.output = output;
12 #if DEBUG
14 #endif
15  }
16 
17  #region WriteComment()
18 
23 
24  public virtual void WriteComment(string msg) {
25  this.WriteComment(0, msg);
26  }
27 
28  public virtual void WriteComment(int indent, string msg) {
29  this.WriteLine("{0}// {1}", this.Indentation(indent), msg);
30  }
31  #endregion
32 
33 #region Write() & WriteLine() & indentation
34 
38  public void WriteLine(int indent, string format, params Object[] obj) {
39  this.WriteIndentation(indent);
40  this.WriteLine(format, obj);
41  }
42 
43  public void WriteLine(string format, params Object[] obj) {
44  this.output.WriteLine(format, obj);
45 #if DEBUG
46  debugMemoryLog.AppendFormat(format, obj);
47  debugMemoryLog.AppendLine();
48 #endif
49 
50  }
51 
52  public void Write(string format, params Object[] obj) {
53  this.output.Write(format, obj);
54 #if DEBUG
55  debugMemoryLog.AppendFormat(format, obj);
56 #endif
57 
58  }
59 
60  public void Write(int indent, string format, params Object[] obj) {
61  this.WriteIndentation(indent);
62  this.Write(format, obj);
63 }
64 
65 public void Write(int indent, string str) {
66  this.WriteIndentation(indent);
67  this.Write(str);
68 
69  }
70 
71 public void Write(string str) {
72  this.output.Write(str);
73 #if DEBUG
74  debugMemoryLog.Append(str);
75 #endif
76 }
77 
78 public void WriteLine(int indent, string str) {
79  this.Write(indent, str);
80  this.WriteLine();
81 }
82 
83 public void WriteLine(string str) {
84  this.Write(str);
85  this.WriteLine();
86  }
87 
88 public void WriteLine() {
89  this.output.WriteLine();
90 #if DEBUG
91  debugMemoryLog.AppendLine();
92 
93 #endif
94  }
95 
96  public void WriteIndentation(int indent) {
97  this.Write(this.Indentation(indent));
98  }
99 
100  private string Indentation(int indent) {
101  return indent > 0 ? " ".PadLeft(indent * 3) : "";
102  }
103 #endregion
104 
105  public void WriteLNAssemblyDirective(string fileName) {
106  this.WriteLine(".assembly extern mscorlib {}");
107  this.WriteLine(".assembly {0} {{}}", fileName);
108  }
109 
110  #region WriteClassVisibility
111 
112  public virtual void WriteClassVisibility(int indent, string name, ClassType type) {
113 
114  Modifier mask = type.ModifierMask;
115 
116  this.Write(indent, ".class ");
117 
118  if ( ( mask & Modifier.Public ) != 0 )
119  this.Write("public ");
120 
121  if ( ( mask & Modifier.Internal ) != 0 )
122  this.Write("private ");
123 
124  if ( ( mask & Modifier.Abstract ) != 0 )
125  this.Write("abstract ");
126  else if ( ( mask & Modifier.Static ) != 0 )
127  this.Write("abstract sealed ");
128  }
129 
130  #endregion
131 
132  public void WriteModule(string fileName) {
133  this.WriteLine(".module {0}.exe", fileName);
134  }
135 
136  public void WriteEndOfClass(string className) {
137  this.WriteCloseBrace();
138  this.WriteComment("End of class " + className);
139  }
140 
141 
142  #region WriteLNInterfaceHeader()
143 
144  // .class interface <flags> <name> implements <class_ref>[, class_ref*]
145  // <flags> := public (public)
146  // | private (internal)
147  //
148  // Interfaz modifiers: new, public, protected, internal, private.
149  // Namespace elements cannot be declared as private, protected or protected internal.
150  //
151  // Nested class currently does not apply because it is commented in grammar file.
152  // For this reason, protected internal, protected and private do not use it.
153 
160  public virtual void WriteLNInterfaceHeader(int indent, string name, InterfaceType type) {
161  Modifier mask = type.ModifierMask;
162 
163  this.Write(indent, ".class interface ");
164 
165  if ( ( mask & Modifier.Public ) != 0 )
166  this.Write("public abstract ");
167 
168  if ( ( mask & Modifier.Internal ) != 0 )
169  this.Write("private abstract ");
170 
171  this.Write("auto ansi {0} ", name);
172 
173  if ( type.InterfaceList.Count != 0 ) {
174  this.Write("implements ");
175  for ( int i = 0; i < type.InterfaceList.Count - 1; i++ ) {
176 
177  if ( type.InterfaceList[i] is BCLInterfaceType )
178  this.Write("[mscorlib]");
179  this.Write("{0}, ", type.InterfaceList[i].FullName);
180  }
181  if ( type.InterfaceList[type.InterfaceList.Count - 1] is BCLInterfaceType )
182  this.Write("[mscorlib]");
183 
184  this.Write("{0}", type.InterfaceList[type.InterfaceList.Count - 1].FullName);
185  } //if
186 
187  this.WriteLine();
188  }
189 
190  #endregion
191 
192 
193 
194  #region WriteField()
195 
196  // .field <flags> <type> <name>
197  // .field <flags> <type> <name> = <const_type>[(<value>)]
198  // <flags> := public (public)
199  // | private (private)
200  // | assembly (internal)
201  // | family (protected)
202  // | famorassem (protected internal)
203  // | static (static)
204  // | static literal (const)
205  //
206  // Field modifiers: new, public, protected, internal, private, static, readonly, volatile
207  // Field cannot be both readonly and volatile
208  // A constant field cannot be static
209  //
210  // Readonly and volatile modifier currently does not apply because it is commented in grammar file.
211 
219  internal void WriteField(int indent, string name, FieldType type, bool constantField) {
220  Modifier mask = type.MemberInfo.ModifierMask;
221 
222  this.Write(indent, ".field ");
223 
224  if ( ( mask & Modifier.Public ) != 0 )
225  this.Write("public ");
226 
227  if ( ( mask & Modifier.Private ) != 0 )
228  this.Write("private ");
229 
230  if ( ( mask & ( Modifier.Protected | Modifier.Internal ) ) == ( Modifier.Protected | Modifier.Internal ) )
231  this.Write("famorassem ");
232  else {
233  if ( ( mask & Modifier.Internal ) != 0 )
234  this.Write("assembly ");
235  if ( ( mask & Modifier.Protected ) != 0 )
236  this.Write("family ");
237  }
238  if ( ( mask & Modifier.Static ) != 0 )
239  this.Write("static ");
240  else if ( constantField )
241  this.Write("static literal ");
242 
243  if (type.FieldTypeExpression is TypeVariable) //if the field is TypeVariable, always is represented by an object in IL Code.
244  this.WriteType(TypeVariable.NewTypeVariable);
245  else
246  this.WriteType(type.FieldTypeExpression);
247  this.Write(" {0}", name);
248 
249  }
254  internal void WriteFieldInitialization(TypeExpression type) {
255  this.Write(" = {0}(", type.ILType());
256  }
257  #endregion
258 
259  #region WriteType()
260 
265  internal void WriteType(TypeExpression type) {
266  this.Write(type.ILType());
267  }
268 
269  #endregion
270 
271  #region WriteNamespace()
272  // .namespace <name>
273 
279  public void WriteNamespace(string name) {
280  this.Write(".namespace {0}", name);
281  }
282 
283  #endregion
284 
285  #region OpenBrace & CloseBrace()
286 
291  public void WriteCloseBrace(int indent) {
292  this.Write(indent, "}");
293  }
294 
295  public void WriteCloseBrace() {
296  this.WriteCloseBrace(0);
297  }
298  public void WriteLineCloseBrace(int indent) {
299  this.WriteCloseBrace(indent);
300  this.WriteLine();
301 
302  }
303  public void WriteOpenBrace(int indent) {
304  this.Write(indent, "{");
305  }
306 
307  public void WriteOpenBrace() {
308  this.WriteOpenBrace(0);
309  }
310  public void WriteLineOpenBrace(int indent) {
311  this.WriteOpenBrace(indent);
312  this.WriteLine();
313 
314  }
315  #endregion
316 
317  #region WriteEntryPoint()
318 
319  public virtual void WriteEntryPoint() {
320  this.Write(".entrypoint");
321  }
322 
323  #endregion
324 
325  #region WriteAuxiliarLocalVariable()
326 
332  public virtual void WriteAuxiliarLocalVariable(string id, string type) {
333  this.Write(".locals init({0} {1})", type, id);
334  }
335 
336  #endregion
337 
338 
339  }
340 }
void WriteLine(int indent, string format, params Object[] obj)
Writes a line terminator in the text stream
void Write(string format, params Object[] obj)
virtual void WriteComment(string msg)
Writes the specified message.
virtual void WriteAuxiliarLocalVariable(string id, string type)
Writes the information of local variables.
void Write(int indent, string format, params Object[] obj)
Represents a interface type.
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
System.Text.StringBuilder StringBuilder
Definition: CSharpParser.cs:4
virtual void WriteLNInterfaceHeader(int indent, string name, InterfaceType type)
Writes the interface header
void WriteCloseBrace(int indent)
Writes the termination token. Requires the output text would be formatted, with tabs for example...
virtual void WriteComment(int indent, string msg)
TypeExpression FieldTypeExpression
Gets the field type.
Definition: FieldType.cs:58
List< InterfaceType > InterfaceList
Gets the list of interfaces
Definition: UserType.cs:157
Modifier
Indicates differents modifiers to use in class (only public, internal or static), fields or methods...
Represents a class type.
Definition: ClassType.cs:35
Representa a field type.
Definition: FieldType.cs:37
virtual void WriteClassVisibility(int indent, string name, ClassType type)
void WriteLine(string format, params Object[] obj)
void WriteNamespace(string name)
Writes the namespace header.