直接移动+改调用方:~100 个委托函数按功能移至 6 个子模块,主文件 保留 _g_gen/set_global_gen/顶层常量/4 个类(AsmOperand、FuncEntry、 FuncDefaultsEntry、ExprCallHandle——CallGenLeafKit/HandlesTranslator 仍以 HandlesExprCall.类名 引用)。 - 子模块函数体内 _g_gen/常量/类裸引用 → HandlesExprCall.xxx 跨模块访问 - 28 个调用方 HandlesExprCall.func( → 子模块前缀并补 import - 全 App 语法/引用/import 验证通过
131 lines
6.2 KiB
Plaintext
131 lines
6.2 KiB
Plaintext
import t, c
|
||
from stdint import *
|
||
import ast
|
||
import memhub
|
||
import string
|
||
import llvmlite
|
||
import stdlib
|
||
import stdio
|
||
import viperlib
|
||
import w32.fileio as fileio
|
||
import lib.core.CompilerFactory as CompilerFactory
|
||
import lib.core.Handles.HandlesBase as HandlesBase
|
||
import lib.core.Handles.HandlesTranslator as HT
|
||
import lib.core.Handles.HandlesVar as HandlesVar
|
||
import lib.core.Handles.HandlesExpr as HandlesExpr
|
||
import lib.core.Handles.HandlesExprOps as HandlesExprOps
|
||
import lib.core.Handles.HandlesExprCall as HandlesExprCall
|
||
import lib.core.Handles.HandlesNonlocal as HandlesNonlocal
|
||
import lib.core.Handles.HandlesType as HandlesType
|
||
import lib.core.Handles.HandlesStruct as HandlesStruct
|
||
import lib.core.Handles.HandlesEnum as HandlesEnum
|
||
import lib.core.Handles.HandlesClassDef as HandlesClassDef
|
||
import lib.core.Handles.HandlesImports as HandlesImports
|
||
import lib.core.StubMerger as StubMerger
|
||
import lib.core.VLogger as VLogger
|
||
import lib.Projectrans.Config as Config
|
||
import lib.Projectrans.Utils as Utils
|
||
import lib.core.Gen.ExprGenBase as ExprGenBase
|
||
import lib.core.Handles.ExprCallCall as ExprCallCall
|
||
|
||
# ============================================================
|
||
# docstring 登记表容量(__doc__ 魔法属性支持)
|
||
#
|
||
# DOC_MAX: 登记条目上限(预注册阶段所有 App/includes 文件的
|
||
# 函数/类/方法 docstring,幂等查重)
|
||
# DOC_KEY_SZ: key "{module_sha1}.{symbol}" 缓冲宽度
|
||
# DOC_VAL_SZ: docstring 内容缓冲宽度
|
||
# ============================================================
|
||
DOC_MAX: t.CDefine = 512
|
||
DOC_KEY_SZ: t.CDefine = 192
|
||
DOC_VAL_SZ: t.CDefine = 4096
|
||
|
||
# ============================================================
|
||
# f-string 翻译常量(对齐 TPC HandlesExprFormat)
|
||
#
|
||
# FSTR_BUF_SIZE: 运行时栈缓冲大小(snprintf 结果缓冲)
|
||
# FSTR_SPEC_MAX: 编译期 format_spec 解析缓冲宽度
|
||
# FSTR_FMT_MAX: 编译期 C printf 格式串缓冲宽度
|
||
# ============================================================
|
||
FSTR_BUF_SIZE: t.CDefine = 1024
|
||
FSTR_SPEC_MAX: t.CDefine = 64
|
||
FSTR_FMT_MAX: t.CDefine = 64
|
||
|
||
|
||
# ============================================================
|
||
# ExprGenMixin - 表达式处理 Mixin(对象化 P3,wiki/14-oo-refactor.md)
|
||
#
|
||
# 从 HandlesExpr.vp(48 函数,无全局状态)与 HandlesExprOps.vp
|
||
# 迁移:模块级纯函数 → 类方法(self 替代,参数保留含 builder/pool)。
|
||
#
|
||
# 迁移原则(与 P2 一致,行为零变化):
|
||
# 1. 方法保留原函数签名(含 builder/pool 首参),仅函数体内
|
||
# 本模块函数调用改为 self.xxx(...)
|
||
# 2. 外部模块调用(HandlesExprCall.xxx / HandlesType.xxx 等)保留模块前缀
|
||
# 3. HandlesExpr.vp / HandlesExprOps.vp 原函数保留为"委托":本地
|
||
# 模块级单例 _g_gen(BuildPipeline/main 注入)cast 成 Generator 类型
|
||
# 4. ExprHandle 类保留在 HandlesExpr.vp(translator handler 模式)
|
||
#
|
||
# @t.NoVTable:静态绑定(方法沿 MRO 直接调用 + this 调整)。
|
||
# ============================================================
|
||
|
||
|
||
# ============================================================
|
||
# ExprGenMixin - 表达式/常量/下标/属性访问/运算符
|
||
# ============================================================
|
||
@t.NoVTable
|
||
class ExprGenMixin(ExprGenBase.ExprGenBase):
|
||
"""表达式处理 Mixin(纯方法化迁移,无状态字段)"""
|
||
|
||
|
||
# ============================================================
|
||
# 翻译值表达式(RHS 分派)— 模块级版本
|
||
# ============================================================
|
||
def translate_value(self, builder: llvmlite.IRBuilder | t.CPtr,
|
||
pool: memhub.MemBuddy | t.CPtr,
|
||
mod: llvmlite.LLVMModule | t.CPtr,
|
||
node: ast.AST | t.CPtr,
|
||
funcs_ptr: t.CPtr = None,
|
||
func_count: int = 0,
|
||
trans: HT.Translator | t.CPtr = None) -> llvmlite.Value | t.CPtr:
|
||
"""翻译值表达式为 LLVM Value"""
|
||
if node is None:
|
||
return None
|
||
k: int = node.kind()
|
||
if k == ast.ASTKind.Constant:
|
||
rv: llvmlite.Value | t.CPtr = HandlesExpr.translate_constant(builder, pool, mod, node, trans)
|
||
return rv
|
||
elif k == ast.ASTKind.Name:
|
||
return HandlesExpr.translate_name_value(builder, pool, node, trans)
|
||
elif k == ast.ASTKind.BinOp:
|
||
return HandlesExpr.translate_binop(pool, builder, mod, node, trans)
|
||
elif k == ast.ASTKind.Call:
|
||
return ExprCallCall.translate_call(pool, builder, mod, node,
|
||
funcs_ptr, func_count, trans)
|
||
elif k == ast.ASTKind.Compare:
|
||
return HandlesExpr.translate_compare(builder, pool, mod, node, trans)
|
||
elif k == ast.ASTKind.UnaryOp:
|
||
return HandlesExpr.translate_unaryop(builder, pool, mod, node, trans)
|
||
elif k == ast.ASTKind.BoolOp:
|
||
return HandlesExpr.translate_boolop(builder, pool, mod, node,
|
||
funcs_ptr, func_count, trans)
|
||
elif k == ast.ASTKind.Subscript:
|
||
return HandlesExpr.translate_subscript(builder, pool, mod, node, trans)
|
||
elif k == ast.ASTKind.Attribute:
|
||
return HandlesExpr.translate_attribute(builder, pool, mod, node, trans)
|
||
elif k == ast.ASTKind.List:
|
||
return HandlesExpr.translate_list_literal(builder, pool, mod, node, None, trans)
|
||
elif k == ast.ASTKind.IfExp:
|
||
return HandlesExpr.translate_ifexp(builder, pool, mod, node, trans)
|
||
elif k == ast.ASTKind.Lambda:
|
||
# 模块级委托:translate_lambda 定义在 FuncGenMixin(跨 Mixin
|
||
# 方法不能 self. 直接调用,见 HandlesExpr.translate_lambda)
|
||
return HandlesExpr.translate_lambda(builder, pool, mod, node, trans)
|
||
elif k == ast.ASTKind.JoinedStr:
|
||
return HandlesExpr.translate_joined_str(builder, pool, mod, node, trans)
|
||
elif k == ast.ASTKind.FormattedValue:
|
||
# 解析器对 f"{expr:spec}"(单 FormattedValue 无字面量)返回裸 FormattedValue
|
||
# 而非 JoinedStr(见 ast/parser.vp _parse_fstring),需单独翻译
|
||
return HandlesExpr.translate_formatted_value(builder, pool, mod, node, trans)
|
||
return None
|