aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2023-02-09 15:51:31 -0330
committerSam Anthony <sam@samanthony.xyz>2023-02-09 15:51:31 -0330
commit3900e2a499df1d13339ac727fee01afd34ace3d8 (patch)
tree55480552651e0186f52e2673abbd5a6009f804c3
parent237685f6cae5f79cf84422617614aaa96699506c (diff)
downloadpfc-3900e2a499df1d13339ac727fee01afd34ace3d8.zip
fix bug: parsing empty buffer
-rw-r--r--src/input.rs8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/input.rs b/src/input.rs
index 21edef7..59bf5b9 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -53,7 +53,9 @@ impl Calculator {
}
self.input_buffer.push(c);
} else if let Ok(op) = Operator::parse(c) {
- self.stack.push(self.input_buffer.parse::<f64>().unwrap());
+ if self.input_buffer.len() > 0 {
+ self.stack.push(self.input_buffer.parse::<f64>().unwrap());
+ }
self.input_buffer = String::new();
self.perform_operation(op);
} else {
@@ -65,7 +67,9 @@ impl Calculator {
// is empty, this simply pops from the stack into the input buffer.
fn swap(&mut self) {
if let Some(f) = self.stack.pop() {
- self.stack.push(self.input_buffer.parse::<f64>().unwrap());
+ if self.input_buffer.len() > 0 {
+ self.stack.push(self.input_buffer.parse::<f64>().unwrap());
+ }
self.input_buffer = format!("{}", f);
}
}