aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-04 17:31:54 +0100
committerMichael Williamson <mike@zwobble.org>2015-10-04 17:31:54 +0100
commit8d2374ba254e637c22e6f1eaa58ccb54d814529f (patch)
treef7971106fe112e163f2af2fcc489c062c4f82ef3 /src/main/java
parente8c8fe86c757bd1208427c0ad4f8134f1b1bff1f (diff)
Throw exception if variable is unbound
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/Environment.java11
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/UnboundVariable.java12
2 files changed, 19 insertions, 4 deletions
diff --git a/src/main/java/org/zwobble/couscous/interpreter/Environment.java b/src/main/java/org/zwobble/couscous/interpreter/Environment.java
index d748654..a24bf1c 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/Environment.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/Environment.java
@@ -9,6 +9,8 @@ import org.zwobble.couscous.ast.VariableNode;
import org.zwobble.couscous.values.ConcreteType;
import org.zwobble.couscous.values.InterpreterValue;
+import lombok.val;
+
public class Environment {
private final Map<Integer, Optional<InterpreterValue>> stackFrame;
private final Project project;
@@ -19,12 +21,13 @@ public class Environment {
}
public InterpreterValue get(int variableId) {
- checkVariable(variableId);
- return stackFrame.get(variableId).get();
+ checkVariableIsInScope(variableId);
+ val value = stackFrame.get(variableId);
+ return value.orElseThrow(() -> new UnboundVariable(variableId));
}
public void put(int variableId, InterpreterValue value) {
- checkVariable(variableId);
+ checkVariableIsInScope(variableId);
stackFrame.put(variableId, Optional.of(value));
}
@@ -41,7 +44,7 @@ public class Environment {
return new Environment(project, stackFrame);
}
- private void checkVariable(int variableId) {
+ private void checkVariableIsInScope(int variableId) {
if (!stackFrame.containsKey(variableId)) {
throw new VariableNotInScope(variableId);
}
diff --git a/src/main/java/org/zwobble/couscous/interpreter/UnboundVariable.java b/src/main/java/org/zwobble/couscous/interpreter/UnboundVariable.java
new file mode 100644
index 0000000..c4aebfb
--- /dev/null
+++ b/src/main/java/org/zwobble/couscous/interpreter/UnboundVariable.java
@@ -0,0 +1,12 @@
+package org.zwobble.couscous.interpreter;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+
+@Value
+@EqualsAndHashCode(callSuper=false)
+public class UnboundVariable extends InterpreterException {
+ private static final long serialVersionUID = 1L;
+
+ int variableId;
+}