From 8a92d5ffa8bcc261f2c00350ef3390e71fa05b34 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 11 Feb 2016 10:49:45 -0800 Subject: [PATCH] Partial LLVM work --- src/compilation.rs | 33 +++++++++++++++++++++++++++------ src/main.rs | 2 ++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/compilation.rs b/src/compilation.rs index c1d143a..c92e6d1 100644 --- a/src/compilation.rs +++ b/src/compilation.rs @@ -2,11 +2,13 @@ extern crate llvm_sys; extern crate iron_llvm; use std::collections::HashMap; -use llvm_sys::prelude::LLVMValueRef; +use self::llvm_sys::prelude::LLVMValueRef; -use iron_llvm::core; -use iron_llvm::core::types::{RealTypeCtor, RealTypeRef}; -use iron_llvm::{LLVMRef, LLVMCtor}; +use self::iron_llvm::core; +use self::iron_llvm::core::types::{RealTypeCtor, RealTypeRef}; +use self::iron_llvm::{LLVMRef, LLVMRefCtor}; + +use parser::{ParseResult, AST, ASTNode}; pub struct Context { context: core::Context, @@ -59,14 +61,14 @@ impl ModuleProvider for SimpleModuleProvider { } fn get_function(&mut self, name: &str) -> Option<(FunctionRef, bool)> { - match.self.module.get_function_by_name(name) { + match self.module.get_function_by_name(name) { Some(f) => Some((f, f.count_basic_block() > 0)), None => None } } } -pub type IRBuildingResult = Result; +pub type IRBuildingResult = Result<(LLVMValueRef, bool), String>; fn error(msg: &str) -> IRBuildingResult { Err(msg.to_string()) @@ -75,3 +77,22 @@ fn error(msg: &str) -> IRBuildingResult { pub trait IRBuilder { fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult; } + +impl IRBuilder for ParseResult { + fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult { + match self { + &Ok(ast) => ast.codegen(context, module_provider), + &Err(err) => Err(err.msg.clone()) + } + } +} + +impl IRBuilder for AST { + fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult { + let mut result = error("empty AST"); + for node in self.iter() { + result = Ok(try!(node.codegen(context, module_provider))); + } + result + } +} diff --git a/src/main.rs b/src/main.rs index 3cb9cc9..7c16cf0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,8 @@ mod parser; use eval::{Evaluator}; mod eval; +mod compilation; + fn main() { let args: Vec = std::env::args().collect(); if let Some(filename) = args.get(1) {