summaryrefslogtreecommitdiff
path: root/src/ast.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ast.c')
-rw-r--r--src/ast.c73
1 files changed, 53 insertions, 20 deletions
diff --git a/src/ast.c b/src/ast.c
index f8c2713..50f6a2e 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -15,6 +15,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
#include "ast.h"
void
@@ -26,38 +28,69 @@ ast_node_accept_visitor(ast_node_t *node, ast_visitor_t *visitor)
}
static void
-ast_function_accept_visitor(ast_node_t *node, ast_visitor_t *visitor)
+ast_node_function_accept_visitor(ast_node_t *node, ast_visitor_t *visitor)
{
- visitor->visit_function(visitor, (ast_function_t *) node);
+ visitor->visit_function(visitor, &node->data.function);
}
static void
-ast_return_stmt_accept_visitor(ast_node_t *node, ast_visitor_t *visitor)
+ast_node_return_stmt_accept_visitor(ast_node_t *node, ast_visitor_t *visitor)
{
- visitor->visit_return_stmt(visitor, (ast_return_stmt_t *) node);
+ visitor->visit_return_stmt(visitor, &node->data.return_stmt);
}
-ast_return_stmt_t
-ast_return_stmt_create(uint32_t number)
+ast_node_t*
+ast_node_new()
{
- return (ast_return_stmt_t) {
- .super = (ast_node_t) {
- .accept_visitor = &ast_return_stmt_accept_visitor
- },
- .number = number
+ ast_node_t *node = (ast_node_t*) malloc(sizeof(ast_node_t));
+ if (node == NULL) {
+ printf("OOO: could no allocate a node");
+ exit(EXIT_FAILURE);
+ }
+ node->kind = AST_UNKOWN_NODE;
+ return node;
+}
+
+void
+ast_node_destroy(ast_node_t *node)
+{
+ switch (node->kind) {
+ case AST_FUNCTION_DECLARATION:
+ ast_node_destroy(node->data.function.body);
+ break;
+ case AST_RETURN_STMT:
+ break;
+ case AST_UNKOWN_NODE:
+ break;
+ default:
+ assert(false && "unmapped free strategy");
+ }
+ free(node);
+}
+
+void
+ast_node_init_return_stmt(ast_node_t *node, uint32_t number)
+{
+ node->accept_visitor = &ast_node_return_stmt_accept_visitor,
+ node->kind = AST_RETURN_STMT;
+ node->data = (ast_node_data_t) {
+ .return_stmt = {
+ .number = number
+ }
};
}
-ast_function_t
-ast_function_create(string_view_t name, type_t return_type, ast_return_stmt_t body)
+void
+ast_node_init_function_declaration(ast_node_t *node, string_view_t name, type_t return_type, ast_node_t* body)
{
- return (ast_function_t) {
- .super = (ast_node_t) {
- .accept_visitor = &ast_function_accept_visitor
- },
- .name = name,
- .return_type = return_type,
- .body = body
+ node->accept_visitor = &ast_node_function_accept_visitor,
+ node->kind = AST_FUNCTION_DECLARATION;
+ node->data = (ast_node_data_t) {
+ .function = {
+ .name = name,
+ .return_type = return_type,
+ .body = body
+ }
};
}