将 ast 英化
This commit is contained in:
1 parent
752f3ddb70
commit
c5570dc8b1
9 files changed
+424
-413
No files matched your search
+56
-57
@@ -7,11 +7,11 @@ from .tokens import TokOp
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ASTKind - 节点类型枚举(kind() 虚函数返回值)
|
||||
# ASTKind - Node type enum (kind() virtual function return value)
|
||||
# ============================================================
|
||||
|
||||
class ASTKind(t.CEnum):
|
||||
# 核心 15 节点
|
||||
# Core 15 nodes
|
||||
Module: t.State
|
||||
FunctionDef: t.State
|
||||
ClassDef: t.State
|
||||
@@ -27,11 +27,11 @@ class ASTKind(t.CEnum):
|
||||
UnaryOp: t.State
|
||||
Call: t.State
|
||||
Compare: t.State
|
||||
# 模块层
|
||||
# Module Layer
|
||||
Expression: t.State
|
||||
Interactive: t.State
|
||||
FunctionType: t.State
|
||||
# 语句
|
||||
# Statement
|
||||
Delete: t.State
|
||||
AugAssign: t.State
|
||||
AnnAssign: t.State
|
||||
@@ -47,7 +47,7 @@ class ASTKind(t.CEnum):
|
||||
Import: t.State
|
||||
ImportFrom: t.State
|
||||
Match: t.State
|
||||
# 表达式
|
||||
# Expression Layer
|
||||
BoolOp: t.State
|
||||
Lambda: t.State
|
||||
IfExp: t.State
|
||||
@@ -69,7 +69,7 @@ class ASTKind(t.CEnum):
|
||||
Tuple: t.State
|
||||
Slice: t.State
|
||||
NamedExpr: t.State
|
||||
# 辅助节点
|
||||
# Auxiliary Nodes
|
||||
ExceptHandler: t.State
|
||||
Arguments: t.State
|
||||
Arg: t.State
|
||||
@@ -78,7 +78,7 @@ class ASTKind(t.CEnum):
|
||||
WithItem: t.State
|
||||
Comprehension: t.State
|
||||
OpNode: t.State
|
||||
# 模式匹配
|
||||
# Pattern Matching
|
||||
MatchCase: t.State
|
||||
MatchValue: t.State
|
||||
MatchSingleton: t.State
|
||||
@@ -91,7 +91,7 @@ class ASTKind(t.CEnum):
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ASTCtx - 名称上下文(Load/Store/Del)
|
||||
# ASTCtx - Name context (Load/Store/Del)
|
||||
# ============================================================
|
||||
|
||||
class ASTCtx(t.CEnum):
|
||||
@@ -101,11 +101,11 @@ class ASTCtx(t.CEnum):
|
||||
|
||||
|
||||
# ============================================================
|
||||
# OpKind - 运算符枚举(BinOp/UnaryOp/Compare 共用)
|
||||
# OpKind - Operator enum (BinOp/UnaryOp/Compare)
|
||||
# ============================================================
|
||||
|
||||
class OpKind(t.CEnum):
|
||||
# 二元算术/位运算
|
||||
# Binary arithmetic operators
|
||||
Add: t.State
|
||||
Sub: t.State
|
||||
Mult: t.State
|
||||
@@ -119,10 +119,10 @@ class OpKind(t.CEnum):
|
||||
BitXor: t.State
|
||||
BitAnd: t.State
|
||||
FloorDiv: t.State
|
||||
# 布尔运算
|
||||
# Boolean operators
|
||||
And: t.State
|
||||
Or: t.State
|
||||
# 比较运算
|
||||
# Comparison operators
|
||||
Eq: t.State
|
||||
Ne: t.State
|
||||
Lt: t.State
|
||||
@@ -133,16 +133,16 @@ class OpKind(t.CEnum):
|
||||
IsNot: t.State
|
||||
In: t.State
|
||||
NotIn: t.State
|
||||
# 一元运算
|
||||
# Unary operators
|
||||
Not: t.State
|
||||
UAdd: t.State
|
||||
USub: t.State
|
||||
Invert: t.State
|
||||
# 空
|
||||
# None
|
||||
NoneOp: t.State
|
||||
|
||||
|
||||
# Constant 子类型标识
|
||||
# Constant subtype identifiers
|
||||
CONST_INT: t.CDefine = 1
|
||||
CONST_FLOAT: t.CDefine = 2
|
||||
CONST_STR: t.CDefine = 3
|
||||
@@ -150,14 +150,14 @@ CONST_BOOL: t.CDefine = 4
|
||||
CONST_NONE: t.CDefine = 5
|
||||
CONST_CHAR: t.CDefine = 6
|
||||
|
||||
# 标志位
|
||||
# Node flags
|
||||
FLAG_IS_ASYNC: t.CDefine = 1
|
||||
FLAG_SIMPLE: t.CDefine = 2
|
||||
FLAG_HAS_STAR: t.CDefine = 4
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ASTFlag - 节点标志位枚举
|
||||
# ASTFlag - Node flags enum
|
||||
# ============================================================
|
||||
|
||||
class ASTFlag(t.CEnum):
|
||||
@@ -167,11 +167,11 @@ class ASTFlag(t.CEnum):
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 辅助函数
|
||||
# Helper Functions
|
||||
# ============================================================
|
||||
|
||||
def _copy_str(pool: memhub.MemManager | t.CPtr, src: str) -> str:
|
||||
"""从 pool 分配并复制 C 字符串"""
|
||||
"""Allocate and copy a C string from pool."""
|
||||
if src is None: return None
|
||||
slen: t.CSizeT = string.strlen(src)
|
||||
buf: str = pool.alloc(slen + 1)
|
||||
@@ -199,7 +199,7 @@ def _inherit_pos(node: AST | t.CPtr, ref: AST | t.CPtr):
|
||||
|
||||
def _init_ast(node: AST | t.CPtr, pool: memhub.MemManager | t.CPtr,
|
||||
lineno: t.CInt, col: t.CInt):
|
||||
"""初始化 AST 基类字段"""
|
||||
"""Initialize AST base fields."""
|
||||
node.parent = None
|
||||
node.pool = pool
|
||||
node.lineno = lineno
|
||||
@@ -210,7 +210,7 @@ def _init_ast(node: AST | t.CPtr, pool: memhub.MemManager | t.CPtr,
|
||||
|
||||
|
||||
def _set_parent_list(lst: list[AST | t.CPtr] | t.CPtr, parent: AST | t.CPtr):
|
||||
"""为 list 中的每个子节点设置 parent 指针"""
|
||||
"""Set list children parent pointers."""
|
||||
if lst is None: return
|
||||
n: t.CSizeT = lst.__len__()
|
||||
i: t.CSizeT = 0
|
||||
@@ -223,17 +223,16 @@ def _set_parent_list(lst: list[AST | t.CPtr] | t.CPtr, parent: AST | t.CPtr):
|
||||
|
||||
def _append_child(lst: list[AST | t.CPtr] | t.CPtr, node: AST | t.CPtr,
|
||||
pool: memhub.MemManager | t.CPtr):
|
||||
"""向 list 追加子节点(模块级函数,避免与 AST.append 方法名冲突)。
|
||||
"""Append child nodes to a list (module-level function, to avoid conflicts with the AST.append method name).
|
||||
|
||||
在 AST.append 方法中调用此函数,绕过编译器把 list.append 误解析为
|
||||
AST.append 虚函数调用的类型推断问题。"""
|
||||
Call this function within the AST.append method to bypass the compiler mistaking list.append for a virtual function call to AST.append when doing type inference."""
|
||||
if lst is None or node is None:
|
||||
return
|
||||
lst.append(node)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 运算符映射:TokOp -> OpKind
|
||||
# Operator Mapping: TokOp -> OpKind
|
||||
# ============================================================
|
||||
|
||||
def _binop_from_op(op_id: t.CInt) -> t.CInt:
|
||||
@@ -281,12 +280,12 @@ def _cmpop_from_op(op_id: t.CInt) -> t.CInt:
|
||||
|
||||
|
||||
# ============================================================
|
||||
# dump 辅助函数
|
||||
# dump Helper Functions
|
||||
# ============================================================
|
||||
|
||||
def _emit(buf: t.CChar | t.CPtr, size: t.CSizeT, pos: t.CSizeT,
|
||||
text: str) -> t.CSizeT:
|
||||
"""追加字面量到 buf+pos,返回新 pos"""
|
||||
"""Append literal to buf+pos."""
|
||||
if buf is None or pos >= size:
|
||||
return pos
|
||||
cur: t.CChar | t.CPtr = (t.CVoid | t.CPtr)(t.CUInt64T(buf) + pos)
|
||||
@@ -299,7 +298,7 @@ def _emit(buf: t.CChar | t.CPtr, size: t.CSizeT, pos: t.CSizeT,
|
||||
|
||||
def _emit_str(buf: t.CChar | t.CPtr, size: t.CSizeT, pos: t.CSizeT,
|
||||
text: str) -> t.CSizeT:
|
||||
"""追加字符串值到 buf+pos(用 %s 格式)"""
|
||||
"""Append a string value to buf pos (using %s format)"""
|
||||
if buf is None or pos >= size:
|
||||
return pos
|
||||
cur: t.CChar | t.CPtr = (t.CVoid | t.CPtr)(t.CUInt64T(buf) + pos)
|
||||
@@ -312,7 +311,7 @@ def _emit_str(buf: t.CChar | t.CPtr, size: t.CSizeT, pos: t.CSizeT,
|
||||
|
||||
def _emit_int(buf: t.CChar | t.CPtr, size: t.CSizeT, pos: t.CSizeT,
|
||||
val: t.CInt64T) -> t.CSizeT:
|
||||
"""追加整数值到 buf+pos"""
|
||||
"""Append an integer value to buf pos (using %lld format)."""
|
||||
if buf is None or pos >= size:
|
||||
return pos
|
||||
cur: t.CChar | t.CPtr = (t.CVoid | t.CPtr)(t.CUInt64T(buf) + pos)
|
||||
@@ -324,7 +323,7 @@ def _emit_int(buf: t.CChar | t.CPtr, size: t.CSizeT, pos: t.CSizeT,
|
||||
|
||||
|
||||
def _op_name(op: t.CInt) -> str:
|
||||
"""返回 OpKind 的字符串名称"""
|
||||
"""Return the string name of an OpKind."""
|
||||
if op == OpKind.Add: return "Add"
|
||||
if op == OpKind.Sub: return "Sub"
|
||||
if op == OpKind.Mult: return "Mult"
|
||||
@@ -359,7 +358,7 @@ def _op_name(op: t.CInt) -> str:
|
||||
|
||||
def _dump_list(lst: list[AST | t.CPtr] | t.CPtr, buf: t.CChar | t.CPtr,
|
||||
size: t.CSizeT, pos: t.CSizeT) -> t.CSizeT:
|
||||
"""遍历 list[AST | CPtr] 容器,多态 dump 每个元素"""
|
||||
"""Traverse a list[AST | CPtr] container, polymorphically dump each element."""
|
||||
if lst is None:
|
||||
pos = _emit(buf, size, pos, "[]")
|
||||
return pos
|
||||
@@ -379,7 +378,7 @@ def _dump_list(lst: list[AST | t.CPtr] | t.CPtr, buf: t.CChar | t.CPtr,
|
||||
|
||||
def _dump_op_list(lst: t.CPtr, buf: t.CChar | t.CPtr,
|
||||
size: t.CSizeT, pos: t.CSizeT) -> t.CSizeT:
|
||||
"""遍历 list[CInt] (OpKind 值),输出 op 名称列表"""
|
||||
"""Traverse a list[CInt] (OpKind values), output a list of op names."""
|
||||
if lst is None:
|
||||
pos = _emit(buf, size, pos, "[]")
|
||||
return pos
|
||||
@@ -398,23 +397,23 @@ def _dump_op_list(lst: t.CPtr, buf: t.CChar | t.CPtr,
|
||||
|
||||
|
||||
# ============================================================
|
||||
# AST - 多态基类
|
||||
#
|
||||
# @t.CVTable 启用 vtable,支持 kind()/type_name()/dump()/accept() 虚函数
|
||||
# 子节点用 list[AST | CPtr] 容器存储(O(1) 随机访问,比链表高效)
|
||||
# parent 字段维护父指针(用于向上遍历)
|
||||
# AST - Polymorphic Base Class
|
||||
|
||||
# @t.CVTable enables vtable, supporting virtual functions like kind()/type_name()/dump()/accept()
|
||||
# Child nodes are stored in a list[AST | CPtr] container (O(1) random access, more efficient than linked lists)
|
||||
# The parent field maintains a pointer to the parent (used for upward traversal)
|
||||
# ============================================================
|
||||
|
||||
@t.CVTable
|
||||
class AST:
|
||||
"""AST 节点基类。所有具体节点类继承此类。
|
||||
"""AST node base class. All specific node classes inherit from this.
|
||||
|
||||
字段:
|
||||
parent: 父节点指针(向上遍历)
|
||||
pool: 分配器(用于子节点/字符串分配)
|
||||
lineno/col_offset: 起始位置
|
||||
end_lineno/end_col_offset: 结束位置
|
||||
children: 子节点列表(body 语句块,通过 append 添加)
|
||||
Fields:
|
||||
parent: Pointer to the parent node (for upward traversal)
|
||||
pool: Allocator (used for allocating child nodes/strings)
|
||||
lineno/col_offset: Starting position
|
||||
end_lineno/end_col_offset: Ending position
|
||||
children: List of child nodes (body block, added via append)
|
||||
"""
|
||||
parent: AST | t.CPtr
|
||||
pool: memhub.MemManager | t.CPtr
|
||||
@@ -425,31 +424,31 @@ class AST:
|
||||
children: list[AST | t.CPtr] | t.CPtr
|
||||
|
||||
def kind(self) -> t.CInt:
|
||||
"""返回节点类型(ASTKind 值),子类覆盖"""
|
||||
"""Returns the node type (ASTKind value), overridden by subclasses"""
|
||||
return 0
|
||||
|
||||
def type_name(self) -> str:
|
||||
"""返回节点类型名,子类覆盖"""
|
||||
"""Return the node type name, subclasses override"""
|
||||
return "AST"
|
||||
|
||||
def dump(self, buf: t.CChar | t.CPtr, size: t.CSizeT,
|
||||
pos: t.CSizeT) -> t.CSizeT:
|
||||
"""多态 dump:子类覆盖以自定义格式。返回写入后的 pos"""
|
||||
"""Polymorphic dump: subclasses override to define custom format. Returns the written pos"""
|
||||
return pos
|
||||
|
||||
def accept(self, visitor: t.CPtr):
|
||||
"""访问者模式入口(待实现):ASTVisitor 在 __visitor.py 定义后,
|
||||
子类覆盖此方法分派到 visitor.visit_X(self)。当前为空实现。"""
|
||||
"""Visitor pattern entry point: subclasses override to dispatch to ASTVisitor in the visitor pattern.
|
||||
Current implementation is empty. override this method to dispatch to visitor.visit_X(self)"""
|
||||
pass
|
||||
|
||||
def append(self, node: AST | t.CPtr):
|
||||
"""将 node 追加为子节点(添加到 children 列表),设置 parent 指针。
|
||||
"""Append node to the children list.
|
||||
|
||||
用于 parser 构建语句块:module.append(stmt), if_node.append(stmt) 等。
|
||||
首次调用时懒初始化 children 列表。
|
||||
Used for parser to build statement blocks: module.append(stmt), if_node.append(stmt), etc.
|
||||
The children list is lazily initialized on first call.
|
||||
|
||||
注意:list[AST|CPtr] 当前按值复制存储(编译器限制),所以必须先设置
|
||||
node.parent 再 append,这样副本中的 parent 才是正确的。"""
|
||||
Note: list[AST|CPtr] is currently stored by value (compiler limitation), so you must set
|
||||
node.parent before appending, so the parent in the copy is correct."""
|
||||
if self is None or node is None:
|
||||
return
|
||||
if self.children is None:
|
||||
@@ -459,16 +458,16 @@ class AST:
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 模块级 dump 函数(供 ast.dump(node, buf, size) 调用)
|
||||
# Module dump function (for ast.dump(node, buf, size))
|
||||
# ============================================================
|
||||
|
||||
def dump(node: AST | t.CPtr, buf: t.CChar | t.CPtr, size: t.CSizeT):
|
||||
"""将 AST 节点序列化到 buf(容量 size)"""
|
||||
"""Serialize AST node to buf (with capacity size)"""
|
||||
if node is None or buf is None or size == 0:
|
||||
return
|
||||
buf[0] = '\0'
|
||||
final_pos: t.CSizeT = node.dump(buf, size, 0)
|
||||
# NUL 终止,防止 printf 越界
|
||||
# NUL-terminate to prevent printf overflow
|
||||
if final_pos < size:
|
||||
buf[final_pos] = '\0'
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user