- json/__init__.vp、math.vp:rawptr[T] 改为 T* - ast/base.vp、ast/stmts.vp:@t.CVTable 改为 virtual class - argparse.vp:t.CPtr(...) 改为 str*(...) - stdarg.vp:arg(type: t.CType) 改为 t.CPtr - stdint.vp:FSIZE_t/LBA_t 换成 LPCSTR - memhub.vp:import 整理、去掉 t.CDefine 前缀、t.CPtr 改 rawptr、格式对齐 - ast/parser.vp、math.vp、linkedlist.vp:对齐与格式清理
238 lines
7.4 KiB
Plaintext
238 lines
7.4 KiB
Plaintext
import t, c
|
||
from stdint import *
|
||
import memhub
|
||
import string
|
||
import viperio
|
||
|
||
# ============================================================
|
||
# JSON 值类型枚举
|
||
# ============================================================
|
||
JSON_NULL: t.CDefine = 0
|
||
JSON_BOOL: t.CDefine = 1
|
||
JSON_INT: t.CDefine = 2
|
||
JSON_FLOAT: t.CDefine = 3
|
||
JSON_STRING: t.CDefine = 4
|
||
JSON_ARRAY: t.CDefine = 5
|
||
JSON_OBJECT: t.CDefine = 6
|
||
|
||
|
||
# ============================================================
|
||
# JsonValue - JSON 值节点
|
||
# 使用 mpool 分配:JsonValue(pool) 从池中分配内存
|
||
# ============================================================
|
||
class JsonValue:
|
||
vtype: int
|
||
bool_val: int
|
||
int_val: int64
|
||
float_val: double
|
||
str_val: str
|
||
next: 'JsonValue' | t.CPtr
|
||
key: str
|
||
child: 'JsonValue' | t.CPtr
|
||
child_count: sizet
|
||
|
||
def __new__(self, pool: rawptr[memhub.MemManager]):
|
||
"""从 mpool 分配 JsonValue 结构体内存并零初始化"""
|
||
ptr: JsonValue* = pool.alloc(JsonValue.__sizeof__())
|
||
if ptr:
|
||
string.memset(ptr, 0, JsonValue.__sizeof__())
|
||
return ptr
|
||
|
||
def type(self) -> int:
|
||
return self.vtype
|
||
|
||
def is_null(self) -> bool:
|
||
return self.vtype == JSON_NULL
|
||
|
||
def is_bool(self) -> bool:
|
||
return self.vtype == JSON_BOOL
|
||
|
||
def is_int(self) -> bool:
|
||
return self.vtype == JSON_INT
|
||
|
||
def is_float(self) -> bool:
|
||
return self.vtype == JSON_FLOAT
|
||
|
||
def is_string(self) -> bool:
|
||
return self.vtype == JSON_STRING
|
||
|
||
def is_array(self) -> bool:
|
||
return self.vtype == JSON_ARRAY
|
||
|
||
def is_object(self) -> bool:
|
||
return self.vtype == JSON_OBJECT
|
||
|
||
def as_bool(self) -> bool:
|
||
return self.bool_val != 0
|
||
|
||
def as_int(self) -> int64:
|
||
return self.int_val
|
||
|
||
def as_float(self) -> double:
|
||
return self.float_val
|
||
|
||
def as_string(self) -> str:
|
||
return self.str_val
|
||
|
||
def __len__(self) -> sizet:
|
||
return self.child_count
|
||
|
||
def __getitem__(self, key: str) -> 'JsonValue' | t.CPtr:
|
||
"""Python风格取值:v["key"] 取对象字段"""
|
||
if self.vtype != JSON_OBJECT:
|
||
return None
|
||
if key == None:
|
||
return None
|
||
cur: JsonValue* = self.child
|
||
while cur != None:
|
||
if cur.key != None:
|
||
if string.strcmp(cur.key, key) == 0:
|
||
return cur
|
||
cur = cur.next
|
||
return None
|
||
|
||
def __setitem__(self, key: str, val: 'JsonValue' | t.CPtr):
|
||
"""Python风格赋值:v["key"] = val"""
|
||
if self.vtype != JSON_OBJECT or key == None or val == None:
|
||
return
|
||
cur: JsonValue* = self.child
|
||
while cur != None:
|
||
if cur.key != None:
|
||
if string.strcmp(cur.key, key) == 0:
|
||
cur.vtype = val.vtype
|
||
cur.bool_val = val.bool_val
|
||
cur.int_val = val.int_val
|
||
cur.float_val = val.float_val
|
||
cur.str_val = val.str_val
|
||
cur.child = val.child
|
||
cur.child_count = val.child_count
|
||
return
|
||
cur = cur.next
|
||
# key 不存在,追加新子节点
|
||
klen: sizet = string.strlen(key)
|
||
kbuf: str = self._pool_alloc(klen + 1)
|
||
if kbuf != None:
|
||
string.memcpy(kbuf, key, klen + 1)
|
||
val.key = kbuf
|
||
self._append_child(val)
|
||
|
||
def get_item(self, index: sizet) -> 'JsonValue' | t.CPtr:
|
||
"""按索引取数组元素:v.get_item(0)"""
|
||
if self.vtype != JSON_ARRAY and self.vtype != JSON_OBJECT:
|
||
return None
|
||
cur: JsonValue* = self.child
|
||
i: sizet = 0
|
||
while cur != None:
|
||
if i == index:
|
||
return cur
|
||
cur = cur.next
|
||
i += 1
|
||
return None
|
||
|
||
def _pool_alloc(self, size: sizet) -> str:
|
||
"""内部方法:从当前对象的 mpool 分配内存(预留,暂返回 None)"""
|
||
return None
|
||
|
||
def _append_child(self, child: 'JsonValue' | t.CPtr):
|
||
if self.child == None:
|
||
self.child = child
|
||
else:
|
||
cur: JsonValue* = self.child
|
||
while cur.next != None:
|
||
cur = cur.next
|
||
cur.next = child
|
||
self.child_count += 1
|
||
|
||
|
||
# ============================================================
|
||
# 构造函数:创建各类 JsonValue(从 mpool 分配)
|
||
# ============================================================
|
||
|
||
def null(pool: rawptr[memhub.MemManager]) -> JsonValue*:
|
||
v: JsonValue* = JsonValue(pool)
|
||
if v == None: return None
|
||
v.vtype = JSON_NULL
|
||
return v
|
||
|
||
def bool_val(pool: rawptr[memhub.MemManager], val: bool) -> JsonValue*:
|
||
v: JsonValue* = JsonValue(pool)
|
||
if v == None: return None
|
||
v.vtype = JSON_BOOL
|
||
v.bool_val = 1 if val else 0
|
||
return v
|
||
|
||
def int_val(pool: rawptr[memhub.MemManager], val: int64) -> JsonValue*:
|
||
v: JsonValue* = JsonValue(pool)
|
||
if v == None: return None
|
||
v.vtype = JSON_INT
|
||
v.int_val = val
|
||
return v
|
||
|
||
def float_val(pool: rawptr[memhub.MemManager], val: double) -> JsonValue*:
|
||
v: JsonValue* = JsonValue(pool)
|
||
if v == None: return None
|
||
v.vtype = JSON_FLOAT
|
||
v.float_val = val
|
||
return v
|
||
|
||
def string_val(pool: rawptr[memhub.MemManager], val: str) -> JsonValue*:
|
||
v: JsonValue* = JsonValue(pool)
|
||
if v == None: return None
|
||
v.vtype = JSON_STRING
|
||
if val != None:
|
||
slen: sizet = string.strlen(val)
|
||
buf: str = pool.alloc(slen + 1)
|
||
if buf != None:
|
||
string.memcpy(buf, val, slen + 1)
|
||
v.str_val = buf
|
||
return v
|
||
|
||
def array(pool: rawptr[memhub.MemManager]) -> JsonValue*:
|
||
v: JsonValue* = JsonValue(pool)
|
||
if v == None: return None
|
||
v.vtype = JSON_ARRAY
|
||
return v
|
||
|
||
def object(pool: rawptr[memhub.MemManager]) -> JsonValue*:
|
||
v: JsonValue* = JsonValue(pool)
|
||
if v == None: return None
|
||
v.vtype = JSON_OBJECT
|
||
return v
|
||
|
||
# 向数组追加元素
|
||
def array_append(pool: rawptr[memhub.MemManager], arr: JsonValue*, item: JsonValue*):
|
||
if arr == None or item == None: return
|
||
if arr.vtype != JSON_ARRAY: return
|
||
arr._append_child(item)
|
||
|
||
# 向对象设置键值对
|
||
def object_set(pool: rawptr[memhub.MemManager], obj: JsonValue*, key: str, val: JsonValue*):
|
||
if obj == None or key == None or val == None: return
|
||
if obj.vtype != JSON_OBJECT: return
|
||
cur: JsonValue* = obj.child
|
||
while cur != None:
|
||
if cur.key != None:
|
||
if string.strcmp(cur.key, key) == 0:
|
||
cur.vtype = val.vtype
|
||
cur.bool_val = val.bool_val
|
||
cur.int_val = val.int_val
|
||
cur.float_val = val.float_val
|
||
cur.str_val = val.str_val
|
||
cur.child = val.child
|
||
cur.child_count = val.child_count
|
||
return
|
||
cur = cur.next
|
||
klen: sizet = string.strlen(key)
|
||
kbuf: str = pool.alloc(klen + 1)
|
||
if kbuf != None:
|
||
string.memcpy(kbuf, key, klen + 1)
|
||
val.key = kbuf
|
||
obj._append_child(val)
|
||
|
||
|
||
# ============================================================
|
||
# 导入子模块
|
||
# ============================================================
|
||
from .__parser import parse
|
||
from .__writer import write
|