Add a very basic Namespace datastructure

This commit is contained in:
Sameer Rahmani 2020-06-27 22:12:09 +01:00
parent 08a651019f
commit 2756349204
4 changed files with 76 additions and 6 deletions

View File

@ -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

View File

@ -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<String, PointerValue<'ctx>>,
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<String, &'a Namespace<'a, 'ctx>>,
pub fpm: &'a PassManager<FunctionValue<'ctx>>,
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) {}
}

View File

@ -8,6 +8,7 @@ use std::string::String;
pub mod ast;
pub mod compiler;
pub mod namespace;
pub mod reader;
pub mod types;

29
src/namespace.rs Normal file
View File

@ -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<String, PointerValue<'ctx>>,
// The option of the current function being compiled
current_fn_opt: Option<FunctionValue<'ctx>>,
}
impl<'a, 'ctx> Namespace<'a, 'ctx> {
/// Gets a defined function given its name.
#[inline]
pub fn get_function(&self, name: &str) -> Option<FunctionValue<'ctx>> {
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()
}
}