1
0
forked from bonsai/harakit

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 untrusted user: 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
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

View File

@ -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);
},
};
}