Skip to content

Commit

Permalink
feat(parser): add dispatching of unary operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 5, 2024
1 parent 926d767 commit f84e6bf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/fuse-parser/src/parsers/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl<'a> Parser<'a> {
}

pub(crate) fn parse_identifier(&mut self) -> ParserResult<Identifier> {
debug_assert!(self.at(TokenKind::Identifier));
let token = self.consume();
let view = self.view_token(*token);
Ok(Identifier {
Expand All @@ -61,6 +62,7 @@ impl<'a> Parser<'a> {
}

fn parse_if(&mut self) -> ParserResult<If> {
debug_assert!(matches!(self.cur_kind(), TokenKind::If | TokenKind::ElseIf));
let start = self.start_span();
// Consume the keyword
self.consume();
Expand Down Expand Up @@ -96,4 +98,9 @@ impl<'a> Parser<'a> {
r#else,
})
}

pub(crate) fn parse_unary_operator_expression(&mut self) -> ParserResult<Expression> {
self.parse_unary_operator()
.map(|op| self.ast.unary_operator_expression(op))
}
}
1 change: 1 addition & 0 deletions crates/fuse-parser/src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod declarations;
mod expressions;
mod functions;
mod numbers;
mod operators;
mod statements;
mod strings;
mod types;
Expand Down
31 changes: 31 additions & 0 deletions crates/fuse-parser/src/parsers/operators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use fuse_ast::UnaryOperator;

use crate::{lexer::TokenKind, Parser, ParserResult};

impl<'a> Parser<'a> {
pub(crate) fn parse_unary_operator(&mut self) -> ParserResult<UnaryOperator> {
match self.cur_kind() {
TokenKind::Not => self.parse_unary_not_operator(),
TokenKind::Plus => self.parse_unary_plus_operator(),
TokenKind::Minus => self.parse_unary_minus_operator(),
_ => Err(Self::unexpected_error(self.cur_token())),
}
}

fn parse_unary_not_operator(&mut self) -> ParserResult<UnaryOperator> {
debug_assert!(self.at(TokenKind::Not));
// Consume the keyword.
self.consume();
todo!()
}

fn parse_unary_plus_operator(&mut self) -> ParserResult<UnaryOperator> {
debug_assert!(self.at(TokenKind::Plus));
todo!()
}

fn parse_unary_minus_operator(&mut self) -> ParserResult<UnaryOperator> {
debug_assert!(self.at(TokenKind::Minus));
todo!()
}
}
4 changes: 4 additions & 0 deletions crates/fuse-parser/src/parsers/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ impl<'a> Parser<'a> {
.parse_expression()
.map(|expr| self.ast.expression_statement(expr)),

TokenKind::Not | TokenKind::Plus | TokenKind::Minus => self
.parse_unary_operator_expression()
.map(|expr| self.ast.expression_statement(expr)),

TokenKind::Function | TokenKind::Fn => {
if self.nth_kind(1) == TokenKind::Identifier {
// function declaration
Expand Down

0 comments on commit f84e6bf

Please sign in to comment.