#[macro_use] extern crate lalrpop_util; #[macro_use] extern crate anyhow; lalrpop_mod!(parser); mod ast; pub mod codegen; mod utils; use std::fs::{self, File}; use std::io::Write; use std::path::PathBuf; use anyhow::Result; use clap::Parser; use crate::codegen::{llvm_ir::LlvmIrCodegen, CodegenBackend}; use crate::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, } 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 = 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(()) }