Expand description
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
…).
§Expression Usage
Let’s define an expression:
let expr = "3 4 + 2 *"; // (3 + 4) * 2
Tokenize it:
let tokens = expr.split_whitespace();
Ripin can evaluate floating-point
expressions:
use ripin::evaluate::FloatExpr;
let expr = FloatExpr::<f32>::from_iter(tokens).unwrap();
assert_eq!(expr.evaluate(), Ok(14.0));
like integers
ones:
use ripin::evaluate::IntExpr;
let expr = IntExpr::<i32>::from_iter(tokens).unwrap();
assert_eq!(expr.evaluate(), Ok(14));
§Variable Expression Usage
You need variable expressions ? No problem Ripin can do this too !
Declare some variables:
let variables = vec![3.0, 500.0];
Once variables as been set, do the same as before:
use ripin::evaluate::VariableFloatExpr;
use ripin::variable::IndexVar;
let expr = "3 4 + 2 * $0 -"; // (3 + 4) * 2 - $0
let tokens = expr.split_whitespace();
let expr = VariableFloatExpr::<f32, IndexVar>::from_iter(tokens).unwrap();
Evaluate the expression with informations about the way of indexation (usize
):
assert_eq!(expr.evaluate_with_variables(&variables), Ok(11.0));
Modules§
- convert_
ref - TryFrom/Into_ref conversion module
- evaluate
Evaluate Trait
and defaultEvaluators
.- expression
- Operation on expressions and
Expression
construction methods. - variable
- Useful structs to use variables with expressions
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.