Change codegen function to accept compiler's reference

This commit is contained in:
Sameer Rahmani 2020-09-12 12:34:08 +01:00
parent 6a7523e0ba
commit a6eb232b48
7 changed files with 34 additions and 33 deletions

View File

@ -47,9 +47,9 @@ impl<'a> Expr {
}
}
impl<'a> Expression<'a> for Expr {
impl Expression for Expr {
fn eval() {}
fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> {
fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> {
match self {
Expr::Sym(s) => s.code_gen(compiler),
_ => Err("NotImplemented".to_string()),

View File

@ -17,7 +17,7 @@
use inkwell::builder::Builder;
use inkwell::context::Context;
use inkwell::passes::PassManager;
use inkwell::values::{BasicValue, BasicValueEnum, FloatValue, FunctionValue, PointerValue};
use inkwell::values::{AnyValueEnum, BasicValue, FloatValue, FunctionValue, PointerValue};
use crate::namespace::Namespace;
use crate::types::Expression;
@ -128,9 +128,9 @@ impl<'ctx> Compiler<'ctx> {
// // }
pub fn compile(
&self,
exprs: Vec<&impl Expression<'ctx>>,
) -> Vec<Result<PointerValue<'ctx>, String>> {
&'ctx self,
exprs: Vec<&impl Expression>,
) -> Vec<Result<AnyValueEnum<'ctx>, String>> {
match self.current_ns() {
Some(ns) => ns,
None => panic!("Current namespace is not set."),

View File

@ -32,8 +32,13 @@ pub mod types;
fn main() -> io::Result<()> {
let yaml = load_yaml!("cli.yml");
let args = App::from(yaml).get_matches();
// Create a compiler
let context = compiler::create_context();
let compiler = compiler::Compiler::new(&context);
let mut compiler = compiler::Compiler::new(&context);
compiler.create_ns("user");
compiler.set_current_ns("user");
if let Some(input) = args.value_of("INPUT") {
let mut f = File::open(input)?;

View File

@ -15,11 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::compiler::Compiler;
use inkwell::values::PointerValue;
use inkwell::values::AnyValueEnum;
pub type ExprResult<'a> = Result<PointerValue<'a>, String>;
pub type ExprResult<'a> = Result<AnyValueEnum<'a>, String>;
pub trait Expression<'a> {
pub trait Expression {
fn eval();
fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a>;
fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx>;
}

View File

@ -18,32 +18,20 @@ use crate::compiler::Compiler;
use crate::types::core::{ExprResult, Expression};
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct List<T>
where
for<'a> T: Expression<'a>,
{
pub struct List<T: Expression> {
first: Box<T>,
rest: Box<T>,
}
impl<T> List<T>
where
for<'a> T: Expression<'a>,
{
pub fn new<S>(first: Box<S>, rest: Box<S>) -> List<S>
where
for<'a> S: Expression<'a>,
{
impl<T: Expression> List<T> {
pub fn new<S: Expression>(first: Box<S>, rest: Box<S>) -> List<S> {
List { first, rest }
}
}
impl<'a, T> Expression<'a> for List<T>
where
for<'b> T: Expression<'b>,
{
impl<T: Expression> Expression for List<T> {
fn eval() {}
fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> {
fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> {
Err("Not implemented on list".to_string())
}
}

View File

@ -44,9 +44,9 @@ impl PartialEq for Number {
impl Eq for Number {}
impl<'a> Expression<'a> for Number {
impl Expression for Number {
fn eval() {}
fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> {
fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> {
Err("Not implemented on numbers".to_string())
}
}

View File

@ -16,6 +16,7 @@
*/
use crate::compiler::Compiler;
use crate::types::core::{ExprResult, Expression};
use inkwell::values::AnyValueEnum;
#[derive(Debug, Clone)]
pub struct Symbol {
@ -30,10 +31,17 @@ impl PartialEq for Symbol {
impl Eq for Symbol {}
impl<'a> Expression<'a> for Symbol {
impl Expression for Symbol {
fn eval() {}
fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> {
Err("Not implemented on symbol".to_string())
fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> {
let bool_t = compiler.context.bool_type();
if self.name == "true" {
Ok(AnyValueEnum::IntValue(bool_t.const_int(1, false)))
} else if self.name == "false" {
Ok(AnyValueEnum::IntValue(bool_t.const_int(0, false)))
} else {
Err("Nothing yet".to_string())
}
}
}