Files
spl/stage3/builtins/lang.spl
T

222 lines
5.4 KiB
Plaintext

#!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;
};
}