Crate ripin [] [src]

This library provide a way to evaluate Reverse Polish Notated expressions for numbers and basic evaluators.

Floats and signed integers has already been implemented with basic evaluators (cf. +, -, *, /, neg, pow...).

Of course you can build your own Evaluators by implementing the Evaluate Trait, the only constraint is the compatibility with the Operand type, that can be everything you want (cf. letters, Enums, chinese symbols...).

Example

use ripin::evaluate::{FloatExpr, IntExpr};

let str_expr = "3 4 + 2 *"; // (3 + 4) * 2
let tokens = str_expr.split_whitespace();
let expr = FloatExpr::<f32>::from_iter(tokens).unwrap();

assert_eq!(expr.evaluate(), Ok(14.0)); // yup that's a Float evaluation

// let's try an Integer evaluation:
let tokens = str_expr.split_whitespace();
let expr = IntExpr::<i32>::from_iter(tokens).unwrap();
assert_eq!(expr.evaluate(), Ok(14));

// You need variable expressions ? No problem Ripin can do this !
use ripin::evaluate::VariableFloatExpr;
use ripin::variable::VarIdx;

let variables = vec![3.0, 500.0];

let str_expr = "3 4 + 2 * $0 -"; // (3 + 4) * 2 - $0
let tokens = str_expr.split_whitespace();
let expr = VariableFloatExpr::<f32, VarIdx>::from_iter(tokens).unwrap();

assert_eq!(expr.evaluate_with_variables::<usize, _>(variables), Ok(11.0));

Modules

convert_ref

Custom conversion module

evaluate

Evaluate Trait and default Evaluators.

expression

Operation on expressions and Expression construction methods.

variable

Structs

Stack

A growable stack implementing push/pop actions.

Functions

pop_two_operands

Removes the last two elements from a stack and return them, or None if there is not enough element.