<commit_before>#include <assert.h>
#include <stdio.h>
#include "object.h"
#include "output.h"

void write_object(object *obj, FILE *f, int h);
void write_pair(object *obj, FILE *f, int h);
void write_vector(object *obj, FILE *f, int h);

void write(object *obj, FILE *f) {
    write_object(obj, f, 0);
}

void display(object *obj, FILE *f) {
    write_object(obj, f, 1);
}

void write_object(object *obj, FILE *f, int h) {
    if (is_null(obj))
        fprintf(f, "()");
    else if (is_locked(obj))
        fprintf(f, "...");
    else if (is_number(obj))
        fprintf(f, "%g", to_number(obj));
    else if (is_string(obj) && h)
        fprintf(f, "%s", to_string(obj));
    else if (is_string(obj))
        fprintf(f, "\"%s\"", to_string(obj));
    else if (is_symbol(obj))
        fprintf(f, "%s", to_symbol(obj));
    else if (is_primitive(obj))
        fprintf(f, "#[primitive-procedure]");
    else if (is_compound(obj))
        fprintf(f, "#[compound-procedure]");
    else if (is_pair(obj))
        write_pair(obj, f, h);
    else if (is_vector(obj))
        write_vector(obj, f, h);
}

void write_pair(object *obj, FILE *f, int h) {
    object *iter;

    obj->locked = 1;
    iter = obj;
    fprintf(f, "(");
    do {
        write_object(car(iter), f, h);
        iter = cdr(iter);
        if (iter)
            fprintf(f, " ");
    } while (is_pair(iter));
    if (iter) {
        fprintf(f, ". ");
        write_object(iter, f, h);
    }
    fprintf(f, ")");
    obj->locked = 0;
}

void write_vector(object *obj, FILE *f, int h) {
    int k;
<commit_msg>Resolved infinite print loops for self-referencing pairs.<commit_after>#include <assert.h>
#include <stdio.h>
#include "object.h"
#include "output.h"

void write_object(object *obj, FILE *f, int h);
void write_pair(object *obj, FILE *f, int h);
void write_vector(object *obj, FILE *f, int h);

void write(object *obj, FILE *f) {
    write_object(obj, f, 0);
}

void display(object *obj, FILE *f) {
    write_object(obj, f, 1);
}

void write_object(object *obj, FILE *f, int h) {
    if (is_null(obj))
        fprintf(f, "()");
    else if (is_locked(obj))
        fprintf(f, "...");
    else if (is_number(obj))
        fprintf(f, "%g", to_number(obj));
    else if (is_string(obj) && h)
        fprintf(f, "%s", to_string(obj));
    else if (is_string(obj))
        fprintf(f, "\"%s\"", to_string(obj));
    else if (is_symbol(obj))
        fprintf(f, "%s", to_symbol(obj));
    else if (is_primitive(obj))
        fprintf(f, "#[primitive-procedure]");
    else if (is_compound(obj))
        fprintf(f, "#[compound-procedure]");
    else if (is_pair(obj))
        write_pair(obj, f, h);
    else if (is_vector(obj))
        write_vector(obj, f, h);
}

void write_pair(object *obj, FILE *f, int h) {
    object *iter;

    obj->locked = 1;
    iter = obj;
    fprintf(f, "(");
    do {
        write_object(car(iter), f, h);
        iter = cdr(iter);
        if (iter)
            fprintf(f, " ");
    } while (is_pair(iter) && !is_locked(iter));
    if (iter) {
        fprintf(f, ". ");
        write_object(iter, f, h);
    }
    fprintf(f, ")");
    obj->locked = 0;
}

void write_vector(object *obj, FILE *f, int h) {
    int k;

{'fraction_non_alphanumeric': 0.18258426966292135, 'fraction_numerical': 0.0024968789013732834, 'mean_word_length': 2.7661574618096356, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '3544389', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1218, 'n_words': 305}