docs: update ROADMAP and PHASE8 for closures with captures

This commit is contained in:
2026-06-09 21:23:58 +03:00
parent 9b473c667c
commit 6414ef236b
2 changed files with 23 additions and 11 deletions
+1
View File
@@ -295,6 +295,7 @@ Crates.io е непреодолимо предимство. Ние се конк
-`bux new`, `bux init`, `bux test`, `bux fmt` — готово -`bux new`, `bux init`, `bux test`, `bux fmt` — готово
- ✅ Basic borrow checker (`@[Checked]`) — готово - ✅ Basic borrow checker (`@[Checked]`) — готово
- ✅ Closures (capture-less anonymous functions) — готово - ✅ Closures (capture-less anonymous functions) — готово
- ✅ Closures with captures — готово
- 🎯 Target: Можеш да напишеш `bux` package manager на Bux - 🎯 Target: Можеш да напишеш `bux` package manager на Bux
### Milestone B: "Използваем за systems programming" (2 месеца) ### Milestone B: "Използваем за systems programming" (2 месеца)
+22 -11
View File
@@ -128,23 +128,34 @@ let s = HttpResponse(404, body: "err"); // positional + named mixed
--- ---
### 6. Closures / Anonymous Functions ### 6. Closures / Anonymous Functions
**Why:** Callbacks, iterators, functional APIs. Currently only named functions exist. **Status:** ✅ Implemented in both bootstrap and selfhost. Capture-less and with captures.
**Syntax:** **Syntax:**
```bux ```bux
// Capture-less closure
let add: func(int, int) -> int = |a, b| { return a + b; }; let add: func(int, int) -> int = |a, b| { return a + b; };
let nums: Array<int> = Array_New<int>();
// Closure with captures
let base: int = 10;
let adder: func(int) -> int = |a: int| -> int { return a + base; };
let result: int = adder(5); // 15
// Pass closure to higher-order function
Array_Filter(nums, |x| { return x > 10; }); Array_Filter(nums, |x| { return x > 10; });
``` ```
**Implementation Steps:** **Implementation:**
1. New AST node: `ClosureExpr` with `params`, `body`, `captures` - Capture-less closures: generate global thunk function, return `&thunk` as function pointer.
2. Parser: parse `|param1, param2| -> Type { body }` - Closures with captures:
3. Type system: closure type as `func(Args) -> Ret` + implicit capture struct 1. Sema: `Scope_LookupUpTo` identifies captured variables from outer scope.
4. C backend: generate struct with captured vars + function pointer 2. HIR Lower: generate `__closure_env_N` struct + global instance `__closure_env_instance_N`.
5. Lifetime: ensure captures outlive closure usage 3. At closure creation site: emit capture assignments (`env_instance.x = x;`).
4. In thunk body: rewrite captured identifiers to `env_instance.x` via `hFieldAccess`.
5. C backend: emit env struct definition + global instance before thunk function.
**Complexity:** High — touches parser, sema, type system, C backend. **Limitations:** One global instance per closure AST node (no multiple instances). No loop/return support in closures yet.
**Complexity:** High — touches parser, sema, type system, HIR/LIR backend.
--- ---
@@ -237,6 +248,6 @@ Task::Spawn(Worker, rx);
6.**Basic borrow checker (`@[Checked]`)** — Done (selfhost) 6.**Basic borrow checker (`@[Checked]`)** — Done (selfhost)
7.**`bux fmt`, `bux test`, `bux new`, `bux init`** — Done (selfhost) 7.**`bux fmt`, `bux test`, `bux new`, `bux init`** — Done (selfhost)
8.**Closures (capture-less)** — Done. Enables callbacks and higher-order functions. 8.**Closures (capture-less)** — Done. Enables callbacks and higher-order functions.
9. **Closures with captures**Needs capture analysis + environment struct. **← NEXT** 9. **Closures with captures**Done. Global env struct per closure AST node.
10. **`for x in collection`** — Depends on closures with captures or trait system. 10. **`for x in collection`** — Depends on closures with captures or trait system. **← NEXT**
11. **Destructors / Drop** — High complexity, needs ownership + move semantics. 11. **Destructors / Drop** — High complexity, needs ownership + move semantics.