Skip to content

Commit

Permalink
add and and or operations
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 14, 2024
1 parent f578d2a commit edaac7e
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 10 deletions.
6 changes: 1 addition & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use lumina::compile_and_run;
fn main() {
let source = r#"
fn fib(n: int) -> int {
if n == 0 {
return n;
}
if n == 1 {
if n == 0 || n == 1 {
return n;
}
Expand Down
4 changes: 4 additions & 0 deletions src/repr/ast/base/expression/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub enum InfixOperation {
Plus,
Eq,
NotEq,
And,
Or,
}

impl InfixOperation {
Expand All @@ -29,6 +31,8 @@ impl TryFrom<Token> for InfixOperation {
Token::Minus => Ok(InfixOperation::Minus),
Token::DoubleEq => Ok(InfixOperation::Eq),
Token::NotEq => Ok(InfixOperation::NotEq),
Token::And => Ok(InfixOperation::And),
Token::Or => Ok(InfixOperation::Or),
_ => Err(()),
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/repr/ir/triple/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub enum BinaryOp {
Sub,
Eq,
NotEq,
And,
Or,
}

impl From<&ast::InfixOperation> for BinaryOp {
Expand All @@ -15,6 +17,8 @@ impl From<&ast::InfixOperation> for BinaryOp {
ast::InfixOperation::Minus => Self::Sub,
ast::InfixOperation::Eq => Self::Eq,
ast::InfixOperation::NotEq => Self::NotEq,
ast::InfixOperation::And => Self::And,
ast::InfixOperation::Or => Self::Or,
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/repr/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub enum Token {
DoubleEq,
#[token("!=")]
NotEq,
#[token("&&")]
And,
#[token("||")]
Or,

/*
* Language tokens
Expand Down Expand Up @@ -104,6 +108,8 @@ impl Display for Token {
Token::Minus => write!(f, "-"),
Token::DoubleEq => write!(f, "=="),
Token::NotEq => write!(f, "!="),
Token::And => write!(f, "&&"),
Token::Or => write!(f, "||"),
Token::Eq => write!(f, "="),
Token::ThinArrow => write!(f, "->"),
Token::Colon => write!(f, ":"),
Expand Down
2 changes: 2 additions & 0 deletions src/stage/codegen/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ impl<'ctx, 'ink, Ctx: LLVMCtx> FunctionGenerator<'ctx, 'ink, Ctx> {
.builder
.build_int_compare(IntPredicate::NE, lhs, rhs, "not_eq_result")
.unwrap(),
BinaryOp::And => self.builder.build_and(lhs, rhs, "and_result").unwrap(),
BinaryOp::Or => self.builder.build_or(lhs, rhs, "or_result").unwrap(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/stage/parse/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod e_integer;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Precedence {
Lowest,
Binary,
Equality,
Sum,
Call,
Expand All @@ -23,6 +24,7 @@ impl Precedence {
pub fn of(token: &Token) -> Self {
match token {
Token::Minus | Token::Plus => Precedence::Sum,
Token::And | Token::Or => Precedence::Binary,
Token::DoubleEq | Token::NotEq => Precedence::Equality,
Token::LeftParen => Precedence::Call,
_ => Precedence::Lowest,
Expand Down
1 change: 1 addition & 0 deletions src/stage/type_check/expression/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ impl InfixOperation {
match (self, left, right) {
(Plus | Minus, Ty::Int, Ty::Int) => Ok(Ty::Int),
(Eq | NotEq, left, right) if left.check(right) => Ok(Ty::Boolean),
(And | Or, Ty::Boolean, Ty::Boolean) => Ok(Ty::Boolean),
(_, left, right) => Err(TyError::Mismatch(*left, *right)),
}
}
Expand Down
6 changes: 1 addition & 5 deletions tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ fn main() -> int {
#[case::fibonacci(
4181,
r#"fn fib(n: int) -> int {
if n == 0 {
return n;
}
if n == 1 {
if n == 0 || n == 1 {
return n;
}
Expand Down

0 comments on commit edaac7e

Please sign in to comment.