setup main to specify an entry point
This commit is contained in:
21
src/ast.rs
21
src/ast.rs
@@ -5,14 +5,6 @@ pub enum Operator {
|
||||
Div,
|
||||
Plus,
|
||||
Minus,
|
||||
// Gt,
|
||||
// Gte,
|
||||
// Lt,
|
||||
// Lte,
|
||||
// Eq,
|
||||
// Mod,
|
||||
// Exp,
|
||||
// FloorDiv,
|
||||
}
|
||||
|
||||
|
||||
@@ -20,10 +12,6 @@ pub struct LiteralInt {
|
||||
pub value: i64
|
||||
}
|
||||
|
||||
// pub struct LiteralString {
|
||||
// value: String
|
||||
// }
|
||||
|
||||
pub struct Identifier {
|
||||
pub name: String
|
||||
}
|
||||
@@ -35,7 +23,6 @@ pub struct FunctionCall {
|
||||
|
||||
pub enum Expression {
|
||||
LiteralInt(LiteralInt),
|
||||
// LiteralString(LiteralString),
|
||||
FunctionCall(FunctionCall),
|
||||
Identifier(Identifier),
|
||||
Op(Box<Expression>, Operator, Box<Expression>),
|
||||
@@ -47,23 +34,15 @@ pub struct Block {
|
||||
|
||||
pub struct VariableDeclaration {
|
||||
pub name: Identifier,
|
||||
// type: Identifier,
|
||||
}
|
||||
|
||||
pub struct Function {
|
||||
pub name: Identifier,
|
||||
// return_type: Identifier,
|
||||
pub arguments: Vec<VariableDeclaration>,
|
||||
pub block: Block,
|
||||
}
|
||||
|
||||
|
||||
// pub struct Assignment {
|
||||
// variable: VariableDeclaration,
|
||||
// expression: Expression,
|
||||
// }
|
||||
|
||||
|
||||
pub struct Module {
|
||||
pub functions: Vec<Function>,
|
||||
}
|
||||
|
||||
@@ -62,7 +62,6 @@ impl<'ctx> ModuleCodeGen<'ctx> {
|
||||
}
|
||||
|
||||
pub fn gen_function_call(&mut self, scope: &Scope<'ctx>, function_call: &ast::FunctionCall) -> IntValue<'ctx> {
|
||||
println!("Calling function: {}", &function_call.name.name);
|
||||
let fn_value = self.module.get_function(&function_call.name.name).unwrap();
|
||||
let mut arguments = Vec::new();
|
||||
for expression in (&function_call.arguments).into_iter() {
|
||||
@@ -89,7 +88,6 @@ impl<'ctx> ModuleCodeGen<'ctx> {
|
||||
args.push(self.context.i64_type().into());
|
||||
}
|
||||
let fn_type = self.context.i64_type().fn_type(&args, false);
|
||||
println!("Adding function: {}", &function.name.name);
|
||||
let fn_value = self.module.add_function(&function.name.name, fn_type, None);
|
||||
fn_value
|
||||
}
|
||||
|
||||
47
src/main.rs
47
src/main.rs
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user