Skip to content

Commit

Permalink
Add mising operators (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
joczkowski authored Nov 30, 2021
1 parent d9b8a53 commit a33e379
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,45 @@ impl Visitor<Value> for Compiler {

Value::Numeric(self.builder.build_fsub(l, r, ""))
}
expression::Operator::Slash => {
let l = match self.walk(&expr.left) {
Value::Numeric(p) => p,
_ => panic!("panic"),
};

let r = match self.walk(&expr.right) {
Value::Numeric(p) => p,
_ => panic!("panic"),
};

Value::Numeric(self.builder.build_fdiv(l, r, ""))
}
expression::Operator::GreaterOrEqual => {
let l = match self.walk(&expr.left) {
Value::Numeric(p) => p,
_ => panic!("panic"),
};

let r = match self.walk(&expr.right) {
Value::Numeric(p) => p,
_ => panic!("panic"),
};

Value::Bool(self.builder.build_fcmp(l, r, llvm::Cmp::GreaterOrEqual, ""))
}
expression::Operator::NotEqual => {
let l = match self.walk(&expr.left) {
Value::Numeric(p) => p,
_ => panic!("panic"),
};

let r = match self.walk(&expr.right) {
Value::Numeric(p) => p,
_ => panic!("panic"),
};

Value::Bool(self.builder.build_fcmp(l, r, llvm::Cmp::NotEqual, ""))
}
_ => todo!("{:?}", expr.operator),
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ impl Builder {
Value(unsafe { LLVMBuildFSub(self.0, lhs.0, rhs.0, c_str(name).as_ptr()) })
}

pub fn build_fdiv(&self, lhs: Value, rhs: Value, name: &str) -> Value {
Value(unsafe { LLVMBuildFDiv(self.0, lhs.0, rhs.0, c_str(name).as_ptr()) })
}

pub fn build_fmul(&self, lhs: Value, rhs: Value, name: &str) -> Value {
Value(unsafe { LLVMBuildFMul(self.0, lhs.0, rhs.0, c_str(name).as_ptr()) })
}
Expand Down
172 changes: 171 additions & 1 deletion tests/compiler_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rocklang::compiler::{Compile, Compiler};
use rocklang::expression::{Assignment, Expression, FuncCall, FuncDecl};
use rocklang::expression::{Assignment, Binary, Expression, FuncCall, FuncDecl, Operator};
use rocklang::parser::{Param, Program, Type};

fn remove_whitespace(s: &str) -> String {
Expand Down Expand Up @@ -157,3 +157,173 @@ fn it_compiles_recursive_fun() {
let mut compiler = Compiler::new(program);
compiler.compile().unwrap();
}

#[test]
fn it_compiles_plus_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::Plus,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

#[test]
fn it_compiles_asterisk_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::Asterisk,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

#[test]
fn it_compiles_less_or_equal_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::LessOrEqual,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

#[test]
fn it_compiles_less_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::Less,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

#[test]
fn it_compiles_greater_or_equal_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::GreaterOrEqual,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

#[test]
fn it_compiles_greater_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::Greater,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

#[test]
fn it_compiles_equal_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::Equal,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

#[test]
fn it_compiles_slash_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::Slash,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

#[test]
fn it_compiles_minus_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::Minus,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

#[test]
fn it_compiles_not_equal_operator() {
let program = Program {
body: vec![Expression::Assignment(Assignment {
left: Box::new(Expression::Identifier("b".to_string())),
right: Box::new(Expression::Binary(Binary {
left: Box::new(Expression::Numeric(10.0)),
operator: Operator::NotEqual,
right: Box::new(Expression::Numeric(2.0)),
})),
})],
};

let mut compiler = Compiler::new(program);
compiler.compile().unwrap()
}

0 comments on commit a33e379

Please sign in to comment.