improve error formatting

This commit is contained in:
Rowan 2025-01-05 11:27:22 -06:00
parent 7010268d94
commit 6db6fab939

View file

@ -131,7 +131,10 @@ impl From<TryFromIntError> 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("<halt>");
match Program::from_program(Tape::new(program)) {
Ok(mut machine) => machine.execute().expect("<halt>"),
Err(e) => println!("{e}"),
};
//let mut machine = Program::from_program(Tape::new(program)).expect("failed to load program");
//machine.execute().expect("<halt>");
}
#[cfg(test)]