spl 编写编译器基础框架 ai修复出现的stage2和stage3问题并提供新语法

This commit is contained in:
zzy committed 2026-11-07 21:52:02 +08:00
1 parent 126b979f72
commit 50ee126d44
87 files changed
+3168 -394

No files matched your search

+9
View File
@@ -86,6 +86,7 @@ type Body = enum {
},
ExprAggregateLit: struct { name: ByteView, fields: Vec },
ExprComptimeCall: struct { name: ByteView, args: Vec },
ExprShapeApply: struct { adapter: ByteView, shapes: Vec, args: Vec },
ExprTry: struct { value: usize }, // `try expr`
ExprCatch: struct { value: usize, pattern: usize, handler: usize }, // `expr catch <pat> { }`
ExprBlock: struct { stmts: Vec, tail_expr: usize },
@@ -129,6 +130,7 @@ type Body = enum {
TypeShapeApply: struct { base: ByteView, binds: Vec },
TypeAssocBind: struct { name: ByteView, ty: usize },
TypeShapeUse: struct { adapter: ByteView, shapes: Vec },
TypeShapeNoDefault: struct { shapes: Vec },
}
fn kind_name(body: Body): []u8 {
@@ -180,6 +182,7 @@ fn kind_name(body: Body): []u8 {
.ExprArrayLit(_) => { ret "array_literal"; },
.ExprAggregateLit(_) => { ret "aggregate_literal"; },
.ExprComptimeCall(_) => { ret "comptime_call"; },
.ExprShapeApply(_) => { ret "shape_apply"; },
.ExprTry(_) => { ret "try_expr"; },
.ExprCatch(_) => { ret "catch_expr"; },
.ExprComptimeInit(_) => { ret "comptime_init"; },
@@ -201,6 +204,7 @@ fn kind_name(body: Body): []u8 {
.TypeShapeApply(_) => { ret "type_shape_apply"; },
.TypeAssocBind(_) => { ret "type_assoc_bind"; },
.TypeShapeUse(_) => { ret "type_shape_use"; },
.TypeShapeNoDefault(_) => { ret "type_shape_no_default"; },
.TypeComptimeCall(_) => { ret "type_comptime_call"; },
.PatternIdent(_) => { ret "pattern_ident"; },
.PatternUnderscore => { ret "pattern_underscore"; },
@@ -530,6 +534,10 @@ fn dump_node(ast: *Ast, buf: *ByteBuffer, stack: *Vec, ref: usize, last: bool):
},
.ExprAggregateLit(a) => { ok = dump_list(ast, buf, stack, a.fields); },
.ExprComptimeCall(c) => { ok = dump_list(ast, buf, stack, c.args); },
.ExprShapeApply(x) => {
ok = dump_list(ast, buf, stack, x.shapes) &&
dump_list(ast, buf, stack, x.args);
},
.ExprTry(t2) => { ok = dump_opt(ast, buf, stack, t2.value, true); },
.ExprCatch(c2) => {
ok = dump_opt(ast, buf, stack, c2.value, false) &&
@@ -593,6 +601,7 @@ fn dump_node(ast: *Ast, buf: *ByteBuffer, stack: *Vec, ref: usize, last: bool):
.TypeShapeApply(a) => { ok = dump_list(ast, buf, stack, a.binds); },
.TypeAssocBind(a) => { ok = dump_opt(ast, buf, stack, a.ty, true); },
.TypeShapeUse(a) => { ok = dump_list(ast, buf, stack, a.shapes); },
.TypeShapeNoDefault(a) => { ok = dump_list(ast, buf, stack, a.shapes); },
.TypeComptimeCall(c) => { ok = dump_list(ast, buf, stack, c.args); },
.PatternIdent(x) => {},
.PatternUnderscore => {},
+47 -1
View File
@@ -773,6 +773,22 @@ fn parse_param_decl_impl(self: *Parser): usize {
name);
}
var ty = parse_type_expr(self);
// `A: X + Y`:参数位的 shape 约束列表。空 adapter 表示纯编译期约束,
// 由 sema 决定它是否只允许出现在类型生成器形参上。
if at_punct(self, .Add) {
var shapes = ref_vec();
match Vec.push(&shapes, &ty as *u8) { .Ok => {}, .Err(error) => {} }
while at_punct(self, .Add) {
advance(self);
var next = parse_type_expr(self);
if next == 0 { break; }
match Vec.push(&shapes, &next as *u8) { .Ok => {}, .Err(error) => {} }
}
ty = new_node(self, .TypeShapeUse(.{
.adapter = ByteView.empty(),
.shapes = shapes,
}), name);
}
ret new_node(self,
.ParamDecl(.{ .attr_list = attrs, .is_comptime = is_comptime, .name = name.text, .type_expr = ty }),
name);
@@ -1204,6 +1220,13 @@ fn parse_type_comptime_call(self: *Parser): usize {
var ok = kind_is_punct(start.kind, .At) || kind_is_punct(start.kind, .Sharp);
if !ok { ret 0; }
advance(self);
if at_punct(self, .LBracket) {
// `@[Shape]`:没有默认 adapter 的 shape 参数。
advance(self);
var shapes = parse_shape_list(self, true);
expect_punct(self, .RBracket, "']' in shape use");
ret new_node(self, .TypeShapeNoDefault(.{ .shapes = shapes }), start);
}
var name = parse_comptime_name(self);
if name.len == 0 { ret 0; }
var args = ref_vec();
@@ -1937,7 +1960,9 @@ fn parse_primary(self: *Parser): usize {
.Match => { ret parse_match_expr(self); },
.Try => {
advance(self);
var tv = parse_expr(self);
// try 是一元前缀运算符;`try x as T` 必须解析成 `(try x) as T`,
// 不能把 `as` 吞进 try 的操作数。
var tv = parse_prefix(self);
ret new_node(self, .ExprTry(.{ .value = tv }), t);
},
.Comptime => {
@@ -2176,8 +2201,29 @@ fn parse_comptime_call(self: *Parser): usize {
var ok = kind_is_punct(start.kind, .At) || kind_is_punct(start.kind, .Sharp);
if !ok { ret 0; }
advance(self);
if at_punct(self, .LBracket) {
parse_error(self, peek(self), "shape parameter requires adapter name in expression");
ret 0;
}
var name = parse_comptime_name(self);
if name.len == 0 { ret 0; }
if at_punct(self, .LBracket) {
advance(self);
var shapes = parse_shape_list(self, true);
if !expect_punct(self, .RBracket, "']' in shape application") { ret 0; }
if !at_punct(self, .LParen) {
parse_error(self, peek(self), "shape application requires '('");
ret 0;
}
advance(self);
var sargs = parse_expr_list(self);
if !expect_punct(self, .RParen, "')' in shape application") { ret 0; }
ret new_node(self, .ExprShapeApply(.{
.adapter = name,
.shapes = shapes,
.args = sargs,
}), start);
}
var args = ref_vec();
if at_punct(self, .LParen) {
advance(self);