Browse Source

Add remaining comparison operators

github-actions
Garrit Franke 2 years ago
parent
commit
82496b7807
  1. 4
      examples/fib.sb
  2. 10
      examples/playground.sb
  3. 32
      src/lexer/mod.rs
  4. 6
      src/parser/node_type.rs

4
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
}

10
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)
}

32
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,

6
src/parser/node_type.rs

@ -86,9 +86,9 @@ impl TryFrom<TokenKind> 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()),

Loading…
Cancel
Save