Hello and welcome.
And in this lecture, we're going to implement the parentheses buffer for the actual tokens.
So you'll know in past lectures we set up a parentheses buffer, which we never actually used.
I'm just going to show you that.
Now, if we go to new expression, you can see that it creates a new parentheses buffer and when we
finish the expression it decrements that we don't bother cleaning up the memory.
Because as I said in previous lectures, we're not so worried about memory management for this project.
So with the parenthesis buffer, we do create it, we do increment the current expression count, decrement
the current expression count, but we don't actually use it.
So we need to actually use it in this lecture.
So what we're going to do is we're going to go to the next C function OC this function here, and we're
going to change this.
So it actually writes to the expression buffer when you do next.
See.
So just by here we're going to say f lex is an expression buffer, right?
Let's process parenthesis buffer C so you can see it really is that easy every time we call next C,
if we're inside an expression, i.e. inside brackets like this, we will actually write that character
that we've read.
To the expression buffer and what we end up with for something like this.
For example, for the number token of 40 and the number token of 30 and the operator token of plus,
they all share a pointer to this parenthesis buffer.
So by the time we've gone to 30 and tokenize that for all of those tokens, the parenthesis buffer will
show 40 plus 30.
So it doesn't matter if you're on the 40 token, the 30 or the plus, we will see that.
And this is useful because later on we can say you have an error on line three, column one, and we
can show them this information.
You see what I mean?
So that they know where to look.
So now let's just go to token create and we just need to make some minor changes here.
So just by here, we're going to say again, Lex is in expression and we're going to say temp token
dot between brackets equals buffer pointer lex process, parentheses buffer.
So what we're now essentially doing is we're taking the parenthesis buffer we set up earlier and we're
grabbing the data pointer from there and we're setting the between brackets pointer in the token, which
is here const char pointer.
So this will allow us to take any pointer and see what's between those brackets.
Okay.
So in this example here, between these brackets, we have five plus ten plus 20.
Each of these have their own tokens.
So if we're on token 20, for example, we would see the full expression, five plus 25 plus ten plus
20.
Right?
If we're on token ten, we'll still see the full expression, five plus ten plus 20.
So that's the idea of of these between brackets, parentheses, track tracking that we're doing.
So all we need to do now, guys, I want you to take the prototype of Lexi's in expression.
Just copy and paste the prototype and just put it at the top of the file by here.
Like that.
Okay.
Just so the whole file can see it.
Because otherwise we have to worry about if we're above this function or below it.
Right?
So let's just go to the terminal and now we're going to do make clean, make.
We can see that Belle's fine.
So we're now going to test this.
So let's go to test see just get rid of all of this because there's quite a lot to look at.
We're going to go bracket 50 plus 20.
Just like that.
And now what I want you to do is I want you to go to the terminal.
I want you to do make I want to go JDB Main and we're just going to break on the Lacks function type
run.
So we're just going to type next and then we're going to press enter and to enter and we're just going
to keep going, right?
Because remember Enter runs the last instruction on JDB that you entered, right?
So we're just going to go like that.
And so let's read all the tokens.
Oh, there we go.
So now we're going to go print process token vec.
Okay, let's just do a vector count on here, make sure that it's correct.
It is.
So now what we're going to do is we're going to go print vector set peak pointer process token VEC zero.
So sets our peak pointer to the start of the of the token array and we're just going to go print asterisk,
struct token asterisk.
Factor peak process token vec.
And we can see our left bracket.
All right.
Between brackets is no, because we're not between brackets yet.
Let's just press the power and press enter again.
And there we go.
What do we see here?
50 plus 20.
So even though we are now on the 50 number token, the between brackets shows everything between those
brackets.
So this is an excellent way of debugging things when they go wrong and also providing more information
to the person compiling a program with your compiler.
So congratulations on implementing the between brackets parentheses.