Hello and welcome.
And in this lecture, we're going to create hexadecimal numbers.
By the end of this lecture, you will be able to create number tokens for hexadecimal strings such as
this, and this will be converted into a number token with its value representing this hexadecimal number.
Let's go to Alexa Dot C.
And just by here we're going to say struct token token, make special number.
So we call the special numbers, right?
If it's hexadecimal or binary, it's a special number and we're just going to go struct token token
equals null struct token last token equals lexa last token.
So here we're just going to do Lex a pop token.
We haven't actually created that function yet which will just pop off the last token for us and we're
actually going to do that now.
So we're going to say void lexa pop token
vector pop lacks process token VEC.
Okay, so we need to figure out if we're dealing with a hexadecimal or binary number, right?
So what we're going to do is we're going to say char C equals P, C, OC, we're going to say if the
C is equal to an x, then they entered a hexadecimal number because we have zero x.
You see what I mean?
Okay.
And the zero here is what we're popping off on line four, five, nine here like the pop token.
We're popping off that zero, right.
Because we don't need that zero.
It was only used that zero number was only used to let us know that that we're creating a special number
such as zero be zero x, right.
But the Lexa would have created a number token out of that zero, which is why we have to pop it off.
Okay.
So just by here, we're going to say token equals token, make special number hexadecimal.
Now, we're not implementing binary numbers in this lecture, but we will be on the next lecture and
we'll basically have an LCF here that checks if the C is equal, B four, stuff like that.
Right.
But we'll get to that in the next lecture.
We're just going to focus on hexadecimal numbers in this lecture.
So all I want to do now is I want you to say return token.
Okay?
Now, just by here, we're going to say struck token, token, make special number hexadecimal.
And what's the first thing we're going to do?
We're going to skip the X, so we go next C because remember, at this point in time, we have zero
x, right?
Zero is being popped off on line four, six, six here and now we're popping off X from the input stream.
OC This will mean we're left only with a hexadecimal number,
so we're just going to go unsigned long number equals zero const char number string equals read hex
number str for string.
So now we're going to create the read hex number string function.
So we're going to go const char
read hex number string and this will create a buffer
and here we'll just return the buffer pointer to the actual data and we're just going to say char C
equals pixie lac c, get f.
Buffer.
C is hex cha.
And then finally, we're going to write our null terminator and the String.
So that's how our read Hex Number String will work.
It will loop through the input stream and it will check if it's a hex char.
And if it is, it will add it to the buffer.
If it isn't, it'll stop looking.
So we now need to create this hex char function.
So we're just going to say ball is hex char and we're just going to say return C above or equal zero
or C below or equal nine or C above or equal A or C above below equal F.
So that will allow us to parse all of the hexadecimal numbers.
Right, and create tokens out of them.
Okay.
So after calling read hex number string, we can expect our number string to actually hold whatever
that hex decimal number was.
ABC 635, for example.
So we go from zero x ABC 635 to this.
Now we need to convert that hexadecimal string into a number that the computer can understand.
So this is very easy.
We just go number equals string tall and it's one l, not two.
And we're just going to pass in number string zero and the base, which is going to be 16 for hexadecimal.
So we're saying to see this is a hexadecimal number, give us a decimal number, and that will return
the integer of that hexadecimal number.
And now we're just going to go return token, make number four value and we're just going to pass in
number.
Simple as that.
So now just scroll down.
And just by here, we're going to say Case X and we're just going to break and we're going to say token
equals token make special number.
So, you know, this X that we see here will be like zero x 5000, right?
We've already tokenized the zero into a number token, so we're left with X now.
So we see case X and that will read the special number which will pop off the zero that we create a
token for on the last cycle.
And we'll essentially create a number token for zero x 5000.
Okay.
So I just want you guys to scroll back up a minute.
Some make special number and here read hex number string we can see that cause is texture.
That was actually a slight mistake in this is hex char function that we're going to fix now.
So firstly we want to convert this to lowercase.
Okay, so we're going to say C equals to lower C and we're going to give it to this and we're instead
going to say C above or equal ask zero and C below equal ASCII nine or C above or equal ascii a and
C below equal ascii f.
Okay.
So that's the correct way to do it because we didn't take into account of lowercase hexadecimal and
we also forgot to use an ampersand here.
We just used all logical laws which was wrong.
So just head over to test dot c check that you've got a hexadecimal string here.
Okay.
You should have a hexadecimal string and just save the Lexa file.
We're going to go back to the terminal, make clean, make, and now we're going to run main and we
can see everything works.
So we now have hexadecimal numbers.
So congratulations on that.
If you want to test this a little further, what we can do is we can attach the debugger.
So we'll do that now.
So just by here, we're going to go gdb main, we're going to go break lexical c and we're going to
break on line 493.
We're going to say run and now we're going to print token our our num and there we go.
We can see the number for that hexadecimal string.
So if I was to just go print zero x ab seven five, we can see it's the same number so we can now create
tokens for hexadecimal strings.
Congratulations.