Partial LLVM work

This commit is contained in:
greg 2016-02-11 10:49:45 -08:00
parent 5ed0ed727b
commit 8a92d5ffa8
2 changed files with 29 additions and 6 deletions

View File

@ -2,11 +2,13 @@ extern crate llvm_sys;
extern crate iron_llvm; extern crate iron_llvm;
use std::collections::HashMap; use std::collections::HashMap;
use llvm_sys::prelude::LLVMValueRef; use self::llvm_sys::prelude::LLVMValueRef;
use iron_llvm::core; use self::iron_llvm::core;
use iron_llvm::core::types::{RealTypeCtor, RealTypeRef}; use self::iron_llvm::core::types::{RealTypeCtor, RealTypeRef};
use iron_llvm::{LLVMRef, LLVMCtor}; use self::iron_llvm::{LLVMRef, LLVMRefCtor};
use parser::{ParseResult, AST, ASTNode};
pub struct Context { pub struct Context {
context: core::Context, context: core::Context,
@ -59,14 +61,14 @@ impl ModuleProvider for SimpleModuleProvider {
} }
fn get_function(&mut self, name: &str) -> Option<(FunctionRef, bool)> { 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)), Some(f) => Some((f, f.count_basic_block() > 0)),
None => None None => None
} }
} }
} }
pub type IRBuildingResult = Result<LLVMValueRef, bool), String>; pub type IRBuildingResult = Result<(LLVMValueRef, bool), String>;
fn error(msg: &str) -> IRBuildingResult { fn error(msg: &str) -> IRBuildingResult {
Err(msg.to_string()) Err(msg.to_string())
@ -75,3 +77,22 @@ fn error(msg: &str) -> IRBuildingResult {
pub trait IRBuilder { pub trait IRBuilder {
fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult; fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult;
} }
impl IRBuilder for ParseResult<AST> {
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
}
}

View File

@ -17,6 +17,8 @@ mod parser;
use eval::{Evaluator}; use eval::{Evaluator};
mod eval; mod eval;
mod compilation;
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
if let Some(filename) = args.get(1) { if let Some(filename) = args.get(1) {