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, Stmt.Visitor { 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, Stmt.Visitor { Map 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, Stmt.Visitor { @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, Stmt.Visitor { 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; - } + 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; - } + 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; + 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. diff --git a/src/com/craftinginterpreters/lox/LoxClass.java b/src/com/craftinginterpreters/lox/LoxClass.java index 0c0223f..5e0e755 100644 --- a/src/com/craftinginterpreters/lox/LoxClass.java +++ b/src/com/craftinginterpreters/lox/LoxClass.java @@ -28,11 +28,17 @@ class LoxClass implements LoxCallable { @Override public Object call(Interpreter interpreter, List arguments) { LoxInstance instance = new LoxInstance(this); + LoxFunction initializer = findMethod("init"); + if (initializer != null) { + initializer.bind(instance).call(interpreter, arguments); + } return instance; } @Override public int arity() { - return 0; + LoxFunction initializer = findMethod("init"); + if (initializer == null) return 0; + return initializer.arity(); } } diff --git a/src/com/craftinginterpreters/lox/LoxFunction.java b/src/com/craftinginterpreters/lox/LoxFunction.java index 371b822..07dd727 100644 --- a/src/com/craftinginterpreters/lox/LoxFunction.java +++ b/src/com/craftinginterpreters/lox/LoxFunction.java @@ -5,8 +5,10 @@ import java.util.List; class LoxFunction implements LoxCallable { private final Stmt.Function declaration; private final Environment closure; + private final boolean isInitializer; - LoxFunction(Stmt.Function declaration, Environment closure) { + LoxFunction(Stmt.Function declaration, Environment closure, boolean isInitializer) { + this.isInitializer = isInitializer; this.closure = closure; this.declaration = declaration; } @@ -14,7 +16,7 @@ class LoxFunction implements LoxCallable { LoxFunction bind(LoxInstance instance) { Environment environment = new Environment(closure); environment.define("this", instance); - return new LoxFunction(declaration, environment); + return new LoxFunction(declaration, environment, isInitializer); } @Override @@ -33,8 +35,13 @@ class LoxFunction implements LoxCallable { try { interpreter.executeBlock(declaration.body, environment); } catch (Return returnValue) { + if (isInitializer) + return closure.getAt(0, "this"); return returnValue.value; } + + if (isInitializer) + return closure.getAt(0, "this"); return null; } diff --git a/src/com/craftinginterpreters/lox/Resolver.java b/src/com/craftinginterpreters/lox/Resolver.java index 70c5970..cda0f60 100644 --- a/src/com/craftinginterpreters/lox/Resolver.java +++ b/src/com/craftinginterpreters/lox/Resolver.java @@ -15,12 +15,11 @@ class Resolver implements Expr.Visitor, Stmt.Visitor { } private enum FunctionType { - NONE, FUNCTION, METHOD + NONE, FUNCTION, INITIALIZER, METHOD } private enum ClassType { - NONE, - CLASS + NONE, CLASS } private ClassType currentClass = ClassType.NONE; @@ -52,6 +51,9 @@ class Resolver implements Expr.Visitor, Stmt.Visitor { for (Stmt.Function method : stmt.methods) { FunctionType declaration = FunctionType.METHOD; + if (method.name.lexeme.equals("init")) { + declaration = FunctionType.INITIALIZER; + } resolveFunction(method, declaration); } @@ -98,6 +100,9 @@ class Resolver implements Expr.Visitor, Stmt.Visitor { } if (stmt.value != null) { + if (currentFunction == FunctionType.INITIALIZER) { + Lox.error(stmt.keyword, "Can't return a value from an initializer."); + } resolve(stmt.value); } @@ -173,8 +178,7 @@ class Resolver implements Expr.Visitor, Stmt.Visitor { @Override public Void visitThisExpr(Expr.This expr) { if (currentClass == ClassType.NONE) { - Lox.error(expr.keyword, - "Can't use 'this' outside of a class."); + Lox.error(expr.keyword, "Can't use 'this' outside of a class."); return null; }