aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2023-02-09 11:37:29 -0330
committerSam Anthony <sam@samanthony.xyz>2023-02-09 11:37:29 -0330
commitaa9eda1e6758be478a9dbe878d8464cb682bc362 (patch)
treee734f32a226c97bd52e633b13cb0cc632ed5095f
parent15cc41d0b11ba91758211376b953a30b84376d25 (diff)
downloadpfc-aa9eda1e6758be478a9dbe878d8464cb682bc362.zip
Function enum
-rw-r--r--src/input.rs46
-rw-r--r--src/lib.rs29
2 files changed, 43 insertions, 32 deletions
diff --git a/src/input.rs b/src/input.rs
index 1dde8d5..ac5c5c8 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -1,6 +1,6 @@
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
-use crate::{Calculator, Operator, Signal};
+use crate::{Calculator, Function, Operator, Signal};
impl Calculator {
pub fn handle_input(&mut self, key: KeyEvent) -> Signal {
@@ -27,7 +27,16 @@ impl Calculator {
self.input_buffer = String::new();
}
KeyCode::Char(c) => self.push_to_buffer(c),
- KeyCode::Enter => self.push_buffer_to_stack(),
+ KeyCode::Enter if self.input_buffer.len() > 0 => {
+ if let Ok(func) = Function::parse(&self.input_buffer) {
+ if let Some(f) = self.stack.pop() {
+ self.stack.push(func.func()(f));
+ }
+ } else {
+ self.stack.push(self.input_buffer.parse::<f64>().unwrap());
+ }
+ self.input_buffer = String::new();
+ }
KeyCode::Backspace => {
self.input_buffer.pop();
}
@@ -45,42 +54,17 @@ impl Calculator {
}
self.input_buffer.push(c);
} else if let Ok(op) = Operator::parse(c) {
- self.push_buffer_to_stack();
- self.perform_operation(op);
+ self.stack.push(self.input_buffer.parse::<f64>().unwrap());
+ self.input_buffer = String::new();
+ self.op(op);
} else {
self.input_buffer.push(c);
}
}
- fn push_buffer_to_stack(&mut self) {
- match self.input_buffer.as_str() {
- "sin" => {
- if let Some(f) = self.stack.last_mut() {
- *f = f.sin();
- }
- }
- "cos" => {
- if let Some(f) = self.stack.last_mut() {
- *f = f.cos();
- }
- }
- "tan" => {
- if let Some(f) = self.stack.last_mut() {
- *f = f.tan();
- }
- }
- _ => {
- if self.input_buffer.len() > 0 {
- self.stack.push(self.input_buffer.parse::<f64>().unwrap());
- }
- }
- }
- self.input_buffer = String::new();
- }
-
fn swap(&mut self) {
if let Some(f) = self.stack.pop() {
- self.push_buffer_to_stack();
+ self.stack.push(self.input_buffer.parse::<f64>().unwrap());
self.input_buffer = format!("{}", f);
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 951a128..a7186e6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -8,7 +8,7 @@ pub struct Calculator {
}
impl Calculator {
- fn perform_operation(&mut self, op: Operator) {
+ fn op(&mut self, op: Operator) {
let rhs = match self.stack.pop() {
Some(f) => f,
None => {
@@ -54,6 +54,33 @@ impl Operator {
pub struct ParseOperatorError(char);
+enum Function {
+ Sin,
+ Cos,
+ Tan,
+}
+
+impl Function {
+ fn parse(s: &str) -> Result<Self, ParseFunctionError> {
+ match s {
+ "sin" => Ok(Self::Sin),
+ "cos" => Ok(Self::Cos),
+ "tan" => Ok(Self::Tan),
+ _ => Err(ParseFunctionError(s.to_string())),
+ }
+ }
+
+ fn func(&self) -> impl Fn(f64) -> f64 {
+ match self {
+ Self::Sin => |f: f64| f.sin(),
+ Self::Cos => |f: f64| f.cos(),
+ Self::Tan => |f: f64| f.tan(),
+ }
+ }
+}
+
+struct ParseFunctionError(String);
+
pub enum Signal {
None,
Exit,