Parser - While loops
This commit is contained in:
parent
529a588900
commit
daec88c2f8
1 changed files with 14 additions and 0 deletions
|
@ -80,6 +80,8 @@ impl Parser {
|
|||
/// statement := "print" expression ";"
|
||||
/// statement := declaration ";"
|
||||
/// statement := block
|
||||
/// statement := if_statement
|
||||
/// statement := while_statement
|
||||
/// ```
|
||||
fn parse_statement(&mut self) -> ParserResult<ast::StmtNode> {
|
||||
if self.expect(&[TokenType::Var]).is_some() {
|
||||
|
@ -88,6 +90,8 @@ impl Parser {
|
|||
self.parse_block()
|
||||
} else if self.expect(&[TokenType::If]).is_some() {
|
||||
self.parse_if_statement()
|
||||
} else if self.expect(&[TokenType::While]).is_some() {
|
||||
self.parse_while_statement()
|
||||
} else if self.expect(&[TokenType::Print]).is_some() {
|
||||
let expression = self.parse_expression()?;
|
||||
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:
|
||||
/// ```
|
||||
/// expression := assignment
|
||||
|
|
Loading…
Reference in a new issue