setup main to specify an entry point

This commit is contained in:
Andrew Segavac
2020-04-19 22:22:15 -06:00
parent 4d537e0f39
commit ba65669225
6 changed files with 101 additions and 37 deletions

View File

@@ -3,26 +3,49 @@ mod ast;
lalrpop_mod!(pub grammar); // synthesized by LALRPOP
use std::fs;
use std::io::Write;
mod compiler;
use inkwell::context::Context;
extern crate clap;
use clap::{Arg, App};
fn main() {
let module_ast = grammar::ModuleParser::new().parse("
fn add(a, b) {
a + b
}
fn subtract(a, b) {
a - b
}
fn main() {
add(4, subtract(5, 2))
}
").unwrap();
let matches = App::new("Boring Language Compiler")
.version("0.0.1")
.author("Andrew Segavac")
.about("Compiles boring language files to LLVM IR.")
.arg(Arg::with_name("OUTPUT")
.short("o")
.long("out")
.value_name("OUTOUT")
.help("Sets an output file")
.takes_value(true))
.arg(Arg::with_name("INPUT")
.help("Sets the input file")
.required(true)
.index(1))
.arg(Arg::with_name("v")
.short("v")
.multiple(true)
.help("Sets the level of verbosity"))
.get_matches();
let input = matches.value_of("INPUT").unwrap();
let default_output = input.rsplitn(2, ".").collect::<Vec<&str>>().last().unwrap().clone();
let output = matches.value_of("OUTPUT").unwrap_or(default_output);
let contents = fs::read_to_string(input).expect("input file not found");
let module_ast = grammar::ModuleParser::new().parse(&contents).unwrap(); //TODO: convert to error
let context = Context::create();
let mut code_gen = compiler::ModuleCodeGen::new(&context, "main".to_string());
code_gen.gen_module(module_ast);
println!("{}", code_gen.dump());
let mut f = fs::File::create(output).expect("Unable to create out file");
f.write_all(code_gen.dump().as_bytes()).expect("Unable to write data");
}