Files
TransPyC/includes/coro.vp
T
TermiNexus 13a8f6c1b6 feat(coro): 语言内协程 M1——async def + await + coro_run 状态机
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/控制流。
2026-08-21 12:36:00 +08:00

29 lines
761 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import t, c
from stdint import *
import stdlib
@t.CVTable
class Coroutine:
"""协程帧句柄(M1:__ptr__ 持有 create_<name>() 返回的帧指针)"""
__ptr__: t.CPtr
def coro_resume(c: Coroutine) -> t.CInt:
# M1 占位:实际由编译器将 coro.coro_run 内联翻译为 resume_<name> 循环,
# 本函数不会被真正调用(仅保证模块可编译)。
return 0
def coro_result(c: Coroutine) -> t.CInt:
# M1 占位:coro_run 内联翻译直接从帧 ret 槽取结果。
return 0
def coro_run(c: Coroutine) -> t.CInt:
# M1 占位实现:编译器在调用点对 coro.coro_run 做内联翻译(resume 循环)。
s: t.CInt = 0
while s == 0:
s = coro_resume(c)
return coro_result(c)