Hello and welcome, everybody.
In this lecture we are going to implement the return keyword.
So, you know, like return null and all that sort of thing.
We're going to actually implement the return keyword in this lecture, so we're going to pause it.
So I had to compile that h search for the statement and just above f statement we're going to say struct
return statement.
And what does a return statement have?
Return 50 return expression.
So it has one node called expression,
the expression of the return.
So we're going to head over to parse the C and we're going to go to parse keyword.
And just by here we're going to say f s equals token se file return and we're going to go l cif for
the actual f statement there.
And if this is true, we're going to say parse return history and we're just going to return.
So by here we're going to say void, parse, return, struct, history, history.
And obviously we're going to expect the keyword return.
That's the very first thing.
And now we're going to essentially parse the expression.
So we're going to say parse expression, root.
We're passing the history there and we're going to say expression node equals no pop.
And then we're going to say make return node, expression node.
And that's it for a basic return.
You know, that's not complete, mind you.
So let's go to Node C and we're going to create the make return node.
So we're going to say void make return node struct node root expression node no create struck.
Node dot type equals node type return.
Dot statement dot return statement.
Dot Expression equals expression node.
Nice and simple.
Those ones always are copy and paste that into the prototype.
So we're just going to put it above the F node.
There you go.
Nice and easy.
So back to positive C, we need to improve this because people can do return semicolon and then what
do you do?
This will fail because we assume there's a value.
So we need to just change this code a little bit as well.
So now that we've expected the keyword return, we're going to say F token.
Next is symbol semicolon.
So, you know, how have they done something like this, like you would in a void method, void function,
because if they have, then that's valid, right?
So if that's the case, we're going to say expect some semicolon.
We're going to say make return node null.
We're just going to return here
for returns with no expressions, expression level return, i.e. return 50.
So in this scenario where at the 50 now because we've parsed returns, so we parse expression for root,
which is 50 pop off the expression, we make the return node.
And now the final thing to do is to expect a semicolon.
And yeah, that's literally it.
So that's our paths return nice and simple, right?
So we're going to make clean that and then belt and now we're going to go to test start C it's going
to say return 56 going to go main.
Everything's fine.
Let's try.
Just return with a semicolon.
Everything's fine.
Perfect.
So we've now successfully implemented the return statement.