diff options
author | Carlos Maniero <carlos@maniero.me> | 2023-05-02 23:45:48 -0300 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-05-03 22:32:37 +0200 |
commit | 57cee82f9136f0a0246d3b1d3191226bd4234d43 (patch) | |
tree | e0c3c4d39fa85bb4e2ee935d9fcb02db8e298895 | |
parent | e3d8e031c6f20c68f2227028ee8b3e73cd9b8161 (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>
-rw-r--r-- | src/ast.c | 70 | ||||
-rw-r--r-- | src/ast.h | 4 | ||||
-rw-r--r-- | src/gas_assembly_generator.c | 3 |
3 files changed, 47 insertions, 30 deletions
@@ -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 } }, + }; } @@ -97,7 +97,6 @@ typedef enum { AST_BINARY_OPERATION, AST_FUNCTION_DECLARATION, - AST_IDENTIFIER, AST_LITERAL, AST_RETURN_STMT, AST_UNKOWN_NODE, @@ -114,7 +113,6 @@ typedef union ast_return_stmt_t return_stmt; ast_variable_declaration_t variable_declaration; ast_variable_assignment_t variable_assignment; - ast_identifier_t identifier; ast_variable_t variable; } ast_node_data_t; @@ -134,8 +132,6 @@ ast_node_init_binary_operation(ast_node_t *node, ast_binary_operation_kind_t kin 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_identifier(ast_node_t *node, string_view_t name); -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); diff --git a/src/gas_assembly_generator.c b/src/gas_assembly_generator.c index fca02ed..d9feedb 100644 --- a/src/gas_assembly_generator.c +++ b/src/gas_assembly_generator.c @@ -123,9 +123,6 @@ gas_assembly_generator_compile(gas_assembly_generator_t *gen, ast_node_t *ast) case AST_RETURN_STMT: gas_assembly_generator_compile_return_stmt(gen, &ast->data.return_stmt); break; - case AST_IDENTIFIER: - assert(false && "TODO: ast identifier not implemented yet"); - break; case AST_VARIABLE_DECLARATION: gas_assembly_generator_compile_variable_declaration(gen, &ast->data.variable_declaration); break; |