12.7 Constructors and Initializers

This commit is contained in:
Tom Willemse 2021-06-12 12:17:43 -07:00
parent e62d1a209e
commit 958ba22a57
4 changed files with 65 additions and 48 deletions

View file

@ -185,7 +185,7 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
Map<String, LoxFunction> methods = new HashMap<>(); Map<String, LoxFunction> methods = new HashMap<>();
for (Stmt.Function method : stmt.methods) { 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); methods.put(method.name.lexeme, function);
} }
@ -202,7 +202,7 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
@Override @Override
public Void visitFunctionStmt(Stmt.Function stmt) { 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); environment.define(stmt.name.lexeme, function);
return null; return null;
} }

View file

@ -28,11 +28,17 @@ class LoxClass implements LoxCallable {
@Override @Override
public Object call(Interpreter interpreter, List<Object> arguments) { public Object call(Interpreter interpreter, List<Object> arguments) {
LoxInstance instance = new LoxInstance(this); LoxInstance instance = new LoxInstance(this);
LoxFunction initializer = findMethod("init");
if (initializer != null) {
initializer.bind(instance).call(interpreter, arguments);
}
return instance; return instance;
} }
@Override @Override
public int arity() { public int arity() {
return 0; LoxFunction initializer = findMethod("init");
if (initializer == null) return 0;
return initializer.arity();
} }
} }

View file

@ -5,8 +5,10 @@ import java.util.List;
class LoxFunction implements LoxCallable { class LoxFunction implements LoxCallable {
private final Stmt.Function declaration; private final Stmt.Function declaration;
private final Environment closure; 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.closure = closure;
this.declaration = declaration; this.declaration = declaration;
} }
@ -14,7 +16,7 @@ class LoxFunction implements LoxCallable {
LoxFunction bind(LoxInstance instance) { LoxFunction bind(LoxInstance instance) {
Environment environment = new Environment(closure); Environment environment = new Environment(closure);
environment.define("this", instance); environment.define("this", instance);
return new LoxFunction(declaration, environment); return new LoxFunction(declaration, environment, isInitializer);
} }
@Override @Override
@ -33,8 +35,13 @@ class LoxFunction implements LoxCallable {
try { try {
interpreter.executeBlock(declaration.body, environment); interpreter.executeBlock(declaration.body, environment);
} catch (Return returnValue) { } catch (Return returnValue) {
if (isInitializer)
return closure.getAt(0, "this");
return returnValue.value; return returnValue.value;
} }
if (isInitializer)
return closure.getAt(0, "this");
return null; return null;
} }

View file

@ -15,12 +15,11 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
} }
private enum FunctionType { private enum FunctionType {
NONE, FUNCTION, METHOD NONE, FUNCTION, INITIALIZER, METHOD
} }
private enum ClassType { private enum ClassType {
NONE, NONE, CLASS
CLASS
} }
private ClassType currentClass = ClassType.NONE; private ClassType currentClass = ClassType.NONE;
@ -52,6 +51,9 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
for (Stmt.Function method : stmt.methods) { for (Stmt.Function method : stmt.methods) {
FunctionType declaration = FunctionType.METHOD; FunctionType declaration = FunctionType.METHOD;
if (method.name.lexeme.equals("init")) {
declaration = FunctionType.INITIALIZER;
}
resolveFunction(method, declaration); resolveFunction(method, declaration);
} }
@ -98,6 +100,9 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
} }
if (stmt.value != null) { if (stmt.value != null) {
if (currentFunction == FunctionType.INITIALIZER) {
Lox.error(stmt.keyword, "Can't return a value from an initializer.");
}
resolve(stmt.value); resolve(stmt.value);
} }
@ -173,8 +178,7 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
@Override @Override
public Void visitThisExpr(Expr.This expr) { public Void visitThisExpr(Expr.This expr) {
if (currentClass == ClassType.NONE) { if (currentClass == ClassType.NONE) {
Lox.error(expr.keyword, Lox.error(expr.keyword, "Can't use 'this' outside of a class.");
"Can't use 'this' outside of a class.");
return null; return null;
} }