Hello and welcome to part two of creating variable nodes.
In this lecture we are going to implement multi variables.
So for example in C you can do something like this, we are going to implement that.
This will allow people to create series of variables, all of it, all of our particular type, all
on the same line without having to create multiple init keywords.
So go to parse dot C and just after we parse our variable, we're going to check if there's any more
variables to parse.
And we do this by saying if token is operator token peak next and we're going to look for a comma.
So if we go back to test that C, you can see here we parse in an X, we check if the next token is
an is a comma, which it is.
So then we know we have another variable to parse.
So when we have multiple variables combined together like that, we call it a variable list.
If you look in our header file.
You can see no type variable, no type variable list.
So in this particular situation and it X ID equals 50 where they're going to have a no type of variable,
there's not a no type of variable.
And we'll essentially have a vector that contains all of the variables.
So we're going to create that now.
So we're just going to say stroke factor, var list equals vector, create size of struct, no pointer.
So it's essentially going to be a vector of nodes.
So the first thing we need to do is we need to pop off the variable we parsed here on line five, seven
three.
So pop off the original variable, we're going to say struct node, var node equals node pop and that
will pop it from the stack.
And then we're just going to push it to the VAR list.
Just like that, make sure you do an ampersand var node.
So it's possible for people to just do this forever.
You know what I mean?
There's no there's no limit.
And because of that, we need to have a wild loop.
So what we're going to do is we're going to say, well, token is operator token peak next and we're
going to check for that comma again.
So while there's still more arguments to go, we're going to process those.
So we're going to start by getting rid of the comma.
So we just do token next.
Now we're just going to go name token equals token next because what comes after the comma a name x,
comma E.
So there's a name that comes next year, a new variable name, and then we're just going to call pause
variable passing the original data type that we parsed earlier, which in our case would be an integer.
And we're just going to call pause variable and passing the new name token and the existing history.
Well, then going to pop that off and push it to the variable list just like that.
Now the final thing we need to do is we need to we need to make a new function and we're going to call
it make variable less node and we're just going to call it just when we finished processing this, we're
going to call it.
So we now need to make that function, obviously.
So we're just going to scroll up.
And just under any of the other makes, we're going to say void make variable list node and we're just
going to pass in the vector.
So now I just wanted to go to compile that hedge.
And.
School to our node.
Just by here we're going to say struct, var list, var list and then we're going to say struct a vector
list, a list of struct node variables.
So that ensures we allow some memory to store this variable list inside of our node that we're about
to create.
So back deposit.
RC, we're going to say no, create ampersand struct node type equals node type variable list dot var
list dot list equals var list vec.
And it's as simple as that that allows us to create a variable list.
And now the only thing I want us to do now is, is at the end of our parse variable functional struct
union, we expect a semicolon.
So we're just going to say expect same and we haven't created this function yet and we're just going
to put in a semicolon and that'll fix that bug.
On the last lecture where if we had a semicolon, it kept us in an infinite loop.
So we're going to need to implement this, expect some function.
So just scroll all the way up.
And.
Just here.
We'll be absolutely fine and we're just going to say.
Static void expects sim chassis and we're just going to look for the next token
and we can see that we actually need to put this under token next for that reason because we're accessing
it.
So we're just going to paste it there and we're just going to say if next token is null or next token
type does not.
Equal symbol type.
Symbol.
Token type symbol.
Or the next token C vowel does not equal C.
So if either of these clauses are true, then the next token is not the symbol we were expecting.
So when we call it spec sim, we're basically saying is the next token a symbol with the character I
provided here.
And if it isn't, then we need to show an error to the user.
So all we're going to say is this compiler error current process expecting symbol
percent c.
However, something else was provided and obviously the percent C will be the C character there.
So this will say expect and symbol semicolon.
However, something else was provided.
Nice.
So that's our expected sim function.
And that will ensure that we can now have semicolons at the end of our variable declarations.
So that should be absolutely fine.
Let's just make cleaning and make and we're just going to do a main and we can see everything compiled
fine.
So that now parses correctly.
And just to test this.
We're going to put another breakpoint so in pars next.
We're just going to put a breakpoint
right by here when we peak.
In pars in the pars function.
So we're going to go gdb main break paths the dot c line 705 again check the line numbers because it
might be different for you.
Yeah.
And we're going to say run and now we're just going to print out asterisk node.
We can see we have a node type of six.
Or we can see our var list is here.
And if I was to go print vector count node far less dot lest we see seven.
Which is how many variables we declared here.
One, two, three, four, five, six, seven.
Seven variables.
So you've successfully implemented the variable lyst note.
So thanks for watching.