Crates.io | dc-ock |
lib.rs | dc-ock |
version | 2.0.1 |
source | src |
created_at | 2022-08-04 21:40:00.346258 |
updated_at | 2022-08-05 00:21:25.393387 |
description | A simple reverse polish notation desk calculator library |
homepage | https://haskell.gay |
repository | https://github.com/lillycat332/dc-rs |
max_upload_size | |
id | 638984 |
size | 14,858 |
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.
...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.
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);
}