Crate wcal

Source
Expand description

A calculator that implement for Arithmetic.

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)

§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);
}

Modules§

generator
Use ast to calculate or generate target code.
lexer
Lexer for the Arithmetic calculator lexical structure.
parser
Parser for the Arithmetic calculator grammar.

Macros§

calculator
Use default parser to calculate the expression.

Traits§

FromAST
Result that can be calculate from the AST

Functions§

calculator
Use a parser to calculate the expression.