<commit_before>#include "compiler.h"
#include "CallList.h"
#include "Call.h"
#include "String.h"
#include "Number.h"
#include "helper.h"
#include <algorithm>
#include <locale>

std::size_t Compiler::lex() {
	lexed_ = CallList(code_);
	return 0;
}

unsigned int Compiler::getVar(const Variable& variable) {
	auto var = vars_.find(variable.getName());
	if (var == vars_.end()) {
		return (vars_[variable.getName()] = bf_.allocCell());
	}
	return var->second;
}

unsigned int Compiler::evaluateTo(Argument& arg) {
	switch (arg.getType()){
	case Argument::CALL:
	{
						   Call& c = static_cast<Call&>(arg);
						   auto function = std::find_if(std::begin(predef_functions), std::end(predef_functions),
							   [&c](std::pair<CallSignature, std::function<int(const Call&, unsigned int)>> k) -> bool {
							   return c.matches(k.first);
						   });
						   if (function != predef_functions.end()){
							   return function->second(c, bf_.allocCell());
						   }
						   else {
							   std::cerr << "Unknown function: " << c << std::endl;
						   }
	}
		break;
	case Argument::Type::VARIABLE:
		return getVar(static_cast<Variable&>(arg));
	case Argument::Type::INTEGER:
	{
									unsigned int t = bf_.allocCell();
									generated_ << bf_.set(t, static_cast<Number&>(arg).getValue());
									return t;
	}
	default:
		return 0;
	}
	return 0;
}

void Compiler::evaluateTo(Argument& arg, unsigned int target) {
	switch (arg.getType()){
	case Argument::CALL:
	{
						   Call& c = static_cast<Call&>(arg);
						   auto function = std::find_if(std::begin(predef_functions), std::end(predef_functions),
							   [&c](std::pair<CallSignature, std::function<int(const Call&, unsigned int)>> k) -> bool {
							   return c.matches(k.first);
						   });
						   if (function != predef_functions.end()){
							   generated_ << function->second(c, target);
						   }
						   else {
							   std::cerr << "Unknown function: " << c << std::endl;
						   }
	}
		break;
	case Argument::Type::VARIABLE:
		//TODO: In this case, the produced variable is temporary and should be treated as such
		generated_ << bf_.copy(getVar(static_cast<Variable&>(arg)), target);
		break;
	case Argument::Type::INTEGER:
		generated_ << bf_.set(target, static_cast<Number&>(arg).getValue());
		break;
	default:
		//TODO: Exception
		break;
	}
}

void Compiler::evaluate(Argument& arg) {<commit_msg>Fixed a bug that outputs a number in brainfuck code when compiling some functions.<commit_after>#include "compiler.h"
#include "CallList.h"
#include "Call.h"
#include "String.h"
#include "Number.h"
#include "helper.h"
#include <algorithm>
#include <locale>

std::size_t Compiler::lex() {
	lexed_ = CallList(code_);
	return 0;
}

unsigned int Compiler::getVar(const Variable& variable) {
	auto var = vars_.find(variable.getName());
	if (var == vars_.end()) {
		return (vars_[variable.getName()] = bf_.allocCell());
	}
	return var->second;
}

unsigned int Compiler::evaluateTo(Argument& arg) {
	switch (arg.getType()){
	case Argument::CALL:
	{
						   Call& c = static_cast<Call&>(arg);
						   auto function = std::find_if(std::begin(predef_functions), std::end(predef_functions),
							   [&c](std::pair<CallSignature, std::function<int(const Call&, unsigned int)>> k) -> bool {
							   return c.matches(k.first);
						   });
						   if (function != predef_functions.end()){
							   return function->second(c, bf_.allocCell());
						   }
						   else {
							   std::cerr << "Unknown function: " << c << std::endl;
						   }
	}
		break;
	case Argument::Type::VARIABLE:
		return getVar(static_cast<Variable&>(arg));
	case Argument::Type::INTEGER:
	{
									unsigned int t = bf_.allocCell();
									generated_ << bf_.set(t, static_cast<Number&>(arg).getValue());
									return t;
	}
	default:
		return 0;
	}
	return 0;
}

void Compiler::evaluateTo(Argument& arg, unsigned int target) {
	switch (arg.getType()){
	case Argument::CALL:
	{
						   Call& c = static_cast<Call&>(arg);
						   auto function = std::find_if(std::begin(predef_functions), std::end(predef_functions),
							   [&c](std::pair<CallSignature, std::function<int(const Call&, unsigned int)>> k) -> bool {
							   return c.matches(k.first);
						   });
						   if (function != predef_functions.end()){
							   function->second(c, target);
						   }
						   else {
							   std::cerr << "Unknown function: " << c << std::endl;
						   }
	}
		break;
	case Argument::Type::VARIABLE:
		//TODO: In this case, the produced variable is temporary and should be treated as such
		generated_ << bf_.copy(getVar(static_cast<Variable&>(arg)), target);
		break;
	case Argument::Type::INTEGER:
		generated_ << bf_.set(target, static_cast<Number&>(arg).getValue());
		break;
	default:
		//TODO: Exception
		break;
	}
}

void Compiler::evaluate(Argument& arg) {
{'fraction_non_alphanumeric': 0.16338786498239802, 'fraction_numerical': 0.0012424932698281218, 'mean_word_length': 3.394904458598726, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 37, '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': '3585073', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1574, 'n_words': 348}