In this lecture, we're going to create our very first token.
So just go to parse a dot C and just above parse.
Next, we're going to say struct token, token next.
And this is going to be a static function, guys.
And what this token next function is going to do, it's going to go into our current process token vector,
and it's going to basically grab the next token, the next token to process.
So we're just going to say struct token next token
equals vector peak, no increment
current process token VEC and we're going to go pause a ignore NL or comment.
So this will ignore the new line tokens or the common tokens and we're just going to pass in the next
token there.
We'll create that function in a moment.
We're now going to say current process pass equals next token pass.
So if we have a look at the pass here, as I said before, we need to know where we are currently processing
during the compilation.
Right.
So we want to know the line number in the column we are on during compilation.
So if something goes wrong, we can say exactly which line and column there was a compile error.
Okay.
So by here when we do token next to get the next token, we update that.
So if something goes wrong when processing this next token, when we tried to turn it into a node,
we will know the line and the column that problem was on.
So now we're going to say parse the last token equals next token and we're just going to make make that
OC so static struct token parse the last token just like that and just by here just set it to null and
then we just return vector peak comm process token vec.
So why are we doing peak no increment and not just returning next token here?
Why aren't we just doing vector peak return the next token because parse, ignore, angel or common
that might change what the last token was.
And that's why we're not doing that.
Okay, so just by here now we're going to create that ignore new line or common function.
So just to static void, parse, ignore NL or comment struct token token.
So we're just going to say, well, token and and token is angel or comment or new line separator token.
We're going to have to create that function as well.
Static ball token is angel or common or new line separator struct token return token type equals equals
token type new line or token type comment or token a symbol which we haven't created yet token to back
slashes.
Okay.
So that's an important function and we're actually going to move this function from here into token
dot C because that's where it belongs, to be honest with you, and give it a the static as well.
It's best to keep it abstracted out.
There you go.
Very nice.
And we're going to need to put that in compiler Todd H as well.
Just like that.
And back to token.
See, we're going to create the token symbol now.
So we're just going to go ball token as symbol struct token token cha si.
So we're just going to say return token type equals token type, symbol and token.
See, Val, because we know symbols are R characters equals C perfect and that should work.
Okay.
So just copy and paste this into the header file.
Just like that.
And you'll see that I only did one equals here.
If you made the same mistake, do two equals.
So back to positive.
See, we're going to skip the token by doing a vector peak on the current process token vector.
And the parser also doesn't care about new lines because only the preprocessor really needs to worry
about that.
So now we're just going to go token equals vector peak, no increment current process token vec.
So that should work absolutely fine.
So what we're now going to do is we're going to say static struct token token peak next struct token
next token equals vector peak now increment can process token vec.
And again, we're going to ignore the new line or comments because the parser doesn't need to concern
itself with those.
And then we're just going to go return vector peak, no increment come process token back.
So again, we're not just returning next token because parse, ignore now or comment possibly might
change where the next token actually is.
So we have to call that right.
Okay then.
So we're ready to start implementing our PA's next function.
So just go in here and we're going to say struct token token equals token peak next.
And if the token is null, we're going to return minus one.
Otherwise we're going to go in at rest equals zero switch token type.
So here we're just going to say case token type number and we're going to say break and we're just going
to say pause, single token to note.
And we're also going to have identifiers here.
So do case token type identifier.
And finally, case token type string.
So these are all things that can be converted directly into nodes, right?
So now just up here, we're going to say void parse, single token to Node.
We're going to say struct token token equals token.
Next struct node node equals.
Now we're going to say switch token type.
Case token type number.
And that's going to say node equals no create.
Okay.
We haven't created that function yet.
We're going to create that now.
So just head over to Node C.
And just by here we're going to say struck node, no create struck node.
Node struck node node equals malloc.
Size of struck node mem copy node node.
Size of struck node.
And we're just going to attach a little warning here for ourselves, because we will need to come back
to this function at some point to adjust it.
Right.
We should set the bind it owner and bind it function here.
So this will just remind us when we compile, it'll show us this warning message, right?
So now we're just going to go no push node and we're going to say return node just like that.
Now we're going to copy and paste no create into the header file and we're going to have to do it to
every function in that file because we haven't done it.
So just go there.
Going to copy and paste?
No, pop.
Just like that.
No peak.
No peak or no?
No push.
And nodes have vector.
Okay.
Nice.
So let's just go to Pazzi again.
And now we should have no create and we're just going to go ampersand struct node whenever type equals
node type number and we're going to say lx num is equal to token lx num.
Okay.
So that's our number node parse correctly.
We're going to say case token type identifier and we're going to say break node equals no create ampersand
struct node dot type equals node type identifier dot asphalt equals token asphalt.
And then we're going to say case no type string.
Break node equals no create ampersand struct node type equals node type string de se file equals token
as val.
And you can see that's complaining that should actually be token type string, not node type string.
So just by here we're going to say default compiler error count process.
This is not a single token that can be converted to a node just like that.
And we don't have to push anything to the node vector because no create actually does that for ACL OC.
So that allows us to create number nodes, identify nodes and string nodes.
So that should work.
Okay.
And we just need to do a no peak buy here.
So as you can see, what's happening is the no peak or peak from the normal vector that we use for normal
operations.
Right.
And then it will push it into the tree vector because this is the root of the tree.
When we're at this point in time, anything left is at the root of the tree.
Because if we have a function body, it's going to pop off all the nodes that have been parsed to put
into that body.
Right.
So anything we're left with when we go to read the next token here with Parse, next is the root of
the tree.
So therefore, it needs to be pushed to the root of the tree.
So we're nearly ready to test this.
So before we go any further, let's just do node set vector and we're going to say process, node,
VEC and process know Trebek just like that.
Now just go to test.
So let's see.
And we're just going to have numbers and identifiers just like that.
And now we're just going to go in here.
We're going to say make clean, make.
We're going to do main.
And we can see that it worked.
Okay, so that's perfect.
If we now go to compiler dot C.
And we're just going to put a breakpoint
on line 59, which is compiler file lock.
And we're just going to say process node VEC, node tree VEC and we're just going to do a vector count.
So we see two.
We do a vector count on Novak.
We see to say that's perfect.
If you really interested, you can do something like this so we can go print struct node, vector back
process.
Novak.
And.
Still a double pointer there like that.
And we can see a BCD, which is the last node on our abstract syntax tree.
So remember, we need to use the two asterisk here for for the reasons I explained earlier.
You'll have to check into the vector c file.
If you're confused, just take a bit of a read of it and it'll make sense.
So, yeah.
Congratulations on implementing your very first node.
Well, three nodes, I should say.
Numbers, identifiers and strings.
Thanks for watching.