Exemple #1
0
        IExpression ParseMultiplicativeExpression()
        {
            var left = ParseUnaryExpression();

            while (true)
            {
                if (SkipIf("*"))
                {
                    var right = ParseUnaryExpression();
                    left = new BinaryOperation(left, OperationType.Multiplication, right);
                }
                else if (SkipIf("/"))
                {
                    var right = ParseUnaryExpression();
                    left = new BinaryOperation(left, OperationType.Division, right);
                }
                else if (SkipIf("%"))
                {
                    var right = ParseUnaryExpression();
                    left = new BinaryOperation(left, OperationType.Remainder, right);
                }
                else
                {
                    break;
                }
            }
            return(left);
        }
Exemple #2
0
        public object Visit(BinaryOperation expression)
        {
            var left  = Calc(expression.Left);
            var right = Calc(expression.Right);

            if (left is null || right is null)
            {
                switch (expression.Operation)
                {
                case OperationType.Equality:
                    return(left == right);
                }
                throw new Exception("Неизвестная операция");
            }
            if (left is int && right is int)
            {
                int leftInt  = (int)left;
                int rightInt = (int)right;
                switch (expression.Operation)
                {
                case OperationType.Addition:
                    return(leftInt + rightInt);

                case OperationType.Subtraction:
                    return(leftInt - rightInt);

                case OperationType.Multiplication:
                    return(leftInt * rightInt);

                case OperationType.Division:
                    return(leftInt / rightInt);

                case OperationType.Remainder:
                    return(leftInt % rightInt);

                case OperationType.Equality:
                    return(leftInt == rightInt);

                case OperationType.Relation:
                    return(leftInt < rightInt);
                }
                throw new Exception("Неизвестная операция");
            }
            if (left is bool && right is bool)
            {
                bool leftBool  = (bool)left;
                bool rightBool = (bool)right;
                switch (expression.Operation)
                {
                case OperationType.Equality:
                    return(leftBool == rightBool);

                case OperationType.Relation:
                    return(!leftBool && rightBool);
                }
                throw new Exception("Неизвестная операция");
            }
            throw new Exception("Неверный тип операндов");
        }
Exemple #3
0
        IExpression ParseEqualityExpression()
        {
            var left = ParseRelationalExpression();

            while (true)
            {
                if (SkipIf("=="))
                {
                    var right = ParseRelationalExpression();
                    left = new BinaryOperation(left, OperationType.Equality, right);
                }
                else
                {
                    break;
                }
            }
            return(left);
        }
Exemple #4
0
        IExpression ParseRelationalExpression()
        {
            var left = ParseAdditiveExpression();

            while (true)
            {
                if (SkipIf("<"))
                {
                    var right = ParseAdditiveExpression();
                    left = new BinaryOperation(left, OperationType.Relation, right);
                }
                else
                {
                    break;
                }
            }
            return(left);
        }
Exemple #5
0
        IExpression ParseAdditiveExpression()
        {
            var left = ParseMultiplicativeExpression();

            while (true)
            {
                if (SkipIf("+"))
                {
                    var right = ParseMultiplicativeExpression();
                    left = new BinaryOperation(left, OperationType.Addition, right);
                }
                else if (SkipIf("-"))
                {
                    var right = ParseMultiplicativeExpression();
                    left = new BinaryOperation(left, OperationType.Subtraction, right);
                }
                else
                {
                    break;
                }
            }
            return(left);
        }