private ASTNode OrCondition()
        {
            var node = AndCondition();

            while (_currentToken.Type == TokenType.Or)
            {
                var token = _currentToken;

                Eat(TokenType.Or);

                node = new ASTBinaryOperator(node, token, AndCondition());
            }

            return(node);
        }
        private ASTNode Term()
        {
            var node = Factor();

            while (FactorOperators.Contains(_currentToken.Type))
            {
                var token = _currentToken;

                Eat(token.Type);

                node = new ASTBinaryOperator(node, token, Factor());
            }

            return(node);
        }
        private ASTNode Comparision()
        {
            if (_currentToken.Type == TokenType.ConstBool)
            {
                var token = _currentToken;
                Eat(TokenType.ConstBool);

                return(new ASTBool(token));
            }

            var node = ArithmeticExpression();

            while (CompareOperators.Contains(_currentToken.Type))
            {
                var token = _currentToken;
                Eat(token.Type);

                node = new ASTBinaryOperator(node, token, ArithmeticExpression());
            }

            return(node);
        }
        private ASTNode ArithmeticExpression()
        {
            var node = Term();

            while (TermOperators.Contains(_currentToken.Type))
            {
                var token = _currentToken;

                switch (token.Type)
                {
                case TokenType.Plus:
                    Eat(TokenType.Plus);
                    break;

                case TokenType.Minus:
                    Eat(TokenType.Minus);
                    break;
                }

                node = new ASTBinaryOperator(node, token, Term());
            }

            return(node);
        }
示例#5
0
        private VisitResult VisitBinaryOperator(ASTBinaryOperator node)
        {
            switch (node.Type)
            {
            case TokenType.Plus:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value + Visit(node.Right).Value
                });

            case TokenType.Minus:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value - Visit(node.Right).Value
                });

            case TokenType.Multiplication:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value *Visit(node.Right).Value
                });

            case TokenType.Divide:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value / Visit(node.Right).Value
                });

            case TokenType.Modulo:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value % Visit(node.Right).Value
                });

            case TokenType.Equal:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value == Visit(node.Right).Value
                });

            case TokenType.NotEqual:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value != Visit(node.Right).Value
                });

            case TokenType.Greater:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value > Visit(node.Right).Value
                });

            case TokenType.GreaterEqual:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value >= Visit(node.Right).Value
                });

            case TokenType.Less:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value < Visit(node.Right).Value
                });

            case TokenType.LessEqual:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value <= Visit(node.Right).Value
                });

            case TokenType.And:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value&& Visit(node.Right).Value
                });

            case TokenType.Or:
                return(new VisitResult
                {
                    Value = Visit(node.Left).Value || Visit(node.Right).Value
                });
            }

            throw new ArgumentException($"Invalid AST node type {node.GetType()}");
        }