From 219dfdfdc98529ce173f894b79d88919a65c2808 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Thu, 20 Apr 2023 13:42:10 -0300 Subject: 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 Reviewed-by: Johnny Richard --- src/ast.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/ast.c') diff --git a/src/ast.c b/src/ast.c index 50f6a2e..7f3af9e 100644 --- a/src/ast.c +++ b/src/ast.c @@ -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 + } + } + }; +} -- cgit v1.2.3