Skip to content

Commit 505d1d4

Browse files
committed
docs: Add pattern matching documentation for switch expressions
Update documentation to cover the newly implemented pattern matching features for switch expressions including grammar commands and usage examples. - Add Pattern Matching section to book/05-semantic-actions.md with all pattern commands (literal, wildcard, range, struct, enum, etc.) - Add comprehensive Switch Expressions section to book/08-zig-example.md - Update README.md with pattern matching in language features - Update BACKLOG.md with completed pattern matching status
1 parent 7224813 commit 505d1d4

4 files changed

Lines changed: 385 additions & 4 deletions

File tree

BACKLOG.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
**Recent Progress**:
77

8+
-**Switch Expression Pattern Matching Complete** (November 27, 2025)
9+
- Multi-case switch with literal patterns and else clause
10+
- Tagged union patterns (`.some`, `.none`) with graceful non-enum handling
11+
- Error patterns (`error.OutOfMemory`)
12+
- Grammar support for all pattern types (literal, wildcard, range, struct, enum, array, pointer, error)
13+
- Documentation updated (`book/05-semantic-actions.md`, `book/08-zig-example.md`)
814
-**LLVM Backend Working** (AOT and JIT modes fully functional - functions, structs, generics)
915
- Parameter value mapping for function calls
1016
- Struct initialization with InsertValue
@@ -285,16 +291,32 @@ Stack-unwinding exceptions for Haxe/Java-style languages. Required for Reflaxe i
285291
- [x] Switch expressions with literal and wildcard patterns
286292
- [x] `test_pattern_match_runtime_execution` passing (Some/None)
287293
- [x] `test_zig_jit_switch_expression` passing
294+
- [x] **Multi-case switch expressions** (multiple literal cases with else)
295+
- [x] **Tagged union patterns** (`.some`, `.none` - graceful handling for non-enum scrutinee)
296+
- [x] **Error patterns** (`error.OutOfMemory`)
297+
- [x] **Grammar support for all pattern types**:
298+
- `literal_pattern` - Match exact values
299+
- `wildcard_pattern` - Match anything (`_` or `else`)
300+
- `range_pattern` - Match value ranges (`1..10`)
301+
- `identifier_pattern` - Bind matched value to variable
302+
- `struct_pattern` - Match struct fields
303+
- `field_pattern` - Match individual struct field
304+
- `enum_pattern` - Match tagged union variant
305+
- `array_pattern` - Match array elements
306+
- `pointer_pattern` - Match through pointer dereference
307+
- `error_pattern` - Match error values
308+
- [x] **Documentation updated** (`book/05-semantic-actions.md`, `book/08-zig-example.md`)
288309

