https://github.com/prashantrahul141/expr-solver
Simple expression solver, using Pratt Parser technique, and a Tree Walker Interpreter, written in rust.
https://github.com/prashantrahul141/expr-solver
crate expression-evaluator expression-parser rust
Last synced: 11 months ago
JSON representation
Simple expression solver, using Pratt Parser technique, and a Tree Walker Interpreter, written in rust.
- Host: GitHub
- URL: https://github.com/prashantrahul141/expr-solver
- Owner: prashantrahul141
- License: mit
- Created: 2024-02-06T20:02:08.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-10T17:13:19.000Z (almost 2 years ago)
- Last Synced: 2025-02-28T04:48:24.568Z (11 months ago)
- Topics: crate, expression-evaluator, expression-parser, rust
- Language: Rust
- Homepage: https://crates.io/crates/expr-solver
- Size: 61.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Expression Solver
Solves a mathematical expression while following precedence and associativity.
The crate provides one public api function.
```rs
fn resolve(input_string: String) -> Result
```
This takes mathematical expressions as String, and returns a Result enum with solved value or incase of an error, error string.
### Examples
```rs
use expr_solver::resolve;
// simple binary expression.
resolve("2+2".to_string()); // Ok(2.0)
// follows precendence, 2 + (2 _ 2) and NOT (2 + 2) _ 2
resolve("2+2*2".to_string()); // Ok(6.0);
// unary expression.
resolve("-2".to_string()); // Ok(-2.0)
// even chain them. -(-2)
resolve("--2".to_string()); // Ok(2.0)
// binary and unary in one expression.
resolve("2+-2".to_string()); // Ok(0.0)
// gives syntax error.
resolve("2)2".to_string()); // 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.