<commit_before>#ifndef HISTORY_H
#define HISTORY_H

#include"objects.hpp"

#define BOOST_CB_DISABLE_DEBUG

#include <boost/circular_buffer.hpp>

/*
 * Keep the history of the last function calls
 */
class History {
private:
	boost::circular_buffer<Object::ref> ring;
	typedef boost::circular_buffer<Object::ref>::reverse_iterator ring_it;
public:
	History(size_t sz) : ring(sz) {}

	// record a function (closure) call -- may delete the oldest call
	inline void enter(Object::ref clos) {
		ring.push_back(clos);
	}

	// record a function return (i.e. a continuation call)
	inline void leave() {
		ring.pop_back();
	}

	// push a list of the last functions called in the process stack
	void to_list(Process & proc);

	// traverse the history
	void traverse(GenericTraverser* gt);
};

void History::to_list(Process & proc) {
	size_t count = 0;
	for (ring_it i = ring.rbegin(); i != ring.rend(); ++i) {
		proc.stack.push(*i);
		count++;
	}
	proc.stack.push(Object::nil());
	// build the list
	while (count-- > 0) {
		bytecode_cons(proc, proc.stack);
	}
}

void History::traverse(GenericTraverser *gt) {
	for (ring_it i = ring.rbegin(); i != ring.rend(); ++i) {
		gt->traverse(*i);
	}<commit_msg>fixed history to take tail calls into account<commit_after>#ifndef HISTORY_H
#define HISTORY_H

#include"objects.hpp"

#define BOOST_CB_DISABLE_DEBUG

#include <boost/circular_buffer.hpp>

/*
 * Keep the history of the last function calls
 */
class History {
private:
	typedef boost::circular_buffer<Object::ref> inner_ring;
	typedef boost::circular_buffer<inner_ring> outer_ring;
	outer_ring ring;

public:
	History(size_t depth, size_t breadth) : ring(sz) {}

	// record a function (closure) call -- may delete the oldest call
	inline void enter(Object::ref clos) {
		ring.push_back(outer_ring(breadth, 1, clos));
	}

	// record a function call in tail position
	inline void enter_tail(Object::ref clos) {
		inner_ring::iterator last = ring.end()-1;
		last->push_back(clos);
	}

	// record a function return (i.e. a continuation call)
	inline void leave() {
		ring.pop_back();
	}

	// push a list of the last functions called in the process stack
	void to_list(Process & proc);

	// traverse the history
	void traverse(GenericTraverser* gt);
};

void History::to_list(Process & proc) {
	size_t count = 0;
	for (outer_ring::reverse_iterator i = ring.rbegin(); i != ring.rend(); ++i) {
		for (inner_ring::reverse_iterator j = i->rbegin(); j != i->rend(); ++i) {
			proc.stack.push(*j);
			count++;
		}
	}
	proc.stack.push(Object::nil());
	// build the list
	while (count-- > 0) {
		bytecode_cons(proc, proc.stack);
	}
}

void History::traverse(GenericTraverser *gt) {
	for (outer_ring::reverse_iterator i = ring.rbegin(); i != ring.rend(); ++i) {
		for (inner_ring::reverse_iterator j = i->rbegin(); j != i->rend(); ++i) {
			gt->traverse(*j);
		}
	}
{'fraction_non_alphanumeric': 0.14883884588318086, 'fraction_numerical': 0.00211118930330753, 'mean_word_length': 4.323970037453184, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, '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': '5237707', 'n_tokens_mistral': 1037, 'n_tokens_neox': 987, 'n_words': 267}