Hello and welcome to part two of implementing the bodies.
In this lecture we are going to implement parser append size for node.
Now it's been a while since we've made this placeholder, so I'm just going to tell you what this function
does again.
So essentially when we go to parse a single statement or several statements, we want to pass back the
summed variable size so that the body knows the size in bytes of itself.
Okay, so what this function is going to do, it's going to essentially adjust the variable size based
on the node you've provided it.
So it's going to add to it.
So let's begin.
We're going to get rid of this compiler warning and we're going to start writing this function.
So firstly, if they provide a null node to us, we're just going to return.
We can't deal with no nodes now.
We only care about variable nodes and variable less nodes.
So we're also going to check for that.
Anything else we don't care about.
Okay.
So the main logic is going to be in here and in here.
Okay.
So before we start filling in this logic, I just want to head over to Node C because we need to create
a special function.
So we're just going to say bool node is struct or union variable.
Okay.
Now this function name is pretty self-explanatory.
It's the node.
They provided a structure or union variable.
So the first thing we do, we say is the node type A variable type, node type variable, right?
Because if it does not equal a node type variable, then obviously we're going to return false because
there's no way it can be a struct or union variable if the node itself is not even a variable node.
Okay.
If the node does have a type of node type variable, then we have a data type we can access.
So we're just going to say return data type is struct or union ampersand node, var dot type.
So yeah, essentially they passing a variable node and we're going to basically access the type and
say is the data type of the variable type of structure or union type.
And we already implemented that.
That just checks to see if the type is a data type struct or a data type union.
Okay.
Perfect.
Let's copy and paste this prototype and we're going to put it in compilers.
I'm just going to put it here.
I like to keep them organized.
Perfect.
Let's head back to paths dot C and now we can carry on implementing our parser.
So we're just going to say if node is struct or union variable node, then we need to treat this specially.
So what we do is we say parser append size for node struct union.
We haven't yet created this function, but it's going to accept a history, a variable size and a node.
Now just above parse or append size for node, we're going to create this function.
So we're going to say parser, append size for the node struct union.
It's going to accept a history, a variable size and the node perfect.
And before we start employing this function, just do a return keyword after line 670 here parser pen
size for node because once we've already appended it, we're done, right?
There's no need to run any code further down here.
We're finished.
So I like to put a nice return keyword to be ready to be very clear that we're finished doing whatever
we want planned to do because we basically transfer control to this function to do the job for us.
Okay.
So the first thing we're going to do is we're going to say asterisk, variable size plus equals variable
size node.
So what we're doing is we're adjusting the variable size variable, the actual number in this in this
pointer, we're adjusting it by the size of this note that was provided to us.
And obviously we use the asterisk here to step into it and change the actual number rather than the
actual point of value.
Now we need to check if we're pointer.
So no var type flags.
Ampersand is pointer day type flag is pointer.
So if it's a pointer, we're just going to return OC.
And the reason for that is because we're about to do some logic down here that doesn't apply to structure
union pointers.
So in C, structures need to be aligned to their largest data type.
So we kind of need to do that here.
So we're going to say struct node largest var node equals variable struct or union body node.
And we have an implemented this function yet we will and we're just going to pass in the node here and
that's going to access the body largest var node variable.
So we now need to implement this function.
And again, it's very self explanatory what this function does, it returns the structure or union's
body node, right?
Very simple to understand what that means.
So we're going to head over to help with that C and we're going to create that function in there.
So just by here, we're going to say struck node, variable struct or union body node that's going to
accept a node.
So this function will basically return the body node of the structure or union provided to it.
So the first thing we need to say is, is if node is struct or union variable and then we pass in the
note and just put a exclamation mark because we're going to note that.
So if it's not, then we're just going to return null because how can we extract a body node from something
that's not a structure or union variable?
Okay, so now by here, we're just going to say if node var dot type dot type equals data, type struct,
then we know we're dealing with a structure.
And if we're dealing with a structure, then we need to go return node, var dot type dot struck node
dot underscore struct dot body nw and that's not a dot that's pointer access.
Yeah.
Cool.
So yeah, that makes sense.
That returns the structure node body.
Yeah.
So here we would usually return the union body but as we have not yet created unions, we can't actually
return anything.
Right.
So we're just going to put a little printf error for ourself.
There are no union nodes yet implemented.
We're just going to use the printf because we can't call compiler because we don't have a current process
to pass to it in this file.
So printf will suffice for now and we'll even just do like an exit or something, right?
Just in case we end up accidentally forgetting to do this, you know, we can even put a warning there
to.
Member to implement unions.
So yeah, that function will return the structure body node one called.
It's also supposed to return the union body.
No, but as I just said, we haven't yet implemented those properly yet.
So let's copy and paste this prototype and we're going to put it in compiler h, just paste it there.
I head over to paths dot C and we can continue implementing this function.
Okay.
Now before we continue writing this function, there's a slight mistake here.
We said paths or pen size for node struct union, but here we're missing the four.
So just after size, just put four by here paths append size four node struct union perfect.
So obviously variable struct or union body.
No, we'll get the body node for the given variable node.
We passed it which will basically resolve to a structure or union body and then we can access that body
to grab the largest variable node of that structure or union.
If there is a largest variable node, it's perfectly possible to have structures with no variables.
Right?
So we do need to check.
Then we're going to go asterisk, variable size plus equals aligned value, asterisk variable size,
largest var node farther type size.
So that will essentially align the values, the given value to give us the total bytes we need to pad
to finish computing that variable size.
Because as I said, the structures and unions, they need to be aligned to the largest variable in in
the in their structure or union.
Right.
Okay.
So that completes that that function for handling structures and unions.
Let's scroll down back to parse append size for node.
So for a normal primitive variable, we just go ask is variable size plus equals variable size and then
we just pass in the node and obviously variable size function will basically return the de type size
for us, nice and simple.
So now we're going to create the paths of a variable list.
So just above parse, append size for now we're going to say void parser, append size for variable
list and that's going to accept a history, a variable size and also a vector.
And this vector will be the actual variable list.
So we're going to say vector set, peak pointer vector to index zero.
We're going to say struct node node equals vector peak pointer VEC.
Now that's not forget to actually name the history variable as well guys for the history there while
node node equals vector pointer vec.
So iterating through the entire variable list vector.
So what do we do here?
It's very simple.
Parse the append size for node.
We'll pass in the history.
The variable size is recursive.
Right?
I'm going to pass in the node and now we are going to have to copy and paste this prototype above here
just to give us access to call that.
Right, because it's below us or you can put it in the header file, whichever you prefer.
Personally, I wouldn't put it in the header file because this function should only really be used in
parsing.
I'd see some people like to put all the prototypes at the top of the source file.
You could do that if you want.
So now the final thing to do is to call that function.
So down to no type variable list, we're going to say parse, append size for variable list.
We're going to pass in the history, the variable size.
And now finally we're going to say node, var list, dot list.
We're just going to pass in that vector list, nice and simple.
Okay, now that should be it.
Let's just check this.
So yeah we can see in body single statement we call pause or pen size for note.
Yep.
Everything looks fine.
Let's give that a compile and make clean the make.
And let's just take a read of this.
Make sure everything's okay in declaration of paths.
Keyword.
And yeah, that's okay because we haven't yet finished implementing this function.
So that's why it says implicit declaration, right?
We'll get round to that when we come back to this later on.
We're just focusing on doing the bodies at the moment and then the structures.
So yeah, congratulations.
You've now implemented the ability to append the variable size for a body.
That concludes part two of implementing the bodies.
Thank you for watching.