From 99a8712464793c2120c2bf880e87fbbab65475ad Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sun, 12 Feb 2023 13:25:00 -0330 Subject: push constant in buffer to stack before performing operation --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/input.rs | 10 ++++------ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5811aac..320bebe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,7 +113,7 @@ dependencies = [ [[package]] name = "pfc" -version = "0.6.1" +version = "0.6.2" dependencies = [ "crossterm", "tui", diff --git a/Cargo.toml b/Cargo.toml index fbc900e..4a9631e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pfc" -version = "0.6.1" +version = "0.6.2" edition = "2021" [dependencies] diff --git a/src/input.rs b/src/input.rs index 462e43c..f1431c5 100644 --- a/src/input.rs +++ b/src/input.rs @@ -46,8 +46,6 @@ impl Calculator { return Signal::None; } - // Push a character into the input buffer. If the character is an operator, - // the particular operation is performed. fn push_to_buffer(&mut self, c: char) { if c == '.' && !self.input_buffer.contains('.') { if self.input_buffer.len() == 0 { @@ -55,8 +53,10 @@ impl Calculator { } self.input_buffer.push(c); } else if let Ok(op) = Operator::parse(c) { - if self.input_buffer.len() > 0 { - self.stack.push(self.input_buffer.parse::().unwrap()); + if let Ok(c) = Constant::parse(&self.input_buffer) { + self.stack.push(c.value()); + } else if let Ok(f) = self.input_buffer.parse::() { + self.stack.push(f); } self.input_buffer = String::new(); self.perform_operation(op); @@ -65,8 +65,6 @@ impl Calculator { } } - // Swap the bottom of the stack and the input buffer. If the input buffer - // is empty, this simply pops from the stack into the input buffer. fn swap(&mut self) { if let Some(st) = self.stack.pop() { if let Ok(bu) = self.input_buffer.parse::() { -- cgit v1.2.3