public void Visit(LessThanOperator lessThan) { lessThan.Left.Accept(this); var leftOperand = tacs.Last().Result; lessThan.Right.Accept(this); var rightOperand = tacs.Last().Result; var result = MakeNewTemp(); var label1 = MakeNewLabel(); var label2 = MakeNewLabel(); tacs.Add(Tac.IfLessThan(leftOperand, rightOperand, label1)); tacs.Add(Tac.Assign("0", result)); tacs.Add(Tac.Goto(label2)); tacs.Add(Tac.Label(label1, result)); tacs.Add(Tac.Assign("1", result)); tacs.Add(Tac.Label(label2, result)); }
public void Visit(EqualsOperator equals) { equals.Left.Accept(this); var leftOperand = tacs.Last().Result; equals.Right.Accept(this); var rightOperand = tacs.Last().Result; var result = MakeNewTemp(); var label1 = MakeNewLabel(); var label2 = MakeNewLabel(); tacs.Add(Tac.IfEqual(leftOperand, rightOperand, label1)); tacs.Add(Tac.Assign("0", result)); tacs.Add(Tac.Goto(label2)); tacs.Add(Tac.Label(label1, result)); tacs.Add(Tac.Assign("1", result)); tacs.Add(Tac.Label(label2, result)); }
public void Visit(IfThenElse ifthenelse) { var elseLabel = MakeNewLabel(); var endLabel = MakeNewLabel(); var resultVariable = MakeNewTemp(); ifthenelse.IfExpr.Accept(this); var ifoperand = tacs.Last().Result; tacs.Add(Tac.IfFalse(ifoperand, elseLabel)); ifthenelse.ThenExpr.Accept(this); tacs.Add(Tac.Assign(tacs.Last().Result, resultVariable)); tacs.Add(Tac.Goto(endLabel)); tacs.Add(Tac.Label(elseLabel, resultVariable)); ifthenelse.ElseExpr.Accept(this); tacs.Add(Tac.Assign(tacs.Last().Result, resultVariable)); tacs.Add(Tac.Label(endLabel, resultVariable)); }
public void Visit(AndOperator and) { var falseLabel = MakeNewLabel(); var endLabel = MakeNewLabel(); and.Left.Accept(this); var leftOperand = tacs.Last().Result; tacs.Add(Tac.IfFalse(leftOperand, falseLabel)); and.Right.Accept(this); var rightOperand = tacs.Last().Result; tacs.Add(Tac.IfFalse(rightOperand, falseLabel)); var resultVariable = MakeNewTemp(); tacs.Add(Tac.Assign("1", resultVariable)); tacs.Add(Tac.Goto(endLabel)); tacs.Add(Tac.Label(falseLabel, resultVariable)); tacs.Add(Tac.Assign("0", resultVariable)); tacs.Add(Tac.Label(endLabel, resultVariable)); }
public void Visit(OrOperator or) { var trueLabel = MakeNewLabel(); var endLabel = MakeNewLabel(); or.Left.Accept(this); var leftOperand = tacs.Last().Result; tacs.Add(Tac.IfTrue(leftOperand, trueLabel)); or.Right.Accept(this); var rightOperand = tacs.Last().Result; tacs.Add(Tac.IfTrue(rightOperand, trueLabel)); var resultVariable = MakeNewTemp(); tacs.Add(Tac.Assign("0", resultVariable)); tacs.Add(Tac.Goto(endLabel)); tacs.Add(Tac.Label(trueLabel, resultVariable)); tacs.Add(Tac.Assign("1", resultVariable)); tacs.Add(Tac.Label(endLabel, resultVariable)); }