feat(tpv): 语言内协程 M1-M7 移植——async/await 状态机 + 异常传播 + 帧池

- M1: async def -> 帧结构体 + create_/resume_ 状态机 + coro.coro_run 内联
- M2: async 参数、嵌套 await 提升 __await_N、If/While 内 await
- M3: For 内 await(range->While)、coro_sleep 轮询挂起、coro_destroy 回池
- M4: raise 存码、await 子帧异常传播、coro_exception、coro_gather
- M5/M6: try/except 链式匹配 + as e 绑定 + 异常类型码
- M7: try/finally、异常消息 msg 槽、自定义异常类、帧池复用
This commit is contained in:
TermiNexus committed 2026-08-22 00:34:32 +08:00
1 parent f0a70748d7
commit 82a86761b9
11 files changed
+7722 -4575

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
import t, c
from stdint import *
import stdio
import coro
# TPV-M3~M7 协程综合测试:
# M3: For 内 await(range→While)、coro_sleep 挂起、coro_destroy 帧回池
# M4: raise 存码、await 子帧异常传播、coro_exception 查询
# M5/M6: try/except(链式匹配 + as e 绑定异常码 + 类型码)
# M7: try/finally、异常消息 msg 槽、自定义异常类、帧池复用
class MyError(Exception):
pass
async def leaf(x: int) -> int:
return x * 2
async def task(n: int) -> int:
s: int = 0
for i in range(3): # M3:For 内 await
s = s + await leaf(i)
if s > 4:
await coro.coro_sleep(5) # M3:coro_sleep 挂起(wake 槽 + 轮询)
try:
raise ValueError("boom") # M4/M6:raise 类型 + M7 消息
except ValueError as e: # M5/M6:as e 绑定异常码
s = s + e
return s
async def raiser() -> int:
try:
raise MyError("fin_msg") # M7:自定义异常类 + 消息
finally:
x: int = 99 # M7:finally 正常路径执行
return 0
def main() -> int:
tt: t.CPtr = task(3)
r: int = coro.coro_run(tt)
e: int = coro.coro_exception(tt)
stdio.printf("coro result = %d exc = %d\n", r, e)
coro.coro_destroy(tt) # M3/M7:destroy 回池
t1: t.CPtr = raiser()
r1: int = coro.coro_run(t1)
e1: int = coro.coro_exception(t1)
m1: t.CPtr = coro.coro_exception_msg(t1)
stdio.printf("coro result = %d exc = %d msg = %s\n", r1, e1, m1)
coro.coro_destroy(t1)
t2: t.CPtr = raiser() # 帧池复用
r2: int = coro.coro_run(t2)
e2: int = coro.coro_exception(t2)
stdio.printf("coro result = %d exc = %d\n", r2, e2)
coro.coro_destroy(t2)
return 0