refactor typing and compilation

This commit is contained in:
Romain Paquet 2024-06-25 11:02:05 +02:00
parent e8deab19cc
commit ba838292f6
8 changed files with 175 additions and 131 deletions

View file

@ -42,6 +42,13 @@ enum Commands {
#[arg(long)]
dump_clir: bool,
},
Run {
/// Paths to the source files
files: Vec<String>,
#[arg(long)]
dump_clir: bool,
},
}
fn parse(files: &Vec<String>) -> Vec<Module> {
@ -55,7 +62,7 @@ fn parse(files: &Vec<String>) -> Vec<Module> {
}
fn check(modules: &mut Vec<Module>) {
for module in modules {
while let Some(module) = modules.pop() {
if let Err(e) = module.type_check() {
eprintln!("{}", e);
return;
@ -85,12 +92,24 @@ fn main() {
}
}
}
Commands::Compile { files, dump_clir } => {
Commands::Compile { files, dump_clir } | Commands::Run { files, dump_clir } => {
let mut jit = jit::JIT::default();
jit.dump_clir = *dump_clir;
for file in files {
match jit.compile(std::fs::read_to_string(file).unwrap().as_str(), *dump_clir) {
match jit.compile_file(file) {
Err(e) => eprintln!("{}", e),
Ok(_code) => println!("Compiled {}", file),
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);
}
}
}
}
}