Hello and welcome to part three of implementing data types.
In this lecture, we're going to continue implementing our paths of data, type in it and our paths
data type in it, type and size functions.
So what's the very first thing we need to do when initializing the type and size?
Well, we need to check that the secondary data type is valid if one is provided.
Okay, this is because you can't do things like struct in it.
And for those reasons we need this in place.
So what we're going to do is we're going to say if parser data type is secondary allowed and we haven't
created this function yet, expected type and there was a secondary token provided, then if that's
not allowed, we need to write an error.
Essentially
you provided an invalid secondary
data type.
Nice.
And we're going to make this function now.
And it's so simple, right?
We simply do return expected type is equal to data type.
Expect primitive, simple as that.
Now just by here we're going to say switch expected type, case data type, expect primitive and we're
just going to go parse the data type and it type in size for primitive.
We haven't created that function yet.
Data type token, data type secondary token and data type out.
Now we're just going to create that function void, parse the data, type in it, type and size for
primitive.
I'm just going to create the data type token and the secondary,
and we're just going to put the data type here.
Okay.
So obviously this function here will be responsible for initializing primitive data types.
So in it short, long stuff like that, right?
So before we continue, just say ball parser data type is secondary allow for type, const char type,
which is going to say return s equal type long or as equal type short or as equal type double or as
equal type float.
Now can we do an init in it.
I can't remember that one.
No we can't.
So that's absolutely fine.
So yeah, these are the ones where there's secondary types of valid.
So for example, I can, I can do a long, long and so on.
Right.
I can do long, long and stuff like that.
And this is where secondary is allowed.
So our long is allowed another long OC.
Nice.
So then just by here we need to say if parse there type is secondary allow for type and we're just going
to put in the data type token and we're also going to check that we actually have a secondary token
and just put the exclamation mark not operator there so we can check that it doesn't that it's not allowed.
So if the secondary type is not allowed and a secondary type was provided, then we need to show an
error here.
Right.
You're not allowed a secondary data type here for the given data type.
Okay.
And we're just going to go data type, token se file.
Nice.
So that handles that to make sure no one passes any secondary data types that aren't allowed.
Okay.
So now for the fun bit, we're going to say S equals data type token as file void.
So if we have a void, then what does that mean?
What it means that we need to set our out type.
It's a data type void.
And it also means that the size of this variable will be zero.
What if they provide
a job?
Well, they provide a char data type out type equals data type job.
And then we'd go data type out size equals one.
However, these literal numbers look really bad, don't they?
Let's go to compile age.
We're going to make an enumeration here.
Right?
So by here we're going to say Enum.
And we're going to say data size zero equals zero.
Data size byte equals one data size word equals two data size d word equals four data size D word equals
eight.
And that's a lot cleaner now, right?
Because we can use those.
So now this this changes to data type data size zero.
And this changes to data size, char or byte, I should say.
And now we're going to do one for short.
So we do elseif as equals.
Okay.
And that should have a data size of word.
Okay.
So that looks absolutely fine.
And I do one for integer.
Okay.
And now we're going to do LCF as equal data type token asphalt.
Long
That's going to be a dirty word.
And you know what?
Actually, that's just going to be a word for now, our longs because we're making a 32 bit compiler.
So to work with lungs properly, we'd need to split it into two sets of four bytes.
So we'll just have we'll just have d word for now and we're going to go L cif.
So I know that's quite a lot, but we've done that now.
Cool.
So if we have anything other than that, then something's gone really wrong.
Okay, so we're just going to get a compiler error.
Go in.
We're going to say bug invalid, primitive value, variable, simple verb, invalid, primitive data
type.
I should say call.
That's fine.
So we still need to take into account our secondary token, which we haven't yet done.
So we're going to have to process that now.
So we're just going to scroll up here and we're going to say void, parse, data, type, adjust for
secondary struct, data type, data type.
So now we're just going to go struct token, data type, secondary token.
And if there is no data type secondary token, we're just going to return because there is no secondary
token to adjust.
And this should actually be called adjust size for secondary, not just for secondary.
So it just size for secondary.
Okay.
So we just need to create a secondary data type now.
So we're just going to go struct data type secondary data type equals Kalak one size of stroke data
type simple as that and copy and paste this prototype and just paste it there because we're about to
call this function.
So we're just going to go pause the data, type in it, type in size for primitive.
And we're going to pass in the secondary token and we're going to have a null secondary token for this
one, of course.
And we're going to pass in the secondary data type by that.
Okay.
So what we're now going to do is we're going to say data type size plus equals secondary data type size
data type secondary equals secondary data type data type flags or equals data type flag secondary.
OC OC, so that should work fine for that.
So yeah, this will one one we call this.
It will adjust the data type size to also include the size of the secondary size, but it will also
have its own secondary data type, which we create here.
Okay.
So we're just going to scroll down here and we're going to say pause the data type, adjust size for
secondary.
We're going to do data type out and data type secondary token calls.
So that should adjust the size of our data type based on the secondary token that was provided if they
bothered to provide one, if like, if they needed to provide one.
So if they pass along long, this size will be adjusted from four bytes to eight.
But obviously really the longs should be eight bytes anyway.
But as I already explained, because we're right in the 32 bit compiler, it will be four bytes for
now unless we plan to implement the full eight bytes later on, which will mean we'll have to split
the the data into two operands, essentially, because we're dealing with we're dealing with 32 bit
compiler here.
We're making a 32 bit compiler.
So okay.
So just by here, we're going to say case data type, expect struct and case data type, expect union.
And we're basically just going to show an error because we don't actually parse structures or unions
yet.
So we can't exactly implement data types for them if we haven't actually invented them yet.
Right.
So we're just going to put structure and union types are currently unsupported and we'll obviously support
them later on through the course when we've created the parsing of structures and unions.
Okay.
And we'll just do a default here as well.
Compiler error can process bug unsupported data type expectation.
And that's just in case we we end up making another one of these in the future, but forget to handle
it or something like that.
So in that default case, we'll catch that.
Right.
Nice.
So that should work.
Okay.
So we're nearly finished with data types for the time being.
So just head back to parse the data, type in it and we're going to call the function we just created
and it type in size.
You're going to pass in the data token data secondary token there, type out the pointer depth and the
expected type.
And then obviously that will go through.
It'll see that it's a primitive and it'll initialize the primitive data type for us based on the string
type of the token provided this is really good because it allows us to deal with nice structured memory
rather than have to parse tokens every time, you know what I mean?
So it makes sense to split split the data up into data types, which is what we're doing.
Okay.
So now just back here, we're going to say data type out type string equals data type token se file.
And we're also going to prevent the use of long, long for the time being because it's not really supported.
You know, our lungs are only 32 bits right now.
So we're just going to say data token s file is equal to long and there's a secondary token.
And that secondary token asphalt is also along and we're just going to do a compiler warning current
process.
Our compiler does not support 64 bit lungs, therefore your long lung is defaulting to 32 bits.
Okay.
I'm really it actually gets set to eight, eight bits as you know, but a bytes, I should say.
But for now, we'll just show this warning.
Just just make sure the user is aware that long, long compatibility is a bit bit off right now and
we're going to see tear type out size equals data size.
DWORD And I know previously I said the long was 64 bits.
Yeah, that was my mistake.
Of course, the long 32 bits in size and the long long is 64 bits.
Okay.
So yeah, essentially we just can't support 64 bit longs right now because registers in 32 bit processors
are 32 bits in size.
Right.
So that means if we need to deal with a 64 bit number, we're going to have to do that in two operations,
which is a bit complicated.
So for now, we're just going to show a compiler warning that says we defaulted to 32 bits.
Okay.
So we've nearly finished implement the data types.
Just scroll down.
And remember we did this point of depth a couple lectures ago.
We're finally ready just underneath to call, pause the day, type in it.
And I know this has been a huge, huge task for everyone.
So I appreciate your patience and I hope it makes sense.
And if it doesn't, please drop me a message.
It's important that we break things down like this because it makes things a lot easier later on.
And this is a very ambitious compiler project.
If this is your first compiler and I hope to see you through to the end and you can see I wrote data
type out there, just change that to D type just like that because we want to pass in the actual data
type and this function argument.
Right.
Okay.
That looks about right to me.
And we can see that we pass the modifies, we parse the actual type, everything looks okay.
Let's add data type C to the Makefile.
So just head to the Makefile and we're going to say just under expression, abort, we're going to copy
it, paste it, and we're going to say data type DTO, data type C de, type C data type.
Oh, I'm going to scroll to the top here and we're going to say build data type
just like that.
So now just get the terminal.
We're going to do make can see that everything works.
Okay, nice.