ai: 生成代码准备重构前的一次提交

This commit is contained in:
zzy committed 2026-11-30 12:58:19 +08:00
1 parent 936a8d0e88
commit 032102f1d7
375 files changed
+4619 -7609

No files matched your search

@@ -0,0 +1,15 @@
// Expect retcode = 0
// §7:comptime fn 可以生成类型。
comptime fn Pair(T: type): type {
ret struct {
a: T,
b: T,
};
}
fn main(): void {
var p: @Pair(i32) = .{ .a = 1, .b = 2 };
@assert(p.a + p.b == 3, "generated aggregate");
}
+18
View File
@@ -0,0 +1,18 @@
// Expect retcode = 0
// §7:@self { ... } 以当前聚合类型构造实例。
type Point = struct {
x: i32,
y: i32,
fn origin(): @self {
ret @self { .x = 0, .y = 0 };
}
};
fn main(): void {
var p = Point.origin();
@assert(p.x == 0, "self init x");
@assert(p.y == 0, "self init y");
}
+25
View File
@@ -0,0 +1,25 @@
// Expect retcode = 0
// §16.3:@meta.reflect / inspect 读取聚合字段。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
type Pair = struct {
a: i32,
b: bool,
};
comptime fn field_count_of(T: type): usize {
var r = @meta.reflect(T);
var n = @meta.inspect(r);
match n.kind {
.AggType(x) => { ret x.fields.len(); },
_ => {},
}
ret 0;
}
fn main(): void {
@assert(@field_count_of(Pair) == 2, "field count");
}
+10
View File
@@ -0,0 +1,10 @@
// Expect retcode = 0
// §16.3:@type_of(expr) 返回语义类型值。
fn main(): void {
var x: i32 = 1;
var y: bool = true;
@assert(@sizeof(@type_of(x)) == 4, "i32 size");
@assert(@sizeof(@type_of(y)) == 1, "bool size");
}
+22
View File
@@ -0,0 +1,22 @@
// Expect retcode = 0
// §4:@default 是库 param hook;显式实参优先。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
#hook(param)
macro fn default(callee: Ref, value: Ref, implicit: Ref, fallback: i32): Ref {
if value != 0 { ret value; }
if fallback != 42 { @meta.error(.DefaultMismatch); ret 0; }
ret @meta.construct(.{ .kind = .LitInt("42") });
}
fn f(@default(42) x: i32): i32 {
ret x;
}
fn main(): void {
@assert(f() == 42, "default value");
@assert(f(7) == 7, "explicit value");
}
@@ -0,0 +1,9 @@
// Expect retcode = 0
// §16.3:Ref 句柄 → 语义类型。
type meta = @import("../support/meta.spl");
fn main(): void {
var t = @meta.type_at(0);
}
+32
View File
@@ -0,0 +1,32 @@
// Expect retcode = 0
// §7:同一参数上的多个 param hook 从右向左执行。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
#hook(param)
macro fn plus_one(callee: Ref, value: Ref, implicit: Ref): Ref {
var one = @meta.construct(.{ .kind = .LitInt("1") });
var sum = @meta.construct(.{
.kind = .Binary(.{ .op = .Add, .lval = value, .rval = one }),
});
ret sum;
}
#hook(param)
macro fn times_two(callee: Ref, value: Ref, implicit: Ref): Ref {
var two = @meta.construct(.{ .kind = .LitInt("2") });
var product = @meta.construct(.{
.kind = .Binary(.{ .op = .Mul, .lval = value, .rval = two }),
});
ret product;
}
fn f(@times_two @plus_one x: i32): i32 {
ret x;
}
fn main(): void {
@assert(f(3) == 8, "rightmost hook first");
}
+19
View File
@@ -0,0 +1,19 @@
// Expect retcode = 0
// §7:hook 只在参数上显式标记时生效,不会被普通参数扫描到。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
#hook(param)
macro fn unused(callee: Ref, value: Ref, implicit: Ref): Ref {
ret @meta.construct(.{ .kind = .LitInt("9") });
}
fn f(x: i32): i32 {
ret x;
}
fn main(): void {
@assert(f(3) == 3, "no implicit hook scan");
}
+25
View File
@@ -0,0 +1,25 @@
// Expect retcode = 0
// §16.3:@meta.reflect 与 @meta.inspect 读取聚合字段。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
type Pair = struct {
a: i32,
b: bool,
};
comptime fn field_count(): usize {
var r = @meta.reflect(Pair);
var n = @meta.inspect(r);
match n.kind {
.AggType(x) => { ret x.fields.len(); },
_ => {},
}
ret 0;
}
fn main(): void {
@assert(@field_count() == 2, "field count");
}
+22
View File
@@ -0,0 +1,22 @@
// Expect retcode = 0
// §16.3:@meta.build 把类型节点转回可 sizeof 的类型。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
type Arr = [4]u8;
comptime fn array_size(): usize {
var r = @meta.reflect(Arr);
var n = @meta.inspect(r);
match n.kind {
.ArrayType(_) => { ret @sizeof(@meta.build(r)); },
_ => {},
}
ret 0;
}
fn main(): void {
@assert(@array_size() == 4, "array size");
}
+12
View File
@@ -0,0 +1,12 @@
// Expect retcode = 0
// §7:comptime fn 可以作为数组长度。
comptime fn five(): usize {
ret 5;
}
fn main(): void {
var a: [@five()]u8 = .[];
@assert(a.len() == 5, "comptime length");
}
@@ -0,0 +1,36 @@
// Expect retcode = 0
// §7:decorator 是声明转换;返回的声明替换原声明。
type meta = @import("../support/meta.spl");
type .{ .SemaNode, .Ref } = @import("../support/sema.spl");
#hook(decorator)
macro fn replace(node: Ref): Ref {
var none: [1]Ref = .[0];
var ret_ty = @meta.reflect(i32);
var seven = @meta.construct(.{ .kind = .LitInt("7") });
var ret_stmt = @meta.construct(.{ .kind = .Return(seven) });
var stmts: [1]Ref = .[ret_stmt];
var body = @meta.construct(.{
.kind = .Block(.{ .stmts = stmts[0..1], .tail = 0 }),
});
var fn_decl = SemaNode {
.kind = .FnDecl(.{
.name = "answer",
.params = none[0..0],
.ret_ty = ret_ty,
.body = body,
}),
};
ret @meta.construct(fn_decl);
}
@replace
fn answer(): i32 {
ret 42;
}
fn main(): void {
@assert(answer() == 7, "decorator replaces declaration");
}
+16
View File
@@ -0,0 +1,16 @@
// Expect retcode = 0
// §15:macro fn 可以接收可变参数。
type meta = @import("../support/meta.spl");
type .{ .SemaNode, .Ref } = @import("../support/sema.spl");
macro fn ignore_args(args: ...): Ref {
var n: SemaNode = .{ .kind = .LitInt("7") };
ret @meta.construct(n);
}
fn main(): void {
var x = @ignore_args(1, "two", true);
@assert(x == 7, "varargs macro");
}
@@ -0,0 +1,7 @@
// Expect compile_error E0325
// §15:可变参数只允许出现在 macro fn / comptime fn。
fn f(args: ...): void {}
fn main(): void {}
+16
View File
@@ -0,0 +1,16 @@
// Expect retcode = 0
// §7:@self 是当前聚合类型,用于返回类型和初始化。
type Box = struct {
value: i32,
fn make(v: i32): @self {
ret @self { .value = v };
}
};
fn main(): void {
var b = Box.make(5);
@assert(b.value == 5, "self type");
}
+23
View File
@@ -0,0 +1,23 @@
// Expect retcode = 0
// §16.3:@meta.reflect 读取指针类型。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
type P = *i32;
comptime fn is_ptr(): bool {
var r = @meta.reflect(P);
var n = @meta.inspect(r);
match n.kind {
.PtrType(_) => { ret true; },
_ => {},
}
ret false;
}
fn main(): void {
@assert(@is_ptr(), "pointer type");
@assert(@sizeof(P) == @sizeof(usize), "pointer size");
}
@@ -0,0 +1,45 @@
// Expect retcode = 0
// §7:多个 decorator 从右向左执行。
type meta = @import("../support/meta.spl");
type .{ .SemaNode, .Ref } = @import("../support/sema.spl");
comptime fn make_f(value: []u8): Ref {
var none: [1]Ref = .[0];
var ret_ty = @meta.reflect(i32);
var lit = @meta.construct(.{ .kind = .LitInt(value) });
var ret_stmt = @meta.construct(.{ .kind = .Return(lit) });
var stmts: [1]Ref = .[ret_stmt];
var body = @meta.construct(.{
.kind = .Block(.{ .stmts = stmts[0..1], .tail = 0 }),
});
var fn_decl = SemaNode {
.kind = .FnDecl(.{
.name = "f",
.params = none[0..0],
.ret_ty = ret_ty,
.body = body,
}),
};
ret @meta.construct(fn_decl);
}
#hook(decorator)
macro fn two(node: Ref): Ref {
ret @make_f("2");
}
#hook(decorator)
macro fn three(node: Ref): Ref {
ret @make_f("3");
}
@three @two
fn f(): i32 {
ret 1;
}
fn main(): void {
@assert(f() == 3, "rightmost decorator runs first");
}
+28
View File
@@ -0,0 +1,28 @@
// Expect retcode = 0
// §4:param hook 可以读取 callee 来识别调用目标。
type meta = @import("../support/meta.spl");
type .{ .SemaNode, .Ref } = @import("../support/sema.spl");
#hook(param)
macro fn check_callee(callee: Ref, value: Ref, implicit: Ref): Ref {
var n = @meta.inspect(callee);
match n.kind {
.FnDecl(f) => {
if f.name.len() == 3 && f.name[0] == 'r' && f.name[1] == 'u' && f.name[2] == 'n' {
ret value;
}
},
_ => {},
}
ret 0;
}
fn run(@check_callee x: i32): i32 {
ret x;
}
fn main(): void {
@assert(run(4) == 4, "param hook sees callee");
}
@@ -0,0 +1,22 @@
// Expect retcode = 0
// §4:同一个 hook 可以挂在多个参数上,缺省时各自补值。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
#hook(param)
macro fn default_seven(callee: Ref, value: Ref, implicit: Ref): Ref {
if value != 0 { ret value; }
ret @meta.construct(.{ .kind = .LitInt("7") });
}
fn add(@default_seven x: i32, @default_seven y: i32): i32 {
ret x + y;
}
fn main(): void {
@assert(add() == 14, "both defaults");
@assert(add(1) == 8, "second default");
@assert(add(1, 2) == 3, "no defaults");
}
@@ -0,0 +1,22 @@
// Expect retcode = 0
// §16.3:@meta.reflect 读取函数类型。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
type F = fn(i32): i32;
comptime fn is_fn(): bool {
var r = @meta.reflect(F);
var n = @meta.inspect(r);
match n.kind {
.FnType(_) => { ret true; },
_ => {},
}
ret false;
}
fn main(): void {
@assert(@is_fn(), "fn type");
}
@@ -0,0 +1,21 @@
// Expect retcode = 0
// §16.3:@meta.reflect → @meta.build → @sizeof 往返。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
type Pair = struct {
a: i32,
b: bool,
};
comptime fn roundtrip_size(): usize {
var r = @meta.reflect(Pair);
var t = @meta.build(r);
ret @sizeof(t);
}
fn main(): void {
@assert(@roundtrip_size() == @sizeof(Pair), "roundtrip size");
}
@@ -0,0 +1,9 @@
// Expect compile_error E0103
// §16.2:元编程错误应带 span。
type meta = @import("../support/meta.spl");
fn main(): void {
@meta.error_at(0, 1, "boom");
}
@@ -0,0 +1,19 @@
// Expect retcode = 0
// §7:comptime 类型函数可以嵌套实例化。
comptime fn Pair(T: type): type {
ret struct {
a: T,
b: T,
};
}
fn main(): void {
var p: @Pair(@Pair(i32)) = .{
.a = .{ .a = 1, .b = 2 },
.b = .{ .a = 3, .b = 4 },
};
@assert(p.a.a == 1, "nested a.a");
@assert(p.b.b == 4, "nested b.b");
}
+26
View File
@@ -0,0 +1,26 @@
// Expect retcode = 0
// §16.3:@meta.reflect 读取枚举的 tag 列表。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
type Color = enum {
Red,
Green,
Blue,
};
comptime fn enum_field_count(): usize {
var r = @meta.reflect(Color);
var n = @meta.inspect(r);
match n.kind {
.AggType(x) => { ret x.fields.len(); },
_ => {},
}
ret 0;
}
fn main(): void {
@assert(@enum_field_count() == 3, "enum tags");
}
+25
View File
@@ -0,0 +1,25 @@
// Expect retcode = 0
// §16.3:@meta.reflect 读取联合字段。
type meta = @import("../support/meta.spl");
type .{ .Ref } = meta;
type U = union {
a: i32,
b: f32,
};
comptime fn union_field_count(): usize {
var r = @meta.reflect(U);
var n = @meta.inspect(r);
match n.kind {
.AggType(x) => { ret x.fields.len(); },
_ => {},
}
ret 0;
}
fn main(): void {
@assert(@union_field_count() == 2, "union fields");
}
@@ -0,0 +1,12 @@
// Expect retcode = 0
// §7:comptime 类型函数可以作为类型实参。
comptime fn Id(T: type): type {
ret T;
}
fn main(): void {
var x: @Id(i32) = 7;
@assert(x == 7, "identity type");
}
+41
View File
@@ -0,0 +1,41 @@
// Expect retcode = 0
// §16.3:TypeInfo 视图。
type .{ .TypeInfo } = @import("../support/sema.spl");
type .{ .type_info } = @import("../support/meta.spl");
type Pair = struct {
a: i32,
b: bool,
};
comptime fn check_int(): bool {
match @type_info(i32) {
.Int(x) => { ret x.bits == 32 && x.signed; },
_ => {},
}
ret false;
}
comptime fn check_ptr(): bool {
match @type_info(*i32) {
.Ptr => { ret true; },
_ => {},
}
ret false;
}
comptime fn check_agg(): bool {
match @type_info(Pair) {
.Agg => { ret true; },
_ => {},
}
ret false;
}
fn main(): void {
@assert(@check_int(), "int");
@assert(@check_ptr(), "ptr");
@assert(@check_agg(), "agg");
}
+24
View File
@@ -0,0 +1,24 @@
// Expect retcode = 0
// §16.3:SemaNode 视图读取聚合字段。
type .{ .Ref, .SemaNode } = @import("../support/sema.spl");
type .{ .reflect, .inspect } = @import("../support/meta.spl");
type Pair = struct {
a: i32,
b: bool,
};
comptime fn count_fields(): usize {
var n = @inspect(@reflect(Pair));
match n.kind {
.AggType(x) => { ret x.fields.len(); },
_ => {},
}
ret 0;
}
fn main(): void {
@assert(@count_fields() == 2, "semnode fields");
}
+12
View File
@@ -0,0 +1,12 @@
// Expect retcode = 0
// §16.3:Ref 句柄 → 语义类型。
type .{ .Ref } = @import("../support/sema.spl");
type .{ .reflect } = @import("../support/meta.spl");
type .{ .at } = @import("../support/type_at.spl");
fn main(): void {
var r: Ref = @reflect(i32);
@assert(@sizeof(@at(r)) == 4, "type at");
}
+45
View File
@@ -0,0 +1,45 @@
// Expect retcode = 0
// §15:真正的变参宏——遍历实参并生成表达式。
type meta = @import("../support/meta.spl");
type .{ .Ref } = @import("../support/sema.spl");
macro fn sum(args: ...): Ref {
var t = @meta.inspect(args);
match t.kind {
.TupleLit(xs) => {
if xs.len == 0 {
ret @meta.construct(.{ .kind = .LitInt("0") });
}
var e0 = @meta.inspect(xs[0]);
var acc: Ref = 0;
match e0.kind {
.TupleLit(p) => { acc = p[0]; },
_ => {},
}
var i: usize = 1;
while i < xs.len {
var en = @meta.inspect(xs[i]);
match en.kind {
.TupleLit(p) => {
acc = @meta.construct(.{
.kind = .Binary(.{ .op = .Add, .lval = acc, .rval = p[0] }),
});
},
_ => {},
}
i = i + 1;
}
ret acc;
},
_ => {},
}
ret @meta.construct(.{ .kind = .LitInt("0") });
}
fn main(): void {
@assert(@sum(1, 2, 3) == 6, "sum three");
@assert(@sum(7) == 7, "sum one");
@assert(@sum(1, 2, 3, 4, 5) == 15, "sum five");
}
+35
View File
@@ -0,0 +1,35 @@
// Expect retcode = 0
// §15 / §16.7:变参宏按实参类型分派。
type meta = @import("../support/meta.spl");
type .{ .Ref } = @import("../support/sema.spl");
macro fn is_bool(args: ...): Ref {
var t = @meta.inspect(args);
match t.kind {
.TupleLit(xs) => {
var e0 = @meta.inspect(xs[0]);
match e0.kind {
.TupleLit(p) => {
var ty = @meta.build(p[2]);
var tn = @meta.inspect(@meta.reflect(ty));
match tn.kind {
.Bool => {
ret @meta.construct(.{ .kind = .LitBool("true") });
},
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret @meta.construct(.{ .kind = .LitBool("false") });
}
fn main(): void {
@assert(@is_bool(true), "bool");
@assert(!(@is_bool(1)), "int");
}
+21
View File
@@ -0,0 +1,21 @@
// Expect retcode = 0
// §4:@default 在第二个参数上,显式实参优先。
type meta = @import("../support/meta.spl");
type .{ .Ref } = @import("../support/sema.spl");
#hook(param)
macro fn default(callee: Ref, value: Ref, implicit: Ref, fallback: i32): Ref {
if value != 0 { ret value; }
ret @meta.construct(.{ .kind = .LitInt("42") });
}
fn f(x: i32, @default(42) y: i32): i32 {
ret x + y;
}
fn main(): void {
@assert(f(1) == 43, "default second");
@assert(f(1, 2) == 3, "explicit second");
}
@@ -0,0 +1,20 @@
// Expect retcode = 0
// §16.3:函数类型的 TypeInfo。
type .{ .TypeInfo } = @import("../support/sema.spl");
type .{ .type_info } = @import("../support/meta.spl");
type F = fn(i32): bool;
comptime fn is_fn(): bool {
match @type_info(F) {
.FnDecl(_) => { ret true; },
_ => {},
}
ret false;
}
fn main(): void {
@assert(@is_fn(), "fn type");
}
@@ -0,0 +1,60 @@
// Expect retcode = 0
// §7:decorator 追加方法到聚合类型。
type meta = @import("../support/meta.spl");
type .{ .SemaNode, .Ref } = @import("../support/sema.spl");
#hook(decorator)
macro fn add_answer(node: Ref): Ref {
var n = @meta.inspect(node);
match n.kind {
.AggType(a) => {
var none: [1]Ref = .[0];
var ret_ty = @meta.reflect(i32);
var forty_two = @meta.construct(.{ .kind = .LitInt("42") });
var ret_stmt = @meta.construct(.{ .kind = .Return(forty_two) });
var stmts: [1]Ref = .[ret_stmt];
var body = @meta.construct(.{
.kind = .Block(.{ .stmts = stmts[0..1], .tail = 0 }),
});
var answer = @meta.construct(.{
.kind = .FnDecl(.{
.name = "answer",
.params = none[0..0],
.ret = ret_ty,
.body = body,
}),
});
var buf: [8]Ref = .[0, 0, 0, 0, 0, 0, 0, 0];
var i: usize = 0;
while i < a.decls.len {
buf[i] = a.decls[i];
i = i + 1;
}
buf[a.decls.len] = answer;
var decls = buf[0..a.decls.len + 1];
var out: SemaNode = .{
.kind = .AggType(.{
.kind = a.kind,
.fields = a.fields,
.decls = decls,
}),
};
ret @meta.construct(out);
},
_ => {},
}
ret node;
}
@add_answer
type S = struct {
value: i32,
};
fn main(): void {
var s = S{ .value = 1 };
@assert(s.answer() == 42, "decorator appends method");
}
+16
View File
@@ -0,0 +1,16 @@
// Expect retcode = 0
// builtins 接口:sizeof、alignof、type_of。
type .{
.sizeof,
.alignof,
.type_of,
} = @import("../support/builtins.spl");
fn main(): void {
@assert(@sizeof(i32) == 4, "sizeof");
@assert(@alignof(i32) == 4, "alignof");
var x: i32 = 1;
@assert(@sizeof(@type_of(x)) == 4, "type_of");
}