stage3 ai编写新语法语义 同时提供fflush

This commit is contained in:
zzy committed 2026-11-05 09:48:43 +08:00
1 parent bb8761d392
commit 126b979f72
170 files changed
+10729 -3175

No files matched your search

+43 -1
View File
@@ -1,4 +1,5 @@
#!builtin
#!role(builtins)
// splc3 的预置头:把编译器底层能力以 `#builtin` 声明暴露给用户/comptime 代码。
// `#builtin(<id>)` 的 id 必须命中 stage3/intrinsic.spl 的 intrinsic_of_id。
@@ -10,8 +11,49 @@
#builtin(alignof) #extern(builtin) comptime fn alignof(t: type): usize;
#builtin(field_count) #extern(builtin) comptime fn field_count(t: type): usize;
#builtin(type_of) comptime fn type_of(v: any): type;
#builtin(self) comptime fn Self(): type;
#builtin(self) comptime fn self(): type;
// spl_ir 接口:单写/单读,经编译器生成的 VM ABI 调用(无显式桥)。
// `#extern(builtin)` 表示后端外部实现(等价于当前后端的 `#extern(vm)`)。
#builtin(echo) #extern(builtin) comptime fn echo(x: u8): u8;
// 运行期 panic:消息是动态切片,由宿主打印并终止。
#extern(vm) fn panic(msg: []u8): void;
type Ref = @import("./spl_ir.spl").Ref;
type Node = @import("./spl_ir.spl").Node;
#builtin(inspect) #extern(builtin) comptime fn inspect(n: Ref): Node;
#builtin(construct) #extern(builtin) comptime fn construct(n: Node): Ref;
// receiver hook:@Self 只挂在方法的第一个参数上。
// 参数类型是 *T 时取地址,是 T 时按值传递;已显式传 &x 的调用不再二次取地址。
#hook(param)
macro fn Self(callee: Ref, value: Ref): Ref {
if value == 0 { ret 0; }
var vn = @inspect(value);
match vn.kind {
.Addr(_) => { ret value; },
_ => {},
}
var cn = @inspect(callee);
match cn.kind {
.FnDecl(f) => {
var pn = @inspect(f.params[0]);
match pn.kind {
.ParamDecl(p) => {
var tn = @inspect(p.ty);
match tn.kind {
.PtrType(_) => { ret @construct(.{ .kind = .Addr(value) }); },
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret value;
}