Skip to content

Commit

Permalink
[ERRORS] adding a function to print verification errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Jul 10, 2024
1 parent a468859 commit b5a4956
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
13 changes: 3 additions & 10 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{error::Error, process::exit};

use std::error::Error;
use Ygen::prelude::*;

pub fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -13,17 +12,11 @@ pub fn main() -> Result<(), Box<dyn Error>> {
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()
Expand Down
26 changes: 25 additions & 1 deletion src/Support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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)
}
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
}

0 comments on commit b5a4956

Please sign in to comment.