Esempio n. 1
0
        private Syntax.Expression ParseBinaryOper(int level, Syntax.Expression lnode, bool stop_if_comma = false)
        {
            Syntax.Expression root = null;

            while (true)
            {
                Syntax.Object.CheckObject(lnode);

                if (scan.Peek().type == Token.Type.QUESTION)
                {
                    scan.Read();
                    Syntax.Expression expr = ParseExpression();
                    CheckToken(scan.Peek(), Token.Type.COLON, true);
                    root = new Syntax.Ternary(lnode);
                    ((Syntax.Ternary)root).SetBranchTrue(expr);
                    ((Syntax.Ternary)root).SetBranchFalse(ParseExpression());
                    lnode = root;
                }

                int op_level = GetOperatorPriority(scan.Peek());

                if (op_level < level || (stop_if_comma && scan.Peek().type == Token.Type.COMMA))
                {
                    return(lnode);
                }

                if (this.parse_const_expr && op_level == 2)
                {
                    throw new Syntax.Exception("недопустимый оператор в константном выражении", scan.GetPos(), scan.GetLine());
                }

                Token oper = scan.Read();

                Syntax.Expression rnode = ParseUnaryExpr();

                int level_next_oper = GetOperatorPriority(scan.Peek());

                try
                {
                    if (op_level < level_next_oper)
                    {
                        rnode = ParseBinaryOper(op_level + 1, rnode, stop_if_comma);
                    }

                    root = new Syntax.BinaryOperator(oper);
                    ((Syntax.BinaryOperator)root).SetLeftOperand(lnode);
                    ((Syntax.BinaryOperator)root).SetRightOperand(rnode);
                }
                catch (Syntax.Exception e)
                {
                    ToHandlerException(e);
                }

                lnode = root;
            }
        }
Esempio n. 2
0
        private Syntax.Expression ParseBinaryOper(int level, Syntax.Expression lnode, bool stop_if_comma = false)
        {
            Syntax.Expression root = null;

            while (true)
            {

                Syntax.Object.CheckObject(lnode);

                if (scan.Peek().type == Token.Type.QUESTION)
                {
                    scan.Read();
                    Syntax.Expression expr = ParseExpression();
                    CheckToken(scan.Peek(), Token.Type.COLON, true);
                    root = new Syntax.Ternary(lnode);
                    ((Syntax.Ternary)root).SetBranchTrue(expr);
                    ((Syntax.Ternary)root).SetBranchFalse(ParseExpression());
                    lnode = root;
                }

                int op_level = GetOperatorPriority(scan.Peek());

                if (op_level < level || (stop_if_comma && scan.Peek().type == Token.Type.COMMA))
                {
                    return lnode;
                }

                if (this.parse_const_expr && op_level == 2)
                {
                    throw new Syntax.Exception("недопустимый оператор в константном выражении", scan.GetPos(), scan.GetLine());
                }

                Token oper = scan.Read();

                Syntax.Expression rnode = ParseUnaryExpr();

                int level_next_oper = GetOperatorPriority(scan.Peek());

                try
                {
                    if (op_level < level_next_oper)
                    {
                        rnode = ParseBinaryOper(op_level + 1, rnode, stop_if_comma);
                    }

                    root = new Syntax.BinaryOperator(oper);
                    ((Syntax.BinaryOperator)root).SetLeftOperand(lnode);
                    ((Syntax.BinaryOperator)root).SetRightOperand(rnode);
                }
                catch (Syntax.Exception e)
                {
                    ToHandlerException(e);
                }

                lnode = root;
            }
        }