stage3 ai编写并提供测试用例并重构nob

This commit is contained in:
zzy committed 2026-10-31 11:06:00 +08:00
1 parent 2c531f23de
commit b4831c8d1c
150 files changed
+15838 -231

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
#!builtin
// splc3 的预置头:把编译器底层能力以 `#builtin` 声明暴露给用户/comptime 代码。
// `#builtin(<id>)` 的 id 必须命中 stage3/intrinsic.spl 的 intrinsic_of_id。
// 查询 / 断言类。静态类型实参由 sema 折成常量;动态类型值
// (comptime VM 里的 type 句柄)走宿主桥,因此同时声明为 extern。
#builtin(sizeof) #extern(builtin) comptime fn sizeof(t: type): usize;
#builtin(bitsizeof) #extern(builtin) comptime fn bitsizeof(t: type): usize;
#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;
// spl_ir 接口:单写/单读,经编译器生成的 VM ABI 调用(无显式桥)。
// `#extern(builtin)` 表示后端外部实现(等价于当前后端的 `#extern(vm)`)。
#builtin(echo) #extern(builtin) comptime fn echo(x: u8): u8;
+83
View File
@@ -0,0 +1,83 @@
// 稳定错误码:编译器诊断和 @meta.error 共用同一张表。
type ErrorCode = enum {
Syntax,
DuplicateDecl,
UndefinedIdentifier,
TypeMismatch,
ArgCount,
NotCallable,
NoField,
ImplHookNotFound,
ImplHookNotMacro,
ImplHookBadReturn,
ImplHookBadWrapper,
ShapeMismatch,
ImplHookArgCount,
ShapeParamNotShape,
MacroCode,
FmtMissingArg,
FmtNonliteral,
FmtBadType,
HookNotMacro,
ParamHook,
Decorator,
DefaultMismatch,
};
fn number(code: ErrorCode): usize {
match code {
.Syntax => { ret 2; },
.DuplicateDecl => { ret 107; },
.UndefinedIdentifier => { ret 101; },
.TypeMismatch => { ret 103; },
.ArgCount => { ret 104; },
.NotCallable => { ret 105; },
.NoField => { ret 106; },
.ImplHookNotFound => { ret 301; },
.ImplHookNotMacro => { ret 302; },
.ImplHookBadReturn => { ret 303; },
.ImplHookBadWrapper => { ret 304; },
.ShapeMismatch => { ret 305; },
.ImplHookArgCount => { ret 306; },
.ShapeParamNotShape => { ret 308; },
.MacroCode => { ret 309; },
.FmtMissingArg => { ret 310; },
.FmtNonliteral => { ret 311; },
.FmtBadType => { ret 312; },
.HookNotMacro => { ret 313; },
.ParamHook => { ret 314; },
.Decorator => { ret 315; },
.DefaultMismatch => { ret 704; },
}
ret 0;
}
fn label(code: ErrorCode): []u8 {
match code {
.Syntax => { ret "E0002"; },
.DuplicateDecl => { ret "E0107"; },
.UndefinedIdentifier => { ret "E0101"; },
.TypeMismatch => { ret "E0103"; },
.ArgCount => { ret "E0104"; },
.NotCallable => { ret "E0105"; },
.NoField => { ret "E0106"; },
.ImplHookNotFound => { ret "E0301"; },
.ImplHookNotMacro => { ret "E0302"; },
.ImplHookBadReturn => { ret "E0303"; },
.ImplHookBadWrapper => { ret "E0304"; },
.ShapeMismatch => { ret "E0305"; },
.ImplHookArgCount => { ret "E0306"; },
.ShapeParamNotShape => { ret "E0308"; },
.MacroCode => { ret "E0309"; },
.FmtMissingArg => { ret "E0310"; },
.FmtNonliteral => { ret "E0311"; },
.FmtBadType => { ret "E0312"; },
.HookNotMacro => { ret "E0313"; },
.ParamHook => { ret "E0314"; },
.Decorator => { ret "E0315"; },
.DefaultMismatch => { ret "E0704"; },
}
ret "";
}
fn main() {}
+25
View File
@@ -0,0 +1,25 @@
// 元编程模块:HIR 反射接口。必须显式 `type meta = @import("meta")` 后使用。
type Ref = @import("./spl_ir.spl").Ref;
type Node = @import("./spl_ir.spl").Node;
type ErrorCode = @import("./error_code.spl").ErrorCode;
type .{ .number } = @import("./error_code.spl");
// hook 的固定前缀签名;`#hook(kind)` 只是声明信息,实际种类由签名和挂载位置决定。
// 自定义参数接在固定前缀之后,因此这三个别名只约束前缀,不约束完整签名。
type Decorator = fn(Ref): Ref;
type ParamHook = fn(Ref, Ref): Ref;
type ImplHook = fn(type, type): Ref;
// 四个原语都由 comptime VM 的宿主桥实现:参数/结果可能来自运行中的 VM,
// 不能依赖 sema 在分析期把实参折成常量。
#builtin(reflect) #extern(builtin) comptime fn reflect(t: type): Ref;
#builtin(build) #extern(builtin) comptime fn build(n: Ref): type;
#builtin(inspect) #extern(builtin) comptime fn inspect(n: Ref): Node;
#builtin(construct) #extern(builtin) comptime fn construct(n: Node): Ref;
// 宏诊断:内部只接收数字,公共入口使用 ErrorCode。
#builtin(error) #extern(builtin) comptime fn error_raw(code: usize): void;
comptime fn error(code: ErrorCode): void {
@error_raw(number(code));
}
+178
View File
@@ -0,0 +1,178 @@
// Expect retcode = 0
// SPL 语言节点接口:一份描述语言自身的 Node 结构,取代 hir.spl 与 reflect.spl 两份重复节点集。
// 面向前端(ast2hir/sema)与 comptime/builtin 共用;只谈语言语义,不带存储/编码。
// 构造与查询各一个:construct 写(对标 Zig @Type),inspect 读(对标 @typeInfo);
// 实现在 spl_ir_builder.spl(arena),本文件零依赖、不含 Vec。
// 节点句柄:指向 HIRNodeContainer 内节点的稳定指针,0 = 空/哨兵。
// 当前用容器内索引实现,保证跨构造/克隆仍然稳定且可序列化。
type Ref = usize;
// Node = 语言结构 kind + 语义 tag 列表。tag 空即无,任何节点都能挂;
// loc 是诊断用源位置;line/col 为 0 表示合成节点没有源位置。
type SourceLoc = struct {
line: usize,
col: usize,
start: usize,
end: usize,
};
type Node = struct {
kind: Kind,
tags: []Ref,
loc: SourceLoc,
// 运算符与聚合种类嵌在 Kind 内,避免为它们各立外层类型。
type Kind = enum {
type Op = enum {
Unknown,
Assign,
Add,
Sub,
Mul,
Div,
Mod,
LogicAnd,
BitAnd,
LogicOr,
BitOr,
LogicNot,
BitNot,
BitXor,
LeftShift,
RightShift,
Equal,
NotEqual,
LessThan,
LessEqual,
GreaterThan,
GreaterEqual,
}
type Agg = enum {
Struct,
Union,
Enum,
Shape,
}
Error,
Unit,
Undefined,
// 所有字面量保留原文,字面量语义与数值解析留给 sema(面向语言,灵活性优先)。
LitInt: []u8,
LitFloat: []u8,
LitBool: []u8,
LitChar: []u8,
LitStr: []u8,
ConstAgg: []u8,
Tag: struct { is_at: bool, name: []u8, args: []Ref },
TopBlock: Ref,
Label: []u8,
FnDecl: struct { is_comptime: bool, injected: []Ref, name: []u8, params: []Ref, ret_ty: Ref, body: Ref },
ParamDecl: struct { is_comptime: bool, by_ref: bool, name: []u8, ty: Ref },
TypeDecl: struct { name: []u8, ty: Ref },
FieldDecl: struct { name: []u8, ty: Ref },
VarDecl: struct { is_comptime: bool, name: []u8, ty: Ref, init: Ref },
ConstDecl: struct { is_comptime: bool, name: []u8, ty: Ref, init: Ref },
Alloc: struct { ty: Ref, value: Ref },
Block: struct { stmts: []Ref, tail: Ref },
If: struct { cond: Ref, then: Ref, els: Ref, ret_ty: Ref },
IfVar: struct { pattern: Ref, init: Ref, then: Ref, els: Ref },
Destructure: struct { pattern: Ref, init: Ref },
Loop: struct { label: Ref, body: Ref, ret_ty: Ref },
Iter: struct { label: Ref, inputs: []Ref, captures: []Ref, body: Ref, else_body: Ref },
Break: struct { label: Ref, value: Ref },
Continue: struct { label: Ref },
Return: Ref,
Defer: Ref,
ErrDefer: Ref,
Try: Ref,
Catch: struct { value: Ref, pattern: Ref, handler: Ref },
IsNonNull: Ref,
OptionalPayload: Ref,
IsTag: struct { value: Ref, name: []u8 },
TagPayload: Ref,
Match: struct { operand: Ref, arms: []Ref },
MatchArm: struct { pattern: Ref, body: Ref },
PatWild,
PatLit: Ref,
PatBind: []u8,
PatTag: struct { name: []u8, sub: Ref },
PatList: []Ref,
PatTuple: []Ref,
PatField: struct { name: []u8, sub: Ref },
AggType: struct { kind: Agg, fields: []Ref, decls: []Ref, injected: []Ref },
ArrayType: struct { len: Ref, elem: Ref },
SliceType: Ref,
PtrType: Ref,
FnType: struct { params: []Ref, ret_ty: Ref },
TupleType: []Ref,
NamedType: []u8,
SelfType,
Import: []u8,
IntType: struct { bits: usize, signed: bool },
FloatType: struct { bits: usize },
Any,
Type,
Bool,
Never,
Field: struct { obj: Ref, name: []u8 },
Index: struct { obj: Ref, idx: Ref },
Range: struct { begin: Ref, end: Ref },
SliceExpr: struct { obj: Ref, begin: Ref, end: Ref },
Cast: struct { to: Ref, value: Ref },
Local: Ref,
Global: []u8,
DeclRef: []u8,
// 稳定函数引用:直接指向函数的 FnDecl Ref,绕过模块/类型名解析。
FnRef: Ref,
Addr: Ref,
Deref: Ref,
FieldLit: struct { name: []u8, value: Ref },
AggLit: struct { name: []u8, fields: []Ref },
ArrayLit: struct { elem_ty: Ref, elems: []Ref },
TupleLit: []Ref,
CompoundAssign: struct { op: Op, lval: Ref, rval: Ref },
Binary: struct { op: Op, lval: Ref, rval: Ref },
Unary: struct { op: Op, val: Ref },
Call: struct { is_comptime: bool, callee: Ref, args: []Ref },
Comptime: Ref,
}
}
// 单写:由 Node 结构构造节点,返回句柄。实现见 spl_ir_builder.spl。
fn construct(node: Node): Ref;
// 单读:由句柄取回 Node 结构。实现见 spl_ir_builder.spl。
fn inspect(r: Ref): Node;
fn main() {
var n: Node = .{ .kind = .LitInt("123") };
@assert(n.tags.len == 0, "tags default empty");
match n.kind {
.LitInt(s) => { @assert(s.len == 3, "lit text kept"); },
_ => { @assert(false, "expected lit int"); },
}
var b: Node = .{ .kind = .Binary(.{ .op = .Add, .lval = 1, .rval = 2 }) };
match b.kind {
.Binary(x) => {
match x.op {
.Add => {},
_ => { @assert(false, "expected add"); },
}
},
_ => { @assert(false, "expected binary"); },
}
}