Skip to content

Commit

Permalink
implement gt, lt, gte, lte operations
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 22, 2024
1 parent e3d53b0 commit 68a7e08
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/codegen/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ impl<'module, 'compiler, 'ink> FunctionGenerator<'module, 'compiler, 'ink> {
.unwrap(),
BinaryOp::And => self.builder.build_and(lhs, rhs, "and_result").unwrap(),
BinaryOp::Or => self.builder.build_or(lhs, rhs, "or_result").unwrap(),
BinaryOp::Greater => self
.builder
.build_int_compare(IntPredicate::SGT, lhs, rhs, "greater_result")
.unwrap(),
BinaryOp::Less => self
.builder
.build_int_compare(IntPredicate::SLT, lhs, rhs, "less_result")
.unwrap(),
BinaryOp::GreaterEq => self
.builder
.build_int_compare(IntPredicate::SGE, lhs, rhs, "greater_eq_result")
.unwrap(),
BinaryOp::LessEq => self
.builder
.build_int_compare(IntPredicate::SLE, lhs, rhs, "less_eq_result")
.unwrap(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
}
fn fib2(n: int) -> int {
if n == 0 || n == 1 {
if n <= 1 {
return n;
}
Expand All @@ -35,7 +35,7 @@ fn main() {
let n = 0;
let counter = 0;
loop {
if counter == 20 {
if counter >= 20 {
break;
}
Expand Down
8 changes: 8 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,10 @@ pub enum InfixOperation {
Plus,
Eq,
NotEq,
Greater,
Less,
GreaterEq,
LessEq,
And,
Or,
}
Expand All @@ -31,6 +35,10 @@ impl TryFrom<Token> for InfixOperation {
Token::Minus => Ok(InfixOperation::Minus),
Token::DoubleEq => Ok(InfixOperation::Eq),
Token::NotEq => Ok(InfixOperation::NotEq),
Token::LeftAngle => Ok(InfixOperation::Less),
Token::RightAngle => Ok(InfixOperation::Greater),
Token::LeftAngleEq => Ok(InfixOperation::LessEq),
Token::RightAngleEq => Ok(InfixOperation::GreaterEq),
Token::And => Ok(InfixOperation::And),
Token::Or => Ok(InfixOperation::Or),
_ => Err(()),
Expand Down
8 changes: 8 additions & 0 deletions src/repr/ir/triple/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ pub enum BinaryOp {
Sub,
Eq,
NotEq,
Greater,
Less,
GreaterEq,
LessEq,
And,
Or,
}
Expand All @@ -17,6 +21,10 @@ impl From<&ast::InfixOperation> for BinaryOp {
ast::InfixOperation::Minus => Self::Sub,
ast::InfixOperation::Eq => Self::Eq,
ast::InfixOperation::NotEq => Self::NotEq,
ast::InfixOperation::Greater => Self::Greater,
ast::InfixOperation::Less => Self::Less,
ast::InfixOperation::GreaterEq => Self::GreaterEq,
ast::InfixOperation::LessEq => Self::LessEq,
ast::InfixOperation::And => Self::And,
ast::InfixOperation::Or => Self::Or,
}
Expand Down
12 changes: 12 additions & 0 deletions src/repr/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ pub enum Token {
DoubleEq,
#[token("!=")]
NotEq,
#[token("<")]
LeftAngle,
#[token(">")]
RightAngle,
#[token("<=")]
LeftAngleEq,
#[token(">=")]
RightAngleEq,
#[token("&&")]
And,
#[token("||")]
Expand Down Expand Up @@ -120,6 +128,10 @@ impl Display for Token {
Token::NotEq => write!(f, "!="),
Token::And => write!(f, "&&"),
Token::Or => write!(f, "||"),
Token::LeftAngle => write!(f, "<"),
Token::RightAngle => write!(f, ">"),
Token::LeftAngleEq => write!(f, "<="),
Token::RightAngleEq => write!(f, ">="),
Token::Eq => write!(f, "="),
Token::AddAssign => write!(f, "+="),
Token::MinusAssign => write!(f, "-="),
Expand Down
7 changes: 6 additions & 1 deletion src/stage/parse/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ impl Precedence {
match token {
Token::Minus | Token::Plus => Precedence::Sum,
Token::And | Token::Or => Precedence::Binary,
Token::DoubleEq | Token::NotEq => Precedence::Equality,
Token::DoubleEq
| Token::NotEq
| Token::LeftAngle
| Token::RightAngle
| Token::LeftAngleEq
| Token::RightAngleEq => Precedence::Equality,
Token::LeftParen => Precedence::Call,
_ => Precedence::Lowest,
}
Expand Down
6 changes: 5 additions & 1 deletion src/stage/type_check/expression/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ 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),
(Eq | NotEq | Greater | Less | GreaterEq | LessEq, 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

0 comments on commit 68a7e08

Please sign in to comment.