Parser - While loops

This commit is contained in:
Emmanuel BENOîT 2023-01-01 18:40:05 +01:00
parent 529a588900
commit daec88c2f8

View file

@ -80,6 +80,8 @@ impl Parser {
/// statement := "print" expression ";" /// statement := "print" expression ";"
/// statement := declaration ";" /// statement := declaration ";"
/// statement := block /// statement := block
/// statement := if_statement
/// statement := while_statement
/// ``` /// ```
fn parse_statement(&mut self) -> ParserResult<ast::StmtNode> { fn parse_statement(&mut self) -> ParserResult<ast::StmtNode> {
if self.expect(&[TokenType::Var]).is_some() { if self.expect(&[TokenType::Var]).is_some() {
@ -88,6 +90,8 @@ impl Parser {
self.parse_block() self.parse_block()
} else if self.expect(&[TokenType::If]).is_some() { } else if self.expect(&[TokenType::If]).is_some() {
self.parse_if_statement() self.parse_if_statement()
} else if self.expect(&[TokenType::While]).is_some() {
self.parse_while_statement()
} else if self.expect(&[TokenType::Print]).is_some() { } else if self.expect(&[TokenType::Print]).is_some() {
let expression = self.parse_expression()?; let expression = self.parse_expression()?;
self.consume(&TokenType::Semicolon, "expected ';' after value")?; self.consume(&TokenType::Semicolon, "expected ';' after value")?;
@ -153,6 +157,16 @@ impl Parser {
}) })
} }
/// Parse the following rule:
/// ```
/// while := "while" condition statement
/// ```
fn parse_while_statement(&mut self) -> ParserResult<ast::StmtNode> {
let condition = self.parse_expression()?;
let body = Box::new(self.parse_statement()?);
Ok(ast::StmtNode::WhileStmt { condition, body })
}
/// Parse the following rule: /// Parse the following rule:
/// ``` /// ```
/// expression := assignment /// expression := assignment