2019-03-18 17:51:09 +00:00
# evalexpr
2016-11-16 16:31:52 +00:00
2019-03-15 11:12:18 +00:00
[![docs ](https://docs.rs/evalexpr/badge.svg?version=0.4.4 "docs" )](https://docs.rs/evalexpr)
2016-11-16 16:12:26 +00:00
2019-03-18 17:51:09 +00:00
Evalexpr is a powerful arithmetic and boolean expression evaluator.
2016-11-16 16:12:26 +00:00
2019-03-18 17:51:09 +00:00
## [Documentation](https://docs.rs/evalexpr)
2017-02-25 06:51:55 +00:00
2019-03-19 08:43:52 +00:00
<!-- cargo - sync - readme start -->
2019-03-19 10:01:06 +00:00
## Quickstart
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
Add `evalexpr` as dependency to your `Cargo.toml` :
2019-03-19 08:43:52 +00:00
```toml
[dependencies]
2019-03-19 10:01:06 +00:00
evalexpr = "0.5"
2019-03-19 08:43:52 +00:00
```
2019-03-19 10:01:06 +00:00
Add the `extern crate` definition to your `main.rs` or `lib.rs` :
2019-03-19 08:43:52 +00:00
```rust
2019-03-19 10:01:06 +00:00
extern crate evalexpr;
2019-03-19 08:43:52 +00:00
```
2019-03-19 10:01:06 +00:00
Then you can use `evalexpr` to evaluate expressions like this:
2019-03-19 08:43:52 +00:00
```rust
2019-03-19 10:01:06 +00:00
use evalexpr::*;
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
assert_eq!(eval("1 + 2 + 3"), Ok(Value::from(6)));
assert_eq!(eval("1 - 2 * 3"), Ok(Value::from(-5)));
assert_eq!(eval("1.0 + 2 * 3"), Ok(Value::from(7.0)));
assert_eq!(eval("true & & 4 > 2"), Ok(Value::from(true)));
2019-03-19 08:43:52 +00:00
```
2019-03-19 10:01:06 +00:00
And you can use variables and functions in expressions like this:
2019-03-19 08:43:52 +00:00
```rust
2019-03-19 10:01:06 +00:00
use evalexpr::*;
let mut configuration = HashMapConfiguration::new();
configuration.insert_variable("five", 5);
configuration.insert_variable("twelve", 12);
configuration.insert_function("f", Function::new(1 /* argument amount */, Box::new(|arguments| {
if let Value::Int(int) = arguments[0] {
Ok(Value::Int(int / 2))
} else if let Value::Float(float) = arguments[0] {
Ok(Value::Float(float / 2.0))
} else {
Err(Error::expected_number(arguments[0].clone()))
}
})));
assert_eq!(eval_with_configuration("five + 8 > f(twelve)", & configuration), Ok(Value::from(true)));
2019-03-19 08:43:52 +00:00
```
2019-03-19 10:01:06 +00:00
You can also precompile expressions like this:
2019-03-19 08:43:52 +00:00
```rust
2019-03-19 10:01:06 +00:00
use evalexpr::*;
let precompiled = build_operator_tree("a * b - c > 5").unwrap();
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
let mut configuration = HashMapConfiguration::new();
configuration.insert_variable("a", 6);
configuration.insert_variable("b", 2);
configuration.insert_variable("c", 3);
assert_eq!(precompiled.eval(& configuration), Ok(Value::from(true)));
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
configuration.insert_variable("c", 8);
assert_eq!(precompiled.eval(& configuration), Ok(Value::from(false)));
2019-03-19 08:43:52 +00:00
```
2019-03-19 10:01:06 +00:00
## Features
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
### Operators
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
Supported binary operators:
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
| Operator | Precedence | Description | | Operator | Precedence | Description |
|----------|------------|-------------|---|----------|------------|-------------|
| + | 95 | Sum | | < | 80 | Lower than |
| - | 95 | Difference | | \> | 80 | Greater than |
| * | 100 | Product | | < = | 80 | Lower than or equal |
| / | 100 | Division | | \>= | 80 | Greater than or equal |
| % | 100 | Modulo | | == | 80 | Equal |
| & & | 75 | Logical and | | != | 80 | Not equal |
| | | | 70 | Logical or | | | |
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
Supported unary operators:
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
| Operator | Precedence | Description |
|----------|------------|-------------|
| - | 110 | Negation |
| ! | 110 | Logical not |
2019-03-19 08:43:52 +00:00
2019-03-19 10:22:06 +00:00
Operators with higher precedence have a higher priority when determining the order of evaluation.
The precedence of variables and values is 200, and the precedence of function literals is 190.
2019-03-19 10:01:06 +00:00
### Values
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
Operators take values as arguments and produce values as results.
Values can be boolean, integer or floating point numbers.
Strings are supported as well, but there are no operations defined for them yet.
Values are denoted as displayed in the following table.
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
| Value type | Example |
|------------|---------|
| `Value::Boolean` | `true` , `false` |
| `Value::Int` | `3` , `-9` , `0` , `135412` |
| `Value::Float` | `3.` , `.35` , `1.00` , `0.5` , `123.554` |
Integers are internally represented as `i64` , and floating point numbers are represented as `f64` .
Operators that take numbers as arguments can either take integers or floating point numbers.
If one of the arguments is a floating point number, all others are converted to floating point numbers as well, and the resulting value is a floating point number as well.
Otherwise, the result is an integer.
2019-03-19 10:22:06 +00:00
Values have a precedence of 200.
2019-03-19 10:01:06 +00:00
### Variables
This crate allows to compile parameterizable formulas by using variables.
A variable is a literal in the formula, that does not contain whitespace or can be parsed as value.
The user needs to provide bindings to the variables for evaluation.
This is done with the `Configuration` trait.
Two structs implementing this trait are predefined.
There is `EmptyConfiguration` , that returns `None` for each request, and `HashMapConfiguration` , that stores mappings from literals to variables in a hash map.
Variables do not have fixed types in the expression itself, but aer typed by the configuration.
The `Configuration` trait contains a function that takes a string literal and returns a `Value` enum.
The variant of this enum decides the type on evaluation.
2019-03-19 10:22:06 +00:00
Variables have a precedence of 200.
2019-03-19 10:01:06 +00:00
### Functions
This crate also allows to define arbitrary functions to be used in parsed expressions.
A function is defined as a `Function` instance.
It contains two properties, the `argument_amount` and the `function` .
The `function` is a boxed `Fn(&[Value]) -> Result<Value, Error>` .
2019-03-19 10:22:06 +00:00
The `argument_amount` determines the length of the slice that is passed to `function` .
It is verified on execution by the crate and does not need to be verified by the `function` .
Be aware that functions need to verify the types of values that are passed to them.
The `error` module contains some shortcuts for verification, and error types for passing a wrong value type.
Also, most numeric functions need to differentiate between being called with integers or floating point numbers, and act accordingly.
Functions are identified by literals, like variables as well.
A literal identifies a function, if it is followed by an opening brace `(` , another literal, or a value.
Same as variables, function bindings are provided by the user via a `Configuration` .
Functions have a precedence of 190.
### Examplary variables and functions in expressions:
| Expression | Valid? | Explanation |
|------------|--------|-------------|
| `a` | yes | |
| `abc` | yes | |
| `a<b` | no | Expression is interpreted as variable `a` , operator `<` and variable `b` |
| `a b` | no | Expression is interpreted as function `a` applied to argument `b` |
| `123` | no | Expression is interpreted as `Value::Int` |
| `true` | no | Expression is interpreted as `Value::Bool` |
| `.34` | no | Expression is interpreted as `Value::Float` |
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
## License
2019-03-19 08:43:52 +00:00
2019-03-19 10:01:06 +00:00
This crate is primarily distributed under the terms of the MIT license.
See [LICENSE ](LICENSE ) for details.
2019-03-19 08:43:52 +00:00
<!-- cargo - sync - readme end -->