e0/bin/e0c.rs

58 lines
1.2 KiB
Rust
Raw Normal View History

2022-04-06 03:07:34 +00:00
use std::fs::{self, File};
2022-07-18 02:00:19 +00:00
use std::io::Write;
2022-04-06 02:33:16 +00:00
use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
2022-07-18 08:21:39 +00:00
use e0::codegen::CodegenBackend;
use e0::codegen::llvm_ir::LlvmIrCodegen;
use e0::parser::ProgramParser;
2022-04-06 02:33:16 +00:00
#[derive(Debug, Parser)]
struct Opt {
2022-04-06 03:17:53 +00:00
path: PathBuf,
2022-04-06 03:07:34 +00:00
2022-04-06 04:32:37 +00:00
/// Path to output the bitcode to.
#[clap(short = 'o', long = "out", name = "bitcode-out")]
2022-04-06 03:17:53 +00:00
out_path: PathBuf,
2022-04-06 04:32:37 +00:00
/// Path to output the AST to, if at all.
#[clap(long = "emit-ast", name = "ast-out")]
emit_ast: Option<PathBuf>,
2022-04-06 02:33:16 +00:00
}
fn main() -> Result<()> {
2022-04-06 03:17:53 +00:00
let opts = Opt::parse();
2022-04-06 02:33:16 +00:00
2022-04-06 11:20:47 +00:00
let contents = fs::read_to_string(&opts.path)?;
2022-04-06 02:33:16 +00:00
2022-04-06 03:17:53 +00:00
let parser = ProgramParser::new();
let ast = parser.parse(&contents).unwrap();
2022-04-06 02:33:16 +00:00
2022-07-18 08:21:39 +00:00
let typed_ast = e0::ast::typed::convert(ast)?;
2022-04-06 11:20:47 +00:00
2022-07-18 02:00:19 +00:00
if let Some(path) = opts.emit_ast {
let mut file = File::create(&path)?;
file.write(format!("{:#?}", typed_ast).as_bytes())?;
}
2022-04-06 03:07:34 +00:00
2022-04-06 03:17:53 +00:00
{
let file = File::create(&opts.out_path)?;
2022-07-18 02:00:19 +00:00
let mut codegen = LlvmIrCodegen::new(file);
codegen.convert(typed_ast)?;
2022-04-06 03:17:53 +00:00
}
2022-04-06 03:07:34 +00:00
2022-07-18 02:00:19 +00:00
// 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.");
// }
2022-04-06 03:17:53 +00:00
Ok(())
2022-04-06 02:33:16 +00:00
}