Tip:
Highlight text to annotate it
X
So once again, as a quiz, we're going to build
a lot of this interpreter, together.
I will get started; you'll complete the rest.
We're going to write an evalexp procedure
to interpret JavaScript arithmetic.
We're only going to handle (+), (-), and numbers for now.
Our procedure walks over a (tree) so it takes a single (tree) as argument.
Note that this is a little different from our HTML interpreter,
which took multiple (tree)s.
Remember that our (tree)s typically look like these.
It's either a tuple "number", followed by something like "5"
or maybe something like "binop", for binary operator,
with a leftchild (tree),
a rightchild (tree), and something like (+) in the middle.
This first part, here, is the nodetype.
We're going to extract it, by getting the zeroth element of the (tree).
If it's a "number", all I have to do is
figure out the integer value of this string.
Python allows me to do that, just by calling (int) as if it were a function--
to turn this string into an integer.
Otherwise, for us, the node might be some binary operator.
So we'll just pull out the leftchild,
the operator, and the rightchild
from the parts of the tuple that you already put there for us
when you did all the hard work of making the parser.
So here's what I want you to do for the quiz:
You'll need to figure out the value of the left and the rightchild--
and the big hint here is recursive function call.
And then once you have those values,
you should actually do the work.
If this is a (+) binary operator, add them together.