Пример #1
0
        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");
        }
Пример #2
0
        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");
        }
Пример #3
0
 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));
     }
Пример #4
0
 private void AddCode(ThreeCode c)
 {
     if (currentLabel.Length == 0)
     {
         program.AddLast(c);
         return;
     }
     if (c.label.Length != 0 && currentLabel != c.label)
     {
         throw new Exception("Core error");
     }
     c.label      = currentLabel;
     currentLabel = "";
     program.AddLast(c);
 }
Пример #5
0
        public override string ToString()
        {
            string res = "";
            string lbl = "";

            if (label.Length > 0)
            {
                lbl = label + ":";
            }
            res += $"{lbl,-11}";

            if (operation == ThreeOperator.None)
            {
                return(res);
            }

            if (operation == ThreeOperator.Goto)
            {
                return(res + "goto " + arg1.ToString());
            }

            if (operation == ThreeOperator.IfGoto)
            {
                res += "if " + arg1.ToString() + " goto " + arg2.ToString();
                return(res);
            }

            if (operation == ThreeOperator.Println)
            {
                return(res + "println " + arg1.ToString());
            }

            res += result + " = ";
            if (operation == ThreeOperator.Logic_not)
            {
                return(res + "!" + arg1.ToString());
            }
            if (operation == ThreeOperator.Assign)
            {
                res += arg1.ToString();
            }
            else
            {
                res += arg1.ToString() + " " + ThreeCode.GetOperatorString(operation) + " " + arg2.ToString();
            }

            return(res);
        }
Пример #6
0
 public GraphNode(ThreeCode code, GraphNode l, GraphNode r)
 {
     this.code = code;
     left      = l;
     right     = r;
 }
Пример #7
0
 public GraphNode(ThreeCode code)
 {
     this.code = code;
     left      = null;
     right     = null;
 }
Пример #8
0
 public bool RightPartsEquals(ThreeCode x, ThreeCode y)
 {
     return(x.arg1.ToString() == y.arg1.ToString() && x.arg2.ToString() == y.arg2.ToString());
 }