Successfully compiling a main function

This commit is contained in:
greg 2016-12-25 19:33:47 -08:00
parent cfb93d9dea
commit c032da712f
2 changed files with 30 additions and 4 deletions

View File

@ -1,5 +1,5 @@
fn main() fn main()
20 25
end end

View File

@ -116,6 +116,7 @@ mod LLVMWrap {
pub fn compile_ast(ast: AST) { pub fn compile_ast(ast: AST) {
println!("Compiling!"); println!("Compiling!");
println!("AST is {:?}", ast);
let context = LLVMWrap::create_context(); let context = LLVMWrap::create_context();
let module = LLVMWrap::module_create_with_name("example module"); let module = LLVMWrap::module_create_with_name("example module");
@ -130,10 +131,14 @@ pub fn compile_ast(ast: AST) {
let bb = LLVMWrap::AppendBasicBlockInContext(context, function, "entry"); let bb = LLVMWrap::AppendBasicBlockInContext(context, function, "entry");
LLVMWrap::PositionBuilderAtEnd(builder, bb); LLVMWrap::PositionBuilderAtEnd(builder, bb);
/*
let int_value: u64 = 84; let int_value: u64 = 84;
let int_value = LLVMWrap::ConstInt(int_type, int_value, false); let int_value = LLVMWrap::ConstInt(int_type, int_value, false);
*/
LLVMWrap::BuildRet(builder, int_value); let value = ast.codegen(context);
LLVMWrap::BuildRet(builder, value);
unsafe { unsafe {
let out_file = CString::new("out.ll").unwrap(); let out_file = CString::new("out.ll").unwrap();
@ -150,25 +155,46 @@ trait CodeGen {
fn codegen(&self, LLVMContextRef) -> LLVMValueRef; fn codegen(&self, LLVMContextRef) -> LLVMValueRef;
} }
impl CodeGen for AST {
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
let first = self.get(0).unwrap();
first.codegen(context)
}
}
impl CodeGen for ASTNode { impl CodeGen for ASTNode {
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef { fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
use self::ASTNode::*; use self::ASTNode::*;
match self { match self {
&ExprNode(ref expr) => expr.codegen(context), &ExprNode(ref expr) => expr.codegen(context),
&FuncNode(ref func) => unimplemented!(), &FuncNode(ref func) => func.codegen(context),
} }
} }
} }
impl CodeGen for Function {
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
let ref body = self.body;
let first = body.get(0).unwrap();
first.codegen(context)
}
}
impl CodeGen for Expression { impl CodeGen for Expression {
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef { fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
use self::Expression::*; use self::Expression::*;
let int_type = LLVMWrap::Int64TypeInContext(context);
match self { match self {
&BinExp(ref op, ref left, ref right) => { &BinExp(ref op, ref left, ref right) => {
unimplemented!() unimplemented!()
}, },
&Number(ref n) => { &Number(ref n) => {
unimplemented!() let native_val = *n as u64;
let int_value: LLVMValueRef = LLVMWrap::ConstInt(int_type, native_val, false);
int_value
}, },
_ => unimplemented!(), _ => unimplemented!(),
} }