forked from bonsai/harakit
rpn(1): fixed Invalid error handling
This commit is contained in:
parent
a30152d783
commit
228a0111c7
16
src/rpn.rs
16
src/rpn.rs
@ -56,7 +56,7 @@ extern crate sysexits;
|
|||||||
|
|
||||||
use sysexits::EX_DATAERR;
|
use sysexits::EX_DATAERR;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
|
#[derive(Clone, 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 {
|
||||||
Add,
|
Add,
|
||||||
@ -67,7 +67,7 @@ enum CalcType {
|
|||||||
Floor,
|
Floor,
|
||||||
Modulo,
|
Modulo,
|
||||||
Val(f64),
|
Val(f64),
|
||||||
Invalid,
|
Invalid(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for CalcType {
|
impl From<&str> for CalcType {
|
||||||
@ -83,7 +83,7 @@ impl From<&str> for CalcType {
|
|||||||
_ => {
|
_ => {
|
||||||
match value.parse::<f64>() {
|
match value.parse::<f64>() {
|
||||||
Ok(x) => Val(x),
|
Ok(x) => Val(x),
|
||||||
Err(_) => Invalid,
|
Err(_) => Invalid(value.to_owned()),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -104,7 +104,9 @@ impl Display for CalcType {
|
|||||||
Val(x) => {
|
Val(x) => {
|
||||||
y = x.to_string(); &y
|
y = x.to_string(); &y
|
||||||
},
|
},
|
||||||
Invalid => "",
|
Invalid(i) => {
|
||||||
|
y = i.to_string(); &y
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -146,13 +148,13 @@ fn eval(
|
|||||||
while let Some(n) = toks.pop_back() {
|
while let Some(n) = toks.pop_back() {
|
||||||
match n {
|
match n {
|
||||||
Val(v) => stack.push_back(v),
|
Val(v) => stack.push_back(v),
|
||||||
Invalid => {
|
Invalid(i) => {
|
||||||
return Err(EvaluationError {
|
return Err(EvaluationError {
|
||||||
message: format!("{}: Invalid token", n)
|
message: format!("{}: Invalid token", i)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
op => {
|
op => {
|
||||||
ops.push_back(op);
|
ops.push_back(op.clone());
|
||||||
oper = true;
|
oper = true;
|
||||||
|
|
||||||
let vals = (
|
let vals = (
|
||||||
|
Loading…
Reference in New Issue
Block a user