Hello and welcome, everybody.
And in this lecture we're going to implement four loops in the parser.
So let's head over to Compiler H and we're going to search for Underscore Stmt.
And just under our F statement, we're going to say struct.
False statements.
Okay.
Now what is a false statement?
Have four inch I, I blow 30 plus plus.
It has an initialize, it has a condition and it has a loop and a body so struck node and it node struck
node condition node struck node loop node struck node body node.
So that is our false statement.
That's how it will look in memory.
Okay, let's head over to parse the dot C and just above parse return.
We're going to say void parse for
and above that we're going to say void parse for loop part.
We're going to accept a history there and we're going to call this parse for statements.
So we're going to say if token next is symbol and we're going to look for a semicolon, then we have
nothing here, i.e. for semicolon.
So essentially we'll call this past for loop part when we're parsing different parts of the actual for
loop.
So as you can see, if we have a semicolon, then this part of the for loop doesn't exist.
So in this example here with this comment, we we use the initialize of the for loop as the example.
So in this scenario, we ignore the semicolon.
And we just do token next return and this should actually return a billion.
So we're going to say return false.
And then down here, we're going to say return true.
So if it does exist, then we're going to say parse expression aboard route and we're going to pass
in our history.
And then, of course, we return.
True.
So now back in the past, false statement, we're going to say struct node and IT nodes equals null.
Struck nodes.
Condition node equals null struck node loop node equals null struck node body node equals no.
We just format that document as well.
So now we're going to say f parse for loop part.
We passed in our history so we're parsing the initialize now.
Then init node equals node pop.
But just above this, we need to actually parse the for keyword and the bracket.
So we're going to say expect keyword for expect operator left bracket.
So that will pause off the for and the left bracket.
And then we have our initialize that we parse there.
And then if we could find an initialize, then we pop that off to set the init node for whatever was
parsed here with parse expression by root online 1222.
So now we're going to do the same for the condition parse parse for loop part history.
And we're going to say condition node equals node pop.
Okay.
And just scroll up a little bit.
We actually also need to pop off the semicolon as well, because if somebody did something like four
in it equals zero, I blow three.
You can see we have a semicolon here that we need to deal with in the initialize, so we also expect
it.
All right, perfect.
So about parse the loop part, but the loop part is dealt with differently.
The actual loop part of the for loop.
So we need to handle that differently.
So we're going to have to create a new function.
So we're going to say ball parse for loop part.
Loop.
So a parsing in the actual loop part of the for loop.
And we're going to say if the token next is symbol right bracket,
then we've obviously finished off for loop.
So we'll just return false.
Otherwise, we're going to parse the expression of a root
and return.
True.
So just scroll down and we're going to say if parse for loop part loop history, then loop node equals
node pop.
And then of course, we have to expect the symbol right back it because we've ended the for loop.
So it's going to be like something like this at this point in time.
Right.
So you can see we've parsed the the in it by here.
Parse the condition by here.
Parse the loop by here.
And then, of course, we're left with that bracket that we pop off here.
Okay.
Sounds good.
Okay.
So now we need to parse the actual body.
So we're going to say parse body, ampersand, variable size.
And we're going to pass in the history and we're going to say body node equals no pop.
Now, finally, we need to call make for node, which doesn't exist yet.
So let's go to nodes.
See.
And we're going to say void make for node struct node and it node struck node condition node struck
node loop node struck node body node.
So then we're just going to say no, create ampersand struct node
dot type equals node type statement for dot statement dot false statement dot init node equals init
node.
We're just going to do the same for the condition node and we're going to do the same for the loop.
And finally, we're going to do the same for the body.
That's perfect.
So we're going to copy and paste this in the compiler H and just above make the turn node will be fine.
And then we go back to paths dot C we're going to say make for node and it node.
Condition node.
Loop node.
Body node.
Nice and simple.
Okay.
So that's it for our for loop functionality by the looks of it.
We're now going to need to call it.
Of course.
So just under here, we're going to say in our pass keyword, we're going to say LCF as equal token
SE file for pause for statement.
And guys, if you want to do this cleaner, I mean, I do advise wait until the end of the course.
But I like to keep it simple because it's training exercise.
Right.
But if you really want to be clever about this, you can have like a lookup table of some kind and then
you all need to use all these if statements.
Right.
But we don't need to do that here because I want to keep the whole thing simple for you to keep the
training simple.
But obviously we're going to end quite a long list of ll save statements here.
And in scenarios like this, you know, some sort of table of some kind or we can like resolve the correct
routine works perfectly for these types of for these types of examples.
Or another way to do it in a cleaner way is to essentially have these as integers.
Right?
So we'll call a function which will basically take in the string, return F for whatever, and it'll
basically return the integer, the enumeration value of what that is.
Okay.
Like we did with token type string, token type number, it could be something like statement type,
return statement type F and then you're able to use a switch.
But yeah, I'm just saying you guys know that in case you want to do that when the course ends, we're
not going to do that here because I want to keep things simple for you and easy to understand.
So we're not going to worry too much about abstractions here in this regard.
Okay, guys, there's one more thing we need to do, which is go to parse for statement, parse for
loop pop.
You'll see down here, we do expect OP semicolon.
That's completely wrong because because that's a symbol.
So we need to expect SIM.
And also this takes a char, so make sure you get rid of those double quotes we'll put up.
We're passing in a char here.
So we now go to test dot C and finally test this.
So we're just going to go in.
It equals zero for I equals zero below 30 I plus equals one don't do plus plus because we haven't implemented
plus plus yet.
Right.
So you'll have problems if you do a plus plus.
So we're going to do we're going to do a make clean and make everything compiles.
I'm just going to do main.
We can see everything compiled.
Fine.
So that's perfect.
We've now implemented for loops.
Okay.
If we get rid of this semicolon, we run it, can see it to text the error error expectancy, double
semicolon and nothing was provided.
So there we go.
For loops are implemented.