document java interpretere
This commit is contained in:
parent
d5f7809ebb
commit
ac2621e431
@ -1,5 +1,8 @@
|
|||||||
package ch.vanwa.lox_interpreter;
|
package ch.vanwa.lox_interpreter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluate Lox expressions.
|
||||||
|
*/
|
||||||
class Interpreter implements Expr.Visitor<Object> {
|
class Interpreter implements Expr.Visitor<Object> {
|
||||||
void interpret(Expr expression) {
|
void interpret(Expr expression) {
|
||||||
try {
|
try {
|
||||||
@ -87,12 +90,18 @@ class Interpreter implements Expr.Visitor<Object> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throw a runtime error if the operand is not an instance of Double.
|
||||||
|
*/
|
||||||
private void checkNumberOperand(Token operator, Object operand) {
|
private void checkNumberOperand(Token operator, Object operand) {
|
||||||
if (operand instanceof Double)
|
if (operand instanceof Double)
|
||||||
return;
|
return;
|
||||||
throw new RuntimeError(operator, "Operand must be a number.");
|
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,
|
private void checkNumberOperands(Token operator,
|
||||||
Object left, Object right) {
|
Object left, Object right) {
|
||||||
if (left instanceof Double && right instanceof Double)
|
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.");
|
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) {
|
private boolean isTruthy(Object object) {
|
||||||
if (object == null)
|
if (object == null)
|
||||||
return false;
|
return false;
|
||||||
@ -109,6 +121,10 @@ class Interpreter implements Expr.Visitor<Object> {
|
|||||||
return true;
|
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) {
|
private boolean isEqual(Object a, Object b) {
|
||||||
if (a == null && b == null)
|
if (a == null && b == null)
|
||||||
return true;
|
return true;
|
||||||
@ -118,6 +134,12 @@ class Interpreter implements Expr.Visitor<Object> {
|
|||||||
return a.equals(b);
|
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) {
|
private String stringify(Object object) {
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
return "nil";
|
return "nil";
|
||||||
|
Loading…
Reference in New Issue
Block a user