Hello and welcome to part three of implementing the scope offsets.
In this part, we're going to implement structure scope offsets.
So let's start by making a new function inside your parser scope offset for structure struct node struct
history.
Okay.
So this function will get called in the event that we are parsing a structure and we happen to parse
a structure body.
So the first thing we're going to do is we're going to say struct parser, scope, entity.
Equals parse scope last entity and we haven't yet implemented that function.
So we're going to do that and we'll name that as the last entity.
So let's just scroll up.
Just under par scope finish.
We're going to say struct positive scope entity, parse scope last entity and we're just going to return
scope, last entity and we're going to pass in the current process.
So it's as simple as that.
And there's our scope entities that we made a while ago,
a scroll back down to where we were.
Okay, so you'll notice we actually did an integer here.
This should be void and now we're just going to continue.
So we're going to say F last entity.
And just appear, we're going to say in ad offset equals zero.
And then here we're going to say offset plus equals last entity stack offset.
And then we're just going to say plus less entity node, var type size.
Just like that.
Now we're just going to say, if variable node is primitive, we're going to pass in our node, then
node var dot padding equals padding offset node var type dot size.
So we're applying padding if it's a primitive variable.
I'll explain all of this to you.
When we finish writing it just by here, we're going to say no.
Var dot a offset equals offset plus no var padding.
So it just gives us the absolute offset and that is it for that actual function.
So let me explain this to you then.
Later on in the course, when we're looping through a structure body, we'll be calling parse a scope
offset on every variable node that we find.
Okay?
That will then go in here.
And obviously, remember, at this point in time, it's inside of a structure body.
If it gets to this function.
So we're basically inside here right where the X, Y and E is.
So let's imagine we're going through the structure body for the first time.
So Integer X gets passed to parse a scope offset which will then call this function.
We haven't done that bit yet.
It will go and pick off the last entity inside the structure scope that will return null so nothing
will happen for instance X anyway, so it'll go through the next element in the body and parse the scope
offset to be called French.
It'll go in here.
It'll peak on the last entity in the structure scope and we get an integer X last entity is now true.
So what we do is we say offset plus equals the last entity stack offset plus the last entity size.
And then if it's a primitive node will apply padding.
And then finally we set the absolute offset by doing offset plus node var padding.
So that's how it works.
Okay.
So as you know, the stack offset is set with the parse a new scope entity function.
Right it by that.
Yeah.
Okay.
We're not yet using this function, but we will be using it soon.
And that is where the actual original stack offset will be set.
So when we do a last entity stack offset plus the variable size, that stack offset will be set by calling
that parse a new scope entity function, which we're not yet doing.
So I hope that makes sense.
Compiler development is not easy, so if you're confused, it's understandable things will come together
soon enough and they'll start to make more sense.
So just stick with it.
And yeah, just know that this stack offset variable here, we set that somewhere else later on when
we go through the course.
Okay.
So just by here, we're going to say if history flags, ampersand, history, flag, inside structure
and we haven't created that yet.
So we're going to create that in a minute.
If that's true, we're going to say pause of scope, offset for structure.
We're going to pass the node and the history and we're going to return now.
So obviously, the caller has a scope offset.
If it knows that it's inside a structure's body parsing, then obviously this flag will be set.
It'll set this flag so that parses KB Offset can see that and know it's a structure.
Let's just head over to parse the SCO offset for global.
This should actually be avoid.
That was my mistake.
Make that a void and get rid of the return zero so we won't do anything for parsing global offsets because
as I've already said, we can't really have global offsets.
So let's just head over to history.
Flag is global scope and we're going to add the structure in there.
And it's inside structure.
It was called.
Copy and paste that.
Set the one there and put another zero.
Perfect.
Okay.
Make sure these numbers are never duplicated.
All right, guys.
And if you ever do get lost or something doesn't work as as shown on the course, just do it.
Get def.
All right.
I have source code attached to every programming lecture just to get def on the repository, and you'll
see exactly what you did wrong.
And obviously, if you have any questions, you can just ask me in the Q&A.
So we finished implementing positive scope offsets for now.
Later on, when we do actually create functions, we will come back and implement what's online 6 to
1.
But for now, the scope offsets are complete.
So thanks for watching this lecture.
Stay tuned for the next one.