refactor: move branch processing to a separate function
This commit is contained in:
parent
92dfd4341c
commit
fb2ef1662f
1 changed files with 11 additions and 15 deletions
26
src/main.rs
26
src/main.rs
|
@ -10,6 +10,14 @@ fn retrieve(memory: &mut HashMap<String, i64>, ops: Vec<String>) -> i64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn branch(labels: &HashMap<String, usize>, ops: Vec<String>) -> usize {
|
||||||
|
if ops.len() >= 3 {
|
||||||
|
return *labels.get(&ops[2]).unwrap();
|
||||||
|
} else {
|
||||||
|
return *labels.get(&ops[1]).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn process(
|
fn process(
|
||||||
line: String,
|
line: String,
|
||||||
accumulator: &mut i64,
|
accumulator: &mut i64,
|
||||||
|
@ -58,28 +66,16 @@ fn process(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"BRA" => {
|
"BRA" => {
|
||||||
if ops.len() >= 3 {
|
return branch(labels, ops);
|
||||||
return *labels.get(&ops[2]).unwrap();
|
|
||||||
} else {
|
|
||||||
return *labels.get(&ops[1]).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
"BRP" => {
|
"BRP" => {
|
||||||
if *accumulator >= 0 {
|
if *accumulator >= 0 {
|
||||||
if ops.len() >= 3 {
|
return branch(labels, ops);
|
||||||
return *labels.get(&ops[2]).unwrap();
|
|
||||||
} else {
|
|
||||||
return *labels.get(&ops[1]).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"BRZ" => {
|
"BRZ" => {
|
||||||
if *accumulator == 0 {
|
if *accumulator == 0 {
|
||||||
if ops.len() >= 3 {
|
return branch(labels, ops);
|
||||||
return *labels.get(&ops[2]).unwrap();
|
|
||||||
} else {
|
|
||||||
return *labels.get(&ops[1]).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"HLT" => std::process::exit(0),
|
"HLT" => std::process::exit(0),
|
||||||
|
|
Loading…
Reference in a new issue