diff --git a/compiler/parser/src/parser/statement.rs b/compiler/parser/src/parser/statement.rs index 9e260bd337..ac460ca97d 100644 --- a/compiler/parser/src/parser/statement.rs +++ b/compiler/parser/src/parser/statement.rs @@ -45,6 +45,7 @@ impl ParserContext<'_> { Token::For => Ok(Statement::Iteration(Box::new(self.parse_loop_statement()?))), Token::Assert | Token::AssertEq | Token::AssertNeq => Ok(self.parse_assert_statement()?), Token::Let => Ok(Statement::Definition(self.parse_definition_statement()?)), + Token::Const => Ok(Statement::Definition(self.parse_const_definition_statement()?)), Token::LeftCurly => Ok(Statement::Block(self.parse_block()?)), Token::Console => Err(ParserError::console_statements_are_not_yet_supported(self.token.span).into()), Token::Finalize => Err(ParserError::finalize_statements_are_deprecated(self.token.span).into()), @@ -314,6 +315,36 @@ impl ParserContext<'_> { Ok(ConsoleStatement { span: keyword + span, function, id: self.node_builder.next_id() }) } + /// Returns a [`DefinitionStatement`] AST node if the next tokens represent a const definition statement. + pub(super) fn parse_const_definition_statement(&mut self) -> Result { + self.expect(&Token::Const)?; + let decl_span = self.prev_token.span; + let decl_type = match &self.prev_token.token { + Token::Const => DeclarationType::Const, + _ => unreachable!( + "parse_const_definition_statement shouldn't produce this as have already ensured that first token was 'Const'" + ), + }; + + // Parse variable name and type. + let place = self.parse_expression()?; + self.expect(&Token::Colon)?; + let type_ = self.parse_type()?.0; + + self.expect(&Token::Assign)?; + let value = self.parse_expression()?; + self.expect(&Token::Semicolon)?; + + Ok(DefinitionStatement { + span: decl_span + value.span(), + declaration_type: decl_type, + place, + type_, + value, + id: self.node_builder.next_id(), + }) + } + /// Returns a [`DefinitionStatement`] AST node if the next tokens represent a definition statement. pub(super) fn parse_definition_statement(&mut self) -> Result { self.expect(&Token::Let)?;