muss/mps-interpreter/src/lang/error.rs

50 lines
1 KiB
Rust
Raw Normal View History

use std::fmt::{Debug, Display, Error, Formatter};
2021-12-29 17:12:58 +00:00
use super::PseudoOp;
use crate::tokens::MpsToken;
#[derive(Debug)]
pub struct SyntaxError {
pub line: usize,
pub token: MpsToken,
}
impl Display for SyntaxError {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(
f,
"SyntaxError (line {}): Unexpected {}",
&self.line, &self.token
)
}
}
impl MpsLanguageError for SyntaxError {
fn set_line(&mut self, line: usize) {
self.line = line
}
}
#[derive(Debug)]
pub struct RuntimeError {
pub line: usize,
2021-12-29 17:12:58 +00:00
pub op: PseudoOp,
pub msg: String,
}
impl Display for RuntimeError {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{} (line {}): {}", &self.msg, &self.line, &self.op)
}
}
impl MpsLanguageError for RuntimeError {
fn set_line(&mut self, line: usize) {
self.line = line
}
}
pub trait MpsLanguageError: Display + Debug {
fn set_line(&mut self, line: usize);
}