public void Convert(ASTNode node) { if (node.Left != null && node.Left.Operand == null && node.Left.Computed == false) Convert(node.Left); if (node.Right != null && node.Right.Operand == null && node.Right.Computed == false) Convert(node.Right); if (node.Operator > ASTOperator.Single && node.Operator < ASTOperator.End) { } else if (node.Operator > ASTOperator.Binary && node.Operator < ASTOperator.Single) { if (node.Operator == ASTOperator.Assign) { Console.WriteLine("{1} {0} {2}", node.Operator, node.Left.ToSSA(), node.Right.ToSSA()); } else Console.WriteLine("rA <- {1} {0} {2}", node.Operator, node.Left.ToSSA(), node.Right.ToSSA()); } else { throw new Exception(); } }
public void ComputeDouble() { if (!this.Right.Computed || !this.Left.Computed) { if (this.Operator != ASTOperator.Add) // FIXME Commutable/Associativity return; if (this.Left.Computed) { var temp = this.Left; this.Left = this.Right; this.Right = temp; } if (this.Right.Computed && this.Operator == this.Left.Operator && this.Left.Right.Computed) { var temp = this.Left; this.Left = this.Right; this.Right = temp; temp = this.Left; this.Left = Right.Left; this.Right.Left = temp; this.Right.ComputeDouble(); /* * + + + * + 4 4 + x +_ * x 3 x 3 4 3 * * + * * * 12 +_6 * + 6 + 2 * x 4 x 4 * */ } return; } switch (this.Operator) { // FIXME check Types case ASTOperator.Add: this.IValue = this.Left.IValue + this.Right.IValue; break; case ASTOperator.Sub: this.IValue = this.Left.IValue - this.Right.IValue; break; case ASTOperator.Mul: this.IValue = this.Left.IValue * this.Right.IValue; break; case ASTOperator.Div: if (this.Right.IValue != 0) this.IValue = this.Left.IValue / this.Right.IValue; else { // WRITE THIS WILL ALWAYS TRIGGER AN ERROR return; } break; case ASTOperator.Rem: if (this.Right.IValue != 0) this.IValue = this.Left.IValue % this.Right.IValue; else { // WRITE THIS WILL ALWAYS TRIGGER AN ERROR return; } break; case ASTOperator.And: case ASTOperator.Or: case ASTOperator.Xor: case ASTOperator.Shl: case ASTOperator.Lshr: case ASTOperator.Ashr: return; case ASTOperator.Less: this.BValue = this.Left.IValue < this.Right.IValue; break; case ASTOperator.LessEq: this.BValue = this.Left.IValue <= this.Right.IValue; break; case ASTOperator.More: this.BValue = this.Left.IValue > this.Right.IValue; break; case ASTOperator.MoreEq: this.BValue = this.Left.IValue >= this.Right.IValue; break; case ASTOperator.Equals: this.BValue = this.Left.IValue == this.Right.IValue; break; case ASTOperator.NotEquals: this.BValue = this.Left.IValue != this.Right.IValue; break; default: throw new Exception(); } this.Computed = true; }
public ASTStatement PushOperator(Token token, ASTOperator opcode) { ASTNode node = new ASTNode(token, opcode); while (inFixStack.Count > 0 && inFixStack.Peek().Priority > node.Priority) { ASTNode nd = inFixStack.Pop(); addPostFixOperator(nd); } inFixStack.Push(node); return this; }
public ASTStatement PushOperand(Token token, ASTOperand operand) { ASTNode node = new ASTNode(token, operand); addPostFixOperand(node); return this; }
public ASTStatement PushInteger(Token token, ASTType type, long value) { ASTNode node = new ASTNode(token, type, value); addPostFixOperand(node); return this; }
public ASTStatement OpenParenthesis(Token token) { ASTNode node = new ASTNode(token, ASTOperator.Parenthesis); inFixStack.Push(node); return this; }
private void addPostFixOperator(ASTNode node) { if (node.Operator > ASTOperator.Single && node.Operator < ASTOperator.End) { if (postFixStack.Count < 1) { throw new Exception(); } ASTNode oprd1 = postFixStack.Pop(); node.Right = oprd1; node.ComputeSingle(); } else if (node.Operator > ASTOperator.Binary && node.Operator < ASTOperator.Single) { if (postFixStack.Count < 2) { throw new Exception(); } ASTNode oprd2 = postFixStack.Pop(); ASTNode oprd1 = postFixStack.Pop(); // Assign have Associativity R to L node.Left = oprd1; node.Right = oprd2; node.ComputeDouble(); } else { throw new Exception(); } postFixStack.Push(node); // Write SSA }
private void addPostFixOperand(ASTNode node) { postFixStack.Push(node); }