Makefile, rpn(1): add sysexits dependency

This commit is contained in:
Emma Tebibyte 2024-02-01 00:41:27 -07:00
parent 32537895cb
commit 4870f4679c
Signed by: emma
GPG Key ID: 06FA419A1698C270
2 changed files with 16 additions and 7 deletions

View File

@ -83,7 +83,7 @@ build/bin/intcmp: src/intcmp.c build
.PHONY: rpn .PHONY: rpn
rpn: build/bin/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 $(RUSTC) $(RUSTFLAGS) -o $@ src/rpn.rs
.PHONY: scrut .PHONY: scrut

View File

@ -52,6 +52,10 @@ use std::{
use CalcType::{ Add, Divide, Multiply, Power, Subtract, Val }; use CalcType::{ Add, Divide, Multiply, Power, Subtract, Val };
extern crate sysexits;
use sysexits::EX_DATAERR;
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)] #[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
/// enum CalcType is a type containing operations used in the calculator. /// enum CalcType is a type containing operations used in the calculator.
enum CalcType { enum CalcType {
@ -156,7 +160,8 @@ fn eval(
Val(_) => { Val(_) => {
return Err( return Err(
EvaluationError { 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 { fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>(); let argv = args().collect::<Vec<String>>();
let stack = VecDeque::new(); let mut stack = VecDeque::new();
let mut buf = String::new(); let mut buf = String::new();
let precision = (-f64::EPSILON.log10() * 0.999).ceil() as usize; let precision = (-f64::EPSILON.log10() * 0.999).ceil() as usize;
if argv.get(1).is_none() { if argv.get(1).is_none() {
while let Ok(_) = stdin().read_line(&mut buf) { while let Ok(_) = stdin().read_line(&mut buf) {
match eval(&buf.trim(), stack.clone()) { match eval(&buf.trim(), stack) {
Ok(val) => { 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()); println!("{}", precise.to_string());
buf.clear(); buf.clear();
}, },
Err(err) => { Err(err) => {
eprintln!("{}: {}", argv[0], err.message); 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()), Ok(val) => println!("{}", val.iter().last().unwrap().to_string()),
Err(err) => { Err(err) => {
eprintln!("{}: {}", argv[0], err.message); eprintln!("{}: {}", argv[0], err.message);
return ExitCode::from(1); return ExitCode::from(EX_DATAERR as u8);
}, },
}; };
} }