aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/craftinginterpreters/lox/Interpreter.java2
-rw-r--r--src/com/craftinginterpreters/lox/LoxFunction.java6
2 files changed, 5 insertions, 3 deletions
diff --git a/src/com/craftinginterpreters/lox/Interpreter.java b/src/com/craftinginterpreters/lox/Interpreter.java
index 4549e11..3e3be86 100644
--- a/src/com/craftinginterpreters/lox/Interpreter.java
+++ b/src/com/craftinginterpreters/lox/Interpreter.java
@@ -153,7 +153,7 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
@Override
public Void visitFunctionStmt(Stmt.Function stmt) {
- LoxFunction function = new LoxFunction(stmt);
+ LoxFunction function = new LoxFunction(stmt, environment);
environment.define(stmt.name.lexeme, function);
return null;
}
diff --git a/src/com/craftinginterpreters/lox/LoxFunction.java b/src/com/craftinginterpreters/lox/LoxFunction.java
index 0f7ebd4..fb3e8e3 100644
--- a/src/com/craftinginterpreters/lox/LoxFunction.java
+++ b/src/com/craftinginterpreters/lox/LoxFunction.java
@@ -4,8 +4,10 @@ import java.util.List;
class LoxFunction implements LoxCallable {
private final Stmt.Function declaration;
+ private final Environment closure;
- LoxFunction(Stmt.Function declaration) {
+ LoxFunction(Stmt.Function declaration, Environment closure) {
+ this.closure = closure;
this.declaration = declaration;
}
@@ -16,7 +18,7 @@ class LoxFunction implements LoxCallable {
@Override
public Object call(Interpreter interpreter, List<Object> arguments) {
- Environment environment = new Environment(interpreter.globals);
+ Environment environment = new Environment(closure);
for (int i = 0; i < declaration.params.size(); i++) {
environment.define(declaration.params.get(i).lexeme, arguments.get(i));