diff options
author | Carlos Maniero <carlos@maniero.me> | 2023-05-02 23:45:56 -0300 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-05-03 23:00:14 +0200 |
commit | 0b0ce0d1735b4358691fb1d4a85b550d45ae9d48 (patch) | |
tree | e87de88ee2af7c8e2c86c6d906194007027f7c5b | |
parent | d97cdd9e84879d8fc3adf4bcf5c130f498e95b79 (diff) |
ast: Replace init by allocation (new) functions
All parsers have been following the patterns bellow:
ast_node_t *node = ast_node_new();
ast_node_init_node_kind(node, ...args);
return node;
Bringing a uncessessary distraction when reading. The pattern bellow was
replaced by:
return ast_node_new_node_kind(...args);
Signed-off-by: Carlos Maniero <carlos@maniero.me>
-rw-r--r-- | src/ast.c | 56 | ||||
-rw-r--r-- | src/ast.h | 35 | ||||
-rw-r--r-- | src/parser.c | 44 |
3 files changed, 78 insertions, 57 deletions
@@ -67,18 +67,24 @@ ast_node_destroy(ast_node_t *node) free(node); } -void -ast_node_init_return_stmt(ast_node_t *node, ast_node_t *argument) +ast_node_t * +ast_node_new_return_stmt(ast_node_t *argument) { + ast_node_t *node = ast_node_new(); + *node = (ast_node_t){ .kind = AST_RETURN_STMT, .data = { .return_stmt = { .argument = argument } }, }; + + return node; } -void -ast_node_init_function_declaration(ast_node_t *node, string_view_t function_name, type_t return_type, vector_t *body) +ast_node_t * +ast_node_new_function_declaration(string_view_t function_name, type_t return_type, vector_t *body) { + ast_node_t *node = ast_node_new(); + *node = (ast_node_t){ .kind = AST_FUNCTION_DECLARATION, .data = { @@ -89,11 +95,15 @@ ast_node_init_function_declaration(ast_node_t *node, string_view_t function_name } }, }; + + return node; } -void -ast_literal_integer_create(ast_node_t *node, uint32_t number) +ast_node_t * +ast_node_new_literal_integer(uint32_t number) { + ast_node_t *node = ast_node_new(); + *node = (ast_node_t){ .kind = AST_LITERAL, .data = { @@ -103,11 +113,15 @@ ast_literal_integer_create(ast_node_t *node, uint32_t number) }, }, }; + + return node; } -void -ast_node_init_binary_operation(ast_node_t *node, ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right) +ast_node_t * +ast_node_new_binary_operation(ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right) { + ast_node_t *node = ast_node_new(); + *node = (ast_node_t){ .kind = AST_BINARY_OPERATION, .data = { @@ -118,11 +132,15 @@ ast_node_init_binary_operation(ast_node_t *node, ast_binary_operation_kind_t kin }, }, }; + + return node; } -void -ast_node_init_variable_declaration(ast_node_t *node, string_view_t variable_name, type_t type, ast_node_t *value) +ast_node_t * +ast_node_new_variable_declaration(string_view_t variable_name, type_t type, ast_node_t *value) { + ast_node_t *node = ast_node_new(); + *node = (ast_node_t){ .kind = AST_VARIABLE_DECLARATION, .data = { @@ -133,11 +151,15 @@ ast_node_init_variable_declaration(ast_node_t *node, string_view_t variable_name }, }, }; + + return node; } -void -ast_node_init_variable_assignment(ast_node_t *node, ast_identifier_t *identifier, ast_node_t *expression) +ast_node_t * +ast_node_new_variable_assignment(ast_identifier_t *identifier, ast_node_t *expression) { + ast_node_t *node = ast_node_new(); + *node = (ast_node_t){ .kind = AST_VARIABLE_ASSIGNMENT, .data = { @@ -147,13 +169,19 @@ ast_node_init_variable_assignment(ast_node_t *node, ast_identifier_t *identifier }, }, }; + + return node; } -void -ast_node_init_variable(ast_node_t *node, ast_identifier_t *identifier) +ast_node_t * +ast_node_new_variable(ast_identifier_t *identifier) { + ast_node_t *node = ast_node_new(); + *node = (ast_node_t){ .kind = AST_VARIABLE, .data = { .variable = { .identifier = identifier } }, }; + + return node; } @@ -127,20 +127,25 @@ ast_node_new(void); void ast_node_destroy(ast_node_t *node); -void -ast_node_init_binary_operation(ast_node_t *node, ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right); -void -ast_node_init_function_declaration(ast_node_t *node, string_view_t function_name, type_t return_type, vector_t *body); -void -ast_node_init_return_stmt(ast_node_t *node, ast_node_t *argument); -void -ast_node_init_variable_declaration(ast_node_t *node, string_view_t variable_name, type_t type, ast_node_t *value); -// FIXME: use the naming convention -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); +ast_node_t * +ast_node_new_binary_operation(ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right); + +ast_node_t * +ast_node_new_function_declaration(string_view_t function_name, type_t return_type, vector_t *body); + +ast_node_t * +ast_node_new_return_stmt(ast_node_t *argument); + +ast_node_t * +ast_node_new_variable_declaration(string_view_t variable_name, type_t type, ast_node_t *value); + +ast_node_t * +ast_node_new_literal_integer(uint32_t number); + +ast_node_t * +ast_node_new_variable(ast_identifier_t *identifier); + +ast_node_t * +ast_node_new_variable_assignment(ast_identifier_t *identifier, ast_node_t *expression); #endif /* AST_H */ diff --git a/src/parser.c b/src/parser.c index 516b48b..f51f550 100644 --- a/src/parser.c +++ b/src/parser.c @@ -99,9 +99,7 @@ parser_literal_integer_node(token_t *token) char number_as_str[token->value.size]; string_view_to_str(&token->value, number_as_str); - ast_node_t *node = ast_node_new(); - ast_literal_integer_create(node, atoi(number_as_str)); - return node; + return ast_node_new_literal_integer(atoi(number_as_str)); } static ast_binary_operation_kind_t @@ -156,10 +154,7 @@ parser_parse_factor(parser_t *parser) return NULL; } - ast_node_t *node = ast_node_new(); - ast_node_init_variable(node, &var_node->data.variable_declaration.identifier); - - return node; + return ast_node_new_variable(&var_node->data.variable_declaration.identifier); } default: { parser_error_t error; @@ -175,8 +170,10 @@ static ast_node_t * parser_parse_term(parser_t *parser) { ast_node_t *node = parser_parse_factor(parser); - if (node == NULL) + + if (node == NULL) { return NULL; + } token_t token; lexer_peek_next_token(parser->lexer, &token); @@ -184,6 +181,7 @@ parser_parse_term(parser_t *parser) while (token.kind == TOKEN_STAR || token.kind == TOKEN_SLASH) { lexer_drop_next_token(parser->lexer); + ast_node_t *left = node; ast_node_t *right = parser_parse_factor(parser); if (right == NULL) { @@ -191,10 +189,7 @@ parser_parse_term(parser_t *parser) return NULL; } - ast_node_t *left = ast_node_new(); - *left = *node; - - ast_node_init_binary_operation(node, token_to_binary_operation_kind(&token), left, right); + node = ast_node_new_binary_operation(token_to_binary_operation_kind(&token), left, right); lexer_peek_next_token(parser->lexer, &token); } @@ -214,8 +209,9 @@ parser_parse_expression(parser_t *parser) { ast_node_t *node = parser_parse_term(parser); - if (node == NULL) + if (node == NULL) { return NULL; + } token_t token; lexer_peek_next_token(parser->lexer, &token); @@ -223,17 +219,15 @@ parser_parse_expression(parser_t *parser) while (token.kind == TOKEN_PLUS || token.kind == TOKEN_MINUS) { lexer_drop_next_token(parser->lexer); + ast_node_t *left = node; ast_node_t *right = parser_parse_term(parser); if (right == NULL) { - ast_node_destroy(node); + ast_node_destroy(left); return NULL; } - ast_node_t *left = ast_node_new(); - *left = *node; - - ast_node_init_binary_operation(node, token_to_binary_operation_kind(&token), left, right); + node = ast_node_new_binary_operation(token_to_binary_operation_kind(&token), left, right); lexer_peek_next_token(parser->lexer, &token); } @@ -258,9 +252,7 @@ parser_parse_return_stmt(parser_t *parser) return NULL; } - ast_node_t *node = ast_node_new(); - ast_node_init_return_stmt(node, argument_token); - return node; + return ast_node_new_return_stmt(argument_token); } static ast_node_t * @@ -300,9 +292,7 @@ parser_parse_variable_assignment(parser_t *parser) assert(variable_declaration_node->kind == AST_VARIABLE_DECLARATION); - ast_node_t *node = ast_node_new(); - ast_node_init_variable_assignment(node, &variable_declaration_node->data.variable_declaration.identifier, expression); - return node; + return ast_node_new_variable_assignment(&variable_declaration_node->data.variable_declaration.identifier, expression); } static ast_node_t * @@ -338,8 +328,7 @@ parser_parse_variable_definition(parser_t *parser) return NULL; } - ast_node_t *node = ast_node_new(); - ast_node_init_variable_declaration(node, variable_name.value, type, expression); + ast_node_t *node = ast_node_new_variable_declaration(variable_name.value, type, expression); scope_push(parser->scope, &node->data.variable_declaration.identifier, node); return node; @@ -533,8 +522,7 @@ parser_parse_function_declaration(parser_t *parser) return NULL; } - ast_node_t *node = ast_node_new(); - ast_node_init_function_declaration(node, func_name_token.value, return_type, body); + ast_node_t *node = ast_node_new_function_declaration(func_name_token.value, return_type, body); // TODO: When implementing function calls the scope must be pushed before the // body to be parsed, otherwise recursion not gonna work. |