aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/craftinginterpreters/lox/Interpreter.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/craftinginterpreters/lox/Interpreter.java')
-rw-r--r--src/com/craftinginterpreters/lox/Interpreter.java84
1 files changed, 42 insertions, 42 deletions
diff --git a/src/com/craftinginterpreters/lox/Interpreter.java b/src/com/craftinginterpreters/lox/Interpreter.java
index ac42369..7dfd0fb 100644
--- a/src/com/craftinginterpreters/lox/Interpreter.java
+++ b/src/com/craftinginterpreters/lox/Interpreter.java
@@ -72,11 +72,11 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
Object right = evaluate(expr.right);
switch (expr.operator.type) {
- case BANG:
- return !isTruthy(right);
- case MINUS:
- checkNumberOperand(expr.operator, right);
- return -(double) right;
+ case BANG:
+ return !isTruthy(right);
+ case MINUS:
+ checkNumberOperand(expr.operator, right);
+ return -(double) right;
}
// Unreachable.
@@ -185,7 +185,7 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
Map<String, LoxFunction> methods = new HashMap<>();
for (Stmt.Function method : stmt.methods) {
- LoxFunction function = new LoxFunction(method, environment);
+ LoxFunction function = new LoxFunction(method, environment, method.name.lexeme.equals("init"));
methods.put(method.name.lexeme, function);
}
@@ -202,7 +202,7 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
@Override
public Void visitFunctionStmt(Stmt.Function stmt) {
- LoxFunction function = new LoxFunction(stmt, environment);
+ LoxFunction function = new LoxFunction(stmt, environment, false);
environment.define(stmt.name.lexeme, function);
return null;
}
@@ -274,41 +274,41 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
Object right = evaluate(expr.right);
switch (expr.operator.type) {
- case GREATER:
- checkNumberOperands(expr.operator, left, right);
- return (double) left > (double) right;
- case GREATER_EQUAL:
- checkNumberOperands(expr.operator, left, right);
- return (double) left >= (double) right;
- case LESS:
- checkNumberOperands(expr.operator, left, right);
- return (double) left < (double) right;
- case LESS_EQUAL:
- checkNumberOperands(expr.operator, left, right);
- return (double) left <= (double) right;
- case BANG_EQUAL:
- return !isEqual(left, right);
- case EQUAL_EQUAL:
- return isEqual(left, right);
- case MINUS:
- checkNumberOperands(expr.operator, left, right);
- return (double) left - (double) right;
- case PLUS:
- if (left instanceof Double && right instanceof Double) {
- return (double) left + (double) right;
- }
-
- if (left instanceof String && right instanceof String) {
- return (String) left + (String) right;
- }
-
- throw new RuntimeError(expr.operator, "Operands must be two numbers or two strings.");
- case SLASH:
- checkNumberOperands(expr.operator, left, right);
- return (double) left / (double) right;
- case STAR:
- checkNumberOperands(expr.operator, left, right);
- return (double) left * (double) right;
+ case GREATER:
+ checkNumberOperands(expr.operator, left, right);
+ return (double) left > (double) right;
+ case GREATER_EQUAL:
+ checkNumberOperands(expr.operator, left, right);
+ return (double) left >= (double) right;
+ case LESS:
+ checkNumberOperands(expr.operator, left, right);
+ return (double) left < (double) right;
+ case LESS_EQUAL:
+ checkNumberOperands(expr.operator, left, right);
+ return (double) left <= (double) right;
+ case BANG_EQUAL:
+ return !isEqual(left, right);
+ case EQUAL_EQUAL:
+ return isEqual(left, right);
+ case MINUS:
+ checkNumberOperands(expr.operator, left, right);
+ return (double) left - (double) right;
+ case PLUS:
+ if (left instanceof Double && right instanceof Double) {
+ return (double) left + (double) right;
+ }
+
+ if (left instanceof String && right instanceof String) {
+ return (String) left + (String) right;
+ }
+
+ throw new RuntimeError(expr.operator, "Operands must be two numbers or two strings.");
+ case SLASH:
+ checkNumberOperands(expr.operator, left, right);
+ return (double) left / (double) right;
+ case STAR:
+ checkNumberOperands(expr.operator, left, right);
+ return (double) left * (double) right;
}
// Unreachable.