Exemplo n.º 1
0
        public override void VisitBinOpNode(BinOpNode binop)
        {
            var toBrL = binop.ToBracketLeft;
            var toBrR = binop.ToBracketRight;

            if (toBrL)
            {
                Text.Append("( ");
            }
            binop.Left.Visit(this);
            if (toBrL)
            {
                Text.Append(" )");
            }
            Text.Append($" {binop.Op} ");
            if (toBrR)
            {
                Text.Append("( ");
            }
            binop.Right.Visit(this);
            if (toBrR)
            {
                Text.Append(" )");
            }
        }
        public override void VisitBinOpNode(BinOpNode binop)
        {
            base.VisitBinOpNode(binop);
            if (binop.Left is IntNumNode left && binop.Right is IntNumNode right)
            {
                var num = 0;

                switch (binop.Op)
                {
                case "+":
                    num = left.Num + right.Num;
                    break;

                case "-":
                    num = left.Num - right.Num;
                    break;

                case "*":
                    num = left.Num * right.Num;
                    break;

                case "/":
                    num = left.Num / right.Num;
                    break;
                }

                var newInt = new IntNumNode(num);
                ReplaceExpr(binop, newInt);
            }
        }
Exemplo n.º 3
0
        public ParseResult BinaryOp(string methodName, Token.TokenType[] ops)
        {
            MethodInfo method = this.GetType().GetMethod(methodName);
            var        result = new ParseResult();
            Node       left   = result.Register((ParseResult)method.Invoke(this, null));

            if (result.error)
            {
                return(result);
            }
            Token opToken;
            Node  right;

            while (ops.Contains(this.currentToken.type))
            {
                opToken = this.currentToken;
                result.Register(this.Advance());
                right = result.Register((ParseResult)method.Invoke(this, null));
                if (result.error)
                {
                    return(result);
                }
                left = new BinOpNode(opToken, left, right);
            }

            return(result.Success(left));
        }
Exemplo n.º 4
0
        public override void VisitBinOpNode(BinOpNode binop)
        {
            binop.Left.Visit(this);
            binop.Right.Visit(this);
            switch (binop.Op)
            {
            case '+':
                genc.Emit(OpCodes.Add);
                break;

            case '-':
                genc.Emit(OpCodes.Sub);
                break;

            case '*':
                genc.Emit(OpCodes.Mul);
                break;

            case '/':
                genc.Emit(OpCodes.Div);
                break;

            case '%':
                genc.Emit(OpCodes.Rem);
                break;
            }
        }
 public Code List(params Code[] values) {
     Node list = null;
     for (int i = 0; i < values.Length; i++) { 
         list = new BinOpNode(NodeType.tpList, NodeTag.opNop, list, (Node)values[i]);
     }
     return list;
 }
Exemplo n.º 6
0
        public override void VisitBinOpNode(BinOpNode binop)
        {
            binop.Left.Visit(this);
            binop.Right.Visit(this);
            switch (binop.Op)
            {
            case "+":
                genc.Emit(OpCodes.Add);
                break;

            case "-":
                genc.Emit(OpCodes.Sub);
                break;

            case "*":
                genc.Emit(OpCodes.Mul);
                break;

            case "/":
                genc.Emit(OpCodes.Div);
                break;

            case "div":
                genc.Emit(OpCodes.Div);
                break;

            case "%":
            case "mod":
                genc.Emit(OpCodes.Rem);
                break;
            }
        }
Exemplo n.º 7
0
 public override void VisitBinOpNode(BinOpNode binop)
 {
     if (depth > 0)
     {
         Count += 1;
     }
 }
 public override void VisitBinOpNode(BinOpNode binop)
 {
     binop.Parent = st.Peek();
     st.Push(binop);
     base.VisitBinOpNode(binop);
     st.Pop();
 }
Exemplo n.º 9
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");
        }
