Rename Node::eval to Node::eval_with_configuration for consistency.

* Rename `Node::eval` to `Node::eval_with_configuration`
 + Add `Node::eval` that calls `Node::eval_with_configuration` with an `EmptyConfiguration`
 + Improve documentation of `Node` and the changed methods

Implements #5
This commit is contained in:
Sebastian Schmidt 2019-03-20 11:58:08 +02:00
parent e387d3370b
commit 58e8603194
2 changed files with 24 additions and 12 deletions

View File

@ -69,10 +69,10 @@
//! configuration.insert_variable("a", 6); //! configuration.insert_variable("a", 6);
//! configuration.insert_variable("b", 2); //! configuration.insert_variable("b", 2);
//! configuration.insert_variable("c", 3); //! configuration.insert_variable("c", 3);
//! assert_eq!(precompiled.eval(&configuration), Ok(Value::from(true))); //! assert_eq!(precompiled.eval_with_configuration(&configuration), Ok(Value::from(true)));
//! //!
//! configuration.insert_variable("c", 8); //! configuration.insert_variable("c", 8);
//! assert_eq!(precompiled.eval(&configuration), Ok(Value::from(false))); //! assert_eq!(precompiled.eval_with_configuration(&configuration), Ok(Value::from(false)));
//! ``` //! ```
//! //!
//! ## Features //! ## Features
@ -234,7 +234,7 @@ pub use value::Value;
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval(string: &str) -> Result<Value, Error> { pub fn eval(string: &str) -> Result<Value, Error> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(&EmptyConfiguration) eval_with_configuration(string, &EmptyConfiguration)
} }
/// Evaluate the given expression string with the given configuration. /// Evaluate the given expression string with the given configuration.
@ -256,7 +256,7 @@ pub fn eval_with_configuration(
string: &str, string: &str,
configuration: &Configuration, configuration: &Configuration,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(configuration) tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_configuration(configuration)
} }
/// Build the operator tree for the given expression string. /// Build the operator tree for the given expression string.
@ -276,10 +276,10 @@ pub fn eval_with_configuration(
/// configuration.insert_variable("two", 2); /// configuration.insert_variable("two", 2);
/// configuration.insert_variable("three", 3); /// configuration.insert_variable("three", 3);
/// ///
/// assert_eq!(precomputed.eval(&configuration), Ok(Value::from(6))); /// assert_eq!(precomputed.eval_with_configuration(&configuration), Ok(Value::from(6)));
/// ///
/// configuration.insert_variable("three", 5); /// configuration.insert_variable("three", 5);
/// assert_eq!(precomputed.eval(&configuration), Ok(Value::from(8))); /// assert_eq!(precomputed.eval_with_configuration(&configuration), Ok(Value::from(8)));
/// ``` /// ```
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*

View File

@ -1,5 +1,6 @@
use crate::{configuration::Configuration, error::Error, operator::*, value::Value}; use crate::{configuration::Configuration, error::Error, operator::*, value::Value};
use token::Token; use token::Token;
use EmptyConfiguration;
mod display; mod display;
@ -7,13 +8,17 @@ mod display;
/// The operator tree is created by the crate-level `build_operator_tree` method. /// The operator tree is created by the crate-level `build_operator_tree` method.
/// It can be evaluated for a given configuration with the `Node::eval` method. /// It can be evaluated for a given configuration with the `Node::eval` method.
/// ///
/// The advantage of constructing the operator tree separately from the actual evaluation is that it can be evaluated arbitrarily often with different configurations.
///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use evalexpr::*; /// use evalexpr::*;
/// ///
/// let node = build_operator_tree("1 + 2").unwrap(); /// let mut configuration = HashMapConfiguration::new();
/// assert_eq!(node.eval(&EmptyConfiguration), Ok(Value::from(3))); /// configuration.insert_variable("alpha", 2);
/// let node = build_operator_tree("1 + alpha").unwrap();
/// assert_eq!(node.eval_with_configuration(&configuration), Ok(Value::from(3)));
/// ``` /// ```
/// ///
#[derive(Debug)] #[derive(Debug)]
@ -34,17 +39,24 @@ impl Node {
Self::new(RootNode) Self::new(RootNode)
} }
/// Evaluates the operator tree rooted at this node. /// Evaluates the operator tree rooted at this node with the given configuration.
/// ///
/// Fails, if an operator is used with a wrong number of arguments or a wrong type. /// Fails, if one of the operators in the expression tree fails.
pub fn eval(&self, configuration: &Configuration) -> Result<Value, Error> { pub fn eval_with_configuration(&self, configuration: &Configuration) -> Result<Value, Error> {
let mut arguments = Vec::new(); let mut arguments = Vec::new();
for child in self.children() { for child in self.children() {
arguments.push(child.eval(configuration)?); arguments.push(child.eval_with_configuration(configuration)?);
} }
self.operator().eval(&arguments, configuration) self.operator().eval(&arguments, configuration)
} }
/// Evaluates the operator tree rooted at this node with an empty configuration.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval(&self) -> Result<Value, Error> {
self.eval_with_configuration(&EmptyConfiguration)
}
fn children(&self) -> &[Node] { fn children(&self) -> &[Node] {
&self.children &self.children
} }