pull out libm. pow and sqrt to be implemented in userlib maybe?

Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
Ava Apples Affine 2024-07-29 11:14:34 -07:00
parent a93fb512aa
commit 48178b5a48
3 changed files with 2 additions and 126 deletions

View file

@ -19,16 +19,16 @@ use crate::segment::{Ctr, Seg};
use crate::sym::{SymTable, ValueType, Symbol, Args};
use crate::error::{Traceback, start_trace};
use libm::pow;
use alloc::rc::Rc;
use alloc::boxed::Box;
use alloc::string::{String, ToString};
#[inline]
fn isnumeric(arg: &Ctr) -> bool {
matches!(arg, Ctr::Integer(_) | Ctr::Float(_))
}
const ADD_DOCSTRING: &str =
"traverses over N args, which must all evaluate to an Integer or Float.
Adds each arg up to a final result. WARNING: does not acocunt for under/overflows.
@ -192,52 +192,6 @@ fn floatcast_callback(ast: &Seg, _: &mut SymTable) -> Result<Ctr, Traceback> {
}
}
const EXP_DOCSTRING: &str = "Takes two args, both expected to be numeric.
Returns the first arg to the power of the second arg.
Does not handle overflow or underflow.";
fn exp_callback(ast: &Seg, _: &mut SymTable) -> Result<Ctr, Traceback> {
let first = *ast.car.clone();
if !isnumeric(&first) {
return Err(start_trace(
("exp", format!("{} is not a number!", first))
.into()))
}
let second: Ctr;
if let Ctr::Seg(ref s) = *ast.cdr {
second = *s.car.clone();
} else {
return Err(start_trace(
("exp", "expected at least two inputs")
.into()))
}
if !isnumeric(&second) {
return Err(start_trace(
("exp", format!("{} is not a number!", second))
.into()))
}
match first {
Ctr::Float(lf) => match second {
Ctr::Float(rf) => Ok(Ctr::Float(pow(lf, rf))),
Ctr::Integer(ri) => Ok(Ctr::Float(pow(lf as f64, ri as f64))),
_ => Err(start_trace(
("exp", "not implemented for these input types")
.into())),
},
Ctr::Integer(li) => match second {
Ctr::Float(rf) => Ok(Ctr::Float(pow(li as f64, rf))),
Ctr::Integer(ri) => Ok(Ctr::Float(pow(li as f64, ri as f64))),
_ => Err(start_trace(
("exp", "not implemented for these input types")
.into())),
},
_ => Err(start_trace(
("exp", "not implemented for these input types")
.into())),
}
}
const MOD_DOCSTRING: &str = "Takes two args, both expected to be numeric.
Returns a list of two values: the modulus and the remainder.
Example: (mod 5 3) -> (1 2)
@ -602,19 +556,6 @@ pub fn add_math_lib(syms: &mut SymTable) {
},
);
syms.insert(
"exp".to_string(),
Symbol {
name: String::from("exp"),
args: Args::Lazy(2),
conditional_branches: false,
docs: EXP_DOCSTRING.to_string(),
optimizable: true,
value: ValueType::Internal(Rc::new(exp_callback)),
..Default::default()
},
);
syms.insert(
"mod".to_string(),
Symbol {