diff --git a/src/codegen/llvm/mod.rs b/src/codegen/llvm/mod.rs index 9766787..31acb56 100644 --- a/src/codegen/llvm/mod.rs +++ b/src/codegen/llvm/mod.rs @@ -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(), } } diff --git a/src/main.rs b/src/main.rs index 847eae8..92f59d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ fn main() { } fn fib2(n: int) -> int { - if n == 0 || n == 1 { + if n <= 1 { return n; } @@ -35,7 +35,7 @@ fn main() { let n = 0; let counter = 0; loop { - if counter == 20 { + if counter >= 20 { break; } diff --git a/src/repr/ast/base/expression/infix.rs b/src/repr/ast/base/expression/infix.rs index 1b07cdf..c51b490 100644 --- a/src/repr/ast/base/expression/infix.rs +++ b/src/repr/ast/base/expression/infix.rs @@ -8,6 +8,10 @@ pub enum InfixOperation { Plus, Eq, NotEq, + Greater, + Less, + GreaterEq, + LessEq, And, Or, } @@ -31,6 +35,10 @@ impl TryFrom 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(()), diff --git a/src/repr/ir/triple/ops.rs b/src/repr/ir/triple/ops.rs index 7630166..2dacba1 100644 --- a/src/repr/ir/triple/ops.rs +++ b/src/repr/ir/triple/ops.rs @@ -6,6 +6,10 @@ pub enum BinaryOp { Sub, Eq, NotEq, + Greater, + Less, + GreaterEq, + LessEq, And, Or, } @@ -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, } diff --git a/src/repr/token.rs b/src/repr/token.rs index 9efb63f..c822738 100644 --- a/src/repr/token.rs +++ b/src/repr/token.rs @@ -16,6 +16,14 @@ pub enum Token { DoubleEq, #[token("!=")] NotEq, + #[token("<")] + LeftAngle, + #[token(">")] + RightAngle, + #[token("<=")] + LeftAngleEq, + #[token(">=")] + RightAngleEq, #[token("&&")] And, #[token("||")] @@ -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, "-="), diff --git a/src/stage/parse/expression/mod.rs b/src/stage/parse/expression/mod.rs index 912a0c3..50c0aaa 100644 --- a/src/stage/parse/expression/mod.rs +++ b/src/stage/parse/expression/mod.rs @@ -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, } diff --git a/src/stage/type_check/expression/infix.rs b/src/stage/type_check/expression/infix.rs index d6ac37e..1e6bfe4 100644 --- a/src/stage/type_check/expression/infix.rs +++ b/src/stage/type_check/expression/infix.rs @@ -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)), }