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-07-18 02:00:19 +00:00
|
|
|
pub mod codegen;
|
2022-04-06 11:20:47 +00:00
|
|
|
mod utils;
|
2022-04-06 02:33:16 +00:00
|
|
|
|
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 02:00:19 +00:00
|
|
|
use crate::codegen::{llvm_ir::LlvmIrCodegen, CodegenBackend};
|
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 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-04-06 11:20:47 +00:00
|
|
|
let typed_ast = ast::typed::convert(ast)?;
|
|
|
|
|
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
|
|
|
}
|