aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-04 17:27:54 +0100
committerMichael Williamson <mike@zwobble.org>2015-10-04 17:27:54 +0100
commite8c8fe86c757bd1208427c0ad4f8134f1b1bff1f (patch)
treecf5dea583a0c0222af227aa439729422f274d1cd /src
parent87f73abf5e7dcec6a9f5ebd213f4a9eb0a5d45db (diff)
Throw exception if getting value of variable out of scope
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/Environment.java5
-rw-r--r--src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterTests.java13
2 files changed, 16 insertions, 2 deletions
diff --git a/src/main/java/org/zwobble/couscous/interpreter/Environment.java b/src/main/java/org/zwobble/couscous/interpreter/Environment.java
index 5985a94..d748654 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/Environment.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/Environment.java
@@ -18,8 +18,9 @@ public class Environment {
this.stackFrame = new HashMap<>(stackFrame);
}
- public InterpreterValue get(int referentId) {
- return stackFrame.get(referentId).get();
+ public InterpreterValue get(int variableId) {
+ checkVariable(variableId);
+ return stackFrame.get(variableId).get();
}
public void put(int variableId, InterpreterValue value) {
diff --git a/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterTests.java b/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterTests.java
index 66a1f8b..58a1b6b 100644
--- a/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterTests.java
@@ -117,6 +117,19 @@ public class InterpreterTests {
assertEquals(new VariableNotInScope(42), exception);
}
+
+ @Test
+ public void errorIfTryingToGetValueOfVariableNotInScope() {
+ val localVariableDeclaration = localVariableDeclaration(
+ 42, StringValue.REF, "x", literal(""));
+ val method = staticMethod("hello")
+ .statement(new ReturnNode(reference(localVariableDeclaration)));
+
+ val exception = assertThrows(VariableNotInScope.class,
+ () -> runMethod(method));
+
+ assertEquals(new VariableNotInScope(42), exception);
+ }
private InterpreterValue runMethod(MethodNode.MethodNodeBuilder methodBuilder, InterpreterValue... arguments) {
val method = methodBuilder.build();