You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pattern matching: match expression with patterns / ranges / type tags
Adds `match scrutinee { pat => stmt, ... }` to OMC. Single biggest
ergonomic gap closed since dicts.
Patterns supported:
- literals (int, float, string, bool, null)
- wildcard `_`
- bind (any bare identifier — captures the value)
- alternation `a | b | c` (reuses existing `|` BitOr token)
- inclusive numeric range `0..9`
- inclusive 1-char string range `"0".."9"`
- type tag (`int`, `string`, `dict`, etc — same names as type_of)
Lexer: new `=>` (FatArrow) and `..` (DotDot) tokens. `match` joins
the keyword set. AST: Statement::Match { scrutinee, arms } with
MatchArm { pattern, body }, Pattern enum.
Interpreter: pattern_matches() helper threads bindings through
recursive match — alternation snapshots the bindings vec and rolls
back on a failed alt so partial bindings don't leak. The matched
arm's bindings are installed via set_var into the surrounding
scope (matching `if`'s no-extra-scope semantics).
VM: compiles to Op::ExecStmt — same tree-walk fallback try/catch
uses. A native lowering would emit guarded jump tables; it's
straightforward (~50 lines per pattern variant) but match isn't
in any hot path yet, so deferred. The existing vm_take_return
machinery handles `return` inside a matched arm cleanly.
Bindings now make this idiomatic in OMC:
fn parse_atom(c) {
match c {
"(" => { return "open"; }
")" => { return "close"; }
"0".."9" => { return "digit"; }
_ => { return "ident"; }
}
}
41/41 functional examples produce identical output under tree-walk
and VM. 92/92 unit tests pass.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
0 commit comments