document java interpretere
This commit is contained in:
parent
d5f7809ebb
commit
ac2621e431
@ -1,5 +1,8 @@
|
||||
package ch.vanwa.lox_interpreter;
|
||||
|
||||
/**
|
||||
* Evaluate Lox expressions.
|
||||
*/
|
||||
class Interpreter implements Expr.Visitor<Object> {
|
||||
void interpret(Expr expression) {
|
||||
try {
|
||||
@ -87,12 +90,18 @@ class Interpreter implements Expr.Visitor<Object> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw a runtime error if the operand is not an instance of Double.
|
||||
*/
|
||||
private void checkNumberOperand(Token operator, Object operand) {
|
||||
if (operand instanceof Double)
|
||||
return;
|
||||
throw new RuntimeError(operator, "Operand must be a number.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw a runtime error if left and right are not both instances of Double.
|
||||
*/
|
||||
private void checkNumberOperands(Token operator,
|
||||
Object left, Object right) {
|
||||
if (left instanceof Double && right instanceof Double)
|
||||
@ -101,6 +110,9 @@ class Interpreter implements Expr.Visitor<Object> {
|
||||
throw new RuntimeError(operator, "Operands must be numbers.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the object is a boolean with value true, false otherwise.
|
||||
*/
|
||||
private boolean isTruthy(Object object) {
|
||||
if (object == null)
|
||||
return false;
|
||||
@ -109,6 +121,10 @@ class Interpreter implements Expr.Visitor<Object> {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if object a and b are not null and a equals be, otherwise return
|
||||
* false.
|
||||
*/
|
||||
private boolean isEqual(Object a, Object b) {
|
||||
if (a == null && b == null)
|
||||
return true;
|
||||
@ -118,6 +134,12 @@ class Interpreter implements Expr.Visitor<Object> {
|
||||
return a.equals(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an object to a string.
|
||||
*
|
||||
* If the object is null, return "nil", if it is a Double properly print it as a
|
||||
* decimal, otherwise just call toString.
|
||||
*/
|
||||
private String stringify(Object object) {
|
||||
if (object == null) {
|
||||
return "nil";
|
||||
|
Loading…
Reference in New Issue
Block a user