Package compilation context into one struct
This commit is contained in:
parent
d3a743442b
commit
db52f9b716
@ -1,5 +1,7 @@
|
|||||||
extern crate llvm_sys;
|
extern crate llvm_sys;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use self::llvm_sys::prelude::*;
|
use self::llvm_sys::prelude::*;
|
||||||
use parser::{ParseResult, AST, ASTNode, Prototype, Function, Expression};
|
use parser::{ParseResult, AST, ASTNode, Prototype, Function, Expression};
|
||||||
|
|
||||||
@ -38,29 +40,40 @@ pub fn compilation_sequence(ast: AST, sourcefile: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type VariableMap = HashMap<String, LLVMValueRef>;
|
||||||
|
|
||||||
|
struct CompilationData {
|
||||||
|
context: LLVMContextRef,
|
||||||
|
module: LLVMModuleRef,
|
||||||
|
builder: LLVMBuilderRef,
|
||||||
|
variables: VariableMap,
|
||||||
|
}
|
||||||
|
|
||||||
fn compile_ast(ast: AST, filename: &str) {
|
fn compile_ast(ast: AST, filename: &str) {
|
||||||
println!("Compiling!");
|
println!("Compiling!");
|
||||||
println!("AST is {:?}", ast);
|
println!("AST is {:?}", ast);
|
||||||
|
|
||||||
|
let names: VariableMap = HashMap::new();
|
||||||
|
|
||||||
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");
|
||||||
let builder = LLVMWrap::CreateBuilderInContext(context);
|
let builder = LLVMWrap::CreateBuilderInContext(context);
|
||||||
|
|
||||||
//let void = LLVMWrap::VoidTypeInContext(context);
|
let mut data = CompilationData {
|
||||||
|
context: context,
|
||||||
|
module: module,
|
||||||
|
builder: builder,
|
||||||
|
variables: names,
|
||||||
|
};
|
||||||
|
|
||||||
let int_type = LLVMWrap::Int64TypeInContext(context);
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||||
let function_type = LLVMWrap::FunctionType(int_type, &Vec::new(), false);
|
let function_type = LLVMWrap::FunctionType(int_type, &Vec::new(), false);
|
||||||
let function = LLVMWrap::AddFunction(module, "main", function_type);
|
let function = LLVMWrap::AddFunction(data.module, "main", function_type);
|
||||||
|
|
||||||
let bb = LLVMWrap::AppendBasicBlockInContext(context, function, "entry");
|
let bb = LLVMWrap::AppendBasicBlockInContext(data.context, function, "entry");
|
||||||
LLVMWrap::PositionBuilderAtEnd(builder, bb);
|
LLVMWrap::PositionBuilderAtEnd(builder, bb);
|
||||||
|
|
||||||
/*
|
let value = ast.codegen(&mut data);
|
||||||
let int_value: u64 = 84;
|
|
||||||
let int_value = LLVMWrap::ConstInt(int_type, int_value, false);
|
|
||||||
*/
|
|
||||||
|
|
||||||
let value = ast.codegen(context, builder);
|
|
||||||
|
|
||||||
LLVMWrap::BuildRet(builder, value);
|
LLVMWrap::BuildRet(builder, value);
|
||||||
|
|
||||||
@ -73,44 +86,44 @@ fn compile_ast(ast: AST, filename: &str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
trait CodeGen {
|
trait CodeGen {
|
||||||
fn codegen(&self, LLVMContextRef, LLVMBuilderRef) -> LLVMValueRef;
|
fn codegen(&self, &mut CompilationData) -> LLVMValueRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodeGen for AST {
|
impl CodeGen for AST {
|
||||||
fn codegen(&self, context: LLVMContextRef, builder: LLVMBuilderRef) -> LLVMValueRef {
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||||
let first = self.get(0).unwrap();
|
let first = self.get(0).unwrap();
|
||||||
first.codegen(context, builder)
|
first.codegen(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodeGen for ASTNode {
|
impl CodeGen for ASTNode {
|
||||||
fn codegen(&self, context: LLVMContextRef, builder: LLVMBuilderRef) -> LLVMValueRef {
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||||
use self::ASTNode::*;
|
use self::ASTNode::*;
|
||||||
match self {
|
match self {
|
||||||
&ExprNode(ref expr) => expr.codegen(context, builder),
|
&ExprNode(ref expr) => expr.codegen(data),
|
||||||
&FuncNode(ref func) => func.codegen(context, builder),
|
&FuncNode(ref func) => func.codegen(data),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodeGen for Function {
|
impl CodeGen for Function {
|
||||||
fn codegen(&self, context: LLVMContextRef, builder: LLVMBuilderRef) -> LLVMValueRef {
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||||
let ref body = self.body;
|
let ref body = self.body;
|
||||||
let first = body.get(0).unwrap();
|
let first = body.get(0).unwrap();
|
||||||
first.codegen(context, builder)
|
first.codegen(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodeGen for Expression {
|
impl CodeGen for Expression {
|
||||||
fn codegen(&self, context: LLVMContextRef, builder: LLVMBuilderRef) -> LLVMValueRef {
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||||
use self::Expression::*;
|
use self::Expression::*;
|
||||||
|
|
||||||
let int_type = LLVMWrap::Int64TypeInContext(context);
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
&BinExp(ref op, ref left, ref right) => {
|
&BinExp(ref op, ref left, ref right) => {
|
||||||
let lhs = left.codegen(context, builder);
|
let lhs = left.codegen(data);
|
||||||
let rhs = right.codegen(context, builder);
|
let rhs = right.codegen(data);
|
||||||
let generator = match op.as_ref() {
|
let generator = match op.as_ref() {
|
||||||
"+" => LLVMWrap::BuildAdd,
|
"+" => LLVMWrap::BuildAdd,
|
||||||
"-" => LLVMWrap::BuildSub,
|
"-" => LLVMWrap::BuildSub,
|
||||||
@ -120,7 +133,7 @@ impl CodeGen for Expression {
|
|||||||
_ => panic!("Bad operator {}", op),
|
_ => panic!("Bad operator {}", op),
|
||||||
};
|
};
|
||||||
|
|
||||||
generator(builder, lhs, rhs, "temp")
|
generator(data.builder, lhs, rhs, "temp")
|
||||||
},
|
},
|
||||||
&Number(ref n) => {
|
&Number(ref n) => {
|
||||||
let native_val = *n as u64;
|
let native_val = *n as u64;
|
||||||
|
@ -178,7 +178,7 @@ impl Evaluator {
|
|||||||
match left {
|
match left {
|
||||||
Variable(var) => {
|
Variable(var) => {
|
||||||
self.add_binding(var, right);
|
self.add_binding(var, right);
|
||||||
return (Null, None);
|
return (Null, None); //TODO variable binding should be an effect
|
||||||
},
|
},
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user