The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
CGAritmethicOperation.cs
Go to the documentation of this file.
1 using TypeSystem.Operations;
2 using TypeSystem;
3 using ErrorManagement;
4 using AST;
5 using Tools;
6 using System.Collections.Generic;
7 using CodeGeneration.ExceptionManagement;
8 namespace CodeGeneration.Operations {
9 
10  internal class CGArithmeticOperation<T> : CGBinaryOperation<T> where T : ILCodeGenerator {
11 
12  public CGArithmeticOperation(VisitorILCodeGeneration<T> visitor, object obj, int indent,
13  ArithmeticExpression node) : base(visitor, obj, indent, node) { }
14  private static UnionType GenerateAllUnionTypes() {
15  UnionType unions = new UnionType(StringType.Instance);
16  unions.AddType(CharType.Instance);
17  unions.AddType(IntType.Instance);
18  unions.AddType(DoubleType.Instance);
19  return unions;
20 
21  }
22  private static UnionType allUnions = GenerateAllUnionTypes();
23  private static UnionType stringUnions = new UnionType(StringType.Instance);
24 
25  protected override UnionType GenerateAll(CGBinaryOperationsInfo ai, BinaryExpression b) {
26  if ( ( (ArithmeticExpression)b ).Operator == ArithmeticOperator.Plus )
27  return stringUnions;
28  return allUnions;
29  }
36  protected override TypeExpression MajorType(TypeExpression typeExpression1, TypeExpression typeExpression2) {
37  return (TypeExpression)typeExpression1.AcceptOperation(new MajorTypeForArithMeticOperation(typeExpression2, (ArithmeticExpression)node), null);
38  }
45  protected override void GenerateOperator(CGBinaryOperationsInfo cga, TypeExpression result, BinaryExpression node) {
46  switch (((ArithmeticExpression)node).Operator) {
47  case ArithmeticOperator.Minus:
48  this.codeGenerator.sub(this.indent);
49  break;
50  case ArithmeticOperator.Plus:
51  if ( TypeExpression.Is<StringType>(result))
52  this.codeGenerator.concat(this.indent);
53  else
54  this.codeGenerator.add(this.indent);
55  break;
56  case ArithmeticOperator.Mult:
57  this.codeGenerator.mul(this.indent);
58  break;
59  case ArithmeticOperator.Div:
60  this.codeGenerator.div(this.indent);
61  break;
62  case ArithmeticOperator.Mod:
63  this.codeGenerator.rem(this.indent);
64  break;
65  default: // Error
66  this.codeGenerator.Comment(string.Format("Error operation {0} {1} {2}", cga.info[0].typeExpression.FullName, ( (ArithmeticExpression)node ).Operator, cga.info[1].typeExpression.FullName));
67  break;
68  }
69 
70  }
71  public override object ReportError(TypeExpression tE) {
72  ErrorManager.Instance.NotifyError(new CodeGenerationError("No se ha definido la operación solicitada"));
73  return null;
74  }
75 
76  }
77 }
static StringType Instance
Gets the unique instance of StringType
Definition: StringType.cs:57
Encapsulates a binary expression of our programming language.
It typechecks the runtime arguments, embeded in a method call, with the parametes of this method...
Representa a union type.
Definition: UnionType.cs:36
This class walks the AST to obtain the IL code.
Represent a string type.
Definition: StringType.cs:36
Encapsulates arithmetic binary expressions.
Abstract class that represents all different types.
Represents a error produced when a MethodType has class information and tries to assign other class i...
ArithmeticOperator
Arithmetic binary operators