forked from bonsai/harakit
rpn(1): refactor to make code more efficient, readable, and maintainable
This commit is contained in:
parent
150fa22f35
commit
a0138be79e
103
src/rpn.rs
103
src/rpn.rs
@ -54,7 +54,7 @@ use CalcType::*;
|
|||||||
|
|
||||||
extern crate sysexits;
|
extern crate sysexits;
|
||||||
|
|
||||||
use sysexits::EX_DATAERR;
|
use sysexits::{ EX_DATAERR, EX_IOERR };
|
||||||
|
|
||||||
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
||||||
#[cfg(target_os="openbsd")] extern crate strerror;
|
#[cfg(target_os="openbsd")] extern crate strerror;
|
||||||
@ -120,13 +120,18 @@ impl Display for CalcType {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct EvaluationError {
|
struct EvaluationError {
|
||||||
message: String,
|
message: String,
|
||||||
code: i32,
|
code: ExitCode,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* I’m no math nerd but I want the highest possible approximation of 0.9
|
/* I’m no math nerd but I want the highest possible approximation of 0.9
|
||||||
* repeating and it seems this can give it to me */
|
* repeating and it seems this can give it to me */
|
||||||
const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0;
|
const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0;
|
||||||
|
|
||||||
|
fn err(argv0: &String, e: std::io::Error, code: Option<u8>) -> ExitCode {
|
||||||
|
eprintln!("{}: {}", argv0, e.strerror());
|
||||||
|
ExitCode::from(code.unwrap_or(1 /* unknown error */))
|
||||||
|
}
|
||||||
|
|
||||||
fn eval(
|
fn eval(
|
||||||
input: &str,
|
input: &str,
|
||||||
initial_stack: VecDeque<f64>,
|
initial_stack: VecDeque<f64>,
|
||||||
@ -153,12 +158,12 @@ fn eval(
|
|||||||
Invalid(i) => {
|
Invalid(i) => {
|
||||||
return Err(EvaluationError {
|
return Err(EvaluationError {
|
||||||
message: format!("{}: Invalid token", i),
|
message: format!("{}: Invalid token", i),
|
||||||
code: EX_DATAERR,
|
code: ExitCode::from(EX_DATAERR),
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
op => {
|
op => {
|
||||||
ops.push_back(op.clone());
|
ops.push_back(op.clone());
|
||||||
oper = true;
|
oper = true; /* this is an operation */
|
||||||
|
|
||||||
let vals = (
|
let vals = (
|
||||||
stack.pop_back(),
|
stack.pop_back(),
|
||||||