From 0f45350becfc48318ba5f327651c0cbd18f9a216 Mon Sep 17 00:00:00 2001 From: Teascade Date: Mon, 22 Jun 2020 22:29:27 +0300 Subject: [PATCH] Add more interesting compilation error messages --- reid_src/test.reid | 3 +-- src/errors.rs | 21 +++++++++++++++++++++ src/main.rs | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/reid_src/test.reid b/reid_src/test.reid index 9a63f69..ab42029 100644 --- a/reid_src/test.reid +++ b/reid_src/test.reid @@ -1,2 +1 @@ -let otus = "gotus"; -let dotus = otus; \ No newline at end of file +let otus = "dotus"; \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs index cdb8808..e691467 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -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) + } +} diff --git a/src/main.rs b/src/main.rs index 7c430cb..67eb5dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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);