Change the codegen signature to accept compiler instead of ns

This commit is contained in:
Sameer Rahmani 2020-09-11 20:55:26 +01:00
parent 5c6635fd0c
commit 6a7523e0ba
6 changed files with 47 additions and 45 deletions

View File

@ -14,7 +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::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()),
}
}

View File

@ -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<FunctionValue<'ctx>>,
// 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<Result<PointerValue<'ctx>, 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<Result<PointerValue<'ctx>, 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 {

View File

@ -14,12 +14,12 @@
* 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::namespace::Namespace;
use crate::compiler::Compiler;
use inkwell::values::PointerValue;
pub type ExprResult<'a> = Result<PointerValue<'a>, String>;
pub trait Expression<'a> {
fn eval();
fn code_gen(&self, ns: &Namespace) -> ExprResult<'a>;
fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a>;
}

View File

@ -14,7 +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::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())
}
}

View File

@ -14,7 +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::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())
}
}

View File

@ -14,9 +14,8 @@
* 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::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())
}
}