From 4870f4679c47b0b69740cc5e589bb84b1e233d70 Mon Sep 17 00:00:00 2001 From: emma Date: Thu, 1 Feb 2024 00:41:27 -0700 Subject: [PATCH] Makefile, rpn(1): add sysexits dependency --- Makefile | 2 +- src/rpn.rs | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index ef52a78..9d4261d 100644 --- a/Makefile +++ b/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 diff --git a/src/rpn.rs b/src/rpn.rs index 6819ca1..7e7f948 100644 --- a/src/rpn.rs +++ b/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::>(); - 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); }, }; }