/*
* 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;
}
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_NAMESPACE:
ast_node_destroy_vector(node->data.ns.nodes);
break;
case AST_FUNCTION_DECLARATION:
ast_node_destroy(node->data.function.body);
break;
case AST_IF_STMT:
ast_node_destroy(node->data.if_stmt.condition);
ast_node_destroy(node->data.if_stmt.body);
break;
case AST_BLOCK:
ast_node_destroy_vector(node->data.block.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){
.result_type = TYPE_VOID,
.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, ast_node_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_namespace(vector_t *nodes)
{
ast_node_t *node = ast_node_new();
*node = (ast_node_t){
.kind = AST_NAMESPACE,
.result_type = TYPE_VOID,
.data = {
.ns = {
.nodes = nodes,
}
},
};
return node;
}
ast_node_t *
ast_node_new_block(vector_t *body)
{
ast_node_t *node = ast_node_new();
*node = (ast_node_t){
.kind = AST_BLOCK,
.data = {
.block = {
.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,
.result_type = TYPE_I32,
.data = {
.literal = {
.kind = AST_LITERAL_INTEGER,
.value = { .integer = number }
},
},
};
return node;
}
ast_node_t *
ast_node_new_if_stmt(ast_node_t *condition, ast_node_t *body)
{
ast_node_t *node = ast_node_new();
*node = (ast_node_t){
.kind = AST_IF_STMT,
.result_type = TYPE_VOID,
.data = {
.if_stmt = {
.condition = condition,
.body = body
},
},
};
return node;
}
ast_node_t *
ast_node_new_literal_bool(bool boolean)
{
ast_node_t *node = ast_node_new();
*node = (ast_node_t){
.kind = AST_LITERAL,
.result_type = TYPE_BOOL,
.data = {
.literal = {
.kind = AST_LITERAL_BOOL,
.value = { .boolean = boolean }
},
},
};
return node;
}
ast_node_t *
ast_node_new_binary_operation(ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right, type_t result_type)
{
ast_node_t *node = ast_node_new();
*node = (ast_node_t){
.kind = AST_BINARY_OPERATION,
.result_type = result_type,
.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,
.result_type = TYPE_VOID,
.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,
.result_type = TYPE_VOID,
.data = {
.variable_assignment = {
.identifier = identifier,
.expression = expression,
},
},
};
return node;
}
ast_node_t *
ast_node_new_variable(ast_identifier_t *identifier, type_t result_type)
{
ast_node_t *node = ast_node_new();
*node = (ast_node_t){
.kind = AST_VARIABLE,
.result_type = result_type,
.data = { .variable = { .identifier = identifier } },
};
return node;
}
ast_node_t *
ast_node_ns_get_function_node_by_sv(ast_node_t *ns, string_view_t name)
{
assert(ns->kind == AST_NAMESPACE);
for (size_t i = 0; i < ns->data.ns.nodes->size; i++) {
ast_node_t *node = vector_at(ns->data.ns.nodes, i);
if (node->kind == AST_FUNCTION_DECLARATION && string_view_eq(node->data.function.identifier.name, name)) {
return node;
}
}
return NULL;
}
ast_node_t *
ast_node_ns_get_function_node_by_name(ast_node_t *ns, char *function_name)
{
return ast_node_ns_get_function_node_by_sv(ns, string_view_from_str(function_name));
}
char *
ast_type_to_str(type_t type)
{
switch (type) {
case TYPE_I32:
return "i32";
case TYPE_BOOL:
return "bool";
case TYPE_VOID:
return "void";
}
assert(false);
}