A powerful expression evaluation crate for Rust.
Go to file
2019-03-19 11:03:34 +02:00
src Add precedences to operators in docs 2019-03-19 11:03:34 +02:00
.gitignore ignore .idea 2018-01-17 07:59:43 +08:00
Cargo.toml Fixed warnings and removed unused dependencies. 2019-03-15 17:21:34 +02:00
LICENSE init repository. 2016-11-17 00:12:26 +08:00
README.md Add precedences to operators in docs 2019-03-19 11:03:34 +02:00
rustfmt.toml Complete rewrite of crate. Simple tokenizer and treeifyer (Or however the compiler people call these.). 2019-03-15 17:14:27 +02:00

evalexpr

docs

Evalexpr is a powerful arithmetic and boolean expression evaluator.

Documentation

Features

Operators

Supported binary operators:

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

Supported unary operators:

Operator Precedence Description
- 110 Negation
! 110 Logical not

Values

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.

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.

Variables

Supported binary operators: ! != "" '' () [] , > < >= <= == + unary/binary - * / % && || n..m.

Supported unary operators: ``

Built-in functions: min() max() len() is_empty() array() converge(). See the builtin module for a detailed description of each.

Where can eval be used?

  • Template engine
  • Scripting language
  • ...

Usage

Add dependency to Cargo.toml

[dependencies]
evalexpr = "0.4"

In your main.rs or lib.rs:

extern crate evalexpr as eval;

Examples

You can do mathematical calculations with supported operators:

use eval::{eval, to_value};

assert_eq!(eval("1 + 2 + 3"), Ok(to_value(6)));
assert_eq!(eval("2 * 2 + 3"), Ok(to_value(7)));
assert_eq!(eval("2 / 2 + 3"), Ok(to_value(4.0)));
assert_eq!(eval("2 / 2 + 3 / 3"), Ok(to_value(2.0)));

You can eval with context:

use eval::{Expr, to_value};

assert_eq!(Expr::new("foo == bar")
               .value("foo", true)
               .value("bar", true)
               .exec(),
           Ok(to_value(true)));

You can access data like javascript by using . and []. [] supports expression.

use eval::{Expr, to_value};
use std::collections::HashMap;

let mut object = HashMap::new();
object.insert("foos", vec!["Hello", "world", "!"]);

assert_eq!(Expr::new("object.foos[1-1] == 'Hello'")
               .value("object", object)
               .exec(),
           Ok(to_value(true)));

You can eval with function:

use eval::{Expr, to_value};

assert_eq!(Expr::new("say_hello()")
               .function("say_hello", |_| Ok(to_value("Hello world!")))
               .exec(),
           Ok(to_value("Hello world!")));

You can create an array with array():

use eval::{eval, to_value};

assert_eq!(eval("array(1, 2, 3, 4, 5)"), Ok(to_value(vec![1, 2, 3, 4, 5])));

You can create an integer array with n..m:

use eval::{eval, to_value};

assert_eq!(eval("0..5"), Ok(to_value(vec![0, 1, 2, 3, 4])));

License

evalexpr is primarily distributed under the terms of the MIT license. See LICENSE for details.