feat: add pattern variable binding to C backend

Implements full pattern matching with variable binding for the C backend:

- Extract variable bindings from patterns (Var, Constructor, Tuple, Record)
- Infer C types for bound variables using variant field type tracking
- Handle recursive ADTs with pointer fields and heap allocation
- Dereference pointer bindings automatically for value semantics

Key implementation details:
- variant_to_type: Maps variant names to parent type for tag generation
- variant_field_types: Maps (type, variant) to field types for inference
- Recursive type fields use Type* pointers with malloc/memcpy
- Pattern bindings dereference pointers to maintain value semantics

Examples that now work:
- match opt { Some(x) => x, None => 0 }
- match tree { Leaf(n) => n, Node(l, r) => sum(l) + sum(r) }

Updates documentation to reflect C backend progress.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 04:23:44 -05:00
parent 6ec1f3bdbb
commit d284ee58a8
3 changed files with 316 additions and 22 deletions

View File

@@ -181,7 +181,7 @@ fn processAny(x: Int @latest): Int = x // any version
### Planned (Not Yet Fully Implemented)
- **Full C Backend**: Basic functions work, closures/lists/pattern variables pending
- **C Backend Lists**: Closures and pattern matching work, lists pending
- **Auto-migration Generation**: Migration bodies stored, execution pending
---
@@ -234,7 +234,7 @@ Quick iteration with type inference and a REPL.
| Limitation | Description |
|------------|-------------|
| **Limited C Backend** | Basic functions work, closures/lists pending |
| **Limited C Backend** | Functions, closures, ADTs work; lists pending |
| **No Package Manager** | Can't share/publish packages yet |
| **New Paradigm** | Effects require learning new concepts |
| **Small Ecosystem** | No community packages yet |
@@ -370,11 +370,12 @@ Values + Effects C Code → GCC/Clang
- ✅ Test Effect (native testing framework)
- ✅ Console.readLine and Console.readInt
- ✅ C Backend (basic functions, Console.print)
- ✅ C Backend closures and pattern matching
- ✅ Watch mode / hot reload
- ✅ Formatter
**In Progress:**
1. **C Backend Extensions** - Closures, lists, pattern variable binding
1. **C Backend Lists** - List operations pending
2. **Schema Evolution** - Type system integration, auto-migration
3. **Error Message Quality** - Context lines shown, suggestions partial