Okay, everybody, in this lecture, we're going to implement switch cases.
We probably should have done it earlier, but we went to do Continuum Break instead and carried on,
but we finally come back to it.
So we're going to say struct case, statement, underscore case and that will take an expression node
and we'll just call it expression.
So that's in our compiler sub substructure, that's called statement in the node structure.
So just smack that there and that will represent our cases.
Head over to Node C and we're going to say void make case node struck node node, call expression node
and we're going to say no, create ampersand struct node dot type equals node type statement case.
We're going to say dot statement dot case dot expression equals expression node.
So that's for creating the case nodes, copy and paste that.
And we're going to put that in our header file just like we did with the others make case node.
So just to let you guys know, of course, what is the case node?
Well, it's like the case is in the switch, right?
So that's what we're making here.
So go to Parse Switch.
And just above there, we're going to say void, parse case.
So what's the case is have they have a keyword called case?
So we do expect keyword case.
We're going to say parse expression, double route history.
We're going to say struct node case expression node equals no pop because that's the expression we just
parsed.
So for example, case five, so five will be the expression, right, that we've just parsed here and
now we expect a colon because case five colon right now we're going to make the case node.
Now at this time, we're only going to support number cases.
So if it doesn't equal a number, then we won't deal with it.
So case expression, no type does not equal no type number.
Then we're going to say compiler error come process.
We only support numbers in our subset of C at this time.
This will not really be a problem because things like definitions will be completely supported.
So you'll still be able to do things like Case Apple.
So it shouldn't be any issue at all.
So now we're going to say struct node case node equals node, pop parser register case and we're going
to pass in the history and our case node.
OC So pause the register case will essentially register the case to the structure, to the switch,
I should say.
Register the case to the switch.
So now we're going to go back to PA's keyword and then scroll down LCF as equal token SE file case.
Parse case.
But we're going to pass in the history and return.
Okay.
So let's check that that works successfully.
So we're going to do a make clean make.
Try the main.
Everything's fine.
Let's now add a case.
So we're going to say switch x case one break.
So let's see if that works.
So we're going to say main.
And just go to that semicolon for the switch there, guys.
And there we go.
Everything is fine.
Perfect.
So we now have switch cases.
Congratulations.