The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
RelationalOperation.cs
Go to the documentation of this file.
1 using TypeSystem;
2 using AST;
3 using ErrorManagement;
4 using System;
5 using TypeSystem.Constraints;
6 using DynVarManagement;
7 namespace TypeSystem.Operations {
9 
10  #region Fields
11 
15  protected bool showErrorMessage;
16  protected Location location;
17 
18  #endregion
19 
20  #region Constructor
21  public RelationalOperation(TypeExpression secondOperand, RelationalOperator relationalOperator, MethodType methodAnalyzed, bool showErrorMessage, Location location) {
22  this.secondOperand = secondOperand;
23  this.relationalOperator = relationalOperator;
24  this.methodAnalyzed = methodAnalyzed;
25  this.showErrorMessage = showErrorMessage;
26  this.location = location;
27  }
28  #endregion
29 
30  #region TypeExpression >= this.secondOperand
31  public override object Exec(TypeExpression firstOperand, object arg) {
32  if (showErrorMessage)
33  ErrorManager.Instance.NotifyError(new OperationNotAllowedError(this.relationalOperator.ToString(), firstOperand.FullName, this.secondOperand.fullName, this.location));
34  return null;
35  }
36  #endregion
37 
38  #region DoubleType >= this.secondOperand
39  public override object Exec(DoubleType firstOperand, object arg) {
40  if ((int)this.secondOperand.AcceptOperation(new PromotionLevelOperation(DoubleType.Instance), arg) != -1)
41  return BoolType.Instance;
42  return this.secondOperand.AcceptOperation(new RelationalOperation(firstOperand, this.relationalOperator, this.methodAnalyzed, this.showErrorMessage, this.location), arg);
43  }
44  #endregion
45 
46  #region IntType >= this.secondOperand
47  public override object Exec(IntType firstOperand, object arg) {
48  if ((int)secondOperand.AcceptOperation(new PromotionLevelOperation(DoubleType.Instance), arg) != -1)
49  return BoolType.Instance;
50  //? quedarĂ­a menos lioso new BinaryAritmeticalOperation(iTE, ...).exec(secondOperand)
51  return secondOperand.AcceptOperation(new RelationalOperation(firstOperand, this.relationalOperator, this.methodAnalyzed, this.showErrorMessage, this.location), arg);
52  }
53  #endregion
54 
55  #region CharType >= this.secondOperand
56  public override object Exec(CharType firstOperand, object arg) {
57  if ((int)secondOperand.AcceptOperation(new PromotionLevelOperation(DoubleType.Instance), arg) != -1)
58  return BoolType.Instance;
59  return this.secondOperand.AcceptOperation(new RelationalOperation(firstOperand, this.relationalOperator, this.methodAnalyzed, this.showErrorMessage, this.location), arg);
60  }
61  #endregion
62 
63  #region String >= this.secondOperand
64  public override object Exec(StringType firstOperand, object arg) {
65  if (relationalOperator == RelationalOperator.Equal || relationalOperator == RelationalOperator.NotEqual) {
66  if ((int)this.secondOperand.AcceptOperation(new PromotionLevelOperation(firstOperand), arg) != -1)
67  return BoolType.Instance;
68  if (this.showErrorMessage) {
69  ErrorManager.Instance.NotifyError(new TypePromotionError(this.secondOperand.FullName, firstOperand.FullName, this.relationalOperator.ToString(), this.location));
70  return null;
71  }
72  }
73  return ReportError(firstOperand);
74  }
75  #endregion
76 
77  #region PropertyType >= this.secondOperand
78  public override object Exec(PropertyType firstOperand, object arg) {
79  if (firstOperand.PropertyTypeExpression != null)
80  return firstOperand.PropertyTypeExpression.AcceptOperation(this, arg);
81  return null;
82  }
83  #endregion
84 
85  #region ArrayType >= this.secondOperand
86  public override object Exec(ArrayType firstOperand, object arg) {
87  // * Operators >= > <= < are note allowed
88  // * Operators >= > <= < are note allowed
89  if ((relationalOperator == RelationalOperator.GreaterThan
90  || relationalOperator == RelationalOperator.GreaterThanOrEqual
91  || relationalOperator == RelationalOperator.LessThan
92  || relationalOperator == RelationalOperator.LessThanOrEqual
93  )
94  && showErrorMessage) {
95  ErrorManager.Instance.NotifyError(new OperationNotAllowedError(relationalOperator.ToString(), firstOperand.FullName, this.secondOperand.FullName, location));
96 
97  return null;
98  }
99  if (!(secondOperand is NullType))
100  ErrorManager.Instance.NotifyError(new OperationNotAllowedError(relationalOperator.ToString(), this.secondOperand.FullName, location)); ;
101  return null;
102  }
103  #endregion
104 
105  #region BoolType >= this.secondOperand
106  public override object Exec(BoolType firstOperand, object arg) {
107  if (relationalOperator == RelationalOperator.Equal || relationalOperator == RelationalOperator.NotEqual) {
108  if ((int)secondOperand.AcceptOperation(new PromotionLevelOperation(firstOperand), arg) != -1)
109  return BoolType.Instance;
110  if (showErrorMessage) {
111  ErrorManager.Instance.NotifyError(new TypePromotionError(secondOperand.FullName, firstOperand.FullName, location));
112  return null;
113  }
114  }
115  return ReportError(firstOperand);
116  }
117  #endregion
118 
119  #region UnionType >= this.secondOperand
120  public override object Exec(UnionType firstOperand, object arg) {
121  // * If all the types in typeset generate a constraint, we simply generate one constraint using the whole union type
122  if (firstOperand.IsFreshVariable() && methodAnalyzed != null) {
123  // * A constraint is added to the method analyzed
124  RelationalConstraint constraint = new RelationalConstraint(firstOperand, secondOperand, relationalOperator, location);
125  methodAnalyzed.AddConstraint(constraint);
126  return constraint.ReturnType;
127  }
128  bool oneCorrectType = false;
129  foreach (TypeExpression type in firstOperand.TypeSet) {
130  TypeExpression ret = (TypeExpression)type.AcceptOperation(new RelationalOperation(secondOperand, relationalOperator, methodAnalyzed, !firstOperand.IsDynamic && showErrorMessage, location), arg);
131  if (ret == null && !firstOperand.IsDynamic)
132  return null;
133  if (ret != null)
134  oneCorrectType = true;
135  }
136  // * If there has been some errors, they have not been shown because the type is dynamic, we show it
137  if (showErrorMessage && firstOperand.IsDynamic && !oneCorrectType)
138  ErrorManager.Instance.NotifyError(new NoTypeAcceptsOperation(firstOperand.FullName, relationalOperator.ToString(), secondOperand.FullName, location));
139  return BoolType.Instance;
140  }
141 
142  #endregion
143 
144  #region TypeVariable >= this.secondOperand
145  public override object Exec(TypeVariable firstOperand, object arg) {
146  if (firstOperand.Substitution != null) {
147  DynVarOptions.Instance.AssignDynamism(firstOperand.Substitution, firstOperand.IsDynamic);
148  return firstOperand.Substitution.AcceptOperation(this, arg);
149  }
150  if (methodAnalyzed != null) {
151  // * A constraint is added to the method analyzed
152  RelationalConstraint constraint = new RelationalConstraint(firstOperand, secondOperand, relationalOperator, location);
153  methodAnalyzed.AddConstraint(constraint);
154  return constraint.ReturnType;
155  }
156  return ReportError(firstOperand);
157  }
158  #endregion
159 
160  #region FieldType >= this.secondOperand
161  public override object Exec(FieldType firstOperand, object arg) {
162 
163  if (firstOperand.FieldTypeExpression != null)
164  return firstOperand.FieldTypeExpression.AcceptOperation(this, arg);
165  return null;
166  }
167  #endregion
168 
169  #region UserType >= this.secondOperand
170  public override object Exec(UserType firstOperand, object arg)
171  {
172  if (relationalOperator == RelationalOperator.Equal || relationalOperator == RelationalOperator.NotEqual)
173  {
174  if (secondOperand is NullType)
175  return BoolType.Instance;
176  if (this.showErrorMessage)
177  {
178  ErrorManager.Instance.NotifyError(new TypePromotionError(this.secondOperand.FullName, firstOperand.FullName, this.relationalOperator.ToString(), this.location));
179  return null;
180  }
181  }
182  return ReportError(firstOperand);
183  }
184  #endregion
185 
186  #region Report Errors
187  // in our case we only notify operations not allowed
188  // for other pruposes invoke explicitly another kind of error
189  public override object ReportError(TypeExpression tE) {
190  if (this.showErrorMessage)
191  ErrorManager.Instance.NotifyError(new OperationNotAllowedError(this.relationalOperator.ToString(), tE.FullName, this.secondOperand.FullName, this.location));
192  return null;
193  }
194  #endregion
195  }
196 
197 }
Its operation AcceptOperation() returns an integer value that indicates a promotion level between two...
override object Exec(TypeVariable firstOperand, object arg)
override object AcceptOperation(TypeSystemOperation op, object arg)
Definition: BoolType.cs:84
override object Exec(PropertyType firstOperand, object arg)
Representa a union type.
Definition: UnionType.cs:36
Represents a error produced when tries to make an operation not allowed for the specified type...
static DoubleType Instance
Gets the unique instance of DoubleType
Definition: DoubleType.cs:57
override object Exec(DoubleType firstOperand, object arg)
Representa an array type.
Definition: ArrayType.cs:35
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
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
Represents a error produced when the type promotion can not be applied to specified expressions...
override object Exec(UserType firstOperand, object arg)
Represent a character type.
Definition: CharType.cs:38
override object Exec(StringType firstOperand, object arg)
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
RelationalOperation(TypeExpression secondOperand, RelationalOperator relationalOperator, MethodType methodAnalyzed, bool showErrorMessage, Location location)
virtual string FullName
Gets the full name of the type Note: WriteType expression is the longest recursive representation of ...
RelationalOperator
Relational binary operators
override object Exec(UnionType firstOperand, object arg)
override object Exec(ArrayType firstOperand, object arg)
Represent a integer type.
Definition: IntType.cs:37
Represents a double type.
Definition: DoubleType.cs:36
override object ReportError(TypeExpression tE)
Represent a null type.
Definition: NullType.cs:36
TypeExpression FieldTypeExpression
Gets the field type.
Definition: FieldType.cs:58
virtual bool IsDynamic
Indicates if the type has been set as dynamic
override object Exec(BoolType firstOperand, object arg)
override object Exec(FieldType firstOperand, object arg)
override bool IsFreshVariable()
To know if it is a type variable with no substitution
Definition: UnionType.cs:630
Representa a property type.
Definition: PropertyType.cs:34
Representa a method type.
Definition: MethodType.cs:37
It represents constraints of the form: ret := op1 op op2 where op is an relational operator (== != &gt;=...
static BoolType Instance
Gets the unique instance of BoolType
Definition: BoolType.cs:57
override object Exec(TypeExpression firstOperand, object arg)
TypeExpression Substitution
Gets the substitution; null if it does not exist
Represent a bool type.
Definition: BoolType.cs:36
override object Exec(CharType firstOperand, object arg)
TypeExpression PropertyTypeExpression
Gets the property type.
Definition: PropertyType.cs:64
override object Exec(IntType firstOperand, object arg)
Representa a field type.
Definition: FieldType.cs:37
Represents a error produced when a dynamic union type has no valid type to be applied an operation ...
Represents a class or interface type.
Definition: UserType.cs:31
virtual object AcceptOperation(TypeSystemOperation op, object arg)