Created
November 3, 2019 23:12
-
-
Save chelseatroy/2b4d9dfea640e0ab4ab5634f83f79a34 to your computer and use it in GitHub Desktop.
Interpreter 2: Scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Procedure: | |
def __init__(self, parameters, expressions, env): | |
self.parameters = parameters | |
self.expressions = expressions | |
self.env = env # PROCEDURE MUST REMEMBER THE ENVIRONMENT AT THE TIME THAT IT WAS DEFINED | |
def __call__(self, *args): | |
# Args are the argument values | |
# Make a new scope for the local variables | |
local_env = {} | |
# Bind the parameter names to values | |
for name, value in zip(self.parameters, args): | |
local_env[name] = value | |
for expression in self.expressions: | |
result = seval(expression, {**self.env, **local_env}) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment