summaryrefslogtreecommitdiff
path: root/src/ast.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ast.c')
-rw-r--r--src/ast.c87
1 files changed, 27 insertions, 60 deletions
diff --git a/src/ast.c b/src/ast.c
index 645a2dd..10bb79d 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -1,23 +1,23 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+#include "ast.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
-#include "ast.h"
void
ast_node_accept_visitor(ast_node_t *node, ast_visitor_t *visitor)
@@ -55,10 +55,10 @@ ast_node_binary_operation_visitor(ast_node_t *node, ast_visitor_t *visitor)
visitor->visit_binary_operation(visitor, &node->data.binary_operation);
}
-ast_node_t*
+ast_node_t *
ast_node_new()
{
- ast_node_t *node = (ast_node_t*) malloc(sizeof(ast_node_t));
+ 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);
@@ -70,7 +70,7 @@ ast_node_new()
static void
ast_node_destroy_vector(vector_t *vector)
{
- for (size_t i=0; i < vector->size; i++) {
+ for (size_t i = 0; i < vector->size; i++) {
ast_node_destroy(vector_at(vector, i));
}
vector_destroy(vector);
@@ -108,27 +108,17 @@ ast_node_destroy(ast_node_t *node)
void
ast_node_init_return_stmt(ast_node_t *node, ast_node_t *argument)
{
- node->accept_visitor = &ast_node_return_stmt_accept_visitor,
+ node->accept_visitor = &ast_node_return_stmt_accept_visitor;
node->kind = AST_RETURN_STMT;
- node->data = (ast_node_data_t) {
- .return_stmt = {
- .argument = argument
- }
- };
+ node->data.return_stmt = (ast_return_stmt_t){ .argument = argument };
}
void
ast_node_init_function_declaration(ast_node_t *node, string_view_t name, type_t return_type, vector_t *body)
{
- node->accept_visitor = &ast_node_function_accept_visitor,
+ 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
- }
- };
+ node->data = (ast_node_data_t){ .function = { .name = name, .return_type = return_type, .body = body } };
}
void
@@ -136,14 +126,7 @@ ast_literal_integer_create(ast_node_t *node, uint32_t number)
{
node->accept_visitor = &ast_node_literal_visitor;
node->kind = AST_LITERAL;
- node->data = (ast_node_data_t) {
- .literal = {
- .kind = AST_LITERAL_INTEGER,
- .value = {
- .integer = number
- }
- }
- };
+ node->data.literal = (ast_literal_t){ .kind = AST_LITERAL_INTEGER, .value = { .integer = number } };
}
void
@@ -151,13 +134,7 @@ ast_node_init_binary_operation(ast_node_t *node, string_view_t op, ast_node_t *l
{
node->accept_visitor = &ast_node_binary_operation_visitor;
node->kind = AST_BINARY_OPERATION;
- node->data = (ast_node_data_t) {
- .binary_operation = {
- .op = op,
- .left = left,
- .right = right
- }
- };
+ node->data = (ast_node_data_t){ .binary_operation = { .op = op, .left = left, .right = right } };
}
void
@@ -165,13 +142,7 @@ ast_node_init_variable_declaration(ast_node_t *node, string_view_t variable_name
{
// FIXME: define the visitor strategy
node->kind = AST_VARIABLE_DECLARATION;
- node->data = (ast_node_data_t) {
- .variable = {
- .name = variable_name,
- .type = type,
- .value = value
- }
- };
+ node->data = (ast_node_data_t){ .variable = { .name = variable_name, .type = type, .value = value } };
}
void
@@ -179,9 +150,5 @@ ast_node_init_identifier(ast_node_t *node, string_view_t name)
{
// FIXME: define the visitor strategy
node->kind = AST_IDENTIFIER;
- node->data = (ast_node_data_t) {
- .variable = {
- .name = name,
- }
- };
+ node->data.identifier = (ast_identifier_t){ .name = name };
}