<commit_before>#include <stdio.h>
#include <string.h> /* strlen */
#include <stdlib.h> /* free */

#include "value.h"
#include "output.h"
#include "plot.h"

/* plot_value deconstructor
 * correctly decr any values held within value
 */
void plot_value_decons(plot_value *value){
    switch( value->type ){
        case plot_type_string:
            /* currently plot strings are allocated via malloc/calloc
             * so must be freed
             * eventually we need to move strings under direct GC control
             */
            value->u.string.len = 0;
            value->u.string.size = 0;
            /* we should only get into decons for non-constant strings
             * so free-ing here is safe
             */
            free(value->u.string.val);
            value->u.string.val = 0;
            break;
        case plot_type_library:
            plot_env_decr(value->u.library.internal);
            value->u.library.internal = 0;
            plot_env_decr(value->u.library.exported);
            value->u.library.exported = 0;
            break;
        case plot_type_promise:
            plot_env_decr(value->u.promise.env);
            plot_value_decr(value->u.promise.expr);
            value->u.promise.expr = 0;
            plot_value_decr(value->u.promise.value);
            value->u.promise.value = 0;
            break;
        case plot_type_lambda:
            plot_env_decr(value->u.lambda.env);
            plot_value_decr(value->u.lambda.body);
            value->u.lambda.body = 0;
            break;
        case plot_type_pair:
            plot_value_decr(value->u.pair.car);
            value->u.pair.car = 0;
            plot_value_decr(value->u.pair.cdr);
            value->u.pair.cdr = 0;
            break;
        case plot_type_binary_port:
            if( value->u.binport.status == plot_port_open ){
                value->u.binport.status = plot_port_closed;
                fclose(value->u.binport.file);
                value->u.binport.file = 0;
            }
            break;
        case plot_type_textual_port:
            if( value->u.textport.status == plot_port_open ){
                value->u.textport.status = plot_port_closed;
                fclose(value->u.textport.file);
                value->u.textport.file = 0;
            }
            break;
        default:
            break;
    }
}

/* `*file` is an already opened file */
plot_value * plot_new_textual_port(plot_port_dir direction, FILE *file){
    plot_value *res;
    res = plot_alloc_value();
    res->type = plot_type_textual_port;

    res->u.textport.file = file;
    res->u.textport.direction = direction;
    res->u.textport.status = plot_port_open;

    return res;
}

/* `*file` is an already opened file */
plot_value * plot_new_binary_port(plot_port_dir direction, FILE *file){
    plot_value *res;
    res = plot_alloc_value();
    res->type = plot_type_binary_port;

    res->u.binport.file = file;
    res->u.binport.direction = direction;
    res->u.binport.status = plot_port_open;

    return res;
}

plot_value * plot_new_eof(void){
    return plot_get_eof_constant();
}

plot_value * plot_new_unspecified(void){
    return plot_get_unspecified_constant();
}

plot_value * plot_new_number(int value){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_number;
    res->u.number.val = value;

    return res;
}

plot_value * plot_new_boolean(int value){
    if( value ){
        return plot_get_true_constant();
    } else {
        return plot_get_false_constant();
    }
}

plot_value * plot_new_character(char value){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_character;
    res->u.character.val = value;

    return res;
}

plot_value * plot_new_symbol(char *val, int len){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_symbol;
    res->u.symbol.val = val;
    res->u.symbol.hash = 0;

    if( len >= 0 ){
        res->u.symbol.len = len;
    } else {
        res->u.symbol.len = strlen(val) + 1;
    }
    res->u.symbol.size = res->u.symbol.len;

    return res;
}

plot_value * plot_new_string(char * val, int len){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_string;
    res->u.string.val = val;

    if( len >= 0 ){
        res->u.string.len = len;
    } else {
        res->u.string.len = strlen(val) + 1;
    }
    res->u.string.size = res->u.string.len;

    return res;
}

/* allocate and construct a new pair with the supplied car and cdr
 * plot_new_pair will NOT incr supplied car or cdr
 */
