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.h | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src/ast.h') diff --git a/src/ast.h b/src/ast.h index fab90c1..1c4bbb8 100644 --- a/src/ast.h +++ b/src/ast.h @@ -29,7 +29,7 @@ typedef struct ast_visitor_t ast_visitor_t; typedef struct ast_node_t ast_node_t; typedef struct ast_return_stmt_t { - uint32_t number; + ast_node_t* argument; } ast_return_stmt_t; typedef struct ast_function_declaration_t { @@ -38,19 +38,35 @@ typedef struct ast_function_declaration_t { ast_node_t* body; } ast_function_declaration_t; +typedef enum { + AST_LITERAL_INTEGER +} ast_literal_kind_t; + +typedef union { + uint32_t integer; +} ast_literal_value_t; + +typedef struct ast_literal_t { + ast_literal_kind_t kind; + ast_literal_value_t value; +} ast_literal_t; + typedef struct ast_visitor_t { void (*visit_function)(struct ast_visitor_t *, ast_function_declaration_t *); void (*visit_return_stmt)(struct ast_visitor_t *, ast_return_stmt_t *); + void (*visit_literal)(struct ast_visitor_t *, ast_literal_t *); } ast_visitor_t; typedef enum { - AST_UNKOWN_NODE, AST_FUNCTION_DECLARATION, - AST_RETURN_STMT + AST_LITERAL, + AST_RETURN_STMT, + AST_UNKOWN_NODE } ast_node_kind_t; typedef union { ast_function_declaration_t function; + ast_literal_t literal; ast_return_stmt_t return_stmt; } ast_node_data_t; @@ -66,7 +82,8 @@ ast_node_t* ast_node_new(); void ast_node_destroy(ast_node_t *node); void ast_node_init_function_declaration(ast_node_t* node, string_view_t name, type_t return_type, ast_node_t *body); -void ast_node_init_return_stmt(ast_node_t* node, uint32_t number); +void ast_node_init_return_stmt(ast_node_t* node, ast_node_t *argument); +void ast_literal_integer_create(ast_node_t* node, uint32_t number); #endif /* AST_H */ -- cgit v1.2.3