289310
**Known Issue** (minor):
311+
290312
- [ ] None literal (arena symbol resolution - Symbol 7 doesn't resolve)
291313
- Root cause: Parser and SSA arena symbol lookup mismatch
292314
- Impact: Minor - workaround exists using if-let on None branch
293315

294316
**Pending Features**:
317+
295318
- [ ] Or patterns (`Some(x) | None`)
296319
- [ ] Pattern guards (`if` conditions)
297-
- [ ] Range patterns (`1..=10`)
298320
- [ ] Slice patterns (`[first, .., last]`)
299321

300322
---

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,11 @@ Goodbye!
253253
### Available Node Types
254254

255255
- **Literals**: `int_literal`, `float_literal`, `string_literal`, `bool_literal`, `char_literal`
256-
- **Expressions**: `variable`, `binary_op`, `unary_op`, `call_expr`, `method_call`, `field_access`, `index`, `array`, `struct_literal`, `cast`, `lambda`
256+
- **Expressions**: `variable`, `binary_op`, `unary_op`, `call_expr`, `method_call`, `field_access`, `index`, `array`, `struct_literal`, `cast`, `lambda`, `switch_expr`
257257
- **Statements**: `let_stmt`, `assignment`, `return_stmt`, `if_stmt`, `while_stmt`, `for_stmt`, `break_stmt`, `continue_stmt`, `expression_stmt`, `block`
258258
- **Declarations**: `function`, `param`, `program`
259259
- **Types**: `primitive_type`, `pointer_type`, `array_type`, `named_type`, `function_type`
260+
- **Patterns**: `literal_pattern`, `wildcard_pattern`, `range_pattern`, `identifier_pattern`, `struct_pattern`, `field_pattern`, `enum_pattern`, `array_pattern`, `pointer_pattern`, `error_pattern`, `switch_case`
260261

261262
See [Zyn Grammar Specification](./docs/ZYN_GRAMMAR_SPEC.md) for complete documentation.
262263

@@ -466,7 +467,7 @@ cargo build --release
466467
- Basic arithmetic (`+`, `-`, `*`, `/`)
467468
- Function calls (including recursive)
468469
- Local variables with stack allocation
469-
- Pattern matching (basic)
470+
- Switch expressions with pattern matching (literals, wildcards, ranges, structs, enums, errors)
470471
- Async/await syntax and runtime
471472

472473
#### ✅ Tiered JIT Compilation
@@ -549,11 +550,13 @@ See [BACKLOG.md](BACKLOG.md) for detailed tasks.
549550
- ✅ Zig-style error handling (try/catch/orelse on error unions)
550551
- ✅ Pattern matching (if let, switch, Some/None/Ok/Err)
551552
- ✅ Generic functions with monomorphization
553+
- ✅ Switch expressions with multi-case patterns and else clause
554+
- ✅ Pattern matching grammar (literals, wildcards, ranges, structs, enums, errors, pointers)
552555

553556
### Q1 2026: Production Features
554557

555558
- ✅ LLVM AOT/JIT backend core complete (functions, structs, generics, control flow)
556-
- 🔄 LLVM backend: advanced features (switch, pattern matching, error unions)
559+
- ✅ Switch expression pattern matching in LLVM backend
557560
- 🔄 Haxe-style exception handling (throw/catch/finally with stack unwinding)
558561
- 🔄 Complete I/O and networking standard library
559562
- 🔄 String operations (needs stdlib integration via plugin system)

book/05-semantic-actions.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,99 @@ primitive_type = { "i32" | "i64" | "bool" | "void" }
275275
}
276276
```
277277

278+
#### Pattern Matching
279+
280+
These commands create pattern nodes for switch expressions and pattern matching:
281+
282+
| Method | Arguments | Creates |
283+
|--------|-----------|---------|
284+
| `literal_pattern` | `value` | Match a literal value (int, string) |
285+
| `wildcard_pattern` | | Match anything (`_` or `else`) |
286+
| `range_pattern` | `start`, `end`, `inclusive` | Match a range (`1..10`) |
287+
| `identifier_pattern` | `name` | Bind matched value to variable |
288+
| `struct_pattern` | `name`, `fields` | Match struct with field patterns |
289+
| `field_pattern` | `name`, `pattern`? | Match a struct field |
290+
| `enum_pattern` | `name`, `variant`, `fields` | Match enum/tagged union variant |
291+
| `array_pattern` | `elements` | Match array elements |
292+
| `pointer_pattern` | `inner`, `mutable` | Match pointer dereference |
293+
| `error_pattern` | `name` | Match error value (`error.OutOfMemory`) |
294+
| `switch_expr` | `scrutinee`, `cases` | Switch expression |
295+
| `switch_case` | `pattern`, `body` | Single switch case arm |
296+
297+
```zyn
298+
// Literal pattern: match exact value
299+
switch_literal_pattern = { integer_literal }
300+
-> TypedExpression {
301+
"commands": [
302+
{ "define": "literal_pattern", "args": { "value": "$1" } }
303+
]
304+
}
305+
306+
// Wildcard pattern: match anything
307+
switch_wildcard_pattern = { "_" }
308+
-> TypedExpression {
309+
"commands": [
310+
{ "define": "wildcard_pattern" }
311+
]
312+
}
313+
314+
// Range pattern: match value in range
315+
switch_range_pattern = { integer_literal ~ ".." ~ integer_literal }
316+
-> TypedExpression {
317+
"commands": [
318+
{ "define": "range_pattern", "args": {
319+
"start": { "define": "literal_pattern", "args": { "value": "$1" } },
320+
"end": { "define": "literal_pattern", "args": { "value": "$2" } },
321+
"inclusive": false
322+
}}
323+
]
324+
}
325+
326+
// Struct pattern: match struct fields
327+
switch_struct_pattern = { identifier ~ "{" ~ struct_field_patterns? ~ "}" }
328+
-> TypedExpression {
329+
"commands": [
330+
{ "define": "struct_pattern", "args": {
331+
"name": { "text": "$1" },
332+
"fields": "$2"
333+
}}
334+
]
335+
}
336+
337+
// Tagged union pattern: .some, .none
338+
switch_tagged_union_pattern = { "." ~ identifier }
339+
-> TypedExpression {
340+
"commands": [
341+
{ "define": "enum_pattern", "args": {
342+
"name": "",
343+
"variant": { "text": "$1" },
344+
"fields": []
345+
}}
346+
]
347+
}
348+
349+
// Error pattern: error.OutOfMemory
350+
switch_error_pattern = { "error" ~ "." ~ identifier }
351+
-> TypedExpression {
352+
"commands": [
353+
{ "define": "error_pattern", "args": {
354+
"name": { "text": "$1" }
355+
}}
356+
]
357+
}
358+
359+
// Pointer pattern: *x
360+
switch_pointer_pattern = { "*" ~ switch_pattern }
361+
-> TypedExpression {
362+
"commands": [
363+
{ "define": "pointer_pattern", "args": {
364+
"inner": "$1",
365+
"mutable": false
366+
}}
367+
]
368+
}
369+
```
370+
278371
## The `fold_binary` Command
279372

280373
This special command builds left-associative binary expression trees from repetition patterns.

0 commit comments

Comments
 (0)