mirror of
https://github.com/imjasonh/nescript
synced 2026-07-08 00:45:38 +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:
parent
c8ae433a7c
commit
f0264b124a
7 changed files with 180 additions and 0 deletions
|
|
@ -509,6 +509,37 @@ while i < 10 {
|
|||
}
|
||||
```
|
||||
|
||||
### Match Statement
|
||||
|
||||
`match` matches a scrutinee against a sequence of patterns and
|
||||
executes the body of the first matching arm. Each arm's pattern is
|
||||
compared against the scrutinee with `==`. An underscore arm `_` acts
|
||||
as the catch-all:
|
||||
|
||||
```
|
||||
enum State { Title, Playing, GameOver }
|
||||
var state: u8 = Title
|
||||
|
||||
on frame {
|
||||
match state {
|
||||
Title => {
|
||||
if button.start { state = Playing }
|
||||
}
|
||||
Playing => {
|
||||
// ... game logic ...
|
||||
}
|
||||
GameOver => {
|
||||
if button.a { state = Title }
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`match` desugars to an `if` / `else if` chain at parse time, so
|
||||
patterns can be any expression that produces a value comparable to
|
||||
the scrutinee.
|
||||
|
||||
### For Loop
|
||||
|
||||
The `for` loop iterates over a half-open integer range `[start, end)`:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue