Tip:
Highlight text to annotate it
X
We just saw how when evaluating statements like a return statement
we could throw an exception.
Now we're in a great position to code up function calls,
which we'll have to set up a new environment and catch the return values.
Once again we extract this statement type from our abstract syntax tree,
and now we want to handle function calls.
There are 2 parts to a function call abstract syntax tree:
the name of the function, like absolute value or myfun or square root,
and then the arguments.
And because the function may have 1, 2, 3, or more arguments,
we just have a list of expressions.
Function name may mean different things in different contexts,
just like any other variable,
so we'll go look it up in the current environment.
We're going to have to decide what it means for something to be a function.
For something like an integer or a string, we can just use a Python integer or string.
For a function, we're going to use a tuple where the first part is function
so that we know one when we see it,
then there's a list of formal parameters, then there's the body,
and then there's the environment, and we'll see how that comes into play later.
For now I'm just going to promise that this is how functions will turn out,
and later, in the next step, we'll make that promise true
by having function definitions produce these 4 tuple values.
So we'll just pull out the function parameters, the function body,
and the function environment.
For example, if we're still calling square root, square root's official formal parameter name
might be x when we passed in the particular actual argument 2.
One of the goals in our interpreter is to rule out bad code.
One easy mistake to make is to pass in the wrong number of arguments,
to have a different number of actual arguments and formal parameters.
We'll just check for that now. They're both lists. Compare their lengths.
Otherwise, we have to make a new environment frame,
which I'll leave for you to do,
and then we want to evaluate the body in that new frame.
We'll have to make a new environment frame and follow all of those steps--
make, potentially, spaces in the new frame for the formal parameters
and assign to them the values of the actual arguments.
So we have quite a bit to do in here.
Then we want to evaluate the body in the new frame,
and we're going to do that with exception handling.
We'll try doing something--you tell me--
and ideally, we will get to the end and somewhere in the middle
we'll have raised that special exception holding the return value.
If that happens, we'll return the return value.
Otherwise, the user wrote code without a return statement; we'll just return None.
All the way up topside they're trying to call something like square root.
Square root better be a function and not some string or some number.
All the way down here we check for that.
If we try to call something that was not a function, we'll just print out an error.
Here's your quiz.
Fill in these 2 pieces of code so that we correctly handle function calls.
It's worth noting that this quiz is pretty tricky.
This is a little more programming than we normally ask you to do.