/* * 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 . */ #include "ast.h" #include #include #include ast_node_t * ast_node_new(void) { ast_node_t *node = (ast_node_t *)malloc(sizeof(ast_node_t)); if (node == NULL) { printf("OOM: could no allocate a node"); exit(EXIT_FAILURE); } node->kind = AST_UNKOWN_NODE; return node; } static void ast_node_destroy_vector(vector_t *vector) { for (size_t i = 0; i < vector->size; i++) { ast_node_destroy(vector_at(vector, i)); } vector_destroy(vector); } void ast_node_destroy(ast_node_t *node) { switch (node->kind) { case AST_FUNCTION_DECLARATION: ast_node_destroy_vector(node->data.function.body); break; case AST_BINARY_OPERATION: ast_node_destroy(node->data.binary_operation.left); ast_node_destroy(node->data.binary_operation.right); break; case AST_VARIABLE_DECLARATION: ast_node_destroy(node->data.variable_declaration.value); break; case AST_RETURN_STMT: ast_node_destroy(node->data.return_stmt.argument); break; case AST_VARIABLE_ASSIGNMENT: ast_node_destroy(node->data.variable_assignment.expression); case AST_LITERAL: case AST_UNKOWN_NODE: case AST_VARIABLE: break; } free(node); } ast_node_t * ast_node_new_return_stmt(ast_node_t *argument) { ast_node_t *node = ast_node_new(); *node = (ast_node_t){ .kind = AST_RETURN_STMT, .data = { .return_stmt = { .argument = argument } }, }; return node; } ast_node_t * ast_node_new_function_declaration(string_view_t function_name, type_t return_type, vector_t *body) { ast_node_t *node = ast_node_new(); *node = (ast_node_t){ .kind = AST_FUNCTION_DECLARATION, .data = { .function = { .identifier = { .name = function_name }, .return_type = return_type, .body = body, } }, }; return node; } ast_node_t * ast_node_new_literal_integer(uint32_t number) { ast_node_t *node = ast_node_new(); *node = (ast_node_t){ .kind = AST_LITERAL, .data = { .literal = { .kind = AST_LITERAL_INTEGER, .value = { .integer = number } }, }, }; return node; } ast_node_t * ast_node_new_binary_operation(ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right) { ast_node_t *node = ast_node_new(); *node = (ast_node_t){ .kind = AST_BINARY_OPERATION, .data = { .binary_operation = { .kind = kind, .left = left, .right = right, }, }, }; return node; } ast_node_t * ast_node_new_variable_declaration(string_view_t variable_name, type_t type, ast_node_t *value) { ast_node_t *node = ast_node_new(); *node = (ast_node_t){ .kind = AST_VARIABLE_DECLARATION, .data = { .variable_declaration = { .identifier = { .name = variable_name }, .type = type, .value = value, }, }, }; return node; } ast_node_t * ast_node_new_variable_assignment(ast_identifier_t *identifier, ast_node_t *expression) { ast_node_t *node = ast_node_new(); *node = (ast_node_t){ .kind = AST_VARIABLE_ASSIGNMENT, .data = { .variable_assignment = { .identifier = identifier, .expression = expression, }, }, }; return node; } ast_node_t * ast_node_new_variable(ast_identifier_t *identifier) { ast_node_t *node = ast_node_new(); *node = (ast_node_t){ .kind = AST_VARIABLE, .data = { .variable = { .identifier = identifier } }, }; return node; }