summaryrefslogtreecommitdiff
path: root/src/ast.h
diff options
context:
space:
mode:
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 */