按规范「注释只讲解作用,不掺杂历史原因,尽量英文」:把 includes 下 75 个 .vp 的中文 # 注释与三引号 docstring 改写为英文,每个注释块压到 2 行以内。 保留要点:作用、约束、危险警告(不得/必须/注意)、ABI 与类型约定(如 BYTEPTR | t.CPtr = i8** 的跨模块坍缩坑)、位宽/长度约定、fail-fast 要求、内存与句柄所有权。 未动:文件头注释(规范豁免,仍是中文)、运行时中文字符串常量(printf / fatal_error / MlirFail 等用户可见消息,属代码字面量)。 验证:独立脚本比对 HEAD 与工作区的代码骨架(剥离 # 注释与三引号 docstring 后的非空行)——75 个文件逐字节一致;行尾 CRLF/LF 未被改动;全文剩余中文仅 547 行头注释、0 条行尾注释。bootstrap 三重重编 SUCCESS;全量 77/77 顺序回归通过。
48 lines
1.9 KiB
Plaintext
48 lines
1.9 KiB
Plaintext
import t
|
|
|
|
|
|
# Variant type tags (fixed enumeration + custom pointer type)
|
|
define VARIANT_NONE = 0 # Untyped / null value
|
|
define VARIANT_INT = 1 # int (i64)
|
|
define VARIANT_FLOAT = 2 # float (f32)
|
|
define VARIANT_DOUBLE = 3 # double (f64)
|
|
define VARIANT_STR = 4 # str (i8*)
|
|
define VARIANT_DICT = 5 # dict*
|
|
define VARIANT_LIST = 6 # list*
|
|
define VARIANT_BOOL = 7 # bool (i1/i32)
|
|
define VARIANT_PTR = 8 # Custom pointer type
|
|
|
|
|
|
class Variant:
|
|
"""Generic variant type storing a type tag and untyped pointer.
|
|
Used for universal object storage inside dict/list, supports nesting and JSON serialization.
|
|
|
|
Usage:
|
|
v: Variant = Variant(VARIANT_INT, t.CNeedPtr(42))
|
|
v2: Variant = Variant(VARIANT_STR, t.CNeedPtr("hello"))
|
|
|
|
# Retrieve value
|
|
ptr: rawptr = v.ptr
|
|
i: int = *((int*)(ptr)) # int values are stored as i32, so read them back through an int* view
|
|
"""
|
|
vtype: int
|
|
ptr: rawptr
|
|
|
|
def __init__(self, vtype: int, val: t.CNeedPtr):
|
|
"""Construct Variant instance.
|
|
t.CNeedPtr automatically obtains address of val (non-pointer values will be allocated on stack and stored).
|
|
Note: The lifecycle of val is managed by the caller; Variant only stores a pointer reference.
|
|
"""
|
|
self.vtype = vtype
|
|
self.ptr = val
|
|
|
|
def is_none(self) -> int: return self.vtype == VARIANT_NONE
|
|
def is_int(self) -> int: return self.vtype == VARIANT_INT
|
|
def is_float(self) -> int: return self.vtype == VARIANT_FLOAT
|
|
def is_double(self) -> int: return self.vtype == VARIANT_DOUBLE
|
|
def is_str(self) -> int: return self.vtype == VARIANT_STR
|
|
def is_dict(self) -> int: return self.vtype == VARIANT_DICT
|
|
def is_list(self) -> int: return self.vtype == VARIANT_LIST
|
|
def is_bool(self) -> int: return self.vtype == VARIANT_BOOL
|
|
def is_ptr(self) -> int: return self.vtype == VARIANT_PTR
|