Hello and welcome, everybody.
And in this lecture we're going to implement switch statements.
Let's head over to Compiler H and we're going to search for our statement and we're just going to scroll
up.
Just above the strike.
Note we're going to make a new structure struct parsed switch case.
So this is a case that we've parsed in the system and all it's going to take is an index index of the
parsed case and we'll use this later.
Don't worry about it too much right now.
Scroll back down to our statements.
And we're going to create our switch statement.
So all I want you to do, I want you to say struct switch statement, switch statement.
Now the switch statements are more complicated than our other statements, so you can expect a bit more
work in this lecture.
So firstly, we'll just switch statements.
Have they have an expression because we're doing something like this, right?
Switch one
case to case three.
Case one.
So let's continue creating the switch statement here.
We're going to say struct no body struct vector cases.
So we're going have a vector of cases and then we're just going to have a boolean has default case.
So, you know, the default keyword and switch statements, which basically means if it's not any of
these cases, run this, it's basically an else.
It's basically like an LS to an F statement, right?
So that'll be true if a default case is present in our switch statement.
Okay.
So yeah, you're probably a bit confused by this vector of cases.
Simply put, it's a vector of cases.
We'll explain more about that as we write the code in the parser.
Expression for the switch probably makes a lot of sense to you because I just explained it.
The body node of the switch.
Of course, you know what that is and I just explain how as the false case.
So I think we're ready to continue.
And if there is any confusion here, just let me know and I'll try to explain it in a better way.
Okay.
So let's go to our paths that C file and by here we're going to say voi parse switch.
Okay.
So the first thing we're going to do, we're going to say parse keyword, parenthesis, expression,
and we're going to put in switch.
So that'll parse the switch for us.
Now we're going to pop off that switch expression.
Next comes the body.
Now we're going to pause the body.
We're going to pass in the history.
Now we're going to pop off the body.
And now here we will make the switch node, which we haven't done yet, and we're not quite finished
because when we pause the body, we're going to see cases.
We need to handle those cases and there needs to be some tracking of what's going on.
So of course we use our history for that tracking.
So we're going to have to amend our history so it can keep track of the switch state.
Okay, everybody.
So before we can create that switch statement, we need to create some other stuff.
So scroll to the top of this file.
Until we find our history which is here, we're going to start using it now for the first proper time.
And we're just going to say struck pause the history switch.
I'm going to call that underscore switch.
And just by here, we're going to say struct history cases and we're going to have a vector of cases.
And this is going to be a vector of parsed switch case which we defined earlier in the header file.
And we're going to have a Boolean has default case,
is there a default keyword in the switch statement body?
And this history will just allow us to keep track of the state of the switch as we're iterating through
the parsing process.
Right.
As we're parsing more and more tokens and going deeper and deeper in the parser, obviously we need
to keep track of the things that we're finding.
And that's that's the whole purpose of the history.
That's why we've made the history in the first place.
So now we're going to say struct history cases, case data.
Okay, everyone.
So once you've done that, we're going to scroll down and just by here we're going to say struct pause
a history switch pause a new switch statement.
Sandwich is going to mem sat.
Our switch by here.
You just said to zero.
So it's annulled.
Now we're going to say history switch dot case data dot cases equals vector creates sizeof struct parsed
switch case history flags or equals history flag in switch statement.
And we're going to need to create that flag.
So just scroll up.
History flag in switch statement equals we're going to copy and paste that one and just shift it along
like that.
And then we're going to go return history switch.
So that just basically creates a new switch statement instance in the actual history object.
Now we're going to create the parser and switch statement, void parser and switch statement struct
parser history, switch switch history.
And we're just going to do nothing.
It's there for show because when we call this, we're going to want to close it.
And obviously that needs to clean.
We need to know when we're planned to stop using it.
So we just have placeholder function that we will call.
So we're just going to go void parse register, case struct history, history, struc node, case node.
So obviously we'll have case nodes will be parsing things like this, right?
And then they'll be registered through the history switch in here.
So the first thing we need to do, we need to assert that we're actually in a switch statement because
we don't want any bugs or anything.
Right?
Then we say struct parsed switch case, s case and we now need to populate the index of the case in
here.
However, we haven't yet created the ability to parse cases, so we're just going to set it to zero
there.
Put a little warning to implement must be set to the case index because you know in C all cases are
numerous numerical.
Even if you pass like a enumeration or something, they're all they're all numerical at the end of the
day, which is why we use an integer for the index.
So yeah, we'll just set it for zero for now, but it does need to be implemented properly later on.
Okay.
So now we need to push that variable s case to the actual vector.
So we're going to say history switch case data cases and we're going to pass in ampersand SE case and
we're going to right click on form at the document as well.
There you go.
Nice and clean.
So, yeah, whenever we come across the case later on in the course, we will we will call Pause the
register case and we will basically look into that case statement and pull out the the actual case number.
So in case five, for example, yeah, in this case, it will pull out the five and the index will be
five.
For now, zero is fine.
Okay.
So now we're ready to go back to our pause switch and we're just going to say pause a new switch statement
we're going to pass in the history and that returns the struct, parse a history switch.
So we're just going to go pause a history switch.
We'll have to call that underscore switch, guys, equals pause.
The new switch statement.
Perfect.
And then just at the bottom here, we're going to say pause a finish, pause the switch end, pause
the end switch statement.
We're going to pass in the switch, we created the address of it.
So it's a pointer and there you go.
So now what we're going to do is right, we create the switch statement as we just created, which will
populate the flags of the history and create a vector of cases.
Then we pause the the parenthesis expression.
Finally we pause the body.
Now the body has our history object.
We're passing it.
We're passing it to it.
Right?
So when we do come across the case, as we're parsing that body, it will then be able to call the register
case, which will push it to the cases for us to access later in the course.
So yeah, that's what's going on.
So the final thing to do for the actual switch statement we'll implement the case is later file thing
to do is to actually make the node OC.
So that's pop over to nodes, see where we can do that.
So we're going to say void make switch nodes, struct node expression node struck node body node struck
vector cases and bool has default case.
So we're just going to go, no, create struct nodes.
We're going to use the node type statement switch.
Yeah.
We're just going to set all of these.
You guys know the drill.
You've created the make notes a few times now.
Okay.
That looks like it will work.
That's copy and paste the prototype and put it in the header file.
Who's going to paste it in there?
Semicolon.
And now we can say make switch note we're going to pass in the expression note the body note.
And I'll be the switch expression note guys, the cases which will be the switch case data cases, and
then we're going to go again in the switch data.
Case data has a default case.
Okay.
So that should work for parsing the switches.
Obviously, when I parsing the cases right now, we're parsing the switches.
So I hope that does make sense to you.
What we're doing there obviously by calling pause and you switch statement, we registered in the history.
Since the history object is passed to the body, anything that gets parsed in the body will also access
that history object.
So as soon as we find the case, we can relay that information back.
Once we finish pause in the body, we have all the cases we're aware of registered in our history object
and we have whether a default case was used.
So that's that's the thinking here, right?
So yeah, we're going to go to parse keyword now and just put in our switch.
There we go.
That should be it.
Let's do a make.
Clean and a make.
I'm going to run Maine.
That works fine.
Let's go to test.
Let's see.
And we're going to do a switch.
Switch one.
See if that works.
Oops.
And everything works.
Perfect.