diff --git a/src/lexer.rs b/src/lexer.rs index 62860b6..042ea8b 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -7,6 +7,12 @@ pub enum Token { /// Integer literal (64-bit) I64(i64), + /// Left Parenthesis ('(') + LParen, + + /// Right Parenthesis (')') + RParen, + /// Plus (+) Add, @@ -99,6 +105,8 @@ impl<'a> Lexer<'a> { '|' => tokens.push(Token::BOr), '&' => tokens.push(Token::BAnd), '^' => tokens.push(Token::BXor), + '(' => tokens.push(Token::LParen), + ')' => tokens.push(Token::RParen), //TODO: Don't panic, keep calm _ => panic!("Lexer encountered unexpected char: '{}'", ch), diff --git a/src/parser.rs b/src/parser.rs index 30c0cea..4ee2034 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -47,12 +47,12 @@ pub enum Ast { /* ## Grammar ### Expressions -expr_primary = LITERAL +expr_primary = LITERAL | "(" expr ")" expr_mul = expr_primary (("*" | "/" | "%") expr_primary)* expr_add = expr_mul (("+" | "-") expr_mul)* expr_shift = expr_add ((">>" | "<<") expr_add)* expr_band = expr_shift ("&" expr_shift)* -expr_bxor = expr_band ("^") expr_band)* +expr_bxor = expr_band ("^" expr_band)* expr_bor = expr_bxor ("|" expr_bxor)* expr = expr_bor */ @@ -110,6 +110,17 @@ impl> Parser { match self.next() { Token::I64(val) => Ast::I64(val), + Token::LParen => { + let inner_expr = self.parse_expr(); + + // Verify that there is a closing parenthesis + if !matches!(self.next(), Token::RParen) { + panic!("Error parsing primary expr: Exepected closing parenthesis ')'"); + } + + inner_expr + } + tok => panic!("Error parsing primary expr: Unexpected Token '{:?}'", tok), } } @@ -133,9 +144,9 @@ pub fn parse, A: IntoIterator>(tokens: A impl BinOpType { /// Get the precedence for a binary operator. Higher value means the OP is stronger binding. /// For example Multiplication is stronger than addition, so Mul has higher precedence than Add. - /// + /// /// The operator precedences are derived from the C language operator precedences. While not all - /// C operators are included or the exact same, the precedence oder is the same. + /// C operators are included or the exact same, the precedence oder is the same. /// See: https://en.cppreference.com/w/c/language/operator_precedence fn precedence(&self) -> u8 { match self {