From c7e62694f88614fac875908d4f044724a3979f4d Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Tue, 2 Jul 2024 12:12:00 +0200 Subject: [PATCH] Resolving and binding chapter --- src/lox/Environment.java | 17 ++++ src/lox/Interpreter.java | 27 ++++- src/lox/Lox.java | 5 + src/lox/Resolver.java | 208 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 src/lox/Resolver.java diff --git a/src/lox/Environment.java b/src/lox/Environment.java index e1f5a1b..0271562 100644 --- a/src/lox/Environment.java +++ b/src/lox/Environment.java @@ -44,4 +44,21 @@ public class Environment { void define(String name, Object value) { values.put(name, value); } + + Environment ancestor(int distance) { + Environment environment = this; + for (int i = 0; i < distance; i++) { + environment = environment.enclosing; + } + + return environment; + } + + 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); + } } diff --git a/src/lox/Interpreter.java b/src/lox/Interpreter.java index 85bc28e..945a30f 100644 --- a/src/lox/Interpreter.java +++ b/src/lox/Interpreter.java @@ -1,12 +1,15 @@ package lox; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class Interpreter implements Expr.Visitor, Stmt.Visitor { final Environment globals = new Environment(); private Environment environment = globals; + private final Map locals = new HashMap<>(); Interpreter() { globals.define("clock", new LoxCallable() { @@ -40,7 +43,14 @@ public class Interpreter implements Expr.Visitor, @Override public Object visitAssignExpr(Expr.Assign expr) { Object value = evaluate(expr.value); - environment.assign(expr.name, value); + + Integer distance = locals.get(expr); + if (distance != null) { + environment.assignAt(distance, expr.name, value); + } else { + globals.assign(expr.name, value); + } + return value; } @@ -153,7 +163,16 @@ public class Interpreter implements Expr.Visitor, @Override public Object visitVariableExpr(Expr.Variable expr) { - return environment.get(expr.name); + 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) { @@ -203,6 +222,10 @@ public class Interpreter implements Expr.Visitor, stmt.accept(this); } + void resolve(Expr expr, int depth) { + locals.put(expr, depth); + } + void executeBlock(List statements, Environment environment) { Environment previous = this.environment; diff --git a/src/lox/Lox.java b/src/lox/Lox.java index 17bd62c..c7c8e76 100644 --- a/src/lox/Lox.java +++ b/src/lox/Lox.java @@ -54,6 +54,11 @@ public class Lox { if (hadError) return; + Resolver resolver = new Resolver(interpreter); + resolver.resolve(statements); + + if (hadError) return; + interpreter.interpret(statements); } diff --git a/src/lox/Resolver.java b/src/lox/Resolver.java new file mode 100644 index 0000000..e84b03e --- /dev/null +++ b/src/lox/Resolver.java @@ -0,0 +1,208 @@ +package lox; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Stack; + +public class Resolver implements Expr.Visitor, Stmt.Visitor { + private final Interpreter interpreter; + private final Stack> scopes = new Stack<>(); + private FunctionType currentFunction = FunctionType.NONE; + + Resolver(Interpreter interpreter) { + this.interpreter = interpreter; + } + + private enum FunctionType { + NONE, + FUNCTION + } + + void resolve(List statements) { + for (Stmt statement : statements) { + resolve(statement); + } + } + + @Override + public Void visitAssignExpr(Expr.Assign expr) { + resolve(expr.value); + resolveLocal(expr, expr.name); + return null; + } + + @Override + public Void visitBinaryExpr(Expr.Binary expr) { + resolve(expr.left); + resolve(expr.right); + return null; + } + + @Override + public Void visitCallExpr(Expr.Call expr) { + resolve(expr.callee); + + for (Expr argument : expr.arguments) { + resolve(argument); + } + + return null; + } + + @Override + public Void visitGroupingExpr(Expr.Grouping expr) { + resolve(expr.expression); + return null; + } + + @Override + public Void visitLiteralExpr(Expr.Literal expr) { + return null; + } + + @Override + public Void visitLogicalExpr(Expr.Logical expr) { + resolve(expr.left); + resolve(expr.right); + return null; + } + + @Override + public Void visitUnaryExpr(Expr.Unary expr) { + resolve(expr.right); + return null; + } + + @Override + public Void visitVariableExpr(Expr.Variable expr) { + if (!scopes.isEmpty() && + scopes.peek().get(expr.name.lexeme) == Boolean.FALSE) { + Lox.error(expr.name, + "Can't read local variable in its own initializer."); + } + + resolveLocal(expr, expr.name); + return null; + } + + @Override + public Void visitBlockStmt(Stmt.Block stmt) { + beginScope(); + resolve(stmt.statements); + endScope(); + return null; + } + + private void resolve(Stmt stmt) { + stmt.accept(this); + } + + private void resolve(Expr expr) { + expr.accept(this); + } + + private void resolveFunction(Stmt.Function function, FunctionType type) { + FunctionType enclosingFunction = currentFunction; + currentFunction = type; + + beginScope(); + for (Token param : function.params) { + declare(param); + define(param); + } + resolve(function.body); + endScope(); + currentFunction = enclosingFunction; + } + + private void beginScope() { + scopes.push(new HashMap()); + } + + private void endScope() { + scopes.pop(); + } + + private void declare(Token name) { + if (scopes.isEmpty()) return; + + Map scope = scopes.peek(); + if (scope.containsKey(name.lexeme)) { + Lox.error(name, + "Already a variable with this name in this scope."); + } + scope.put(name.lexeme, false); + } + + private void define(Token name) { + if (scopes.isEmpty()) return; + scopes.peek().put(name.lexeme, true); + } + + private void resolveLocal(Expr expr, Token name) { + for (int i = scopes.size() - 1; i >= 0; i--) { + if (scopes.get(i).containsKey(name.lexeme)) { + interpreter.resolve(expr, scopes.size() - 1 - i); + return; + } + } + } + + @Override + public Void visitExpressionStmt(Stmt.Expression stmt) { + resolve(stmt.expression); + return null; + } + + @Override + public Void visitFunctionStmt(Stmt.Function stmt) { + declare(stmt.name); + define(stmt.name); + + resolveFunction(stmt, FunctionType.FUNCTION); + return null; + } + + @Override + public Void visitIfStmt(Stmt.If stmt) { + resolve(stmt.condition); + resolve(stmt.thenBranch); + if (stmt.elseBranch != null) resolve(stmt.elseBranch); + return null; + } + + @Override + public Void visitPrintStmt(Stmt.Print stmt) { + resolve(stmt.expression); + return null; + } + + @Override + public Void visitReturnStmt(Stmt.Return stmt) { + if (currentFunction == FunctionType.NONE) { + Lox.error(stmt.keyword, "Can't return from top-level code."); + } + + if (stmt.value != null) { + resolve(stmt.value); + } + + return null; + } + + @Override + public Void visitVarStmt(Stmt.Var stmt) { + declare(stmt.name); + if (stmt.initializer != null) { + resolve(stmt.initializer); + } + define(stmt.name); + return null; + } + + @Override + public Void visitWhileStmt(Stmt.While stmt) { + return null; + } +}