commit
642289588d
@ -121,6 +121,11 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
|
|||||||
Ok(Value::Float(max_float))
|
Ok(Value::Float(max_float))
|
||||||
}
|
}
|
||||||
})),
|
})),
|
||||||
|
"if" => Some(Function::new(|argument| {
|
||||||
|
let mut arguments = argument.as_fixed_len_tuple(3)?;
|
||||||
|
let result_index = if arguments[0].as_boolean()? { 1 } else { 2 };
|
||||||
|
Ok(arguments.swap_remove(result_index))
|
||||||
|
})),
|
||||||
"len" => Some(Function::new(|argument| {
|
"len" => Some(Function::new(|argument| {
|
||||||
if let Ok(subject) = argument.as_string() {
|
if let Ok(subject) = argument.as_string() {
|
||||||
Ok(Value::from(subject.len() as i64))
|
Ok(Value::from(subject.len() as i64))
|
||||||
|
@ -327,6 +327,7 @@
|
|||||||
//! | `floor` | 1 | Numeric | Returns the largest integer less than or equal to a number |
|
//! | `floor` | 1 | Numeric | Returns the largest integer less than or equal to a number |
|
||||||
//! | `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 |
|
||||||
//! | `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 |
|
||||||
|
@ -408,6 +408,12 @@ fn test_builtin_functions() {
|
|||||||
assert_eq!(eval("shl(-6, 5)"), Ok(Value::Int(-192)));
|
assert_eq!(eval("shl(-6, 5)"), Ok(Value::Int(-192)));
|
||||||
assert_eq!(eval("shr(5, 1)"), Ok(Value::Int(2)));
|
assert_eq!(eval("shr(5, 1)"), Ok(Value::Int(2)));
|
||||||
assert_eq!(eval("shr(-6, 5)"), Ok(Value::Int(-1)));
|
assert_eq!(eval("shr(-6, 5)"), Ok(Value::Int(-1)));
|
||||||
|
assert_eq!(eval("if(true, -6, 5)"), Ok(Value::Int(-6)));
|
||||||
|
assert_eq!(eval("if(false, -6, 5)"), Ok(Value::Int(5)));
|
||||||
|
assert_eq!(
|
||||||
|
eval("if(2-1==1, \"good\", 0)"),
|
||||||
|
Ok(Value::String(String::from("good")))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -459,6 +465,12 @@ fn test_no_panic() {
|
|||||||
IntType::max_value()
|
IntType::max_value()
|
||||||
))
|
))
|
||||||
.is_ok());
|
.is_ok());
|
||||||
|
assert!(eval("if").is_err());
|
||||||
|
assert!(eval("if()").is_err());
|
||||||
|
assert!(eval("if(true, 1)").is_err());
|
||||||
|
assert!(eval("if(false, 2)").is_err());
|
||||||
|
assert!(eval("if(1,1,1)").is_err());
|
||||||
|
assert!(eval("if(true,1,1,1)").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user