diff options
author | Carlos Maniero <carlosmaniero@gmail.com> | 2023-04-29 15:55:39 -0300 |
---|---|---|
committer | Carlos Maniero <carlosmaniero@gmail.com> | 2023-04-29 19:02:38 -0300 |
commit | 9f034b4ebfe15844ea610ec2dd2fca1a9f7ce338 (patch) | |
tree | bb415654b8af0dac33fdce6537f0ab32234c3a20 /src | |
parent | 817fbfef34908931ebeaa4da1a8d21ef79572e5a (diff) |
ast: Introduce ast_identifier_t for named ast nodes
Prior to this change, ast_variable_declaration_t and
ast_function_declaration_t used a string_view as an identifier. However,
to support scoped identifiers, it is more appropriate to use an
ast_identifier_t as a reference.
Signed-off-by: Carlos Maniero <carlosmaniero@gmail.com>
Co-authored-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/ast.c | 9 | ||||
-rw-r--r-- | src/ast.h | 16 | ||||
-rw-r--r-- | src/gas_assembly_generator.c | 2 |
3 files changed, 15 insertions, 12 deletions
@@ -77,10 +77,12 @@ ast_node_init_return_stmt(ast_node_t *node, ast_node_t *argument) } void -ast_node_init_function_declaration(ast_node_t *node, string_view_t name, type_t return_type, vector_t *body) +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 = { .name = name, .return_type = return_type, .body = body } }; + node->data = (ast_node_data_t){ + .function = { .identifier = { .name = function_name }, .return_type = return_type, .body = body } + }; } void @@ -101,7 +103,8 @@ 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 = { .name = variable_name, .type = type, .value = value } }; + node->data = + (ast_node_data_t){ .variable = { .identifier = { .name = variable_name }, .type = type, .value = value } }; } void @@ -32,9 +32,14 @@ typedef struct ast_return_stmt_t ast_node_t *argument; } ast_return_stmt_t; -typedef struct ast_function_declaration_t +typedef struct ast_identifier_t { string_view_t name; +} ast_identifier_t; + +typedef struct ast_function_declaration_t +{ + ast_identifier_t identifier; type_t return_type; vector_t *body; } ast_function_declaration_t; @@ -70,14 +75,9 @@ typedef struct ast_literal_t ast_literal_value_t value; } ast_literal_t; -typedef struct ast_identifier_t -{ - string_view_t name; -} ast_identifier_t; - typedef struct ast_variable_declaration_t { - string_view_t name; + ast_identifier_t identifier; type_t type; ast_node_t *value; } ast_variable_declaration_t; @@ -117,7 +117,7 @@ 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 name, type_t return_type, vector_t *body); +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 diff --git a/src/gas_assembly_generator.c b/src/gas_assembly_generator.c index 34864dd..ea76c5f 100644 --- a/src/gas_assembly_generator.c +++ b/src/gas_assembly_generator.c @@ -71,7 +71,7 @@ gas_assembly_generator_compile_function(gas_assembly_generator_t *gen, ast_funct { assert(func); - if (!string_view_eq(func->name, string_view_from_str("main"))) { + if (!string_view_eq(func->identifier.name, string_view_from_str("main"))) { fprintf(stderr, "[ERROR]: no main function has been defined!\n"); exit(EXIT_FAILURE); } |