diff --git a/examples/simple.rs b/examples/simple.rs index cda05c75..3549cb6f 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,5 +1,4 @@ -use std::{error::Error, process::exit}; - +use std::error::Error; use Ygen::prelude::*; pub fn main() -> Result<(), Box> { @@ -13,17 +12,11 @@ pub fn main() -> Result<(), Box> { let entry = func.addBlock("entry"); builder.positionAtEnd(entry); - let val = builder.BuildAdd(Type::i32(5), Type::i32(5))?; +let val = builder.BuildAdd(Type::i32(5), Type::i32(5))?; builder.BuildRet( val ); - match module.verify() { - Ok(_) => {}, - Err(e) => { - println!("{}", e); - exit(0) - }, - }; + module.verify().print(); println!("{}", module.dumpColored() diff --git a/src/Support/mod.rs b/src/Support/mod.rs index 6b65bbf3..890171f0 100644 --- a/src/Support/mod.rs +++ b/src/Support/mod.rs @@ -4,8 +4,32 @@ mod pad; mod srcmngr; mod tokmngr; +use std::process::exit; + pub use color::{Colorize, ColorEncoder}; pub use cli::Cli; pub use pad::Pad; pub use srcmngr::SrcMngr; -pub use tokmngr::TokenMgr; \ No newline at end of file +pub use tokmngr::TokenMgr; + +use crate::IR::VerifyError; + +/// Prints the error and exits the process +/// If no error occured this function just returns +pub trait PrintErrorAndExit { + /// Prints the error and exits the process + /// If no error occured this function just returns + fn print(&self); +} + +impl PrintErrorAndExit for Result<(), VerifyError> { + fn print(&self) { + match self { + Ok(_) => {}, + Err(e) => { + println!("{}", e); + exit(-1) + } + } + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 7c234628..90ac257c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,7 @@ pub mod prelude { pub use crate::IR::FunctionType; pub use crate::IR::TypeMetadata; pub use crate::Target::Triple; + pub use crate::Support::PrintErrorAndExit; pub use crate::IR::ir::*; }