diff options
author | JJ | 2024-01-02 05:37:22 +0000 |
---|---|---|
committer | JJ | 2024-01-02 05:37:22 +0000 |
commit | 3fb0a86c7cf24b5f3e0aa28ea97691152f42bb4a (patch) | |
tree | d19c63c46ecd3414c35ca6fc29a5fefca8a6e0fa /std/magic | |
parent | bf580bb5580d48e6c2a59ffe094f0edc391d7690 (diff) |
std: much fleshing out. completely partially implement the prelude.
Diffstat (limited to 'std/magic')
-rw-r--r-- | std/magic/arrays.pk | 20 | ||||
-rw-r--r-- | std/magic/booleans.pk | 42 | ||||
-rw-r--r-- | std/magic/numbers.pk | 77 | ||||
-rw-r--r-- | std/magic/pointers.pk | 7 |
4 files changed, 146 insertions, 0 deletions
diff --git a/std/magic/arrays.pk b/std/magic/arrays.pk new file mode 100644 index 0000000..a0207c1 --- /dev/null +++ b/std/magic/arrays.pk @@ -0,0 +1,20 @@ +## std.arrays: The array[T, S] primitive and associated functions. +## A stub module for documentation. + +## Primitive fixed-size arrays. Their size is statically known at compile-time. +pub type array[T, S: static[uint]] + +## Array access. Returns None if i is out of range. +pub func get[T, S: static[uint]](self: array[T, S], i: uint): T? +## Array mutation. +# todo: how do we detect range errors? +pub func set[T, S: static[uint]](self: mut array[T, S], i: uint, val: T): T? +## A helper function to get the length of an array. +## Known to the compiler, and computed at compile-time. +pub func len[T, S: static[uint]](self: array[T, S], i: uint): T? + +type ArrayIter[T, S: static[uint]] = struct + ... +pub func iter[T, S: static[uint]](self: array[T, S]): ArrayIter[T, S] + +# todo: Eq, PartialEq, Ord, PartialOrd diff --git a/std/magic/booleans.pk b/std/magic/booleans.pk new file mode 100644 index 0000000..3c7e28e --- /dev/null +++ b/std/magic/booleans.pk @@ -0,0 +1,42 @@ +## std.booleans: Booleans and related types. Mostly compiler magic. +## A stub module for documentation. + +pub type unit = union[sole] +pub type bool = union[false, true] + +# note: puck could resolve kleene.false vs bool.false... +# this is probably not worth it compared to improved type inference +# pub type Kleene = union[False, Maybe, True] + +## Boolean equality +pub func ==(a: bool, b: bool): bool = + match a + of (true, true), (false, false): true + of (false, true), (true, false): false + +pub func !=(a: bool, b: bool): bool = + not a == b + +## Boolean negation +pub func not(a: bool): bool = + match a + of true: false + of false: true + +## Boolean conjunction +pub func and(a: bool, b: bool): bool = + match (a, b) + of (true, true): true + of _: false + +## Boolean disjunction +pub func or(a: bool, b: bool): bool = + match (a, b) + of (false, false): false + of _: true + +## Boolean exclusive disjunction +pub func xor(a: bool, b: bool): bool = + match (a, b) + of (true, true), (false, false): false + of (true, false), (false, true): true diff --git a/std/magic/numbers.pk b/std/magic/numbers.pk new file mode 100644 index 0000000..007f4b7 --- /dev/null +++ b/std/magic/numbers.pk @@ -0,0 +1,77 @@ +## std.numbers: Operations on numbers and such. +## A stub module for documentation. +# reference: https://en.wikipedia.org/wiki/IEEE_754 + +## The default integer type. Size depends on architecture. +pub type int +## A signed 8-bit integer. +pub type i8 +## A signed 16-bit integer. +pub type i16 +## A signed 32-bit integer. +pub type i32 +## A signed 64-bit integer. +pub type i64 +## A signed 128-bit integer. +pub type i128 + +## The default unsigned integer type. Size depends on architecture. +pub type uint +## An unsigned 8-bit integer. +pub type u8 +## An unsigned 16-bit integer. +pub type u16 +## An unsigned 32-bit integer. +pub type u32 +## An unsigned 64-bit integer. +pub type u64 +## An unsigned 128-bit integer. +pub type u128 + +## A floating-point type. Takes up 64 bits by default. +pub type float = f64 +## A 32-bit floating point type. Single precision. +pub type f32 +## A 64-bit floating point type. Double precision. +pub type f64 +## A 128-bit floating point type. Quadruple precision. +pub type f128 + +## A decimal type, according to IEEE 754. Takes up 64 bits by default. +pub type decimal = dec64 +## A 64-bit decimal type. +pub type dec64 +## A 128-bit decimal type. +pub type dec128 + +## 8-bit bytes. +pub type byte = u8 +## 4-byte chars. These represent distinct Unicode characters. +pub type char = distinct u32 +## An alias to `char`, to match with `str`. +pub type chr = char + +## The IEEE floating point value of *Not a Number*. +pub const NaN: f64 = 0x7FF7FFFFFFFFFFFF +## The IEEE floating point value of positive infinity. +pub const Inf: f64 = 0x7FF0000000000000 +## The IEEE floating point value of negative infinity. +pub const NegInf: f64 = 0xFFF0000000000000 + +# todo: type conversions + +pub func +[T](a: T, b: T): T +pub func -[T](a: T, b: T): T +pub func *[T](a: T, b: T): T +pub func /[T](a: T, b: T): float +pub func ^[T](a: T, b: T): T # todo: should be exp? + +pub func div[T](a: T, b: T): T +pub func mod[T](a: T, b: T): T # fixme +pub func rem[T](a: T, b: T): T + +pub func shl[T](a: T): T +pub func shr[T](a: T): T + +pub func abs[T](a: T): T +pub func neg[T](a: T): bool diff --git a/std/magic/pointers.pk b/std/magic/pointers.pk new file mode 100644 index 0000000..e2a95cf --- /dev/null +++ b/std/magic/pointers.pk @@ -0,0 +1,7 @@ +## std.pointers: Pointer arithmetic. Unsafe. + +pub type ptr[T] +pub type ref[T] +pub type unique[T] + +# idk what goes here |