Add more interesting compilation error messages

This commit is contained in:
Sofia 2020-06-22 22:29:27 +03:00
parent 267d3aabf0
commit 0f45350bec
3 changed files with 23 additions and 3 deletions

View File

@ -1,2 +1 @@
let otus = "gotus";
let dotus = otus;
let otus = "dotus";

View File

@ -75,3 +75,24 @@ impl CompilerError {
}
}
}
impl Display for CompilerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let text = match self {
CompilerError::Fatal => "Fatal error".to_string(),
CompilerError::VariableExists(pos, name) => {
format!("Variable '{}' already exists, re-assign at {}", pos, name)
}
CompilerError::VariableNotExists(pos, name) => {
format!("Variable '{}' does not exist, at {}", pos, name)
}
CompilerError::InvalidScopeExit(pos) => {
format!("Attempted to escape a scope invalidly at {}", pos)
}
CompilerError::LetFailed(pos, error) => {
format!("Let statement failed at {}:\n {}", pos, error)
}
CompilerError::CanNotAssignVoidType => format!("Can not assign void type to variable"),
};
write!(f, "{}", text)
}
}

View File

@ -20,7 +20,7 @@ fn main() {
dbg!(&parsed);
let compiled = Compiler::from(parsed).compile();
if let Err(error) = compiled {
eprintln!("Compilation error: {:#?}", error);
eprintln!("Compilation error: {}", error);
std::process::exit(1);
}
dbg!(compiled);