munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.42k stars 1.01k forks source link

challenge 10.2 answer modify #1152

Closed ppk03 closed 5 months ago

ppk03 commented 6 months ago

I belive the following code for challenge 10.2:

private boolean checkNext(TokenType tokenType) {
  if (isAtEnd()) return false;
  if (tokens.get(current + 1).type == EOF) return false;
  return tokens.get(current + 1).type == tokenType;
}

would be better if you change it to:

private boolean checkNext(TokenType type) {
  if (isAtEnd()) return false;
  return tokens.get(current + 1).type == type;
}

If someone type "checkNext(EOF)", he can still get correct result.

munificent commented 5 months ago

If someone type "checkNext(EOF)", he can still get correct result.

That's true, but the parser never needs to do that. (It would also be a little strange if there was a place in the grammar where the interpretation of a token changed only if it was literally the last token in the file.)