aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-06-26 02:50:06 -0500
committerCody <cody@codyq.dev>2023-06-26 02:50:06 -0500
commit03021268fc319aa5e19e7bbcac226f3f626a6f84 (patch)
treefd4f8b705fa7963fdfa8804e2074aa48f67554e5
parente9ad9c9de91f0a64cfd15c4b93da15e2bf998030 (diff)
downloadsloth-03021268fc319aa5e19e7bbcac226f3f626a6f84.tar.gz
well float on baby would you understand 🎶🎶🎶
Days are getting shorter and the nights are getting cold I like the autumn but this place is getting old I pack up my belongings and I head to the cost It might not be alot but I feel I'm making most The days are getting longer and the nights smell green Guess its not surprising but its springing as you please 🎶🎶🎶
-rw-r--r--sloth/src/analysis/setup.rs35
-rw-r--r--sloth/src/codegen/mod.rs25
-rw-r--r--sloth/src/main.rs18
-rw-r--r--sloth/src/symtable.rs2
4 files changed, 15 insertions, 65 deletions
diff --git a/sloth/src/analysis/setup.rs b/sloth/src/analysis/setup.rs
index 1bad7c0..577fe46 100644
--- a/sloth/src/analysis/setup.rs
+++ b/sloth/src/analysis/setup.rs
@@ -213,38 +213,3 @@ pub(super) fn propagate_types(node: &mut Expr) -> Result<(), AnalysisError> {
Ok(())
}
-
-#[cfg(test)]
-mod tests {
- use crate::analysis::setup::propagate_types;
- use crate::parser::ast::{BinaryOp, Expr, ExprKind, Literal};
- use crate::symtable::{Symbol, SymbolTable, Type};
-
- #[test]
- fn haiiiiiuwu() {
- let mut table = SymbolTable::new();
- // table.insert("poggo".to_owned(), Symbol::Value(Type::Integer));
- // table.insert("poggu".to_owned(), Symbol::Value(Type::Float));
-
- let mut x = Expr::new(
- 0,
- 0,
- ExprKind::BinaryOp {
- op: BinaryOp::Add,
- lhs: Box::new(Expr::new(1, 0, Literal::Float(1.).into(), table.clone())),
- rhs: Box::new(Expr::new(
- 2,
- 0,
- ExprKind::Identifier("poggu".to_owned()),
- table.clone(),
- )),
- },
- table,
- );
-
- propagate_types(&mut x).expect("oh noes something went fucky wucky >~<");
-
- println!("{x:#?}");
- panic!()
- }
-}
diff --git a/sloth/src/codegen/mod.rs b/sloth/src/codegen/mod.rs
index 81832e9..a3a1239 100644
--- a/sloth/src/codegen/mod.rs
+++ b/sloth/src/codegen/mod.rs
@@ -1,6 +1,5 @@
use std::collections::HashMap;
use std::io::Write;
-use std::path::Path;
use inkwell::builder::Builder;
use inkwell::context::Context;
@@ -8,7 +7,7 @@ use inkwell::module::Module;
use inkwell::targets::{
CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine,
};
-use inkwell::types::{BasicMetadataTypeEnum, BasicType, BasicTypeEnum};
+use inkwell::types::{BasicMetadataTypeEnum, BasicTypeEnum};
use inkwell::values::{
BasicMetadataValueEnum, BasicValue, BasicValueEnum, FunctionValue, PointerValue,
};
@@ -20,7 +19,7 @@ use crate::parser::ast::{
};
use crate::symtable::{SymbolTable, Type};
-pub struct Compiler<'ctx> {
+pub struct Codegen<'ctx> {
context: &'ctx Context,
builder: Builder<'ctx>,
module: Module<'ctx>,
@@ -31,12 +30,12 @@ pub struct Compiler<'ctx> {
references: HashMap<i32, PointerValue<'ctx>>,
}
-impl<'ctx> Compiler<'ctx> {
+impl<'ctx> Codegen<'ctx> {
pub fn new(context: &'ctx Context, module: &str) -> Self {
let builder = context.create_builder();
let module = context.create_module(module);
- Compiler {
+ Codegen {
context,
builder,
module,
@@ -136,10 +135,6 @@ impl<'ctx> Compiler<'ctx> {
// Position the builder at the end of the loop
self.builder.position_at_end(after_bb);
}
- StmtKind::Return(expr) => {
- let res = self.codegen_expr(expr).unwrap();
- self.builder.build_return(Some(&res));
- }
StmtKind::DefineVariable {
identifier, value, ..
} => {
@@ -183,7 +178,10 @@ impl<'ctx> Compiler<'ctx> {
}
};
}
- _ => (),
+ StmtKind::Return(expr) => {
+ let res = self.codegen_expr(expr).unwrap();
+ self.builder.build_return(Some(&res));
+ }
}
}
@@ -238,7 +236,7 @@ impl<'ctx> Compiler<'ctx> {
let ptr = self.references.get(&symbol.id).unwrap();
self.builder
- .build_load(self.type_as_basic_type(symbol.typ.clone()), *ptr, "deref")
+ .build_load(self.type_as_basic_type(symbol.typ.clone()), *ptr, "")
}
ExprKind::BinaryOp { op, lhs, rhs } => match lhs.typ {
Some(Type::Integer) | Some(Type::Boolean) => {
@@ -291,7 +289,7 @@ impl<'ctx> Compiler<'ctx> {
None => unreachable!("Critical Error: Type should never be null by this point"),
_ => todo!(),
},
- ExprKind::UnaryOp { op, value } => todo!(),
+ ExprKind::UnaryOp { .. } => todo!(),
ExprKind::Call { callee, args } => {
// FIXME: Callee is an expression but for now were just
// extracting an identifier to it. Change this
@@ -373,9 +371,6 @@ impl<'ctx> Compiler<'ctx> {
let buffer = machine
.write_to_memory_buffer(&self.module, filetype)
.unwrap();
- // machine
- // .write_to_file(&self.module, filetype, Path::new("export.o"))
- // .unwrap();
file.write_all(buffer.as_slice()).unwrap();
}
diff --git a/sloth/src/main.rs b/sloth/src/main.rs
index 34aad51..5ce631a 100644
--- a/sloth/src/main.rs
+++ b/sloth/src/main.rs
@@ -16,7 +16,7 @@ pub mod symtable;
use std::fs::File;
use std::{env, fs};
-use codegen::Compiler;
+use codegen::Codegen;
use inkwell::context::Context;
use inkwell::targets::FileType;
use itertools::Itertools;
@@ -25,7 +25,6 @@ use parser::AstParser;
use symtable::{Symbol, SymbolTable};
use crate::analysis::analyze;
-
use crate::symtable::Type;
fn main() {
@@ -59,18 +58,11 @@ fn main() {
return;
}
- // println!("{ast:#?}");
- // println!("Suces");
-
+ // Generating code for module
let context = Context::create();
- let mut compiler = Compiler::new(&context, "s");
+ let mut codegen = Codegen::new(&context, "s");
let mut output_file = File::create("output.o").unwrap();
- compiler.codegen(&ast);
- compiler.write_obj(&mut output_file, FileType::Object);
-
- // Compiler::codegen(&context, "hi", &ast);
-
- // let graph = GraphBuilder::generate(Some(&source), &ast).unwrap();
- // println!("{graph}");
+ codegen.codegen(&ast);
+ codegen.write_obj(&mut output_file, FileType::Object);
}
diff --git a/sloth/src/symtable.rs b/sloth/src/symtable.rs
index 8bfd16e..ef31584 100644
--- a/sloth/src/symtable.rs
+++ b/sloth/src/symtable.rs
@@ -3,8 +3,6 @@ use std::collections::hash_map::Entry::Vacant;
use std::collections::HashMap;
use std::rc::Rc;
-use inkwell::values::PointerValue;
-
#[derive(Debug, Default)]
struct Scope {
parent: Option<Rc<Scope>>,