basic jit
This commit is contained in:
parent
374daaff7f
commit
511be952aa
16 changed files with 971 additions and 495 deletions
94
src/main.rs
94
src/main.rs
|
|
@ -1,10 +1,11 @@
|
|||
mod ast;
|
||||
mod parsing;
|
||||
mod typing;
|
||||
pub mod ast;
|
||||
pub mod jit;
|
||||
pub mod parsing;
|
||||
pub mod typing;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use crate::ast::untyped::module::Module;
|
||||
use crate::ast::Module;
|
||||
|
||||
/// Experimental compiler for lila
|
||||
#[derive(Parser, Debug)]
|
||||
|
|
@ -18,53 +19,80 @@ struct Cli {
|
|||
#[derive(Subcommand, Debug)]
|
||||
enum Commands {
|
||||
Parse {
|
||||
/// Path to the source files
|
||||
/// Paths to the source files
|
||||
files: Vec<String>,
|
||||
|
||||
/// Dump the AST to stdout
|
||||
#[arg(long)]
|
||||
dump_ast: bool,
|
||||
|
||||
/// Add missing return types in the AST
|
||||
#[arg(long)]
|
||||
type_check: bool,
|
||||
},
|
||||
TypeCheck {
|
||||
/// Paths to the source files
|
||||
files: Vec<String>,
|
||||
|
||||
/// Dump the AST to stdout
|
||||
#[arg(long)]
|
||||
dump_ast: bool,
|
||||
},
|
||||
Compile {
|
||||
/// Paths to the source files
|
||||
files: Vec<String>,
|
||||
|
||||
/// Dump the CLIR to stdout
|
||||
#[arg(long)]
|
||||
dump_clir: bool,
|
||||
},
|
||||
}
|
||||
|
||||
fn parse(files: &Vec<String>) -> Vec<Module> {
|
||||
let paths = files.iter().map(std::path::Path::new);
|
||||
paths
|
||||
.map(|path| match parsing::parse_file(&path) {
|
||||
Ok(module) => module,
|
||||
Err(e) => panic!("Parsing error: {:#?}", e),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn check(modules: &mut Vec<Module>) {
|
||||
for module in modules {
|
||||
if let Err(e) = module.type_check() {
|
||||
eprintln!("{}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
|
||||
match &cli.command {
|
||||
Commands::Parse {
|
||||
files,
|
||||
dump_ast,
|
||||
type_check,
|
||||
} => {
|
||||
let paths = files.iter().map(std::path::Path::new);
|
||||
let modules: Vec<Module> = paths
|
||||
.map(|path| match parsing::parse_file(&path) {
|
||||
Ok(module) => module,
|
||||
Err(e) => panic!("Parsing error: {:#?}", e),
|
||||
})
|
||||
.collect();
|
||||
|
||||
if *type_check {
|
||||
for module in &modules {
|
||||
if let Err(e) = module.type_check() {
|
||||
eprintln!("{}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Commands::Parse { files, dump_ast } => {
|
||||
let modules = parse(files);
|
||||
if *dump_ast {
|
||||
for module in &modules {
|
||||
println!("{:#?}", &module);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
println!("Parsing OK");
|
||||
}
|
||||
Commands::TypeCheck { files, dump_ast } => {
|
||||
let mut modules = parse(files);
|
||||
check(&mut modules);
|
||||
if *dump_ast {
|
||||
for module in &modules {
|
||||
println!("{:#?}", &module);
|
||||
}
|
||||
}
|
||||
}
|
||||
Commands::Compile { files, dump_clir } => {
|
||||
let mut jit = jit::JIT::default();
|
||||
for file in files {
|
||||
match jit.compile(std::fs::read_to_string(file).unwrap().as_str(), *dump_clir) {
|
||||
Err(e) => eprintln!("{}", e),
|
||||
Ok(_code) => println!("Compiled {}", file),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue