diff --git a/src/compiler.rs b/src/compiler.rs index 62849ec..5320456 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -26,15 +26,15 @@ use std::collections::HashMap; pub struct Compiler<'ctx> { pub context: &'ctx Context, pub builder: Builder<'ctx>, - // /// This hashmap contains all the namespaces that has to be compiled and - // /// maps two different keys to the same namespace. Since namespace names - // /// can not contain `/` char, the keys of this map are the namespace - // /// name and the path to the file containing the namespace. For example: - // /// - // /// A let's say we have a namespace `abc.xyz`, this namespace will have - // /// two entries in this hashmap. One would be the ns name itself which - // /// is `abc.xyz` in this case and the otherone would be - // /// `/path/to/abc/xyz.srn` file that contains the ns. + /// This hashmap contains all the namespaces that has to be compiled and + /// maps two different keys to the same namespace. Since namespace names + /// can not contain `/` char, the keys of this map are the namespace + /// name and the path to the file containing the namespace. For example: + /// + /// A let's say we have a namespace `abc.xyz`, this namespace will have + /// two entries in this hashmap. One would be the ns name itself which + /// is `abc.xyz` in this case and the otherone would be + /// `/path/to/abc/xyz.srn` file that contains the ns. pub namespaces: HashMap>, //pub fpm: &'a PassManager>, current_ns_name: Option, @@ -89,19 +89,14 @@ impl<'ctx> Compiler<'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> { - // // let builder = self.context.create_builder(); - - // // let entry = self.current_fn().get_first_basic_block().unwrap(); - - // // match entry.get_first_instruction() { - // // Some(first_instr) => builder.position_before(&first_instr), - // // None => builder.position_at_end(entry), - // // } - - // // builder.build_alloca(self.context.f64_type(), name) - // // } + // NOTE: Debug only funciton + pub fn compile_ns_str(&self, ns_name: String) -> String { + let ns = self.namespaces.get(&ns_name).map(|x| x); + match ns { + Some(v) => v.compile(), + None => panic!("Can't find namespace '{}'.", ns_name), + } + } } pub fn create_context() -> Context { diff --git a/src/main.rs b/src/main.rs index aa4d4f1..f103f06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,11 @@ fn main() -> io::Result<()> { println!("AST: {:#?}", v); let g = compiler::compile(&mut compiler, v); - println!("GEN: {:?}", g) + println!("GEN: {:?}", g); + + let compiled_module = compiler.compile_ns_str("user".to_string()); + println!("\n=================\nMODULE USER:"); + println!("{}", compiled_module); } Err(e) => println!(">> error {:?}", e), } diff --git a/src/namespace.rs b/src/namespace.rs index 0c32ff4..34a0b87 100644 --- a/src/namespace.rs +++ b/src/namespace.rs @@ -14,6 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +use crate::compiler::Compiler; use crate::scope::Scope; use crate::types::ExprResult; use crate::values::Value; @@ -121,4 +122,8 @@ impl<'ctx> Namespace<'ctx> { Err(e) => Err(e), } } + + pub fn compile(&self) -> String { + self.module.print_to_string().to_string() + } }