Esempio n. 1
0
        public Object Visit_BinaryExpr(BinaryExpr expr)
        {
            Object left  = Evaluate(expr.left);
            Object right = Evaluate(expr.right);

            switch (expr.op.type)
            {
            case TokenType.Greater:
                CheckNumberOperands(expr.op, left, right);
                return((double)left > (double)right);

            case TokenType.GreaterEqual:
                return((double)left >= (double)right);

            case TokenType.Less:
                return((double)left < (double)right);

            case TokenType.LessEqual:
                return((double)left <= (double)right);

            case TokenType.Minus:
                CheckNumberOperand(expr.op, 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 RuntimeException(expr.op.line, expr.op.column, "Operands must be two numbers or two strings.");

            case TokenType.Slash: return((double)left / (double)right);

            case TokenType.Star: return((double)left * (double)right);

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

            case TokenType.EqualEqual: return(IsEqual(left, right));
            }
            throw new Exception("Unhandled case");
        }
Esempio n. 2
0
 public Void Visit_BinaryExpr(BinaryExpr expr)
 {
     Resolve(expr.left);
     Resolve(expr.right);
     return(null);
 }
Esempio n. 3
0
 public string Visit_BinaryExpr(BinaryExpr expr)
 {
     return(Parenthesize(expr.op.ToText(), expr.left, expr.right));
 }