summaryrefslogtreecommitdiff
path: root/src/ast.c
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-02 23:45:56 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-05-03 23:00:14 +0200
commit0b0ce0d1735b4358691fb1d4a85b550d45ae9d48 (patch)
treee87de88ee2af7c8e2c86c6d906194007027f7c5b /src/ast.c
parentd97cdd9e84879d8fc3adf4bcf5c130f498e95b79 (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>
Diffstat (limited to 'src/ast.c')
-rw-r--r--src/ast.c56
1 files changed, 42 insertions, 14 deletions
diff --git a/src/ast.c b/src/ast.c
index 8a86890..bac4021 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -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;
}