forked from bonsai/harakit
Makefile, rpn(1): add sysexits dependency
This commit is contained in:
parent
32537895cb
commit
4870f4679c
2
Makefile
2
Makefile
@ -83,7 +83,7 @@ build/bin/intcmp: src/intcmp.c build
|
||||
|
||||
.PHONY: rpn
|
||||
rpn: build/bin/rpn
|
||||
build/bin/rpn: src/rpn.rs build
|
||||
build/bin/rpn: src/rpn.rs build sysexits
|
||||
$(RUSTC) $(RUSTFLAGS) -o $@ src/rpn.rs
|
||||
|
||||
.PHONY: scrut
|
||||
|
21
src/rpn.rs
21
src/rpn.rs
@ -52,6 +52,10 @@ use std::{
|
||||
|
||||
use CalcType::{ Add, Divide, Multiply, Power, Subtract, Val };
|
||||
|
||||
extern crate sysexits;
|
||||
|
||||
use sysexits::EX_DATAERR;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
|
||||
/// enum CalcType is a type containing operations used in the calculator.
|
||||
enum CalcType {
|
||||
@ -156,7 +160,8 @@ fn eval(
|
||||
Val(_) => {
|
||||
return Err(
|
||||
EvaluationError {
|
||||
message: "Unexpected value in the operator stack.".to_string()
|
||||
message: "Unexpected value in the operator stack."
|
||||
.to_string()
|
||||
}
|
||||
)
|
||||
},
|
||||
@ -173,21 +178,25 @@ fn round_precise(value: &f64, precision: usize) -> f64 {
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
let stack = VecDeque::new();
|
||||
let mut stack = VecDeque::new();
|
||||
let mut buf = String::new();
|
||||
let precision = (-f64::EPSILON.log10() * 0.999).ceil() as usize;
|
||||
|
||||
if argv.get(1).is_none() {
|
||||
while let Ok(_) = stdin().read_line(&mut buf) {
|
||||
match eval(&buf.trim(), stack.clone()) {
|
||||
match eval(&buf.trim(), stack) {
|
||||
Ok(val) => {
|
||||
let precise = round_precise(val.iter().last().unwrap(), precision);
|
||||
stack = val.clone();
|
||||
let precise = round_precise(
|
||||
stack.iter().last().unwrap(),
|
||||
precision,
|
||||
);
|
||||
println!("{}", precise.to_string());
|
||||
buf.clear();
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("{}: {}", argv[0], err.message);
|
||||
return ExitCode::from(1);
|
||||
return ExitCode::from(EX_DATAERR as u8);
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -203,7 +212,7 @@ fn main() -> ExitCode {
|
||||
Ok(val) => println!("{}", val.iter().last().unwrap().to_string()),
|
||||
Err(err) => {
|
||||
eprintln!("{}: {}", argv[0], err.message);
|
||||
return ExitCode::from(1);
|
||||
return ExitCode::from(EX_DATAERR as u8);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user