Implement changes requested in PR

https://github.com/ISibboI/evalexpr/pull/101#issuecomment-1076466426
This commit is contained in:
Ophir LOJKINE 2022-03-23 16:33:53 +01:00
parent 678c881377
commit 8eedf720e7
4 changed files with 46 additions and 30 deletions

View File

@ -345,10 +345,11 @@ This crate offers a set of builtin functions.
| `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 | | `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 |
| `ceil` | 1 | Numeric | Returns the smallest integer greater than or equal to a number | | `ceil` | 1 | Numeric | Returns the smallest integer greater than or equal to a number |
| `if` | 3 | Boolean, Any, Any | If the first argument is true, returns the second argument, otherwise, returns the third | | `if` | 3 | Boolean, Any, Any | If the first argument is true, returns the second argument, otherwise, returns the third |
| `is_nan` | 1 | Numeric | Returns true if the argument is the floating-point value NaN, false otherwise | | `typeof` | 1 | Any | returns "string", "float", "int", "boolean", "tuple", or "empty" depending on the type of the argument |
| `is_finite` | 1 | Numeric | Returns true if the argument is a finite floating-point number, false otherwise | | `math::is_nan` | 1 | Numeric | Returns true if the argument is the floating-point value NaN, false if it is another floating-point value, and throws an error if it is not a number |
| `is_infinite` | 1 | Numeric | Returns true if the argument is an infinite floating-point number, false otherwise | | `math::is_finite` | 1 | Numeric | Returns true if the argument is a finite floating-point number, false otherwise |
| `is_normal` | 1 | Numeric | Returns true if the argument is a floating-point number that is neither zero, infinite, [subnormal](https://en.wikipedia.org/wiki/Subnormal_number), or NaN, false otherwise | | `math::is_infinite` | 1 | Numeric | Returns true if the argument is an infinite floating-point number, false otherwise |
| `math::is_normal` | 1 | Numeric | Returns true if the argument is a floating-point number that is neither zero, infinite, [subnormal](https://en.wikipedia.org/wiki/Subnormal_number), or NaN, false otherwise |
| `math::ln` | 1 | Numeric | Returns the natural logarithm of the number | | `math::ln` | 1 | Numeric | Returns the natural logarithm of the number |
| `math::log` | 2 | Numeric, Numeric | Returns the logarithm of the number with respect to an arbitrary base | | `math::log` | 2 | Numeric, Numeric | Returns the logarithm of the number with respect to an arbitrary base |
| `math::log2` | 1 | Numeric | Returns the base 2 logarithm of the number | | `math::log2` | 1 | Numeric | Returns the base 2 logarithm of the number |

View File

@ -24,13 +24,9 @@ macro_rules! simple_math {
} }
fn float_is(func: fn(f64) -> bool) -> Option<Function> { fn float_is(func: fn(f64) -> bool) -> Option<Function> {
Some(Function::new(move |argument| { Some(Function::new(move |argument|
if let Ok(num) = argument.as_float() { Ok(func(argument.as_number()?).into())
Ok(func(num).into()) ))
} else {
Ok(false.into())
}
}))
} }
macro_rules! int_function { macro_rules! int_function {
@ -86,11 +82,21 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
"floor" => simple_math!(floor), "floor" => simple_math!(floor),
"round" => simple_math!(round), "round" => simple_math!(round),
"ceil" => simple_math!(ceil), "ceil" => simple_math!(ceil),
"is_nan" => float_is(f64::is_nan), "math::is_nan" => float_is(f64::is_nan),
"is_finite" => float_is(f64::is_finite), "math::is_finite" => float_is(f64::is_finite),
"is_infinite" => float_is(f64::is_infinite), "math::is_infinite" => float_is(f64::is_infinite),
"is_normal" => float_is(f64::is_normal), "math::is_normal" => float_is(f64::is_normal),
// Other // Other
"typeof" => Some(Function::new(move |argument|
Ok(match argument {
Value::String(_) => "string",
Value::Float(_) => "float",
Value::Int(_) => "int",
Value::Boolean(_) => "boolean",
Value::Tuple(_) => "tuple",
Value::Empty => "empty",
}.into())
)),
"min" => Some(Function::new(|argument| { "min" => Some(Function::new(|argument| {
let arguments = argument.as_tuple()?; let arguments = argument.as_tuple()?;
let mut min_int = IntType::max_value(); let mut min_int = IntType::max_value();

View File

@ -328,10 +328,11 @@
//! | `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 | //! | `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 |
//! | `ceil` | 1 | Numeric | Returns the smallest integer greater than or equal to a number | //! | `ceil` | 1 | Numeric | Returns the smallest integer greater than or equal to a number |
//! | `if` | 3 | Boolean, Any, Any | If the first argument is true, returns the second argument, otherwise, returns the third | //! | `if` | 3 | Boolean, Any, Any | If the first argument is true, returns the second argument, otherwise, returns the third |
//! | `is_nan` | 1 | Numeric | Returns true if the argument is the floating-point value NaN, false otherwise | //! | `typeof` | 1 | Any | returns "string", "float", "int", "boolean", "tuple", or "empty" depending on the type of the argument |
//! | `is_finite` | 1 | Numeric | Returns true if the argument is a finite floating-point number, false otherwise | //! | `math::is_nan` | 1 | Numeric | Returns true if the argument is the floating-point value NaN, false if it is another floating-point value, and throws an error if it is not a number |
//! | `is_infinite` | 1 | Numeric | Returns true if the argument is an infinite floating-point number, false otherwise | //! | `math::is_finite` | 1 | Numeric | Returns true if the argument is a finite floating-point number, false otherwise |
//! | `is_normal` | 1 | Numeric | Returns true if the argument is a floating-point number that is neither zero, infinite, [subnormal](https://en.wikipedia.org/wiki/Subnormal_number), or NaN, false otherwise | //! | `math::is_infinite` | 1 | Numeric | Returns true if the argument is an infinite floating-point number, false otherwise |
//! | `math::is_normal` | 1 | Numeric | Returns true if the argument is a floating-point number that is neither zero, infinite, [subnormal](https://en.wikipedia.org/wiki/Subnormal_number), or NaN, false otherwise |
//! | `math::ln` | 1 | Numeric | Returns the natural logarithm of the number | //! | `math::ln` | 1 | Numeric | Returns the natural logarithm of the number |
//! | `math::log` | 2 | Numeric, Numeric | Returns the logarithm of the number with respect to an arbitrary base | //! | `math::log` | 2 | Numeric, Numeric | Returns the logarithm of the number with respect to an arbitrary base |
//! | `math::log2` | 1 | Numeric | Returns the base 2 logarithm of the number | //! | `math::log2` | 1 | Numeric | Returns the base 2 logarithm of the number |

View File

@ -362,17 +362,22 @@ fn test_builtin_functions() {
assert_eq!(eval("round(1.9)"), Ok(Value::Float(2.0))); assert_eq!(eval("round(1.9)"), Ok(Value::Float(2.0)));
assert_eq!(eval("ceil(1.1)"), Ok(Value::Float(2.0))); assert_eq!(eval("ceil(1.1)"), Ok(Value::Float(2.0)));
assert_eq!(eval("ceil(1.9)"), Ok(Value::Float(2.0))); assert_eq!(eval("ceil(1.9)"), Ok(Value::Float(2.0)));
assert_eq!(eval("is_nan(\"xxx\")"), Ok(Value::Boolean(false))); assert_eq!(eval("math::is_nan(1.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_nan(1.0/0.0)"), Ok(Value::Boolean(false))); assert_eq!(eval("math::is_nan(0.0/0.0)"), Ok(Value::Boolean(true)));
assert_eq!(eval("is_nan(0.0/0.0)"), Ok(Value::Boolean(true))); assert_eq!(eval("math::is_finite(1.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_finite(1.0/0.0)"), Ok(Value::Boolean(false))); assert_eq!(eval("math::is_finite(0.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_finite(0.0/0.0)"), Ok(Value::Boolean(false))); assert_eq!(eval("math::is_finite(0.0)"), Ok(Value::Boolean(true)));
assert_eq!(eval("is_finite(0.0)"), Ok(Value::Boolean(true))); assert_eq!(eval("math::is_infinite(0.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_infinite(0.0/0.0)"), Ok(Value::Boolean(false))); assert_eq!(eval("math::is_infinite(1.0/0.0)"), Ok(Value::Boolean(true)));
assert_eq!(eval("is_infinite(1.0/0.0)"), Ok(Value::Boolean(true))); assert_eq!(eval("math::is_normal(1.0/0.0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_normal(1.0/0.0)"), Ok(Value::Boolean(false))); assert_eq!(eval("math::is_normal(0)"), Ok(Value::Boolean(false)));
assert_eq!(eval("is_normal(0)"), Ok(Value::Boolean(false)));
// Other // Other
assert_eq!(eval("typeof(4.0, 3)"), Ok(Value::String("tuple".into())));
assert_eq!(eval("typeof(4.0)"), Ok(Value::String("float".into())));
assert_eq!(eval("typeof(4)"), Ok(Value::String("int".into())));
assert_eq!(eval("typeof(\"\")"), Ok(Value::String("string".into())));
assert_eq!(eval("typeof(true)"), Ok(Value::String("boolean".into())));
assert_eq!(eval("typeof()"), Ok(Value::String("empty".into())));
assert_eq!(eval("min(4.0, 3)"), Ok(Value::Int(3))); assert_eq!(eval("min(4.0, 3)"), Ok(Value::Int(3)));
assert_eq!(eval("max(4.0, 3)"), Ok(Value::Float(4.0))); assert_eq!(eval("max(4.0, 3)"), Ok(Value::Float(4.0)));
assert_eq!(eval("len(\"foobar\")"), Ok(Value::Int(6))); assert_eq!(eval("len(\"foobar\")"), Ok(Value::Int(6)));
@ -440,10 +445,13 @@ fn test_errors() {
eval("true-"), eval("true-"),
Err(EvalexprError::WrongOperatorArgumentAmount { Err(EvalexprError::WrongOperatorArgumentAmount {
actual: 1, actual: 1,
expected: 2 expected: 2,
}) })
); );
assert_eq!(eval("!(()true)"), Err(EvalexprError::AppendedToLeafNode)); assert_eq!(eval("!(()true)"), Err(EvalexprError::AppendedToLeafNode));
assert_eq!(eval("math::is_nan(\"xxx\")"), Err(EvalexprError::ExpectedNumber {
actual: Value::String("xxx".to_string())
}));
} }
#[test] #[test]