[Chapter12&Classes] - Adding support for classes
This commit is contained in:
parent
9559cab2ce
commit
05a8ac6127
@ -39,219 +39,260 @@ class Interpreter implements Expr.Visitor<Object>,Stmt.Visitor<Void> {
|
|||||||
return evaluate(expr.right);
|
return evaluate(expr.right);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public Object visitUnaryExpr(Expr.Unary expr) {
|
public Object visitSetExpr(Expr.Set expr) {
|
||||||
Object right = evaluate(expr.right);
|
Object object = evaluate(expr.object);
|
||||||
|
|
||||||
switch (expr.operator.type) {
|
if (!(object instanceof LoxInstance)) {
|
||||||
case BANG:
|
throw new RuntimeError(expr.name,
|
||||||
return !isTruthy(right);
|
"Only instances have fields.");
|
||||||
case MINUS:
|
}
|
||||||
checkNumberOperand(expr.operator, right);
|
|
||||||
return -(double)right;
|
Object value = evaluate(expr.value);
|
||||||
|
((LoxInstance)object).set(expr.name, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object visitThisExpr(Expr.This expr) {
|
||||||
|
return lookUpVariable(expr.keyword, expr);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object visitUnaryExpr(Expr.Unary expr) {
|
||||||
|
Object right = evaluate(expr.right);
|
||||||
|
|
||||||
|
switch (expr.operator.type) {
|
||||||
|
case BANG:
|
||||||
|
return !isTruthy(right);
|
||||||
|
case MINUS:
|
||||||
|
checkNumberOperand(expr.operator, right);
|
||||||
|
return -(double)right;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unreachable.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object visitVariableExpr(Expr.Variable expr) {
|
||||||
|
return lookUpVariable(expr.name, expr);
|
||||||
|
}
|
||||||
|
private Object lookUpVariable(Token name, Expr expr) {
|
||||||
|
Integer distance = locals.get(expr);
|
||||||
|
if (distance != null) {
|
||||||
|
return environment.getAt(distance, name.lexeme);
|
||||||
|
} else {
|
||||||
|
return globals.get(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void checkNumberOperand(Token operator, Object operand) {
|
||||||
|
if (operand instanceof Double) return;
|
||||||
|
throw new RuntimeError(operator, "Operand must be a number.");
|
||||||
|
}
|
||||||
|
private void checkNumberOperands(Token operator,
|
||||||
|
Object left, Object right) {
|
||||||
|
if (left instanceof Double && right instanceof Double) return;
|
||||||
|
|
||||||
|
throw new RuntimeError(operator, "Operands must be numbers.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unreachable.
|
@Override
|
||||||
return null;
|
public Object visitGroupingExpr(Expr.Grouping expr) {
|
||||||
}
|
return evaluate(expr.expression);
|
||||||
@Override
|
}
|
||||||
public Object visitVariableExpr(Expr.Variable expr) {
|
@Override
|
||||||
return lookUpVariable(expr.name, expr);
|
public Object visitBinaryExpr(Expr.Binary expr) {
|
||||||
}
|
Object left = evaluate(expr.left);
|
||||||
private Object lookUpVariable(Token name, Expr expr) {
|
Object right = evaluate(expr.right);
|
||||||
Integer distance = locals.get(expr);
|
|
||||||
if (distance != null) {
|
switch (expr.operator.type) {
|
||||||
return environment.getAt(distance, name.lexeme);
|
case GREATER:
|
||||||
} else {
|
checkNumberOperands(expr.operator, left, right);
|
||||||
return globals.get(name);
|
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 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 BANG_EQUAL: return !isEqual(left, right);
|
||||||
|
case EQUAL_EQUAL: return isEqual(left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unreachable.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object visitCallExpr(Expr.Call expr){
|
||||||
|
Object callee = evaluate(expr.callee);
|
||||||
|
List<Object> arguments = new ArrayList<>();
|
||||||
|
for (Expr argument : expr.arguments){
|
||||||
|
arguments.add(evaluate(argument));
|
||||||
|
}
|
||||||
|
if(!(callee instanceof LoxCallable)){
|
||||||
|
throw new RuntimeError(expr.paren,"Can only call functions and classes");
|
||||||
|
}
|
||||||
|
LoxCallable function = (LoxCallable)callee;
|
||||||
|
if(arguments.size() != function.arity()){
|
||||||
|
throw new RuntimeError(expr.paren, "Expected " + function.arity() + " arguments but got " + arguments.size() + ".");
|
||||||
|
}
|
||||||
|
return function.call(this, arguments);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object visitGetExpr(Expr.Get expr) {
|
||||||
|
Object object = evaluate(expr.object);
|
||||||
|
if (object instanceof LoxInstance) {
|
||||||
|
return ((LoxInstance) object).get(expr.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeError(expr.name,
|
||||||
|
"Only instances have properties.");
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitExpressionStmt(Stmt.Expression stmt) {
|
||||||
|
evaluate(stmt.expression);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitFunctionStmt(Stmt.Function stmt) {
|
||||||
|
LoxFunction function = new LoxFunction(stmt, environment,
|
||||||
|
false);
|
||||||
|
environment.define(stmt.name.lexeme, function);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitIfStmt(Stmt.If stmt){
|
||||||
|
if(isTruthy(evaluate(stmt.condition))){
|
||||||
|
execute(stmt.thenBranch);
|
||||||
|
}
|
||||||
|
else if(stmt.elseBranch != null){
|
||||||
|
execute(stmt.elseBranch);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitPrintStmt(Stmt.Print stmt) {
|
||||||
|
Object value = evaluate(stmt.expression);
|
||||||
|
System.out.println(stringify(value));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitReturnStmt(Stmt.Return stmt) {
|
||||||
|
Object value = null;
|
||||||
|
if (stmt.value != null) value = evaluate(stmt.value);
|
||||||
|
|
||||||
|
throw new Return(value);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitVarStmt(Stmt.Var stmt) {
|
||||||
|
Object value = null;
|
||||||
|
if (stmt.initializer != null) {
|
||||||
|
value = evaluate(stmt.initializer);
|
||||||
|
}
|
||||||
|
|
||||||
|
environment.define(stmt.name.lexeme, value);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitWhileStmt(Stmt.While stmt){
|
||||||
|
while(isTruthy(evaluate(stmt.condition))){
|
||||||
|
execute(stmt.body);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object visitAssignExpr(Expr.Assign expr) {
|
||||||
|
Object value = evaluate(expr.value);
|
||||||
|
Integer distance = locals.get(expr);
|
||||||
|
if (distance != null) {
|
||||||
|
environment.assignAt(distance, expr.name, value);
|
||||||
|
} else {
|
||||||
|
globals.assign(expr.name, value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
private Object evaluate(Expr expr) {
|
||||||
|
return expr.accept(this);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
private void checkNumberOperand(Token operator, Object operand) {
|
|
||||||
if (operand instanceof Double) return;
|
|
||||||
throw new RuntimeError(operator, "Operand must be a number.");
|
|
||||||
}
|
|
||||||
private void checkNumberOperands(Token operator,
|
|
||||||
Object left, Object right) {
|
|
||||||
if (left instanceof Double && right instanceof Double) return;
|
|
||||||
|
|
||||||
throw new RuntimeError(operator, "Operands must be numbers.");
|
private boolean isTruthy(Object object) {
|
||||||
}
|
if (object == null) return false;
|
||||||
|
if (object instanceof Boolean) return (boolean)object;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
private boolean isEqual(Object a, Object b) {
|
||||||
|
if (a == null && b == null) return true;
|
||||||
|
if (a == null) return false;
|
||||||
|
|
||||||
@Override
|
return a.equals(b);
|
||||||
public Object visitGroupingExpr(Expr.Grouping expr) {
|
}
|
||||||
return evaluate(expr.expression);
|
void interpret(List<Stmt> statements) {
|
||||||
}
|
try {
|
||||||
@Override
|
for (Stmt statement : statements) {
|
||||||
public Object visitBinaryExpr(Expr.Binary expr) {
|
execute(statement);
|
||||||
Object left = evaluate(expr.left);
|
|
||||||
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 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,
|
} catch (RuntimeError error) {
|
||||||
"Operands must be two numbers or two strings.");
|
Lox.runtimeError(error);
|
||||||
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 BANG_EQUAL: return !isEqual(left, right);
|
|
||||||
case EQUAL_EQUAL: return isEqual(left, right);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unreachable.
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Object visitCallExpr(Expr.Call expr){
|
|
||||||
Object callee = evaluate(expr.callee);
|
|
||||||
List<Object> arguments = new ArrayList<>();
|
|
||||||
for (Expr argument : expr.arguments){
|
|
||||||
arguments.add(evaluate(argument));
|
|
||||||
}
|
|
||||||
if(!(callee instanceof LoxCallable)){
|
|
||||||
throw new RuntimeError(expr.paren,"Can only call functions and classes");
|
|
||||||
}
|
|
||||||
LoxCallable function = (LoxCallable)callee;
|
|
||||||
if(arguments.size() != function.arity()){
|
|
||||||
throw new RuntimeError(expr.paren, "Expected " + function.arity() + " arguments but got " + arguments.size() + ".");
|
|
||||||
}
|
|
||||||
return function.call(this, arguments);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Void visitExpressionStmt(Stmt.Expression stmt) {
|
|
||||||
evaluate(stmt.expression);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Void visitFunctionStmt(Stmt.Function stmt) {
|
|
||||||
LoxFunction function = new LoxFunction(stmt,environment);
|
|
||||||
environment.define(stmt.name.lexeme, function);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Void visitIfStmt(Stmt.If stmt){
|
|
||||||
if(isTruthy(evaluate(stmt.condition))){
|
|
||||||
execute(stmt.thenBranch);
|
|
||||||
}
|
|
||||||
else if(stmt.elseBranch != null){
|
|
||||||
execute(stmt.elseBranch);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Void visitPrintStmt(Stmt.Print stmt) {
|
|
||||||
Object value = evaluate(stmt.expression);
|
|
||||||
System.out.println(stringify(value));
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Void visitReturnStmt(Stmt.Return stmt) {
|
|
||||||
Object value = null;
|
|
||||||
if (stmt.value != null) value = evaluate(stmt.value);
|
|
||||||
|
|
||||||
throw new Return(value);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Void visitVarStmt(Stmt.Var stmt) {
|
|
||||||
Object value = null;
|
|
||||||
if (stmt.initializer != null) {
|
|
||||||
value = evaluate(stmt.initializer);
|
|
||||||
}
|
|
||||||
|
|
||||||
environment.define(stmt.name.lexeme, value);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Void visitWhileStmt(Stmt.While stmt){
|
|
||||||
while(isTruthy(evaluate(stmt.condition))){
|
|
||||||
execute(stmt.body);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Object visitAssignExpr(Expr.Assign expr) {
|
|
||||||
Object value = evaluate(expr.value);
|
|
||||||
Integer distance = locals.get(expr);
|
|
||||||
if (distance != null) {
|
|
||||||
environment.assignAt(distance, expr.name, value);
|
|
||||||
} else {
|
|
||||||
globals.assign(expr.name, value);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
private Object evaluate(Expr expr) {
|
|
||||||
return expr.accept(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isTruthy(Object object) {
|
|
||||||
if (object == null) return false;
|
|
||||||
if (object instanceof Boolean) return (boolean)object;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
private boolean isEqual(Object a, Object b) {
|
|
||||||
if (a == null && b == null) return true;
|
|
||||||
if (a == null) return false;
|
|
||||||
|
|
||||||
return a.equals(b);
|
|
||||||
}
|
|
||||||
void interpret(List<Stmt> statements) {
|
|
||||||
try {
|
|
||||||
for (Stmt statement : statements) {
|
|
||||||
execute(statement);
|
|
||||||
}
|
}
|
||||||
} catch (RuntimeError error) {
|
|
||||||
Lox.runtimeError(error);
|
|
||||||
}
|
}
|
||||||
}
|
private void execute(Stmt stmt) {
|
||||||
private void execute(Stmt stmt) {
|
stmt.accept(this);
|
||||||
stmt.accept(this);
|
}
|
||||||
}
|
void resolve(Expr expr, int depth) {
|
||||||
void resolve(Expr expr, int depth) {
|
locals.put(expr, depth);
|
||||||
locals.put(expr, depth);
|
}
|
||||||
}
|
void executeBlock(List<Stmt> statements,
|
||||||
void executeBlock(List<Stmt> statements,
|
Environment environment) {
|
||||||
Environment environment) {
|
Environment previous = this.environment;
|
||||||
Environment previous = this.environment;
|
try {
|
||||||
try {
|
this.environment = environment;
|
||||||
this.environment = environment;
|
|
||||||
|
|
||||||
for (Stmt statement : statements) {
|
for (Stmt statement : statements) {
|
||||||
execute(statement);
|
execute(statement);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.environment = previous;
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
this.environment = previous;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitBlockStmt(Stmt.Block stmt) {
|
public Void visitBlockStmt(Stmt.Block stmt) {
|
||||||
executeBlock(stmt.statements, new Environment(environment));
|
executeBlock(stmt.statements, new Environment(environment));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public void visitClassStmt(Stmt.Class stmt){
|
||||||
|
environment.define(stmt.name.lexeme, null);
|
||||||
|
Map<String, LoxFunction> methods = new HashMap<>();
|
||||||
|
for (Stmt.Function method : stmt.methods) {
|
||||||
|
LoxFunction function = new LoxFunction(method, environment,
|
||||||
|
method.name.lexeme.equals("init"));
|
||||||
|
methods.put(method.name.lexeme, function);
|
||||||
|
}
|
||||||
|
|
||||||
|
LoxClass klass = new LoxClass(stmt.name.lexeme, methods);
|
||||||
|
environment.assign(stmt.name, klass);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
private String stringify(Object object) {
|
private String stringify(Object object) {
|
||||||
if (object == null) return "nil";
|
if (object == null) return "nil";
|
||||||
|
|
||||||
|
|||||||
43
src/com/lox/LoxClass.java
Normal file
43
src/com/lox/LoxClass.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package com.lox;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class LoxClass implements LoxCallable {
|
||||||
|
final String name;
|
||||||
|
private final Map<String, LoxFunction> methods;
|
||||||
|
|
||||||
|
LoxClass(String name, Map<String, LoxFunction> methods) {
|
||||||
|
this.name = name;
|
||||||
|
this.methods = methods;
|
||||||
|
}
|
||||||
|
LoxFunction findMethod(String name) {
|
||||||
|
if (methods.containsKey(name)) {
|
||||||
|
return methods.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object call(
|
||||||
|
Intepreter 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(){
|
||||||
|
LoxFunction initializer = findMethod("init");
|
||||||
|
if (initializer == null) return 0;
|
||||||
|
return initializer.arity();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,10 +5,21 @@ 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;
|
||||||
LoxFunction(Stmt.Function declaration, Environment closure) {
|
private final boolean isInitializer;
|
||||||
|
|
||||||
|
LoxFunction(Stmt.Function declaration, Environment closure,
|
||||||
|
boolean isInitializer) {
|
||||||
|
this.isInitializer = isInitializer;
|
||||||
this.closure = closure;
|
this.closure = closure;
|
||||||
this.declaration = declaration;
|
this.declaration = declaration;
|
||||||
}
|
}
|
||||||
|
LoxFunction bind(LoxInstance instance) {
|
||||||
|
Environment environment = new Environment(closure);
|
||||||
|
environment.define("this", instance);
|
||||||
|
return new LoxFunction(declaration, environment,
|
||||||
|
isInitializer);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "<fn " + declaration.name.lexeme + ">";
|
return "<fn " + declaration.name.lexeme + ">";
|
||||||
@ -28,8 +39,12 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/com/lox/LoxInstance.java
Normal file
33
src/com/lox/LoxInstance.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package com.lox;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class LoxInstance {
|
||||||
|
private LoxClass klass;
|
||||||
|
private final Map<String, Object> fields = new HashMap<>();
|
||||||
|
LoxInstance(LoxClass klass){
|
||||||
|
this.klass = klass;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object get(Token name) {
|
||||||
|
if (fields.containsKey(name.lexeme)) {
|
||||||
|
return fields.get(name.lexeme);
|
||||||
|
}
|
||||||
|
LoxFunction method = klass.findMethod(name.lexeme);
|
||||||
|
|
||||||
|
if (method != null) return method.bind(this);
|
||||||
|
|
||||||
|
throw new RuntimeError(name,
|
||||||
|
"Undefined property '" + name.lexeme + "'.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void set(Token name, Object value) {
|
||||||
|
fields.put(name.lexeme, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return klass.name + " Instance";
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -31,6 +31,19 @@ class Parser {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private Stmt classDeclaration(){
|
||||||
|
Token name = consume(IDENTIFIER, "Expect class name.");
|
||||||
|
consume(LEFT_BRACE, "Expect '{' before class body");
|
||||||
|
|
||||||
|
List<Stmt.Function> methods = new ArrayList<>();
|
||||||
|
while (!check(RIGHT_BRACE) && !isAtEnd()) {
|
||||||
|
methods.add(function("method"));
|
||||||
|
}
|
||||||
|
|
||||||
|
consume(RIGHT_BRACE, "Expect '}' after class body.");
|
||||||
|
|
||||||
|
return new Stmt.Class(name,)
|
||||||
|
}
|
||||||
private Expr expression(){
|
private Expr expression(){
|
||||||
return assignment();
|
return assignment();
|
||||||
}
|
}
|
||||||
@ -174,8 +187,13 @@ class Parser {
|
|||||||
if (expr instanceof Expr.Variable) {
|
if (expr instanceof Expr.Variable) {
|
||||||
Token name = ((Expr.Variable)expr).name;
|
Token name = ((Expr.Variable)expr).name;
|
||||||
return new Expr.Assign(name, value);
|
return new Expr.Assign(name, value);
|
||||||
|
} else if (expr instanceof Expr.Get) {
|
||||||
|
Expr.Get get = (Expr.Get)expr;
|
||||||
|
return new Expr.Set(get.object, get.name, value);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
error(equals, "Invalid assignment target.");
|
error(equals, "Invalid assignment target.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,6 +293,10 @@ class Parser {
|
|||||||
while(true){
|
while(true){
|
||||||
if(match(LEFT_PAREN)){
|
if(match(LEFT_PAREN)){
|
||||||
expr = finishCall(expr);
|
expr = finishCall(expr);
|
||||||
|
} else if(match(DOT)){
|
||||||
|
Token name = consume(IDENTIFIER
|
||||||
|
, "Expect property name after '.'.");
|
||||||
|
expr = new Expr.Get(expr, name);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
break;
|
break;
|
||||||
@ -300,6 +322,8 @@ class Parser {
|
|||||||
if (match(IDENTIFIER)) {
|
if (match(IDENTIFIER)) {
|
||||||
return new Expr.Variable(previous());
|
return new Expr.Variable(previous());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (match(THIS)) return new Expr.This(previous());
|
||||||
|
|
||||||
throw error(peek(), "Expect expression.");
|
throw error(peek(), "Expect expression.");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,8 +14,18 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
|||||||
}
|
}
|
||||||
private enum FunctionType {
|
private enum FunctionType {
|
||||||
NONE,
|
NONE,
|
||||||
|
FUNCTION,
|
||||||
|
INITIALIZER,
|
||||||
|
METHOD,
|
||||||
FUNCTION
|
FUNCTION
|
||||||
}
|
}
|
||||||
|
private enum ClassType {
|
||||||
|
NONE,
|
||||||
|
CLASS
|
||||||
|
}
|
||||||
|
|
||||||
|
private ClassType currentClass = ClassType.NONE;
|
||||||
|
|
||||||
private void resolve(Expr expr) {
|
private void resolve(Expr expr) {
|
||||||
expr.accept(this);
|
expr.accept(this);
|
||||||
}
|
}
|
||||||
@ -41,6 +51,27 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
|
public void visitClassStmt(Stmt.Class stmt){
|
||||||
|
ClassType enclosingClass = currentClass;
|
||||||
|
currentClass = ClassType.CLASS;
|
||||||
|
|
||||||
|
declare(stmt.name);
|
||||||
|
define(stmt.name);
|
||||||
|
beginScope();
|
||||||
|
scopes.peek().put("this", true);
|
||||||
|
for (Stmt.Function method : stmt.methods) {
|
||||||
|
FunctionType declaration = FunctionType.METHOD;
|
||||||
|
if (method.name.lexeme.equals("init")) {
|
||||||
|
declaration = FunctionType.INITIALIZER;
|
||||||
|
}
|
||||||
|
resolveFunction(method, declaration);
|
||||||
|
}
|
||||||
|
endScope();
|
||||||
|
|
||||||
|
currentClass = enclosingClass;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
public Void visitExpressionStmt(Stmt.Expression stmt) {
|
public Void visitExpressionStmt(Stmt.Expression stmt) {
|
||||||
resolve(stmt.expression);
|
resolve(stmt.expression);
|
||||||
return null;
|
return null;
|
||||||
@ -72,6 +103,10 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
|||||||
Lox.error(stmt.keyword, "Can't return from top-level code.");
|
Lox.error(stmt.keyword, "Can't return from top-level code.");
|
||||||
}
|
}
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,7 +168,11 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitGetExpr(Expr.Get expr){
|
||||||
|
resolve(expr.object);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public Void visitGroupingExpr(Expr.Grouping expr) {
|
public Void visitGroupingExpr(Expr.Grouping expr) {
|
||||||
resolve(expr.expression);
|
resolve(expr.expression);
|
||||||
@ -150,6 +189,22 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
|||||||
resolve(expr.right);
|
resolve(expr.right);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitSetExpr(Expr.Set expr) {
|
||||||
|
resolve(expr.value);
|
||||||
|
resolve(expr.object);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitThisExpr(Expr.This expr) {
|
||||||
|
if (currentClass == ClassType.NONE) {
|
||||||
|
Lox.error(expr.keyword,
|
||||||
|
"Can't use 'this' outside of a class.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
resolveLocal(expr, expr.keyword);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitUnaryExpr(Expr.Unary expr) {
|
public Void visitUnaryExpr(Expr.Unary expr) {
|
||||||
|
|||||||
@ -16,18 +16,22 @@ public class GenerateAst {
|
|||||||
"Assign : Token name, Expr value",
|
"Assign : Token name, Expr value",
|
||||||
"Binary : Expr left, Token operator, Expr right",
|
"Binary : Expr left, Token operator, Expr right",
|
||||||
"Call : Expr callee, Token paren, List<Expr> arguments",
|
"Call : Expr callee, Token paren, List<Expr> arguments",
|
||||||
|
"Get : Expr object, Token name",
|
||||||
"Grouping : Expr expression",
|
"Grouping : Expr expression",
|
||||||
"Literal : Object value",
|
"Literal : Object value",
|
||||||
"Logical : Expr left, Token operator, Expr right",
|
"Logical : Expr left, Token operator, Expr right",
|
||||||
|
"Set : Expr object, Token name, Expr value",
|
||||||
|
"This : Token keyword",
|
||||||
"Unary : Token operator, Expr right",
|
"Unary : Token operator, Expr right",
|
||||||
"Variable : Token name"
|
"Variable : Token name"
|
||||||
));
|
));
|
||||||
defineAst(outputDir, "Stmt", Arrays.asList(
|
defineAst(outputDir, "Stmt", Arrays.asList(
|
||||||
"Block : List<Stmt> statements",
|
"Block : List<Stmt> statements",
|
||||||
|
"Class : Token name, List<Stmt.Function> methods",
|
||||||
"If : Expr condition, Stmt thenBranch," +
|
"If : Expr condition, Stmt thenBranch," +
|
||||||
" Stmt elseBranch",
|
" Stmt elseBranch",
|
||||||
"Expression : Expr expression",
|
"Expression : Expr expression",
|
||||||
"Function : Token name, List<Token> params," +
|
"Function : Token name, List<Token> params," +
|
||||||
" List<Stmt> body",
|
" List<Stmt> body",
|
||||||
"Print : Expr expression",
|
"Print : Expr expression",
|
||||||
"Return : Token keyword, Expr value",
|
"Return : Token keyword, Expr value",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user