In this lecture we're going to implement one line and multi line comments.
So currently if we did something like this, it's not going to work so well, right?
Unexpected token.
So we're going to actually implement comments now and Alexa will be able to identify them.
So just go to Alexa Dot C and just by here we're going to say struct token token, make one line comment
and we're going to say buffer buffer equals buffer, create sha c equals zero.
So we're going to say let's get KF buffer C and what's our expression going to be?
C does not equal new line and C does not equal and the file OC.
So that makes sense because we are reading a one line comment.
So hello world should be read until a new line character is found or an end of file in this example.
Right?
So now we're just going to go return token create ampersand struct token dot type equals token type
comment and the s file will be the buffer pointer of the buffer.
So that is our function that will handle one line comments.
We now need to make multi line comments which is a little more tricky.
So we're going to say struct token token make multi line comment and that should be one word that multi
line.
So we're going to say struck buffer buffer equals buffer create char C equals zero while one lacks get
C if we're going to pass in the buffer and our character and the expression this time will be that our
character does not equal asterisk and it does not equal end the file.
So what we're going to say now is, is if C is equal to end of file, then we never close this comment,
but the source file ended.
So that's a bug.
Right?
So we'll say compiler error lacks process compiler.
You did not close this multi line comment and we're now going to say elseif C is equal to the asterisk.
Then skip the asterisk.
And now we need to peek ahead and see if the next character is equal to a forward slash, because that
means we're ending the comment, right?
If we look at this hello world, we can see that it's the asterisk and then a forward slash when this
ends.
Right.
But it's perfectly valid to have to have a forward slash asterisk here and have as many asterisks as
you want in this comment.
That's why we must check for an asterisk forward slash.
Right.
And that's exactly what we're doing here.
Right?
Liszewski equals asterisk peak.
See forward slash.
So we know that when we find this final forward slash, the comments ended.
So all that we do is, is we, we read the character off and we just do break.
And then at the end of our wild loop we just do return token, create ampersand struct token, and the
type will be token type comment and the string value will be the buffer pointer, of course.
And there we go.
So those two functions will parse both single line comments and multi line comments.
So now we need to write the logic which chooses the correct one.
So how are we going to do that?
We're going to say struct token handle comment.
We're going to say cha c equals pixie if C is equal to a forward slash.
Then we know we're handling a comment.
So we do next SI.
And here we just return null.
So if there's no comment, then we return.
No.
There was no comment to handle.
So now we're going to say if p c equals equals another rate slash.
So again, that means that this is definitely a single line comment now.
Right.
Because we have to forward slashes.
Yeah.
So we're going to go next C to pop that off.
Return token, make one line comment so that selects the correct one and we're going to say ls f pixie
is equal to an asterisk.
Then then this is a multi line comment.
Right?
So again, we're going to pop that off and we're going to go return token, make multi line comment.
Now in the event it's neither of these cases and most likely means we've mistaken a division operator
for a comment.
Now, this is the same reason why our operator case excludes division, which I said I'd explain that
later on and now I am we exclude division because forward slash can also be the start of a comment,
which is why we handle that later.
So if it's neither of these, then we're actually dealing with a division operator, in which case we
need to push it back to the stack and we need to call return token, make operator or String OC, which
will then go through here and create an operator for that division.
And we should copy and paste all of this.
And just put it below tokamak operator or string because we are using that and we can't access it because
because we were above it.
So now we should be able to resolve that perfectly fine because it's above us.
Okay, nice.
So now we just need to call, handle comment.
You know, we break the problem down as much as we can and then it becomes easier to work with.
And compiler development is no exception.
And in fact, you need to break things down in compiler development because if you don't, you're going
to have a lot of problems.
So that's what I'm trying to teach you in this course, how to break down your compiler problems.
So just go to read next token.
And just by here, we're going to say token equals handle common.
Now if handle common returns a token, then we know that we process the comment.
So then we just return token.
Otherwise we'll we'll access the switch case and liquefy the correct token or tokenize the correct input
to a token, I should say so.
Yeah, we should now have comments.
Let's do a make clean and make.
We're going to do main and we can see our one line comment now works absolutely fine.
So if we try to do something like a multi line, that should work too and we can see that works.
So we now have common tokens.