diff --git a/src/ast.rs b/src/ast.rs index 2dd5ffd..a3cfc2f 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -47,9 +47,9 @@ impl<'a> Expr { } } -impl<'a> Expression<'a> for Expr { +impl Expression for Expr { fn eval() {} - fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> { + fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> { match self { Expr::Sym(s) => s.code_gen(compiler), _ => Err("NotImplemented".to_string()), diff --git a/src/compiler.rs b/src/compiler.rs index e09160b..6920a31 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -17,7 +17,7 @@ use inkwell::builder::Builder; use inkwell::context::Context; use inkwell::passes::PassManager; -use inkwell::values::{BasicValue, BasicValueEnum, FloatValue, FunctionValue, PointerValue}; +use inkwell::values::{AnyValueEnum, BasicValue, FloatValue, FunctionValue, PointerValue}; use crate::namespace::Namespace; use crate::types::Expression; @@ -128,9 +128,9 @@ impl<'ctx> Compiler<'ctx> { // // } pub fn compile( - &self, - exprs: Vec<&impl Expression<'ctx>>, - ) -> Vec, String>> { + &'ctx self, + exprs: Vec<&impl Expression>, + ) -> Vec, String>> { match self.current_ns() { Some(ns) => ns, None => panic!("Current namespace is not set."), diff --git a/src/main.rs b/src/main.rs index f74f155..b1fe1cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,8 +32,13 @@ pub mod types; fn main() -> io::Result<()> { let yaml = load_yaml!("cli.yml"); let args = App::from(yaml).get_matches(); + + // Create a compiler let context = compiler::create_context(); - let compiler = compiler::Compiler::new(&context); + let mut compiler = compiler::Compiler::new(&context); + + compiler.create_ns("user"); + compiler.set_current_ns("user"); if let Some(input) = args.value_of("INPUT") { let mut f = File::open(input)?; diff --git a/src/types/core.rs b/src/types/core.rs index 34e3a76..a404898 100644 --- a/src/types/core.rs +++ b/src/types/core.rs @@ -15,11 +15,11 @@ * along with this program. If not, see . */ use crate::compiler::Compiler; -use inkwell::values::PointerValue; +use inkwell::values::AnyValueEnum; -pub type ExprResult<'a> = Result, String>; +pub type ExprResult<'a> = Result, String>; -pub trait Expression<'a> { +pub trait Expression { fn eval(); - fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a>; + fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx>; } diff --git a/src/types/list.rs b/src/types/list.rs index d1a4b6b..dc44c53 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -18,32 +18,20 @@ use crate::compiler::Compiler; use crate::types::core::{ExprResult, Expression}; #[derive(Debug, Eq, PartialEq, Clone)] -pub struct List -where - for<'a> T: Expression<'a>, -{ +pub struct List { first: Box, rest: Box, } -impl List -where - for<'a> T: Expression<'a>, -{ - pub fn new(first: Box, rest: Box) -> List - where - for<'a> S: Expression<'a>, - { +impl List { + pub fn new(first: Box, rest: Box) -> List { List { first, rest } } } -impl<'a, T> Expression<'a> for List -where - for<'b> T: Expression<'b>, -{ +impl Expression for List { fn eval() {} - fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> { + fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> { Err("Not implemented on list".to_string()) } } diff --git a/src/types/number.rs b/src/types/number.rs index 46c3d6b..d4ddb80 100644 --- a/src/types/number.rs +++ b/src/types/number.rs @@ -44,9 +44,9 @@ impl PartialEq for Number { impl Eq for Number {} -impl<'a> Expression<'a> for Number { +impl Expression for Number { fn eval() {} - fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> { + fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> { Err("Not implemented on numbers".to_string()) } } diff --git a/src/types/symbol.rs b/src/types/symbol.rs index 0d31509..82a7221 100644 --- a/src/types/symbol.rs +++ b/src/types/symbol.rs @@ -16,6 +16,7 @@ */ use crate::compiler::Compiler; use crate::types::core::{ExprResult, Expression}; +use inkwell::values::AnyValueEnum; #[derive(Debug, Clone)] pub struct Symbol { @@ -30,10 +31,17 @@ impl PartialEq for Symbol { impl Eq for Symbol {} -impl<'a> Expression<'a> for Symbol { +impl Expression for Symbol { fn eval() {} - fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> { - Err("Not implemented on symbol".to_string()) + fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> { + let bool_t = compiler.context.bool_type(); + if self.name == "true" { + Ok(AnyValueEnum::IntValue(bool_t.const_int(1, false))) + } else if self.name == "false" { + Ok(AnyValueEnum::IntValue(bool_t.const_int(0, false))) + } else { + Err("Nothing yet".to_string()) + } } }