47 lines
2.0 KiB
Python
47 lines
2.0 KiB
Python
"""临时诊断:用 vast 解析 SuperTest/App/main.vp,dump super 相关语句的 AST 形态"""
|
||
import sys
|
||
sys.path.insert(0, r"d:\Users\TermiNexus\Desktop\TransPyC")
|
||
import os
|
||
os.environ['VAST_USE_C'] = '0' # 用纯 Python vast,行为一致且可打印
|
||
import lib.vast as ast
|
||
|
||
src_path = r"d:\Users\TermiNexus\Desktop\TransPyC\Test\SuperTest\App\main.vp"
|
||
with open(src_path, "r", encoding="utf-8") as f:
|
||
src = f.read()
|
||
|
||
tree = ast.parse(src, filename=src_path)
|
||
|
||
def walk_node(node, depth=0):
|
||
"""递归打印含 super 的调用形态"""
|
||
fields = list(ast.iter_fields(node))
|
||
# 打印所有 Call 节点,func 为 Name/Attribute 的
|
||
if isinstance(node, ast.Call):
|
||
func = node.func
|
||
if isinstance(func, ast.Name) and func.id == "super":
|
||
print(f" CALL(func=Name('super'), args={len(node.args)}, lineno={node.lineno})")
|
||
elif isinstance(func, ast.Attribute):
|
||
val = func.value
|
||
val_desc = "?"
|
||
if isinstance(val, ast.Call) and isinstance(val.func, ast.Name):
|
||
val_desc = f"Call(Name('{val.func.id}'), args={len(val.args)})"
|
||
elif isinstance(val, ast.Name):
|
||
val_desc = f"Name('{val.id}')"
|
||
elif isinstance(val, ast.Attribute):
|
||
val_desc = f"Attribute(...)"
|
||
print(f" CALL(func=Attribute(value={val_desc}, attr='{func.attr}'), args={len(node.args)}, lineno={node.lineno})")
|
||
for name, value in fields:
|
||
if isinstance(value, list):
|
||
for item in value:
|
||
if isinstance(item, ast.AST):
|
||
walk_node(item, depth + 1)
|
||
elif isinstance(value, ast.AST):
|
||
walk_node(value, depth + 1)
|
||
|
||
# 打印所有语句,标注含 super 的
|
||
for stmt in tree.body:
|
||
s = ast.unparse(stmt) if hasattr(ast, 'unparse') else ""
|
||
txt = f"{type(stmt).__name__}: {s}" if s else type(stmt).__name__
|
||
print(f"stmt[{getattr(stmt, 'lineno', '?')}] {txt}")
|
||
if "super" in str(stmt):
|
||
walk_node(stmt)
|