Skip to content

Commit

Permalink
well float on baby would you understand 🎶🎶🎶
Browse files Browse the repository at this point in the history
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

🎶🎶🎶
  • Loading branch information
cody-quinn committed Jun 26, 2023
1 parent e9ad9c9 commit 0302126
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 65 deletions.
35 changes: 0 additions & 35 deletions sloth/src/analysis/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
}
}
25 changes: 10 additions & 15 deletions sloth/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::collections::HashMap;
use std::io::Write;
use std::path::Path;

use inkwell::builder::Builder;
use inkwell::context::Context;
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,
};
Expand All @@ -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>,
Expand All @@ -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,
Expand Down Expand Up @@ -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, ..
} => {
Expand Down Expand Up @@ -183,7 +178,10 @@ impl<'ctx> Compiler<'ctx> {
}
};
}
_ => (),
StmtKind::Return(expr) => {
let res = self.codegen_expr(expr).unwrap();
self.builder.build_return(Some(&res));
}
}
}

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
18 changes: 5 additions & 13 deletions sloth/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +25,6 @@ use parser::AstParser;
use symtable::{Symbol, SymbolTable};

use crate::analysis::analyze;

use crate::symtable::Type;

fn main() {
Expand Down Expand Up @@ -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);
}
2 changes: 0 additions & 2 deletions sloth/src/symtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down

0 comments on commit 0302126

Please sign in to comment.