Compare commits
2 commits
master
...
chapter-7-
Author | SHA1 | Date | |
---|---|---|---|
0068365533 | |||
5ff2f8be5a |
1 changed files with 15 additions and 2 deletions
|
@ -110,10 +110,23 @@ class Interpreter implements Expr.Visitor<Object> {
|
||||||
return (String) left + (String) right;
|
return (String) left + (String) right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (left instanceof String) {
|
||||||
|
return (String) left + stringify(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (right instanceof String) {
|
||||||
|
return stringify(left) + (String) right;
|
||||||
|
}
|
||||||
|
|
||||||
throw new RuntimeError(expr.operator, "Operands must be two numbers or two strings.");
|
throw new RuntimeError(expr.operator, "Operands must be two numbers or two strings.");
|
||||||
case SLASH:
|
case SLASH: {
|
||||||
checkNumberOperands(expr.operator, left, right);
|
checkNumberOperands(expr.operator, left, right);
|
||||||
return (double) left / (double) right;
|
double rhs = (double) right;
|
||||||
|
if (rhs == 0) {
|
||||||
|
throw new RuntimeError(expr.operator, "Division by zero.");
|
||||||
|
}
|
||||||
|
return (double) left / rhs;
|
||||||
|
}
|
||||||
case STAR:
|
case STAR:
|
||||||
checkNumberOperands(expr.operator, left, right);
|
checkNumberOperands(expr.operator, left, right);
|
||||||
return (double) left * (double) right;
|
return (double) left * (double) right;
|
||||||
|
|
Loading…
Reference in a new issue