<commit_before>#ifndef ATL_PRINT_HH
#define ATL_PRINT_HH
/**
 * @file /home/ryan/programming/atl/print.hpp
 * @author <NAME> <<EMAIL>>
 * Created on Aug 10, 2013
 */
#include <functional>
#include <iostream>
#include <utility>

#include "./utility.hpp"
#include "./helpers.hpp"

namespace atl
{
	namespace printer
	{
		struct Printer
		{
			virtual std::ostream& print(std::ostream&) const = 0;
		};

		typedef ::Range<typename Ast::const_iterator> Range;

		struct PrintRange : public Printer
		{
			Range _range;
			char _delim_open, _delim_close;

			PrintRange(Range const& range, char delim_open, char delim_close)
				: _range(range),
				  _delim_open(delim_open),
				  _delim_close(delim_close)
			{}

			virtual std::ostream& print(std::ostream&) const;
		};

		struct PrintAny : public Printer
		{
			const Any &a;
			PrintAny(const Any &a_) : a(a_) {}
			virtual std::ostream& print(std::ostream&) const;
		};


		PrintRange range(Range const& rr, char open='(', char close=')')
		{ return PrintRange(rr, open, close); }

		PrintAny any(Any const& aa) { return PrintAny(aa); }

		std::ostream& operator<<(std::ostream& out, const printer::Printer& p) { return p.print(out); }

		std::ostream& PrintRange::print(std::ostream& out) const
		{
			if(_range.empty())
				return out << _delim_open << _delim_close << endl;
			else {
				out << _delim_open << printer::any(_range[0]) << flush;
				for(auto& vv : slice(_range, 1)) out << " " << printer::any(vv);
				return out << _delim_close;
			}
		}

		std::ostream& PrintAny::print(std::ostream& out) const
		{
			auto trim_addr = [](void *pntr)
				{ return reinterpret_cast<long>(pntr) & (256 - 1); };

			using namespace std;
			switch(a._tag)
				{
				case tag<Undefined>::value:
					{
						out << "#<Undefined ";
						return out << ":" << hex << trim_addr(a.value) << ">";
					}

				case tag<Symbol>::value:
					return out << "'" << unwrap<string>(a);
				case tag<Type>::value:
					return out << "#{" << unwrap<Type>(a).value << "}";
				case tag<Fixnum>::value:
					return out << value<Fixnum>(a);
				case tag<Bool>::value:
					{
						if(value<bool>(a))
							return out << "True";
						else
							return out << "False";
					}
				case tag<String>::value:
					return out << '"' << *reinterpret_cast<string*>(a.value) << '"';
				case tag<CxxFunctor>::value:
					return out << unwrap<CxxFunctor>(a)._name;
				case tag<Pointer>::value:
					{
						if(a.value)
							return out << "#<Pointer-" << hex << (reinterpret_cast<long>(a.value) & (256 - 1)) << ">";
						else
							return out << "#<Pointer-NULL>";
					}
				case tag<Ast>::value:
					return out << range(make_range(unwrap<Ast>(a)));
				case tag<AstData>::value:
					return out << range(make_range(unwrap<AstData>(a)));
				case tag<Slice>::value:
					return out << range(make_range(unwrap<Slice>(a)), '[', ']');
				case tag<DefProcedure>::value:
					{
						out << "#<DefProcedure" << flush;
						auto closure = unwrap<DefProcedure>(a).closure;
						if(!closure.empty())
							{
								out << " (" << closure.size() << " free vars)";
							}
						return out << ">" << flush;;
					}
				default:
					return out << "#<" << type_name(a._tag) << ">";
				}
		}
	}

	void dbg_any(Any vv)
	{ cout << printer::any(vv) << endl; }

	void dbg_pbv(PassByValue value)
	{ return dbg_any(value.as_Any()); }

	void dbg_ast(Ast const& vv)
	{ cout << printer::range(make_range(vv)) << endl; }

}


#endif
<commit_msg>Print type information<commit_after>#ifndef ATL_PRINT_HH
#define ATL_PRINT_HH
/**
 * @file /home/ryan/programming/atl/print.hpp
 * @author <NAME> <<EMAIL>>
 * Created on Aug 10, 2013
 */
#include <functional>
#include <iostream>
#include <utility>

#include "./utility.hpp"
#include "./helpers.hpp"

namespace atl
{
	namespace printer
	{
		struct Printer
		{
			virtual std::ostream& print(std::ostream&) const = 0;
		};

