예제 #1
0
        private Expr Addition()
        {
            Expr expr = Multiplication();

            while (Match(MINUS, PLUS))
            {
                Token op    = Previous();
                Expr  right = Multiplication();
                expr = new Expr.Binary(expr, op, right);
            }

            return(expr);
        }
예제 #2
0
        private Expr LeftBinary(Func <Expr> leftP, TokenType[] tokens, Func <Expr> rightP)
        {
            Expr expr = leftP();

            while (MatchAny(tokens))
            {
                var op    = Previous();
                var right = rightP();
                expr = new Expr.Binary(expr, op, right);
            }

            return(expr);
        }
예제 #3
0
        private Expr Multiplication()
        {
            Expr expr = Unary();

            while (Match(SLASH, STAR))
            {
                Token op    = Previous();
                Expr  right = Unary();
                expr = new Expr.Binary(expr, op, right);
            }

            return(expr);
        }
예제 #4
0
        private Expr Comparison()
        {
            Expr expr = Addition();

            while (Match(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL))
            {
                Token op    = Previous();
                Expr  right = Addition();
                expr = new Expr.Binary(expr, op, right);
            }

            return(expr);
        }
예제 #5
0
        private Expr Equality()
        {
            var expr = Comparison();

            while (Match(BANG_EQUAL, EQUAL_EQUAL))
            {
                Token op    = Previous();
                Expr  right = Comparison();
                expr = new Expr.Binary(expr, op, right);
            }

            return(expr);
        }
예제 #6
0
 public string VisitBinary(Expr.Binary expr)
 {
     return(Parenthesize(expr.Op.Lexeme, expr.Left, expr.Right));
 }
예제 #7
0
 public Unit VisitBinary(Expr.Binary expr)
 {
     Resolve(expr.Left);
     Resolve(expr.Right);
     return(Unit.Default);
 }
예제 #8
0
        public object VisitBinary(Expr.Binary expr)
        {
            // We evaluate the operands left-to-right, and we're eager.
            object left  = Evaluate(expr.Left);
            object right = Evaluate(expr.Right);

            switch (expr.Op.Type)
            {
            case TokenType.Greater:
                AssertNumberOperands(expr.Op, left, right);
                return((double)left > (double)right);

            case TokenType.GreaterEqual:
                AssertNumberOperands(expr.Op, left, right);
                return((double)left >= (double)right);

            case TokenType.Less:
                AssertNumberOperands(expr.Op, left, right);
                return((double)left < (double)right);

            case TokenType.LessEqual:
                AssertNumberOperands(expr.Op, left, right);
                return((double)left <= (double)right);

            case TokenType.Minus:
                AssertNumberOperands(expr.Op, left, right);
                return((double)left - (double)right);

            case TokenType.Plus:
                if (left is double && right is double)
                {
                    return((double)left + (double)right);
                }
                else if (left is string && right is string)
                {
                    return((string)left + (string)right);
                }
                else
                {
                    throw new RuntimeError(expr.Op, "Operands must be two numbers or two strings");
                }

            case TokenType.Slash:
                AssertNumberOperands(expr.Op, left, right);
                return((double)left / (double)right);

            case TokenType.Star:
                AssertNumberOperands(expr.Op, left, right);
                return((double)left * (double)right);

            case TokenType.Percent:
                AssertNumberOperands(expr.Op, left, right);
                return((double)left % (double)right);

            case TokenType.BangEqual:
                return(!IsEqual(left, right));

            case TokenType.EqualEqual:
                return(IsEqual(left, right));
            }

            throw new NotSupportedException();
        }
예제 #9
0
 object Expr.IVisitor <object> .VisitBinaryExpr(Expr.Binary expr)
 {
     Resolve(expr.left);
     Resolve(expr.right);
     return(null);
 }