Skip to content

Commit

Permalink
Fix access to variables from outer clauses
Browse files Browse the repository at this point in the history
This fixes issue #4.
  • Loading branch information
rbaltrusch committed Apr 10, 2022
1 parent 9969e21 commit 1e4ea23
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions interpreter/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@ def stack_append(self, value: Value) -> None:

def check_variable(self, name: str) -> bool:
"""Returns True if the passed name is in the variables list"""
return name in self._variables[-1]
return any(name in variables for variables in self._variables)

def get_variable(self, name: str) -> Variable:
"""Returns a Variable if it can be looked up by name.
Raises an UndefinedVariableException if variable cannot be found.
"""

try:
value = self._variables[-1][name]
except KeyError:
#pylint: disable=raise-missing-from
raise exceptions.UndefinedVariableException(name)
return value
for variables in self._variables[::-1]:
value = variables.get(name)
if value is not None:
return value
raise exceptions.UndefinedVariableException(name)

def set_variable(self, name: str, value: Any) -> None:
"""Sets the value of the variable identified by name to the specified value."""
Expand Down

0 comments on commit 1e4ea23

Please sign in to comment.