		typedef ::Range<typename Ast::const_iterator> Range;

		struct PrintRange : public Printer
		{
			Range _range;
			char _delim_open, _delim_close;

			PrintRange(Range const& range, char delim_open, char delim_close)
				: _range(range),
				  _delim_open(delim_open),
				  _delim_close(delim_close)
			{}

			virtual std::ostream& print(std::ostream&) const;
		};

		struct PrintAny : public Printer
		{
			const Any &a;
			PrintAny(const Any &a_) : a(a_) {}
			virtual std::ostream& print(std::ostream&) const;
		};


		PrintRange range(Range const& rr, char open='(', char close=')')
		{ return PrintRange(rr, open, close); }

		PrintAny any(Any const& aa) { return PrintAny(aa); }

		std::ostream& operator<<(std::ostream& out, const printer::Printer& p) { return p.print(out); }

		std::ostream& PrintRange::print(std::ostream& out) const
		{
			if(_range.empty())
				return out << _delim_open << _delim_close << endl;
			else {
				out << _delim_open << printer::any(_range[0]) << flush;
				for(auto& vv : slice(_range, 1)) out << " " << printer::any(vv);
				return out << _delim_close;
			}
		}

		std::ostream& PrintAny::print(std::ostream& out) const
		{
			auto trim_addr = [](void *pntr)
				{ return reinterpret_cast<long>(pntr) & (256 - 1); };

			using namespace std;
			switch(a._tag)
				{
				case tag<Undefined>::value:
					{
						out << "#<Undefined ";
						return out << ":" << hex << trim_addr(a.value) << ">";
					}

				case tag<Symbol>::value:
					return out << "'" << unwrap<string>(a);
				case tag<Type>::value:
					return out << "#{" << unwrap<Type>(a).value << "}";
				case tag<Fixnum>::value:
					return out << value<Fixnum>(a);
				case tag<Bool>::value:
					{
						if(value<bool>(a))
							return out << "True";
						else
							return out << "False";
					}
				case tag<String>::value:
					return out << '"' << *reinterpret_cast<string*>(a.value) << '"';
				case tag<CxxFunctor>::value:
					return out << unwrap<CxxFunctor>(a)._name;
				case tag<Pointer>::value:
					{
						if(a.value)
							return out << "#<Pointer-" << hex << (reinterpret_cast<long>(a.value) & (256 - 1)) << ">";
						else
							return out << "#<Pointer-NULL>";
					}
				case tag<Ast>::value:
					return out << range(make_range(unwrap<Ast>(a)));
				case tag<AstData>::value:
					return out << range(make_range(unwrap<AstData>(a)));
				case tag<Slice>::value:
					return out << range(make_range(unwrap<Slice>(a)), '[', ']');
				case tag<DefProcedure>::value:
					{
						out << "#<DefProcedure" << flush;
						auto closure = unwrap<DefProcedure>(a).closure;
						if(!closure.empty())
							{
								out << " (" << closure.size() << " free vars)";
							}
						return out << ">" << flush;;
					}
				default:
					return out << "#<" << type_name(a._tag) << ">";
				}
		}
	}

	void dbg_any(Any vv)
	{ cout << printer::any(vv) << endl; }

	void dbg_pbv(PassByValue value)
	{ return dbg_any(value.as_Any()); }

	void dbg_ast(Ast const& vv)
	{ cout << printer::range(make_range(vv)) << endl; }

	std::ostream& print_type(PassByValue const& value, std::ostream& out)
	{
		switch(value._tag)
			{
			case tag<Ast>::value:
				{
					auto ast = unwrap<Ast>(value);
					out << "(";
					if(!ast.empty())
						{
							print_type(ast[0], out);
							for(auto& vv : slice(ast, 1))
								{
									out << ' ';
									print_type(vv, out);
								}
						}
					return out << ")";
				}
			case tag<Type>::value:
				{ return out << "#<Type: " << unwrap<Type>(value).value << ">"; }
			default:
				return out << type_name(value._tag);
			}
	}
	void dbg_type(PassByValue const& value)
	{ print_type(value, std::cout) << std::endl; }
}


#endif

{'fraction_non_alphanumeric': 0.18404016382613292, 'fraction_numerical': 0.0047562425683709865, 'mean_word_length': 3.102981029810298, 'pattern_counts': {'":': 2, '<': 292, '<?xml version=': 0, '>': 80, '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': '2420413', 'n_tokens_mistral': 3383, 'n_tokens_neox': 2621, 'n_words': 552}