Test if functions can actually be called with multiple arguments

This commit is contained in:
Sebastian Schmidt 2019-03-19 12:46:55 +02:00
parent 53136e826a
commit c58d5dcda5
2 changed files with 14 additions and 2 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "evalexpr" name = "evalexpr"
version = "0.5.0" version = "0.6.0"
description = "Expression evaluator" description = "Expression evaluator"
keywords = ["expression", "evaluate", "evaluator", "arithmetic", "boolean"] keywords = ["expression", "evaluate", "evaluator", "arithmetic", "boolean"]
authors = ["isibboi <isibboi@gmail.com>"] authors = ["isibboi <isibboi@gmail.com>"]

View File

@ -29,6 +29,7 @@
//! //!
//! ```rust //! ```rust
//! use evalexpr::*; //! use evalexpr::*;
//! use evalexpr::error::expect_number;
//! //!
//! let mut configuration = HashMapConfiguration::new(); //! let mut configuration = HashMapConfiguration::new();
//! configuration.insert_variable("five", 5); //! configuration.insert_variable("five", 5);
@ -42,8 +43,19 @@
//! Err(Error::expected_number(arguments[0].clone())) //! 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("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: //! You can also precompile expressions like this:
@ -162,7 +174,7 @@
//! //!
mod configuration; mod configuration;
mod error; pub mod error;
mod function; mod function;
mod operator; mod operator;
mod token; mod token;