feat: Add support for loop statements.

This commit is contained in:
Jooris Hadeler
2026-01-15 21:42:15 +01:00
parent 6f19d38bf0
commit b0f3937227
3 changed files with 14 additions and 1 deletions

View File

@@ -73,6 +73,10 @@ pub enum Statement {
elze: Option<Box<Statement>>,
},
Loop {
body: Box<Statement>,
},
Compound {
body: Vec<Statement>,
},

View File

@@ -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());
}

View File

@@ -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<Statement> {
self.expect(&[TokenKind::KwLoop])?;
let body = Box::new(self.parse_compound_statement()?);
Ok(Statement::Loop { body })
}
fn parse_if_statement(&mut self) -> ParserResult<Statement> {
self.expect(&[TokenKind::KwIf])?;