Crate mexprp [] [src]

MEXPRP

A math expression parsing and evaluating library

Usage

There are three different ways to parse and evaluate an equation.

With eval()

This function parses and evaluates a string all at once with the default context. There's also an eval_ctx() function which takes a reference to a Context as well that will be used instead of the default Context.

mexprp::eval("10 / (2 + 3)"); // Ok(2.0)

With Expression

Expression::parse() parses a string into a tree representation (a Term). It can also be parsed with a context with parse_ctx, and it will store that context within it for future evaluations. It can also be evaluated with a reference to any other context with eval_ctx. It's important to ensure that the custom context contains any definitions the Expression depends on.

let expr = Expression::parse("3 ^ 4 / 9").unwrap();
let res = expr.eval(); // Ok(9.0)

With Term

A Term is an Expression, but without any extra overhead.

let term = Term::parse("10 ^ -3").unwrap();
let res = term.eval(); // Ok(0.001)

Using Contexts

You can evaluate expressions with custom variable and function definition's by defining a context. When defining custom functions, it's important to remember to parse the expression with the custom context, or else the parser will recognize your functions as variables instead. Expressions will store the context you parse them with, but you have to evaluate Terms with a reference to a context using Term::eval_ctx. For more info see the context module.

Re-exports

pub use func::Func;
pub use expr::Calculation;
pub use expr::Expression;
pub use expr::Term;
pub use context::Context;
pub use errors::EvalError;
pub use errors::MathError;
pub use errors::ParseError;

Modules

context

Context struct

errors

All the errors

expr

Contains expressions and terms

func

Contains Function trait

op

Contains definitions for Operations

opers

Contains implementations for operations

parse

Contains methods for parsing equations into token representations

Functions

eval

Parse and evaluate a string

eval_ctx

Parse and evaluate a string with the given context