From 82496b78079492bad80c44db26d098888d4ee7f2 Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Mon, 7 Dec 2020 13:07:46 +0100 Subject: [PATCH] Add remaining comparison operators --- examples/fib.sb | 4 ++-- examples/playground.sb | 10 +++++----- src/lexer/mod.rs | 32 +++++++++++++++++++++++++++++--- src/parser/node_type.rs | 6 +++--- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/examples/fib.sb b/examples/fib.sb index 367147f..626dd4a 100644 --- a/examples/fib.sb +++ b/examples/fib.sb @@ -1,10 +1,10 @@ main :: () { let num = 10 - print(fib(num)) + return fib(num) } fib :: (n) { - if (n <= 1) { + if n <= 1 { return n } diff --git a/examples/playground.sb b/examples/playground.sb index 8cef15b..dc24494 100644 --- a/examples/playground.sb +++ b/examples/playground.sb @@ -1,12 +1,12 @@ main :: () { - return fib(3) + fib() + let num = 10 + return fib(num) } fib :: (n) { - if n { - let x = 1 + n - return x + if 1 >= n { + return n } - return 1 + return fib(n-1) + fib(n-2) } \ No newline at end of file diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index fc4e486..e189a49 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -57,6 +57,8 @@ pub enum TokenKind { DoubleColon, /// ";" SemiColon, + /// "!" + Exclamation, /// "," Comma, /// "=" @@ -65,8 +67,14 @@ pub enum TokenKind { Equals, /// "<" LessThan, + /// "<=" + LessThanOrEqual, /// ">" GreaterThan, + /// ">=" + GreaterThanOrEqual, + /// "!=" + NotEqual, /// "(" BraceOpen, /// ")" @@ -99,7 +107,6 @@ pub enum Keyword { If, Else, Return, - FunctionDecl, Boolean, Unknown, } @@ -187,8 +194,27 @@ impl Cursor<'_> { }, ';' => SemiColon, ',' => Comma, - '<' => LessThan, - '>' => GreaterThan, + '<' => match self.first() { + '=' => { + self.bump(); + LessThanOrEqual + } + _ => LessThan, + }, + '>' => match self.first() { + '=' => { + self.bump(); + GreaterThanOrEqual + } + _ => GreaterThan, + }, + '!' => match self.first() { + '=' => { + self.bump(); + NotEqual + } + _ => Exclamation, + }, '(' => BraceOpen, ')' => BraceClose, '[' => SquareBraceOpen, diff --git a/src/parser/node_type.rs b/src/parser/node_type.rs index b8a999d..365810e 100644 --- a/src/parser/node_type.rs +++ b/src/parser/node_type.rs @@ -86,9 +86,9 @@ impl TryFrom for BinOp { TokenKind::LessThan => Ok(BinOp::LessThan), TokenKind::GreaterThan => Ok(BinOp::GreaterThan), TokenKind::Equals => Ok(BinOp::Equal), - // TokenKind::LessThanOrEqual => BinOp::LessThanOrEqual, - // TokenKind::GreaterThanOrEqual => BinOp::GreaterThanOrEqual, - // TokenKind::NotEquals => BinOp::NotEqual, + TokenKind::LessThanOrEqual => Ok(BinOp::LessThanOrEqual), + TokenKind::GreaterThanOrEqual => Ok(BinOp::GreaterThanOrEqual), + TokenKind::NotEqual => Ok(BinOp::NotEqual), // TokenKind::And => BinOp::And, // TokenKind::Or => BinOp::Or, other => Err(format!("Token {:?} cannot be converted into a BinOp", other).into()),