Added units to prelude functions.

This commit is contained in:
PaddiM8 2020-06-18 17:41:57 +02:00
parent 2413ad080f
commit 49e4d286b3
2 changed files with 66 additions and 53 deletions

View File

@ -241,8 +241,15 @@ fn eval_fn_call_expr(
_ => None, _ => None,
}; };
if let Some(result) = prelude_func { if let Some((result, func_unit)) = prelude_func {
return Ok((result, unit.into())); return Ok((
result,
if unit.len() > 0 {
unit.into()
} else {
func_unit.into()
},
));
} }
// Special functions // Special functions

View File

@ -16,54 +16,54 @@ pub const CONSTANTS: phf::Map<&'static str, &'static str> = phf::phf_map! {
}; };
use funcs::*; use funcs::*;
pub const UNARY_FUNCS: phf::Map<&'static str, UnaryFuncInfo> = phf::phf_map! { pub const UNARY_FUNCS: phf::Map<&'static str, (UnaryFuncInfo, &'static str)> = phf::phf_map! {
"cos" => UnaryFuncInfo(cos, Trig), "cos" => (UnaryFuncInfo(cos, Trig), ""),
"cosec" => UnaryFuncInfo(cosec, Trig), "cosec" => (UnaryFuncInfo(cosec, Trig), ""),
"cosech" => UnaryFuncInfo(cosech, Trig), "cosech" => (UnaryFuncInfo(cosech, Trig), ""),
"cosh" => UnaryFuncInfo(cosh, Trig), "cosh" => (UnaryFuncInfo(cosh, Trig), ""),
"cot" => UnaryFuncInfo(cot, Trig), "cot" => (UnaryFuncInfo(cot, Trig), ""),
"coth" => UnaryFuncInfo(coth, Trig), "coth" => (UnaryFuncInfo(coth, Trig), ""),
"sec" => UnaryFuncInfo(sec, Trig), "sec" => (UnaryFuncInfo(sec, Trig), ""),
"sech" => UnaryFuncInfo(sech, Trig), "sech" => (UnaryFuncInfo(sech, Trig), ""),
"sin" => UnaryFuncInfo(sin, Trig), "sin" => (UnaryFuncInfo(sin, Trig), ""),
"sinh" => UnaryFuncInfo(sinh, Trig), "sinh" => (UnaryFuncInfo(sinh, Trig), ""),
"tan" => UnaryFuncInfo(tan, Trig), "tan" => (UnaryFuncInfo(tan, Trig), ""),
"tanh" => UnaryFuncInfo(tanh, Trig), "tanh" => (UnaryFuncInfo(tanh, Trig), ""),
"acos" => UnaryFuncInfo(acos, InverseTrig), "acos" => (UnaryFuncInfo(acos, InverseTrig), "rad"),
"acosec" => UnaryFuncInfo(acosec, InverseTrig), "acosec" => (UnaryFuncInfo(acosec, InverseTrig), "rad"),
"acosech" => UnaryFuncInfo(acosech, InverseTrig), "acosech" => (UnaryFuncInfo(acosech, InverseTrig), "rad"),
"acosh" => UnaryFuncInfo(acosh, InverseTrig), "acosh" => (UnaryFuncInfo(acosh, InverseTrig), "rad"),
"acot" => UnaryFuncInfo(acot, InverseTrig), "acot" => (UnaryFuncInfo(acot, InverseTrig), "rad"),
"acoth" => UnaryFuncInfo(acoth, InverseTrig), "acoth" => (UnaryFuncInfo(acoth, InverseTrig), "rad"),
"asec" => UnaryFuncInfo(asec, InverseTrig), "asec" => (UnaryFuncInfo(asec, InverseTrig), "rad"),
"asech" => UnaryFuncInfo(asech, InverseTrig), "asech" => (UnaryFuncInfo(asech, InverseTrig), "rad"),
"asin" => UnaryFuncInfo(asin, InverseTrig), "asin" => (UnaryFuncInfo(asin, InverseTrig), "rad"),
"asinh" => UnaryFuncInfo(asinh, InverseTrig), "asinh" => (UnaryFuncInfo(asinh, InverseTrig), "rad"),
"atan" => UnaryFuncInfo(atan, InverseTrig), "atan" => (UnaryFuncInfo(atan, InverseTrig), "rad"),
"atanh" => UnaryFuncInfo(atanh, InverseTrig), "atanh" => (UnaryFuncInfo(atanh, InverseTrig), "rad"),
"abs" => UnaryFuncInfo(abs, Other), "abs" => (UnaryFuncInfo(abs, Other), ""),
"cbrt" => UnaryFuncInfo(cbrt, Other), "cbrt" => (UnaryFuncInfo(cbrt, Other), ""),
"ceil" => UnaryFuncInfo(ceil, Other), "ceil" => (UnaryFuncInfo(ceil, Other), ""),
"exp" => UnaryFuncInfo(exp, Other), "exp" => (UnaryFuncInfo(exp, Other), ""),
"floor" => UnaryFuncInfo(floor, Other), "floor" => (UnaryFuncInfo(floor, Other), ""),
"frac" => UnaryFuncInfo(frac, Other), "frac" => (UnaryFuncInfo(frac, Other), ""),
"gamma" => UnaryFuncInfo(gamma, Other), "gamma" => (UnaryFuncInfo(gamma, Other), ""),
"Γ" => UnaryFuncInfo(gamma, Other), "Γ" => (UnaryFuncInfo(gamma, Other), ""),
"log" => UnaryFuncInfo(log, Other), "log" => (UnaryFuncInfo(log, Other), ""),
"ln" => UnaryFuncInfo(ln, Other), "ln" => (UnaryFuncInfo(ln, Other), ""),
"round" => UnaryFuncInfo(round, Other), "round" => (UnaryFuncInfo(round, Other), ""),
"sqrt" => UnaryFuncInfo(sqrt, Other), "sqrt" => (UnaryFuncInfo(sqrt, Other), ""),
"" => UnaryFuncInfo(sqrt, Other), "" => (UnaryFuncInfo(sqrt, Other), ""),
"trunc" => UnaryFuncInfo(trunc, Other), "trunc" => (UnaryFuncInfo(trunc, Other), ""),
}; };
pub const BINARY_FUNCS: phf::Map<&'static str, BinaryFuncInfo> = phf::phf_map! { pub const BINARY_FUNCS: phf::Map<&'static str, (BinaryFuncInfo, &'static str)> = phf::phf_map! {
"max" => BinaryFuncInfo(max, Other), "max" => (BinaryFuncInfo(max, Other), ""),
"min" => BinaryFuncInfo(min, Other), "min" => (BinaryFuncInfo(min, Other), ""),
"hyp" => BinaryFuncInfo(hyp, Other), "hyp" => (BinaryFuncInfo(hyp, Other), ""),
"log" => BinaryFuncInfo(logx, Other), "log" => (BinaryFuncInfo(logx, Other), ""),
"root" => BinaryFuncInfo(nth_root, Other), "root" => (BinaryFuncInfo(nth_root, Other), ""),
}; };
enum FuncType { enum FuncType {
@ -113,9 +113,12 @@ pub fn call_unary_func(
name: &str, name: &str,
x: Float, x: Float,
angle_unit: &str, angle_unit: &str,
) -> Option<Float> { ) -> Option<(Float, String)> {
if let Some(func_info) = UNARY_FUNCS.get(name) { if let Some((func_info, func_unit)) = UNARY_FUNCS.get(name) {
Some(func_info.call(context, x, &angle_unit)) Some((
func_info.call(context, x, &angle_unit),
func_unit.to_string(),
))
} else { } else {
None None
} }
@ -127,9 +130,12 @@ pub fn call_binary_func(
x: Float, x: Float,
y: Float, y: Float,
angle_unit: &str, angle_unit: &str,
) -> Option<Float> { ) -> Option<(Float, String)> {
if let Some(func_info) = BINARY_FUNCS.get(name) { if let Some((func_info, func_unit)) = BINARY_FUNCS.get(name) {
Some(func_info.call(context, x, y, angle_unit)) Some((
func_info.call(context, x, y, angle_unit),
func_unit.to_string(),
))
} else { } else {
None None
} }