From 6db6fab93948135e7a2e14b9527cceaa5aa4bfff Mon Sep 17 00:00:00 2001 From: rowan Date: Sun, 5 Jan 2025 11:27:22 -0600 Subject: [PATCH] improve error formatting --- src/main.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index e2baa8e..679a660 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,7 +131,10 @@ impl From for ProgramError { impl fmt::Display for ProgramError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{} ({:?})", self.kind, self.source) + match &self.source { + Some(source) => write!(f, "{} ({source})", self.kind), + None => write!(f, "{}", self.kind), + } } } @@ -162,8 +165,8 @@ impl Program { (b'a', false, true) => Err(SyntaxError::new(i, r#"unexpected "a". expected "w", "!" if continuing a word, or anything else to end it"#.to_string())), (b'w', true, true) => Err(SyntaxError::new(i, r#"unexpected "w". expected "a""#.to_string())), (b'!', true, _) | (b'!', _, false)=> Err(SyntaxError::new(i, r#"unexpected "!": may only appear at the end of a word"#.to_string())), - (v, _, false) => Err(SyntaxError::new(i, format!("unexpected {v}: whitespace or any non-word character should follow"))), - (v, _, _) => Err(SyntaxError::new(i, format!("unexpected {v}"))), + (v, _, false) => Err(SyntaxError::new(i, format!("unexpected {}: whitespace or any non-word character should follow", v as char))), + (v, _, _) => Err(SyntaxError::new(i, format!("unexpected {}", v as char))), } }); @@ -327,8 +330,12 @@ fn main() { Err(_) => &mut args[1].as_bytes(), }; - let mut machine = Program::from_program(Tape::new(program)).expect("failed to load program"); - machine.execute().expect(""); + match Program::from_program(Tape::new(program)) { + Ok(mut machine) => machine.execute().expect(""), + Err(e) => println!("{e}"), + }; + //let mut machine = Program::from_program(Tape::new(program)).expect("failed to load program"); + //machine.execute().expect(""); } #[cfg(test)]