Problem 1 (100pts): Frame
Implement the define
and lookup
methods of the Frame class, in scheme_classes.py
. Each Frame
object has the following instance attributes:
bindings
is a dictionary representing the bindings in the frame. It maps Scheme symbols (represented as Python strings) to Scheme values.parent
is the parentFrame
instance. The parent of the Global Frame isNone
.
define
takes a symbol (represented by a Python string) and a value. It binds the symbol to the value in theFrame
instance.lookup
takes a symbol and returns the value bound to that symbol in the first frame of the environment in which the symbol is bound. The environment for aFrame
instance consists of that frame, its parent frame, and all its ancestor frames, including the Global Frame.
- If the symbol is bound in the current frame, return its value.
- If the symbol is not bound in the current frame, and the frame has a parent frame, continue lookup in the parent frame.
- If the symbol is not found in the current frame and there is no parent frame, raise a
SchemeError
.
Use Ok to unlock and test your code:
python ok -q 01 -u
python ok -q 01
After you complete this problem, you can start your Scheme interpreter (with python scheme.py
). You should be able to look up built-in procedure names:
scm> +
#[+]
scm> odd?
#[odd?]
However, your Scheme interpreter will still not be able to call these procedures. Let's fix that.
Remember, at this point you can only exit the interpreter by pressing Ctrl-d
.