Skip to content

Commit

Permalink
feat(parser): Parse bit ops
Browse files Browse the repository at this point in the history
  • Loading branch information
yhara committed Nov 17, 2019
1 parent 37c44e1 commit 3bda8b6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
36 changes: 28 additions & 8 deletions src/parser/expression_parser.rs
Expand Up @@ -303,13 +303,7 @@ impl<'a> Parser<'a> {

fn parse_relational_expr(&mut self) -> Result<AstExpression, Error> {
self.lv += 1; self.debug_log("parse_relational_expr");
//TODO:
// parse_relational_expr
// parse_bitwise_or
// parse_bitwise_and
// parse_bitwise_shift
// parse_additive_expr
let mut expr = self.parse_additive_expr()?; // additive (> >= < <=) additive
let mut expr = self.parse_bitwise_or()?; // additive (> >= < <=) additive
let mut nesting = false;
loop {
let op = match self.next_nonspace_token() {
Expand All @@ -322,7 +316,7 @@ impl<'a> Parser<'a> {
self.skip_ws();
self.consume_token();
self.skip_wsn();
let right = self.parse_additive_expr()?;
let right = self.parse_bitwise_or()?;

if nesting {
if let AstExpressionBody::MethodCall { arg_exprs, .. } = &expr.body {
Expand All @@ -340,6 +334,32 @@ impl<'a> Parser<'a> {
Ok(expr)
}

fn parse_bitwise_or(&mut self) -> Result<AstExpression, Error> {
let mut symbols = HashMap::new();
symbols.insert(Token::BitOr, "|");
symbols.insert(Token::BitOr, "^");
self.parse_binary_operator("parse_bitwise_or",
Parser::parse_bitwise_and,
symbols)
}

fn parse_bitwise_and(&mut self) -> Result<AstExpression, Error> {
let mut symbols = HashMap::new();
symbols.insert(Token::BitAnd, "&");
self.parse_binary_operator("parse_bitwise_and",
Parser::parse_bitwise_shift,
symbols)
}

fn parse_bitwise_shift(&mut self) -> Result<AstExpression, Error> {
let mut symbols = HashMap::new();
symbols.insert(Token::BitLShift, "<<");
symbols.insert(Token::BitRShift, ">>");
self.parse_binary_operator("parse_bitwise_shift",
Parser::parse_additive_expr,
symbols)
}

fn parse_additive_expr(&mut self) -> Result<AstExpression, Error> {
let mut symbols = HashMap::new();
symbols.insert(Token::BinaryPlus, "+");
Expand Down
12 changes: 10 additions & 2 deletions src/parser/lexer.rs
Expand Up @@ -358,6 +358,10 @@ impl<'a> Lexer<'a> {
next_cur.proceed(self.src);
(Token::LessEq, LexerState::ExprBegin)
}
else if c2 == Some('<') {
next_cur.proceed(self.src);
(Token::BitLShift, LexerState::ExprBegin)
}
else {
(Token::LessThan, LexerState::ExprBegin)
}
Expand All @@ -367,6 +371,10 @@ impl<'a> Lexer<'a> {
next_cur.proceed(self.src);
(Token::GraterEq, LexerState::ExprBegin)
}
else if c2 == Some('>') {
next_cur.proceed(self.src);
(Token::BitRShift, LexerState::ExprBegin)
}
else {
(Token::GraterThan, LexerState::ExprBegin)
}
Expand All @@ -391,7 +399,7 @@ impl<'a> Lexer<'a> {
(Token::AndAnd, LexerState::ExprBegin)
}
else {
(Token::And, LexerState::ExprBegin)
(Token::BitAnd, LexerState::ExprBegin)
}
},
'|' => {
Expand All @@ -400,7 +408,7 @@ impl<'a> Lexer<'a> {
(Token::OrOr, LexerState::ExprBegin)
}
else {
(Token::Or, LexerState::ExprBegin)
(Token::BitOr, LexerState::ExprBegin)
}
},
c => {
Expand Down
14 changes: 10 additions & 4 deletions src/parser/token.rs
Expand Up @@ -37,10 +37,13 @@ pub enum Token {
Comma, // ,
Colon, // :
ColonColon, // ::
And, // &
AndAnd, // &&
Or, // |
OrOr, // ||
BitAnd, // &
BitOr, // |
BitXor, // ^
BitLShift, // <<
BitRShift, // >>
// Keywords
KwClass,
KwEnd,
Expand Down Expand Up @@ -106,10 +109,13 @@ impl Token {
Token::Comma => false, // ,
Token::Colon => true, // :
Token::ColonColon => true, // ::
Token::And => true, // &
Token::AndAnd => false, // &&
Token::Or => false, // |
Token::OrOr => false, // ||
Token::BitAnd => false, // &
Token::BitOr => false, // |
Token::BitXor => false, // ^
Token::BitLShift => false, // <<
Token::BitRShift => false, // >>
// Keywords
Token::KwClass => false,
Token::KwEnd => false,
Expand Down

0 comments on commit 3bda8b6

Please sign in to comment.