Пример #1
0
        private Expr term()
        {
            Expr expr = factor();

            while (match(TokenType.PLUS, TokenType.MINUS))
            {
                Token op    = previous();
                Expr  right = factor();
                expr = new Expr.Binary(expr, op, right);
            }

            return(expr);
        }
Пример #2
0
        private Expr comparison()
        {
            Expr expr = term();

            while (match(TokenType.GREATER, TokenType.GREATER_EQUAL, TokenType.LESS, TokenType.LESS_EQUAL))
            {
                Token op    = previous();
                Expr  right = term();
                expr = new Expr.Binary(expr, op, right);
            }

            return(expr);
        }
Пример #3
0
        private Expr equality()
        {
            Expr expr = comparison();

            while (match(TokenType.EXCLAM_EQUAL, TokenType.EQUAL_EQUAL))
            {
                Token op    = previous();
                Expr  right = comparison();
                expr = new Expr.Binary(expr, op, right);
            }

            return(expr);
        }
Пример #4
0
 public string visitBinaryExpr(Expr.Binary expr)
 {
     return(paranthesize(expr.op.lexeme, expr.left, expr.right));
 }
Пример #5
0
        public object visitBinaryExpr(Expr.Binary expr)
        {
            object left  = evaluate(expr.left);
            object right = evaluate(expr.right);

            switch (expr.op.type)
            {
            case TokenType.SLASH:
                checkNumberOperand(expr.op, left, right);
                if ((double)right == 0)
                {
                    throw new RuntimeError(expr.op, "Division by 0 not permitted!");
                }
                return((double)left / (double)right);

            case TokenType.MINUS:
                checkNumberOperand(expr.op, left, right);
                return((double)left - (double)right);

            case TokenType.STAR:
                checkNumberOperand(expr.op, 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);
                }
                throw new RuntimeError(expr.op, "Operands must be 2 numbers or 2 strings!");

            case TokenType.GREATER:
                checkNumberOperand(expr.op, left, right);
                return((double)left > (double)right);

            case TokenType.GREATER_EQUAL:
                checkNumberOperand(expr.op, left, right);
                return((double)left >= (double)right);

            case TokenType.LESS:
                checkNumberOperand(expr.op, left, right);
                return((double)left < (double)right);

            case TokenType.LESS_EQUAL:
                checkNumberOperand(expr.op, left, right);
                return((double)left <= (double)right);

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

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



            return(null);
        }