summaryrefslogtreecommitdiff
path: root/src/ast.c
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-02 23:45:48 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-05-03 22:32:37 +0200
commit57cee82f9136f0a0246d3b1d3191226bd4234d43 (patch)
treee0c3c4d39fa85bb4e2ee935d9fcb02db8e298895 /src/ast.c
parente3d8e031c6f20c68f2227028ee8b3e73cd9b8161 (diff)
style: Improve ast node initialization
This also removes the identifier node since it was replaced by variable. Signed-off-by: Carlos Maniero <carlos@maniero.me>
Diffstat (limited to 'src/ast.c')
-rw-r--r--src/ast.c70
1 files changed, 47 insertions, 23 deletions
diff --git a/src/ast.c b/src/ast.c
index b460f1a..d83e5e6 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -24,7 +24,7 @@ ast_node_new(void)
{
ast_node_t *node = (ast_node_t *)malloc(sizeof(ast_node_t));
if (node == NULL) {
- printf("OOO: could no allocate a node");
+ printf("OOM: could no allocate a node");
exit(EXIT_FAILURE);
}
node->kind = AST_UNKOWN_NODE;
@@ -60,7 +60,6 @@ ast_node_destroy(ast_node_t *node)
case AST_VARIABLE_ASSIGNMENT:
ast_node_destroy(node->data.variable_assignment.expression);
case AST_LITERAL:
- case AST_IDENTIFIER:
case AST_UNKOWN_NODE:
case AST_VARIABLE:
break;
@@ -71,51 +70,76 @@ ast_node_destroy(ast_node_t *node)
void
ast_node_init_return_stmt(ast_node_t *node, ast_node_t *argument)
{
- node->kind = AST_RETURN_STMT;
- node->data.return_stmt = (ast_return_stmt_t){ .argument = argument };
+ *node = (ast_node_t){
+ .kind = AST_RETURN_STMT,
+ .data = { .return_stmt = { .argument = argument } },
+ };
}
void
ast_node_init_function_declaration(ast_node_t *node, string_view_t function_name, type_t return_type, vector_t *body)
{
- node->kind = AST_FUNCTION_DECLARATION;
- node->data = (ast_node_data_t){
- .function = { .identifier = { .name = function_name }, .return_type = return_type, .body = body }
+ *node = (ast_node_t){
+ .kind = AST_FUNCTION_DECLARATION,
+ .data = {
+ .function = {
+ .identifier = { .name = function_name },
+ .return_type = return_type,
+ .body = body,
+ }
+ },
};
}
void
ast_literal_integer_create(ast_node_t *node, uint32_t number)
{
- node->kind = AST_LITERAL;
- node->data.literal = (ast_literal_t){ .kind = AST_LITERAL_INTEGER, .value = { .integer = number } };
+ *node = (ast_node_t){
+ .kind = AST_LITERAL,
+ .data = {
+ .literal = {
+ .kind = AST_LITERAL_INTEGER,
+ .value = { .integer = number }
+ },
+ },
+ };
}
void
ast_node_init_binary_operation(ast_node_t *node, ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right)
{
- node->kind = AST_BINARY_OPERATION;
- node->data = (ast_node_data_t){ .binary_operation = { .kind = kind, .left = left, .right = right } };
+ *node = (ast_node_t){
+ .kind = AST_BINARY_OPERATION,
+ .data = {
+ .binary_operation = {
+ .kind = kind,
+ .left = left,
+ .right = right,
+ },
+ },
+ };
}
void
ast_node_init_variable_declaration(ast_node_t *node, string_view_t variable_name, type_t type, ast_node_t *value)
{
- node->kind = AST_VARIABLE_DECLARATION;
- node->data = (ast_node_data_t){ .variable_declaration = {
- .identifier = { .name = variable_name }, .type = type, .value = value } };
+ *node = (ast_node_t){
+ .kind = AST_VARIABLE_DECLARATION,
+ .data = {
+ .variable_declaration = {
+ .identifier = { .name = variable_name },
+ .type = type,
+ .value = value,
+ },
+ },
+ };
}
void
ast_node_init_variable(ast_node_t *node, ast_identifier_t *identifier)
{
- node->kind = AST_VARIABLE;
- node->data = (ast_node_data_t){ .variable = { .identifier = identifier } };
-}
-
-void
-ast_node_init_identifier(ast_node_t *node, string_view_t name)
-{
- node->kind = AST_IDENTIFIER;
- node->data.identifier = (ast_identifier_t){ .name = name };
+ *node = (ast_node_t){
+ .kind = AST_VARIABLE,
+ .data = { .variable = { .identifier = identifier } },
+ };
}