perlang-org / perlang

The Perlang Programming Language
https://perlang.org
MIT License
16 stars 1 forks source link

Rewrite `for` handling to use a dedicated `Stmt.For` type #403

Closed perlun closed 1 month ago

perlun commented 1 year ago

Moved to GitLab

Please continue to the new version of this issue here: https://gitlab.perlang.org/perlang/perlang/-/issues/403. The GitHub page you are currently reading will not contain the latest information on this issue.


Background

The Lox language implements for loops using existing statements, like Stmt.While (https://craftinginterpreters.com/control-flow.html#for-loops). The reason for this is because:

  1. for loops doesn't really need a statement of its own. It can be perfectly represented using existing building blocks already supported in the language.
  2. (My guess) This gave @munificent the chance to explain syntactic sugar to the reader, and how to implement it for a practical use case like this.

While this was fine while we were a simple interpreter, it is becoming less suitable now that we are becoming a compiler. Particularly this: when we emit C++ code, it makes more sense to let the C++ code include a literal forloop than a more low-level construct, in case the C++ code needs to be read and understood by humans. (Machines don't care, they just execute whatever we tell them to execute.)

Proposed solution

Add a Stmt.For type, which wraps a for statements. Add support for it both our interpreted and compiled modes of operation.

munificent commented 1 year ago

2. (My guess) This gave @munificent the chance to explain syntactic sugar to the reader, and how to implement it for a practical use case like this.

Your guess is correct. :)

perlun commented 1 year ago

Thanks. :blush: :+1: