diff --git a/src/main.rs b/src/main.rs index e112217..d5d6cd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,9 +70,12 @@ fn parse(files: &[String]) -> Vec { .collect() } -fn check(modules: &mut Vec, source_cache: &mut SourceCache) { +fn check(modules: &mut Vec, source_cache: &mut SourceCache) -> usize { + let mut failed_count = 0; + for module in modules { if let Err(errors) = module.type_check() { + failed_count += 1; for error in errors { error .to_report(module) @@ -82,9 +85,11 @@ fn check(modules: &mut Vec, source_cache: &mut SourceCache) { } } } + + failed_count } -fn main() { +fn main() -> anyhow::Result<()> { let cli = Cli::parse(); match &cli.command { @@ -96,6 +101,7 @@ fn main() { } } println!("Parsing OK"); + Ok(()) } Commands::Check { files, dump_ast } => { @@ -104,18 +110,24 @@ fn main() { file_cache: ariadne::FileCache::default(), }; let mut modules = parse(files); - check(&mut modules, &mut source_cache); + let failed_count = check(&mut modules, &mut source_cache); if *dump_ast { for module in &modules { println!("{:#?}", &module); } } + + if failed_count > 0 { + anyhow::bail!("{}/{} files failed to compile", failed_count, files.len()); + } + + Ok(()) } Commands::Compile { files, dump_clir } | Commands::Run { files, dump_clir } => { if files.is_empty() { println!("No input files"); - return; + return Ok(()); } let mut source_cache = SourceCache { @@ -127,22 +139,22 @@ fn main() { jit.dump_clir = *dump_clir; for (id, file) in files.iter().enumerate() { + println!("Compiling {file}"); match jit.compile_file(file, id as u32, &mut source_cache) { - Err(e) => eprintln!("{}", e), + Err(e) => eprintln!("{e}"), Ok(code) => { - println!("Compiled {}", file); - if let Commands::Run { .. } = cli.command { let ret = unsafe { let code_fn: unsafe extern "sysv64" fn() -> i32 = std::mem::transmute(code); code_fn() }; - println!("Main returned {}", ret); + println!("Main returned {ret}"); } } } } + Ok(()) } } }