This commit is contained in:
47
orchid-std/src/std/boolean.rs
Normal file
47
orchid-std/src/std/boolean.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use orchid_api_derive::Coding;
|
||||
use orchid_base::error::OrcRes;
|
||||
use orchid_base::format::FmtUnit;
|
||||
use orchid_base::sym;
|
||||
use orchid_extension::atom::{Atomic, TAtom};
|
||||
use orchid_extension::atom_thin::{ThinAtom, ThinVariant};
|
||||
use orchid_extension::conv::{ToExpr, TryFromExpr};
|
||||
use orchid_extension::expr::Expr;
|
||||
use orchid_extension::gen_expr::{GExpr, sym_ref};
|
||||
use orchid_extension::tree::{GenMember, cnst, comments, fun, prefix};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Coding)]
|
||||
pub struct Bool(pub bool);
|
||||
impl Atomic for Bool {
|
||||
type Variant = ThinVariant;
|
||||
type Data = Self;
|
||||
}
|
||||
impl ThinAtom for Bool {
|
||||
async fn print(&self) -> FmtUnit { self.0.to_string().into() }
|
||||
}
|
||||
impl TryFromExpr for Bool {
|
||||
async fn try_from_expr(expr: Expr) -> OrcRes<Self> {
|
||||
match TAtom::<Bool>::downcast(expr.handle()).await {
|
||||
Err(e) => Err(e.mk_err().await),
|
||||
Ok(atom) => Ok(atom.value),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ToExpr for Bool {
|
||||
async fn to_gen(self) -> GExpr {
|
||||
sym_ref(if self.0 { sym!(std::true) } else { sym!(std::false) })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_bool_lib() -> Vec<GenMember> {
|
||||
prefix("std", [
|
||||
comments(
|
||||
[
|
||||
"Returns the second argument if the bool is true, the third argument otherwise",
|
||||
"|type: Bool -> T -> T -> T|",
|
||||
],
|
||||
fun(true, "ifthenelse", async |Bool(b): Bool, t: Expr, f: Expr| if b { t } else { f }),
|
||||
),
|
||||
cnst(true, "true", Bool(true)),
|
||||
cnst(true, "false", Bool(false)),
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user