diff options
Diffstat (limited to 'src')
3 files changed, 34 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; +} 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 58a1b6b..46b30d9 100644 --- a/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterTests.java +++ b/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterTests.java @@ -10,6 +10,7 @@ import org.zwobble.couscous.ast.LiteralNode; import org.zwobble.couscous.ast.MethodNode; import org.zwobble.couscous.ast.ReturnNode; import org.zwobble.couscous.interpreter.Interpreter; +import org.zwobble.couscous.interpreter.UnboundVariable; import org.zwobble.couscous.interpreter.VariableNotInScope; import org.zwobble.couscous.interpreter.WrongNumberOfArguments; import org.zwobble.couscous.values.ConcreteType; @@ -130,6 +131,20 @@ public class InterpreterTests { assertEquals(new VariableNotInScope(42), exception); } + + @Test + public void errorIfTryingToGetValueOfUnboundVariable() { + val localVariableDeclaration = localVariableDeclaration( + 42, StringValue.REF, "x", literal("")); + val method = staticMethod("hello") + .statement(new ReturnNode(reference(localVariableDeclaration))) + .statement(localVariableDeclaration); + + val exception = assertThrows(UnboundVariable.class, + () -> runMethod(method)); + + assertEquals(new UnboundVariable(42), exception); + } private InterpreterValue runMethod(MethodNode.MethodNodeBuilder methodBuilder, InterpreterValue... arguments) { val method = methodBuilder.build(); |
