feat(cli): set exit code on error
This commit is contained in:
parent
f5f4da31e5
commit
f0cef31b34
1 changed files with 20 additions and 8 deletions
28
src/main.rs
28
src/main.rs
|
|
@ -70,9 +70,12 @@ fn parse(files: &[String]) -> Vec<Module> {
|
|||
.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 {
|
||||
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<Module>, 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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue