The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
ArithmeticConstraint.cs
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
3 // Project rROTOR
4 // --------------------------------------------------------------------------
5 // File: ArithmeticConstraint.cs
6 // Author: Francisco Ortin - francisco.ortin@gmail.com
7 // Description:
8 // Arithmetic or bitwise constraint to be executed each time the associated method
9 // has been called.
10 // It represents constraints of the form:
11 // ret := op1 op op2
12 // where op is a binary arithmetical operator (+ - / * %) or a bitwise operator (& | ^) or
13 // ret := op op1
14 // where op is a unary arithmetical operator (+ - ++ --) or a bitwise operator (~)
15 // Implements Composite pattern [Leaf].
16 // Implements Command pattern [Concrete Command].
17 // Implements Memento pattern [Memento].
18 // --------------------------------------------------------------------------
19 // Create date: 24-04-2007
20 // Modification date: 24-04-2007
22 
23 using System;
24 using System.Collections.Generic;
25 using System.Text;
26 
27 using AST;
28 using ErrorManagement;
29 using Tools;
30 using TypeSystem.Operations;
31 
32 namespace TypeSystem.Constraints {
39 
40  #region Fields
41  private TypeExpression secondOperand;
45 
49  private Enum op;
50 
54  private bool isBinary;
55 
56  #endregion
57 
58  #region Properties
59  public TypeExpression SecondOperand {
63  get { return this.secondOperand; }
64  }
65 
69  public Enum BinaryOperator {
70  get {
71  if (!this.IsBinary)
72  throw new InvalidOperationException("Not binary operator is defined for unary expressions");
73  return this.op;
74  }
75  }
76 
81  get {
82  if (this.IsBinary)
83  throw new InvalidOperationException("Not unary operator is defined for binary expressions");
84  return (UnaryOperator)this.op;
85  }
86  }
87 
91  public bool IsBinary {
92  get { return this.isBinary; }
93  }
94  #endregion
95 
96 
97  #region Constructor
98  public ArithmeticConstraint(TypeExpression firstOperand, TypeExpression secondOperand, Enum op, Location location)
106  : base(firstOperand, location) {
107  this.secondOperand = secondOperand;
108  this.op = op;
109  this.isBinary = true; // * Binary
110  }
119  private ArithmeticConstraint(TypeExpression firstOperand, TypeExpression secondOperand, TypeVariable returnType, Enum op, Location location)
120  : base(firstOperand, returnType, location) {
121  this.secondOperand = secondOperand;
122  this.op = op;
123  this.isBinary = true; // * Binary
124  }
131  public ArithmeticConstraint(TypeExpression firstOperand, UnaryOperator op, Location location)
132  : base(firstOperand, location) {
133  this.op = op;
134  this.isBinary = false; // * Unary
135  }
143  private ArithmeticConstraint(TypeExpression firstOperand, TypeVariable returnType, UnaryOperator op, Location location)
144  : base(firstOperand, returnType, location) {
145  this.op = op;
146  this.isBinary = false; // * Unary
147  }
148  #endregion
149 
150  #region BuildTypeExpressionString()
151  protected override string BuildTypeExpressionString() {
152  if (this.IsBinary)
153  // * Binary
154  return String.Format("{0}:={1} {2} {3}", this.ReturnType.FullName, this.FirstOperand.FullName, op.ToString(), this.SecondOperand.FullName);
155  // * Unary
156  return String.Format("{0}:={1} {2}", this.ReturnType.FullName, op.ToString(), this.FirstOperand.FullName);
157  }
158  #endregion
159 
160  #region CloneTypeVariables()
161  public override Constraint CloneTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<EquivalenceClass> equivalenceClasses) {
170  TypeExpression newFirstOperand = this.FirstOperand.CloneTypeVariables(typeVariableMappings, equivalenceClasses, new List<ClassType>());
171  TypeVariable newReturnType = (TypeVariable)this.ReturnType.CloneTypeVariables(typeVariableMappings, equivalenceClasses, new List<ClassType>());
172  if (this.IsBinary) {
173  // * Binary
174  TypeExpression newSecondOperand = this.SecondOperand.CloneTypeVariables(typeVariableMappings, equivalenceClasses, new List<ClassType>());
175  return new ArithmeticConstraint(newFirstOperand, newSecondOperand, newReturnType, this.BinaryOperator, this.Location);
176  }
177  // * Unary
178  return new ArithmeticConstraint(newFirstOperand, newReturnType, this.UnaryOperator, this.Location);
179  }
180  #endregion
181 
182  #region Check()
183  public override TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage,
191  SortOfUnification activeSortOfUnification, Location location) {
192  TypeExpression result;
193  if (this.IsBinary)
194  result= (TypeExpression) this.FirstOperand.AcceptOperation(ArithmeticalOperation.Create(this.SecondOperand, this.BinaryOperator, methodAnalyzed, true, this.Location), null);
195  else // * Unary
196  result = (TypeExpression)this.FirstOperand.AcceptOperation(ArithmeticalOperation.Create(this.UnaryOperator, methodAnalyzed, true, this.Location), null);
197  if (result == null && showInvocationMessage) {
198  ErrorManager.Instance.NotifyError(new ConstraintError(location));
199  return null;
200  }
201  // * If no error exists, we unify the return type variable with the actual result
202  this.ReturnType.Unify(result, SortOfUnification.Equivalent, new List<Pair<TypeExpression, TypeExpression>>());
203  this.ReturnType.ValidTypeExpression = this.ValidTypeExpression = false;
204  return this.ReturnType;
205  }
206  #endregion
207 
208 
209  }
210 }
override TypeExpression CloneTypeVariables(IDictionary< TypeVariable, TypeVariable > typeVariableMappings, IList< EquivalenceClass > equivalenceClasses, IList< ClassType > clonedClasses)
Method that clones each type variable of a type expression. Equivalence classes are not cloned (but i...
Represents a error produced when a constraint has not been satisfied
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
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
ArithmeticConstraint(TypeExpression firstOperand, UnaryOperator op, Location location)
Constructor for unary arithmetic operations
UnaryOperator
Unary operators
This class instantiates elements of type UnaryArithmeticalOperation, and BinaryArithmeticalOperation...
It represents constraints of the form: ret := op1 op op2 Where op is an arithmetic operator ...
static ArithmeticalOperation Create(UnaryOperator unaryOperator, MethodType methodAnalyzed, bool showErrorMessage, Location location)
A factory Method to create UnaryAritmeticaslOperation objects Implements a factory method...