summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2024-04-07 01:14:08 -0700
committerIrene Knapp <ireneista@irenes.space>2024-04-07 01:14:08 -0700
commit407eed923fa95c544a05e6b7b44a72c44c320d3b (patch)
treef499f21c1e3e6c63af1a0aaf3a738f13ffe6f743 /src
parentdb4a9cb87970c6cf1b13175b1fe6b92dc29097bd (diff)
refactor register commands so string-names and character-names use the same underlying implementations
Change-Id: I25dcd19103806384ef107803c5182edeb1de2257
Diffstat (limited to 'src')
-rw-r--r--src/main.rs98
1 files 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