Continue work

This commit is contained in:
Austen Adler 2021-04-28 21:07:45 -04:00
parent fbedb044c5
commit a8f574bd80
3 changed files with 37 additions and 50 deletions

View File

@ -146,9 +146,7 @@ impl<'a> Calculator<'a> {
self.state = CalculatorState::WaitingForRegister(RegisterState::Save);
Ok(())
}
_ => {
return Err(CalculatorError::NoSuchOperator(c));
}
_ => Err(CalculatorError::NoSuchOperator(c)),
},
CalculatorState::WaitingForConstant => {
let f = self
@ -176,9 +174,9 @@ impl<'a> Calculator<'a> {
// Record that we are running macro c
self.active_macros.insert(c);
for c in value.chars() {
self.take_input(c).or_else(|e| {
self.take_input(c).map_err(|e| {
self.cancel();
Err(e)
e
})?;
}
// Macro c should be over now
@ -207,8 +205,6 @@ impl<'a> Calculator<'a> {
Ok(())
}
}
//}
//Ok(())
}
pub fn cancel(&mut self) {
@ -220,7 +216,7 @@ impl<'a> Calculator<'a> {
Ok(())
}
pub fn edit(&mut self) -> CalculatorResult<()> {
if self.l.len() > 0 {
if !self.l.is_empty() {
return Ok(());
}
@ -275,15 +271,15 @@ impl<'a> Calculator<'a> {
self.registers.iter()
}
pub fn push_constant(&mut self, key: char) -> CalculatorResult<()> {
match self.constants.get(&key) {
Some(CalculatorConstant { value, .. }) => {
let value = *value;
self.push(value)
}
None => Err(CalculatorError::NoSuchConstant(key)),
}
}
// pub fn push_constant(&mut self, key: char) -> CalculatorResult<()> {
// match self.constants.get(&key) {
// Some(CalculatorConstant { value, .. }) => {
// let value = *value;
// self.push(value)
// }
// None => Err(CalculatorError::NoSuchConstant(key)),
// }
// }
// pub fn push_register(&mut self, key: char) -> CalculatorResult<()> {
// match self.registers.get(&key) {
// Some(f) => {
@ -298,12 +294,12 @@ impl<'a> Calculator<'a> {
// self.registers.insert(key, f);
// Ok(())
// }
pub fn get_macro(&mut self, key: char) -> Result<&CalculatorMacro<'a>, CalculatorError> {
match self.macros.get(&key) {
Some(m) => Ok(m),
None => Err(CalculatorError::NoSuchMacro(key)),
}
}
// pub fn get_macro(&mut self, key: char) -> Result<&CalculatorMacro<'a>, CalculatorError> {
// match self.macros.get(&key) {
// Some(m) => Ok(m),
// None => Err(CalculatorError::NoSuchMacro(key)),
// }
// }
pub fn flush_l(&mut self) -> CalculatorResult<bool> {
if self.l.is_empty() {
@ -315,13 +311,13 @@ impl<'a> Calculator<'a> {
Ok(true)
}
}
pub fn push(&mut self, f: f64) -> CalculatorResult<()> {
fn push(&mut self, f: f64) -> CalculatorResult<()> {
self.direct_state_change(CalculatorStateChange {
pop: OpArgs::None,
push: OpArgs::Unary(f),
})
}
pub fn pop(&mut self) -> CalculatorResult<f64> {
fn pop(&mut self) -> CalculatorResult<f64> {
let f = self.checked_get(0)?;
self.direct_state_change(CalculatorStateChange {
pop: OpArgs::Unary(f),
@ -332,7 +328,6 @@ impl<'a> Calculator<'a> {
pub fn get_stack(&self) -> &VecDeque<f64> {
&self.stack
}
//TODO: VecDeque could have other types
pub fn op(&mut self, op: CalculatorOperation) -> CalculatorResult<()> {
// Dup is special -- don't actually run it if l needs to be flushed
if self.flush_l()? {
@ -384,7 +379,7 @@ impl<'a> Calculator<'a> {
.ok_or_else(|| CalculatorError::EmptyHistory(String::from("redo")))?;
return self.apply_state_change(s, true);
}
// TODO: This should not happen -- need to pull all macro/register accesses into their own enum
// Macros are a no-op as an operator
CalculatorOperation::Macro(_) => {
return Ok(());
}
@ -466,13 +461,15 @@ impl<'a> Calculator<'a> {
self.stack.pop_front();
}
OpArgs::Binary([a, 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_eq(0, *a)?;
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();
}
@ -513,9 +510,8 @@ impl<'a> Calculator<'a> {
}
fn checked_get(&self, idx: usize) -> CalculatorResult<f64> {
match self.stack.get(idx) {
None => Err(CalculatorError::NotEnoughStackEntries),
Some(r) => Ok(*r),
}
self.stack
.get(idx)
.ok_or(Err(CalculatorError::NotEnoughStackEntries))
}
}

View File

@ -182,7 +182,7 @@ fn main() -> Result<(), Box<dyn Error>> {
};
draw_clippy_rect(
ClippyRectangle {
title: title,
title,
msg: app
.calculator
.get_registers_iter()
@ -285,7 +285,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult<bool
_ => {}
},
}
return Ok(false);
Ok(false)
}
struct ClippyRectangle<'a> {

View File

@ -87,16 +87,6 @@ impl Events {
}
}
pub fn fill_event_buf(&self, mac: &str) {
for c in mac.chars() {
// TODO: Catch errors
if let Err(_) = self.tx.send(Event::Input(Key::Char(c))) {
//return;
}
}
//self.tx.send(Event::MacroEnd);
}
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
self.rx.recv()
}
@ -104,6 +94,7 @@ impl Events {
pub fn try_next(&self) -> Result<Event<Key>, mpsc::TryRecvError> {
self.rx.try_recv()
}
pub fn try_iter(&self) -> TryIter<Event<Key>> {
self.rx.try_iter()
}