diff --git a/dev.org b/dev.org index 6304e05..210ede3 100644 --- a/dev.org +++ b/dev.org @@ -24,6 +24,8 @@ Then here is the list or parsers that we have considered ** Data structures - Pure functional datastructures papaer :: https://www.cs.cmu.edu/~rwh/theses/okasaki.pdf - +- Dynamic typing: syntax and proof theory :: https://reader.elsevier.com/reader/sd/pii/0167642394000042?token=CEFF5C5D1B03FD680762FC4889A14C0CA2BB28FE390EC51099984536E12AC358F3D28A5C25C274296ACBBC32E5AE23CD +- Representing Type Information in Dynamically Typed Languages :: https://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.4394 +- An empirical study on the impact of static typing on software maintainability :: https://www.researchgate.net/publication/259634489_An_empirical_study_on_the_impact_of_static_typing_on_software_maintainability ** Cranelift - Source tree :: https://github.com/bytecodealliance/wasmtime/tree/master/cranelift diff --git a/src/compiler.rs b/src/compiler.rs index f38eb2a..2f0c5d9 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -1,18 +1,56 @@ use inkwell::builder::Builder; use inkwell::context::Context; -use inkwell::module::Module; +use inkwell::passes::PassManager; use inkwell::values::{BasicValue, BasicValueEnum, FloatValue, FunctionValue, PointerValue}; +use crate::namespace::Namespace; use crate::types::Expression; use std::collections::HashMap; pub struct Compiler<'a, 'ctx> { - context: &'ctx Context, - builder: &'a Builder<'ctx>, - module: &'a Module<'ctx>, - scope: HashMap>, + pub context: &'ctx Context, + pub builder: &'a 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. + pub namespaces: HashMap>, + pub fpm: &'a PassManager>, + + current_ns_name: Option<&'a str>, } impl<'a, 'ctx> Compiler<'a, 'ctx> { + #[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() + } + + /// 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) + } + pub fn compile(exprs: &impl Expression) {} } diff --git a/src/main.rs b/src/main.rs index ea31790..72a4b3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::string::String; pub mod ast; pub mod compiler; +pub mod namespace; pub mod reader; pub mod types; diff --git a/src/namespace.rs b/src/namespace.rs new file mode 100644 index 0000000..07c7b92 --- /dev/null +++ b/src/namespace.rs @@ -0,0 +1,29 @@ +use inkwell::module::Module; +use inkwell::values::{FunctionValue, PointerValue}; +use std::collections::HashMap; + +pub struct Namespace<'a, 'ctx> { + /// Each namespace in serene contains it's own LLVM module. You can + /// think of modules as compilation units. Object files if you prefer. + /// This way we should be able to hot swap the namespaces. + pub module: &'a Module<'ctx>, + + scope: HashMap>, + + // The option of the current function being compiled + current_fn_opt: Option>, +} + +impl<'a, 'ctx> Namespace<'a, 'ctx> { + /// Gets a defined function given its name. + #[inline] + pub fn get_function(&self, name: &str) -> Option> { + self.module.get_function(name) + } + + /// Returns the `FunctionValue` representing the function being compiled. + #[inline] + pub fn current_fn(&self) -> FunctionValue<'ctx> { + self.current_fn_opt.unwrap() + } +}