From 407eed923fa95c544a05e6b7b44a72c44c320d3b Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sun, 7 Apr 2024 01:14:08 -0700 Subject: refactor register commands so string-names and character-names use the same underlying implementations Change-Id: I25dcd19103806384ef107803c5182edeb1de2257 --- src/main.rs | 98 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 45 insertions(+), 53 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2e6e9ea..f392572 100644 --- a/src/main.rs +++ b/src/main.rs @@ -527,39 +527,13 @@ fn make_register_string_readtable() -> ReadTable { } }); - result.set_action(']', move |c, state| { + result.set_action(']', move |_, state| { state.mode = Mode::CommandChar; - match state.reg_command.as_str() { - "l" => { - if state.registers.contains_key(&state.wip_str) { - state.stack.push(state.registers[&state.wip_str].clone()); - } - - state.wip_str.clear(); - - Ok(()) - } - - "s" => { - if state.stack.is_empty() { - return Err(err_msg(format!( - "Data underflow attempting to store to register {}", c))); - } - state.registers.insert(state.wip_str.clone(), - state.stack.pop().unwrap()); - - state.wip_str.clear(); - - Ok(()) - } - - _ => { + let name = state.wip_str.clone(); + state.wip_str.clear(); - Err(err_msg(format!( - "Unsupported register command {}", state.reg_command))) - } - } + dispatch_register_command(&name, state) }); result @@ -570,30 +544,9 @@ fn make_register_character_readtable() -> ReadTable { let mut result = ReadTable::new(move |c, state| { state.mode = Mode::CommandChar; - match state.reg_command.as_str() { - "s" => { - if state.stack.is_empty() { - return Err(err_msg(format!( - "Data underflow attempting to store to register {}", c))); - } - state.registers.insert(String::from(c), state.stack.pop().unwrap()); - - Ok(()) - } - - "l" => { - if state.registers.contains_key(&String::from(c)) { - state.stack.push(state.registers[&String::from(c)].clone()); - } - - Ok(()) - } + let name = String::from(c); - _ => { - Err(err_msg(format!( - "Unsupported register command {}", state.reg_command))) - } - } + dispatch_register_command(&name, state) }); result.set_action('[', move |_, state| { @@ -605,6 +558,45 @@ fn make_register_character_readtable() -> ReadTable { } +fn dispatch_register_command(name: &str, state: &mut RPState) + -> Result<(), Exit> +{ + match state.reg_command.as_str() { + "l" => load_from_register(name, state), + "s" => store_to_register(name, state), + + _ => { + Err(err_msg(format!( + "Unsupported register command {}", state.reg_command))) + } + } +} + + +fn store_to_register(name: &str, state: &mut RPState) -> Result<(), Exit> { + if !state.stack.is_empty() { + state.registers.insert(name.into(), state.stack.pop().unwrap()); + + Ok(()) + } else { + Err(err_msg(format!( + "Data underflow attempting to store to register {}", name))) + } +} + + +fn load_from_register(name: &str, state: &mut RPState) -> Result<(), Exit> { + if state.registers.contains_key(name) { + state.stack.push(state.registers[name].clone()); + + Ok(()) + } else { + Err(err_msg(format!( + "Register {} is empty and was expected not to be", name))) + } +} + + fn eval(input: &str, state: &mut RPState) -> Result<(), Exit> { for (_cpos, c) in input.char_indices() { // TODO eventually this slightly weird logic will go away because -- cgit 1.4.1