mirror of
https://github.com/imjasonh/nescript
synced 2026-07-08 00:45:38 +00:00
Language: for i in start..end loops
Adds a \`for NAME in START..END { BODY }\` half-open range loop:
for i in 0..8 {
total += arr[i]
}
- Lexer: \`for\`, \`in\` keywords and the \`..\` range operator
- AST: new \`Statement::For\` variant with var/start/end/body
- Parser: \`parse_for\` after \`while\` / \`loop\`
- Analyzer: registers the loop variable as a u8 symbol for the body
(restoring any shadowed outer symbol afterwards), allocates it via
the normal RAM allocator, and tracks it as "used"
- IR lowering: desugars to \`var = start; while var < end { body;
var = var + 1 }\` using a \`for_step\` continue-edge block so
\`continue\` properly increments the index
- AST codegen: no-op (legacy path doesn't need for loops)
- Tests: parse + full-pipeline integration
https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
parent
8f0e0635df
commit
240da57b54
10 changed files with 248 additions and 1 deletions
|
|
@ -484,6 +484,24 @@ while i < 10 {
|
|||
}
|
||||
```
|
||||
|
||||
### For Loop
|
||||
|
||||
The `for` loop iterates over a half-open integer range `[start, end)`:
|
||||
|
||||
```
|
||||
for i in 0..8 {
|
||||
total += arr[i]
|
||||
}
|
||||
```
|
||||
|
||||
The loop variable is a `u8` scoped to the loop body. Both bounds can
|
||||
be any expression that evaluates to `u8` at runtime, including
|
||||
constants or variables. The range is half-open, so `0..8` iterates
|
||||
`0, 1, 2, ..., 7` (8 iterations). For a closed range, use `0..9`.
|
||||
|
||||
The loop is desugared into a `while` loop with an index variable, so
|
||||
`break` and `continue` work the same as in any loop body.
|
||||
|
||||
### Loop (Infinite)
|
||||
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue