1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-08 17:06:04 +00:00

Language: match statement

match state {
        Title => { if button.start { state = Playing } }
        Playing => { /* ... */ }
        GameOver => { if button.a { state = Title } }
        _ => {}
    }

- Lexer: \`match\` keyword and \`=>\` (FatArrow) token
- Parser: \`parse_match\` after the existing loop constructs. Each
  arm is \`pattern => { body }\`, with \`_\` as the catch-all. The
  match scrutinee is parsed with struct-literal restriction enabled
  so the following \`{\` is unambiguously the match body, not a
  struct literal.
- The parser desugars match directly into an if/else-if chain so
  the analyzer, IR lowering, and codegen don't need new AST variants
  — each arm becomes \`scrutinee == pattern\` as the condition, and
  the default arm (if any) becomes the final \`else\` block.

Tests cover parse + full pipeline integration for state-style
dispatch using an enum.

https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
Claude 2026-04-12 17:21:00 +00:00
parent c8ae433a7c
commit f0264b124a
No known key found for this signature in database
7 changed files with 180 additions and 0 deletions

View file

@ -228,6 +228,36 @@ fn program_with_for_loop() {
rom::validate_ines(&rom_data).expect("should be valid iNES");
}
#[test]
fn program_with_match_statement() {
// Note: the parser doesn't support `;` as a statement separator,
// so each arm body uses newlines between statements.
let source = r#"
game "Match" { mapper: NROM }
enum Mode { Idle, Run, Jump }
var mode: u8 = Idle
var x: u8 = 0
on frame {
match mode {
Idle => { if button.a { mode = Run } }
Run => {
x += 1
if button.b { mode = Jump }
}
Jump => {
x += 2
if button.a { mode = Idle }
}
_ => {}
}
wait_frame
}
start Main
"#;
let rom_data = compile(source);
rom::validate_ines(&rom_data).expect("should be valid iNES");
}
#[test]
fn program_with_struct_literals() {
let source = r#"