feat(cli): set exit code on error

This commit is contained in:
Romain Paquet 2025-11-05 20:23:17 +01:00
parent f5f4da31e5
commit f0cef31b34

View file

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