Change the codegen signature to accept compiler instead of ns
This commit is contained in:
parent
5c6635fd0c
commit
6a7523e0ba
|
@ -14,7 +14,7 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* 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};
|
use crate::types::{ExprResult, Expression, List, Number, Symbol};
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||||
|
@ -49,9 +49,9 @@ impl<'a> Expr {
|
||||||
|
|
||||||
impl<'a> Expression<'a> for Expr {
|
impl<'a> Expression<'a> for Expr {
|
||||||
fn eval() {}
|
fn eval() {}
|
||||||
fn code_gen(&self, ns: &Namespace) -> ExprResult<'a> {
|
fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a> {
|
||||||
match self {
|
match self {
|
||||||
Expr::Sym(s) => s.code_gen(ns),
|
Expr::Sym(s) => s.code_gen(compiler),
|
||||||
_ => Err("NotImplemented".to_string()),
|
_ => Err("NotImplemented".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,15 +65,11 @@ pub struct Compiler<'ctx> {
|
||||||
// /// `/path/to/abc/xyz.srn` file that contains the ns.
|
// /// `/path/to/abc/xyz.srn` file that contains the ns.
|
||||||
pub namespaces: HashMap<&'ctx str, Namespace<'ctx>>,
|
pub namespaces: HashMap<&'ctx str, Namespace<'ctx>>,
|
||||||
//pub fpm: &'a PassManager<FunctionValue<'ctx>>,
|
//pub fpm: &'a PassManager<FunctionValue<'ctx>>,
|
||||||
|
current_ns_name: Option<&'ctx str>,
|
||||||
// current_ns_name: Option<&'a str>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ctx> Compiler<'ctx> {
|
impl<'ctx> Compiler<'ctx> {
|
||||||
pub fn new(context: &'ctx Context) -> 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);
|
//let user_ns = Namespace::new(&context, default_ns_name);
|
||||||
//namespaces.insert(default_ns_name, &user_ns);
|
//namespaces.insert(default_ns_name, &user_ns);
|
||||||
// let fpm = PassManager::create(&user_ns.module);
|
// let fpm = PassManager::create(&user_ns.module);
|
||||||
|
@ -88,26 +84,34 @@ impl<'ctx> Compiler<'ctx> {
|
||||||
// fpm.add_reassociate_pass();
|
// fpm.add_reassociate_pass();
|
||||||
|
|
||||||
// fpm.initialize();
|
// fpm.initialize();
|
||||||
//, builder, fpm, namespaces, Some(&default_ns_name)
|
|
||||||
//Compiler::new(context)
|
|
||||||
//let builder = context.create_builder();
|
|
||||||
Compiler {
|
Compiler {
|
||||||
builder: context.create_builder(),
|
builder: context.create_builder(),
|
||||||
context: context,
|
context: context,
|
||||||
namespaces: HashMap::new(),
|
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.
|
pub fn create_ns(&mut self, ns_name: &'ctx str) {
|
||||||
// #[inline]
|
self.namespaces
|
||||||
// pub fn current_fn(&self) -> FunctionValue<'ctx> {
|
.insert(ns_name, Namespace::new(&self.context, ns_name));
|
||||||
// self.current_ns().unwrap().current_fn()
|
}
|
||||||
// }
|
|
||||||
|
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.
|
// /// Creates a new stack allocation instruction in the entry block of the function.
|
||||||
// // fn create_entry_block_alloca(&self, name: &str) -> PointerValue<'ctx> {
|
// // 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)
|
// // builder.build_alloca(self.context.f64_type(), name)
|
||||||
// // }
|
// // }
|
||||||
|
|
||||||
// pub fn compile(
|
pub fn compile(
|
||||||
// &self,
|
&self,
|
||||||
// exprs: Vec<&impl Expression<'ctx>>,
|
exprs: Vec<&impl Expression<'ctx>>,
|
||||||
// ) -> Vec<Result<PointerValue<'ctx>, String>> {
|
) -> Vec<Result<PointerValue<'ctx>, String>> {
|
||||||
// let current_ns = match self.current_ns() {
|
match self.current_ns() {
|
||||||
// Some(ns) => ns,
|
Some(ns) => ns,
|
||||||
// None => panic!("Current namespace is not set."),
|
None => panic!("Current namespace is not set."),
|
||||||
// };
|
};
|
||||||
|
|
||||||
// let mut generated_code = vec![];
|
let mut generated_code = vec![];
|
||||||
|
|
||||||
// for expr in &exprs {
|
for expr in &exprs {
|
||||||
// let code = expr.code_gen(current_ns);
|
generated_code.push(expr.code_gen(&self));
|
||||||
// generated_code.push(code);
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// generated_code
|
generated_code
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_context() -> Context {
|
pub fn create_context() -> Context {
|
||||||
|
|
|
@ -14,12 +14,12 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
use crate::namespace::Namespace;
|
use crate::compiler::Compiler;
|
||||||
use inkwell::values::PointerValue;
|
use inkwell::values::PointerValue;
|
||||||
|
|
||||||
pub type ExprResult<'a> = Result<PointerValue<'a>, String>;
|
pub type ExprResult<'a> = Result<PointerValue<'a>, String>;
|
||||||
|
|
||||||
pub trait Expression<'a> {
|
pub trait Expression<'a> {
|
||||||
fn eval();
|
fn eval();
|
||||||
fn code_gen(&self, ns: &Namespace) -> ExprResult<'a>;
|
fn code_gen(&self, compiler: &Compiler) -> ExprResult<'a>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* 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 crate::types::core::{ExprResult, Expression};
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||||
|
@ -43,7 +43,7 @@ where
|
||||||
for<'b> T: Expression<'b>,
|
for<'b> T: Expression<'b>,
|
||||||
{
|
{
|
||||||
fn eval() {}
|
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())
|
Err("Not implemented on list".to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* 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 crate::types::core::{ExprResult, Expression};
|
||||||
// Note: I kept the number implementation simple for now
|
// Note: I kept the number implementation simple for now
|
||||||
// but we need to decide on our approach to numbers, are
|
// 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 {
|
impl<'a> Expression<'a> for Number {
|
||||||
fn eval() {}
|
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())
|
Err("Not implemented on numbers".to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,8 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* 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 crate::types::core::{ExprResult, Expression};
|
||||||
use inkwell::values::PointerValue;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Symbol {
|
pub struct Symbol {
|
||||||
|
@ -33,7 +32,7 @@ impl Eq for Symbol {}
|
||||||
|
|
||||||
impl<'a> Expression<'a> for Symbol {
|
impl<'a> Expression<'a> for Symbol {
|
||||||
fn eval() {}
|
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())
|
Err("Not implemented on symbol".to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue