aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/craftinginterpreters/lox/Environment.java
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-03-03 22:44:52 -0800
committerGravatar Tom Willemse2021-03-03 22:44:52 -0800
commitb04b6bcf2f31526f6cfa593c342165a65cb9e7ac (patch)
treeaefe4490a9b7ce6adb48c826ef9c5da483335e55 /src/com/craftinginterpreters/lox/Environment.java
parent715e60b87ea39b0b2acc293e8639e8527c260f93 (diff)
downloadcrafting-interpreters-b04b6bcf2f31526f6cfa593c342165a65cb9e7ac.tar.gz
crafting-interpreters-b04b6bcf2f31526f6cfa593c342165a65cb9e7ac.zip
Chapter 11 - Resolving and Binding
Diffstat (limited to 'src/com/craftinginterpreters/lox/Environment.java')
-rw-r--r--src/com/craftinginterpreters/lox/Environment.java17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/com/craftinginterpreters/lox/Environment.java b/src/com/craftinginterpreters/lox/Environment.java
index 0a95282..65c6d88 100644
--- a/src/com/craftinginterpreters/lox/Environment.java
+++ b/src/com/craftinginterpreters/lox/Environment.java
@@ -43,4 +43,21 @@ 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);
+ }
}