Exemplo n.º 10
0
 public override void VisitBinOpNode(BinOpNode binop)
 {
     PreVisit(binop);
     binop.Left.Visit(this);
     binop.Right.Visit(this);
     PostVisit(binop);
 }
 public override void VisitBinOpNode(BinOpNode binop)
 {
     if (depth < 0)
     {
         ++opCnt;
     }
 }
        public override void VisitBinOpNode(BinOpNode binop)
        {
            switch (binop.Op)
            {
            case OpType.MULT:
                if (binop.Left is IntNumNode && (binop.Left as IntNumNode).Num == 1)
                {
                    binop.Right.Visit(this);
                    ReplaceExpr(binop, binop.Right);
                }
                else if (binop.Right is IntNumNode && (binop.Right as IntNumNode).Num == 1)
                {
                    binop.Left.Visit(this);
                    ReplaceExpr(binop, binop.Left);
                }
                else
                {
                    base.VisitBinOpNode(binop);
                }
                break;

            case OpType.DIV:
                if (binop.Right is IntNumNode && (binop.Right as IntNumNode).Num == 1)
                {
                    binop.Left.Visit(this);
                    ReplaceExpr(binop, binop.Left);
                }
                break;

            default:
                base.VisitBinOpNode(binop);
                break;
            }
        }
Exemplo n.º 13
0
        public ObjectBase VisitBinOpNode(BinOpNode node)
        {
            var        left  = this.Visit(node.leftNode);
            var        right = this.Visit(node.rightNode);
            ObjectBase result;

            switch (node.token.type)
            {
            case Token.TokenType.TokenPlus:
                result = left.AddTo(right);
                break;

            case Token.TokenType.TokenMinus:
                result = left.SubBy(right);
                break;

            case Token.TokenType.TokenMul:
                result = left.MulBy(right);
                break;

            case Token.TokenType.TokenDiv:
                result = left.DivBy(right);
                break;

            default:
                result = null;
                break;
            }

            result.SetPosition(node.start, node.end);
            return(result);
        }
Exemplo n.º 14
0
        public override void VisitBinOpNode(BinOpNode binop)
        {
            depth++;
            switch (binop.Op)
            {
            case '+':
                sum++;
                break;

            case '-':
                sum++;
                break;

            case '*':
                sum += 3;
                break;

            default:
                sum += 3;
                break;
            }
            base.VisitBinOpNode(binop);
            depth--;
            if (depth == 0)
            {
                compList.Add(sum);
                sum = 0;
            }
        }
Exemplo n.º 15
0
 public override void VisitBinOpNode(BinOpNode binop)
 {
     Text += "(";
     binop.Left.Visit(this);
     Text += " " + binop.Op + " ";
     binop.Right.Visit(this);
     Text += ")";
 }
Exemplo n.º 16
0
        public override void VisitBinOpNode(BinOpNode binop)
        {
            int count = GetOpCountBinOpNode(binop);

            if (count > MaxCount)
            {
                MaxCount = count;
            }
        }
 public override void VisitBinOpNode(BinOpNode binop)
 {
     if (CycleDepth > 0)
     {
         OpCount++;
     }
     binop.Left.Visit(this);
     binop.Right.Visit(this);
 }
Exemplo n.º 18
0
        public override void VisitBinOpNode(BinOpNode binop)
        {
            Node parent = st.Pop();

            st.Push(parent);
            binop.Parent = parent;
            st.Push(binop);
            base.VisitBinOpNode(binop);
            st.Pop();
        }
