munificent / craftinginterpreters

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

Q: what about compound binary operators? #1092

Closed nullndr closed 1 year ago

nullndr commented 1 year ago

I'm trying to parse the binary not in and is not Python operators.

I successfully added a check in the scanner in this way:

TokenType concatenatedToken;

switch (scanner.start[0]) {
...
case 'n': {
  concatenatedToken = check_kw(s, 1, 2, "ot", Token_NOT);
  break;
}
// the same for `is not`.
...
}

if (concatenatedToken == Token_NOT || concatenatedToken == Token_IS) {
  skip_whitespace(s);
  switch (scanner.current[0]) {
  case 'i':
    return check_conc_kw(s, 1, 1, "n", Token_NOTIN);
  case 'n':
    return check_conc_kw(s, 1, 2, "ot", Token_ISNOT);
  }
}

check_conc_kw() is the same as checkKeyword() except for the fact that use scanner.current (Not sure if this is the best way, but it works).

Now I'm not sure how to continue in the compiler, should I refactor the advance() function in order to check while parsing or should I make some modification in parsePrecedence()?

Note that I have a token for both not in (Token_NOTIN) and is not (Token_ISNOT).

nullndr commented 1 year ago

Oh, nevermind, I did it