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.
(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.
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, likeStmt.While
(https://craftinginterpreters.com/control-flow.html#for-loops). The reason for this is because: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.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
for
loop 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 afor
statements. Add support for it both our interpreted and compiled modes of operation.