stage2 提供近乎完整的功能但出现严重性能问题 添加stage0的原生性能记录 stage1添加更多内置函数 nob重构代码使其更加健壮
This commit is contained in:
1 parent
b5a7a811b3
commit
e02b0c633a
35 files changed
+14018
-216
No files matched your search
@@ -209,6 +209,7 @@ type Ast = @self;
|
||||
nodes: Vec, // Node
|
||||
root: usize,
|
||||
sources: *Sources, // 借用;可为 null(dump 时不解析行列)
|
||||
strings: Vec, // ByteBuffer:已解码的字符串字面量字节(ByteView 指向其中)
|
||||
|
||||
type AstResult = enum {
|
||||
Ok: Ast,
|
||||
@@ -231,6 +232,7 @@ fn new(sources: *Sources): AstResult {
|
||||
.nodes = Vec.blank(@sizeof(Node), @alignof(Node)),
|
||||
.root = 0,
|
||||
.sources = sources,
|
||||
.strings = Vec.blank(@sizeof(ByteBuffer), @alignof(ByteBuffer)),
|
||||
};
|
||||
var sentinel = new_node(.None, span_none());
|
||||
match Vec.push(&ast.nodes, &sentinel as *u8) {
|
||||
@@ -257,6 +259,15 @@ fn drop(self: *Ast) {
|
||||
i = i + 1;
|
||||
}
|
||||
Vec.drop(&self.nodes);
|
||||
var k: usize = 0;
|
||||
while k < self.strings.len {
|
||||
match Vec.get(&self.strings, k) {
|
||||
.Ok(ptr) => { ByteBuffer.drop(ptr as *ByteBuffer); },
|
||||
.Err(error) => {},
|
||||
}
|
||||
k = k + 1;
|
||||
}
|
||||
Vec.drop(&self.strings);
|
||||
self.root = 0;
|
||||
}
|
||||
|
||||
@@ -281,6 +292,26 @@ fn alloc(self: *Ast, body: Body, span: Span): usize {
|
||||
ret self.nodes.len - 1;
|
||||
}
|
||||
|
||||
// 注册一段已解码的字符串字节(Ast 拥有;ByteView 指向其中,生命周期同 Ast)。
|
||||
// 空字符串也保证非空 data 指针(镜像 stage1:空字面量仍有稳定的非空字节地址)。
|
||||
fn push_string(self: *Ast, bytes: ByteBuffer): ByteView {
|
||||
var b = bytes;
|
||||
if b.storage.len == 0 {
|
||||
match ByteBuffer.reserve(&b, 1) { .Ok => {}, .Err(error) => {} }
|
||||
}
|
||||
@assert(b.storage.data != null, "ast string pool must be non-null");
|
||||
// 直接构造,避免 from_slice 把空串规范成 null 视图。
|
||||
var view = ByteView { .ptr = b.storage.data, .len = b.storage.len };
|
||||
match Vec.push(&self.strings, &b as *u8) {
|
||||
.Ok => { ret view; },
|
||||
.Err(error) => {
|
||||
ByteBuffer.drop(&b);
|
||||
ret ByteView.empty();
|
||||
},
|
||||
}
|
||||
ret ByteView.empty();
|
||||
}
|
||||
|
||||
fn count(self: *Ast): usize {
|
||||
ret self.nodes.len;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user