summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-02 23:45:54 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-05-03 22:56:15 +0200
commite255b793d66441097955163e3abb8f5d68ed54ba (patch)
tree413c1972e2a452ad15ba44e62034391c40b804c2
parentd876d8a95cc9a7d4e06a3f6eceaacfce0993046b (diff)
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 <carlos@maniero.me>
-rw-r--r--src/ast.c14
-rw-r--r--src/ast.h2
-rw-r--r--src/parser.c25
3 files changed, 27 insertions, 14 deletions
diff --git a/src/ast.c b/src/ast.c
index d83e5e6..8a86890 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -136,6 +136,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)
{
*node = (ast_node_t){
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;
}