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 System;
2 using CodeGeneration.Operations;
3 using TypeSystem;
4 using AST;
5 
6 namespace CodeGeneration.NewOperations
7 {
8 
9  internal class CGArithmeticOperation<T> : NewOperations.CGBinaryOperation<T> where T : ILCodeGenerator
10  {
11 
12 
13  public CGArithmeticOperation(VisitorILCodeGeneration<T> visitor, object obj, int indent, ArithmeticExpression node) : base(visitor, obj, indent, node)
14  {
15  LoadSecondOperand();
16  }
17 
18  protected override sealed void LoadSecondOperand()
19  {
20  if (String.IsNullOrEmpty(label_op2))
21  {
22  this.node.SecondOperand.Accept(this.visitor, this.obj);
23  label_op2 = "tempvar_" + this.codeGenerator.NewLabel;
24  if (IsInternallyAnObject(node.SecondOperand))
25  this.codeGenerator.WriteAuxiliarLocalVariable(this.indent, label_op2,"object");
26  else
27  this.codeGenerator.WriteAuxiliarLocalVariable(this.indent, label_op2, this.node.SecondOperand.ILTypeExpression.ILType());
28  this.codeGenerator.stloc(this.indent, label_op2);
29  }
30  else
31  this.codeGenerator.ldloc(this.indent, label_op2);
32  }
33 
34  protected override void GenerateRightOperand(TypeExpression teLeft, UnionType teRight)
35  {
36  if (node is ArithmeticExpression)
37  {
38  label_result = "tempvar_" + this.codeGenerator.NewLabel;
39  this.codeGenerator.WriteAuxiliarLocalVariable(this.indent, label_result, node.ExpressionType.ILType());
40  }
41  base.GenerateRightOperand(teLeft,teRight);
42  }
43 
44  protected override UnionType GenerateAllUnionTypes()
45  {
46  UnionType unions = new UnionType();
47  unions.AddType(IntType.Instance);
48  unions.AddType(DoubleType.Instance);
49  unions.AddType(CharType.Instance);
50  if(((ArithmeticExpression)node).Operator == ArithmeticOperator.Plus)
51  unions.AddType(StringType.Instance);
52  return unions;
53  }
54 
55  protected override TypeExpression MajorType(TypeExpression typeExpression1, TypeExpression typeExpression2)
56  {
57  return (TypeExpression)typeExpression1.AcceptOperation(new MajorTypeForArithMeticOperation(typeExpression2, (ArithmeticExpression)node), null);
58  }
59 
60  protected override void GenerateOperator(BinaryExpression node, TypeExpression result)
61  {
62  ArithmeticOperator arithmeticOperator = ((ArithmeticExpression) node).Operator;
63  bool isConcat = false;
64  switch (arithmeticOperator)
65  {
66  case ArithmeticOperator.Minus:
67  this.codeGenerator.sub(this.indent);
68  break;
69  case ArithmeticOperator.Plus:
70  if (TypeExpression.Is<StringType>(result))
71  {
72  isConcat = true;
73  this.codeGenerator.concat(this.indent);
74  }
75  else
76  this.codeGenerator.add(this.indent);
77  break;
78  case ArithmeticOperator.Mult:
79  this.codeGenerator.mul(this.indent);
80  break;
81  case ArithmeticOperator.Div:
82  this.codeGenerator.div(this.indent);
83  break;
84  case ArithmeticOperator.Mod:
85  this.codeGenerator.rem(this.indent);
86  break;
87  default: // Error
88  this.codeGenerator.Comment("ERROR");
89  break;
90  }
91  if (!isConcat && !IsValueType(node.ExpressionType) && !(node.ExpressionType is StringType))
92  this.codeGenerator.Box(indent, result);
93  if (!string.IsNullOrEmpty(label_result))
94  {
95  this.codeGenerator.stloc(this.indent, label_result);
96  }
97  }
98 
99 
100  }
101 }
Encapsulates a binary expression of our programming language.
Representa a union type.
Definition: UnionType.cs:36
This class walks the AST to obtain the IL code.
TypeExpression ExpressionType
Gets or sets the type of the expression.
Definition: Expression.cs:71
Represent a string type.
Definition: StringType.cs:36
Encapsulates arithmetic binary expressions.
Abstract class that represents all different types.
Expression SecondOperand
Gets the second operand of the binary expression
ArithmeticOperator
Arithmetic binary operators