feat: async/await/spawn + fix else-if lowering bug
Self-hosted compiler (buxc2):
- Add async/await/spawn tokens, parsing, HIR, lowering, C emission
- Fix pointer type emission (*T -> T*) in C backend
- Fix sizeof(Type) parsing with parentheses
- Fix import ::{...} infinite loop guard
- Fix int64 emission by avoiding else-if chain workaround
- Add debug-free cli and hir_lower
Bootstrap compiler (Nim):
- Fix critical else-if lowering bug in hir_lower.nim:
when both elseIfs and else block exist, elseIfs were dropped
causing all else-if chains to collapse to if+else only
Runtime:
- Fix bux_async_await memory corruption (don't free in run)
- Add bux_remove_from_ready for safe cleanup
- Fix forward declaration and deduplicate bux_now_ms
Docs & examples:
- Update async.bux example with proper int64 params
- Update README, LanguageRef, Stdlib, PLAN for async features
- Mark Phase 7.10 bootstrap loop as completed
All 27 examples pass. buxc2 check/build work on all examples.
This commit is contained in:
+63
-1
@@ -18,7 +18,8 @@ This document describes the Bux programming language as implemented by the boots
|
||||
10. [Generics](#generics)
|
||||
11. [Error Handling](#error-handling)
|
||||
12. [Modules and Imports](#modules-and-imports)
|
||||
13. [Operators](#operators)
|
||||
13. [Async/Await](#asyncawait)
|
||||
14. [Operators](#operators)
|
||||
|
||||
---
|
||||
|
||||
@@ -42,6 +43,7 @@ Identifiers start with a letter or underscore, followed by letters, digits, or u
|
||||
func, let, var, const, type, struct, enum, union, interface, extend
|
||||
module, import, pub, extern, if, else, while, do, loop, for, in
|
||||
break, continue, return, match, as, is, null, self, super, sizeof
|
||||
async, await, spawn
|
||||
```
|
||||
|
||||
### String Literals
|
||||
@@ -499,6 +501,66 @@ func PrivateFunc() -> int {
|
||||
|
||||
---
|
||||
|
||||
## Async/Await
|
||||
|
||||
Bux supports stackful coroutines via `async`/`await` with a round-robin scheduler.
|
||||
|
||||
### Declaring Async Functions
|
||||
|
||||
```bux
|
||||
async func Compute() -> int {
|
||||
PrintLine("step 1");
|
||||
bux_async_yield();
|
||||
PrintLine("step 2");
|
||||
return 42;
|
||||
}
|
||||
```
|
||||
|
||||
### Spawning Tasks
|
||||
|
||||
```bux
|
||||
let handle = spawn Compute();
|
||||
```
|
||||
|
||||
### Awaiting Results
|
||||
|
||||
```bux
|
||||
let result: int = handle.await as int;
|
||||
```
|
||||
|
||||
### Full Example
|
||||
|
||||
```bux
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
async func Compute() -> int {
|
||||
PrintLine("Compute: start");
|
||||
bux_async_yield();
|
||||
PrintLine("Compute: done");
|
||||
return 42;
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let h = spawn Compute();
|
||||
let r: int = h.await as int;
|
||||
PrintInt(r);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### Runtime Functions
|
||||
|
||||
| Function | Description |
|
||||
|----------|-------------|
|
||||
| `bux_async_yield()` | Yield control to the scheduler |
|
||||
| `bux_async_spawn(fn)` | Create a new coroutine from a function |
|
||||
| `bux_async_await(handle)` | Block until coroutine completes, return result |
|
||||
| `bux_async_run()` | Run the scheduler (called implicitly from main) |
|
||||
| `bux_async_sleep(ms)` | Sleep for `ms` milliseconds (non-blocking) |
|
||||
| `bux_async_return(value, size)` | Copy return value into task result buffer |
|
||||
|
||||
---
|
||||
|
||||
## Operators
|
||||
|
||||
### Arithmetic
|
||||
|
||||
+38
-1
@@ -276,6 +276,43 @@ These C functions are provided by `runtime.c` and are available via `extern` dec
|
||||
|
||||
---
|
||||
|
||||
## Std::Async
|
||||
|
||||
Low-level async runtime for stackful coroutines. These functions are used by the `async`/`await` language features.
|
||||
|
||||
### Functions
|
||||
|
||||
| Function | Signature | Description |
|
||||
|----------|-----------|-------------|
|
||||
| `bux_async_spawn` | `func bux_async_spawn(fn: *void) -> *void` | Create a new coroutine from a function pointer |
|
||||
| `bux_async_yield` | `func bux_async_yield()` | Yield control to the scheduler |
|
||||
| `bux_async_await` | `func bux_async_await(handle: *void) -> *void` | Block until coroutine completes, return result pointer |
|
||||
| `bux_async_run` | `func bux_async_run()` | Run the round-robin scheduler |
|
||||
| `bux_async_sleep` | `func bux_async_sleep(ms: int64)` | Non-blocking sleep for `ms` milliseconds |
|
||||
| `bux_async_return` | `func bux_async_return(value: *void, size: int64)` | Copy return value into task result buffer |
|
||||
| `bux_now_ms` | `func bux_now_ms() -> int64` | Monotonic clock in milliseconds |
|
||||
|
||||
### Example
|
||||
|
||||
```bux
|
||||
extern func bux_async_yield();
|
||||
extern func bux_async_spawn(fn: *void) -> *void;
|
||||
extern func bux_async_await(handle: *void) -> *void;
|
||||
|
||||
async func Compute() -> int {
|
||||
bux_async_yield();
|
||||
return 42;
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let h = spawn Compute();
|
||||
let r: int = h.await as int;
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Future Modules
|
||||
|
||||
- `Std::Result` — Shipped via algebraic enums ✅
|
||||
@@ -284,4 +321,4 @@ These C functions are provided by `runtime.c` and are available via `extern` dec
|
||||
- `Std::Os` — `Args`, `Env`, `Exit`, `Cwd`
|
||||
- `Std::Fmt` — String formatting with interpolation
|
||||
- `Std::Iter` — Iterator trait and combinators
|
||||
- `Std::Task` / `Std::Channel` — Lightweight concurrency
|
||||
- `Std::Task` / `Std::Channel` — Lightweight concurrency (pthread-based threads)
|
||||
Reference in New Issue
Block a user