From c58d5dcda539c847591ec0c7f64e6a4781c706e4 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 19 Mar 2019 12:46:55 +0200 Subject: [PATCH] Test if functions can actually be called with multiple arguments --- Cargo.toml | 2 +- src/lib.rs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 07e3bfc..5101cdb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "evalexpr" -version = "0.5.0" +version = "0.6.0" description = "Expression evaluator" keywords = ["expression", "evaluate", "evaluator", "arithmetic", "boolean"] authors = ["isibboi "] diff --git a/src/lib.rs b/src/lib.rs index 7eb90fe..bcd065e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,7 @@ //! //! ```rust //! use evalexpr::*; +//! use evalexpr::error::expect_number; //! //! let mut configuration = HashMapConfiguration::new(); //! configuration.insert_variable("five", 5); @@ -42,8 +43,19 @@ //! Err(Error::expected_number(arguments[0].clone())) //! } //! }))); +//! configuration.insert_function("avg", Function::new(2 /* argument amount */, Box::new(|arguments| { +//! expect_number(&arguments[0])?; +//! expect_number(&arguments[1])?; +//! +//! if let (Value::Int(a), Value::Int(b)) = (&arguments[0], &arguments[1]) { +//! Ok(Value::Int((a + b) / 2)) +//! } else { +//! Ok(Value::Float((arguments[0].as_float()? + arguments[1].as_float()?) / 2.0)) +//! } +//! }))); //! //! assert_eq!(eval_with_configuration("five + 8 > f(twelve)", &configuration), Ok(Value::from(true))); +//! assert_eq!(eval_with_configuration("avg(2, 4) == 3", &configuration), Ok(Value::from(true))); //! ``` //! //! You can also precompile expressions like this: @@ -162,7 +174,7 @@ //! mod configuration; -mod error; +pub mod error; mod function; mod operator; mod token;