zxul767 / lox

An interpreter for the Lox language
1 stars 0 forks source link

refactor parsing functions in `clox` to be self-contained #11

Open zxul767 opened 1 year ago

zxul767 commented 1 year ago

the general current pattern of parsing functions in clox is as follows:

...
if (match(TOKEN_FOR)) { // consumes current token if it matches
   for_statement(...)
}
...
void for_statement(...) {
   consume(TOKEN_LEFT_PAREN);
   ...
}

i believe this can a be bit confusing since the parsing of the whole construct (in this example the for statement) is spread over two places (or more in some cases).

i think it might be better to structure parsing functions so that they are self-contained, as follows:

...
if (current_is(TOKEN_FOR)) {
   for_statement(...)
}
...
void for_statement(...) {
   consume(TOKEN_FOR);
   consume(TOKEN_LEFT_PAREN);
   ...
}

this restructuring adds no additional runtime overhead but can make code more straightforward to grok since all logical steps for parsing a construct are in its corresponding function.