TPC 新增协程生成器 AsyncFunctionHandle(lib/core/Handles/HandlesAsyncFunctions.py):
async def 编译为帧结构体 {state, done, ret, 局部变量槽} + create_<name>()(malloc
分配)+ resume_<name>()(switch 状态机,0=SUSPEND/1=DONE)。await 按顶层赋值
切分函数体:创建子帧 -> resume -> 取子帧 ret -> 存父帧槽 -> 父挂起(state 前进);
普通调用 async 函数生成 create(返回帧指针);coro.coro_run 在调用点内联翻译为
resume 循环。vast parser 已支持语法;新增 includes/coro.vp(Coroutine + 占位
运行时)与 Test/CoroTest(期望输出 coro result = 14)。
M1 限制:async 无参数、返回 i32/int;协程体仅顶层 AnnAssign/Assign/Expr/Return;
await 仅顶层赋值形式、子协程假设一次完成(无嵌套挂起);coro_run 依赖本模块
先赋值的协程变量。M2 将支持参数/嵌套 await/控制流。
22 lines
339 B
Plaintext
22 lines
339 B
Plaintext
import t, c
|
|
from stdint import *
|
|
import stdio
|
|
import coro
|
|
|
|
|
|
async def leaf() -> int:
|
|
return 7
|
|
|
|
|
|
async def task() -> int:
|
|
a: int = await leaf()
|
|
b: int = await leaf()
|
|
return a + b
|
|
|
|
|
|
def main() -> t.CInt:
|
|
tt: coro.Coroutine = task()
|
|
r: t.CInt = coro.coro_run(tt)
|
|
stdio.printf("coro result = %d\n", r)
|
|
return 0
|