1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-08 08:55: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:
Claude 2026-04-12 16:55:18 +00:00
parent 8f0e0635df
commit 240da57b54
No known key found for this signature in database
10 changed files with 248 additions and 1 deletions

View file

@ -843,6 +843,7 @@ impl Parser {
}
TokenKind::KwIf => self.parse_if(),
TokenKind::KwWhile => self.parse_while(),
TokenKind::KwFor => self.parse_for(),
TokenKind::KwLoop => self.parse_loop(),
TokenKind::KwBreak => {
let span = self.current_span();
@ -983,6 +984,24 @@ impl Parser {
Ok(Statement::Loop(body, start))
}
fn parse_for(&mut self) -> Result<Statement, Diagnostic> {
let start = self.current_span();
self.expect(&TokenKind::KwFor)?;
let (var, _) = self.expect_ident()?;
self.expect(&TokenKind::KwIn)?;
let start_expr = self.parse_expr()?;
self.expect(&TokenKind::DotDot)?;
let end_expr = self.parse_expr()?;
let body = self.parse_block()?;
Ok(Statement::For {
var,
start: start_expr,
end: end_expr,
body,
span: start,
})
}
fn parse_draw(&mut self) -> Result<Statement, Diagnostic> {
let start = self.current_span();
self.expect(&TokenKind::KwDraw)?;