修复了 TPV 的一些错误,包括闭包等
This commit is contained in:
@@ -81,47 +81,81 @@ def translate_children(trans: HT.Translator | t.CPtr,
|
||||
|
||||
cn_count: t.CSizeT = ch.__len__()
|
||||
added_total: int = 0
|
||||
stdio.printf("[TC] cn_count=%d\n", cn_count)
|
||||
stdio.fflush(0)
|
||||
|
||||
for ci in range(cn_count):
|
||||
child: ast.AST | t.CPtr = ch.get(ci)
|
||||
if child is None: continue
|
||||
kd: int = child.kind()
|
||||
stdio.printf("[TC] ci=%d kd=%d\n", ci, kd)
|
||||
stdio.fflush(0)
|
||||
|
||||
if kd == ast.ASTKind.Import:
|
||||
stdio.printf("[TC] ci=%d 处理 Import\n", ci)
|
||||
stdio.fflush(0)
|
||||
trans.ImportsH.HandleImport(child)
|
||||
stdio.printf("[TC] ci=%d Import 后\n", ci)
|
||||
stdio.fflush(0)
|
||||
elif kd == ast.ASTKind.ImportFrom:
|
||||
stdio.printf("[TC] ci=%d 处理 ImportFrom\n", ci)
|
||||
stdio.fflush(0)
|
||||
trans.ImportsH.HandleImportFromModule(child)
|
||||
trans.ImportsH.HandleImportFromNames(child)
|
||||
stdio.printf("[TC] ci=%d ImportFrom 后\n", ci)
|
||||
stdio.fflush(0)
|
||||
elif kd == ast.ASTKind.FunctionDef:
|
||||
# Phase 1a 声明模式:只注册 CExport/State 函数到全局表(解决翻译顺序依赖)
|
||||
# Phase 1b 全量翻译:正常翻译函数体
|
||||
fd_dbg: ast.FunctionDef | t.CPtr = (ast.FunctionDef | t.CPtr)(child)
|
||||
fd_name_dbg: str = "?" if fd_dbg is None or fd_dbg.name is None else fd_dbg.name
|
||||
stdio.printf("[TC] ci=%d FunctionDef name=%s _declare_only=%d 前\n", ci, fd_name_dbg, trans._declare_only)
|
||||
stdio.fflush(0)
|
||||
if trans._declare_only == 0:
|
||||
added: int = HandlesFunctions.translate_function_def(trans, child)
|
||||
added_total += added
|
||||
elif trans._declare_only == 1:
|
||||
_register_cexport_from_funcdef(trans, child)
|
||||
stdio.printf("[TC] ci=%d FunctionDef 后\n", ci)
|
||||
stdio.fflush(0)
|
||||
elif kd == ast.ASTKind.ClassDef:
|
||||
# ClassDef 在模块级直接处理(不需要 builder)
|
||||
# _declare_only=2(import扫描模式)时跳过,只处理 import 依赖
|
||||
cd_node: ast.ClassDef | t.CPtr = (ast.ClassDef | t.CPtr)(child)
|
||||
cd_name: str = "?" if cd_node is None or cd_node.name is None else cd_node.name
|
||||
stdio.printf("[TC] ci=%d ClassDef name=%s 前\n", ci, cd_name)
|
||||
stdio.fflush(0)
|
||||
if trans._declare_only != 2:
|
||||
cd_node: ast.ClassDef | t.CPtr = (ast.ClassDef | t.CPtr)(child)
|
||||
cd_name: str = "?" if cd_node is None or cd_node.name is None else cd_node.name
|
||||
HandlesClassDef.translate_class_def(trans, child)
|
||||
stdio.printf("[TC] ci=%d ClassDef 后\n", ci)
|
||||
stdio.fflush(0)
|
||||
elif trans._declare_only == 0 and trans._cur_builder is not None:
|
||||
# 有 builder → 委托 HandlesBody 分派
|
||||
stdio.printf("[TC] ci=%d translate_stmt 前\n", ci)
|
||||
stdio.fflush(0)
|
||||
added = HandlesBody.translate_stmt(trans, child)
|
||||
added_total += added
|
||||
stdio.printf("[TC] ci=%d translate_stmt 后\n", ci)
|
||||
stdio.fflush(0)
|
||||
elif kd == ast.ASTKind.AnnAssign and trans._declare_only != 2:
|
||||
# 模块级 AnnAssign
|
||||
# _declare_only=2(import扫描模式)时跳过
|
||||
# _declare_only=1(struct注册模式)时只处理 CDefine(在 handle_module_level_var 内部判断)
|
||||
# _declare_only=0(全量翻译)时处理所有模块级 AnnAssign
|
||||
stdio.printf("[TC] ci=%d AnnAssign 前\n", ci)
|
||||
stdio.fflush(0)
|
||||
added = handle_module_level_var(trans, child)
|
||||
added_total += added
|
||||
stdio.printf("[TC] ci=%d AnnAssign 后\n", ci)
|
||||
stdio.fflush(0)
|
||||
elif trans._declare_only == 0 and kd == ast.ASTKind.Assign:
|
||||
# 无 builder 的模块级 Assign → 创建全局变量(仅全量翻译模式)
|
||||
stdio.printf("[TC] ci=%d Assign 前\n", ci)
|
||||
stdio.fflush(0)
|
||||
added = handle_module_level_var(trans, child)
|
||||
added_total += added
|
||||
stdio.printf("[TC] ci=%d Assign 后\n", ci)
|
||||
stdio.fflush(0)
|
||||
|
||||
return added_total
|
||||
|
||||
|
||||
Reference in New Issue
Block a user