The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
UnaryArithmeticalOperation.cs
Go to the documentation of this file.
1 using TypeSystem;
2 using AST;
3 using ErrorManagement;
4 using DynVarManagement;
5 using TypeSystem.Constraints;
6 //TODO: HAbrĂ¡ que ver que se hace con StringtYPE, con el NULL
7 namespace TypeSystem.Operations {
14  #region Fields
17  protected bool showErrorMessage;
18  protected Location location;
19  #endregion
20 
21  #region Constructor T
22  public UnaryArithmeticalOperation(UnaryOperator unaryOperator, MethodType methodAnalyzed, bool showErrorMessage, Location location) {
23  this.unaryOperator = unaryOperator;
24  this.methodAnalyzed = methodAnalyzed;
25  this.showErrorMessage = showErrorMessage;
26  this.location = location;
27  }
28  #endregion
29 
30  #region +- FieldType
31  public override object Exec(FieldType operand, object arg) {
32  return operand.FieldTypeExpression.AcceptOperation(this, arg);
33  }
34  #endregion
35 
36  #region +- PropertyType
37  public override object Exec(PropertyType operand, object arg) {
38  return operand.PropertyTypeExpression.AcceptOperation(this, arg);
39  }
40  #endregion
41 
42  #region +- TypeVariable
43  public override object Exec(TypeVariable operand, object arg) {
44  if (operand.Substitution != null) {
45  DynVarOptions.Instance.AssignDynamism(operand.Substitution, operand.IsDynamic);
46  return operand.Substitution.AcceptOperation(this, arg);
47  }
48  if (methodAnalyzed != null) {
49  // * A constraint is added to the method analyzed
50  ArithmeticConstraint constraint = new ArithmeticConstraint(operand, unaryOperator, location);
51  methodAnalyzed.AddConstraint(constraint);
52  return constraint.ReturnType;
53  }
54  return ReportError(operand);
55  }
56  #endregion
57 
58  #region +- \/...
59 
60  public override object Exec(UnionType operand, object arg) {
61  // * If all the types in typeset generate a constraint, we simply generate one constraint using the whole union type
62  if (operand.IsFreshVariable() && methodAnalyzed != null) {
63  // * A constraint is added to the method analyzed
64  ArithmeticConstraint constraint = new ArithmeticConstraint(operand, unaryOperator, location);
65  methodAnalyzed.AddConstraint(constraint);
66  return constraint.ReturnType;
67  }
68  TypeExpression returnType = null;
69  foreach (TypeExpression type in operand.TypeSet) {
70  TypeExpression ret = (TypeExpression)type.AcceptOperation(ArithmeticalOperation.Create(unaryOperator, methodAnalyzed, !operand.IsDynamic && showErrorMessage, location), arg);
71  if (ret == null && !operand.IsDynamic)
72  return null;
73  if (ret != null)
74  returnType = UnionType.collect(returnType, ret);
75  }
76  // * If there has been some errors, they have not been shown because the type is dynamic, we show it
77  if (showErrorMessage && operand.IsDynamic && returnType == null)
78  ErrorManager.Instance.NotifyError(new NoTypeAcceptsOperation(operand.FullName, unaryOperator.ToString(), location));
79  return returnType;
80  }
81  #endregion
82 
83  #region +- BoolType
84 
85  public override object Exec(BoolType operand, object arg) {
86  return ReportError(operand);
87  }
88  #endregion
89 
90  #region +- Double
91 
92  public override object Exec(DoubleType operand, object arg) {
93  return DoubleType.Instance;
94  }
95  #endregion
96 
97  #region +- IntType
98 
99  public override object Exec(IntType operand, object arg) {
100  return IntType.Instance;
101  }
102  #endregion
103 
104  #region +- CharType
105 
106  public override object Exec(CharType operand, object arg) {
107  return CharType.Instance;
108  }
109  #endregion
110 
111 
112  #region Report Errors
113  // in our case we only notify operations not allowed
114  // for other pruposes invoke explicitly another kind of error
115  public override object ReportError(TypeExpression tE) {
116  if (this.showErrorMessage)
117  ErrorManager.Instance.NotifyError(new OperationNotAllowedError(this.unaryOperator.ToString(), tE.FullName, this.location));
118  return null;
119  }
120  #endregion
121  }
122 
123 }
124 
static CharType Instance
Gets the unique instance of CharType
Definition: CharType.cs:58
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(CharType operand, object arg)
override object Exec(FieldType operand, object arg)
override object Exec(BoolType operand, object arg)
UnaryArithmeticalOperation(UnaryOperator unaryOperator, MethodType methodAnalyzed, bool showErrorMessage, Location location)
override object Exec(PropertyType operand, object arg)
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
Class Unary AritmethicalOperation Implements: Factory method Role:
override object Exec(DoubleType operand, object arg)
Represent a character type.
Definition: CharType.cs:38
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
Represent a integer type.
Definition: IntType.cs:37
override object Exec(TypeVariable operand, object arg)
Represents a double type.
Definition: DoubleType.cs:36
virtual bool IsDynamic
Indicates if the type has been set as dynamic
UnaryOperator
Unary operators
This class instantiates elements of type UnaryArithmeticalOperation, and BinaryArithmeticalOperation...
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 arithmetic operator ...
static IntType Instance
Gets the unique instance of IntType
Definition: IntType.cs:57
override object Exec(IntType operand, object arg)
TypeExpression Substitution
Gets the substitution; null if it does not exist
Represent a bool type.
Definition: BoolType.cs:36
static ArithmeticalOperation Create(UnaryOperator unaryOperator, MethodType methodAnalyzed, bool showErrorMessage, Location location)
A factory Method to create UnaryAritmeticaslOperation objects Implements a factory method...
override object Exec(UnionType operand, 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 ...
virtual object AcceptOperation(TypeSystemOperation op, object arg)