Problem 1: Evaluating Names (100 pts)
The first type of PyCombinator expression that we want to evaluate are names. In our program, a name is an instance of the Name
class. Each instance has a string
attribute which is the name of the variable -- e.g. "x"
.
Recall that the value of a name depends on the current environment. In our implementation, an environment is represented by a dictionary that maps variable names (strings) to their values (instances of the Value
class).
The method Name.eval
takes in the current environment as the parameter env
and returns the value bound to the Name
's string
in this environment. Implement it as follows:
- If the name exists in the current environment, look it up and return the value it is bound to.
- If the name does not exist in the current environment, return
None
def eval(self, env):
"""
>>> env = {
... 'a': Number(1),
... 'b': LambdaFunction([], Literal(0), {})
... }
>>> Name('a').eval(env)
Number(1)
>>> Name('b').eval(env)
LambdaFunction([], Literal(0), {})
>>> print(Name('c').eval(env))
None
"""
"*** YOUR CODE HERE ***"
Use Ok to test your code:
python ok -q name_eval
Now that you have implemented the evaluation of names, you can look up names in the global environment like add
and sub
(see the full list of primitive math operators in global_env
at the bottom of expr.py
). You can also try looking up undefined names to see how the NameError
is displayed!
python repl.py
> add
<primitive function add>
Unfortunately, you still cannot call these functions. We'll fix that next!