diff --git a/src/ast.rs b/src/ast.rs index f2eca64..4339040 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -73,6 +73,10 @@ pub enum Statement { elze: Option>, }, + Loop { + body: Box, + }, + Compound { body: Vec, }, diff --git a/src/main.rs b/src/main.rs index 2f95dc8..7327e20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ pub mod parser; pub mod token; fn main() { - let mut parser = Parser::new("if a < 12 { let b = 10; } else { let c = 20; }"); + let mut parser = Parser::new("loop {}"); println!("{:#?}", parser.parse_statement()); } diff --git a/src/parser.rs b/src/parser.rs index 5b9afdc..3d7ea49 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -60,6 +60,7 @@ impl<'src> Parser<'src> { match self.peek_no_eof()?.kind { TokenKind::KwLet => self.parse_let_statement(), TokenKind::KwIf => self.parse_if_statement(), + TokenKind::KwLoop => self.parse_loop_statement(), _ => { let expr = self.parse_expression(0)?; @@ -69,6 +70,14 @@ impl<'src> Parser<'src> { } } + fn parse_loop_statement(&mut self) -> ParserResult { + self.expect(&[TokenKind::KwLoop])?; + + let body = Box::new(self.parse_compound_statement()?); + + Ok(Statement::Loop { body }) + } + fn parse_if_statement(&mut self) -> ParserResult { self.expect(&[TokenKind::KwIf])?;