<commit_before>#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "bel-ast.h"

bel_ast_node* bel_new_ast_node_token(bel_ast_token_type type) {
    bel_ast_node* node;
    node = malloc(sizeof(bel_ast_node));
    node->token = malloc(sizeof(bel_ast_node_token));
    node->token->type  = BEL_TOKEN;
    node->token->ttype = type;
    node->token->left  = NULL;
    node->token->right = NULL;
    return node;
};

bel_ast_node* bel_new_ast_node_value(bel_ast_value_type type, char* value) {
    bel_ast_node* node;
    char*         copy_value;

    if (value) {
        copy_value = malloc(strlen(value) + 1);
        strcpy(copy_value, value);
    } else {
        copy_value = NULL;
    }

    node = malloc(sizeof(bel_ast_node));
    node->value = malloc(sizeof(bel_ast_node_value));
    node->value->type  = BEL_VALUE;
    node->value->vtype = type;
    node->value->value = copy_value;
    return node;
};

bel_ast_node* bel_copy_ast_node(bel_ast_node* node) {
	bel_ast_node* copy_node;
    char* copy_value;

    if (!node) {
        return NULL;
	}

	copy_node = malloc(sizeof(bel_ast_node));

    if (node->type_info->type == BEL_VALUE) {
		if (node->value->value) {
			copy_value = malloc(strlen(node->value->value) + 1);
			strcpy(copy_value, node->value->value);
		} else {
			copy_value = NULL;
		}

		copy_node->value = malloc(sizeof(bel_ast_node_value));
		copy_node->value->type  = node->value->type;
		copy_node->value->vtype = node->value->vtype;
		copy_node->value->value = copy_value;
    } else {
		copy_node->token = malloc(sizeof(bel_ast_node_token));
		copy_node->token->type  = node->token->type;
		copy_node->token->ttype = node->token->ttype;
		copy_node->token->left  = bel_copy_ast_node(node->token->left);
		copy_node->token->right = bel_copy_ast_node(node->token->right);
	}

	return copy_node;
}

bel_ast_node* bel_set_value(bel_ast_node* node, char* value) {
    char* copy_value;

    if (!node) {
        // TODO Debug node error to stderr; node is NULL
        return NULL;
    }

    if (node->type_info->type != BEL_VALUE) {
        // TODO Debug node error to stderr; node cannot hold value
        return NULL;
    }

    if (value) {
        copy_value = malloc(strlen(value) + 1);
        strcpy(copy_value, value);
    } else {
        copy_value = NULL;
    }

	node->value->value = copy_value;
	return node;
}

bel_ast* bel_new_ast() {
    bel_ast* ast;
    ast = malloc(sizeof(bel_ast));
    ast->root = NULL;
    return ast;
};

void bel_free_ast(bel_ast* ast) {
    if (!ast) {
        return;
    }
    bel_free_ast_node(ast->root);
    free(ast);
};

void bel_free_ast_node(bel_ast_node* node) {
    if (node->type_info->type == BEL_TOKEN) {
        if (node->token->left != NULL) {
            bel_free_ast_node(node->token->left);
        }
        if (node->token->right != NULL) {
            bel_free_ast_node(node->token->right);
        }
	free(node->token);
    } else {
	free(node->value->value);
	free(node->value);
    }<commit_msg>do not free bel_ast_node if NULL pointer<commit_after>#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "bel-ast.h"

bel_ast_node* bel_new_ast_node_token(bel_ast_token_type type) {
    bel_ast_node* node;
    node = malloc(sizeof(bel_ast_node));
    node->token = malloc(sizeof(bel_ast_node_token));
    node->token->type  = BEL_TOKEN;
    node->token->ttype = type;
    node->token->left  = NULL;
    node->token->right = NULL;
    return node;
};

bel_ast_node* bel_new_ast_node_value(bel_ast_value_type type, char* value) {
    bel_ast_node* node;
    char*         copy_value;

    if (value) {
        copy_value = malloc(strlen(value) + 1);
        strcpy(copy_value, value);
    } else {
        copy_value = NULL;
    }

    node = malloc(sizeof(bel_ast_node));
    node->value = malloc(sizeof(bel_ast_node_value));
    node->value->type  = BEL_VALUE;
    node->value->vtype = type;
    node->value->value = copy_value;
    return node;
};

bel_ast_node* bel_copy_ast_node(bel_ast_node* node) {
	bel_ast_node* copy_node;
    char* copy_value;

    if (!node) {
        return NULL;
	}

	copy_node = malloc(sizeof(bel_ast_node));

    if (node->type_info->type == BEL_VALUE) {
		if (node->value->value) {
			copy_value = malloc(strlen(node->value->value) + 1);
			strcpy(copy_value, node->value->value);
		} else {
			copy_value = NULL;
		}

		copy_node->value = malloc(sizeof(bel_ast_node_value));
		copy_node->value->type  = node->value->type;
		copy_node->value->vtype = node->value->vtype;
		copy_node->value->value = copy_value;
    } else {
		copy_node->token = malloc(sizeof(bel_ast_node_token));
		copy_node->token->type  = node->token->type;
		copy_node->token->ttype = node->token->ttype;
		copy_node->token->left  = bel_copy_ast_node(node->token->left);
		copy_node->token->right = bel_copy_ast_node(node->token->right);
	}

	return copy_node;
}

bel_ast_node* bel_set_value(bel_ast_node* node, char* value) {
    char* copy_value;

    if (!node) {
        // TODO Debug node error to stderr; node is NULL
        return NULL;
    }

    if (node->type_info->type != BEL_VALUE) {
        // TODO Debug node error to stderr; node cannot hold value
        return NULL;
    }

    if (value) {
        copy_value = malloc(strlen(value) + 1);
        strcpy(copy_value, value);
    } else {
        copy_value = NULL;
    }

	node->value->value = copy_value;
	return node;
}

bel_ast* bel_new_ast() {
    bel_ast* ast;
    ast = malloc(sizeof(bel_ast));
    ast->root = NULL;
    return ast;
};

void bel_free_ast(bel_ast* ast) {
    if (!ast) {
        return;
    }
    bel_free_ast_node(ast->root);
    free(ast);
};

void bel_free_ast_node(bel_ast_node* node) {
    if(!node) {
        return;
    }
    if (node->type_info->type == BEL_TOKEN) {
        if (node->token->left != NULL) {
            bel_free_ast_node(node->token->left);
        }
        if (node->token->right != NULL) {
            bel_free_ast_node(node->token->right);
        }
        free(node->token);
    } else {
        free(node->value->value);
        free(node->value);
    }
{'fraction_non_alphanumeric': 0.14782038675843986, 'fraction_numerical': 0.0009832841691248771, 'mean_word_length': 2.9425064599483206, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 153, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5165185', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2211, 'n_words': 390}