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;
}
+44 -2
View File
@@ -22,6 +22,20 @@ type ErrorCode = enum {
ParamHook,
Decorator,
DefaultMismatch,
ShapeMethodMissing,
ShapeAssocMismatch,
ShapeAssocUnbound,
ShapeMethodConflict,
OpNotImplemented,
NotIterable,
TryNoSuccess,
CatchHandlerType,
TryErrorMismatch,
CatchPattern,
ErrdeferNoError,
DuplicateSuccess,
TryNoError,
LambdaCapture,
};
fn number(code: ErrorCode): usize {
@@ -47,7 +61,21 @@ fn number(code: ErrorCode): usize {
.HookNotMacro => { ret 313; },
.ParamHook => { ret 314; },
.Decorator => { ret 315; },
.DefaultMismatch => { ret 704; },
.DefaultMismatch => { ret 316; },
.ShapeMethodMissing => { ret 317; },
.ShapeAssocMismatch => { ret 318; },
.ShapeAssocUnbound => { ret 319; },
.ShapeMethodConflict => { ret 320; },
.OpNotImplemented => { ret 321; },
.NotIterable => { ret 322; },
.TryNoSuccess => { ret 701; },
.CatchHandlerType => { ret 702; },
.TryErrorMismatch => { ret 703; },
.CatchPattern => { ret 705; },
.ErrdeferNoError => { ret 706; },
.DuplicateSuccess => { ret 707; },
.TryNoError => { ret 708; },
.LambdaCapture => { ret 801; },
}
ret 0;
}
@@ -75,7 +103,21 @@ fn label(code: ErrorCode): []u8 {
.HookNotMacro => { ret "E0313"; },
.ParamHook => { ret "E0314"; },
.Decorator => { ret "E0315"; },
.DefaultMismatch => { ret "E0704"; },
.DefaultMismatch => { ret "E0316"; },
.ShapeMethodMissing => { ret "E0317"; },
.ShapeAssocMismatch => { ret "E0318"; },
.ShapeAssocUnbound => { ret "E0319"; },
.ShapeMethodConflict => { ret "E0320"; },
.OpNotImplemented => { ret "E0321"; },
.NotIterable => { ret "E0322"; },
.TryNoSuccess => { ret "E0701"; },
.CatchHandlerType => { ret "E0702"; },
.TryErrorMismatch => { ret "E0703"; },
.CatchPattern => { ret "E0705"; },
.ErrdeferNoError => { ret "E0706"; },
.DuplicateSuccess => { ret "E0707"; },
.TryNoError => { ret "E0708"; },
.LambdaCapture => { ret "E0801"; },
}
ret "";
}
+221
View File
@@ -0,0 +1,221 @@
#!role(lang)
// 语言内置类型的声明面。布局由 #repr(...) 映射到编译器内部表示,
// 方法体和 conformance 都写在这里。
type .{ .Iter, .Step, .Index, .IntoIter,
.Add, .Sub, .Mul, .Div, .Mod, .Neg,
.BitAnd, .BitOr, .BitXor, .Shl, .Shr, .BitNot,
.Eq, .Ord, .Ordering } = @import("ops");
#repr("slice")
#repr_field(_ptr, 0)
#repr_field(_len, 1)
comptime fn Slice(T: type): type {
ret struct: Iter[Item = T] + IntoIter[Item = T, Iter = @self] + Index[Output = T] {
_ptr: *T,
_len: usize,
fn len(self: @self): usize {
ret self._len;
}
fn as_ptr(@Self self: *@self): *T {
ret self._ptr;
}
fn from_raw_parts(p: *T, n: usize): @self {
ret .{ ._ptr = p, ._len = n };
}
fn index(@Self self: *@self, i: usize): *T {
ret self._ptr + i;
}
fn iter(@Self self: *@self): @self {
ret self.*;
}
fn next(@Self self: *@self): @Step(T) {
if self._len == 0 {
ret .Done;
}
var v = self._ptr[0];
self._ptr = self._ptr + 1;
self._len = self._len - 1;
ret .Item(v);
}
};
}
#repr("array")
comptime fn Array(T: type, N: usize): type {
ret struct: IntoIter[Item = T, Iter = @Slice(T)] + Index[Output = T] {
#builtin(array_len)
fn len(self: @self): usize;
#builtin(array_index)
fn index(@Self self: *@self, i: usize): *T;
#builtin(array_iter)
fn iter(@Self self: *@self): @Slice(T);
#builtin(array_as_ptr)
fn as_ptr(@Self self: *@self): *T;
};
}
#repr("int")
comptime fn Int(bits: usize, signed: bool): type {
ret struct: Add[Rhs = @self, Output = @self]
+ Sub[Rhs = @self, Output = @self]
+ Mul[Rhs = @self, Output = @self]
+ Div[Rhs = @self, Output = @self]
+ Mod[Rhs = @self, Output = @self]
+ Neg[Output = @self]
+ BitAnd[Rhs = @self, Output = @self]
+ BitOr[Rhs = @self, Output = @self]
+ BitXor[Rhs = @self, Output = @self]
+ Shl[Rhs = @self, Output = @self]
+ Shr[Rhs = @self, Output = @self]
+ BitNot[Output = @self]
+ Eq[Rhs = @self]
+ Ord[Rhs = @self] {
#op(add)
fn add(lhs: @self, rhs: @self): @self;
#op(sub)
fn sub(lhs: @self, rhs: @self): @self;
#op(mul)
fn mul(lhs: @self, rhs: @self): @self;
#op(div)
fn div(lhs: @self, rhs: @self): @self;
#op(mod)
fn rem(lhs: @self, rhs: @self): @self;
#op(neg)
fn neg(value: @self): @self;
#op(bit_and)
fn bit_and(lhs: @self, rhs: @self): @self;
#op(bit_or)
fn bit_or(lhs: @self, rhs: @self): @self;
#op(bit_xor)
fn bit_xor(lhs: @self, rhs: @self): @self;
#op(shl)
fn shl(lhs: @self, rhs: @self): @self;
#op(shr)
fn shr(lhs: @self, rhs: @self): @self;
#op(bit_not)
fn bit_not(value: @self): @self;
#op(eq)
fn eq(lhs: @self, rhs: @self): bool;
#op(ord)
fn cmp(lhs: @self, rhs: @self): Ordering;
};
}
#repr("float")
comptime fn Float(bits: usize): type {
ret struct: Add[Rhs = @self, Output = @self]
+ Sub[Rhs = @self, Output = @self]
+ Mul[Rhs = @self, Output = @self]
+ Div[Rhs = @self, Output = @self]
+ Mod[Rhs = @self, Output = @self]
+ Neg[Output = @self]
+ Eq[Rhs = @self]
+ Ord[Rhs = @self] {
#op(add)
fn add(lhs: @self, rhs: @self): @self;
#op(sub)
fn sub(lhs: @self, rhs: @self): @self;
#op(mul)
fn mul(lhs: @self, rhs: @self): @self;
#op(div)
fn div(lhs: @self, rhs: @self): @self;
#op(mod)
fn rem(lhs: @self, rhs: @self): @self;
#op(neg)
fn neg(value: @self): @self;
#op(eq)
fn eq(lhs: @self, rhs: @self): bool;
#op(ord)
fn cmp(lhs: @self, rhs: @self): Ordering;
};
}
#repr("bool")
comptime fn Bool(): type {
ret struct: Eq[Rhs = @self] + Ord[Rhs = @self] {
#op(eq)
fn eq(lhs: @self, rhs: @self): bool;
#op(ord)
fn cmp(lhs: @self, rhs: @self): Ordering;
};
}
comptime fn Range(): type {
ret struct: Iter[Item = usize] {
begin: usize,
end: usize,
fn next(@Self self: *@self): @Step(usize) {
if self.begin >= self.end {
ret .Done;
}
var v = self.begin;
self.begin = self.begin + 1;
ret .Item(v);
}
};
}
#repr("ptr")
comptime fn Ptr(key: usize): type {
ret struct: Add[Rhs = usize, Output = @self]
+ Sub[Rhs = usize, Output = @self]
+ Eq[Rhs = @self]
+ Ord[Rhs = @self] {
#op(add)
fn add(lhs: @self, rhs: usize): @self;
#op(sub)
fn sub(lhs: @self, rhs: usize): @self;
#op(eq)
fn eq(lhs: @self, rhs: @self): bool;
#op(ord)
fn cmp(lhs: @self, rhs: @self): Ordering;
};
}
#repr("fn")
comptime fn Fn(key: usize): type {
ret struct: Eq[Rhs = @self] + Ord[Rhs = @self] {
#op(eq)
fn eq(lhs: @self, rhs: @self): bool;
#op(ord)
fn cmp(lhs: @self, rhs: @self): Ordering;
};
}
+2
View File
@@ -1,3 +1,5 @@
#!builtin
// 元编程模块:HIR 反射接口。必须显式 `type meta = @import("meta")` 后使用。
type Ref = @import("./spl_ir.spl").Ref;
type Node = @import("./spl_ir.spl").Node;
+142
View File
@@ -0,0 +1,142 @@
#!role(ops)
// 语言运算符协议。运算符解析只认本模块的 shape;
// 用户模块里的同名 shape 不参与语言分派。
type Ordering = enum {
Less,
Equal,
Greater,
};
#lang(add)
type Add = shape {
type Rhs;
type Output;
fn add(lhs: @self, rhs: Rhs): Output;
};
#lang(sub)
type Sub = shape {
type Rhs;
type Output;
fn sub(lhs: @self, rhs: Rhs): Output;
};
#lang(mul)
type Mul = shape {
type Rhs;
type Output;
fn mul(lhs: @self, rhs: Rhs): Output;
};
#lang(div)
type Div = shape {
type Rhs;
type Output;
fn div(lhs: @self, rhs: Rhs): Output;
};
#lang(mod)
type Mod = shape {
type Rhs;
type Output;
fn rem(lhs: @self, rhs: Rhs): Output;
};
#lang(neg)
type Neg = shape {
type Output;
fn neg(value: @self): Output;
};
#lang(bit_and)
type BitAnd = shape {
type Rhs;
type Output;
fn bit_and(lhs: @self, rhs: Rhs): Output;
};
#lang(bit_or)
type BitOr = shape {
type Rhs;
type Output;
fn bit_or(lhs: @self, rhs: Rhs): Output;
};
#lang(bit_xor)
type BitXor = shape {
type Rhs;
type Output;
fn bit_xor(lhs: @self, rhs: Rhs): Output;
};
#lang(shl)
type Shl = shape {
type Rhs;
type Output;
fn shl(lhs: @self, rhs: Rhs): Output;
};
#lang(shr)
type Shr = shape {
type Rhs;
type Output;
fn shr(lhs: @self, rhs: Rhs): Output;
};
#lang(bit_not)
type BitNot = shape {
type Output;
fn bit_not(value: @self): Output;
};
#lang(eq)
type Eq = shape {
type Rhs;
fn eq(lhs: @self, rhs: Rhs): bool;
};
#lang(ord)
type Ord = shape {
type Rhs;
fn cmp(lhs: @self, rhs: Rhs): Ordering;
};
#lang(index)
type Index = shape {
type Output;
fn index(@Self self: *@self, idx: usize): *Output;
};
type Len = shape {
fn len(@Self self: @self): usize;
};
comptime fn Step(T: type): type {
ret enum {
Item: T,
Done,
};
}
#lang(call)
type Call = shape {
type Args;
type Output;
fn call(self: @self, args: Args): Output;
};
#lang(iter)
type Iter = shape {
type Item;
fn next(@Self self: *@self): @Step(Item);
};
#lang(into_iter)
type IntoIter = shape {
type Item;
type Iter;
fn iter(@Self self: *@self): Iter;
};
+7 -4
View File
@@ -86,7 +86,7 @@ type Node = struct {
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 },
Iter: struct { label: Ref, inputs: []Ref, captures: []Ref, pattern_type: Ref, body: Ref, else_body: Ref },
Break: struct { label: Ref, value: Ref },
Continue: struct { label: Ref },
Return: Ref,
@@ -110,7 +110,7 @@ type Node = struct {
PatTuple: []Ref,
PatField: struct { name: []u8, sub: Ref },
AggType: struct { kind: Agg, fields: []Ref, decls: []Ref, injected: []Ref },
AggType: struct { kind: Agg, conforms: []Ref, fields: []Ref, decls: []Ref, injected: []Ref },
ArrayType: struct { len: Ref, elem: Ref },
SliceType: Ref,
PtrType: Ref,
@@ -149,6 +149,10 @@ type Node = struct {
Unary: struct { op: Op, val: Ref },
Call: struct { is_comptime: bool, callee: Ref, args: []Ref },
Comptime: Ref,
AssocDecl: struct { name: []u8 },
ShapeApply: struct { base: Ref, binds: []Ref },
ShapeUse: struct { adapter: []u8, shapes: []Ref },
AssocBind: struct { name: []u8, ty: Ref },
}
}
@@ -159,9 +163,8 @@ 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"); },
.LitInt(s) => { @assert(s[0] == '1', "lit text kept"); },
_ => { @assert(false, "expected lit int"); },
}