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

+286
View File
@@ -0,0 +1,286 @@
#! @file maybe.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the properties and methods of `maybe`, and `something`, which wraps
#! a value in one.
#!
#! `maybe`, which holds a value or `nothing`, is among the types the syntax
#! makes, which the compiler declares itself, as it does `i64`: `none` comes
#! first, so a `maybe` left without a value holds nothing. A value converts to
#! a `maybe` of its type wherever one is wanted, and it is read by `is`,
#! `match`, or `else`, as `val x := find(key) else { return; };`. `nothing` is
#! apart from `void`, the value of what gives nothing, so `maybe<void>` tells a
#! value missing from the value of `void`.
#!
#! The methods reading the value, as `unwrap`, move it out of what can be
#! written, as `move` does, so a `maybe` of what closes is left holding
#! nothing, and copy it out of what is only read, which a value that closes
#! refuses. Those making another value of it, as `map`, move it in, as an
#! iterator's adapters do: `as_ref` lends them what one that is only read
#! holds.
module core;
#! Whether it holds a value.
#! @param this The `maybe`.
#! @returns Whether it does.
pub val maybe<T>::is_some: bool { get => this is some; }
#! Whether it holds nothing.
#! @param this The `maybe`.
#! @returns Whether it does.
pub val maybe<T>::is_none: bool { get => this is none; }
#! Whether it holds a value a test passes.
#! @tparam F The test.
#! @param this The `maybe`.
#! @param test The test, given a reference to the value.
#! @returns Whether it holds one that passes.
pub def maybe<T>::is_some_and<F>(&this, test: F): bool
where F: invokable<def(&T): bool>
{
return this is some(&value) && test(value);
}
#! Gives the value it holds, moved out as `move` moves it, so one that closes
#! leaves it holding nothing. It stops the program where it holds nothing.
#! @param this The `maybe`.
#! @returns The value.
pub def maybe<T>::unwrap(&mut this): T {
var held := move(this);
return move(value) if held is some(&mut value);
fault("A `maybe` holding nothing was unwrapped.");
}
#! Gives a copy of the value it holds, which a value that closes refuses. It
#! stops the program where it holds nothing.
#! @param this The `maybe`.
#! @returns The value.
pub def maybe<T>::unwrap(&this): T {
if const T is closeable {
copied::<T>();
} else {
return value if this is some(value);
}
fault("A `maybe` holding nothing was unwrapped.");
}
#! Gives the value it holds, moved out as `unwrap` moves it. It stops the
#! program where it holds nothing, saying why.
#! @param this The `maybe`.
#! @param why What went wrong, where it holds nothing.
#! @returns The value.
pub def maybe<T>::expect(&mut this, why: strview): T {
var held := move(this);
return move(value) if held is some(&mut value);
fault(why);
}
#! Gives a copy of the value it holds, which a value that closes refuses. It
#! stops the program where it holds nothing, saying why.
#! @param this The `maybe`.
#! @param why What went wrong, where it holds nothing.
#! @returns The value.
pub def maybe<T>::expect(&this, why: strview): T {
if const T is closeable {
copied::<T>();
} else {
return value if this is some(value);
}
fault(why);
}
#! Gives the value it holds, moved out as `unwrap` moves it, or a fallback
#! where it holds nothing.
#! @param this The `maybe`.
#! @param fallback The value given where it holds nothing.
#! @returns The value, or the fallback.
pub def maybe<T>::unwrap_or(&mut this, fallback: T): T {
var held := move(this);
return move(value) if held is some(&mut value);
return move(&mut fallback);
}
#! Gives a copy of the value it holds, which a value that closes refuses, or a
#! fallback where it holds nothing.
#! @param this The `maybe`.
#! @param fallback The value given where it holds nothing.
#! @returns The value, or the fallback.
pub def maybe<T>::unwrap_or(&this, fallback: T): T {
if const T is closeable {
copied::<T>();
} else {
return value if this is some(value);
}
return move(&mut fallback);
}
#! Gives the value it holds, moved out as `unwrap` moves it, or what a
#! function makes where it holds nothing, which is called only then.
#! @tparam F The function.
#! @param this The `maybe`.
#! @param make The function, making the value where it holds nothing.
#! @returns The value, or what the function made.
pub def maybe<T>::unwrap_or_else<F>(&mut this, make: F): T where F: invokable<def(): T> {
var held := move(this);
return move(value) if held is some(&mut value);
return make();
}
#! Takes what it holds, leaving it holding nothing, whatever the value is.
#! @param this The `maybe`.
#! @returns What it held.
pub def maybe<T>::take(&mut this): maybe<T> {
var made := move(this);
this = nothing {};
return made;
}
#! Puts a value in it, in place of what it held.
#! @param this The `maybe`.
#! @param value The value it holds from now on.
#! @returns What it held before.
pub def maybe<T>::replace(&mut this, value: T): maybe<T> {
var made := move(this);
this = maybe::<T> { some = move(&mut value) };
return made;
}
#! Lends what it holds: a reference to the value, or nothing.
#! @param this The `maybe`.
#! @returns Maybe a reference to the value; otherwise, `nothing`.
pub def maybe<T>::as_ref(&this): maybe<&T> {
return maybe::<&T> { some = value } if this is some(&value);
return nothing {};
}
#! Lends what it holds to be written: a reference to the value, or nothing.
#! @param this The `maybe`.
#! @returns Maybe a reference to the value; otherwise, `nothing`.
pub def maybe<T>::as_mut(&mut this): maybe<&mut T> {
return maybe::<&mut T> { some = value } if this is some(&mut value);
return nothing {};
}
#! Makes a value of the value it holds, which a function is given, moved in.
#! @tparam U What the function makes.
#! @tparam F The function.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param f The function, called on the value where it holds one.
#! @returns Maybe what the function made; otherwise, `nothing`.
pub def maybe<T>::map<U, F>(&mut this, f: F): maybe<U> where F: invokable<def(T): U> {
var held := move(this);
return maybe::<U> { some = f(move(value)) } if held is some(&mut value);
return nothing {};
}
#! Gives what a function makes of the value it holds, which may be nothing:
#! the next step of work that may find nothing.
#! @tparam U What the function may make.
#! @tparam F The function.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param f The function, called on the value where it holds one.
#! @returns What the function gave; otherwise, `nothing`.
pub def maybe<T>::and_then<U, F>(&mut this, f: F): maybe<U> where F: invokable<def(T): maybe<U>> {
var held := move(this);
return f(move(value)) if held is some(&mut value);
return nothing {};
}
#! Keeps the value it holds where a test passes it.
#! @tparam F The test.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param test The test, given a reference to the value.
#! @returns Maybe the value; otherwise, `nothing` where it fails the test.
pub def maybe<T>::filter<F>(&mut this, test: F): maybe<T> where F: invokable<def(&T): bool> {
var held := move(this);
return held if held is some(&value) && test(value);
return nothing {};
}
#! Gives what it holds where it holds a value, and another `maybe` otherwise.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param other What is given where it holds nothing.
#! @returns It, or the other.
pub def maybe<T>::or(&mut this, other: maybe<T>): maybe<T> {
var held := move(this);
return held if held is some;
return move(&mut other);
}
#! Gives what it holds where it holds a value, and what a function makes
#! otherwise, which is called only then.
#! @tparam F The function.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param make The function, making what is given where it holds nothing.
#! @returns It, or what the function made.
pub def maybe<T>::or_else<F>(&mut this, make: F): maybe<T> where F: invokable<def(): maybe<T>> {
var held := move(this);
return held if held is some;
return make();
}
#! Makes a `result` of it: the value it holds, or an error where it holds
#! nothing.
#! @tparam E The error's type.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param error The error, where it holds nothing.
#! @returns The result.
pub def maybe<T>::ok_or<E>(&mut this, error: E): result<T, E> {
var held := move(this);
return result::<T, E> { value = move(value) } if held is some(&mut value);
return result::<T, E> { error = move(&mut error) };
}
#! Whether it holds what another does: both nothing, or equal values, as `==`
#! compares them.
#! @param this The `maybe`.
#! @param other The other.
#! @returns Whether they are equal.
#! @remarks A `maybe` of a type that is not `equatable` is an error where it is
#! compared.
pub def maybe<T>::equals(&this, other: &maybe<T>): bool {
if this is some(&mine) {
return other is some(&theirs) && equals::<T>(mine, theirs);
}
return other is none;
}
#! Hashes what it holds, so a `maybe` holding nothing hashes apart from one
#! holding a value.
#! @param this The `maybe`.
#! @returns The hash.
#! @remarks A `maybe` of a type that is not `hashable` is an error where it is
#! hashed.
pub def maybe<T>::hash(&this): u64 {
return mix(1, hash::<T>(value)) if this is some(&value);
return 0;
}
#! Builds a copy of a `maybe`, holding a copy of its value, as `copy` makes it.
#! @param other The `maybe` to copy.
#! @returns The copy.
pub def maybe<T>::copy_of(other: &maybe<T>): maybe<T> {
return maybe::<T> { some = copy::<T>(value) } if other is some(&value);
return nothing {};
}
#! Refuses a copy of a value that closes, which a `maybe` or a `result` only
#! read would give of what it holds.
#! @tparam T The type of what it holds.
def copied<T>(): void {
if const T is closeable {
compile_error("A `maybe` or `result` that is only read gives a copy of what it holds, which a value that closes refuses: call it on one that can be written, as a `var`, which moves the value out.");
}
}
#! Wraps a value in a `maybe`, holding it.
#! @tparam T The type of the value.
#! @param data The value to hold.
#! @returns A `maybe` holding the value, as `some`.
pub def something<T>(data: T): maybe<T> {
return maybe::<T> { some = move(&mut data) };
}
+357
View File
@@ -0,0 +1,357 @@
#! @file result.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the properties and methods of `result`, and `ok` and `err`, which
#! make one holding a value or an error.
#!
#! `result`, which holds either the value an operation gave or the error that
#! stopped it, is among the types the syntax makes, which the compiler declares
#! itself. It must be handled where it is made, since letting it go would lose
#! the error it may hold: by `match`, by `is`, or by `else`, as
#! `val f := open(path) else (e) { return e; };`.
#!
#! The methods reading what it holds, as `unwrap`, move it out of what can be
#! written, as `move` does, and copy it out of what is only read, which a value
#! that closes refuses. Those making another `result` of it, as `map`, move it
#! in, as an iterator's adapters do: `as_ref` lends them what one that is only
#! read holds. Each makes what it gives by naming the field, so a `result` whose
#! value and error are of one type is told apart.
module core;
#! Makes a result holding a value.
#! @param value The value to hold.
#! @returns The result, holding the value.
pub def result<T, E>::ok(value: T): result<T, E> {
return result::<T, E> { value = move(&mut value) };
}
#! Makes a result holding an error.
#! @param error The error to hold.
#! @returns The result, holding the error.
pub def result<T, E>::err(error: E): result<T, E> {
return result::<T, E> { error = move(&mut error) };
}
#! Whether it holds the value, where the work succeeded.
#! @param this The result.
#! @returns Whether it does.
pub val result<T, E>::is_ok: bool { get => this is value; }
#! Whether it holds the error, where the work failed.
#! @param this The result.
#! @returns Whether it does.
pub val result<T, E>::is_err: bool { get => this is error; }
#! Whether it holds a value a test passes.
#! @tparam F The test.
#! @param this The result.
#! @param test The test, given a reference to the value.
#! @returns Whether it holds one that passes.
pub def result<T, E>::is_ok_and<F>(&this, test: F): bool
where F: invokable<def(&T): bool>
{
return this is value(&held) && test(held);
}
#! Whether it holds an error a test passes.
#! @tparam F The test.
#! @param this The result.
#! @param test The test, given a reference to the error.
#! @returns Whether it holds one that passes.
pub def result<T, E>::is_err_and<F>(&this, test: F): bool
where F: invokable<def(&E): bool>
{
return this is error(&held) && test(held);
}
#! Gives the value it holds, moved out as `move` moves it. It stops the program
#! where it holds an error.
#! @param this The result.
#! @returns The value.
pub def result<T, E>::unwrap(&mut this): T {
var held := move(this);
return move(got) if held is value(&mut got);
fault("A `result` holding an error was unwrapped.");
}
#! Gives a copy of the value it holds, which a value that closes refuses. It
#! stops the program where it holds an error.
#! @param this The result.
#! @returns The value.
pub def result<T, E>::unwrap(&this): T {
if const T is closeable {
copied::<T>();
} else {
return got if
this is value(got);
}
fault("A `result` holding an error was unwrapped.");
}
#! Gives the value it holds, moved out as `unwrap` moves it. It stops the
#! program where it holds an error, saying why.
#! @param this The result.
#! @param why What went wrong, where it holds an error.
#! @returns The value.
pub def result<T, E>::expect(&mut this, why: strview): T {
var held := move(this);
return move(got) if
held is value(&mut got);
fault(why);
}
#! Gives a copy of the value it holds, which a value that closes refuses. It
#! stops the program where it holds an error, saying why.
#! @param this The result.
#! @param why What went wrong, where it holds an error.
#! @returns The value.
pub def result<T, E>::expect(&this, why: strview): T {
if const T is closeable {
copied::<T>();
} else {
return got if
this is value(got);
}
fault(why);
}
#! Gives the error it holds, moved out as `move` moves it. It stops the program
#! where it holds a value.
#! @param this The result.
#! @returns The error.
pub def result<T, E>::unwrap_err(&mut this): E {
var held := move(this);
return move(got) if
held is error(&mut got);
fault("A `result` holding a value was unwrapped for its error.");
}
#! Gives a copy of the error it holds, which an error that closes refuses. It
#! stops the program where it holds a value.
#! @param this The result.
#! @returns The error.
pub def result<T, E>::unwrap_err(&this): E {
if const E is closeable {
copied::<E>();
} else {
return got if
this is error(got);
}
fault("A `result` holding a value was unwrapped for its error.");
}
#! Gives the error it holds, moved out as `unwrap_err` moves it. It stops the
#! program where it holds a value, saying why.
#! @param this The result.
#! @param why What went wrong, where it holds a value.
#! @returns The error.
pub def result<T, E>::expect_err(&mut this, why: strview): E {
var held := move(this);
return move(got) if
held is error(&mut got);
fault(why);
}
#! Gives a copy of the error it holds, which an error that closes refuses. It
#! stops the program where it holds a value, saying why.
#! @param this The result.
#! @param why What went wrong, where it holds a value.
#! @returns The error.
pub def result<T, E>::expect_err(&this, why: strview): E {
if const E is closeable {
copied::<E>();
} else {
return got if
this is error(got);
}
fault(why);
}
#! Gives the value it holds, moved out as `unwrap` moves it, or a fallback
#! where it holds an error.
#! @param this The result.
#! @param fallback The value given where it holds an error.
#! @returns The value, or the fallback.
pub def result<T, E>::unwrap_or(&mut this, fallback: T): T {
var held := move(this);
return move(got) if
held is value(&mut got);
return move(&mut fallback);
}
#! Gives a copy of the value it holds, which a value that closes refuses, or a
#! fallback where it holds an error.
#! @param this The result.
#! @param fallback The value given where it holds an error.
#! @returns The value, or the fallback.
pub def result<T, E>::unwrap_or(&this, fallback: T): T {
if const T is closeable {
copied::<T>();
} else {
return got if
this is value(got);
}
return move(&mut fallback);
}
#! Gives the value it holds, moved out as `unwrap` moves it, or what a function
#! makes of the error, which it is called on only then.
#! @tparam F The function.
#! @param this The result.
#! @param make The function, given the error, moved out.
#! @returns The value, or what the function made.
pub def result<T, E>::unwrap_or_else<F>(&mut this, make: F): T where F: invokable<def(E): T> {
var held := move(this);
return match held {
value(&mut got) => move(got),
error(&mut bad) => make(move(bad))
};
}
#! Lends what it holds: a reference to the value or to the error.
#! @param this The result.
#! @returns The result of the references.
pub def result<T, E>::as_ref(&this): result<&T, &E> {
return match this {
value(&got) => result::<&T, &E> { value = got },
error(&bad) => result::<&T, &E> { error = bad }
};
}
#! Lends what it holds to be written: a reference to the value or to the
#! error.
#! @param this The result.
#! @returns The result of the references.
pub def result<T, E>::as_mut(&mut this): result<&mut T, &mut E> {
return match this {
value(&mut got) => result::<&mut T, &mut E> { value = got },
error(&mut bad) => result::<&mut T, &mut E> { error = bad }
};
}
#! Makes a value of the value it holds, which a function is given, moved in,
#! keeping the error it holds otherwise.
#! @tparam U What the function makes.
#! @tparam F The function.
#! @param this The result, moved in as `unwrap` moves it.
#! @param f The function, called on the value where it holds one.
#! @returns The result of what the function made, or of the error.
pub def result<T, E>::map<U, F>(&mut this, f: F): result<U, E>
where F: invokable<def(T): U>
{
var held := move(this);
return match held {
value(&mut got) => result::<U, E> { value = f(move(got)) },
error(&mut bad) => result::<U, E> { error = move(bad) }
};
}
#! Makes another error of the error it holds, which a function is given, moved
#! in, keeping the value it holds otherwise.
#! @tparam D What the function makes.
#! @tparam F The function.
#! @param this The result, moved in as `unwrap` moves it.
#! @param f The function, called on the error where it holds one.
#! @returns The result of the value, or of what the function made.
pub def result<T, E>::map_err<D, F>(&mut this, f: F): result<T, D>
where F: invokable<def(E): D>
{
var held := move(this);
return match held {
value(&mut got) => result::<T, D> { value = move(got) },
error(&mut bad) => result::<T, D> { error = f(move(bad)) }
};
}
#! Gives what a function makes of the value it holds, which may fail too: the
#! next step of work that may fail, keeping the error it holds otherwise.
#! @tparam U What the function may make.
#! @tparam F The function.
#! @param this The result, moved in as `unwrap` moves it.
#! @param f The function, called on the value where it holds one.
#! @returns What the function gave, or the result of the error.
pub def result<T, E>::and_then<U, F>(&mut this, f: F): result<U, E>
where F: invokable<def(T): result<U, E>>
{
var held := move(this);
return match held {
value(&mut got) => f(move(got)),
error(&mut bad) => result::<U, E> { error = move(bad) }
};
}
#! Gives what a function makes of the error it holds, which may recover from
#! it, keeping the value it holds otherwise.
#! @tparam D The error of what the function makes.
#! @tparam F The function.
#! @param this The result, moved in as `unwrap` moves it.
#! @param f The function, called on the error where it holds one.
#! @returns The result of the value, or what the function gave.
pub def result<T, E>::or_else<D, F>(&mut this, f: F): result<T, D>
where F: invokable<def(E): result<T, D>>
{
var held := move(this);
return match held {
value(&mut got) => result::<T, D> { value = move(got) },
error(&mut bad) => f(move(bad))
};
}
#! Gives the value it holds, as a `maybe`, losing the error.
#! @param this The result, moved in as `unwrap` moves it.
#! @returns Maybe the value; otherwise, `nothing` where it holds an error.
pub def result<T, E>::to_maybe(&mut this): maybe<T> {
var held := move(this);
return maybe::<T> { some = move(got) } if held is value(&mut got);
return nothing {};
}
#! Gives the error it holds, as a `maybe`, losing the value.
#! @param this The result, moved in as `unwrap` moves it.
#! @returns Maybe the error; otherwise, `nothing` where it holds a value.
pub def result<T, E>::err_to_maybe(&mut this): maybe<E> {
var held := move(this);
return maybe::<E> { some = move(got) } if held is error(&mut got);
return nothing {};
}
#! Whether it holds what another does: equal values, or equal errors, as `==`
#! compares them.
#! @param this The result.
#! @param other The other.
#! @returns Whether they are equal.
#! @remarks A `result` whose value or error is of a type that is not
#! `equatable` is an error where it is compared.
pub def result<T, E>::equals(&this, other: &result<T, E>): bool {
return match this {
value(&mine) => other is value(&theirs) && equals::<T>(mine, theirs),
error(&mine) => other is error(&theirs) && equals::<E>(mine, theirs)
};
}
#! Hashes what it holds, so a value hashes apart from an error.
#! @param this The result.
#! @returns The hash.
#! @remarks A `result` whose value or error is of a type that is not `hashable`
#! is an error where it is hashed.
pub def result<T, E>::hash(&this): u64 {
return match this {
value(&got) => mix(1, hash::<T>(got)),
error(&bad) => mix(2, hash::<E>(bad))
};
}
#! Builds a copy of a result, holding a copy of its value or its error, as
#! `copy` makes it.
#! @param other The result to copy.
#! @returns The copy.
pub def result<T, E>::copy_of(other: &result<T, E>): result<T, E> {
return match other {
value(&got) => result::<T, E> { value = copy::<T>(got) },
error(&bad) => result::<T, E> { error = copy::<E>(bad) }
};
}