#notation #reverse #polish #calculator #back-end #operations

dc-ock

A simple reverse polish notation desk calculator library

2 stable releases

2.0.1 Aug 5, 2022
1.0.0 Aug 4, 2022
0.3.0 Aug 4, 2022
0.2.0 Aug 4, 2022
0.1.2 Aug 4, 2022

#785 in Math

37 downloads per month
Used in dc-cli

MIT license

14KB
231 lines

dc.rs

dc.rs (also dc-ock, for the Open Computing Kit) is a reverse Polish notation (postfix) calculator library, providing backend functionality roughly akin to the unix "dc" command, written in Rust. It lacks many of the more advanced features of dc, but still has core mathematical operations needed for most simple usage.

There is a command line utility available built on this library.

Building dc.rs

...is a little bit awkward, but it's not too bad.

To build dc.rs, you'll need cargo.You can build the library with cargo build --lib, or you can build the executable with cargo build --features bbin --bin dc-cli.

It's a bit weird because there are binary-only dependencies, but rust doesn't support them yet, so we hack around it by using the --features flag. This stops massive downloads of the binary dependencies when you build the library.

Examples

Conversion from string to CalcType:

fn main() {
  let x: CalcType = str_to_calc_type("1").unwrap(); // returns Val(1.)
  let y: CalcType = str_to_calc_type("+").unwrap(); // returns Addition
  let z: CalcType = str_to_calc_type("*").unwrap(); // returns Multiplication
  // and so on.
}

Evaluating expressions safely:

fn main() {
    let expr = "1 2 +";
    match safe_eval(expr) {
        Ok(x) => println!("{:?}", x), // prints [3.0]
        Err(e) => println!("{}", e),  // prints an error message
    }
}  

Evaluating expressions with stack persistence:

fn main() {
    let mut stack: VecDeque<f64> = VecDeque::new();
    stack.push_back(2.);
    stack.push_back(7.5);
    stack.push_back(3.5);

    stack = safe_eval_with_stack("+ +", stack).unwrap();
    println!("{:?}", stack);
}

No runtime deps