aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/craftinginterpreters/lox/Interpreter.java
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-06-12 12:17:43 -0700
committerGravatar Tom Willemse2021-06-12 12:17:43 -0700
commit958ba22a570594ed2ce60d24cd082ce3b8a6b89a (patch)
tree81c48eb2de22314be24449b2333d84e465d2c8f6 /src/com/craftinginterpreters/lox/Interpreter.java
parente62d1a209e352306f36879dadd85773167e06246 (diff)
downloadcrafting-interpreters-958ba22a570594ed2ce60d24cd082ce3b8a6b89a.tar.gz
crafting-interpreters-958ba22a570594ed2ce60d24cd082ce3b8a6b89a.zip
12.7 Constructors and Initializers
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.