Extract address parsing code to dedicated function

This commit is contained in:
pjht 2022-10-12 15:41:43 -05:00
parent bd7670cf8b
commit 0d78a8399d

View File

@ -315,19 +315,9 @@ fn main() -> Result<(), ReplError> {
|args, state| {
let mut out = String::new();
while !state.cpu.stopped {
let stop_addr = args.get_one::<String>("stop_addr");
let stop_addr = stop_addr
.map(|s| {
parse::<u32>(s).or_else(|_| {
state
.symbols
.iter()
.find(|sym| &sym.name == s)
.map(|sym| sym.value as u32)
.ok_or(Error::Misc("No such symbol"))
})
})
let stop_addr = args
.get_one::<String>("stop_addr")
.map(|s| parse_addr(s, &state.symbols))
.transpose()?;
if stop_addr.map(|a| state.cpu.pc() == a).unwrap_or(false) {
break;
@ -474,3 +464,13 @@ fn disas_fmt(cpu: &mut M68K, addr: u32) -> (String, Result<u32, DisassemblyError
Err(e) => (format!("0x{:x}: {}\n", addr, e), Err(e)),
}
}
fn parse_addr(addr: &str, symbols: &[Symbol]) -> Result<u32, Error> {
parse::<u32>(addr).or_else(|_| {
symbols
.iter()
.find(|sym| sym.name == addr)
.map(|sym| sym.value as u32)
.ok_or(Error::Misc("No such symbol"))
})
}