35 lines
1.1 KiB
Rust
35 lines
1.1 KiB
Rust
use orchid_base::number::Numeric;
|
|
use orchid_extension::tree::{GenMember, fun, prefix};
|
|
use ordered_float::NotNan;
|
|
|
|
use super::num_atom::{Float, HomoArray, Int, Num};
|
|
|
|
pub fn gen_num_lib() -> Vec<GenMember> {
|
|
prefix("std::number", [
|
|
fun(true, "add", async |a: Num, b: Num| {
|
|
Num(match HomoArray::new([a.0, b.0]) {
|
|
HomoArray::Int([a, b]) => Numeric::Int(a + b),
|
|
HomoArray::Float([a, b]) => Numeric::Float(a + b),
|
|
})
|
|
}),
|
|
fun(true, "neg", async |a: Num| {
|
|
Num(match a.0 {
|
|
Numeric::Int(i) => Numeric::Int(-i),
|
|
Numeric::Float(f) => Numeric::Float(-f),
|
|
})
|
|
}),
|
|
fun(true, "mul", async |a: Num, b: Num| {
|
|
Num(match HomoArray::new([a.0, b.0]) {
|
|
HomoArray::Int([a, b]) => Numeric::Int(a * b),
|
|
HomoArray::Float([a, b]) => Numeric::Float(a * b),
|
|
})
|
|
}),
|
|
fun(true, "idiv", async |a: Int, b: Int| Int(a.0 / b.0)),
|
|
fun(true, "imod", async |a: Int, b: Int| Int(a.0 % b.0)),
|
|
fun(true, "fdiv", async |a: Float, b: Float| Float(a.0 / b.0)),
|
|
fun(true, "fmod", async |a: Float, b: Float| {
|
|
Float(a.0 - NotNan::new((a.0 / b.0).trunc()).unwrap() * b.0)
|
|
}),
|
|
])
|
|
}
|