spl 编写编译器基础框架 ai修复出现的stage2和stage3问题并提供新语法
This commit is contained in:
1 parent
126b979f72
commit
50ee126d44
87 files changed
+3168
-394
No files matched your search
@@ -1,6 +1,8 @@
|
||||
#!builtin
|
||||
#!role(builtins)
|
||||
|
||||
type meta = @import("meta");
|
||||
|
||||
// splc3 的预置头:把编译器底层能力以 `#builtin` 声明暴露给用户/comptime 代码。
|
||||
// `#builtin(<id>)` 的 id 必须命中 stage3/intrinsic.spl 的 intrinsic_of_id。
|
||||
|
||||
@@ -27,26 +29,25 @@ type Node = @import("./spl_ir.spl").Node;
|
||||
#builtin(construct) #extern(builtin) comptime fn construct(n: Node): Ref;
|
||||
|
||||
// receiver hook:@Self 只挂在方法的第一个参数上。
|
||||
// 参数类型是 *T 时取地址,是 T 时按值传递;已显式传 &x 的调用不再二次取地址。
|
||||
// 参数类型是 *T 时取地址,是 T 时按值传递;implicit 本身已是指向接收者的指针时直接复用。
|
||||
#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; },
|
||||
_ => {},
|
||||
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
|
||||
if implicit == 0 {
|
||||
if value != 0 { ret value; }
|
||||
ret 0;
|
||||
}
|
||||
|
||||
var cn = @inspect(callee);
|
||||
var cn = @meta.inspect(callee);
|
||||
match cn.kind {
|
||||
.FnDecl(f) => {
|
||||
var pn = @inspect(f.params[0]);
|
||||
var pn = @meta.inspect(f.params[0]);
|
||||
match pn.kind {
|
||||
.ParamDecl(p) => {
|
||||
var tn = @inspect(p.ty);
|
||||
var tn = @meta.inspect(p.ty);
|
||||
match tn.kind {
|
||||
.PtrType(_) => { ret @construct(.{ .kind = .Addr(value) }); },
|
||||
.PtrType(_) => {
|
||||
if @ptr_ref(implicit) { ret implicit; }
|
||||
ret @meta.construct(.{ .kind = .Addr(implicit) });
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
@@ -55,5 +56,39 @@ macro fn Self(callee: Ref, value: Ref): Ref {
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret value;
|
||||
ret implicit;
|
||||
}
|
||||
|
||||
// implicit 已是 `*T` 形式(局部变量或参数)时不再二次取地址。
|
||||
comptime fn ptr_ref(r: Ref): bool {
|
||||
var n = @meta.inspect(r);
|
||||
var decl: Ref = 0;
|
||||
match n.kind {
|
||||
.Addr(_) => { ret true; },
|
||||
.Local(p) => { decl = p; },
|
||||
_ => {},
|
||||
}
|
||||
if decl == 0 { ret false; }
|
||||
var dn = @meta.inspect(decl);
|
||||
var ty: Ref = 0;
|
||||
match dn.kind {
|
||||
.ParamDecl(p) => { ty = p.ty; },
|
||||
.VarDecl(v) => { ty = v.ty; },
|
||||
_ => {},
|
||||
}
|
||||
if ty == 0 { ret false; }
|
||||
var tn = @meta.inspect(ty);
|
||||
match tn.kind {
|
||||
.PtrType(_) => { ret true; },
|
||||
.ShapeUse(x) => {
|
||||
var sn = @meta.inspect(x.shapes[0]);
|
||||
match sn.kind { .PtrType(_) => { ret true; }, _ => {} }
|
||||
},
|
||||
.ShapeNoDefault(xs) => {
|
||||
var sn = @meta.inspect(xs[0]);
|
||||
match sn.kind { .PtrType(_) => { ret true; }, _ => {} }
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
+32
-32
@@ -17,7 +17,7 @@ comptime fn Slice(T: type): type {
|
||||
_ptr: *T,
|
||||
_len: usize,
|
||||
|
||||
fn len(self: @self): usize {
|
||||
fn len(@Self self: @self): usize {
|
||||
ret self._len;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ comptime fn Slice(T: type): type {
|
||||
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;
|
||||
fn len(@Self self: @self): usize;
|
||||
|
||||
#builtin(array_index)
|
||||
fn index(@Self self: *@self, i: usize): *T;
|
||||
@@ -83,46 +83,46 @@ comptime fn Int(bits: usize, signed: bool): type {
|
||||
+ Eq[Rhs = @self]
|
||||
+ Ord[Rhs = @self] {
|
||||
#op(add)
|
||||
fn add(lhs: @self, rhs: @self): @self;
|
||||
fn add(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(sub)
|
||||
fn sub(lhs: @self, rhs: @self): @self;
|
||||
fn sub(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(mul)
|
||||
fn mul(lhs: @self, rhs: @self): @self;
|
||||
fn mul(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(div)
|
||||
fn div(lhs: @self, rhs: @self): @self;
|
||||
fn div(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(mod)
|
||||
fn rem(lhs: @self, rhs: @self): @self;
|
||||
fn rem(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(neg)
|
||||
fn neg(value: @self): @self;
|
||||
fn neg(@Self value: @self): @self;
|
||||
|
||||
#op(bit_and)
|
||||
fn bit_and(lhs: @self, rhs: @self): @self;
|
||||
fn bit_and(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(bit_or)
|
||||
fn bit_or(lhs: @self, rhs: @self): @self;
|
||||
fn bit_or(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(bit_xor)
|
||||
fn bit_xor(lhs: @self, rhs: @self): @self;
|
||||
fn bit_xor(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(shl)
|
||||
fn shl(lhs: @self, rhs: @self): @self;
|
||||
fn shl(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(shr)
|
||||
fn shr(lhs: @self, rhs: @self): @self;
|
||||
fn shr(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(bit_not)
|
||||
fn bit_not(value: @self): @self;
|
||||
fn bit_not(@Self value: @self): @self;
|
||||
|
||||
#op(eq)
|
||||
fn eq(lhs: @self, rhs: @self): bool;
|
||||
fn eq(@Self lhs: @self, rhs: @self): bool;
|
||||
|
||||
#op(ord)
|
||||
fn cmp(lhs: @self, rhs: @self): Ordering;
|
||||
fn cmp(@Self lhs: @self, rhs: @self): Ordering;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -137,28 +137,28 @@ comptime fn Float(bits: usize): type {
|
||||
+ Eq[Rhs = @self]
|
||||
+ Ord[Rhs = @self] {
|
||||
#op(add)
|
||||
fn add(lhs: @self, rhs: @self): @self;
|
||||
fn add(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(sub)
|
||||
fn sub(lhs: @self, rhs: @self): @self;
|
||||
fn sub(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(mul)
|
||||
fn mul(lhs: @self, rhs: @self): @self;
|
||||
fn mul(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(div)
|
||||
fn div(lhs: @self, rhs: @self): @self;
|
||||
fn div(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(mod)
|
||||
fn rem(lhs: @self, rhs: @self): @self;
|
||||
fn rem(@Self lhs: @self, rhs: @self): @self;
|
||||
|
||||
#op(neg)
|
||||
fn neg(value: @self): @self;
|
||||
fn neg(@Self value: @self): @self;
|
||||
|
||||
#op(eq)
|
||||
fn eq(lhs: @self, rhs: @self): bool;
|
||||
fn eq(@Self lhs: @self, rhs: @self): bool;
|
||||
|
||||
#op(ord)
|
||||
fn cmp(lhs: @self, rhs: @self): Ordering;
|
||||
fn cmp(@Self lhs: @self, rhs: @self): Ordering;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -166,10 +166,10 @@ comptime fn Float(bits: usize): type {
|
||||
comptime fn Bool(): type {
|
||||
ret struct: Eq[Rhs = @self] + Ord[Rhs = @self] {
|
||||
#op(eq)
|
||||
fn eq(lhs: @self, rhs: @self): bool;
|
||||
fn eq(@Self lhs: @self, rhs: @self): bool;
|
||||
|
||||
#op(ord)
|
||||
fn cmp(lhs: @self, rhs: @self): Ordering;
|
||||
fn cmp(@Self lhs: @self, rhs: @self): Ordering;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -196,16 +196,16 @@ comptime fn Ptr(key: usize): type {
|
||||
+ Eq[Rhs = @self]
|
||||
+ Ord[Rhs = @self] {
|
||||
#op(add)
|
||||
fn add(lhs: @self, rhs: usize): @self;
|
||||
fn add(@Self lhs: @self, rhs: usize): @self;
|
||||
|
||||
#op(sub)
|
||||
fn sub(lhs: @self, rhs: usize): @self;
|
||||
fn sub(@Self lhs: @self, rhs: usize): @self;
|
||||
|
||||
#op(eq)
|
||||
fn eq(lhs: @self, rhs: @self): bool;
|
||||
fn eq(@Self lhs: @self, rhs: @self): bool;
|
||||
|
||||
#op(ord)
|
||||
fn cmp(lhs: @self, rhs: @self): Ordering;
|
||||
fn cmp(@Self lhs: @self, rhs: @self): Ordering;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -213,9 +213,9 @@ comptime fn Ptr(key: usize): type {
|
||||
comptime fn Fn(key: usize): type {
|
||||
ret struct: Eq[Rhs = @self] + Ord[Rhs = @self] {
|
||||
#op(eq)
|
||||
fn eq(lhs: @self, rhs: @self): bool;
|
||||
fn eq(@Self lhs: @self, rhs: @self): bool;
|
||||
|
||||
#op(ord)
|
||||
fn cmp(lhs: @self, rhs: @self): Ordering;
|
||||
fn cmp(@Self lhs: @self, rhs: @self): Ordering;
|
||||
};
|
||||
}
|
||||
+14
-14
@@ -14,94 +14,94 @@ type Ordering = enum {
|
||||
type Add = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn add(lhs: @self, rhs: Rhs): Output;
|
||||
fn add(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(sub)
|
||||
type Sub = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn sub(lhs: @self, rhs: Rhs): Output;
|
||||
fn sub(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(mul)
|
||||
type Mul = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn mul(lhs: @self, rhs: Rhs): Output;
|
||||
fn mul(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(div)
|
||||
type Div = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn div(lhs: @self, rhs: Rhs): Output;
|
||||
fn div(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(mod)
|
||||
type Mod = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn rem(lhs: @self, rhs: Rhs): Output;
|
||||
fn rem(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(neg)
|
||||
type Neg = shape {
|
||||
type Output;
|
||||
fn neg(value: @self): Output;
|
||||
fn neg(@Self value: @self): Output;
|
||||
};
|
||||
|
||||
#lang(bit_and)
|
||||
type BitAnd = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn bit_and(lhs: @self, rhs: Rhs): Output;
|
||||
fn bit_and(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(bit_or)
|
||||
type BitOr = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn bit_or(lhs: @self, rhs: Rhs): Output;
|
||||
fn bit_or(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(bit_xor)
|
||||
type BitXor = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn bit_xor(lhs: @self, rhs: Rhs): Output;
|
||||
fn bit_xor(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(shl)
|
||||
type Shl = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn shl(lhs: @self, rhs: Rhs): Output;
|
||||
fn shl(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(shr)
|
||||
type Shr = shape {
|
||||
type Rhs;
|
||||
type Output;
|
||||
fn shr(lhs: @self, rhs: Rhs): Output;
|
||||
fn shr(@Self lhs: @self, rhs: Rhs): Output;
|
||||
};
|
||||
|
||||
#lang(bit_not)
|
||||
type BitNot = shape {
|
||||
type Output;
|
||||
fn bit_not(value: @self): Output;
|
||||
fn bit_not(@Self value: @self): Output;
|
||||
};
|
||||
|
||||
#lang(eq)
|
||||
type Eq = shape {
|
||||
type Rhs;
|
||||
fn eq(lhs: @self, rhs: Rhs): bool;
|
||||
fn eq(@Self lhs: @self, rhs: Rhs): bool;
|
||||
};
|
||||
|
||||
#lang(ord)
|
||||
type Ord = shape {
|
||||
type Rhs;
|
||||
fn cmp(lhs: @self, rhs: Rhs): Ordering;
|
||||
fn cmp(@Self lhs: @self, rhs: Rhs): Ordering;
|
||||
};
|
||||
|
||||
#lang(index)
|
||||
|
||||
@@ -148,10 +148,12 @@ type Node = struct {
|
||||
Binary: struct { op: Op, lval: Ref, rval: Ref },
|
||||
Unary: struct { op: Op, val: Ref },
|
||||
Call: struct { is_comptime: bool, callee: Ref, args: []Ref },
|
||||
ShapeCall: struct { adapter: []u8, shapes: []Ref, args: []Ref },
|
||||
Comptime: Ref,
|
||||
AssocDecl: struct { name: []u8 },
|
||||
ShapeApply: struct { base: Ref, binds: []Ref },
|
||||
ShapeUse: struct { adapter: []u8, shapes: []Ref },
|
||||
ShapeNoDefault: []Ref,
|
||||
AssocBind: struct { name: []u8, ty: Ref },
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user