An interpreter is a program used to understand other programs
We will start off with the Calculator language to look at Scheme Syntax (and how to interpret it)
You will be writing an interpreter for the final project (this is a super cool project)
How does it work
Essentially is the code version of what we've studied during week 1!
Evaluate operators/operands, apply operands to operators; this whole process is done through our own interpreter
Pair Class
We will be using the Pair class (very similar to Link, but with a few added bonuses/slight differences)
Has a first and rest attribute
Also has a map method where it applies a function to every argument
Must provide nil, does not default to Link.empty like with our Link class
If p is a Pair containing a proper call expression, we get the operator by doing p.first, and get the operands with p.rest. To get the first operand, we need to do p.rest.first
Pair class
classPair:"""Represents the built-in pair data structure in Scheme."""def__init__(self, first, rest):
self.first = first
ifnot scheme_valid_cdrp(rest):
raise SchemeError("cdr can only be a pair, nil, or a promise but was {}".format(rest))
self.rest = rest
defmap(self, fn):"""Maps fn to every element in a list, returning a new
Pair.
>>> Pair(1, Pair(2, Pair(3, nil))).map(lambda x: x * x)
Pair(1, Pair(4, Pair(9, nil)))
"""assertisinstance(self.rest, Pair) or self.rest is nil, \
"rest element in pair must be another pair or nil"return Pair(fn(self.first), self.rest.map(fn))