ai: 生成代码准备重构前的一次提交
This commit is contained in:
1 parent
936a8d0e88
commit
032102f1d7
375 files changed
+4619
-7609
No files matched your search
@@ -0,0 +1,23 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §6:@[*W] 没有默认 adapter;调用点必须显式写 @A[Shape](arg)。
|
||||
|
||||
type .{ .identity } = @import("../support/adapter.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 3; }
|
||||
};
|
||||
|
||||
fn via(w: @[*W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(@identity[*W](&a)) == 3, "explicit adapter");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §6:@static[*W] 是带默认 adapter 的指针 shape 参数。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 1; }
|
||||
};
|
||||
|
||||
fn via(w: @[*W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(&a) == 1, "static pointer shape");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §6:@static[W] 只接受值,不接受指针。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 5; }
|
||||
};
|
||||
|
||||
fn via(w: @static[W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(a) == 5, "value shape");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Expect compile_error E0317
|
||||
|
||||
// §16.6:显式 conformance 但缺少 shape 方法。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn other(@Self self: *@self): usize { ret 1; }
|
||||
};
|
||||
|
||||
fn via(w: @static[*W]): void {}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
via(&a);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Expect compile_error E0305
|
||||
|
||||
// §16.6:未声明 conformance 的类型不能传给 shape 参数。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct {
|
||||
fn id(@Self self: *@self): usize { ret 1; }
|
||||
};
|
||||
|
||||
fn via(w: @static[*W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(&a) == 1);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:shape 可以声明多个方法,conforming 类型全部实现。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
fn name(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 1; }
|
||||
fn name(@Self self: *@self): usize { ret 2; }
|
||||
};
|
||||
|
||||
fn via(w: @static[*W]): usize {
|
||||
ret w.id() + w.name();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(&a) == 3, "both methods");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:shape 方法可以写穿具体类型。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn set(@Self self: *@self, v: i32): void;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
value: i32,
|
||||
|
||||
fn set(@Self self: *@self, v: i32): void {
|
||||
self.value = v;
|
||||
}
|
||||
};
|
||||
|
||||
fn via(w: @static[*W], v: i32): void {
|
||||
w.set(v);
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
via(&a, 7);
|
||||
@assert(a.value == 7, "write through shape");
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:shape 方法可以返回值。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn get(@Self self: *@self): i32;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
value: i32,
|
||||
|
||||
fn get(@Self self: *@self): i32 {
|
||||
ret self.value;
|
||||
}
|
||||
};
|
||||
|
||||
fn via(w: @static[*W]): i32 {
|
||||
ret w.get();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{ .value = 11 };
|
||||
@assert(via(&a) == 11, "return value");
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:同一个 shape 可以被多个类型显式实现。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 1; }
|
||||
};
|
||||
|
||||
type B = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 2; }
|
||||
};
|
||||
|
||||
fn via(w: @static[*W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
var b = B{};
|
||||
@assert(via(&a) == 1, "A");
|
||||
@assert(via(&b) == 2, "B");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §6:同一个类型既能按值传给 @static[W],也能按指针传给 @static[*W]。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 9; }
|
||||
};
|
||||
|
||||
fn by_value(w: @static[W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn by_pointer(w: @static[*W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(by_value(a) == 9, "value");
|
||||
@assert(by_pointer(&a) == 9, "pointer");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:一个类型可以同时显式实现多个 shape。
|
||||
|
||||
type A = shape {
|
||||
fn a(): i32;
|
||||
};
|
||||
|
||||
type B = shape {
|
||||
fn b(): i32;
|
||||
};
|
||||
|
||||
type C = struct: A + B {
|
||||
fn a(): i32 { ret 1; }
|
||||
fn b(): i32 { ret 2; }
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var c = C{};
|
||||
@assert(c.a() + c.b() == 3, "two conformances");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §6:@[Shape] 没有默认 adapter,调用点必须显式 @adapter[Shape](arg)。
|
||||
|
||||
type .{ .identity } = @import("../support/adapter.spl");
|
||||
|
||||
type Cal = shape {
|
||||
fn call(a: i32): i32;
|
||||
};
|
||||
|
||||
type Add = struct {
|
||||
fn call(a: i32): i32 { ret a + 4; }
|
||||
};
|
||||
|
||||
fn use_cal(c: @[Cal]): i32 {
|
||||
ret c.call(1);
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
@assert(use_cal(@identity[Cal](Add{})) == 5, "explicit adapter");
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:#op 只标注运算符绑定,普通函数调用仍然可用。
|
||||
|
||||
#op(add)
|
||||
fn add(a: i32, b: i32): i32 {
|
||||
ret a + b;
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
@assert(add(1, 2) == 3, "plain call");
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:shape 方法可以通过字段指针访问具体类型。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type Inner = struct {
|
||||
v: i32,
|
||||
};
|
||||
|
||||
type W = shape {
|
||||
fn get(@Self self: *@self): i32;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
inner: *Inner,
|
||||
|
||||
fn get(@Self self: *@self): i32 {
|
||||
ret self.inner.v;
|
||||
}
|
||||
};
|
||||
|
||||
fn via(w: @static[*W]): i32 {
|
||||
ret w.get();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var i = Inner{ .v = 6 };
|
||||
var a = A{ .inner = &i };
|
||||
@assert(via(&a) == 6, "field pointer");
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Expect compile_error E0103
|
||||
|
||||
// §6:@[Shape] / @[*Shape] 只允许出现在参数位置;
|
||||
// 变量位置没有单态化上下文,不能作为真实类型使用。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 1; }
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
var w: @[*W] = &a;
|
||||
@assert(w.id() == 1);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Expect compile_error E0103
|
||||
|
||||
// §6:@[*Shape] 没有默认 adapter,调用点必须显式 @A[Shape](arg)。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 1; }
|
||||
};
|
||||
|
||||
fn via(w: @[*W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(&a) == 1);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §6:有默认 adapter 时,调用点也可以显式写出来。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 4; }
|
||||
};
|
||||
|
||||
fn via(w: @static[W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(@static[W](a)) == 4, "explicit static");
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:两个 shape 可以有同名方法,由 conformance 决定解析。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type A1 = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A2 = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type X = struct: A1 + A2 {
|
||||
fn id(@Self self: *@self): usize { ret 5; }
|
||||
};
|
||||
|
||||
fn via1(w: @static[*A1]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn via2(w: @static[*A2]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var x = X{};
|
||||
@assert(via1(&x) == 5, "A1");
|
||||
@assert(via2(&x) == 5, "A2");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §6:@static[*W] 有默认 adapter,调用点也可以显式写 @static[*W](&a)。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 2; }
|
||||
};
|
||||
|
||||
fn via(w: @static[*W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(@static[*W](&a)) == 2, "explicit static pointer");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §6:@dyn[W] 是库 adapter 的默认行为。
|
||||
|
||||
type .{ .dyn } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 1; }
|
||||
};
|
||||
|
||||
fn via(w: @dyn[*W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(&a) == 1);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:shape 参数可以和普通参数混合。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 2; }
|
||||
};
|
||||
|
||||
fn twice(w: @static[*W]): usize {
|
||||
ret w.id() + w.id();
|
||||
}
|
||||
|
||||
fn with_extra(w: @static[*W], n: usize): usize {
|
||||
ret w.id() + n;
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(twice(&a) == 4, "twice");
|
||||
@assert(with_extra(&a, 10) == 12, "extra arg");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §5:同一 (类型, shape) 的实例重复调用结果一致。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
value: usize,
|
||||
|
||||
fn id(@Self self: *@self): usize {
|
||||
ret self.value;
|
||||
}
|
||||
};
|
||||
|
||||
fn via(w: @static[*W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{ .value = 3 };
|
||||
@assert(via(&a) == 3, "first");
|
||||
@assert(via(&a) == 3, "second");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §5:同一个函数可以有两个 shape 参数。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
value: usize,
|
||||
|
||||
fn id(@Self self: *@self): usize {
|
||||
ret self.value;
|
||||
}
|
||||
};
|
||||
|
||||
fn combine(a: @static[*W], b: @static[*W]): usize {
|
||||
ret a.id() + b.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var x = A{ .value = 1 };
|
||||
var y = A{ .value = 2 };
|
||||
@assert(combine(&x, &y) == 3, "two shapes");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §6:@dyn[W] 值形式。
|
||||
|
||||
type .{ .dyn } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): usize { ret 1; }
|
||||
};
|
||||
|
||||
fn via(w: @dyn[W]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
@assert(via(a) == 1);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:lang.spl 的 Add shape 支持 +。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
type .{ .Add } = @import("../support/lang.spl");
|
||||
|
||||
type Num = struct: Add {
|
||||
v: i32,
|
||||
|
||||
fn add(@Self self: *@self, other: *@self): @self {
|
||||
ret @self { .v = self.v + other.v };
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var a = Num{ .v = 1 };
|
||||
var b = Num{ .v = 2 };
|
||||
var c = a + b;
|
||||
@assert(c.v == 3, "add");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:shape 方法可以返回指针。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn get(@Self self: *@self): *i32;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
value: i32,
|
||||
|
||||
fn get(@Self self: *@self): *i32 {
|
||||
ret &self.value;
|
||||
}
|
||||
};
|
||||
|
||||
fn via(w: @static[*W]): *i32 {
|
||||
ret w.get();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{ .value = 7 };
|
||||
var p = via(&a);
|
||||
@assert(p.* == 7, "return pointer");
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Expect compile_error E0317
|
||||
|
||||
// §16.6:shape 方法签名不匹配。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W {
|
||||
fn id(@Self self: *@self): i32 {
|
||||
ret 1;
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Expect compile_error E0103
|
||||
|
||||
// §16.6:关联类型互相约束。
|
||||
|
||||
type A = shape {
|
||||
type Item: B;
|
||||
};
|
||||
|
||||
type B = shape {
|
||||
type Item: A;
|
||||
};
|
||||
|
||||
fn main(): void {}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:lang.spl 的 Eq shape 支持 ==。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
type .{ .Eq } = @import("../support/lang.spl");
|
||||
|
||||
type Num = struct: Eq {
|
||||
v: i32,
|
||||
|
||||
fn eq(@Self self: *@self, other: *@self): bool {
|
||||
ret self.v == other.v;
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var a = Num{ .v = 1 };
|
||||
var b = Num{ .v = 1 };
|
||||
var c = Num{ .v = 2 };
|
||||
@assert(a == b, "equal");
|
||||
@assert(!(a == c), "not equal");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:lang.spl 的 Sub shape 支持 -。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
type .{ .Sub } = @import("../support/lang.spl");
|
||||
|
||||
type Num = struct: Sub {
|
||||
v: i32,
|
||||
|
||||
fn sub(@Self self: *@self, other: *@self): @self {
|
||||
ret @self { .v = self.v - other.v };
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var a = Num{ .v = 3 };
|
||||
var b = Num{ .v = 1 };
|
||||
var c = a - b;
|
||||
@assert(c.v == 2, "sub");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:lang.spl 的 Mul shape 支持 *。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
type .{ .Mul } = @import("../support/lang.spl");
|
||||
|
||||
type Num = struct: Mul {
|
||||
v: i32,
|
||||
|
||||
fn mul(@Self self: *@self, other: *@self): @self {
|
||||
ret @self { .v = self.v * other.v };
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var a = Num{ .v = 2 };
|
||||
var b = Num{ .v = 3 };
|
||||
var c = a * b;
|
||||
@assert(c.v == 6, "mul");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:lang.spl 的 Div shape 支持 /。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
type .{ .Div } = @import("../support/lang.spl");
|
||||
|
||||
type Num = struct: Div {
|
||||
v: i32,
|
||||
|
||||
fn div(@Self self: *@self, other: *@self): @self {
|
||||
ret @self { .v = self.v / other.v };
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var a = Num{ .v = 6 };
|
||||
var b = Num{ .v = 2 };
|
||||
var c = a / b;
|
||||
@assert(c.v == 3, "div");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:lang.spl 的 Ord shape 支持 < 和 >。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
type .{ .Ord } = @import("../support/lang.spl");
|
||||
|
||||
type Num = struct: Ord {
|
||||
v: i32,
|
||||
|
||||
fn cmp(@Self self: *@self, other: *@self): i32 {
|
||||
ret self.v - other.v;
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var a = Num{ .v = 1 };
|
||||
var b = Num{ .v = 2 };
|
||||
@assert(a < b, "less");
|
||||
@assert(b > a, "greater");
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §5:实例 key 包含具体类型,A 和 B 各自的 W1 实例互不影响。
|
||||
|
||||
type .{ .static } = @import("../support/shape_identity.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type W1 = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type W2 = shape {
|
||||
fn id(@Self self: *@self): usize;
|
||||
};
|
||||
|
||||
type A = struct: W1 + W2 {
|
||||
fn id(@Self self: *@self): usize { ret 1; }
|
||||
};
|
||||
|
||||
type B = struct: W1 + W2 {
|
||||
fn id(@Self self: *@self): usize { ret 2; }
|
||||
};
|
||||
|
||||
fn via1(w: @static[*W1]): usize {
|
||||
ret w.id();
|
||||
}
|
||||
|
||||
fn main(): void {
|
||||
var a = A{};
|
||||
var b = B{};
|
||||
@assert(via1(&a) == 1, "A instance");
|
||||
@assert(via1(&b) == 2, "B instance");
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §9 / §16.3:两个 Format(S) 实例的 Spec 互相隔离。
|
||||
|
||||
type .{
|
||||
.Sink,
|
||||
.Formatter,
|
||||
.Format,
|
||||
} = @import("../support/format.spl");
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
|
||||
type Buf = struct: Sink {
|
||||
buf: []u8,
|
||||
len: usize,
|
||||
|
||||
fn write(@Self self: *@self, bytes: []const u8): bool {
|
||||
var i: usize = 0;
|
||||
while i < bytes.len() {
|
||||
self.buf[self.len] = bytes[i];
|
||||
self.len = self.len + 1;
|
||||
i = i + 1;
|
||||
}
|
||||
ret true;
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var b1: [8]u8 = .[];
|
||||
var b2: [8]u8 = .[];
|
||||
var s1 = Buf{ .buf = b1[0..8], .len = 0 };
|
||||
var s2 = Buf{ .buf = b2[0..8], .len = 0 };
|
||||
var f1 = @Format(Buf).with(&s1);
|
||||
var f2 = @Format(Buf).with(&s2);
|
||||
f1.set_spec(.{ .width = 3, .fill = ' ', .align = .Left });
|
||||
f2.set_spec(.{ .width = 7, .fill = '*', .align = .Right });
|
||||
@assert(f1.spec().width == 3, "first spec");
|
||||
@assert(f2.spec().width == 7, "second spec");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:Index shape 支持 b[i]。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
type .{ .Index } = @import("../support/lang.spl");
|
||||
|
||||
type Buf = struct: Index {
|
||||
data: [4]u8,
|
||||
type Item = u8,
|
||||
|
||||
fn index(@Self self: *@self, i: usize): *u8 {
|
||||
ret &self.data[i];
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var b = Buf{ .data = .[1, 2, 3, 4] };
|
||||
var p = b[0];
|
||||
@assert(p.* == 1, "index");
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Expect retcode = 0
|
||||
|
||||
// §16.6:Call shape 支持 a()。
|
||||
|
||||
type .{ .Self } = @import("../support/self_hook.spl");
|
||||
type .{ .Call } = @import("../support/lang.spl");
|
||||
|
||||
type Adder = struct: Call {
|
||||
type Output = i32,
|
||||
base: i32,
|
||||
|
||||
fn call(@Self self: *@self): i32 {
|
||||
ret self.base;
|
||||
}
|
||||
};
|
||||
|
||||
fn main(): void {
|
||||
var a = Adder{ .base = 5 };
|
||||
@assert(a() == 5, "call");
|
||||
}
|
||||
Reference in new issue
Block a user