From e255b793d66441097955163e3abb8f5d68ed54ba Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Tue, 2 May 2023 23:45:54 -0300 Subject: parser: Variable assignment allocates their own node This commit makes variable assignment parser to allocate memory for the node. It also moves the node initialization to the ast.c to follow our standard for node initialization. Signed-off-by: Carlos Maniero --- src/ast.c | 14 ++++++++++++++ src/ast.h | 2 ++ src/parser.c | 25 +++++++++++-------------- 3 files changed, 27 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/ast.c b/src/ast.c index d83e5e6..8a86890 100644 --- a/src/ast.c +++ b/src/ast.c @@ -135,6 +135,20 @@ ast_node_init_variable_declaration(ast_node_t *node, string_view_t variable_name }; } +void +ast_node_init_variable_assignment(ast_node_t *node, ast_identifier_t *identifier, ast_node_t *expression) +{ + *node = (ast_node_t){ + .kind = AST_VARIABLE_ASSIGNMENT, + .data = { + .variable_assignment = { + .identifier = identifier, + .expression = expression, + }, + }, + }; +} + void ast_node_init_variable(ast_node_t *node, ast_identifier_t *identifier) { diff --git a/src/ast.h b/src/ast.h index e8fe0f6..fb2bb74 100644 --- a/src/ast.h +++ b/src/ast.h @@ -140,5 +140,7 @@ void ast_literal_integer_create(ast_node_t *node, uint32_t number); void ast_node_init_variable(ast_node_t *node, ast_identifier_t *identifier); +void +ast_node_init_variable_assignment(ast_node_t *node, ast_identifier_t *identifier, ast_node_t *expression); #endif /* AST_H */ diff --git a/src/parser.c b/src/parser.c index 5457c00..2ddeb6c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -244,23 +244,23 @@ parser_parse_return_stmt(parser_t *parser) return node; } -static bool -parser_parse_variable_assignment(parser_t *parser, ast_node_t *node) +static ast_node_t * +parser_parse_variable_assignment(parser_t *parser) { token_t variable_token; if (!expected_token(&variable_token, parser, TOKEN_NAME)) { - return false; + return NULL; } if (!drop_expected_token(parser, TOKEN_EQUAL)) - return false; + return NULL; ast_node_t *expression = ast_node_new(); if (!parser_parse_expression(parser, expression) || !drop_expected_token(parser, TOKEN_SEMICOLON)) { ast_node_destroy(expression); - return false; + return NULL; } ast_node_t *variable_declaration_node = scope_get(parser->scope, variable_token.value); @@ -272,16 +272,14 @@ parser_parse_variable_assignment(parser_t *parser, ast_node_t *node) sprintf(error.message, "trying to assign '" SVFMT "' before defining it.", SVARG(&variable_token.value)); parser->errors[parser->errors_len++] = error; - return false; + return NULL; } assert(variable_declaration_node->kind == AST_VARIABLE_DECLARATION); - node->kind = AST_VARIABLE_ASSIGNMENT; - node->data = (ast_node_data_t){ .variable_assignment = { - .identifier = &variable_declaration_node->data.variable_declaration.identifier, - .expression = expression } }; - return true; + ast_node_t *node = ast_node_new(); + ast_node_init_variable_assignment(node, &variable_declaration_node->data.variable_declaration.identifier, expression); + return node; } static ast_node_t * @@ -439,11 +437,10 @@ parser_parse_block_declarations(parser_t *parser) } if (is_next_statement_a_variable_assignement(parser)) { - ast_node_t *variable_assignment = ast_node_new(); + ast_node_t *variable_assignment = parser_parse_variable_assignment(parser); - if (!parser_parse_variable_assignment(parser, variable_assignment)) { + if (variable_assignment == NULL) { scope_leave(parser->scope); - ast_node_destroy(variable_assignment); vector_destroy(body); return NULL; } -- cgit v1.2.3