Hello and welcome to part two of implementing the data types.
We're now going to go ahead and implement parse data type type.
So we're just going to go void parse data type type, struct data type D type
and we're going to go struct token data type token equals null struct token data type secondary token
equals null.
And I've already made you aware of what a data type is and what a secondary data type is.
So for example, long in it, long is the data type in it is the second data type call.
So now we're going to call a function we haven't created yet parse to get data type tokens.
And we're going to do ampersand data type token, ampersand data type secondary token just like that.
So this is a very simple function, so let's just create it and it's going to take a token, but that's
going to be a multi dimensional pointer, their data type token struct token data type, secondary token.
Nice.
So we're going to say asterisk data type token equals token next.
So that will just ensure that we set the data type token here on line two, seven, eight to whatever
the next token is.
Okay.
Now we're going to peek ahead for the next token
OC and we're going to say if token is primitive
keyword and we're just going to provide the next token, we haven't created that function yet and we're
now just going to implement this function.
So just head to tokens.
See, we're going to paste this in as a bulletin when you construct keyword.
And just at the top here, we're going to say define primitive types total and we're going to put seven
and we're going to just go const char of types equals and we're also going to go primitive types total
they're OC and then we're going to say void char shot in it long float and double
and I just buy here we're going to just replace this with struct token
and we're going to say if the token type does not equal token type keyword, then return false and then
we're going to save it.
Init equals zero a below primitive types total a plus plus.
And we're going to say if s equals primitive types I token s file then return true.
Otherwise we're going to return false.
So there's our function and that will tell us if any token is a primitive type or not.
And we wrote a similar function in the Lexa, but it also checked for union and struct.
So go to compiler h and we're going to paste the prototype.
Okay.
Just like that great single about the.
See now.
And we can continue writing this function.
So if the next token is a primitive keyword as well, then we're going to say data type secondary token
equals token next.
Or if you want to make it a little more cleaner, we can just say next token and then we can go token
next, which is a bit more clear what's going on.
Cause so that as you can see, that that gets our first data type token and then it looks to see if
we have a second data type token.
And if it does, it gets that one as well.
Nice.
So let's head back to parsed data type type and we can continue working on that.
So we're going to say in it, expected type equals, parse data type expected for type string.
And we're going to just pass in the data type token as Val by there.
So we haven't really covered the concept of expected types yet.
Essentially, an expected type will basically be like, does this data type expect to work with primitive
types?
Does it expect to work with structure types or union types?
So that's what we're going to make with this.
So just go to compile that and we're going to create enumeration.
So we're just going to go in enum data type, expect primitive data type, expect union data type,
expect struct.
So these these expected types will basically guide the parser in creating the data type.
Okay.
So let's create this function.
So we're going to see in it parse the data type expected four type string, const char string.
And we're going to have a default type of primitive.
And then essentially what we're going to do is we're going to say if s equal string union and type is
equal expect union else elseif as equals string struct and type equals expect struct.
And then we're just going to return the type just like that.
So obviously if you imagine the parser, right, it has something like this struct ABC when we do get
data type tokens on this input struct ABC, the data type token will be set to to struct.
And the secondary token will still be null.
Okay.
And then what then happens is we will do parse the data type for type string and obviously struck will
go in here because that's the token we parsed and then the type would be set to expect struct.
So that's what's happening there, guys.
Okay, cool.
So we're going to create a new file called Data Type C, and this is just going to contain helper routines
for working with data types.
So just include compiler that hedge and we're going to create a function here, pull data type is struct
or union for name and we're going to say const char name and we're just going to do return s equal name
union or SQL name struct.
So that will obviously return true if the the name that you provided here is either a union or a struct.
Okay.
So just copy and paste that into compiler hedge.
Just like that.
And now hep deposits C And we're just going to say if data type is struct or union for name and again,
we're going to pass in the token like that.
So back to our struct ABC example, what comes after struct, the structure name, which is ABC.
So that's what we're checking for here, right?
We check, oh, this is a struct data type.
Therefore on line three or seven by here, we need to parse the name.
So that's what we're going to do here.
Okay, guys, call.
So the first thing we're going to do is we're going to peak ahead.
So we're going to say token peak next.
Type equals type identifier.
Token type identifier, not node
OC.
And if that's true, then the real data type token should be the identifier.
OC So again, with our struct ABC, what will happen is on line three or nine here we change the data
type token from struct to ABC OC and that's what will happen essentially.
So the data type is now ABC no longer the struct keyword here.
Okay.
So type token.
Nice.
Now structures don't necessarily need names.
So let me show you what I mean.
We can go here and we can say struct init x ABC.
That is a valid structure and that structure has no name.
So in this scenario where we check for an identifier and we get the data type token, that won't work
for this example because we're currently just after the struct.
That's where we're parsing.
If I did ABC by here, then yes, we would we would take that and that would become the new data type
token as shown on line 313.
However, in a situation where we don't have that, we need to handle that, right?
So we're going to handle structures with no name now.
And we do that by making an LS.
This structure has no name, so we need to handle it
and we're just going to go data type token equals parser, build random type name.
So in our compiler structures will have random names if they don't get provided any essentially.
And we also need to put a flag there to make that clear.
Right, that there is nothing there.
So we're going to go D type flags or equals struct union, no name, that type flag struck unit and
no name code.
So that just lets the person who's dealing with this data type know that this structure has a random
name.
It's not it's not one that was set by the programmer.
So that deals with our structures.
So we're now going to build the paths that build random type name function.
So just do struct token parser, build random type name and we're just going to go char temp name that's
going to hold 25 bytes.
S printf temp name, custom type name under scroll percent a parser get random type index.
Okay.
So now we need to create that type index function and this is going to be very easy.
So we're going to go parse a get random type index and it's not necessarily going to be random, right?
We're going to go static in it.
X equals equals zero, x plus plus return x.
So that is our function and how that works.
Right?
So obviously, as you know, a static variable means that it's global memory.
So this X will always stay the same, right?
So when we increment this by one, an X and you call this x will be one.
So I'm not I know not all of you will probably be aware of what static variables do, which is why I'm
telling you.
So this, this compiler cost, you know, it also improves your C skills because you're basically building
a C compiler, right?
So that's pretty cool.
So now we need to create some memory.
So we're going to say as value equals malloc size of temp name and we're essentially going to copy from
the temp buffer into S4 OC just like that.
So let me bring you all up to speed so far.
So essentially what we're doing is right, we're filling temp name with custom type name, underscore
percent I Okay.
So this will become something like custom type, name one, custom type, name two, custom type, name
three.
Because every time it calls the random type index, it incremented by one.
Right.
Okay.
Well then copying that into this dynamic buffer because we're about to create a token where we'll return
what the file was.
And we cannot we obviously can't return the temp name because it's on the stack.
Right.
Which is why we need to declare the buffer for it.
So we're just going to say struct token token equals clock size of struct token.
Okay.
And just put a one by there as well.
Call and type names are identifiers only.
So we're artificially creating a token here.
So yeah, that looks fine.
So we should just be at every turn the token now and there we go.
We artificially created an identified token.
Nice.
So that's it.
For pause in the data types of structures and primitive types, we now need to deal with pointers.
So if someone was to do something like init, a ask is ask us a we need to be able to deal with that.
Right.
So the data type here in it asks is ask risk.
We need to better cope with that and deal with that.
So we need to create some code for that.
So to do that, we're going to say in it points a depth equals pause, a gap pointer depth.
So this is what I call it in my compilers, right pointer depth.
How deep is the pointer in this situation where this comma is there, since each pointer has as a depth
of two because it has two asterisks.
So we need to create that function.
So we're just going to go in.
It has to get pointer depth and we're just going to say init depth equals zero return depth while token
next is operator.
We haven't created that function yet.
Depth plus plus token next.
So while the next token is an operator, increase the depth by one and pop off that token.
So we need to create this token.
Next is operator function.
Okay, so we're just going to scroll up.
Just after token Pete next we're going to say static bool token next is operator const char opp struct
token token equals token peak next.
And we're basically just going to say return token type equals token type operator.
And actually we've got a token dot C.
Yeah.
What we'll do instead is go to token C, we'll say ball.
Token is operator.
Stock token.
Token const char val.
And we'll just return token type.
It's token type operator and s equal token s val.
Val.
Okay, so this function here will check that the token you're providing is an operator token and it's
as far as equal to the value provided here.
Copy and paste that into compiler hedge.
We'll just put it.
By here.
No problem.
Okay.
And now go back to paths dot C and instead of checking this here, we'll just say token is operator
and we'll pass in the next token.
And then finally the operator that they pass to us here.
Yeah.
So there we are.
That works and it's cleaner because we, we don't have to write the same code twice.
Perfect.
So let's just scroll back to where we were.
Okay.
So at this point in time, we've got the current point of depth.
That's perfectly fine.
So now we just need to initialize our data type.
So we're going to say void the data type and ed struct token data type, token struct token data type,
secondary token struct data type data type out integer pointer depth and finally integer expected type.
So we're going to create another function here.
Void, parse the data, type in it, type and size.
So it's all about breaking down the problem.
We want to break it down as much as possible because it's easier to work with.
So we're going to say struct token data type token struct token data type secondary token
struct data type data type out.
And it pointed to APS and it expected type.
So it's basically the same arguments, guys.
Okay.
And that concludes part two of parsing data types in part three will finish these initialization functions.
They are bigger than you think, which is why we're going to have a part three of this data type series.
So thanks for watching and stay tuned to the next lecture.