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.h | |
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.h')
-rw-r--r-- | src/ast.h | 25 |
1 files changed, 21 insertions, 4 deletions
@@ -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 */ |