Parser - Return statement

This commit is contained in:
Emmanuel BENOîT 2023-01-02 20:20:47 +01:00
parent 9b9103c170
commit 0290fbc226

View file

@ -111,6 +111,7 @@ impl Parser {
/// statement := while_statement /// statement := while_statement
/// statement := for_statement /// statement := for_statement
/// statement := loop_control_statement /// statement := loop_control_statement
/// statement := return_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() {
@ -129,6 +130,8 @@ impl Parser {
self.parse_for_statement(None) self.parse_for_statement(None)
} else if let Some(lcs) = self.expect(&[TokenType::Break, TokenType::Continue]) { } else if let Some(lcs) = self.expect(&[TokenType::Break, TokenType::Continue]) {
self.parse_loop_control_statement(&lcs) self.parse_loop_control_statement(&lcs)
} else if let Some(ret) = self.expect(&[TokenType::Return]) {
self.parse_return_statement(&ret)
} 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")?;
@ -426,6 +429,22 @@ impl Parser {
}) })
} }
/// Parse the following rule:
/// ```
/// return_statement := "return" expression? ";"
/// ```
fn parse_return_statement(&mut self, ret_token: &Token) -> ParserResult<ast::StmtNode> {
let value = if self.check(&TokenType::Semicolon) {
None
} else {
Some(self.parse_expression()?)
};
Ok(ast::StmtNode::Return {
token: ret_token.clone(),
value,
})
}
/// Parse the following rule: /// Parse the following rule:
/// ``` /// ```
/// expression := assignment /// expression := assignment