[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);
|
||||
}
|
||||
@Override
|
||||
public Object visitUnaryExpr(Expr.Unary expr) {
|
||||
Object right = evaluate(expr.right);
|
||||
public Object visitSetExpr(Expr.Set expr) {
|
||||
Object object = evaluate(expr.object);
|
||||
|
||||
switch (expr.operator.type) {
|
||||
case BANG:
|
||||
return !isTruthy(right);
|
||||
case MINUS:
|
||||
checkNumberOperand(expr.operator, right);
|
||||
return -(double)right;
|
||||
if (!(object instanceof LoxInstance)) {
|
||||
throw new RuntimeError(expr.name,
|
||||
"Only instances have fields.");
|
||||
}
|
||||
|
||||
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.
|
||||
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);
|
||||
@Override
|
||||
public Object visitGroupingExpr(Expr.Grouping expr) {
|
||||
return evaluate(expr.expression);
|
||||
}
|
||||
@Override
|
||||
public Object visitBinaryExpr(Expr.Binary expr) {
|
||||
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,
|
||||
"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
|
||||
public Object visitGroupingExpr(Expr.Grouping expr) {
|
||||
return evaluate(expr.expression);
|
||||
}
|
||||
@Override
|
||||
public Object visitBinaryExpr(Expr.Binary expr) {
|
||||
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;
|
||||
return a.equals(b);
|
||||
}
|
||||
void interpret(List<Stmt> statements) {
|
||||
try {
|
||||
for (Stmt statement : statements) {
|
||||
execute(statement);
|
||||
}
|
||||
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 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);
|
||||
}
|
||||
} catch (RuntimeError error) {
|
||||
Lox.runtimeError(error);
|
||||
}
|
||||
}
|
||||
private void execute(Stmt stmt) {
|
||||
stmt.accept(this);
|
||||
}
|
||||
void resolve(Expr expr, int depth) {
|
||||
locals.put(expr, depth);
|
||||
}
|
||||
void executeBlock(List<Stmt> statements,
|
||||
Environment environment) {
|
||||
Environment previous = this.environment;
|
||||
try {
|
||||
this.environment = environment;
|
||||
private void execute(Stmt stmt) {
|
||||
stmt.accept(this);
|
||||
}
|
||||
void resolve(Expr expr, int depth) {
|
||||
locals.put(expr, depth);
|
||||
}
|
||||
void executeBlock(List<Stmt> statements,
|
||||
Environment environment) {
|
||||
Environment previous = this.environment;
|
||||
try {
|
||||
this.environment = environment;
|
||||
|
||||
for (Stmt statement : statements) {
|
||||
execute(statement);
|
||||
for (Stmt statement : statements) {
|
||||
execute(statement);
|
||||
}
|
||||
} finally {
|
||||
this.environment = previous;
|
||||
}
|
||||
} finally {
|
||||
this.environment = previous;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitBlockStmt(Stmt.Block stmt) {
|
||||
executeBlock(stmt.statements, new Environment(environment));
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Void visitBlockStmt(Stmt.Block stmt) {
|
||||
executeBlock(stmt.statements, new Environment(environment));
|
||||
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) {
|
||||
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 {
|
||||
private final Stmt.Function declaration;
|
||||
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.declaration = declaration;
|
||||
}
|
||||
LoxFunction bind(LoxInstance instance) {
|
||||
Environment environment = new Environment(closure);
|
||||
environment.define("this", instance);
|
||||
return new LoxFunction(declaration, environment,
|
||||
isInitializer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<fn " + declaration.name.lexeme + ">";
|
||||
@ -28,8 +39,12 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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(){
|
||||
return assignment();
|
||||
}
|
||||
@ -174,8 +187,13 @@ class Parser {
|
||||
if (expr instanceof Expr.Variable) {
|
||||
Token name = ((Expr.Variable)expr).name;
|
||||
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.");
|
||||
}
|
||||
|
||||
@ -275,6 +293,10 @@ class Parser {
|
||||
while(true){
|
||||
if(match(LEFT_PAREN)){
|
||||
expr = finishCall(expr);
|
||||
} else if(match(DOT)){
|
||||
Token name = consume(IDENTIFIER
|
||||
, "Expect property name after '.'.");
|
||||
expr = new Expr.Get(expr, name);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
@ -300,6 +322,8 @@ class Parser {
|
||||
if (match(IDENTIFIER)) {
|
||||
return new Expr.Variable(previous());
|
||||
}
|
||||
|
||||
if (match(THIS)) return new Expr.This(previous());
|
||||
|
||||
throw error(peek(), "Expect expression.");
|
||||
}
|
||||
|
||||
@ -14,8 +14,18 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||
}
|
||||
private enum FunctionType {
|
||||
NONE,
|
||||
FUNCTION,
|
||||
INITIALIZER,
|
||||
METHOD,
|
||||
FUNCTION
|
||||
}
|
||||
private enum ClassType {
|
||||
NONE,
|
||||
CLASS
|
||||
}
|
||||
|
||||
private ClassType currentClass = ClassType.NONE;
|
||||
|
||||
private void resolve(Expr expr) {
|
||||
expr.accept(this);
|
||||
}
|
||||
@ -41,6 +51,27 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
@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) {
|
||||
resolve(stmt.expression);
|
||||
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.");
|
||||
}
|
||||
if (stmt.value != null) {
|
||||
if (currentFunction == FunctionType.INITIALIZER) {
|
||||
Lox.error(stmt.keyword,
|
||||
"Can't return a value from an initializer.");
|
||||
}
|
||||
resolve(stmt.value);
|
||||
}
|
||||
|
||||
@ -133,7 +168,11 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitGetExpr(Expr.Get expr){
|
||||
resolve(expr.object);
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Void visitGroupingExpr(Expr.Grouping expr) {
|
||||
resolve(expr.expression);
|
||||
@ -150,6 +189,22 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||
resolve(expr.right);
|
||||
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
|
||||
public Void visitUnaryExpr(Expr.Unary expr) {
|
||||
|
||||
@ -16,18 +16,22 @@ public class GenerateAst {
|
||||
"Assign : Token name, Expr value",
|
||||
"Binary : Expr left, Token operator, Expr right",
|
||||
"Call : Expr callee, Token paren, List<Expr> arguments",
|
||||
"Get : Expr object, Token name",
|
||||
"Grouping : Expr expression",
|
||||
"Literal : Object value",
|
||||
"Logical : Expr left, Token operator, Expr right",
|
||||
"Set : Expr object, Token name, Expr value",
|
||||
"This : Token keyword",
|
||||
"Unary : Token operator, Expr right",
|
||||
"Variable : Token name"
|
||||
));
|
||||
defineAst(outputDir, "Stmt", Arrays.asList(
|
||||
"Block : List<Stmt> statements",
|
||||
"Class : Token name, List<Stmt.Function> methods",
|
||||
"If : Expr condition, Stmt thenBranch," +
|
||||
" Stmt elseBranch",
|
||||
"Expression : Expr expression",
|
||||
"Function : Token name, List<Token> params," +
|
||||
"Function : Token name, List<Token> params," +
|
||||
" List<Stmt> body",
|
||||
"Print : Expr expression",
|
||||
"Return : Token keyword, Expr value",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user