[Chapter13&Inheritance] - Fixing compilation errors
This commit is contained in:
parent
2117e9ed07
commit
a7d56eaf86
@ -4,6 +4,23 @@ class AstPrinter implements Expr.Visitor<String> {
|
||||
String print(Expr expr) {
|
||||
return expr.accept(this);
|
||||
}
|
||||
@Override
|
||||
public String visitSuperExpr(Expr.Super expr){
|
||||
return "TODO";
|
||||
}
|
||||
@Override
|
||||
public String visitSetExpr(Expr.Set expr){
|
||||
return "TODO";
|
||||
}
|
||||
@Override
|
||||
public String visitGetExpr(Expr.Get expr) {
|
||||
return "TODO";
|
||||
}
|
||||
@Override
|
||||
public String visitThisExpr(Expr.This expr) {
|
||||
return "TODO";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitCallExpr(Expr.Call expr){
|
||||
return "TODO";
|
||||
|
||||
@ -40,9 +40,6 @@ class Environment {
|
||||
void define(String name, Object value) {
|
||||
values.put(name, value);
|
||||
}
|
||||
Object getAt(int distance, String name) {
|
||||
return ancestor(distance).values.get(name);
|
||||
}
|
||||
void assignAt(int distance, Token name, Object value) {
|
||||
ancestor(distance).values.put(name.lexeme, value);
|
||||
}
|
||||
|
||||
@ -53,6 +53,8 @@ class Interpreter implements Expr.Visitor<Object>,Stmt.Visitor<Void> {
|
||||
@Override
|
||||
public Object visitSuperExpr(Expr.Super expr) {
|
||||
int distance = locals.get(expr);
|
||||
LoxClass superclass = (LoxClass)environment.getAt(
|
||||
distance,"super");
|
||||
LoxInstance object = (LoxInstance)environment.getAt(
|
||||
distance - 1, "this");
|
||||
LoxFunction method = superclass.findMethod(expr.method.lexeme);
|
||||
@ -290,7 +292,7 @@ class Interpreter implements Expr.Visitor<Object>,Stmt.Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public void visitClassStmt(Stmt.Class stmt){
|
||||
public Void visitClassStmt(Stmt.Class stmt){
|
||||
Object superclass = null;
|
||||
if (stmt.superclass != null) {
|
||||
superclass = evaluate(stmt.superclass);
|
||||
@ -311,7 +313,10 @@ class Interpreter implements Expr.Visitor<Object>,Stmt.Visitor<Void> {
|
||||
methods.put(method.name.lexeme, function);
|
||||
}
|
||||
|
||||
LoxClass klass = new LoxClass(stmt.name.lexeme, methods,(LoxClass)superclass,methods);
|
||||
LoxClass klass = new LoxClass(
|
||||
stmt.name.lexeme
|
||||
, (LoxClass)superclass
|
||||
, methods);
|
||||
if (superclass != null) {
|
||||
environment = environment.enclosing;
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ class LoxClass implements LoxCallable {
|
||||
}
|
||||
@Override
|
||||
public Object call(
|
||||
Intepreter interpreter
|
||||
Interpreter interpreter
|
||||
, List<Object> arguments){
|
||||
LoxInstance instance = new LoxInstance(this);
|
||||
LoxFunction initializer = findMethod("init");
|
||||
|
||||
@ -16,8 +16,7 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||
NONE,
|
||||
FUNCTION,
|
||||
INITIALIZER,
|
||||
METHOD,
|
||||
FUNCTION
|
||||
METHOD
|
||||
}
|
||||
private enum ClassType {
|
||||
NONE,
|
||||
@ -27,9 +26,6 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||
|
||||
private ClassType currentClass = ClassType.NONE;
|
||||
|
||||
private void resolve(Expr expr) {
|
||||
expr.accept(this);
|
||||
}
|
||||
private void resolveFunction(
|
||||
Stmt.Function function, FunctionType type) {
|
||||
FunctionType enclosingFunction = currentFunction;
|
||||
@ -52,7 +48,7 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public void visitClassStmt(Stmt.Class stmt){
|
||||
public Void visitClassStmt(Stmt.Class stmt){
|
||||
ClassType enclosingClass = currentClass;
|
||||
currentClass = ClassType.CLASS;
|
||||
|
||||
@ -144,7 +140,7 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||
scopes.pop();
|
||||
}
|
||||
@Override
|
||||
public void visitVarStmt(Stmt.Var stmt){
|
||||
public Void visitVarStmt(Stmt.Var stmt){
|
||||
declare(stmt.name);
|
||||
if(stmt.initializer != null){
|
||||
resolve(stmt.initializer);
|
||||
|
||||
@ -27,8 +27,9 @@ abstract class Stmt {
|
||||
final List<Stmt> statements;
|
||||
}
|
||||
static class Class extends Stmt {
|
||||
Class(Token name, List<Stmt.Function> methods) {
|
||||
Class(Token name, Expr.Variable superclass , List<Stmt.Function> methods) {
|
||||
this.name = name;
|
||||
this.superclass = superclass;
|
||||
this.methods = methods;
|
||||
}
|
||||
|
||||
@ -38,6 +39,7 @@ abstract class Stmt {
|
||||
}
|
||||
|
||||
final Token name;
|
||||
final Expr.Variable superclass ;
|
||||
final List<Stmt.Function> methods;
|
||||
}
|
||||
static class If extends Stmt {
|
||||
|
||||
@ -28,7 +28,7 @@ public class GenerateAst {
|
||||
));
|
||||
defineAst(outputDir, "Stmt", Arrays.asList(
|
||||
"Block : List<Stmt> statements",
|
||||
"Class : Token name, List<Stmt.Function> methods",
|
||||
"Class : Token name, Expr.Variable superclass , List<Stmt.Function> methods",
|
||||
"If : Expr condition, Stmt thenBranch," +
|
||||
" Stmt elseBranch",
|
||||
"Expression : Expr expression",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user