Hello and welcome.
And in this lecture we're going to implement the parsing of keywords.
So we're going to go void, pause, keyword, struct, history, history.
And just by here we're going to say case token type keyword and we're going to say pause, keyword,
rest equals zero.
So let's just scroll up to our parse keyword function.
And this parse keyword function is going to be responsible for literally all keyword tokens.
So when we see a void here, obviously we know that there's a function coming next or it could be a
variable, right?
Same for any that we see in it.
It could be a function, it could be a variable.
We don't know until we look further ahead.
But all of this happens in the past keyword.
Okay, so we're just going to start by going struct token token equals token peak next and we'll even
check for the size of keyword here, but we won't do that right now.
Okay.
So before we continue, we just need to create a few more functions.
So just go static pull is keyword variable modifier
const char val
and we're just going to go return as equal val unsigned
or as equal val signed or as equal val static or as equal val const or as equal val extern or as equal
val ignore type check.
And we should just have one underscore there.
Okay, nice.
That's the first function.
So this will let us know if it's unsigned sign static cons extern something like that added as a token
in our source file.
So we're now going to do we're going to head over to Alexa and we're just going to say pull keyword
is data type const char string and we're just going to go return s equal string void or x equals string
char or as equal string in it or as equal string shot or as equals string float or as equal string double
or as equal string long or as equal string struct or as equals string union and that's that.
So now just right click from a document and we're going to take this and put it as a prototype and compiler
dot H.
OC just like that.
Okay.
So we're ready to start using those functions.
So just back in Pass Key, where we're going to say F is key word variable modifier.
So can s file or keyword is data type, so can S file.
So if it's either of those, then we're either parsing a variable, a function or a structure or union.
So we're going to call a special function for that parse variable function or struct union, right?
And we're going to pass in the history there and then we're just going to return just by there because
we don't want to run anything else if we've dealt with it like that, because we know we know that it's
either a variable function or struct union.
So it's been handled in this function.
So we're going to say void parse, variable
function or struct union struct history.
So the first thing we need to do in this function is figure out the return type, the variable type
or the structure type.
Until we parse further ahead, we have no idea if it's a return type of variable type or a structure
type, right?
So what we're going to do is we're going to parse that.
So we're going to go struct data type D type and we haven't actually created the data structure yet.
That's fine.
And we're going to say parse data type, ampersand type.
So we're going to have to create that function to.
So here, what do you see?
You see static ball.
Yeah.
Now what you see here you see ball A So this data type will apply for either the return type of a function,
the structure or union type or a variable data type.
Okay.
So that's why we're parsing it here for all of them because it's going to be one of those.
So we need to make data type structure.
So just go to compile it and just scroll up.
And just by here we're going to say struct data type
and it flags in it type.
So this will be like i.e. type of long init float, etc., right?
So that's going to be an integer type representing either one of those or basically any primitive type.
Right.
And we're going to say struct data, type data type secondary.
And I'm just going to go back into full screen mode in my virtual box.
There you go.
That's better.
So we're going to go const char type string.
So this will be like the data type string.
So if this has a type of long, then this this will basically equal long.
So now we're going to go size t size and this will be the size of the, of the data type.
So for example, long would be eight bytes, so the size would be eight.
And now we're going to have a pointer depth on the data type.
So for example, if I did something like this, that would have a pointer depth of three, whereas if
this was just one, it would have a point of depth of one.
Okay, so that makes sense.
That's what the pointer depth is for.
So we're now going to have a union and this union will be used for structure union types.
So essentially they'll point to a node of struct node.
And a node of union node.
So obviously it can only be a structure union.
It can't be both, which is why it's inside of a union.
And our data type will also have a structure for array brackets, but we're not going to do that just
now because we haven't even implemented arrays yet and just rename data type secondary to just secondary.
So what is the secondary data type?
Well, for example, I can have long, long a so two data types here.
Right.
Which is why we have a secondary so that that will be equal to whatever the second data type was in
this case its long long in it secondary would be in it and the main data type will be long.
So that's what the secondary data type is.
So it's a nice little comment there to make that clearer.
Okay.
So let's just go back to parse a dot C and here we're just going to say volume, parse data, type struct
data type D type mem set D type zero size of struct data type.
Oh, and I actually forgot to make the data type flags, so just go to compile that.
And just here, we're going to say enum data type flag is signed equals 0b1, two, three, four, one,
two, three, four.
And then just take off the final zero and we'll put a one there and we're going to say data type flag
is static and we're just going to copy and paste that, remove the the one and put a one there data
type flag is const.
So obviously these are all the possible options, right?
And again, just take it off and put it there.
Data type flag is pointer.
So this is true.
If the data type is a pointer, data type,
data type flag is array.
So again, is this data type.
Sort of like an array that has array brackets.
If so, that said
OC.
Data type flag is externe.
And we're just gonna copy and paste that.
Just as before, we're gonna take the shift.
The one to the left.
To the left.
Data type flag is restrict.
And then we're going to say data type, flag, ignore, type checking
and we've reached the end of the mass.
So we're going to have to extend it a little bit.
So we're going to say data type flag is secondary.
So that will be set for secondary data types.
We're going to copy and paste.
I'm just going to add a zero at the end.
Data type flag struct union no name.
So this is set if if we have a structure union that has no name.
Again, copy and paste this one, add another zero.
And finally, data type flag is literal and make sure you put any equals there.
So again, copy and paste, add another zero.
Okay.
So yeah, what these flags are tell us.
It'll tell us if the data type is signed, static constant is a pointer, if it's an array, if it's
external, everything you would expect to see programming language.
And obviously if this is applied to the, to the data type of a function such as the static keyword
here, then obviously that static keyword would make the function static and it would be on the return
type of the function.
So we're now going to make our data type types.
So just make a new enum and we're going to say data type, void data type, char data type, short data
type, integer data type.
Long data type.
Float data type double data type struct.
Data type union and finally, data type unknown.
So we pretty much have everything right.
We got void, we got char, we got shot, we got integer, we got long float, double struct and union.
So obviously if it's a struct or union type, then you would look into the type string to find the structure
that it is.
So for example, if we parse something like struct dog, the type would be set to data type struct,
but the type string would be dog if that makes sense.
Right.
Cool.
So let's head back to the parser now in the C programming language, all variables are signed by default.
So when we first parse a data type, it's going to have the signed flag attached automatically.
So what's the parsed data function going to do?
It's going to parse the modifiers.
That's the first thing.
Parse data type modifiers.
What else is it going to do?
It's going to parse the actual type and we can also have modifiers after they specify the type.
So we also need to parse modifiers again.
So we're going to start by creating the parsed data type modifies functions.
So we're going to say void parse data type modifiers.
It's going to take a data type.
And we're going to say struct token token peak next.
And data types can have many modifiers.
So we're going to need a while loop here token and the token type is a keyword.
So now allow us to parse through all of the different modifiers that you see here.
So what we're going to say is, is if it's not a keyword modifier, a variable modifier, then we're
going to break.
And now what we're going to do is we're going to basically check the value and set all of these possible
flags based on the values we've read.
So just by here, we're going to say if SW equal token SE file signed,
then DX type flags or equals is signed nice and easy else f as equal token se file unsigned then d type
flags and equals type flag is signed.
So we just do that and set the flag right just like that else.
If as equal token as fail static d type flags or equals is static elseif s equal token s file const
d type flags or equals as const elseif as equal token s file extern d type flags or equals is extern
ls f as equals
so can s file ignore type check d type flags or equals ignore type checking OC.
So they are our modifiers.
So as you can see, we parse the token and then we set the flag based on what that token was.
Is it a const token?
Then we set the const flag and as you can see, this will mean that it will be very easy to examine
data types later on because we have everything we need in this data structure.
We have the size of the rainbow, we have how many pointer depth it has.
We have the type as an integer.
So it's faster to parse.
We have the flags.
So as you can see, it makes a lot more sense to break it down into memory like this, right?
So now that we've read that modifier, we now need to do token next because we only did a peek here,
we never actually popped it off.
Right?
So just by here, we're going to say token next and then we're going to say token equals token peak
next to take us to the next one.
So that's it for parsing modifiers.
It's a nice and easy one.
You can see we just loop through it until we no longer find a variable modifier, basically.
So we now need to implement the actual paths data type type.
But I appreciate that we've done quite a bit of work here.
I'd hate to make you all get lost.
So what we're going to do is we're going to continue this in the next lecture that allow you to compare
your source code with mine and make sure everything looks okay.
You can find the source code in the lecture description.
We'll continue what we're doing here on the next lecture.
Thanks for watching.