Hello, everybody.
So in this lecture, we are going to start implementing structures.
And the reason we're going to start implementing structures is because we want to be able to use structure
variables.
And if you try that currently, it will tell you structure and union types are not currently supported.
And that's okay because we first need to be able to parse structures before we can make variables for
them, right?
So we are going to start parsing structures.
They can get complicated because obviously they can have nested variables, but in this lecture we're
going to make a start at parsing them.
Okay, so let's just get rid of this and we're going to create a new structure and it's going to contain
nothing inside.
If we do main on that can see that it doesn't like it.
Okay, that's absolutely fine.
By the end of this lecture, it will be able to recognize structures, even if the variables aren't
being parsed inside the structure until later on in the course.
So the first thing I want you to do is just copy what I did here.
Do struct ABC and your test file and then we're going to go to parse C and look for the parse variable
functional structure union function.
And we're going to check here now for a structure declaration.
And we do that by saying if data type is struct or union and not for name, don't don't do the for name.
Actually, data type is structure union.
That's what the function is going to be called.
And we're going to go ampersand D type.
And then we're just going to go ampersand.
Ampersand.
Token next is symbol and we're going to check for the left bracket like that.
So if both of these situations are true, then we have a structure because the data type we parsed here,
which is struct ABC, it is a structure so that that evaluates to true.
And then the next symbol is one of these left brackets.
So we know that this is basically a structure declaration.
So we're now going to implement these functions.
So just head over to the data type.
See, we're going to start with the data type is struct or union, and that's going to accept the data
type.
And we're just going to return D type type equals data type struct or DE type type equals data type
union.
And it really is as simple as that for that function.
We're going to copy and paste that into the header file.
Just like that.
Okay.
That resolves that.
Now for token, next is symbol.
So scroll up and we're going to look for token.
Next is operator.
We're going to write underneath it.
Static ball token next is symbol.
That's going to accept a char.
We're going to say struct token token equals token peak next and then we're going to say return token
as symbol and we're going to provide the token and see.
So that's very simple.
We can see that that function will work just fine.
Cool.
So we can go back to where we were and we can carry on writing the code.
So some of you might have made the same mistake as me.
There's an empty bracket here that we don't need, cause it should look like that.
So we're going to create a new function.
We're going to call void, parse, struct or union struct data type, D type.
And then just by here, we're going to call that.
So that will basically parse the structure or union that we've detected here.
And we need to also pass in the data type as well.
So now we're going to say switch de type type and we're going to say case data, type, struct, break,
case data type, onion break.
And we're going to have a default and we're going to say compile error code process the provided data
type.
It's not a structural union
and that will be a compiler bug as well.
So we might want to just specify that.
So now here we're going to say void, parse, struct, struct, data type, D type, and we're going
to say in data, type, struct, parse, struct, nice and simple.
So we now need to detect forward declarations because what's to stop someone from doing something like
this?
Okay, they forward declared structure test, but they haven't necessarily defined it like we have ABC.
We need to detect for such a scenario.
So what we're going to say is we're going to say bool is forward.
Declaration equals logical not token as symbol token peak next and then we're going to check for that
bracket.
Okay.
So if this symbol is provided, then is for declaration will be false.
But if it's not provided is false, declaration will be true.
And there we are with our simple line of code.
We be able we're able to detect if something's a for declaration or not.
So now I just want to scroll to the top of this source file.
And just by here we're going to say void, parse of scope, new and we'll let you just going to call
scope new and you'll see the compiler doesn't recognize it.
It's because we haven't put the scoped out C functions in the header file yet.
We'll do that in a moment and we're just going to go current process and then zero by here.
So just head over to scope dot C.
And copy and paste scope new into the header file.
Just so it's recognizable.
And we're going to have to do this for most of the files.
Most of the functions, I mean.
Just paste them all in.
And you know, we can just copy and paste this whole thing in there
and then remove the bodies a bit easier.
Just like that.
There's quite a lot.
So then all of our functions are accessible outside of the source file then, which is what we wanted.
Okay.
Nice.
So since we haven't yet called scope creep root, we need to just quickly do that before we continue
this lecture.
So go back to parsed dot C and we're going to go to our parse function.
And just above current process, we're going to say scope, create root and we're just going to pass
in the process and that's it for that.
So now we can go back to where we were.
So once you're back at your PA's stroke function, we can continue working on this lecture.
So all we're going to say is we're going to say if it's not a forward declaration, then it obviously
has a scope.
And if it has a scope, we need to say parse the scope new.
Okay, then we're going to call a function called parse struct, no new scope.
And when you're going to pass the data, type in there.
And again, we're going to check if it's not a forward declaration, parse the scope finish and now
we need to implement that function.
So what we're going to do is we're going to Visual Studio code, we're going to hold control and left
click on Scope New and it'll take us straight there.
And underneath it we're going to say parse, scope, finish, and we're just going to go scope, finish,
process, simple as that.
We can then search for paths of scope new to take us back to parse struct so we now need to implement
this parse a struck no new scope function and what it does is in the name really it does the same as
parse stroke but it doesn't create any scopes.
Yeah.
So we're going to say void parse struct, no new scope struct data type, D type.
And here is where the actual parse and logic of the structures go.
So why do we need to create new scopes?
Well, because everything in between these brackets is its own static scope, as I explained in the
Scopes Lecture.
So that is the reason why we need to do that.
Okay.
So for now, we're not going to fill in this function.
And that will conclude this lecture here.
We've set up the basic functions for parsing and structures.
We just haven't implemented them yet.
So I think this is a good place to end this lecture.
On the next lecture, we will actually start creating the structure node structures.
Stay tuned.