From d8eed924cc2ceff5ca7267b000ccc6cd692098dc Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 29 Aug 2019 13:21:43 +0300 Subject: [PATCH] Add documentation about operator assignments --- CHANGELOG.md | 1 + README.md | 22 ++++++++++++++++++++++ src/lib.rs | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d99e5c2..dbb723d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Add a macro for more convenient definition of contexts including the direct definition of static contexts * Add API for value decomposition * Allow using context operations in `eval` calls without context + * Operator assignment operators for each binary operation (`+=`, `-=`, ...) ### Removed diff --git a/README.md b/README.md index 56bbc6f..3b3b415 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,8 @@ assert_eq!(eval_empty_with_context_mut("a = 5.0", &mut context), Err(EvalexprErr assert_eq!(context.get_value("a"), Some(&Value::from(5))); // And use the value in another expression like this assert_eq!(eval_int_with_context_mut("a = a + 2; a", &mut context), Ok(7)); +// It is also possible to safe a bit of typing by using an operator-assignment operator +assert_eq!(eval_int_with_context_mut("a += 2; a", &mut context), Ok(9)); ``` And you can use **variables** and **functions** in expressions like this: @@ -144,6 +146,14 @@ Supported binary operators: | && | 75 | Logical and | | || | 70 | Logical or | | = | 50 | Assignment | +| += | 50 | Sum-Assignment or String-Concatenation-Assignment | +| -= | 50 | Difference-Assignment | +| *= | 50 | Product-Assignment | +| /= | 50 | Division-Assignment | +| %= | 50 | Modulo-Assignment | +| ^= | 50 | Exponentiation-Assignment | +| &&= | 50 | Logical-And-Assignment | +| ||= | 50 | Logical-Or-Assignment | | , | 40 | Aggregation | | ; | 0 | Expression Chaining | @@ -183,6 +193,18 @@ assert_eq!(eval_int_with_context("a", &context), Ok(5)); assert_eq!(context.get_value("a"), Some(5.into()).as_ref()); ``` +For each binary operator, there exists and equivalent operator-assignment operator. +Here are some examples: + +```rust +use evalexpr::*; + +assert_eq!(eval_int("a = 2; a *= 2; a += 2; a"), Ok(6)); +assert_eq!(eval_float("a = 2.2; a /= 2.0 / 4 + 1; a"), Ok(2.2 / (2.0 / 4.0 + 1.0))); +assert_eq!(eval_string("a = \"abc\"; a += \"def\"; a"), Ok("abcdef".to_string())); +assert_eq!(eval_boolean("a = true; a &&= false; a"), Ok(false)); +``` + #### The Expression Chaining Operator The expression chaining operator works as one would expect from programming languages that use the semicolon to end statements, like `Rust`, `C` or `Java`. diff --git a/src/lib.rs b/src/lib.rs index 2335adf..b2a8e85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,8 @@ //! assert_eq!(context.get_value("a"), Some(&Value::from(5))); //! // And use the value in another expression like this //! assert_eq!(eval_int_with_context_mut("a = a + 2; a", &mut context), Ok(7)); +//! // It is also possible to safe a bit of typing by using an operator-assignment operator +//! assert_eq!(eval_int_with_context_mut("a += 2; a", &mut context), Ok(9)); //! ``` //! //! And you can use **variables** and **functions** in expressions like this: @@ -131,6 +133,14 @@ //! | && | 75 | Logical and | //! | || | 70 | Logical or | //! | = | 50 | Assignment | +//! | += | 50 | Sum-Assignment or String-Concatenation-Assignment | +//! | -= | 50 | Difference-Assignment | +//! | *= | 50 | Product-Assignment | +//! | /= | 50 | Division-Assignment | +//! | %= | 50 | Modulo-Assignment | +//! | ^= | 50 | Exponentiation-Assignment | +//! | &&= | 50 | Logical-And-Assignment | +//! | ||= | 50 | Logical-Or-Assignment | //! | , | 40 | Aggregation | //! | ; | 0 | Expression Chaining | //! @@ -170,6 +180,18 @@ //! assert_eq!(context.get_value("a"), Some(5.into()).as_ref()); //! ``` //! +//! For each binary operator, there exists and equivalent operator-assignment operator. +//! Here are some examples: +//! +//! ```rust +//! use evalexpr::*; +//! +//! assert_eq!(eval_int("a = 2; a *= 2; a += 2; a"), Ok(6)); +//! assert_eq!(eval_float("a = 2.2; a /= 2.0 / 4 + 1; a"), Ok(2.2 / (2.0 / 4.0 + 1.0))); +//! assert_eq!(eval_string("a = \"abc\"; a += \"def\"; a"), Ok("abcdef".to_string())); +//! assert_eq!(eval_boolean("a = true; a &&= false; a"), Ok(false)); +//! ``` +//! //! #### The Expression Chaining Operator //! //! The expression chaining operator works as one would expect from programming languages that use the semicolon to end statements, like `Rust`, `C` or `Java`.