Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+267
View File
@@ -0,0 +1,267 @@
#! @file kinds.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the traits saying what kind of value a type's are: `numeric`, which
#! a type implements to have all of arithmetic, and which every number has, and
#! those the compiler gives: `integer` and `real`, with the functions parsing
#! each from text, `ordinal`, which every integer type and every enum is,
#! `zeroable`, which every type with a zero value is, and those naming the parts
#! of a type, as `signature` does a function's: `pointer`, `reference`, `array`,
#! and `generic`.
module core;
#! A type with arithmetic.
#!
#! It composes `additive` and `multiplicative`, and `divisible` and `modular`
#! beside them, so it asks for `add`, `sub`, `mul`, `div`, `rem`, and `negate`
#! between two values of the type, with their `equals`. `+`, `-`, `*`, `/`, and
#! `%` call them, the compound assignments such as `+=` call them too, and `-x`
#! calls `negate`. Numbers are numeric without declaring anything. It does not
#! ask for an order, so vectors and matrices can be numeric; write
#! `where T: numeric, ordered` for both.
#!
#! A type has each operator by the method that gives it, so one that means only
#! some of them declares only those, and satisfies the smaller traits this one
#! composes.
#!
#! Within a generic bounded by it, a constant such as `x + 1`, and `as` between
#! numbers, work where `T` is a number and are errors in an instance where it is
#! not.
pub trait numeric : additive, multiplicative, divisible, modular {}
#! An integer type, signed or unsigned, of any width.
#!
#! A type parameter bounded by `integer` has what numbers have, as `numeric` and
#! `ordered` give, along with bitwise operators, shifts, and indexing, and it is
#! `ordinal`. The compiler decides what is an integer, so no other type can
#! implement it.
pub trait integer {}
#! Reads the text as a signed 64-bit integer, in decimal or with a `0x`, `0o`,
#! or `0b` prefix, where `_` may separate digits.
#! @param this The text to read, which may start with `-` or `+`.
#! @returns The integer, or why the text is not one.
pub def integer::parse<T>(text: strview):
result<T, failure> where T : integer
{
var at := 0u;
var neg := false;
if text.length > 0 {
neg = text[0] == '-';
at = (text[0] == '+' || neg) as uint;
}
var base := 10u;
if text.length > at + 1 && text[at] == '0' {
val marker := text[at + 1];
if marker == 'x' || marker == 'X' { base = 16; } else
if marker == 'o' || marker == 'O' { base = 8; } else
if marker == 'b' || marker == 'B' { base = 2; }
if base != 10 { at += 2; }
}
return failure { $"`{text}` has no digits" }
if at >= text.length;
const signed := 0x8000000000000000u;
const unsigned := 0x7FFFFFFFFFFFFFFFu;
val limit := neg ? signed : unsigned;
var magnitude := 0u;
for byte in text[at:] {
continue if byte == '_';
val digit := byte.digit_value;
return failure { $"`{text}` is not a base {base} integer" }
if digit >= base;
return failure { $"`{text}` does not fit in 64 bits" }
if magnitude > (limit - digit) / base;
magnitude = magnitude * base + digit;
}
return neg
? (0 - magnitude) as T
: magnitude as T;
}
#! A type of real numbers: `f32` or `f64`, or an alias of either.
#!
#! A type parameter bounded by `real` has what numbers have, as `numeric` and
#! `ordered` give, along with a zero value, and `as` to and from the other
#! numbers. It is what the functions of `std::math` ask of what they take, as
#! `math::sqrt<T>(x: T): T where T: real`. The compiler decides what is real, so
#! no other type can implement it.
pub trait real {}
#! Reads the text as a real number such as `42`, `-0.5`, `.25`, or `6.02e23`.
#! @param this The text to read, which may start with `-` or `+`, and may end
#! with an exponent: `e` or `E`, an optional sign, and digits.
#! @returns The 64-bit float nearest the number, halfway between two going to
#! the one whose last bit is zero, or why the text is not one. A number
#! too large for 64 bits is a failure, and one too small is zero.
pub def real::parse<T>(text: strview):
result<T, failure> where T : real
{
var at := 0u;
var neg := false;
if text.length > 0 {
neg = text[0] == '-';
at = (text[0] == '+' || neg) as uint;
}
# The digits, with at most one `.` among them.
val start := at;
var digits := 0u;
var dotted := false;
while at < text.length {
val byte := text[at];
if byte == '.' && !dotted {
dotted = true;
} else {
break if !byte.is_digit;
digits++;
}
at++;
}
return failure { $"`{text}` is not a number" }
if digits == 0;
# The exponent, which is held at a hundred thousand, far past where every number
# is too large or too small, so a long one cannot overflow it.
var exponent := 0;
val written := text.substr(start, at - start);
if at < text.length && (text[at] == 'e' || text[at] == 'E') {
at++;
var down := false;
if at < text.length {
down = text[at] == '-';
at += (text[at] == '+' || down) as uint;
}
val first := at;
while at < text.length && text[at].is_digit {
if exponent < 100000 {
exponent = exponent * 10 + (text[at].digit_value as i64);
}
at++;
}
return failure { $"`{text}` has a malformed exponent" }
if at == first;
if down {
exponent = -exponent;
}
}
return failure { $"`{text}` is not a number" }
if at != text.length;
return failure { $"`{text}` is too large for 64 bits" }
if decimal_digits::nearest(written, exponent)
is not some(result);
return neg
? -result as T
: result as T;
}
#! A value that is an integer underneath: every integer type, and every enum.
#!
#! A type parameter bounded by `ordinal` compares with `==`, orders with `<`,
#! and converts to and from the integer types with `as`, which is what flags and
#! tables indexed by an enum need. An integer becomes an enum only when it is
#! some member's value, and stops the program otherwise.
#!
#! The compiler decides what is ordinal, so no other type can implement it.
pub trait ordinal {}
#! A pointer, whatever it points at, however many pointers deep.
#!
#! A pointer is an address. It is compared, ordered, and hashed as one, starts
#! as `null`, and moves by whole elements, so `p + 1` is the element after `p`,
#! and two pointers subtract to the number of elements between them. Nothing of
#! what it points at is reached through it: `*p`, `p[i]`, and `through(p)` reach
#! that.
#!
#! A type parameter bounded by `pointer` has what pointers have, as `equatable`,
#! `ordered`, and `zeroable` give, along with moving by an integer and
#! subtracting. The compiler decides what is a pointer, so no type can implement
#! it.
#!
#! A pointer names what it points at, `T::target`, and whether what it points at
#! is written through it, `T::mutable`, so `*mut u8` gives `u8` and `true`. A
#! pointer to a pointer gives the pointer it points at, one step at a time.
pub trait pointer {
#! The type it points at.
type target;
#! Whether it writes what it points at, as `*mut T` does.
const mutable: bool;
}
#! A reference, `&T` or `&mut T`.
#!
#! A reference names what it refers to, `T::target`, and whether it writes it,
#! `T::mutable`, so `&mut point` gives `point` and `true`. A type parameter is
#! a reference where it was given one, as `f(&x)` gives `T` the reference. A
#! reference satisfies other traits through what it refers to, but this one only
#! by being one. The compiler decides what is a reference, so no type can
#! implement it.
pub trait reference {
#! The type it refers to.
type target;
#! Whether it writes what it refers to, as `&mut T` does.
const mutable: bool;
}
#! A fixed array, `T[N]`.
#!
#! An array names the type of its elements, `T::element`, and how many it holds,
#! `T::length`, so `u8[16]` gives `u8` and `16`. An array of arrays gives the
#! array it holds, one step at a time. The compiler decides what is an array, so
#! no type can implement it.
pub trait array {
#! The type of its elements.
type element;
#! How many elements it holds.
const length: u64;
}
#! A generic type given its type arguments, as `vector<u8>` or `map<K, V>`.
#!
#! It names the pack of what it was given, `T::args`, in the order they are
#! written, with what a pack parameter was given in its place, so `pair<i64,
#! bool>` gives `(i64, bool)`, and `T is vector`, without arguments, says
#! whether it is an instance of `vector`. An alias is the type it names, so
#! `strview` gives `(u8)` as `span<u8>` does. A type without type parameters, a
#! pointer, a reference, an array, and a function are not generic. The compiler
#! decides what is generic, so no type can implement it.
pub trait generic {
#! The type arguments, as a pack, which only the compiler gives.
type args...;
}
#! A type with a zero value, which a variable left without a value starts at: a
#! number, `bool`, a pointer, an enum with a member of value 0, and an array,
#! struct, or union of what is, where a struct's fields with defaults start at
#! their defaults instead. A reference or a function is not, nor is what holds
#! one.
#!
#! Generic code bounded by it may write `var made: T;`, and code that only may
#! asks `if const T is zeroable` first.
#!
#! The compiler decides what is zeroable, so no type can implement it.
pub trait zeroable {}