<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wannabe-lisp.h"

char buf[128];

void printout(list_t *l, char *s)
{
	extern void printout_iter(list_t* l, int d, char* s);
	printout_iter(l, 0, s);
}

void printout_iter(list_t* l, int d, char *out)
{
	int i;

	/* switch based on type */
	if (l->type == NUMBER) {
		sprintf(buf, "%d", l->val);
		strcat(out, buf);
	} else if(l->type == SYMBOL) {
		sprintf(buf, "%s", l->head);
		strcat(out, buf);
	} else if (l->type == LIST) { 
		sprintf(buf, "(%s", l->head);
		strcat(out, buf);
		for (i = 0; i < l->cc; ++i) {
			if (i > 0) {
				sprintf(buf, " ");
				strcat(out, buf);
			}
			printout_iter(l->c[i], d + 1, out);
		}
		sprintf(buf, ")");
		strcat(out, buf);
	} else if (l->type == CLOSURE) {
		sprintf(buf, "#<CLOSURE:%p>", l->closure);
		strcat(out, buf);
	} else if (l->type == BOOL) {
		if (l->val)
			sprintf(buf, "#t");
		else
			sprintf(buf, "#f");
		strcat(out, buf);
	} else if (l->type == CONS) {
		if (l->cc == 2 && l->c[1]->cc == 0 && 
			!(l->c[1]->type == SYMBOL && !strcmp(l->c[1]->head, "NIL"))) {
			sprintf(buf, "(");
			strcat(out, buf);
			printout_iter(l->c[0], d + 1, out);
			/* (cons 'a '()) => (a)
			 * r6rs.pdf, section 11.9, page 48
			 */
			if (!(l->c[1]->type == CONS && l->c[1]->cc == 0)) {
				sprintf(buf, " . ");
				strcat(out, buf);
				printout_iter(l->c[1], d + 1, out);
			}
			sprintf(buf, ")");
			strcat(out, buf);
		} else if (l->cc) {
			sprintf(buf, "(");
			strcat(out, buf);
			while (1) {
				printout_iter(l->c[0], d + 1, out);
				if (l->cc == 2 && l->c[1]->cc) {
					sprintf(buf, " ");
					strcat(out, buf);
					l = l->c[1];
				} else
					break;
			}
			sprintf(buf, ")");
			strcat(out, buf);
		} else {
			sprintf(buf, "()");
			strcat(out, buf);
		}
	}
}


<commit_msg>Fix printing of cons/lists containing closures or lists as elements<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wannabe-lisp.h"

char buf[128];

void printout(list_t *l, char *s)
{
	extern void printout_iter(list_t* l, int d, char* s);
	printout_iter(l, 0, s);
}

void printout_iter(list_t* l, int d, char *out)
{
	int i;

	/* switch based on type */
	if (l->type == NUMBER) {
		sprintf(buf, "%d", l->val);
		strcat(out, buf);
	} else if(l->type == SYMBOL) {
		sprintf(buf, "%s", l->head);
		strcat(out, buf);
	} else if (l->type == LIST) { 
		sprintf(buf, "(%s", l->head);
		strcat(out, buf);
		for (i = 0; i < l->cc; ++i) {
			if (i > 0) {
				sprintf(buf, " ");
				strcat(out, buf);
			}
			printout_iter(l->c[i], d + 1, out);
		}
		sprintf(buf, ")");
		strcat(out, buf);
	} else if (l->type == CLOSURE) {
		sprintf(buf, "#<CLOSURE:%p>", l->closure);
		strcat(out, buf);
	} else if (l->type == BOOL) {
		if (l->val)
			sprintf(buf, "#t");
		else
			sprintf(buf, "#f");
		strcat(out, buf);
	} else if (l->type == CONS) {
		if (l->cc == 2
			&& (l->c[1]->type == SYMBOL && !strcmp(l->c[1]->head, "NIL")
				|| (l->c[1]->type == CONS && l->c[1]->cc == 0))) {
			sprintf(buf, "(");
			strcat(out, buf);
			printout_iter(l->c[0], d + 1, out);
			sprintf(buf, ")");
			strcat(out, buf);
		}
		else
		if (l->cc == 2 && (l->c[1]->cc != 2 || l->c[1]->type != CONS)) {
			sprintf(buf, "(");
			strcat(out, buf);
			printout_iter(l->c[0], d + 1, out);
			/* (cons 'a '()) => (a)
			 * r6rs.pdf, section 11.9, page 48
			 */
			if (!(l->c[1]->type == CONS && l->c[1]->cc == 0)) {
				sprintf(buf, " . ");
				strcat(out, buf);
				printout_iter(l->c[1], d + 1, out);
			}
			sprintf(buf, ")");
			strcat(out, buf);
		} else if (l->cc) {
			sprintf(buf, "(");
			strcat(out, buf);
			while (1) {
				if (l->cc == 2 && l->c[1]->cc) {
					printout_iter(l->c[0], d + 1, out);
					sprintf(buf, " ");
					strcat(out, buf);
					l = l->c[1];
				} else {
					if (l->cc == 2
						&& (l->c[1]->type == SYMBOL && !strcmp(l->c[1]->head, "NIL")
						|| (l->c[1]->type == CONS && l->c[1]->cc == 0))) {
						printout_iter(l->c[0], d + 1, out);
					} else { 
						printout_iter(l, d + 1, out);
					}
					break;
				}
			}
			sprintf(buf, ")");
			strcat(out, buf);
		} else {
			sprintf(buf, "()");
			strcat(out, buf);
		}
	}
}



{'fraction_non_alphanumeric': 0.25868263473053893, 'fraction_numerical': 0.01868263473053892, 'mean_word_length': 2.695575221238938, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 97, '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': '4168686', 'n_tokens_mistral': 2172, 'n_tokens_neox': 1875, 'n_words': 405}