#expression #reverse #polish #rpn

ripin

A library that handle Reverse Polish notated expressions, compiles, and evaluate them

3 releases

Uses old Rust 2015

0.1.2 Jan 11, 2017
0.1.1 Jan 7, 2017
0.1.0 Jan 5, 2017

#2079 in Rust patterns

22 downloads per month

MIT license

48KB
1K SLoC

Ripin-rs

Crates.io Documentation Build Status Coverage Status

A library to handle Reverse Polish notated expressions.

Ripin can also evaluate variable expressions and is not limited to string tokens, it can handle any iterator type, the only limit is your imagination. There is examples to understand how to implement your own expression from custom types.

Installation

Ripin is available on crates.io and can be included in your Cargo enabled project like this:

[dependencies]
ripin = "0.1"

Then include it in your code like this:

extern crate ripin;

Examples

Ripin can evaluate Reverse Polish Notated expressions.

use ripin::expression::FloatExpr;

let expr_str = "3 4 + 2 *"; // (3 + 4) * 2
let tokens = expr_str.split_whitespace();

let expr = FloatExpr::<f32>::from_iter(tokens).unwrap();

println!("Expression {:?} gives {:?}", expr_str, expr.evaluate())

It is also possible to use variables in your expressions to make them more "variable".

use std::collections::HashMap;
use ripin::evaluate::VariableFloatExpr;
use ripin::variable::IndexVar;

let mut variables = HashMap::new(); // using a Vec is the same
variables.insert(0, 3.0);
variables.insert(1, 500.0);

let expr_str = "3 $1 + 2 * $0 -"; // (3 + $1) * 2 - $0
let tokens = expr_str.split_whitespace();

let expr = VariableFloatExpr::<f32, IndexVar>::from_iter(tokens).unwrap();

let result = expr.evaluate_with_variables(&variables);

println!("Expression {:?} gives {:?}", expr_str, result); // Ok(1003)

Dependencies

~245KB