summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-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