Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zxul767 opened this issue Nov 7, 2022 · 0 comments
Open

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

zxul767 opened this issue Nov 7, 2022 · 0 comments

Comments

@zxul767
Copy link
Owner

zxul767 commented Nov 7, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant