Hello and welcome.
And in this lecture we're going to implement the parsing of unions.
So the first thing we're going to do, we're going to say void, parse, union, no scope.
That's going to take a data type and boolean that says A's forward declaration.
We're going to create some variables body note, body variable size,
and we're going to say if it's not a forward declaration, pause the body
and the history will be history.
Begin history, flag inside union.
Then we're going to say body node equals no pop here.
We're then going to call make union node, which we haven't created yet.
So let's head over to node dot C before we do head over to compile h search for underscore struct.
And we're basically just going to copy and paste struct and change it to union.
So now we can head back to node c.
Search for make struct node and I copy and paste it when changed to make a union note.
Change this to make type union.
Change this.
To underscore union.
Place it in.
And there we go.
There should be OC.
Copy and paste this into.
Compile.
Under make stroke note.
Just like that.
And now we can go back to positive.
See and finish.
Make a union note.
So make union note.
It's going to ask for a child name.
So we're going to say D type type string and then just passing the body note.
We're now going to pop that off.
If there's a body note, we're going to change the size to equal body note, body size.
Now we're going to check to see if we have an identifier because we are declaring a union here.
So there might be a name F token peak.
Next type is token type identifier struct token LAA name equals token.
Next
union node flags or equals node flag has variable combined make variable node and register history begin
zero d type var name and we'll set that to know union nodes dot union
dot var equals node pop.
And then we're going to say expect sem semicolon and then we're going to push the union node to the
stack.
So basically if someone does this right and they create a union such as hello, that would have been
parsed before we call this function, then we call this which parses the body.
Okay, now if there's an identifier such as here.
Hi then.
Hi will be handled here which will then make a variable node and register it.
Because we've not only declared a union, we've actually created a union variable.
So that's what's happening.
So now we're going to say voi parse union struct data type, de type, and that's going to call parse
union no scope.
So it's actually going to be the exact same thing as this.
So copy and paste it and instead of parse struct, no new scope, we do parse union, no scope.
Okay, nice and simple.
So now we're going to search for parse, struct or union and we can see here we have a space missing
for data type union.
We're going to see parse union.
We're going to pass in the D type.
Okay.
We're going to do a make clean and a make.
See everything works fine but we can see here and helper our little warning remember to implement unions
so go to help her line 38 can see here that we need to handle unions so we're going to get rid of this
and we're going to return null
and then we're going to say if note file the type two type equals data type union.
So Nevada type union node dot union dot body n.
So now we're going to do a make clean and make.
You can see that compiles fine.
We're going to go to test at sea and we're just going to try that.
So we're going to say Union A, B, C and it x.
If that compiles without problems to make, we can see union types are currently not supported.
So we're going to search for that.
So we can fix that.
So we can see data type, expect union.
So we're going to need to fix this.
So it's going to be a nice copy and paste of the of the struct change that to data type union.
Change this to size of union.
Change this for union node for name.
And we need to create those functions now.
So just use control left click on struct node for name so it'll take us to where that is handled call
it's handled here.
So we're just going to copy and paste it
and this will be called Union Node for name.
I change this to node type union.
So we're going to copy and paste the prototype and we're going to put it in compiler h, search for
struct node for name, just paste it underneath.
Now we're going to go to parse dot C and we're going to control left click on size of struct and to
copy and paste size of struct which is in the paths of that C file size of union.
I am using Visual Studio code.
I recommend the same.
I'm going to change this to union name.
And we're just going to check here.
No type union and change this to underscore union a should be it.
So let's do a make clean and I make.
To a maine can still see unions are not supported.
And I can actually see that there's still some leftover stuff.
So by here in no sea we don't handle it.
So go to Node Sort C because if you search for the strings, you'll be able to find what the problem
is.
Right?
So here I've even left a comment on what it should look like.
So a know that c we should change to that and in sum resolve a dot c.
We need to copy and paste this for the bill for union node and change that to a union.
So we're going to make clean again and make.
So let's just go back to test start.
See, I'm going to get rid of this dog point a thing I did earlier because there's no structure of dog
to a main and we can see everything works fine.
So we now have unions and we should be able to give them names as well.
It should work to a main.
And there we go.
So unions now implement it.
Congratulations.