e0/src/main.rs

52 lines
1,018 B
Rust
Raw Normal View History

2022-04-06 02:33:16 +00:00
#[macro_use]
extern crate lalrpop_util;
#[macro_use]
extern crate anyhow;
lalrpop_mod!(parser);
mod ast;
2022-04-06 03:07:34 +00:00
use std::fs::{self, File};
2022-04-06 02:33:16 +00:00
use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
2022-04-06 03:07:34 +00:00
use inkwell::context::Context;
2022-04-06 02:33:16 +00:00
use crate::parser::ProgramParser;
#[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 03:17:53 +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();
println!("AST: {ast:?}");
2022-04-06 02:33:16 +00:00
2022-04-06 03:17:53 +00:00
let mut context = Context::create();
let module = ast::llvm::convert(&mut context, ast)?;
2022-04-06 03:07:34 +00:00
2022-04-06 03:17:53 +00:00
{
let file = File::create(&opts.out_path)?;
module.write_bitcode_to_file(&file, true, true);
println!("Emitted.");
}
2022-04-06 03:07:34 +00:00
2022-04-06 03:17:53 +00:00
Ok(())
2022-04-06 02:33:16 +00:00
}