dc-ock

Crates.iodc-ock
lib.rsdc-ock
version2.0.1
sourcesrc
created_at2022-08-04 21:40:00.346258
updated_at2022-08-05 00:21:25.393387
descriptionA simple reverse polish notation desk calculator library
homepagehttps://haskell.gay
repositoryhttps://github.com/lillycat332/dc-rs
max_upload_size
id638984
size14,858
Lilly (lillycat332)

documentation

README

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);
}
Commit count: 29

cargo fmt