This repository has been archived on 2026-08-20. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
el-retired/engrams/el-stdlib/src/math.rs
T

89 lines
4.7 KiB
Rust

//! Math operations: abs, max, min, floor, ceil, pow, sqrt, clamp.
use el_types::{Type, TypeEnv};
use super::fn_type;
pub fn register(env: &mut TypeEnv) {
env.register_fn("math_abs", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_max", fn_type(vec![Type::Float, Type::Float], Type::Float));
env.register_fn("math_min", fn_type(vec![Type::Float, Type::Float], Type::Float));
env.register_fn("math_floor", fn_type(vec![Type::Float], Type::Int));
env.register_fn("math_ceil", fn_type(vec![Type::Float], Type::Int));
env.register_fn("math_round", fn_type(vec![Type::Float], Type::Int));
env.register_fn("math_pow", fn_type(vec![Type::Float, Type::Float], Type::Float));
env.register_fn("math_sqrt", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_clamp", fn_type(vec![Type::Float, Type::Float, Type::Float], Type::Float));
env.register_fn("math_abs_int", fn_type(vec![Type::Int], Type::Int));
env.register_fn("math_max_int", fn_type(vec![Type::Int, Type::Int], Type::Int));
env.register_fn("math_min_int", fn_type(vec![Type::Int, Type::Int], Type::Int));
// Trig
env.register_fn("math_sin", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_cos", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_tan", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_asin", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_acos", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_atan2", fn_type(vec![Type::Float, Type::Float], Type::Float));
env.register_fn("math_exp", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_ln", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_log2", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_log10", fn_type(vec![Type::Float], Type::Float));
env.register_fn("math_mod", fn_type(vec![Type::Float, Type::Float], Type::Float));
env.register_fn("math_pi", fn_type(vec![], Type::Float));
env.register_fn("math_e", fn_type(vec![], Type::Float));
// Conversion
env.register_fn("int_to_float", fn_type(vec![Type::Int], Type::Float));
env.register_fn("float_to_int", fn_type(vec![Type::Float], Type::Int));
env.register_fn("is_nil", fn_type(vec![Type::Unknown], Type::Bool));
env.register_fn("unwrap_or", fn_type(vec![Type::Unknown, Type::Unknown], Type::Unknown));
// Decimal
env.register_fn("decimal_add", fn_type(vec![Type::Float, Type::Float], Type::Float));
env.register_fn("decimal_sub", fn_type(vec![Type::Float, Type::Float], Type::Float));
env.register_fn("decimal_mul", fn_type(vec![Type::Float, Type::Float], Type::Float));
env.register_fn("decimal_div", fn_type(vec![Type::Float, Type::Float], Type::Float));
env.register_fn("decimal_round", fn_type(vec![Type::Float, Type::Int], Type::Float));
// Time
env.register_fn("time_now_utc", fn_type(vec![], Type::Int));
env.register_fn("time_to_parts", fn_type(vec![Type::Int], Type::Unknown));
env.register_fn("time_from_parts", fn_type(vec![Type::Unknown], Type::Int));
env.register_fn("time_format", fn_type(vec![Type::Int, Type::String], Type::String));
env.register_fn("time_parse", fn_type(vec![Type::String], Type::Int));
env.register_fn("time_add", fn_type(vec![Type::Int, Type::Int, Type::String], Type::Int));
env.register_fn("time_diff", fn_type(vec![Type::Int, Type::Int, Type::String], Type::Int));
env.register_fn("time_start_of", fn_type(vec![Type::Int, Type::String], Type::Int));
env.register_fn("time_tz_offset", fn_type(vec![Type::String], Type::Int));
env.register_fn("time_to_tz", fn_type(vec![Type::Int, Type::String], Type::Unknown));
// Observer
env.register_fn("observe", fn_type(vec![Type::String, Type::String], Type::Int));
env.register_fn("unobserve", fn_type(vec![Type::Int], Type::Unknown));
}
#[cfg(test)]
mod tests {
use super::*;
fn env() -> TypeEnv {
let mut e = TypeEnv::with_builtins();
register(&mut e);
e
}
#[test]
fn test_math_abs_registered() {
assert!(env().lookup_fn("math_abs").is_some());
}
#[test]
fn test_math_sqrt_returns_float() {
let e = env();
let ty = e.lookup_fn("math_sqrt").unwrap();
assert!(matches!(ty, Type::Fn { return_type, .. } if matches!(return_type.as_ref(), Type::Float)));
}
#[test]
fn test_math_floor_returns_int() {
let e = env();
let ty = e.lookup_fn("math_floor").unwrap();
assert!(matches!(ty, Type::Fn { return_type, .. } if matches!(return_type.as_ref(), Type::Int)));
}
}