- Mark capture-less closures as ✅ Done in ROADMAP
- Add closure syntax examples to LanguageRef.md Functions section
- Update PHASE8_STRATEGY.md Milestone A with closures
6.8 KiB
Bux Language Roadmap — New Constructs
Updated: 2026-06-08 | Status: In Progress
This document tracks planned language constructs beyond Phase 8 strategy.
✅ Done
1. defer Statement
Status: ✅ Implemented in both bootstrap and selfhost.
Syntax:
func ReadFile(path: String) -> String {
let fd: int = Open(path);
defer Close(fd); // runs on any exit from scope
defer PrintLine("done"); // LIFO order
let data: String = ReadAll(fd);
return data; // both defers run before return
}
2. Native switch / case
Status: ✅ Implemented in both bootstrap and selfhost. Desugars to if-else chain.
Syntax:
switch statusCode {
case 200: PrintLine("OK");
case 404: PrintLine("Not Found");
case 500: PrintLine("Server Error");
default: PrintLine("Unknown");
}
3. Operator Overloading
Status: ✅ Implemented in bootstrap. Selfhost has no method-table yet (not needed for selfhost-loop parity).
Supported operators:
func Vec2_operator_add(self: *Vec2, other: Vec2) -> Vec2 { ... }
func Vec2_operator_sub(self: *Vec2, other: Vec2) -> Vec2 { ... }
func Vec2_operator_eq(self: *Vec2, other: Vec2) -> bool { ... }
func Vec2_operator_lt(self: *Vec2, other: Vec2) -> bool { ... }
func MyArray_operator_index_get(self: *MyArray, idx: int) -> int { ... }
func MyArray_operator_index_set(self: *MyArray, idx: int, value: int) { ... }
Notes:
- Works via method-table lookup in sema + hir_lower.
- Generic method instantiation supported.
- Short-circuit operators (
&&,||) remain builtin.
4. String Interpolation
Status: ✅ Implemented in bootstrap (selfhost reserves AST node).
Syntax:
let name: String = "Bux";
let msg: String = f"Hello, {name}!";
let num: int = 42;
let msg2: String = f"Count: {num}";
Notes:
f"..."prefix enables interpolation.- Escaped braces:
\{and\}. - Auto-converts
int,uint,float,bool, andStringinside braces.
5. Named / Default Parameters
Status: ✅ Implemented in both bootstrap and selfhost.
Syntax:
func HttpResponse(code: int = 200, body: String = "") -> Response { ... }
let r: Response = HttpResponse(body: "hello"); // code=200
let s = HttpResponse(404, body: "err"); // positional + named mixed
Notes:
- Bootstrap parser already parsed defaults; added named-arg parsing and sema injection.
- Selfhost parser parses
= defaultExprin params andname: valueat call sites. - Sema injects default expressions and reorders named args into param order.
6. CLI Commands (bux new, bux init, bux test, bux fmt)
Status: ✅ Implemented in selfhost.
| Command | Description |
|---|---|
bux new <name> |
Create a new project directory with bux.toml and src/Main.bux |
bux init |
Initialize a Bux project in the current directory |
bux test [dir] |
Build and run the project binary, reporting pass/fail |
| `bux fmt <file | dir>` |
7. Basic Borrow Checker (@[Checked])
Status: ✅ Implemented in selfhost.
Features:
@[Checked]attribute enables per-function borrow checking.&T(shared reference) and&mut T(mutable reference) type syntax.- Rejects write-through raw pointer (
*T) in checked functions. - Detects double mutable borrow (
Swap(&mut x, &mut x)). - Tracks use-after-move for
own Tvalues.
P0 — Critical (Unlocks Major Use Cases)
8. Full Selfhost Bootstrap Loop
Why: The selfhost compiler must compile itself deterministically.
Status: 🔄 In progress — borrow checker works, but some features still missing in selfhost vs bootstrap.
6. Closures / Anonymous Functions
Why: Callbacks, iterators, functional APIs. Currently only named functions exist.
Syntax:
let add: func(int, int) -> int = |a, b| { return a + b; };
let nums: Array<int> = Array_New<int>();
Array_Filter(nums, |x| { return x > 10; });
Implementation Steps:
- New AST node:
ClosureExprwithparams,body,captures - Parser: parse
|param1, param2| -> Type { body } - Type system: closure type as
func(Args) -> Ret+ implicit capture struct - C backend: generate struct with captured vars + function pointer
- Lifetime: ensure captures outlive closure usage
Complexity: High — touches parser, sema, type system, C backend.
7. for x in collection Iterator Loops
Why: Currently only for i in 0..10 works. No way to iterate arrays/channels/maps.
Syntax:
for item in arr {
PrintLine(item);
}
for msg in channel {
Process(msg);
}
Implementation Steps:
- Parser: extend
forto acceptfor <ident> in <expr> { ... } - Desugar to while loop using
Iter_HasNext/Iter_Nextor trait-based iterator - C backend: generate standard while loop with iterator state
Complexity: Medium — needs either trait system enhancement or hardcoded desugaring.
P1 — High Impact
8. Destructors / Drop Trait
Why: own T exists but nothing cleans up automatically. Complements defer.
Syntax:
extend Array<T> {
func Drop(self: own Array<T>) {
Array_Free(self);
}
}
Implementation Steps:
- Define
Dropinterface in stdlib - C backend: emit
Drop(value)before variable goes out of scope - Respect move semantics — don't drop moved values
Complexity: High — needs ownership tracking + move semantics.
P2 — Nice to Have
9. Trait System Enhancement
Why: Currently have interface + extend (basic). Need trait bounds, associated types.
Syntax:
func Sort<T: Comparable>(arr: &mut Array<T>) { ... }
10. CTFE (Compile-Time Function Execution)
Why: Precomputed tables for embedded / kernel dev.
Syntax:
const func Fib(n: int) -> int { ... }
const TABLE_SIZE = Fib(20);
11. Concurrency
Why: Go-style goroutines + channels, but without GC.
Syntax:
let (tx, rx) = Channel::New<int>();
Task::Spawn(Worker, rx);
Recommended Order
- ✅
defer— Done - ✅
switch/case— Done - ✅ Operator overloading — Done (bootstrap)
- ✅ String interpolation — Done (bootstrap)
- ✅ Named/default parameters — Done
- ✅ Basic borrow checker (
@[Checked]) — Done (selfhost) - ✅
bux fmt,bux test,bux new,bux init— Done (selfhost) - ✅ Closures (capture-less) — Done. Enables callbacks and higher-order functions.
- Closures with captures — Needs capture analysis + environment struct. ← NEXT
for x in collection— Depends on closures with captures or trait system.- Destructors / Drop — High complexity, needs ownership + move semantics.