Exemplo n.º 19
0
 /// <summary>
 /// Посещает узлы <paramref name="binop"/> и применяет оптимизацию по
 /// замене выражений вида: 1 * ex, ex * 1, ex / 1 на ex
 /// </summary>
 /// <param name="binop"></param>
 public override void VisitBinOpNode(BinOpNode binop)
 {
     IsPerformed = false;
     if (binop.Op[0] == '*' && binop.Right is IntNumNode innRight &&
         innRight.Num == 1)
     {
         binop.Left.Visit(this);
         ReplaceExpr(binop, binop.Left);
         IsPerformed = true;
     }
        public Code List(params Code[] values)
        {
            Node list = null;

            for (int i = 0; i < values.Length; i++)
            {
                list = new BinOpNode(NodeType.tpList, NodeTag.opNop, list, (Node)values[i]);
            }
            return(list);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Create multiplication tree for nodes in list
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private static Node CreateMultiplicationTree(List <Node> list)
        {
            switch (list.Count)
            {
            case 0:
                return(new EmptyNode());

            case 1:
                return(list[0]);

            default:
                Node result        = new EmptyNode();
                int  notEmptyIndex = -1;
                for (int i = 1; i < list.Count; i++)
                {
                    if (result is EmptyNode)
                    {
                        if (list[i] is EmptyNode)                                   // If current node is empty
                        {
                            if (notEmptyIndex == -1 && !(list[i - 1] is EmptyNode)) // If previous node is not empty and no waiting nodes
                            {
                                notEmptyIndex = i - 1;
                            }
                            continue;
                        }

                        if (notEmptyIndex > -1)     // If we have a non empty node waiting
                        {
                            result        = ParseMO(list[notEmptyIndex], "*", list[i]);
                            notEmptyIndex = -1;
                        }
                        else                              // If no nodes are waiting
                        {
                            if (list[i - 1] is EmptyNode) // If previous node is empty
                            {
                                continue;
                            }
                            result = ParseMO(list[i - 1], "*", list[i]);
                        }
                    }
                    else
                    {
                        if (list[i] is EmptyNode)     // Remove empty nodes
                        {
                            continue;
                        }
                        BinOpNode temp = result as BinOpNode;
                        temp.right = ParseMO(temp.right, "*", list[i]);
                        result     = temp;
                    }
                }
                return(result);
            }
        }
Exemplo n.º 22
0
        public override void VisitBinOpNode(BinOpNode binOpNode)
        {
            base.VisitBinOpNode(binOpNode);
            var operationIsMult = binOpNode.Op == OpType.MULT;
            var leftIsZero      = binOpNode.Left is IntNumNode && (binOpNode.Left as IntNumNode).Num == 0;
            var rightIsZero     = binOpNode.Right is IntNumNode && (binOpNode.Right as IntNumNode).Num == 0;

            if (operationIsMult && (leftIsZero || rightIsZero))
            {
                ReplaceExpr(binOpNode, new IntNumNode(0));
            }
        }
        public override void VisitBinOpNode(BinOpNode binop)
        {
            binop.Left.Visit(this);
            binop.Right.Visit(this);
            switch (binop.Op)
            {
            case '*':
            case '/': CurrentComplexity += 3; break;

            case '+':
            case '-': CurrentComplexity += 1; break;
            }
        }
Exemplo n.º 24
0
 public override void VisitBinOpNode(BinOpNode binop)
 {
     binop.Left.Visit(this);
     if (binop.Op == '+' || binop.Op == '-')
     {
         ++ComplexityExprs[ComplexityExprs.Count - 1];
     }
     else if (binop.Op == '*' || binop.Op == '/')
     {
         ComplexityExprs[ComplexityExprs.Count - 1] += 3;
     }
     binop.Right.Visit(this);
 }
Exemplo n.º 25
0
 public override void VisitBinOpNode(BinOpNode binop)
 {
     if (binop.Left is IntNumNode && (binop.Left as IntNumNode).Num == 0 &&
         binop.Op[0] == '+')
     {
         binop.Right.Visit(this);
         ReplaceExpr(binop, binop.Right);
     }
     else
     {
         base.VisitBinOpNode(binop);
     }
 }
Exemplo n.º 26
0
 public override void VisitBinOpNode(BinOpNode binop)
 {
     if (binop.Op.Equals('+') || binop.Op.Equals('-'))
     {
         Complexity[Complexity.Count - 1] += 1;
     }
     if (binop.Op.Equals('/') || binop.Op.Equals('*'))
     {
         Complexity[Complexity.Count - 1] += 3;
     }
     binop.Left.Visit(this);
     binop.Right.Visit(this);
 }
Exemplo n.º 27
0
 public override void VisitBinOpNode(BinOpNode binop)
 {
     if ((binop.Left is IdNode) && (binop.Right is IdNode) &&
         String.Equals((binop.Left as IdNode).Name, (binop.Right as IdNode).Name) &&
         (binop.Op == "-"))
     {
         ReplaceExpr(binop, new IntNumNode(0));
     }
     else
     {
         base.VisitBinOpNode(binop); // Обойти потомков обычным образом
     }
 }
Exemplo n.º 28
0
        public override void VisitBinOpNode(BinOpNode binop)
        {
            var left  = binop.Left as UnOpNode;
            var right = binop.Right as UnOpNode;

            if (left != null && right != null && left.Op == right.Op &&
                left.Op == OpType.NOT && left.Expr is IdNode idl)
            {
                if (right.Expr is IdNode idr && idl.Name == idr.Name)
                {
                    if (binop.Op == OpType.EQUAL)
                    {
                        ReplaceExpr(binop, new BoolValNode(true));
                    }
                    if (binop.Op == OpType.NOTEQUAL)
                    {
                        ReplaceExpr(binop, new BoolValNode(false));
                    }
                }
            }
            else
            if (left != null && left.Op == OpType.NOT && left.Expr is IdNode &&
                binop.Right is IdNode && (left.Expr as IdNode).Name == (binop.Right as IdNode).Name)
            {
                if (binop.Op == OpType.EQUAL)
                {
                    ReplaceExpr(binop, new BoolValNode(false));
                }
                if (binop.Op == OpType.NOTEQUAL)
                {
                    ReplaceExpr(binop, new BoolValNode(true));
                }
            }
            else
            if (right != null && right.Op == OpType.NOT && right.Expr is IdNode &&
                binop.Left is IdNode && (right.Expr as IdNode).Name == (binop.Left as IdNode).Name)
            {
                if (binop.Op == OpType.EQUAL)
                {
                    ReplaceExpr(binop, new BoolValNode(false));
                }
                if (binop.Op == OpType.NOTEQUAL)
                {
                    ReplaceExpr(binop, new BoolValNode(true));
                }
            }
            else
            {
                base.VisitBinOpNode(binop);
            }
        }
Exemplo n.º 29
0
 public override void VisitBinOpNode(BinOpNode binop)
 {
     if (binop.Left is ExprNode el)
     {
         el.Visit(this);
     }
     if (binop.Right is ExprNode er)
     {
         er.Visit(this);
     }
     if (binop.Left is IntNumNode l && binop.Right is IntNumNode r && binop.Op == "*")
     {
         ReplaceExpr(binop, new IntNumNode(l.Num * r.Num));
     }
Exemplo n.º 30
0
        public override void VisitBinOpNode(BinOpNode binop)
        {
            var isZeroLeft  = binop.Left is IntNumNode && (binop.Left as IntNumNode).Num == 0;
            var isZeroRight = binop.Right is IntNumNode && (binop.Right as IntNumNode).Num == 0;

            if (string.Equals(binop.Op, "*") && (isZeroRight || isZeroLeft))
            {
                ReplaceExpr(binop, new IntNumNode(0));
            }
            else
            {
                base.VisitBinOpNode(binop);
            }
        }
Exemplo n.º 31
0
        private int GetOpCountBinOpNode(BinOpNode binop)
        {
            int res = 1;

            if (binop.Left is BinOpNode)
            {
                res += GetOpCountBinOpNode(binop.Left as BinOpNode);
            }
            if (binop.Right is BinOpNode)
            {
                res += GetOpCountBinOpNode(binop.Right as BinOpNode);
            }
            return(res);
        }
 private static Node listToTree(Node expr, BinOpNode list)
 {
     BinOpNode tree = null; 
     do { 
         Node elem = list.right;
         NodeTag cop = NodeTag.opNop;
         if (elem.type == NodeType.tpUnknown) { 
             elem.type = expr.type;
         }
         if (expr.type == NodeType.tpInt) { 
             if (elem.type == NodeType.tpReal) { 
                 expr = new UnaryOpNode(NodeType.tpReal, NodeTag.opIntToReal, expr);
                 cop = NodeTag.opRealEq;
             } else if (elem.type == NodeType.tpInt) { 
                 cop = NodeTag.opIntEq;
             }
         } else if (expr.type == NodeType.tpReal) {
             if (elem.type == NodeType.tpReal) { 
                 cop = NodeTag.opRealEq;
             } else if (elem.type == NodeType.tpInt) { 
                 cop = NodeTag.opRealEq;
                 elem = QueryImpl.int2real(elem);
             }
         } else if (expr.type == NodeType.tpDate && elem.type == NodeType.tpDate) {
             cop = NodeTag.opDateEq;
         } else if (expr.type == NodeType.tpStr && elem.type == NodeType.tpStr) {
             cop = NodeTag.opStrEq;
         } else if (expr.type == NodeType.tpObj && elem.type == NodeType.tpObj) {
             cop = NodeTag.opObjEq;
         } else if (expr.type == NodeType.tpBool && elem.type == NodeType.tpBool) {
             cop = NodeTag.opBoolEq;
         }
         if (cop == NodeTag.opNop) { 
             throw new CodeGeneratorException("Invalid argument types");
         }
         BinOpNode cmp = new BinOpNode(NodeType.tpBool, cop, expr, elem);
         if (tree == null) { 
             tree = cmp; 
         } else {
             tree = new BinOpNode(NodeType.tpBool, NodeTag.opBoolOr, cmp, tree);
         }
     } while ((list = (BinOpNode)list.left) != null);
     return tree;
 }