summaryrefslogtreecommitdiff
path: root/src/ast.h
diff options
context:
space:
mode:
authorCarlos Maniero <carlosmaniero@gmail.com>2023-04-20 11:05:54 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-04-20 16:14:14 +0200
commita47e5ceb6eefdac9c5f5473e1fee0d33a5f4646e (patch)
tree20beb62b87998ec301d461175c8500f222101a75 /src/ast.h
parent547643d357aadfcb402ec9b951e23975da8593cc (diff)
ast: Allows recursive nodes
Previously, the abstract syntax tree (AST) used static types, meaning that an ast_function_t would always have a ast_return_stmt_t as its body. However, this assumption is not always true, as we may have void functions that do not have a return statement. Additionally, the ast_return_stmt_t always had a number associated with it, but this too is not always the case. To make this possible, I need to perform a few changes in the whole project. One of the main changes is that there is no longer the inheritance hack. That mechanism was replaced by composition and pointers where required for recursive type reference. It is important to mention that I decided to use union type to implement the composition. There is two main advantages in this approach: 1. There is only one function to allocate memory for all kind of nodes. 2. There is no need to cast the data. In summary, this commit introduces changes to support dynamic typing in the AST, by replacing the inheritance hack with composition and using union types to simplify memory allocation and type casting. 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.h41
1 files changed, 28 insertions, 13 deletions
diff --git a/src/ast.h b/src/ast.h
index 7f5a198..fab90c1 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -19,39 +19,54 @@
#include <stdint.h>
#include "string_view.h"
-#define ast_visitor_visit(visitor, node) ast_node_accept_visitor((ast_node_t *) node, (ast_visitor_t *) visitor);
+#define ast_visitor_visit(visitor, node) ast_node_accept_visitor(node, (ast_visitor_t *) visitor);
typedef enum {
TYPE_I32
} type_t;
typedef struct ast_visitor_t ast_visitor_t;
-
-typedef struct ast_node_t {
- void (*accept_visitor)(struct ast_node_t *, ast_visitor_t *);
-} ast_node_t;
+typedef struct ast_node_t ast_node_t;
typedef struct ast_return_stmt_t {
- struct ast_node_t super;
uint32_t number;
} ast_return_stmt_t;
-typedef struct ast_function_t {
- struct ast_node_t super;
+typedef struct ast_function_declaration_t {
string_view_t name;
type_t return_type;
- ast_return_stmt_t body;
-} ast_function_t;
+ ast_node_t* body;
+} ast_function_declaration_t;
typedef struct ast_visitor_t {
- void (*visit_function)(struct ast_visitor_t *, ast_function_t *);
+ void (*visit_function)(struct ast_visitor_t *, ast_function_declaration_t *);
void (*visit_return_stmt)(struct ast_visitor_t *, ast_return_stmt_t *);
} ast_visitor_t;
+typedef enum {
+ AST_UNKOWN_NODE,
+ AST_FUNCTION_DECLARATION,
+ AST_RETURN_STMT
+} ast_node_kind_t;
+
+typedef union {
+ ast_function_declaration_t function;
+ ast_return_stmt_t return_stmt;
+} ast_node_data_t;
+
+typedef struct ast_node_t {
+ void (*accept_visitor)(ast_node_t *, ast_visitor_t *);
+ ast_node_kind_t kind;
+ ast_node_data_t data;
+} ast_node_t;
+
void ast_node_accept_visitor(ast_node_t *node, ast_visitor_t *visitor);
-ast_function_t ast_function_create(string_view_t name, type_t return_type, ast_return_stmt_t body);
-ast_return_stmt_t ast_return_stmt_create(uint32_t number);
+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);
#endif /* AST_H */