Expression Solver
Solves a mathematical expression while following precedence and associativity.
The crate provides one public api function.
This takes mathematical expressions as String, and returns a Result enum with solved value or incase of an error, error string.
Examples
use resolve;
// simple binary expression.
resolve; // Ok(2.0)
// follows precendence, 2 + (2 _ 2) and NOT (2 + 2) _ 2
resolve; // Ok(6.0);
// unary expression.
resolve; // Ok(-2.0)
// even chain them. -(-2)
resolve; // Ok(2.0)
// binary and unary in one expression.
resolve; // Ok(0.0)
// gives syntax error.
resolve; // Err(String);
Inner workings
There are three steps involved
1. Lexical Analysis.
Breaks the input string into indiviual tokens.
2. Parser
This uses a Pratt Parsing technique to parse the stream of tokens into Abstract Syntax Tree (AST).
3. Interpreting
Uses a 'Tree-Walk' interpreter to evalute the AST.