57 lines
1.2 KiB
Rust
57 lines
1.2 KiB
Rust
use std::fs::{self, File};
|
|
use std::io::Write;
|
|
use std::path::PathBuf;
|
|
|
|
use anyhow::Result;
|
|
use clap::Parser;
|
|
use e0::codegen::CodegenBackend;
|
|
use e0::codegen::llvm_ir::LlvmIrCodegen;
|
|
use e0::parser::ProgramParser;
|
|
|
|
#[derive(Debug, Parser)]
|
|
struct Opt {
|
|
path: PathBuf,
|
|
|
|
/// Path to output the bitcode to.
|
|
#[clap(short = 'o', long = "out", name = "bitcode-out")]
|
|
out_path: PathBuf,
|
|
|
|
/// Path to output the AST to, if at all.
|
|
#[clap(long = "emit-ast", name = "ast-out")]
|
|
emit_ast: Option<PathBuf>,
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
let opts = Opt::parse();
|
|
|
|
let contents = fs::read_to_string(&opts.path)?;
|
|
|
|
let parser = ProgramParser::new();
|
|
let ast = parser.parse(&contents).unwrap();
|
|
|
|
let typed_ast = e0::ast::typed::convert(ast)?;
|
|
|
|
if let Some(path) = opts.emit_ast {
|
|
let mut file = File::create(&path)?;
|
|
file.write(format!("{:#?}", typed_ast).as_bytes())?;
|
|
}
|
|
|
|
{
|
|
let file = File::create(&opts.out_path)?;
|
|
let mut codegen = LlvmIrCodegen::new(file);
|
|
codegen.convert(typed_ast)?;
|
|
}
|
|
|
|
// let ctx = ast::cranelift::convert(
|
|
// opts.path.display().to_string(),
|
|
// typed_ast,
|
|
// )?;
|
|
|
|
// {
|
|
// let file = File::create(&opts.out_path)?;
|
|
// ctx.compile_and_emit();
|
|
// println!("Emitted.");
|
|
// }
|
|
|
|
Ok(())
|
|
}
|