From 62fecab0a4fd2d67ef18409bbf7963b324b753ce Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Fri, 12 Feb 2021 19:46:45 +0100 Subject: [PATCH] feat: llvm statements --- src/generator/llvm.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/generator/llvm.rs b/src/generator/llvm.rs index f2e71c3..783e9a8 100644 --- a/src/generator/llvm.rs +++ b/src/generator/llvm.rs @@ -3,7 +3,6 @@ use crate::parser::node_type::*; use inkwell::builder::Builder; use inkwell::context::Context; use inkwell::module::Module; -use inkwell::targets::{InitializationConfig, Target}; use inkwell::types::*; pub struct LLVMGenerator<'ctx> { @@ -27,9 +26,8 @@ impl<'ctx> Generator for LLVMGenerator<'ctx> { } impl<'ctx> LLVMGenerator<'ctx> { - fn generate_function(&mut self, func: Function) { - let arg_types: Vec = func - .arguments + fn convert_to_llvm_args(&mut self, args: Vec) -> Vec> { + let arg_types: Vec = args .iter() .map(|arg| match arg.ty { Some(Type::Int) => self.ctx.i32_type().as_basic_type_enum(), @@ -40,6 +38,11 @@ impl<'ctx> LLVMGenerator<'ctx> { None => panic!("Function argument has no type"), }) .collect(); + return arg_types; + } + + fn generate_function(&mut self, func: Function) { + let arg_types: Vec = self.convert_to_llvm_args(func.arguments); let func_type = match func.ret_type { Some(Type::Int) => self.ctx.i32_type().fn_type(&arg_types, false), @@ -47,6 +50,26 @@ impl<'ctx> LLVMGenerator<'ctx> { None => self.ctx.void_type().fn_type(&arg_types, false), _ => todo!(), }; - self.module.add_function(&func.name, func_type, None); + let function = self.module.add_function(&func.name, func_type, None); + let _basic_block = self.ctx.append_basic_block(function, "entry"); + self.generate_statement(func.body); + } + + fn generate_statement(&mut self, statement: Statement) { + match statement { + Statement::Block(statements, scope) => { + for s in statements { + self.generate_statement(s); + } + } + Statement::Exp(expression) => self.generate_expression(expression), + _ => todo!(), + }; + } + + fn generate_expression(&mut self, expr: Expression) { + match expr { + _ => todo!(), + } } }