<commit_before>#include "cfl_eval.h"

#include <stdio.h>
#include <string.h>

void* cfl_eval_malloc(size_t size)
{
    void* result = malloc(size);

    if(!result)
    {
        fprintf(stderr, "MEMORY ERROR: Ran out of memory "
                        "during evaluation\n");
    }

    return result;
}

int cfl_substitute(cfl_node* target, char* variable, cfl_node* value)
{
    int i;

    switch(target->type)
    {
        case CFL_NODE_VARIABLE:
            if(!strcmp(target->data, variable))
            {
                cfl_delete_node(target);

                if(!cfl_copy_node(target, value))
                    return 0;
            }
            break;
        case CFL_NODE_FUNCTION:
            if(strcmp(target->children[0]->data, variable))
                if(!cfl_substitute(target->children[1], variable, value))
                    return 0;
            break;
        case CFL_NODE_LET_REC:
            if(strcmp(target->children[0]->data, variable))
            {
                if(strcmp(target->children[1]->data, variable))
                {
                    if(!cfl_substitute(target->children[2], variable, value) ||
                       !cfl_substitute(target->children[3], variable, value))
                        return 0;
                }
                else if(!cfl_substitute(target->children[3], variable, value))
                    return 0;
            }
            break;
        case CFL_NODE_CASE:
            if(!cfl_substitute(target->children[0], variable, value) ||
               !cfl_substitute(target->children[1], variable, value))
                return 0;

            if(strcmp(target->children[2]->data, variable) &&
               strcmp(target->children[3]->data, variable))
                    if(!cfl_substitute(target->children[4], variable, value))
                        return 0;
            break;
        default:
            for(i = 0; i < target->number_of_children; ++i)
                if(!cfl_substitute(target->children[i], variable, value))
                    return 0;
            break;
    }

    return 1;
}

int cfl_evaluate(cfl_node* node)
{
    if(node->type == CFL_NODE_LIST)
    {
        cfl_list_node* pos = node->data;

        while(pos)
        {
            if(!cfl_evaluate(pos->node))
                return 0;

            pos = pos->next;
        }
    }
    else if(node->type == CFL_NODE_AND)
    {
        if(!cfl_evaluate(node->children[0]))
            return 0;

        if(!*((bool*) node->children[0]->data))
        {
            cfl_delete_node(node);

            cfl_create_node_bool(node, false);

            return 1;
        }

        if(!cfl_evaluate(node->children[1]))
            return 0;

        cfl_delete_node(node);

        cfl_create_node_bool(node, *((bool*) node->children[1]->data));
    }
    else if(node->type == CFL_NODE_OR)
    {
        if(!cfl_evaluate(node->children[0]))
            return 0;

        if(*((bool*) node->children[0]->data))
        {
            cfl_delete_node(node);

            cfl_create_node_bool(node, true);

            return 1;
        }

        if(!cfl_evaluate(node->children[1]))
            return 0;

        cfl_delete_node(node);

        cfl_create_node_bool(node, *((bool*) node->children[1]->data));
    }
    else if(node->type == CFL_NODE_NOT)
    {
        if(!cfl_evaluate(node->children[0]))
            return 0;
<commit_msg>Fixed memory bug in eval<commit_after>#include "cfl_eval.h"

#include <stdio.h>
#include <string.h>

void* cfl_eval_malloc(size_t size)
{
    void* result = malloc(size);

    if(!result)
    {
        fprintf(stderr, "MEMORY ERROR: Ran out of memory "
                        "during evaluation\n");
    }

    return result;
}

int cfl_substitute(cfl_node* target, char* variable, cfl_node* value)
{
    int i;

    switch(target->type)
    {
        case CFL_NODE_VARIABLE:
            if(!strcmp(target->data, variable))
            {
                cfl_delete_node(target);

                if(!cfl_copy_node(target, value))
                    return 0;
            }
            break;
        case CFL_NODE_FUNCTION:
            if(strcmp(target->children[0]->data, variable))
                if(!cfl_substitute(target->children[1], variable, value))
                    return 0;
            break;
        case CFL_NODE_LET_REC:
            if(strcmp(target->children[0]->data, variable))
            {
                if(strcmp(target->children[1]->data, variable))
                {
                    if(!cfl_substitute(target->children[2], variable, value) ||
                       !cfl_substitute(target->children[3], variable, value))
                        return 0;
                }
                else if(!cfl_substitute(target->children[3], variable, value))
                    return 0;
            }
            break;
        case CFL_NODE_CASE:
            if(!cfl_substitute(target->children[0], variable, value) ||
               !cfl_substitute(target->children[1], variable, value))
                return 0;

            if(strcmp(target->children[2]->data, variable) &&
               strcmp(target->children[3]->data, variable))
                    if(!cfl_substitute(target->children[4], variable, value))
                        return 0;
            break;
        default:
            for(i = 0; i < target->number_of_children; ++i)
                if(!cfl_substitute(target->children[i], variable, value))
                    return 0;
            break;
    }

    return 1;
}

int cfl_evaluate(cfl_node* node)
{
    if(node->type == CFL_NODE_LIST)
    {
        cfl_list_node* pos = node->data;

        while(pos)
        {
            if(!cfl_evaluate(pos->node))
                return 0;

            pos = pos->next;
        }
    }
    else if(node->type == CFL_NODE_AND)
    {
        if(!cfl_evaluate(node->children[0]))
            return 0;

        if(!*((bool*) node->children[0]->data))
        {
            cfl_delete_node(node);

            cfl_create_node_bool(node, false);

            return 1;
        }

        if(!cfl_evaluate(node->children[1]))
            return 0;

        bool result = *((bool*) node->children[1]->data);

        cfl_delete_node(node);

        cfl_create_node_bool(node, result);
    }
    else if(node->type == CFL_NODE_OR)
    {
        if(!cfl_evaluate(node->children[0]))
            return 0;

        if(*((bool*) node->children[0]->data))
        {
            cfl_delete_node(node);

            cfl_create_node_bool(node, true);

            return 1;
        }

        if(!cfl_evaluate(node->children[1]))
            return 0;

        bool result = *((bool*) node->children[1]->data);

        cfl_delete_node(node);

        cfl_create_node_bool(node, result);
    }
    else if(node->type == CFL_NODE_NOT)
    {
        if(!cfl_evaluate(node->children[0]))
            return 0;

{'fraction_non_alphanumeric': 0.12918175235336712, 'fraction_numerical': 0.011006517016654598, 'mean_word_length': 1.616900341038272, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 89, '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': '1493938', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2056, 'n_words': 348}