2019-04-07 06:10:36 +00:00
|
|
|
#[cfg(feature = "regex_support")]
|
|
|
|
use regex::Regex;
|
|
|
|
|
2019-08-29 07:02:05 +00:00
|
|
|
use crate::{
|
|
|
|
value::{FloatType, IntType},
|
2019-08-29 08:10:54 +00:00
|
|
|
EvalexprError, Function, Value, ValueType,
|
2019-08-29 07:02:05 +00:00
|
|
|
};
|
2019-03-19 18:08:23 +00:00
|
|
|
|
2021-05-30 06:52:28 +00:00
|
|
|
macro_rules! simple_math {
|
|
|
|
($func:ident) => {
|
|
|
|
Some(Function::new(|argument| {
|
|
|
|
let num = argument.as_number()?;
|
|
|
|
Ok(Value::Float(num.$func()))
|
|
|
|
}))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-19 18:08:23 +00:00
|
|
|
pub fn builtin_function(identifier: &str) -> Option<Function> {
|
|
|
|
match identifier {
|
2021-05-30 06:52:28 +00:00
|
|
|
// Log
|
2021-05-30 23:04:33 +00:00
|
|
|
"math::ln" => simple_math!(ln),
|
|
|
|
"math::log" => Some(Function::new(|argument| {
|
2021-05-30 06:52:28 +00:00
|
|
|
let tuple = argument.as_fixed_len_tuple(2)?;
|
|
|
|
let (a, b) = (tuple[0].as_number()?, tuple[1].as_number()?);
|
|
|
|
Ok(Value::Float(a.log(b)))
|
|
|
|
})),
|
2021-05-30 23:04:33 +00:00
|
|
|
"math::log2" => simple_math!(log2),
|
|
|
|
"math::log10" => simple_math!(log10),
|
2021-05-30 06:52:28 +00:00
|
|
|
// Cos
|
2021-05-30 23:04:33 +00:00
|
|
|
"math::cos" => simple_math!(cos),
|
|
|
|
"math::acos" => simple_math!(acos),
|
|
|
|
"math::cosh" => simple_math!(cosh),
|
|
|
|
"math::acosh" => simple_math!(acosh),
|
2021-05-30 06:52:28 +00:00
|
|
|
// Sin
|
2021-05-30 23:04:33 +00:00
|
|
|
"math::sin" => simple_math!(sin),
|
|
|
|
"math::asin" => simple_math!(asin),
|
|
|
|
"math::sinh" => simple_math!(sinh),
|
|
|
|
"math::asinh" => simple_math!(asinh),
|
2021-05-30 06:52:28 +00:00
|
|
|
// Tan
|
2021-05-30 23:04:33 +00:00
|
|
|
"math::tan" => simple_math!(tan),
|
|
|
|
"math::atan" => simple_math!(atan),
|
|
|
|
"math::tanh" => simple_math!(tanh),
|
|
|
|
"math::atanh" => simple_math!(atanh),
|
2021-05-30 06:52:28 +00:00
|
|
|
// Root
|
2021-05-30 23:04:33 +00:00
|
|
|
"math::sqrt" => simple_math!(sqrt),
|
|
|
|
"math::cbrt" => simple_math!(cbrt),
|
2021-05-30 06:52:28 +00:00
|
|
|
// Rounding
|
|
|
|
"floor" => simple_math!(floor),
|
|
|
|
"round" => simple_math!(round),
|
|
|
|
"ceil" => simple_math!(ceil),
|
|
|
|
// Other
|
2021-05-28 11:12:26 +00:00
|
|
|
"min" => Some(Function::new(|argument| {
|
2019-08-29 05:56:49 +00:00
|
|
|
let arguments = argument.as_tuple()?;
|
2019-05-04 11:54:19 +00:00
|
|
|
let mut min_int = IntType::max_value();
|
|
|
|
let mut min_float = 1.0f64 / 0.0f64;
|
|
|
|
debug_assert!(min_float.is_infinite());
|
2019-03-19 18:08:23 +00:00
|
|
|
|
2019-05-04 11:54:19 +00:00
|
|
|
for argument in arguments {
|
|
|
|
if let Value::Float(float) = argument {
|
2019-08-29 05:56:49 +00:00
|
|
|
min_float = min_float.min(float);
|
2019-05-04 11:54:19 +00:00
|
|
|
} else if let Value::Int(int) = argument {
|
2019-08-29 05:56:49 +00:00
|
|
|
min_int = min_int.min(int);
|
2019-03-19 18:08:23 +00:00
|
|
|
} else {
|
2021-05-28 06:07:26 +00:00
|
|
|
return Err(EvalexprError::expected_number(argument));
|
2019-03-19 18:08:23 +00:00
|
|
|
}
|
2019-05-04 11:54:19 +00:00
|
|
|
}
|
2019-03-19 18:08:23 +00:00
|
|
|
|
2019-05-04 11:54:19 +00:00
|
|
|
if (min_int as FloatType) < min_float {
|
|
|
|
Ok(Value::Int(min_int))
|
|
|
|
} else {
|
|
|
|
Ok(Value::Float(min_float))
|
|
|
|
}
|
2021-05-28 11:12:26 +00:00
|
|
|
})),
|
|
|
|
"max" => Some(Function::new(|argument| {
|
2019-08-29 05:56:49 +00:00
|
|
|
let arguments = argument.as_tuple()?;
|
2019-05-04 11:54:19 +00:00
|
|
|
let mut max_int = IntType::min_value();
|
|
|
|
let mut max_float = -1.0f64 / 0.0f64;
|
|
|
|
debug_assert!(max_float.is_infinite());
|
2019-03-19 18:08:23 +00:00
|
|
|
|
2019-05-04 11:54:19 +00:00
|
|
|
for argument in arguments {
|
|
|
|
if let Value::Float(float) = argument {
|
2019-08-29 05:56:49 +00:00
|
|
|
max_float = max_float.max(float);
|
2019-05-04 11:54:19 +00:00
|
|
|
} else if let Value::Int(int) = argument {
|
2019-08-29 05:56:49 +00:00
|
|
|
max_int = max_int.max(int);
|
2019-03-19 18:08:23 +00:00
|
|
|
} else {
|
2021-05-28 06:07:26 +00:00
|
|
|
return Err(EvalexprError::expected_number(argument));
|
2019-03-19 18:08:23 +00:00
|
|
|
}
|
2019-05-04 11:54:19 +00:00
|
|
|
}
|
2019-04-07 06:10:36 +00:00
|
|
|
|
2019-05-04 11:54:19 +00:00
|
|
|
if (max_int as FloatType) > max_float {
|
|
|
|
Ok(Value::Int(max_int))
|
|
|
|
} else {
|
|
|
|
Ok(Value::Float(max_float))
|
|
|
|
}
|
2021-05-28 11:12:26 +00:00
|
|
|
})),
|
|
|
|
"len" => Some(Function::new(|argument| {
|
2019-08-29 08:10:54 +00:00
|
|
|
if let Ok(subject) = argument.as_string() {
|
|
|
|
Ok(Value::from(subject.len() as i64))
|
|
|
|
} else if let Ok(subject) = argument.as_tuple() {
|
|
|
|
Ok(Value::from(subject.len() as i64))
|
|
|
|
} else {
|
|
|
|
Err(EvalexprError::type_error(
|
|
|
|
argument.clone(),
|
|
|
|
vec![ValueType::String, ValueType::Tuple],
|
|
|
|
))
|
|
|
|
}
|
2021-05-28 11:12:26 +00:00
|
|
|
})),
|
2021-05-30 06:52:28 +00:00
|
|
|
// String functions
|
2019-04-07 06:10:36 +00:00
|
|
|
#[cfg(feature = "regex_support")]
|
2021-05-28 11:12:26 +00:00
|
|
|
"str::regex_matches" => Some(Function::new(|argument| {
|
2019-08-29 05:56:49 +00:00
|
|
|
let arguments = argument.as_tuple()?;
|
2019-05-04 11:43:29 +00:00
|
|
|
|
2019-08-29 05:56:49 +00:00
|
|
|
let subject = arguments[0].as_string()?;
|
|
|
|
let re_str = arguments[1].as_string()?;
|
|
|
|
match Regex::new(&re_str) {
|
|
|
|
Ok(re) => Ok(Value::Boolean(re.is_match(&subject))),
|
2019-05-04 11:54:19 +00:00
|
|
|
Err(err) => Err(EvalexprError::invalid_regex(
|
|
|
|
re_str.to_string(),
|
|
|
|
format!("{}", err),
|
|
|
|
)),
|
|
|
|
}
|
2021-05-28 11:12:26 +00:00
|
|
|
})),
|
2019-04-07 06:10:36 +00:00
|
|
|
#[cfg(feature = "regex_support")]
|
2021-05-28 11:12:26 +00:00
|
|
|
"str::regex_replace" => Some(Function::new(|argument| {
|
2019-08-29 05:56:49 +00:00
|
|
|
let arguments = argument.as_tuple()?;
|
2019-05-04 11:43:29 +00:00
|
|
|
|
2019-08-29 05:56:49 +00:00
|
|
|
let subject = arguments[0].as_string()?;
|
|
|
|
let re_str = arguments[1].as_string()?;
|
|
|
|
let repl = arguments[2].as_string()?;
|
|
|
|
match Regex::new(&re_str) {
|
2019-08-29 07:02:05 +00:00
|
|
|
Ok(re) => Ok(Value::String(
|
|
|
|
re.replace_all(&subject, repl.as_str()).to_string(),
|
|
|
|
)),
|
2019-05-04 11:54:19 +00:00
|
|
|
Err(err) => Err(EvalexprError::invalid_regex(
|
|
|
|
re_str.to_string(),
|
|
|
|
format!("{}", err),
|
|
|
|
)),
|
|
|
|
}
|
2021-05-28 11:12:26 +00:00
|
|
|
})),
|
|
|
|
"str::to_lowercase" => Some(Function::new(|argument| {
|
2019-08-29 05:56:49 +00:00
|
|
|
let subject = argument.as_string()?;
|
2019-05-04 11:54:19 +00:00
|
|
|
Ok(Value::from(subject.to_lowercase()))
|
2021-05-28 11:12:26 +00:00
|
|
|
})),
|
|
|
|
"str::to_uppercase" => Some(Function::new(|argument| {
|
2019-08-29 05:56:49 +00:00
|
|
|
let subject = argument.as_string()?;
|
2019-05-04 11:54:19 +00:00
|
|
|
Ok(Value::from(subject.to_uppercase()))
|
2021-05-28 11:12:26 +00:00
|
|
|
})),
|
|
|
|
"str::trim" => Some(Function::new(|argument| {
|
2019-08-29 05:56:49 +00:00
|
|
|
let subject = argument.as_string()?;
|
2019-05-04 11:54:19 +00:00
|
|
|
Ok(Value::from(subject.trim()))
|
2021-05-28 11:12:26 +00:00
|
|
|
})),
|
2021-05-30 06:52:28 +00:00
|
|
|
"str::from" => Some(Function::new(|argument| {
|
|
|
|
Ok(Value::String(argument.to_string()))
|
|
|
|
})),
|
2019-03-19 18:08:23 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|