修正了一些错误
This commit is contained in:
@@ -3,6 +3,7 @@ from stdint import *
|
||||
import ast
|
||||
import string
|
||||
import memhub
|
||||
import viperlib
|
||||
|
||||
|
||||
# ============================================================
|
||||
@@ -117,12 +118,13 @@ def _PyiTypeStr(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
|
||||
if k == ast.ASTKind.Tuple:
|
||||
tup: ast.Tuple | t.CPtr = (ast.Tuple | t.CPtr)(annot)
|
||||
if tup.elts is not None:
|
||||
tn: t.CSizeT = tup.elts.__len__()
|
||||
tup_elts: list[ast.AST | t.CPtr] | t.CPtr = tup.elts
|
||||
tn: t.CSizeT = tup_elts.__len__()
|
||||
ti: t.CSizeT = 0
|
||||
while ti < tn:
|
||||
if ti > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
el: ast.AST | t.CPtr = tup.elts.get(ti)
|
||||
el: ast.AST | t.CPtr = tup_elts.get(ti)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, el)
|
||||
ti += 1
|
||||
return pos
|
||||
@@ -133,12 +135,13 @@ def _PyiTypeStr(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, cl.func)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "(")
|
||||
if cl.args is not None:
|
||||
can: t.CSizeT = cl.args.__len__()
|
||||
cl_args: list[ast.AST | t.CPtr] | t.CPtr = cl.args
|
||||
can: t.CSizeT = cl_args.__len__()
|
||||
cai: t.CSizeT = 0
|
||||
while cai < can:
|
||||
if cai > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
ca: ast.AST | t.CPtr = cl.args.get(cai)
|
||||
ca: ast.AST | t.CPtr = cl_args.get(cai)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, ca)
|
||||
cai += 1
|
||||
pos = _PyiEmit(buf, buf_size, pos, ")")
|
||||
@@ -159,10 +162,11 @@ def _PyiFunction(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
|
||||
"""生成函数签名到 buf,返回新的 pos"""
|
||||
# 装饰器
|
||||
if fn.decorator_list is not None:
|
||||
dn: t.CSizeT = fn.decorator_list.__len__()
|
||||
fn_decorator_list: list[ast.AST | t.CPtr] | t.CPtr = fn.decorator_list
|
||||
dn: t.CSizeT = fn_decorator_list.__len__()
|
||||
di: t.CSizeT = 0
|
||||
while di < dn:
|
||||
dec: ast.AST | t.CPtr = fn.decorator_list.get(di)
|
||||
dec: ast.AST | t.CPtr = fn_decorator_list.get(di)
|
||||
if dec is not None:
|
||||
pos = _PyiIndent(buf, buf_size, pos, indent_level)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "@")
|
||||
@@ -184,13 +188,14 @@ def _PyiFunction(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
|
||||
|
||||
# 位置参数
|
||||
if args.args is not None:
|
||||
an: t.CSizeT = args.args.__len__()
|
||||
args_args: list[ast.AST | t.CPtr] | t.CPtr = args.args
|
||||
an: t.CSizeT = args_args.__len__()
|
||||
ai: t.CSizeT = 0
|
||||
while ai < an:
|
||||
if first == 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
first = 0
|
||||
ag: ast.Arg | t.CPtr = (ast.Arg | t.CPtr)(args.args.get(ai))
|
||||
ag: ast.Arg | t.CPtr = (ast.Arg | t.CPtr)(args_args.get(ai))
|
||||
if ag.arg is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ag.arg)
|
||||
if ag.annotation is not None:
|
||||
@@ -242,10 +247,11 @@ def _PyiClass(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
|
||||
"""生成类定义到 buf,返回新的 pos"""
|
||||
# 装饰器
|
||||
if cls.decorator_list is not None:
|
||||
dcn: t.CSizeT = cls.decorator_list.__len__()
|
||||
cls_decorator_list: list[ast.AST | t.CPtr] | t.CPtr = cls.decorator_list
|
||||
dcn: t.CSizeT = cls_decorator_list.__len__()
|
||||
dci: t.CSizeT = 0
|
||||
while dci < dcn:
|
||||
ddec: ast.AST | t.CPtr = cls.decorator_list.get(dci)
|
||||
ddec: ast.AST | t.CPtr = cls_decorator_list.get(dci)
|
||||
if ddec is not None:
|
||||
pos = _PyiIndent(buf, buf_size, pos, indent_level)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "@")
|
||||
@@ -260,14 +266,15 @@ def _PyiClass(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
|
||||
|
||||
# PEP 695 类型参数 (class Foo[T]:)
|
||||
if cls.type_params is not None:
|
||||
tpn: t.CSizeT = cls.type_params.__len__()
|
||||
cls_type_params: list[ast.AST | t.CPtr] | t.CPtr = cls.type_params
|
||||
tpn: t.CSizeT = cls_type_params.__len__()
|
||||
if tpn > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, "[")
|
||||
tpi: t.CSizeT = 0
|
||||
while tpi < tpn:
|
||||
if tpi > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
tpname: str = cls.type_params.get(tpi)
|
||||
tpname: str = cls_type_params.get(tpi)
|
||||
if tpname is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, tpname)
|
||||
tpi += 1
|
||||
@@ -275,14 +282,15 @@ def _PyiClass(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
|
||||
|
||||
# 基类
|
||||
if cls.bases is not None:
|
||||
bn: t.CSizeT = cls.bases.__len__()
|
||||
cls_bases: list[ast.AST | t.CPtr] | t.CPtr = cls.bases
|
||||
bn: t.CSizeT = cls_bases.__len__()
|
||||
if bn > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, "(")
|
||||
bi: t.CSizeT = 0
|
||||
while bi < bn:
|
||||
if bi > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
base: ast.AST | t.CPtr = cls.bases.get(bi)
|
||||
base: ast.AST | t.CPtr = cls_bases.get(bi)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, base)
|
||||
bi += 1
|
||||
pos = _PyiEmit(buf, buf_size, pos, ")")
|
||||
@@ -293,10 +301,11 @@ def _PyiClass(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
|
||||
has_member: int = 0
|
||||
|
||||
if cls.children is not None:
|
||||
cn: t.CSizeT = cls.children.__len__()
|
||||
cls_children: list[ast.AST | t.CPtr] | t.CPtr = cls.children
|
||||
cn: t.CSizeT = cls_children.__len__()
|
||||
ci: t.CSizeT = 0
|
||||
while ci < cn:
|
||||
stmt: ast.AST | t.CPtr = cls.children.get(ci)
|
||||
stmt: ast.AST | t.CPtr = cls_children.get(ci)
|
||||
if stmt is not None:
|
||||
sk: int = stmt.kind()
|
||||
|
||||
@@ -306,10 +315,11 @@ def _PyiClass(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
|
||||
# __init__ 方法:先提取 self.x: Type 属性
|
||||
if mfn.name is not None and string.strcmp(mfn.name, "__init__") == 0:
|
||||
if mfn.children is not None:
|
||||
icn: t.CSizeT = mfn.children.__len__()
|
||||
mfn_children: list[ast.AST | t.CPtr] | t.CPtr = mfn.children
|
||||
icn: t.CSizeT = mfn_children.__len__()
|
||||
ici: t.CSizeT = 0
|
||||
while ici < icn:
|
||||
ibody: ast.AST | t.CPtr = mfn.children.get(ici)
|
||||
ibody: ast.AST | t.CPtr = mfn_children.get(ici)
|
||||
if ibody is not None and ibody.kind() == ast.ASTKind.AnnAssign:
|
||||
iaa: ast.AnnAssign | t.CPtr = (ast.AnnAssign | t.CPtr)(ibody)
|
||||
if iaa.target is not None and iaa.target.kind() == ast.ASTKind.Attribute:
|
||||
@@ -391,21 +401,23 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy | t.CPtr, tree: ast.AST | t.CPtr,
|
||||
buf[pos] = 0
|
||||
return pos
|
||||
|
||||
n: t.CSizeT = tree.children.__len__()
|
||||
tree_children: list[ast.AST | t.CPtr] | t.CPtr = tree.children
|
||||
n: t.CSizeT = tree_children.__len__()
|
||||
|
||||
# 第一遍扫描:检测 import t/c
|
||||
has_import_t: int = 0
|
||||
has_import_c: int = 0
|
||||
i: t.CSizeT = 0
|
||||
while i < n:
|
||||
stmt: ast.AST | t.CPtr = tree.children.get(i)
|
||||
stmt: ast.AST | t.CPtr = tree_children.get(i)
|
||||
if stmt is not None and stmt.kind() == ast.ASTKind.Import:
|
||||
imp: ast.Import | t.CPtr = (ast.Import | t.CPtr)(stmt)
|
||||
if imp.names is not None:
|
||||
an: t.CSizeT = imp.names.__len__()
|
||||
imp_names: list[ast.AST | t.CPtr] | t.CPtr = imp.names
|
||||
an: t.CSizeT = imp_names.__len__()
|
||||
ai: t.CSizeT = 0
|
||||
while ai < an:
|
||||
al: ast.Alias | t.CPtr = (ast.Alias | t.CPtr)(imp.names.get(ai))
|
||||
al: ast.Alias | t.CPtr = (ast.Alias | t.CPtr)(imp_names.get(ai))
|
||||
if al is not None and al.name is not None:
|
||||
if string.strcmp(al.name, "t") == 0:
|
||||
has_import_t = 1
|
||||
@@ -427,7 +439,7 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy | t.CPtr, tree: ast.AST | t.CPtr,
|
||||
# 第二遍扫描:生成声明
|
||||
i = 0
|
||||
while i < n:
|
||||
stmt = tree.children.get(i)
|
||||
stmt = tree_children.get(i)
|
||||
if stmt is not None:
|
||||
k: int = stmt.kind()
|
||||
|
||||
@@ -436,12 +448,13 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy | t.CPtr, tree: ast.AST | t.CPtr,
|
||||
imp2: ast.Import | t.CPtr = (ast.Import | t.CPtr)(stmt)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "import ")
|
||||
if imp2.names is not None:
|
||||
an2: t.CSizeT = imp2.names.__len__()
|
||||
imp2_names: list[ast.AST | t.CPtr] | t.CPtr = imp2.names
|
||||
an2: t.CSizeT = imp2_names.__len__()
|
||||
ai2: t.CSizeT = 0
|
||||
while ai2 < an2:
|
||||
if ai2 > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
al2: ast.Alias | t.CPtr = (ast.Alias | t.CPtr)(imp2.names.get(ai2))
|
||||
al2: ast.Alias | t.CPtr = (ast.Alias | t.CPtr)(imp2_names.get(ai2))
|
||||
if al2 is not None and al2.name is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, al2.name)
|
||||
if al2.asname is not None and string.strlen(al2.asname) > 0:
|
||||
@@ -462,12 +475,13 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy | t.CPtr, tree: ast.AST | t.CPtr,
|
||||
pos = _PyiEmit(buf, buf_size, pos, ifr.module)
|
||||
pos = _PyiEmit(buf, buf_size, pos, " import ")
|
||||
if ifr.names is not None:
|
||||
an3: t.CSizeT = ifr.names.__len__()
|
||||
ifr_names: list[ast.AST | t.CPtr] | t.CPtr = ifr.names
|
||||
an3: t.CSizeT = ifr_names.__len__()
|
||||
ai3: t.CSizeT = 0
|
||||
while ai3 < an3:
|
||||
if ai3 > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
al3: ast.Alias | t.CPtr = (ast.Alias | t.CPtr)(ifr.names.get(ai3))
|
||||
al3: ast.Alias | t.CPtr = (ast.Alias | t.CPtr)(ifr_names.get(ai3))
|
||||
if al3 is not None and al3.name is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, al3.name)
|
||||
if al3.asname is not None and string.strlen(al3.asname) > 0:
|
||||
@@ -496,15 +510,31 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy | t.CPtr, tree: ast.AST | t.CPtr,
|
||||
pos = _PyiEmit(buf, buf_size, pos, nm.id)
|
||||
pos = _PyiEmit(buf, buf_size, pos, ": ")
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, aa.annotation)
|
||||
# 如果有值(如 CDefine 常量),输出 = value
|
||||
if aa.value is not None and aa.value.kind() == ast.ASTKind.Constant:
|
||||
cv: ast.Constant | t.CPtr = (ast.Constant | t.CPtr)(aa.value)
|
||||
if cv.const_kind == ast.CONST_INT:
|
||||
num_buf: bytes = mb.alloc(32)
|
||||
if num_buf is not None:
|
||||
viperlib.snprintf(num_buf, 32, " = %d", cv.int_val)
|
||||
pos = _PyiEmit(buf, buf_size, pos, num_buf)
|
||||
elif cv.const_kind == ast.CONST_BOOL:
|
||||
if cv.int_val != 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, " = True")
|
||||
else:
|
||||
pos = _PyiEmit(buf, buf_size, pos, " = False")
|
||||
elif cv.const_kind == ast.CONST_NONE:
|
||||
pos = _PyiEmit(buf, buf_size, pos, " = None")
|
||||
pos = _PyiEmit(buf, buf_size, pos, "\n")
|
||||
|
||||
# 全局赋值(无注解)— 从 Constant value 推断类型
|
||||
elif k == ast.ASTKind.Assign:
|
||||
asg: ast.Assign | t.CPtr = (ast.Assign | t.CPtr)(stmt)
|
||||
if asg.targets is not None and asg.value is not None:
|
||||
atn: t.CSizeT = asg.targets.__len__()
|
||||
asg_targets: list[ast.AST | t.CPtr] | t.CPtr = asg.targets
|
||||
atn: t.CSizeT = asg_targets.__len__()
|
||||
if atn == 1:
|
||||
atg: ast.AST | t.CPtr = asg.targets.get(0)
|
||||
atg: ast.AST | t.CPtr = asg_targets.get(0)
|
||||
if atg is not None and atg.kind() == ast.ASTKind.Name:
|
||||
agn: ast.Name | t.CPtr = (ast.Name | t.CPtr)(atg)
|
||||
aval: ast.AST | t.CPtr = asg.value
|
||||
|
||||
Reference in New Issue
Block a user