private ThreeAddressValueType GenVariable(LogicExprNode expr) { if (expr is BooleanNode) { return(new ThreeAddressLogicValue((expr as BooleanNode).Val)); } if (expr is LogicIdNode) { return(new ThreeAddressStringValue((expr as LogicIdNode).Name.Name)); } if (expr is LogicOpNode) { LogicOpNode op = expr as LogicOpNode; string res = GenTempVariable(); ThreeAddressValueType arg1 = GenVariable(op.Left); ThreeAddressValueType arg2 = GenVariable(op.Right); ThreeOperator p = ThreeCode.ParseOperator(op.Operation); AddCode(new ThreeCode(res, p, arg1, arg2)); return(new ThreeAddressStringValue(res)); } if (expr is LogicNotNode) { LogicNotNode lnot = expr as LogicNotNode; string res = GenTempVariable(); ThreeAddressValueType arg1 = GenVariable(lnot.LogExpr); AddCode(new ThreeCode(res, ThreeOperator.Logic_not, arg1)); return(new ThreeAddressStringValue(res)); } throw new Exception("UNKNOW VALUE. Send autors of ThreeAddressCode"); }
private ThreeAddressValueType GenVariable(ExprNode expr) { if (expr is IdNode) { return(new ThreeAddressStringValue((expr as IdNode).Name)); } if (expr is DoubleNumNode) { return(new ThreeAddressDoubleValue((expr as DoubleNumNode).Num)); } if (expr is IntNumNode) { return(new ThreeAddressIntValue((expr as IntNumNode).Num)); } if (expr is BinOpNode) { BinOpNode op = expr as BinOpNode; string res = GenTempVariable(); ThreeAddressValueType arg1 = GenVariable(op.Left); ThreeAddressValueType arg2 = GenVariable(op.Right); ThreeOperator p = ThreeCode.ParseOperator(op.Op); AddCode(new ThreeCode(res, p, arg1, arg2)); return(new ThreeAddressStringValue(res)); } throw new Exception("UNKNOW VALUE. Send autors of ThreeAddressCode"); }
public ThreeCode(string res, ThreeAddressValueType a1) { label = ""; result = res; arg1 = a1; arg2 = null; operation = ThreeOperator.Assign; }
public ThreeCode(string res, ThreeOperator op, ThreeAddressValueType a1, ThreeAddressValueType a2 = null) { label = ""; result = res; arg1 = a1; arg2 = a2; operation = op; }
public override void VisitAssignNode(AssignNode a) { if (a.Expr is BinOpNode binOp && (binOp.Left is IdNode || binOp.Left is DoubleNumNode || binOp.Left is IntNumNode) && (binOp.Right is IdNode || binOp.Right is DoubleNumNode || binOp.Right is IntNumNode)) { ThreeAddressValueType arg1 = GenVariable(binOp.Left); ThreeAddressValueType arg2 = GenVariable(binOp.Right); AddCode(new ThreeCode(a.Id.ToString(), ThreeCode.ParseOperator(binOp.Op), arg1, arg2)); }
public override void VisitIfNode(IfNode ifn) { string label_true = GenLabel(); string label_end = GenLabel(); ThreeAddressValueType expr = GenVariable(ifn.Cond); AddCode(new ThreeCode("", ThreeOperator.IfGoto, expr, new ThreeAddressStringValue(label_true))); if (ifn.Else != null) { ifn.Else.Visit(this); } AddCode(new ThreeCode("", ThreeOperator.Goto, new ThreeAddressStringValue(label_end), null)); currentLabel = label_true; ifn.If.Visit(this); currentLabel = label_end; }
public override void VisitWhileNode(WhileNode w) { string expr = GenTempVariable(); string label_start = currentLabel.Length > 0 ? currentLabel : GenLabel(); currentLabel = label_start; ThreeAddressValueType val = GenVariable(w.Expr); AddCode(new ThreeCode(expr, ThreeOperator.Assign, val, null)); string label_middle = GenLabel(); string label_end = GenLabel(); AddCode(new ThreeCode("", ThreeOperator.IfGoto, new ThreeAddressStringValue(expr), new ThreeAddressStringValue(label_middle))); AddCode(new ThreeCode("", ThreeOperator.Goto, new ThreeAddressStringValue(label_end), null)); currentLabel = label_middle; w.Stat.Visit(this); AddCode(new ThreeCode("", ThreeOperator.Goto, new ThreeAddressStringValue(label_start), null)); currentLabel = label_end; }
public override void VisitForNode(ForNode f) { ThreeAddressValueType expr = GenVariable(f.Start); AddCode(new ThreeCode(f.Id.Name, expr)); string label_start = GenLabel(); string label_middle = GenLabel(); string label_end = GenLabel(); string b = GenTempVariable(); currentLabel = label_start; ThreeAddressValueType val = GenVariable(f.End); AddCode(new ThreeCode(b, ThreeOperator.Logic_less, new ThreeAddressStringValue(f.Id.Name), val)); AddCode(new ThreeCode("", ThreeOperator.IfGoto, new ThreeAddressStringValue(b), new ThreeAddressStringValue(label_middle))); AddCode(new ThreeCode("", ThreeOperator.Goto, new ThreeAddressStringValue(label_end), null)); currentLabel = label_middle; f.Stat.Visit(this); AddCode(new ThreeCode(f.Id.Name, ThreeOperator.Plus, new ThreeAddressStringValue(f.Id.Name), new ThreeAddressIntValue(1))); AddCode(new ThreeCode("", ThreeOperator.Goto, new ThreeAddressStringValue(label_start), null)); currentLabel = label_end; }