语法侧(M2.4):ast/base.vp 枚举尾部追加 ASTKind.New;ast/exprs.vp 新增 NEW_SRC_*/NEW_LIFE_* 与 class New;ast/parser.vp 新增 _is_new_expr/_parse_new_expr(new 为软关键字,不进 Keyword 表,new=1/new.x/new(1) 仍是标识符);ast/__init__.vp 再导出 New 与常量。 其余为既有 WIP 改动一并快照,保持基线一致(39 文件,+312/-237)。 未纳入:ctraits.vpi(已删除)、ctraits.vpnci、tomllib/(均未跟踪)。
118 lines
4.2 KiB
Plaintext
118 lines
4.2 KiB
Plaintext
from stdint import *
|
|
import memhub
|
|
|
|
|
|
# Import submodule
|
|
from .tokens import (
|
|
Token, TokenType, Keyword, TokOp, _init_tables,
|
|
)
|
|
# Common Section: Base Classes, Enums, Constants, Helper Functions, Dump
|
|
from .base import (
|
|
AST, Stmt, ExprNode, ASTKind, ASTCtx, OpKind, ASTFlag,
|
|
# Constants
|
|
CONST_INT, CONST_FLOAT, CONST_STR, CONST_BOOL, CONST_NONE, CONST_CHAR,
|
|
FLAG_IS_ASYNC, FLAG_SIMPLE, FLAG_HAS_STAR,
|
|
# Helper Functions
|
|
_init_ast, _copy_str, _set_pos, _inherit_pos, _set_parent_list,
|
|
_binop_from_op, _augop_from_op, _cmpop_from_op,
|
|
_emit, _emit_str, _emit_int, _dump_list,
|
|
dump, _op_name,
|
|
)
|
|
# Statement Nodes: Module, Expression, Interactive, FunctionType
|
|
from .stmts import (
|
|
# Module Layer
|
|
Module, Expression, Interactive, FunctionType,
|
|
# Statement
|
|
FunctionDef, ClassDef, Return, Delete, Assign, AugAssign, AnnAssign,
|
|
For, While, If, With, Raise, Try, Assert, Global, Nonlocal,
|
|
Pass, Break, Continue, FallThrough, Expr, Import, ImportFrom, Match,
|
|
)
|
|
# Expression Nodes: BoolOp, BinOp, UnaryOp, Lambda, IfExp, Dict, Set
|
|
from .exprs import (
|
|
# Expression Layer
|
|
BoolOp, BinOp, UnaryOp, Lambda, IfExp, Dict, Set,
|
|
ListComp, SetComp, DictComp, GeneratorExp,
|
|
Await, Yield, YieldFrom, FormattedValue, JoinedStr,
|
|
Constant, NamedExpr, Attribute, Subscript, Starred, Name,
|
|
List, Tuple, Slice,
|
|
Call, Compare, OpNode,
|
|
# M2.4 `new` 描述符分配节点与描述符常量
|
|
New,
|
|
NEW_SRC_DEFAULT, NEW_SRC_STACK, NEW_SRC_HEAP, NEW_SRC_MEMGR,
|
|
NEW_LIFE_DEFAULT, NEW_LIFE_SLIFE, NEW_LIFE_HLIFE,
|
|
|
|
name_get_id, constant_get_str_val,
|
|
binop_get_left, binop_get_right,
|
|
attribute_get_attr, attribute_get_value,
|
|
)
|
|
# Auxiliary Nodes
|
|
from .astaux import (
|
|
ExceptHandler, Arguments, Arg, Keyword, Alias, WithItem,
|
|
Comprehension,
|
|
)
|
|
# Pattern Matching
|
|
from .match import (
|
|
MatchCase, MatchValue, MatchSingleton, MatchSequence,
|
|
MatchMapping, MatchClass, MatchStar, MatchAs, MatchOr,
|
|
)
|
|
# from .visitor import ASTVisitor
|
|
from .lexer import Lexer, _lexer_init, tokenize, new_lexer
|
|
from .parser import Parser, _parser_init, parse_tokens, new_parser
|
|
from .parser import _parse_test
|
|
from .parser import _dbg_lookup_lit_val, _dbg_lit_found
|
|
from .parser import get_parse_err_line, get_parse_err_col, get_parse_err_msg
|
|
|
|
# ============================================================
|
|
# Global Memory Pool Pointer (follows project rules: declare _mbuddy)
|
|
# User initializes in main: ast._mbuddy = memhub.MemBuddy(arena, size)
|
|
# ============================================================
|
|
_mbuddy: memhub.MemBuddy*
|
|
|
|
|
|
# ============================================================
|
|
# Common API: parse
|
|
# ============================================================
|
|
def parse(src: str, pool: memhub.MemManager*) -> AST*:
|
|
"""Parse Python source code, return the AST Module node.
|
|
|
|
Parameters:
|
|
src: NUL source code string, terminated with NUL
|
|
pool: memhub.MemManager instance, to allocate AST nodes
|
|
|
|
Returns:
|
|
AST Module node, or None
|
|
"""
|
|
if src is None or pool is None: return None
|
|
_init_tables(pool)
|
|
lx: Lexer* = new_lexer(pool)
|
|
if lx is None: return None
|
|
_lexer_init(lx, src, pool)
|
|
tokens: Token* = tokenize(lx)
|
|
if tokens is None: return None
|
|
tree: AST* = parse_tokens(pool, tokens)
|
|
return tree
|
|
|
|
|
|
def parse_expression(src: str, pool: memhub.MemManager*) -> Expression*:
|
|
"""Parse a single expression, return the Expression node.
|
|
|
|
Parameters:
|
|
src: NUL expression string, terminated with NUL
|
|
pool: memhub.MemManager instance, to allocate AST nodes
|
|
|
|
Returns:
|
|
Expression node (wrapper; body holds the actual expression), or None
|
|
"""
|
|
if src is None or pool is None: return None
|
|
_init_tables(pool)
|
|
lx: Lexer* = new_lexer(pool)
|
|
if lx is None: return None
|
|
_lexer_init(lx, src, pool)
|
|
tokens: Token* = tokenize(lx)
|
|
if tokens is None: return None
|
|
ps: Parser* = new_parser(pool)
|
|
if ps is None: return None
|
|
_parser_init(ps, tokens, pool)
|
|
body: AST* = _parse_test(ps)
|
|
return Expression(pool, body)
|