From 6a7523e0baebfaeeed92082d9e9e425953f09d26 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Fri, 11 Sep 2020 20:55:26 +0100 Subject: [PATCH] Change the codegen signature to accept compiler instead of ns --- src/ast.rs | 6 ++-- src/compiler.rs | 69 +++++++++++++++++++++++---------------------- src/types/core.rs | 4 +-- src/types/list.rs | 4 +-- src/types/number.rs | 4 +-- src/types/symbol.rs | 5 ++-- 6 files changed, 47 insertions(+), 45 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index c04e2ef..2dd5ffd 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -use crate::namespace::Namespace; +use crate::compiler::Compiler; use crate::types::{ExprResult, Expression, List, Number, Symbol}; #[derive(Debug, Eq, PartialEq, Clone)] @@ -49,9 +49,9 @@ impl<'a> Expr { impl<'a> Expression<'a> for Expr { fn eval() {} - fn code_gen(&self, ns: &Namespace) -> ExprResult<'a> { + fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> { match self { - Expr::Sym(s) => s.code_gen(ns), + Expr::Sym(s) => s.code_gen(compiler), _ => Err("NotImplemented".to_string()), } } diff --git a/src/compiler.rs b/src/compiler.rs index 3cc5901..e09160b 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -65,15 +65,11 @@ pub struct Compiler<'ctx> { // /// `/path/to/abc/xyz.srn` file that contains the ns. pub namespaces: HashMap<&'ctx str, Namespace<'ctx>>, //pub fpm: &'a PassManager>, - - // current_ns_name: Option<&'a str>, + current_ns_name: Option<&'ctx str>, } impl<'ctx> Compiler<'ctx> { pub fn new(context: &'ctx Context) -> Compiler<'ctx> { - //let default_ns_name = "user"; - // let builder = context.create_builder(); - //let context = Context::create(); //let user_ns = Namespace::new(&context, default_ns_name); //namespaces.insert(default_ns_name, &user_ns); // let fpm = PassManager::create(&user_ns.module); @@ -88,26 +84,34 @@ impl<'ctx> Compiler<'ctx> { // fpm.add_reassociate_pass(); // fpm.initialize(); - //, builder, fpm, namespaces, Some(&default_ns_name) - //Compiler::new(context) - //let builder = context.create_builder(); Compiler { builder: context.create_builder(), context: context, namespaces: HashMap::new(), + current_ns_name: None, } } - // #[inline] - // pub fn current_ns(&self) -> Option<&'a Namespace<'a, 'ctx>> { - // let ns = self.current_ns_name?; - // self.namespaces.get(ns).map(|x| *x) - // } - // /// Returns the `FunctionValue` representing the function being compiled. - // #[inline] - // pub fn current_fn(&self) -> FunctionValue<'ctx> { - // self.current_ns().unwrap().current_fn() - // } + pub fn create_ns(&mut self, ns_name: &'ctx str) { + self.namespaces + .insert(ns_name, Namespace::new(&self.context, ns_name)); + } + + pub fn set_current_ns(&mut self, ns_name: &'ctx str) { + self.current_ns_name = Some(ns_name); + } + + #[inline] + pub fn current_ns(&self) -> Option<&Namespace<'ctx>> { + let ns = self.current_ns_name?; + self.namespaces.get(ns).map(|x| x) + } + + /// Returns the `FunctionValue` representing the function being compiled. + #[inline] + pub fn current_fn(&self) -> FunctionValue<'ctx> { + self.current_ns().unwrap().current_fn() + } // /// Creates a new stack allocation instruction in the entry block of the function. // // fn create_entry_block_alloca(&self, name: &str) -> PointerValue<'ctx> { @@ -123,24 +127,23 @@ impl<'ctx> Compiler<'ctx> { // // builder.build_alloca(self.context.f64_type(), name) // // } - // pub fn compile( - // &self, - // exprs: Vec<&impl Expression<'ctx>>, - // ) -> Vec, String>> { - // let current_ns = match self.current_ns() { - // Some(ns) => ns, - // None => panic!("Current namespace is not set."), - // }; + pub fn compile( + &self, + exprs: Vec<&impl Expression<'ctx>>, + ) -> Vec, String>> { + match self.current_ns() { + Some(ns) => ns, + None => panic!("Current namespace is not set."), + }; - // let mut generated_code = vec![]; + let mut generated_code = vec![]; - // for expr in &exprs { - // let code = expr.code_gen(current_ns); - // generated_code.push(code); - // } + for expr in &exprs { + generated_code.push(expr.code_gen(&self)); + } - // generated_code - // } + generated_code + } } pub fn create_context() -> Context { diff --git a/src/types/core.rs b/src/types/core.rs index 88e6f12..34e3a76 100644 --- a/src/types/core.rs +++ b/src/types/core.rs @@ -14,12 +14,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -use crate::namespace::Namespace; +use crate::compiler::Compiler; use inkwell::values::PointerValue; pub type ExprResult<'a> = Result, String>; pub trait Expression<'a> { fn eval(); - fn code_gen(&self, ns: &Namespace) -> ExprResult<'a>; + fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a>; } diff --git a/src/types/list.rs b/src/types/list.rs index 5a9fdcb..d1a4b6b 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -use crate::namespace::Namespace; +use crate::compiler::Compiler; use crate::types::core::{ExprResult, Expression}; #[derive(Debug, Eq, PartialEq, Clone)] @@ -43,7 +43,7 @@ where for<'b> T: Expression<'b>, { fn eval() {} - fn code_gen(&self, ns: &Namespace) -> ExprResult<'a> { + fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> { Err("Not implemented on list".to_string()) } } diff --git a/src/types/number.rs b/src/types/number.rs index 4b9004b..46c3d6b 100644 --- a/src/types/number.rs +++ b/src/types/number.rs @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -use crate::namespace::Namespace; +use crate::compiler::Compiler; use crate::types::core::{ExprResult, Expression}; // Note: I kept the number implementation simple for now // but we need to decide on our approach to numbers, are @@ -46,7 +46,7 @@ impl Eq for Number {} impl<'a> Expression<'a> for Number { fn eval() {} - fn code_gen(&self, ns: &Namespace) -> ExprResult<'a> { + fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> { Err("Not implemented on numbers".to_string()) } } diff --git a/src/types/symbol.rs b/src/types/symbol.rs index 63d207e..0d31509 100644 --- a/src/types/symbol.rs +++ b/src/types/symbol.rs @@ -14,9 +14,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -use crate::namespace::Namespace; +use crate::compiler::Compiler; use crate::types::core::{ExprResult, Expression}; -use inkwell::values::PointerValue; #[derive(Debug, Clone)] pub struct Symbol { @@ -33,7 +32,7 @@ impl Eq for Symbol {} impl<'a> Expression<'a> for Symbol { fn eval() {} - fn code_gen(&self, ns: &Namespace) -> ExprResult<'a> { + fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> { Err("Not implemented on symbol".to_string()) } }