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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*!
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.4"
```
# Example: with `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: with `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: SUBEXP,
// | tokens: [
// | Token(
// | type: SUBEXP,
// | tokens: [
// | Token(type: NUMBER, literal: "2")
// | Token(type: PLUS, literal: "+")
// | Token(type: NUMBER, literal: "5")
// | ],
// | ),
// | Token(type: PRODUCT, literal: "*"),
// | Token(
// | type: SUBEXP,
// | tokens: [
// | Token(type: NUMBER, literal: "5")
// | Token(type: MINUS, literal: "-")
// | Token(type: NUMBER, literal: "9")
// | Token(type: PLUS, literal: "+")
// | Token(
// | type: SUBEXP,
// | tokens: [
// | Token(type: NUMBER, literal: "8")
// | Token(type: PLUS, literal: "-")
// | Token(type: NUMBER, literal: "5")
// | ],
// | ),
// | ],
// | ),
// | ],
// | ),
// | Token(type: PLUS, literal: "+")
// | Token(type: NUMBER, literal: "35")
// |
let input = "[ (2 + 5) * (5 - 9 + (8 - 5)) ] + 35";
let tokens = Lexer::lex(input.clone()).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, input.clone());
match result {
Ok(v) => assert_eq!(v, 42.0),
Err(_) => {
// Do something ...
}
};
```
> For details refer to [repository](https://github.com/theiskaa/mate).
*/