Beispiel #1
0
        private Expr term()
        {
            Expr expr = factor();

            while (match(TokenType.MINUS, TokenType.PLUS))
            {
                Token _operator = previous();
                Expr  right     = factor();
                expr = new Expr.BinaryExpr(expr, _operator, right);
            }
            return(expr);
        }
Beispiel #2
0
        private Expr factor()
        {
            Expr expr = unary();

            while (match(TokenType.FORWARD_SLASH, TokenType.ASTERISK))
            {
                Token _operator = previous();
                Expr  right     = unary();
                expr = new Expr.BinaryExpr(expr, _operator, right);
            }
            return(expr);
        }
Beispiel #3
0
        private Expr equality()
        {
            Expr expr = comparison();

            while (match(TokenType.EXCLAMATION_EQUALS, TokenType.EQUALS_EQUALS))
            {
                Token _operator = previous();
                Expr  right     = comparison();
                expr = new Expr.BinaryExpr(expr, _operator, right);
            }
            return(expr);
        }
Beispiel #4
0
        private Expr comparison()
        {
            Expr expr = term();

            while (match(TokenType.GREATER_THAN, TokenType.GREATER_THAN_EQUALS, TokenType.LESS_THAN, TokenType.LESS_THAN_EQUALS))
            {
                Token _operator = previous();
                Expr  right     = term();
                expr = new Expr.BinaryExpr(expr, _operator, right);
            }
            return(expr);
        }
Beispiel #5
0
        private Expr Multiplication()
        {
            var expr = Unary();

            while (Match(SLASH, STAR))
            {
                var @operator = Previous();
                var right     = Unary();
                expr = new Expr.BinaryExpr(expr, @operator, right);
            }

            return(expr);
        }
Beispiel #6
0
        private Expr isNull()
        {
            Expr expr = or();

            if (match(TokenType.QUESTION_QUESTION))
            {
                Token qq    = previous();
                Expr  right = ternaryExpression();
                expr = new Expr.BinaryExpr(expr, qq, right);
            }

            return(expr);
        }
Beispiel #7
0
        private Expr Addition()
        {
            var expr = Multiplication();

            while (Match(MINUS, PLUS))
            {
                var @operator = Previous();
                var right     = Multiplication();
                expr = new Expr.BinaryExpr(expr, @operator, right);
            }

            return(expr);
        }
Beispiel #8
0
        private Expr Comparison()
        {
            var expr = Addition();

            while (Match(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL))
            {
                var @operator = Previous();
                var right     = Addition();
                expr = new Expr.BinaryExpr(expr, @operator, right);
            }

            return(expr);
        }
Beispiel #9
0
        private Expr Equality()
        {
            var expr = Comparison();

            while (Match(BANG_EQUAL, EQUAL_EQUAL))
            {
                var @operator = Previous();
                var right     = Comparison();
                expr = new Expr.BinaryExpr(expr, @operator, right);
            }

            return(expr);
        }
Beispiel #10
0
        private Statement varDeclaration()
        {
            Token name = consume(TokenType.IDENTIFIER, "Expect variable name.");

            Expr initializer = null;

            if (match(TokenType.EQUALS))
            {
                initializer = expression();
            }
            while (match(TokenType.COMMA))
            {
                Token _operator = previous();
                Expr  right     = term();
                initializer = new Expr.BinaryExpr(initializer, _operator, right);
            }
            consume(TokenType.SEMICOLON, "Expect ';' after variable declaration.");
            return(new Statement.Var(name, initializer));
        }
Beispiel #11
0
 public object visitBinaryExpr(Expr.BinaryExpr binaryExpr)
 {
     resolve(binaryExpr.left);
     resolve(binaryExpr.right);
     return(null);
 }
Beispiel #12
0
 public string VisitBinaryExpr(Expr.BinaryExpr expr)
 {
     return(Parenthesize(expr.Operator.Lexeme, expr.Left, expr.Right));
 }
Beispiel #13
0
 public string visitBinaryExpr(Expr.BinaryExpr expr)
 {
     return(parenthesize(expr.operatorToken.lexeme, new Expr[] { expr.left, expr.right }));
 }
Beispiel #14
0
 public object VisitBinaryExpr(Expr.BinaryExpr expr)
 {
     Resolve(expr.Left);
     Resolve(expr.Right);
     return(null);
 }
Beispiel #15
0
        public object visitBinaryExpr(Expr.BinaryExpr binaryExpr)
        {
            object left  = evaluate(binaryExpr.left);
            object right = evaluate(binaryExpr.right);

            switch (binaryExpr.operatorToken.type)
            {
            case TokenType.COMMA:
                return(right);

            case TokenType.GREATER_THAN:
                checkNumberOperand(binaryExpr.operatorToken, left, right);
                return((double)left > (double)right);

            case TokenType.GREATER_THAN_EQUALS:
                checkNumberOperand(binaryExpr.operatorToken, left, right);
                return((double)left >= (double)right);

            case TokenType.LESS_THAN:
                checkNumberOperand(binaryExpr.operatorToken, left, right);
                return((double)left < (double)right);

            case TokenType.LESS_THAN_EQUALS:
                checkNumberOperand(binaryExpr.operatorToken, left, right);
                return((double)left <= (double)right);

            case TokenType.EQUALS_EQUALS:
                return(isEqual(left, right));

            case TokenType.EXCLAMATION_EQUALS:
                return(!isEqual(left, right));

            case TokenType.MINUS:
                checkNumberOperand(binaryExpr.operatorToken, left, right);
                return((double)left - (double)right);

            case TokenType.PLUS:
                if (left is double && right is double)
                {
                    return((double)left + (double)right);
                }
                if (left is string && right is string)
                {
                    return((string)left + (string)right);
                }
                if (left is double && right is string)
                {
                    return(((double)left).ToString() + (string)right);
                }
                if (left is string && right is double)
                {
                    return((string)left + ((double)right).ToString());
                }
                throw new Exceptions.RuntimeError(binaryExpr.operatorToken, "Operands must be numbers or strings.");

            case TokenType.FORWARD_SLASH:
                checkNumberOperand(binaryExpr.operatorToken, left, right);
                if ((double)right == 0)
                {
                    throw new Exceptions.RuntimeError(binaryExpr.operatorToken, "Cannot divide by zero.");
                }
                return((double)left / (double)right);

            case TokenType.ASTERISK:
                checkNumberOperand(binaryExpr.operatorToken, left, right);
                return((double)left * (double)right);

            case TokenType.QUESTION_QUESTION:
                if (left is null || left is Null)
                {
                    return(right);
                }
                return(left);
            }

            return(null);
        }