From 1db0144eb7a6d70e466974b29279e85b4ed5a3ca Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 26 Apr 2020 18:24:01 +0200 Subject: [PATCH] Fix register rgd name --- src/bin/lsambler.rs | 22 ++++++++++++++++++---- src/registers.rs | 13 +++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/bin/lsambler.rs b/src/bin/lsambler.rs index 1b59ceb..6bc224a 100644 --- a/src/bin/lsambler.rs +++ b/src/bin/lsambler.rs @@ -28,13 +28,25 @@ struct Opts { fn main() -> io::Result<()> { let opts: Opts = Opts::from_args(); - let contents = read_to_string(opts.input_file)?; + let input_file_name = opts.input_file; + let contents = read_to_string(&input_file_name)?; let f = File::create(opts.output_file)?; let mut writer = BufWriter::new(f); + let mut line_number = 0; contents .lines() - .map(|line| get_token(line)) - .filter_map(|token| Some(token?.to_bytecode())) + .filter_map(|line| { + line_number += 1; + if let Some(token) = get_token(line) { + Some(token.to_bytecode()) + } else { + println!( + "Failed to parse instruction '{}' \n-> {}:{}", + line, &input_file_name, line_number + ); + None + } + }) .for_each(|code| { writer.write(&code).expect("Failed to write output."); }); @@ -43,8 +55,8 @@ fn main() -> io::Result<()> { Ok(()) } +/// Parses the line into a token fn get_token(line: &str) -> Option> { - println!("{}", line); let mut instr_parts = line.split_whitespace(); match instr_parts.next()? { @@ -85,6 +97,8 @@ fn get_token(line: &str) -> Option> { } } +/// Parses a value depending on if it starts with 0x (as a hex value) +/// or just is a plain base-10 number fn parse_value(value: &str) -> Result { if value.starts_with("0x") { let value = value.trim_start_matches("0x"); diff --git a/src/registers.rs b/src/registers.rs index eebce69..012290d 100644 --- a/src/registers.rs +++ b/src/registers.rs @@ -2,7 +2,7 @@ pub const RCS: u8 = 0x01; pub const RCR: u8 = 0x02; pub const RCG: u8 = 0x03; pub const RCB: u8 = 0x04; -pub const RCD: u8 = 0x05; +pub const RGD: u8 = 0x05; pub const RGP: u8 = 0x06; pub const RGI: u8 = 0x07; pub const RGO: u8 = 0x08; @@ -15,14 +15,19 @@ pub fn get_register_by_name(name: &str) -> Option { ("rcr", RCR), ("rcg", RCG), ("rcb", RCB), - ("rcd", RCD), + ("rgd", RGD), ("rgp", RGP), ("rgi", RGI), ("rgo", RGO), ("rgl", RGL), ] .iter() - .find(|(reg, bc)| *reg == name)?; + .find(|(reg, bc)| *reg == name); - Some((*item).1) + if let Some(item) = item { + Some((*item).1) + } else { + println!("Unknown register: {}", name); + None + } }