The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
CGRuntimeFreshTEPromotionOperation.cs
Go to the documentation of this file.
1 using TypeSystem.Operations;
2 using TypeSystem;
3 using ErrorManagement;
4 namespace CodeGeneration.Operations {
8  public class CGRuntimeFreshTEPromotionOperation<T> : TypeSystemOperation where T : ILCodeGenerator {
9 
10  private int indent;
11  private T codeGenerator;
12 
13  public CGRuntimeFreshTEPromotionOperation(int indent, T codeGenerator) {
14  this.indent = indent;
15  this.codeGenerator = codeGenerator;
16  }
17 
18  #region genInteger
19  public override object Exec(IntType operand, object arg) {
25  // * char to int
26  // * If we must promote to an integer, a Char could be on the stack
27  this.codeGenerator.dup(this.indent);
28  this.codeGenerator.isinst(this.indent, CharType.Instance);
29 
30  string notACharLabel = this.codeGenerator.NewLabel;
31 
32  this.codeGenerator.brfalse(this.indent, notACharLabel);
33  this.codeGenerator.UnboxAny(this.indent, CharType.Instance);
34 
35  string endLabel = this.codeGenerator.NewLabel;
36 
37  this.codeGenerator.br(this.indent, endLabel);
38  this.codeGenerator.WriteLabel(this.indent, notACharLabel);
39  this.codeGenerator.UnboxAny(this.indent, IntType.Instance);
40  this.codeGenerator.WriteLabel(this.indent, endLabel);
41  return null;
42  }
43 
44  #endregion
45 
46  #region Double ...
47  public override object Exec(DoubleType operand, object arg) {
54  // * char to double or int to double
55  // * If we must promote to an double, a Char or a Int32 could be on the stack
56  this.codeGenerator.dup(this.indent);
57  this.codeGenerator.isinst(this.indent, CharType.Instance);
58 
59  string notACharLabel = this.codeGenerator.NewLabel;
60 
61  this.codeGenerator.brfalse(this.indent, notACharLabel);
62  this.codeGenerator.UnboxAny(this.indent, CharType.Instance);
63  this.codeGenerator.convToDouble(this.indent);
64 
65  string endLabel = codeGenerator.NewLabel;
66 
67  this.codeGenerator.br(this.indent, endLabel);
68  this.codeGenerator.WriteLabel(this.indent, notACharLabel);
69  this.codeGenerator.dup(this.indent);
70  this.codeGenerator.isinst(this.indent, IntType.Instance);
71 
72  string notAInt32Label = this.codeGenerator.NewLabel;
73 
74  this.codeGenerator.brfalse(this.indent, notAInt32Label);
75  this.codeGenerator.UnboxAny(this.indent, IntType.Instance);
76  this.codeGenerator.convToDouble(this.indent);
77  this.codeGenerator.br(this.indent, endLabel);
78  this.codeGenerator.WriteLabel(this.indent, notAInt32Label);
79  this.codeGenerator.UnboxAny(this.indent, DoubleType.Instance);
80  this.codeGenerator.WriteLabel(this.indent, endLabel);
81 
82  return null;
83  }
84 
85  #endregion
86 
87  #region TypeVariable ...
88  public override object Exec(TypeVariable operand, object arg) {
94  if (!operand.IsValueType())
95  return null;
96  if (TypeExpression.Is<IntType>(operand))
97  return this.Exec(TypeExpression.As<IntType>(operand), arg);
98  if (TypeExpression.Is<DoubleType>(operand))
99  return this.Exec(TypeExpression.As<DoubleType>(operand), arg);
100  return this.Exec((TypeExpression)operand, arg);
101  }
102 
103  #endregion
104 
105  #region TypeExpression ...
106  public override object Exec(TypeExpression operand, object arg) {
112  if (!operand.IsValueType())
113  return null;
114  // * If not promotion is needed, we simply Unbox
115  codeGenerator.UnboxAny(this.indent, operand);
116  return null;
117  }
118 
119  #endregion
120  #region String...
121  public override object Exec(StringType s, object arg) {
127  codeGenerator.WriteLine(this.indent, "callvirt\tinstance string [mscorlib]System.Object::ToString()");
128  return null;
129  }
130 
131  #endregion
132  public override object ReportError(TypeExpression tE) {
133  ErrorManager.Instance.NotifyError(new CodeGenerationError("No se ha definido la operación solicitada"));
134  return null;
135  }
136 
137  }
138 
139 }
override bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
Represent a string type.
Definition: StringType.cs:36
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
Represent a integer type.
Definition: IntType.cs:37
Represents a error produced when a MethodType has class information and tries to assign other class i...
abstract bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
Represents a double type.
Definition: DoubleType.cs:36