Code cleanup

This commit is contained in:
Austen Adler 2021-04-30 00:26:42 -04:00
parent 9bd0e72fbc
commit d81fb1f45d
2 changed files with 19 additions and 25 deletions

View File

@ -8,7 +8,7 @@ use constants::{
CalculatorState, RegisterState, CalculatorState, RegisterState,
}; };
use errors::{CalculatorError, CalculatorResult}; use errors::{CalculatorError, CalculatorResult};
use operations::{CalculatorOperation, MacroState}; use operations::{CalculatorOperation, CalculatorStateChange, MacroState, OpArgs};
use std::collections::{HashSet, VecDeque}; use std::collections::{HashSet, VecDeque};
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
@ -17,20 +17,6 @@ enum HistoryMode {
Macro, Macro,
} }
#[derive(PartialEq, Debug)]
enum OpArgs {
Macro(MacroState),
Unary(f64),
Binary([f64; 2]),
None,
}
#[derive(PartialEq, Debug)]
struct CalculatorStateChange {
pop: OpArgs,
push: OpArgs,
}
impl CalculatorStateChange { impl CalculatorStateChange {
pub fn is_noop(&self) -> bool { pub fn is_noop(&self) -> bool {
self.push == OpArgs::None && self.pop == OpArgs::None self.push == OpArgs::None && self.pop == OpArgs::None
@ -316,6 +302,9 @@ impl<'a> Calculator<'a> {
pub fn get_registers_iter(&self) -> CalculatorRegistersIter { pub fn get_registers_iter(&self) -> CalculatorRegistersIter {
self.registers.iter() self.registers.iter()
} }
pub fn get_stack(&self) -> &VecDeque<f64> {
&self.stack
}
pub fn flush_l(&mut self) -> CalculatorResult<bool> { pub fn flush_l(&mut self) -> CalculatorResult<bool> {
if self.l.is_empty() { if self.l.is_empty() {
@ -330,6 +319,7 @@ impl<'a> Calculator<'a> {
fn within_macro(&self) -> bool { fn within_macro(&self) -> bool {
!self.active_macros.is_empty() !self.active_macros.is_empty()
} }
fn push(&mut self, f: f64) -> CalculatorResult<()> { fn push(&mut self, f: f64) -> CalculatorResult<()> {
self.direct_state_change(CalculatorStateChange { self.direct_state_change(CalculatorStateChange {
pop: OpArgs::None, pop: OpArgs::None,
@ -344,9 +334,6 @@ impl<'a> Calculator<'a> {
})?; })?;
Ok(f) Ok(f)
} }
pub fn get_stack(&self) -> &VecDeque<f64> {
&self.stack
}
pub fn op(&mut self, op: CalculatorOperation) -> CalculatorResult<()> { pub fn op(&mut self, op: CalculatorOperation) -> CalculatorResult<()> {
// Dup is special -- don't actually run it if l needs to be flushed // Dup is special -- don't actually run it if l needs to be flushed
if self.flush_l()? { if self.flush_l()? {
@ -529,13 +516,6 @@ impl<'a> Calculator<'a> {
OpArgs::Binary([a, b]) => { OpArgs::Binary([a, b]) => {
self.stack_eq(0, *a)?; self.stack_eq(0, *a)?;
self.stack_eq(1, *b)?; self.stack_eq(1, *b)?;
// if forward {
// self.stack_eq(0, *a)?;
// self.stack_eq(1, *b)?;
// } else {
// self.stack_eq(0, *a)?;
// self.stack_eq(1, *b)?;
// }
self.stack.pop_front(); self.stack.pop_front();
self.stack.pop_front(); self.stack.pop_front();
} }

View File

@ -35,3 +35,17 @@ pub enum CalculatorOperation {
E, E,
Macro(MacroState), Macro(MacroState),
} }
#[derive(PartialEq, Debug)]
pub enum OpArgs {
Macro(MacroState),
Unary(f64),
Binary([f64; 2]),
None,
}
#[derive(PartialEq, Debug)]
pub struct CalculatorStateChange {
pub pop: OpArgs,
pub push: OpArgs,
}