plot_value * plot_new_pair(struct plot_value *car, struct plot_value *cdr){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_pair;
    res->u.pair.car = car;
    res->u.pair.cdr = cdr;
<commit_msg>changing plot_new_pair arg names to avoid shadowing<commit_after>#include <stdio.h>
#include <string.h> /* strlen */
#include <stdlib.h> /* free */

#include "value.h"
#include "output.h"
#include "plot.h"

/* plot_value deconstructor
 * correctly decr any values held within value
 */
void plot_value_decons(plot_value *value){
    switch( value->type ){
        case plot_type_string:
            /* currently plot strings are allocated via malloc/calloc
             * so must be freed
             * eventually we need to move strings under direct GC control
             */
            value->u.string.len = 0;
            value->u.string.size = 0;
            /* we should only get into decons for non-constant strings
             * so free-ing here is safe
             */
            free(value->u.string.val);
            value->u.string.val = 0;
            break;
        case plot_type_library:
            plot_env_decr(value->u.library.internal);
            value->u.library.internal = 0;
            plot_env_decr(value->u.library.exported);
            value->u.library.exported = 0;
            break;
        case plot_type_promise:
            plot_env_decr(value->u.promise.env);
            plot_value_decr(value->u.promise.expr);
            value->u.promise.expr = 0;
            plot_value_decr(value->u.promise.value);
            value->u.promise.value = 0;
            break;
        case plot_type_lambda:
            plot_env_decr(value->u.lambda.env);
            plot_value_decr(value->u.lambda.body);
            value->u.lambda.body = 0;
            break;
        case plot_type_pair:
            plot_value_decr(value->u.pair.car);
            value->u.pair.car = 0;
            plot_value_decr(value->u.pair.cdr);
            value->u.pair.cdr = 0;
            break;
        case plot_type_binary_port:
            if( value->u.binport.status == plot_port_open ){
                value->u.binport.status = plot_port_closed;
                fclose(value->u.binport.file);
                value->u.binport.file = 0;
            }
            break;
        case plot_type_textual_port:
            if( value->u.textport.status == plot_port_open ){
                value->u.textport.status = plot_port_closed;
                fclose(value->u.textport.file);
                value->u.textport.file = 0;
            }
            break;
        default:
            break;
    }
}

/* `*file` is an already opened file */
plot_value * plot_new_textual_port(plot_port_dir direction, FILE *file){
    plot_value *res;
    res = plot_alloc_value();
    res->type = plot_type_textual_port;

    res->u.textport.file = file;
    res->u.textport.direction = direction;
    res->u.textport.status = plot_port_open;

    return res;
}

/* `*file` is an already opened file */
plot_value * plot_new_binary_port(plot_port_dir direction, FILE *file){
    plot_value *res;
    res = plot_alloc_value();
    res->type = plot_type_binary_port;

    res->u.binport.file = file;
    res->u.binport.direction = direction;
    res->u.binport.status = plot_port_open;

    return res;
}

plot_value * plot_new_eof(void){
    return plot_get_eof_constant();
}

plot_value * plot_new_unspecified(void){
    return plot_get_unspecified_constant();
}

plot_value * plot_new_number(int value){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_number;
    res->u.number.val = value;

    return res;
}

plot_value * plot_new_boolean(int value){
    if( value ){
        return plot_get_true_constant();
    } else {
        return plot_get_false_constant();
    }
}

plot_value * plot_new_character(char value){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_character;
    res->u.character.val = value;

    return res;
}

plot_value * plot_new_symbol(char *val, int len){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_symbol;
    res->u.symbol.val = val;
    res->u.symbol.hash = 0;

    if( len >= 0 ){
        res->u.symbol.len = len;
    } else {
        res->u.symbol.len = strlen(val) + 1;
    }
    res->u.symbol.size = res->u.symbol.len;

    return res;
}

plot_value * plot_new_string(char * val, int len){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_string;
    res->u.string.val = val;

    if( len >= 0 ){
        res->u.string.len = len;
    } else {
        res->u.string.len = strlen(val) + 1;
    }
    res->u.string.size = res->u.string.len;

    return res;
}

/* allocate and construct a new pair with the supplied car and cdr
 * plot_new_pair will NOT incr supplied car or cdr
 */
plot_value * plot_new_pair(struct plot_value *left, struct plot_value *right){
    plot_value *res;
    res = plot_alloc_value();

    res->type = plot_type_pair;
    res->u.pair.car = left;
    res->u.pair.cdr = right;

{'fraction_non_alphanumeric': 0.12117452440033086, 'fraction_numerical': 0.003515301902398677, 'mean_word_length': 2.129407958589453, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 127, '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': '704097', 'n_tokens_mistral': 3246, 'n_tokens_neox': 3159, 'n_words': 640}