wcal 0.2.0

A calculator include lexer and parser.
Documentation
  • Coverage
  • 48.94%
    23 out of 47 items documented3 out of 19 items with examples
  • Size
  • Source code size: 35.81 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.6 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 15s Average build duration of successful builds.
  • all releases: 15s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • weijunji/wcal
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • weijunji

wcal

Build Crates.io version shield Docs Crates.io license shield

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:

  • Use logos to parse the expression to tokens.
  • Use a parser to parse tokens to a AST.
  • Calculate the result from the AST.

The following parser is available:

  • Top-down parser (default)

Library Usage

Example

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.

Executable Usage

Build

Requirement: rust cargo

$ cargo run build --release

Command line mode

$ 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.

Interactive mode

$ 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!