diff options
author | Carlos Maniero <carlosmaniero@gmail.com> | 2023-04-20 13:42:10 -0300 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-04-20 18:57:43 +0200 |
commit | 219dfdfdc98529ce173f894b79d88919a65c2808 (patch) | |
tree | 8792c686d05b107875d43f570ed6666f91dda19e /src/ast.c | |
parent | 58da5195ef7d517f851ebfabfe79cfa27c0b2dde (diff) |
parser: Create the literal node type
Since we want to extend our code to support multiple kind of expression
it does not make sense that the return statement always return a number.
For now on, return statement has an ast_node_t as argument, meaning that
it could be anything. The literal_node_t was also implemented in order
to keep the application behavior.
Following the C's calling convention the literal values are stored at
%eax and the return takes this argument to do anything it is needed.
Signed-off-by: Carlos Maniero <carlosmaniero@gmail.com>
Reviewed-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'src/ast.c')
-rw-r--r-- | src/ast.c | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -39,6 +39,12 @@ ast_node_return_stmt_accept_visitor(ast_node_t *node, ast_visitor_t *visitor) visitor->visit_return_stmt(visitor, &node->data.return_stmt); } +static void +ast_node_literal_visitor(ast_node_t *node, ast_visitor_t *visitor) +{ + visitor->visit_literal(visitor, &node->data.literal); +} + ast_node_t* ast_node_new() { @@ -62,6 +68,8 @@ ast_node_destroy(ast_node_t *node) break; case AST_UNKOWN_NODE: break; + case AST_LITERAL: + break; default: assert(false && "unmapped free strategy"); } @@ -69,13 +77,13 @@ ast_node_destroy(ast_node_t *node) } void -ast_node_init_return_stmt(ast_node_t *node, uint32_t number) +ast_node_init_return_stmt(ast_node_t *node, ast_node_t *argument) { node->accept_visitor = &ast_node_return_stmt_accept_visitor, node->kind = AST_RETURN_STMT; node->data = (ast_node_data_t) { .return_stmt = { - .number = number + .argument = argument } }; } @@ -94,3 +102,17 @@ ast_node_init_function_declaration(ast_node_t *node, string_view_t name, type_t }; } +void +ast_literal_integer_create(ast_node_t *node, uint32_t number) +{ + node->accept_visitor = &ast_node_literal_visitor; + node->kind = AST_LITERAL; + node->data = (ast_node_data_t) { + .literal = { + .kind = AST_LITERAL_INTEGER, + .value = { + .integer = number + } + } + }; +} |