更新了库
This commit is contained in:
1 parent
14ff9d109f
commit
5e768a9831
7 files changed
+239
-13
No files matched your search
@@ -155,6 +155,7 @@ FLAG_IS_ASYNC: t.CDefine = 1
|
||||
FLAG_SIMPLE: t.CDefine = 2
|
||||
FLAG_HAS_STAR: t.CDefine = 4
|
||||
FLAG_IS_EXTERN: t.CDefine = 8
|
||||
FLAG_IS_EXPORT: t.CDefine = 16
|
||||
|
||||
|
||||
# ============================================================
|
||||
|
||||
@@ -2,6 +2,7 @@ import t, c
|
||||
from stdint import *
|
||||
import memhub
|
||||
import string
|
||||
import stdio
|
||||
from .base import (
|
||||
AST, ExprNode, ASTKind, ASTCtx, OpKind,
|
||||
_init_ast, _copy_str, _set_parent_list,
|
||||
@@ -53,6 +54,34 @@ class Name(ExprNode):
|
||||
return pos
|
||||
|
||||
|
||||
def name_get_id(node: Name | t.CPtr) -> str:
|
||||
"""获取 Name.id 字段(跨模块访问器,stub 字段访问陷阱防护)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.id
|
||||
|
||||
|
||||
def constant_get_str_val(node: Constant | t.CPtr) -> str:
|
||||
"""获取 Constant.str_val 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.str_val
|
||||
|
||||
|
||||
def binop_get_left(node: BinOp | t.CPtr) -> AST | t.CPtr:
|
||||
"""获取 BinOp.left 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.left
|
||||
|
||||
|
||||
def binop_get_right(node: BinOp | t.CPtr) -> AST | t.CPtr:
|
||||
"""获取 BinOp.right 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.right
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Constant nodes (subtype marked by const_kind)
|
||||
# ============================================================
|
||||
@@ -943,6 +972,27 @@ class Attribute(ExprNode):
|
||||
return pos
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 字段访问器(跨模块 stub 字段访问陷阱防护)
|
||||
#
|
||||
# 其他模块(如 TypeGenCType)直接访问 ast.Attribute 的字段时,
|
||||
# TPC 可能按 stub 类型生成错误 GEP(读垃圾地址 → 0xC0000005 崩溃)。
|
||||
# 本模块是完整类型,字段偏移正确;跨模块调用方用这些访问器。
|
||||
# ============================================================
|
||||
def attribute_get_value(node: Attribute | t.CPtr) -> AST | t.CPtr:
|
||||
"""获取 Attribute.value 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.value
|
||||
|
||||
|
||||
def attribute_get_attr(node: Attribute | t.CPtr) -> str:
|
||||
"""获取 Attribute.attr 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.attr
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Subscript Node
|
||||
# ============================================================
|
||||
|
||||
+35
-8
@@ -10,7 +10,7 @@ from .base import (
|
||||
_set_pos, _binop_from_op, _augop_from_op, _cmpop_from_op, _copy_str, _inherit_pos,
|
||||
_init_ast,
|
||||
CONST_INT, CONST_FLOAT, CONST_STR, CONST_BOOL, CONST_NONE, CONST_CHAR,
|
||||
FLAG_IS_ASYNC, FLAG_IS_EXTERN,
|
||||
FLAG_IS_ASYNC, FLAG_IS_EXTERN, FLAG_IS_EXPORT,
|
||||
)
|
||||
from .exprs import (
|
||||
BoolOp, BinOp, UnaryOp, Lambda, IfExp, Dict, Set,
|
||||
@@ -95,18 +95,20 @@ def _cur_kw(ps: Parser | t.CPtr) -> t.CInt:
|
||||
|
||||
def _is_funcdef_kw(ps: Parser | t.CPtr, kw: t.CInt) -> int:
|
||||
"""判断当前 token 是否为函数定义关键字(def 及其别名 fn/fun/func/function,
|
||||
以及 extern 声明关键字)
|
||||
以及 extern/export 声明关键字)
|
||||
|
||||
def 是硬关键字直接判定;fn/fun/func/function/extern 是软关键字,
|
||||
def 是硬关键字直接判定;fn/fun/func/function/extern/export 是软关键字,
|
||||
仅当下一个 token 是 Name 且再下一个是 '('(或泛型 '[T]' 前的 '[')时
|
||||
判定为函数定义。extern 还允许 'extern def A(...)' 形式(下一个是 def)。
|
||||
判定为函数定义。extern 还允许 'extern def A(...)' 形式(下一个是 def),
|
||||
export 同样允许 'export def A(...)' 形式。
|
||||
否则按普通标识符处理,兼容既有代码把 func 用作
|
||||
字段/参数名(如 AST 节点 func 字段、Call.func 注解 `func: AST | t.CPtr`)。
|
||||
"""
|
||||
if kw == Keyword.Def:
|
||||
return 1
|
||||
if kw == Keyword.Extern:
|
||||
# extern def A(...) 或 extern A(...)(extern 后是 def 或 Name+'(/'[')
|
||||
if kw == Keyword.Extern or kw == Keyword.Export:
|
||||
# extern/export def A(...) 或 extern/export A(...)
|
||||
# (extern/export 后是 def 或 Name+'(/'[')
|
||||
nxt: Token | t.CPtr = ps.cur.next
|
||||
if nxt is None:
|
||||
return 0
|
||||
@@ -2229,12 +2231,19 @@ def _parse_funcdef(ps: Parser | t.CPtr, is_async: t.CInt) -> AST | t.CPtr:
|
||||
lineno: t.CInt = _cur_lineno(ps)
|
||||
col: t.CInt = _cur_col(ps)
|
||||
is_extern_fd: t.CInt = 0
|
||||
is_export_fd: t.CInt = 0
|
||||
if _cur_kw(ps) == Keyword.Extern:
|
||||
# extern 声明:extern [def] A(...) [-> T] [": pass" 可选]
|
||||
is_extern_fd = 1
|
||||
_advance(ps) # extern
|
||||
if _cur_kw(ps) == Keyword.Def:
|
||||
_advance(ps)
|
||||
elif _cur_kw(ps) == Keyword.Export:
|
||||
# export 声明:export [def] A(...) [-> T] [": pass" 可选]
|
||||
is_export_fd = 1
|
||||
_advance(ps) # export
|
||||
if _cur_kw(ps) == Keyword.Def:
|
||||
_advance(ps)
|
||||
else:
|
||||
_advance(ps) # def / fn / ...
|
||||
if _cur_type(ps) != TokenType.Name:
|
||||
@@ -2280,15 +2289,33 @@ def _parse_funcdef(ps: Parser | t.CPtr, is_async: t.CInt) -> AST | t.CPtr:
|
||||
returns = BinOp(ps.pool, returns, OpKind.BitOr, state_attr_fd)
|
||||
else:
|
||||
returns = state_attr_fd
|
||||
elif is_export_fd:
|
||||
# export 声明:返回注解改写为 <原类型> | t.CExport,复用既有
|
||||
# CExport 识别路径(vpi 注解含 "CExport" → 定义端裸名 + 不加模块 SHA1 头)
|
||||
export_attr_fd: AST | t.CPtr = Attribute(ps.pool,
|
||||
Name(ps.pool, "t", ASTCtx.Load),
|
||||
"CExport", ASTCtx.Load)
|
||||
if returns != None:
|
||||
returns = BinOp(ps.pool, returns, OpKind.BitOr, export_attr_fd)
|
||||
else:
|
||||
returns = export_attr_fd
|
||||
dec_list: list[AST | t.CPtr] | t.CPtr = ps.pending_decorators
|
||||
ps.pending_decorators = None
|
||||
node: FunctionDef | t.CPtr = FunctionDef(ps.pool, name, args, dec_list, returns, is_async, type_params_fd)
|
||||
if is_extern_fd:
|
||||
node.flags = node.flags | FLAG_IS_EXTERN
|
||||
if is_extern_fd == 0 or _match_op(ps, TokOp.Colon):
|
||||
if is_export_fd:
|
||||
node.flags = node.flags | FLAG_IS_EXPORT
|
||||
if (is_extern_fd == 0 and is_export_fd == 0) or _match_op(ps, TokOp.Colon):
|
||||
_expect_op(ps, TokOp.Colon)
|
||||
_parse_suite_into_children(ps, node)
|
||||
# extern 无 ': pass' 形式(extern A(...) -> int):body 为空,仅声明
|
||||
else:
|
||||
# extern/export 无 ': pass' 形式(extern A(...) -> int):body 为空,仅声明。
|
||||
# 必须消费本行 NewLine:模块级/类体语句循环只 _skip_nl(Nl),
|
||||
# 残留 NewLine 会被下一轮当表达式解析(_parse_atom → error_count++)
|
||||
# → parse_tokens 返回 None(TPV 编译 extern 文件报 7 个扫描失败的根因)
|
||||
if _cur_type(ps) == TokenType.NewLine:
|
||||
_advance(ps)
|
||||
_set_pos(node, lineno, col, 0, 0)
|
||||
return node
|
||||
|
||||
|
||||
+4
-1
@@ -79,6 +79,8 @@ class Keyword(t.CEnum):
|
||||
Function: t.State
|
||||
# extern 函数声明关键字(FFI,等价 t.State 标记;软判定见 _is_funcdef_kw)
|
||||
Extern: t.State
|
||||
# export 函数声明关键字(等价 t.CExport,裸名导出不加 SHA1 头;软判定见 _is_funcdef_kw)
|
||||
Export: t.State
|
||||
|
||||
|
||||
# ============================================================
|
||||
@@ -258,7 +260,8 @@ def _init_keywords(pool: memhub.MemManager | t.CPtr):
|
||||
_kw_intern(pool, "fun", Keyword.Fun)
|
||||
_kw_intern(pool, "func", Keyword.Func)
|
||||
_kw_intern(pool, "function", Keyword.Function)
|
||||
_kw_intern(pool, "extern", Keyword.Extern)
|
||||
_kw_intern(pool, "extern", Keyword.Extern)
|
||||
_kw_intern(pool, "export", Keyword.Export)
|
||||
|
||||
|
||||
# ============================================================
|
||||
|
||||
Reference in new issue
Block a user