<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>

#include "ncore.h"
#include "builtins.h"
#include "mpc.h"
#include "mempool.h"

/* Constuctor and destructor for environment types */
nenv* nenv_new(void) {
    nenv* e = malloc(sizeof(nenv));
    e->par = NULL;
    e->count = 0;
    e->syms = NULL;
    e->vals = NULL;
    e->protected = NULL;
    return e;
}

void nenv_del(nenv* e) {
    for (int i = 0; i < e->count; i++) {
        free(e->syms[i]);
        nval_del(e->vals[i]);
    }
    free(e->syms);
    free(e->vals);
    free(e->protected);
    free(e);
}

/* Environment manipulation functions */
nval* nenv_get(nenv* e, nval* k) {
    for (int i = 0; i < e->count; i++) {
        if (strcmp(e->syms[i], k->sym) == 0) {
            return nval_copy(e->vals[i]);
        }
    }

    /* Check all parent environments if symbol not found */
    if (e->par) {
        return nenv_get(e->par, k);
    }
    return nval_err("Symbol '%s' not declared", k->sym);
}

bool nenv_add_val(nenv* e, nval* k, nval* v, bool p) {
    /* Check if variable already exists */
    for (int i = 0; i < e->count; i++) {
        if (strcmp(e->syms[i], k->sym) == 0) {
            if (!e->protected[i]) {
                nval_del(e->vals[i]);
                e->vals[i] = nval_copy(v);
                if (p) { e->protected[i] = true; }
                else { e->protected[i] = false; }
                return true;
            }
            return false;
        }
    }

    /* If not, create it */
    e->count++;
    e->vals = realloc(e->vals, sizeof(nval*) * e->count);
    e->syms = realloc(e->syms, sizeof(char*) * e->count);
    e->protected = realloc(e->protected, sizeof(bool) * e->count);

    e->vals[e->count-1] = nval_copy(v);
    e->syms[e->count-1] = malloc(strlen(k->sym)+1);
    if (p) { e->protected[e->count-1] = true; }
    else { e->protected[e->count-1] = false; }
    strcpy(e->syms[e->count-1], k->sym);
    return true;
}

bool nenv_put(nenv* e, nval* k, nval* v) {
    return nenv_add_val(e, k, v, false);
}

bool nenv_put_protected(nenv* e, nval* k, nval* v) {
    return nenv_add_val(e, k, v, true);
}

void nenv_rem(nenv* e, nval* k) {
    while (e->par) { e = e->par; }
    for (int i = 0; i < e->count; i++) {
        if (strcmp(e->syms[i], k->sym) == 0) {
            if (e->vals[i]->type == NVAL_FUN && e->vals[i]->builtin) {
                printf("Error: Cannot undefine builtin function\n");
                break;
            }
            free(e->syms[i]);
            nval_del(e->vals[i]);
            memmove(&e->syms[i], &e->syms[i+1], sizeof(char*) * (e->count-i-1));
            memmove(&e->vals[i], &e->vals[i+1], sizeof(nval*) * (e->count-i-1));
            e->count--;
            e->syms = realloc(e->syms, sizeof(char*) * e->count);
            e->vals = realloc(e->vals, sizeof(nval*) * e->count);
            return;
        }
    }
    return;
}<commit_msg>Constants cannot be undefined<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>

#include "ncore.h"
#include "builtins.h"
#include "mpc.h"
#include "mempool.h"

/* Constuctor and destructor for environment types */
nenv* nenv_new(void) {
    nenv* e = malloc(sizeof(nenv));
    e->par = NULL;
    e->count = 0;
    e->syms = NULL;
    e->vals = NULL;
    e->protected = NULL;
    return e;
}

void nenv_del(nenv* e) {
    for (int i = 0; i < e->count; i++) {
        free(e->syms[i]);
        nval_del(e->vals[i]);
    }
    free(e->syms);
    free(e->vals);
    free(e->protected);
    free(e);
}

/* Environment manipulation functions */
nval* nenv_get(nenv* e, nval* k) {
    for (int i = 0; i < e->count; i++) {
        if (strcmp(e->syms[i], k->sym) == 0) {
            return nval_copy(e->vals[i]);
        }
    }

    /* Check all parent environments if symbol not found */
    if (e->par) {
        return nenv_get(e->par, k);
    }
    return nval_err("Symbol '%s' not declared", k->sym);
}

bool nenv_add_val(nenv* e, nval* k, nval* v, bool p) {
    /* Check if variable already exists */
    for (int i = 0; i < e->count; i++) {
        if (strcmp(e->syms[i], k->sym) == 0) {
            if (!e->protected[i]) {
                nval_del(e->vals[i]);
                e->vals[i] = nval_copy(v);
                if (p) { e->protected[i] = true; }
                else { e->protected[i] = false; }
                return true;
            }
            return false;
        }
    }

    /* If not, create it */
    e->count++;
    e->vals = realloc(e->vals, sizeof(nval*) * e->count);
    e->syms = realloc(e->syms, sizeof(char*) * e->count);
    e->protected = realloc(e->protected, sizeof(bool) * e->count);

    e->vals[e->count-1] = nval_copy(v);
    e->syms[e->count-1] = malloc(strlen(k->sym)+1);
    if (p) { e->protected[e->count-1] = true; }
    else { e->protected[e->count-1] = false; }
    strcpy(e->syms[e->count-1], k->sym);
    return true;
}

bool nenv_put(nenv* e, nval* k, nval* v) {
    return nenv_add_val(e, k, v, false);
}

bool nenv_put_protected(nenv* e, nval* k, nval* v) {
    return nenv_add_val(e, k, v, true);
}

void nenv_rem(nenv* e, nval* k) {
    while (e->par) { e = e->par; }
    for (int i = 0; i < e->count; i++) {
        if (strcmp(e->syms[i], k->sym) == 0) {
            if (e->vals[i]->type == NVAL_FUN && e->vals[i]->builtin) {
                printf("Error: Cannot undefine builtin function\n");
                break;
            }
            if (e->protected[i]) {
                printf("Error: Cannot undefine constant\n");
                break;
            }
            free(e->syms[i]);
            nval_del(e->vals[i]);
            memmove(&e->syms[i], &e->syms[i+1], sizeof(char*) * (e->count-i-1));
            memmove(&e->vals[i], &e->vals[i+1], sizeof(nval*) * (e->count-i-1));
            e->count--;
            e->syms = realloc(e->syms, sizeof(char*) * e->count);
            e->vals = realloc(e->vals, sizeof(nval*) * e->count);
            return;
        }
    }
    return;
}
{'fraction_non_alphanumeric': 0.20246913580246914, 'fraction_numerical': 0.005925925925925926, 'mean_word_length': 2.054801407742584, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 158, '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': '1573480', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2339, 'n_words': 504}