语法侧(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/(均未跟踪)。
338 lines
11 KiB
Plaintext
338 lines
11 KiB
Plaintext
from stdint import *
|
|
import memhub
|
|
import string
|
|
from .base import (
|
|
AST, ASTKind,
|
|
_init_ast, _copy_str, _set_parent_list,
|
|
_emit, _emit_str, _emit_int, _dump_list,
|
|
)
|
|
|
|
|
|
# ============================================================
|
|
# MatchCase Node
|
|
# ============================================================
|
|
class MatchCase(AST):
|
|
"""MatchCase(pattern: AST, guard: AST)"""
|
|
pattern: AST*
|
|
guard: AST*
|
|
|
|
def __new__(self, pool: memhub.MemManager*,
|
|
pattern: AST*, guard: AST*):
|
|
ptr: MatchCase* = pool.alloc(MatchCase.__sizeof__())
|
|
if ptr: string.memset(ptr, 0, MatchCase.__sizeof__())
|
|
return ptr
|
|
|
|
def __init__(self, pool: memhub.MemManager*,
|
|
pattern: AST*, guard: AST*):
|
|
_init_ast(self, pool, 0, 0)
|
|
self.pattern = pattern
|
|
self.guard = guard
|
|
|
|
def kind(self) -> int:
|
|
return ASTKind.MatchCase
|
|
|
|
def type_name(self) -> str:
|
|
return "MatchCase"
|
|
|
|
def dump(self, buf: str, size: sizet, pos: sizet) -> sizet:
|
|
pos = _emit(buf, size, pos, "MatchCase(pattern=")
|
|
if self.pattern is not None:
|
|
pos = self.pattern.dump(buf, size, pos)
|
|
pos = _emit(buf, size, pos, ", guard=")
|
|
if self.guard is not None:
|
|
pos = self.guard.dump(buf, size, pos)
|
|
pos = _emit(buf, size, pos, ", body=")
|
|
pos = _dump_list(self.children, buf, size, pos)
|
|
pos = _emit(buf, size, pos, ")")
|
|
return pos
|
|
|
|
|
|
# ============================================================
|
|
# MatchValue Node
|
|
# ============================================================
|
|
class MatchValue(AST):
|
|
"""MatchValue(value: AST)"""
|
|
value: AST*
|
|
|
|
def __new__(self, pool: memhub.MemManager*, value: AST*):
|
|
ptr: MatchValue* = pool.alloc(MatchValue.__sizeof__())
|
|
if ptr: string.memset(ptr, 0, MatchValue.__sizeof__())
|
|
return ptr
|
|
|
|
def __init__(self, pool: memhub.MemManager*, value: AST*):
|
|
_init_ast(self, pool, 0, 0)
|
|
self.value = value
|
|
|
|
def kind(self) -> int:
|
|
return ASTKind.MatchValue
|
|
|
|
def type_name(self) -> str:
|
|
return "MatchValue"
|
|
|
|
def dump(self, buf: str, size: sizet, pos: sizet) -> sizet:
|
|
pos = _emit(buf, size, pos, "MatchValue(value=")
|
|
if self.value is not None:
|
|
pos = self.value.dump(buf, size, pos)
|
|
pos = _emit(buf, size, pos, ")")
|
|
return pos
|
|
|
|
|
|
# ============================================================
|
|
# MatchSingleton Node
|
|
# ============================================================
|
|
class MatchSingleton(AST):
|
|
"""MatchSingleton(value: AST)"""
|
|
value: AST*
|
|
|
|
def __new__(self, pool: memhub.MemManager*, value: AST*):
|
|
ptr: MatchSingleton* = pool.alloc(MatchSingleton.__sizeof__())
|
|
if ptr: string.memset(ptr, 0, MatchSingleton.__sizeof__())
|
|
return ptr
|
|
|
|
def __init__(self, pool: memhub.MemManager*, value: AST*):
|
|
_init_ast(self, pool, 0, 0)
|
|
self.value = value
|
|
|
|
def kind(self) -> int:
|
|
return ASTKind.MatchSingleton
|
|
|
|
def type_name(self) -> str:
|
|
return "MatchSingleton"
|
|
|
|
def dump(self, buf: str, size: sizet, pos: sizet) -> sizet:
|
|
pos = _emit(buf, size, pos, "MatchSingleton(value=")
|
|
if self.value is not None:
|
|
pos = self.value.dump(buf, size, pos)
|
|
pos = _emit(buf, size, pos, ")")
|
|
return pos
|
|
|
|
|
|
# ============================================================
|
|
# MatchSequence Node
|
|
# ============================================================
|
|
class MatchSequence(AST):
|
|
"""MatchSequence(patterns: list[rawptr[AST]])"""
|
|
patterns: list[AST*]*
|
|
|
|
def __new__(self, pool: memhub.MemManager*,
|
|
patterns: list[AST*]*):
|
|
ptr: MatchSequence* = pool.alloc(MatchSequence.__sizeof__())
|
|
if ptr: string.memset(ptr, 0, MatchSequence.__sizeof__())
|
|
return ptr
|
|
|
|
def __init__(self, pool: memhub.MemManager*,
|
|
patterns: list[AST*]*):
|
|
_init_ast(self, pool, 0, 0)
|
|
self.patterns = patterns
|
|
_set_parent_list(patterns, self)
|
|
|
|
def kind(self) -> int:
|
|
return ASTKind.MatchSequence
|
|
|
|
def type_name(self) -> str:
|
|
return "MatchSequence"
|
|
|
|
def dump(self, buf: str, size: sizet, pos: sizet) -> sizet:
|
|
pos = _emit(buf, size, pos, "MatchSequence(patterns=")
|
|
pos = _dump_list(self.patterns, buf, size, pos)
|
|
pos = _emit(buf, size, pos, ")")
|
|
return pos
|
|
|
|
|
|
# ============================================================
|
|
# MatchMapping Node
|
|
# ============================================================
|
|
class MatchMapping(AST):
|
|
"""MatchMapping(keys: list[rawptr[AST]], patterns: list[rawptr[AST]], rest: str)"""
|
|
keys: list[AST*]*
|
|
patterns: list[AST*]*
|
|
rest: str
|
|
|
|
def __new__(self, pool: memhub.MemManager*,
|
|
keys: list[AST*]*,
|
|
patterns: list[AST*]*,
|
|
rest: str):
|
|
ptr: MatchMapping* = pool.alloc(MatchMapping.__sizeof__())
|
|
if ptr: string.memset(ptr, 0, MatchMapping.__sizeof__())
|
|
return ptr
|
|
|
|
def __init__(self, pool: memhub.MemManager*,
|
|
keys: list[AST*]*,
|
|
patterns: list[AST*]*,
|
|
rest: str):
|
|
_init_ast(self, pool, 0, 0)
|
|
self.keys = keys
|
|
self.patterns = patterns
|
|
self.rest = _copy_str(pool, rest)
|
|
_set_parent_list(keys, self)
|
|
_set_parent_list(patterns, self)
|
|
|
|
def kind(self) -> int:
|
|
return ASTKind.MatchMapping
|
|
|
|
def type_name(self) -> str:
|
|
return "MatchMapping"
|
|
|
|
def dump(self, buf: str, size: sizet, pos: sizet) -> sizet:
|
|
pos = _emit(buf, size, pos, "MatchMapping(keys=")
|
|
pos = _dump_list(self.keys, buf, size, pos)
|
|
pos = _emit(buf, size, pos, ", patterns=")
|
|
pos = _dump_list(self.patterns, buf, size, pos)
|
|
pos = _emit(buf, size, pos, ", rest='")
|
|
pos = _emit_str(buf, size, pos, self.rest)
|
|
pos = _emit(buf, size, pos, "')")
|
|
return pos
|
|
|
|
|
|
# ============================================================
|
|
# MatchClass Node
|
|
# ============================================================
|
|
|
|
class MatchClass(AST):
|
|
"""MatchClass(cls: AST, patterns: list[rawptr[AST]],
|
|
kwd_attrs: list[AST*], kwd_patterns: list[AST*])"""
|
|
cls: AST*
|
|
patterns: list[AST*]*
|
|
kwd_attrs: list[AST*]*
|
|
kwd_patterns: list[AST*]*
|
|
|
|
def __new__(self, pool: memhub.MemManager*,
|
|
cls: AST*,
|
|
patterns: list[AST*]*,
|
|
kwd_attrs: list[AST*]*,
|
|
kwd_patterns: list[AST*]*):
|
|
ptr: MatchClass* = pool.alloc(MatchClass.__sizeof__())
|
|
if ptr: string.memset(ptr, 0, MatchClass.__sizeof__())
|
|
return ptr
|
|
|
|
def __init__(self, pool: memhub.MemManager*,
|
|
cls: AST*,
|
|
patterns: list[AST*]*,
|
|
kwd_attrs: list[AST*]*,
|
|
kwd_patterns: list[AST*]*):
|
|
_init_ast(self, pool, 0, 0)
|
|
self.cls = cls
|
|
self.patterns = patterns
|
|
self.kwd_attrs = kwd_attrs
|
|
self.kwd_patterns = kwd_patterns
|
|
_set_parent_list(patterns, self)
|
|
_set_parent_list(kwd_attrs, self)
|
|
_set_parent_list(kwd_patterns, self)
|
|
|
|
def kind(self) -> int:
|
|
return ASTKind.MatchClass
|
|
|
|
def type_name(self) -> str:
|
|
return "MatchClass"
|
|
|
|
def dump(self, buf: str, size: sizet, pos: sizet) -> sizet:
|
|
pos = _emit(buf, size, pos, "MatchClass(cls=")
|
|
if self.cls is not None: pos = self.cls.dump(buf, size, pos)
|
|
pos = _emit(buf, size, pos, ", patterns=")
|
|
pos = _dump_list(self.patterns, buf, size, pos)
|
|
pos = _emit(buf, size, pos, ", kwd_attrs=")
|
|
pos = _dump_list(self.kwd_attrs, buf, size, pos)
|
|
pos = _emit(buf, size, pos, ", kwd_patterns=")
|
|
pos = _dump_list(self.kwd_patterns, buf, size, pos)
|
|
pos = _emit(buf, size, pos, ")")
|
|
return pos
|
|
|
|
|
|
# ============================================================
|
|
# MatchStar Node
|
|
# ============================================================
|
|
|
|
class MatchStar(AST):
|
|
"""MatchStar(name: str)"""
|
|
name: str
|
|
|
|
def __new__(self, pool: memhub.MemManager*, name: str):
|
|
ptr: MatchStar* = pool.alloc(MatchStar.__sizeof__())
|
|
if ptr: string.memset(ptr, 0, MatchStar.__sizeof__())
|
|
return ptr
|
|
|
|
def __init__(self, pool: memhub.MemManager*, name: str):
|
|
_init_ast(self, pool, 0, 0)
|
|
self.name = _copy_str(pool, name)
|
|
|
|
def kind(self) -> int:
|
|
return ASTKind.MatchStar
|
|
|
|
def type_name(self) -> str:
|
|
return "MatchStar"
|
|
|
|
def dump(self, buf: str, size: sizet, pos: sizet) -> sizet:
|
|
pos = _emit(buf, size, pos, "MatchStar(name='")
|
|
pos = _emit_str(buf, size, pos, self.name)
|
|
pos = _emit(buf, size, pos, "')")
|
|
return pos
|
|
|
|
|
|
# ============================================================
|
|
# MatchAs Node
|
|
# ============================================================
|
|
|
|
class MatchAs(AST):
|
|
"""MatchAs(pattern: AST, name: str)"""
|
|
pattern: AST*
|
|
name: str
|
|
|
|
def __new__(self, pool: memhub.MemManager*,
|
|
pattern: AST*, name: str):
|
|
ptr: MatchAs* = pool.alloc(MatchAs.__sizeof__())
|
|
if ptr: string.memset(ptr, 0, MatchAs.__sizeof__())
|
|
return ptr
|
|
|
|
def __init__(self, pool: memhub.MemManager*,
|
|
pattern: AST*, name: str):
|
|
_init_ast(self, pool, 0, 0)
|
|
self.pattern = pattern
|
|
self.name = _copy_str(pool, name)
|
|
|
|
def kind(self) -> int:
|
|
return ASTKind.MatchAs
|
|
|
|
def type_name(self) -> str:
|
|
return "MatchAs"
|
|
|
|
def dump(self, buf: str, size: sizet, pos: sizet) -> sizet:
|
|
pos = _emit(buf, size, pos, "MatchAs(pattern=")
|
|
if self.pattern is not None: pos = self.pattern.dump(buf, size, pos)
|
|
pos = _emit(buf, size, pos, ", name='")
|
|
pos = _emit_str(buf, size, pos, self.name)
|
|
pos = _emit(buf, size, pos, "')")
|
|
return pos
|
|
|
|
|
|
# ============================================================
|
|
# MatchOr Node
|
|
# ============================================================
|
|
|
|
class MatchOr(AST):
|
|
"""MatchOr(patterns: list[rawptr[AST]])"""
|
|
patterns: list[AST*]*
|
|
|
|
def __new__(self, pool: memhub.MemManager*,
|
|
patterns: list[AST*]*):
|
|
ptr: MatchOr* = pool.alloc(MatchOr.__sizeof__())
|
|
if ptr: string.memset(ptr, 0, MatchOr.__sizeof__())
|
|
return ptr
|
|
|
|
def __init__(self, pool: memhub.MemManager*,
|
|
patterns: list[AST*]*):
|
|
_init_ast(self, pool, 0, 0)
|
|
self.patterns = patterns
|
|
_set_parent_list(patterns, self)
|
|
|
|
def kind(self) -> int:
|
|
return ASTKind.MatchOr
|
|
|
|
def type_name(self) -> str:
|
|
return "MatchOr"
|
|
|
|
def dump(self, buf: str, size: sizet, pos: sizet) -> sizet:
|
|
pos = _emit(buf, size, pos, "MatchOr(patterns=")
|
|
pos = _dump_list(self.patterns, buf, size, pos)
|
|
pos = _emit(buf, size, pos, ")")
|
|
return pos
|