From fb2ef1662f20864c5e2f8ec33774e618e1d653da Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Wed, 29 Jan 2025 19:38:01 +0000 Subject: [PATCH] refactor: move branch processing to a separate function --- src/main.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6b57935..f50facc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,14 @@ fn retrieve(memory: &mut HashMap, ops: Vec) -> i64 { } } +fn branch(labels: &HashMap, ops: Vec) -> usize { + if ops.len() >= 3 { + return *labels.get(&ops[2]).unwrap(); + } else { + return *labels.get(&ops[1]).unwrap(); + } +} + fn process( line: String, accumulator: &mut i64, @@ -58,28 +66,16 @@ fn process( } } "BRA" => { - if ops.len() >= 3 { - return *labels.get(&ops[2]).unwrap(); - } else { - return *labels.get(&ops[1]).unwrap(); - } + return branch(labels, ops); } "BRP" => { if *accumulator >= 0 { - if ops.len() >= 3 { - return *labels.get(&ops[2]).unwrap(); - } else { - return *labels.get(&ops[1]).unwrap(); - } + return branch(labels, ops); } } "BRZ" => { if *accumulator == 0 { - if ops.len() >= 3 { - return *labels.get(&ops[2]).unwrap(); - } else { - return *labels.get(&ops[1]).unwrap(); - } + return branch(labels, ops); } } "HLT" => std::process::exit(0),