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); self.state = CalculatorState::WaitingForRegister(RegisterState::Save);
Ok(()) Ok(())
} }
_ => { _ => Err(CalculatorError::NoSuchOperator(c)),
return Err(CalculatorError::NoSuchOperator(c));
}
}, },
CalculatorState::WaitingForConstant => { CalculatorState::WaitingForConstant => {
let f = self let f = self
@ -176,9 +174,9 @@ impl<'a> Calculator<'a> {
// Record that we are running macro c // Record that we are running macro c
self.active_macros.insert(c); self.active_macros.insert(c);
for c in value.chars() { for c in value.chars() {
self.take_input(c).or_else(|e| { self.take_input(c).map_err(|e| {
self.cancel(); self.cancel();
Err(e) e
})?; })?;
} }
// Macro c should be over now // Macro c should be over now
@ -207,8 +205,6 @@ impl<'a> Calculator<'a> {
Ok(()) Ok(())
} }
} }
//}
//Ok(())
} }
pub fn cancel(&mut self) { pub fn cancel(&mut self) {
@ -220,7 +216,7 @@ impl<'a> Calculator<'a> {
Ok(()) Ok(())
} }
pub fn edit(&mut self) -> CalculatorResult<()> { pub fn edit(&mut self) -> CalculatorResult<()> {
if self.l.len() > 0 { if !self.l.is_empty() {
return Ok(()); return Ok(());
} }
@ -275,15 +271,15 @@ impl<'a> Calculator<'a> {
self.registers.iter() self.registers.iter()
} }
pub fn push_constant(&mut self, key: char) -> CalculatorResult<()> { // pub fn push_constant(&mut self, key: char) -> CalculatorResult<()> {
match self.constants.get(&key) { // match self.constants.get(&key) {
Some(CalculatorConstant { value, .. }) => { // Some(CalculatorConstant { value, .. }) => {
let value = *value; // let value = *value;
self.push(value) // self.push(value)
} // }
None => Err(CalculatorError::NoSuchConstant(key)), // None => Err(CalculatorError::NoSuchConstant(key)),
} // }
} // }
// pub fn push_register(&mut self, key: char) -> CalculatorResult<()> { // pub fn push_register(&mut self, key: char) -> CalculatorResult<()> {
// match self.registers.get(&key) { // match self.registers.get(&key) {
// Some(f) => { // Some(f) => {
@ -298,12 +294,12 @@ impl<'a> Calculator<'a> {
// self.registers.insert(key, f); // self.registers.insert(key, f);
// Ok(()) // Ok(())
// } // }
pub fn get_macro(&mut self, key: char) -> Result<&CalculatorMacro<'a>, CalculatorError> { // pub fn get_macro(&mut self, key: char) -> Result<&CalculatorMacro<'a>, CalculatorError> {
match self.macros.get(&key) { // match self.macros.get(&key) {
Some(m) => Ok(m), // Some(m) => Ok(m),
None => Err(CalculatorError::NoSuchMacro(key)), // None => Err(CalculatorError::NoSuchMacro(key)),
} // }
} // }
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() {
@ -315,13 +311,13 @@ impl<'a> Calculator<'a> {
Ok(true) Ok(true)
} }
} }
pub 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,
push: OpArgs::Unary(f), push: OpArgs::Unary(f),
}) })
} }
pub fn pop(&mut self) -> CalculatorResult<f64> { fn pop(&mut self) -> CalculatorResult<f64> {
let f = self.checked_get(0)?; let f = self.checked_get(0)?;
self.direct_state_change(CalculatorStateChange { self.direct_state_change(CalculatorStateChange {
pop: OpArgs::Unary(f), pop: OpArgs::Unary(f),
@ -332,7 +328,6 @@ impl<'a> Calculator<'a> {
pub fn get_stack(&self) -> &VecDeque<f64> { pub fn get_stack(&self) -> &VecDeque<f64> {
&self.stack &self.stack
} }
//TODO: VecDeque could have other types
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()? {
@ -384,7 +379,7 @@ impl<'a> Calculator<'a> {
.ok_or_else(|| CalculatorError::EmptyHistory(String::from("redo")))?; .ok_or_else(|| CalculatorError::EmptyHistory(String::from("redo")))?;
return self.apply_state_change(s, true); 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(_) => { CalculatorOperation::Macro(_) => {
return Ok(()); return Ok(());
} }
@ -466,13 +461,15 @@ impl<'a> Calculator<'a> {
self.stack.pop_front(); self.stack.pop_front();
} }
OpArgs::Binary([a, b]) => { OpArgs::Binary([a, b]) => {
if forward { self.stack_eq(0, *a)?;
self.stack_eq(0, *a)?; self.stack_eq(1, *b)?;
self.stack_eq(1, *b)?; // if forward {
} else { // self.stack_eq(0, *a)?;
self.stack_eq(0, *a)?; // self.stack_eq(1, *b)?;
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();
} }
@ -513,9 +510,8 @@ impl<'a> Calculator<'a> {
} }
fn checked_get(&self, idx: usize) -> CalculatorResult<f64> { fn checked_get(&self, idx: usize) -> CalculatorResult<f64> {
match self.stack.get(idx) { self.stack
None => Err(CalculatorError::NotEnoughStackEntries), .get(idx)
Some(r) => Ok(*r), .ok_or(Err(CalculatorError::NotEnoughStackEntries))
}
} }
} }

View File

@ -182,7 +182,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}; };
draw_clippy_rect( draw_clippy_rect(
ClippyRectangle { ClippyRectangle {
title: title, title,
msg: app msg: app
.calculator .calculator
.get_registers_iter() .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> { 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> { pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
self.rx.recv() self.rx.recv()
} }
@ -104,6 +94,7 @@ impl Events {
pub fn try_next(&self) -> Result<Event<Key>, mpsc::TryRecvError> { pub fn try_next(&self) -> Result<Event<Key>, mpsc::TryRecvError> {
self.rx.try_recv() self.rx.try_recv()
} }
pub fn try_iter(&self) -> TryIter<Event<Key>> { pub fn try_iter(&self) -> TryIter<Event<Key>> {
self.rx.try_iter() self.rx.try_iter()
} }