| Crates.io | wcal |
| lib.rs | wcal |
| version | 0.2.0 |
| created_at | 2021-04-22 15:10:07.595441+00 |
| updated_at | 2021-04-23 11:11:17.607518+00 |
| description | A calculator include lexer and parser. |
| homepage | |
| repository | https://github.com/weijunji/wcal |
| max_upload_size | |
| id | 388165 |
| size | 36,338 |
A calculator write by rust
Allow operator: + - * / ( ).
Result can be i128 or f64. A warning will
occur while result is i128 and division cast
happened, such as 3/2=1.
This calculator has three steps:
logos to parse the expression to tokens.The following parser is available:
use wcal::{calculator, parser};
fn main() {
let res: f64 = calculator!("1+2").unwrap();
assert_eq!(res, 3f64);
let res: i128 = calculator("1+2", wcal::parser::top_down_parser::parse).unwrap();
assert_eq!(res, 3);
let res: f64 = calculator("1+2", wcal::parser::top_down_parser::parse).unwrap();
assert_eq!(res, 3f64);
}
For more usage of this crate, please see the document.
Requirement: rust cargo
$ cargo run build --release
$ wcal "2*6+(1/2)" -f "2*6+(1/2)"
i> 2*6+(1/2)
Warning: division will cause a cast
12
f> 2*6+(1/2)
12.5
Default mod is i128, use -f to change to
f64, use -i to change back.
$ wcal
i> help
i Enter i128 mod
f Enter f64 mod
quit
q Quit
i> f
Enter f64 mod
f> 1/2
0.5
f> quit
Bye!