12.7 Constructors and Initializers
This commit is contained in:
parent
e62d1a209e
commit
958ba22a57
4 changed files with 65 additions and 48 deletions
|
@ -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;
|
||||
}
|
||||
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.
|
||||
|
|
|
@ -28,11 +28,17 @@ class LoxClass implements LoxCallable {
|
|||
@Override
|
||||
public Object call(Interpreter interpreter, List<Object> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,12 +15,11 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
|||
}
|
||||
|
||||
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<Void>, Stmt.Visitor<Void> {
|
|||
|
||||
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<Void>, Stmt.Visitor<Void> {
|
|||
}
|
||||
|
||||
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<Void>, Stmt.Visitor<Void> {
|
|||
@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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue