1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/*!
This crate provides a library for parsing and calculating arithmetic expressions inputted as &str(string).
Uses `Lexer` structure to parse string input in to token list, and `Calculator` structure to calculate final result from token list.
Has also a general wrapper structure that implements `Lexer` and `Calculator` inside of it. And makes it easy to calculate arithmetic
expression's result directly without dealing with parsing and calculating manually.
# Usage
This crate is on [crates.io](http://crates.io/crates/mate-rs) and can be used by adding mate-rs to your dependencies in your project's `Cargo.toml`.
```toml
[dependencies]
mate-rs = "0.1.3"
```
# Example: Simple usage | `Mate`
`Mate` is general wrapper structure for `Lexer` and `Calculator`.
has only one method that used to `calculate` result via string(&str) input.
```rust
use mate_rs::mate::Mate;
let result = Mate::calculate("6 * 7");
match result {
Ok(v) => assert_eq!(v, 42.0),
Err(_) => {
// Do something ...
}
};
```
# Example: Complicated usage | `Lexer` and `Calculator`
`Lexer` is the main structure that parses string-input to token-list.
`Calculator` is the structure that used to calculate final result via `Lexer`'s result.
```rust
use mate_rs::{calculator::Calculator, lexer::Lexer};
// Generated tokens gonna be something like:
// | Token(type: NUMBER literal: "-2"),
// | Token(type: PLUS literal: "+"),
// | Token(type: NUMBER literal: "2"),
// | Token(type: PLUS literal: "+"),
// | Token(
// | type: SUBEXP,
// | tokens: [
// | Token(type: NUMBER, literal: "6")
// | Token(type: PRODUCT, literal: "*")
// | Token(type: NUMBER, literal: "7")
// | ],
// | ),
let tokens = Lexer::lex(" - 2 + 2 + 6 * 7").unwrap(); // should handle error case also
// Result will be calculated from tokens, by X/O/Y algorithm.
//
// ╭────────╮ ╭───────────╮ ╭────────╮
// │ NUMBER │ │ OPERATION │ │ NUMBER │
// ╰────────╯ ╰───────────╯ ╰────────╯
// ╰───╮ │ ╭───╯
// ▼ ▼ ▼
// X [+, -, *, /] Y
//
let result = Calculator::calculate(tokens);
match result {
Ok(v) => assert_eq!(v, 42.0),
Err(_) => {
// Do something ...
}
};
```
> For details refer to [repository](https://github.com/theiskaa/mate).
*/
pub mod calculator;
pub mod errors;
pub mod lexer;
pub mod mate;
pub mod monitor;
pub mod token;
pub mod utils;