Add a function to compiler to compiler modules to string

This commit is contained in:
Sameer Rahmani 2020-09-20 23:01:50 +01:00
parent 40bcb3b16c
commit ce78534cd3
3 changed files with 27 additions and 23 deletions

View File

@ -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<String, Namespace<'ctx>>,
//pub fpm: &'a PassManager<FunctionValue<'ctx>>,
current_ns_name: Option<String>,
@ -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 {

View File

@ -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),
}

View File

@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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()
}
}