Hello and welcome, everybody.
And in this lesson we're going to start creating if statements.
So I just went ahead to compile a page and we're going to change our structure node so that it can support
statements.
So just in compiler dot hedge on struck node, we're going to scroll down and we're going to create
a new union called Statement inside of that other union inside the node.
Okay.
So here you have the union and we have another union at the bottom called Statement.
Now here we're just going to say struct f statement if statement.
So obviously this will represent the F statement, pretty self explanatory.
It's inside the union of statement because it's a statement.
And you know what?
This can actually be called a struct because I just realized, you know, we are already in the union,
so there's no need to have to do another union.
So just call it a struct struct statement and the name of it is going to be Stmt.
So now if statement, we're now going to create some variables.
So we're going to start with the condition node.
So this is like F condition, right?
You see what I mean?
So that's the condition in between the F statement.
Now what else do F statements have?
They have bodies.
You know, what's between the bracket, a body.
So we're going to say struct node,
body, node.
And obviously this will actually be a body node.
It will link to a node type of body here.
Now we're done, right?
Nope, not quite.
What else do you have?
Statements have.
Well, they can either end here or they can have an L so else f so we're going to say struck node next.
So in a scenario of F condition else, the else will be on the next by here.
So the F condition will represent these.
And then the next we'll point to the else.
Does that make sense?
So the actual else node, which we haven't created yet, but we will create soon.
Okay.
So that's it for the actual structure of the F statement.
So now what we're going to do is we're going to pause it at C and we're just going to say just above
here, but pause, keyword, void, pause, F statement and it's going to accept a history and we're
just going to say expect keyword F.
We haven't created that yet.
So let's head to expect OP, which is there and we're just going to make one underneath expect keyword.
Okay.
And we're going to say token next.
Token equals token next.
And you just do that so you guys can see.
So we're going to say we expect the keyword that's provided.
If there's no next token or the next token type does an equal type keyword or the next token file does
not equal key word, then we're going to say compiler error can process expect in the key word, but
something else was provided.
OC So now we can go back to expect keyword and we can carry on with our parsing of the statement.
So what happens after an if statement?
Well, we have brackets, so we need to parse those next.
So we're going to say expect OP left bracket.
Now we have our condition.
So we're going to say parse expression, double root.
We're going to pass in our history, we're going to say expect same right bracket because remember,
the right bracket is symbol, the left bracket is an operator.
And in our implementation now we're going to pop off the condition.
Note that we paused on line 1265 here.
We're going to go a size t var size.
We're about to parse the body, parse body, ampersand, var size.
And we're going to say history.
Now we pop off the body note.
So that obviously pauses the whatever's in between these brackets in these brackets here.
Okay.
And now we're going to say make if node and we don't have that function yet, we're going to make it.
It's going to expect a condition node, a body node and the next node.
But the next node will be no for now because we'll make that in another video when we implement else
and else if there will be another video.
So make F node, we need to now create that.
So let's head over to nodes C search for make under scroll so it takes us to the next one.
Okay.
So we got quite a few in here.
So we're going to say void make if Node is going to accept condition node, a body node and a next node.
So we're just going to say no, create ampersand struct node.
So type equals node, type F, statement F and then we're just going to say SMT Stmt die if statement
so condition node equals condition node dot stmt if statement to body node equals body node die step
TMT if statement next equals next node.
And that's just going to be it.
And that should create our if if nodes just copy and paste that.
Now we're going to put in the header file.
So we're going to just check it under make function node.
You know, you can use control F control V control C to really speed up the development process.
Guys, when you get lots and lots of lines it's so much easier to just do control f search next, next,
next until you find what you're looking for because as soon as you start getting over 1000 lines, it
starts to get complicated.
So that's my tip, guys.
You know why?
Why am I searching?
Because it takes me straight there and it's done.
There we go.
We're back where we were, nice and easy, so we've now completed our positive statement function.
We now need to call it.
So that's what we're going to do now.
So just in our pass keyword down here, we're going to say if x equals token TSS fail f parse F statement,
we're going to pass the history in there to and then we're just going to return nice and easy.
Okay.
So we're now going to do is we're going to say clear, make, clean, make.
And we can see there's an error on 1273.
And that's a very simple error.
Guys, I did condition body.
My mistake should be condition node.
Right.
I just did it again.
Condition node.
There we go.
Try that again.
Make clean.
Make.
And there we go.
It compiles.
Let's now test it.
So we're going to go here.
We're going to say f one and it X in a Y equals 20.
And now we're going to try that.
So we're going to say main, we can see everything compiled.
Okay, nice.
So just, you know that I like to test things with the debugger just to make sure everything's normal.
So we're going to go gdb main break positive statement and this is for your experience as well guys.
So if things go wrong you can fix things.
So we're going to go next.
Next, next we can see the condition nodes there and the condition node is correct.
We're going to go next pause, body print, the vast, vast size, which is four, which is absolutely
correct because we have an integer in here, a declaration.
So of course it should be four because that body, when we've parsed it, it'll see that variable and
it'll add to the var size.
So we get a nice four.
That's good.
When I go next and I'm finally make F Node and now we're going to go asterisk.
No Pop and we can see down here if statement condition no body node.
So that's perfect and we can see next is no perfect guys.
So rather than give you too much in this one lecture, we're going to end this lecture here.
In the next lecture we are going to implement ls and else f Thanks for watching.