diff options
| author | Mel <einebeere@gmail.com> | 2021-10-22 22:44:21 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2021-10-22 22:52:24 +0200 |
| commit | c35a7ba06a47a8b26bd784df552ad4c881383898 (patch) | |
| tree | 1ae23ace7246a0a97d6d53d6b0d0b88528ca5ad1 /src/parse | |
| parent | ce9007bd5aefc37cf7e48eefc0fab4049c8a6b7d (diff) | |
| download | rabbithole-c35a7ba06a47a8b26bd784df552ad4c881383898.tar.zst rabbithole-c35a7ba06a47a8b26bd784df552ad4c881383898.zip | |
Boolean logic and If expressions.
Diffstat (limited to 'src/parse')
| -rw-r--r-- | src/parse/ast/nodes.rs | 3 | ||||
| -rw-r--r-- | src/parse/parser.rs | 38 |
2 files changed, 36 insertions, 5 deletions
diff --git a/src/parse/ast/nodes.rs b/src/parse/ast/nodes.rs index 2347d5b..36b15c8 100644 --- a/src/parse/ast/nodes.rs +++ b/src/parse/ast/nodes.rs @@ -63,6 +63,7 @@ pub enum Literal { Int(u32), Float(f32), Str(String), + Bool(bool), } impl Literal { @@ -71,6 +72,8 @@ impl Literal { Int(int) => Self::Int(int), Float(float) => Self::Float(float), Str(string) => Self::Str(string), + KeywordTrue => Self::Bool(true), + KeywordFalse => Self::Bool(false), _ => panic!("Can't create literal from '{:?}'.", token.variant), } } diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 41fd954..219065b 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -76,7 +76,7 @@ impl<T: Iterator<Item = Token>> Parser<T> { fn assignment_expression(&mut self) -> Result<Expression> { // Parse any expressions as l-values for now. - let left = self.term_expression()?; + let left = self.equality_expression()?; if let Some(op) = consume_if!(self, Assign | ConstAssign) { let right = self.assignment_expression()?; @@ -91,6 +91,38 @@ impl<T: Iterator<Item = Token>> Parser<T> { } } + fn equality_expression(&mut self) -> Result<Expression> { + let mut left = self.comparison_expression()?; + + while let Some(op) = consume_if!(self, OpEq | OpNeq) { + let right = self.comparison_expression()?; + + left = Expression::Binary { + left: Box::new(left), + op: BinaryOperator::from_token(op), + right: Box::new(right), + }; + } + + Ok(left) + } + + fn comparison_expression(&mut self) -> Result<Expression> { + let mut left = self.term_expression()?; + + while let Some(op) = consume_if!(self, OpGt | OpGte | OpLt | OpLte) { + let right = self.term_expression()?; + + left = Expression::Binary { + left: Box::new(left), + op: BinaryOperator::from_token(op), + right: Box::new(right), + }; + } + + Ok(left) + } + fn term_expression(&mut self) -> Result<Expression> { let mut left = self.factor_expression()?; @@ -143,10 +175,6 @@ impl<T: Iterator<Item = Token>> Parser<T> { let literal = self.tokens.next().unwrap(); Ok(Expression::Literal(Literal::from_token(literal))) } - // Identifier(_) => match self.tokens.next().unwrap().variant { - // Identifier(identifier) => Ok(Expression::Identifier(identifier)), - // _ => unreachable!(), - // }, Ident(_) => Ok(Expression::Identifier(inner!( self.tokens.next().unwrap(), Ident |
