<commit_before>#include "hof.h"

#include "cache.h"
#include "combinators.h"
#include "colors.h"
#include "verbose.h"

bool evaluationListIsWellFormed(const EvaluationList& list)
{
    EvaluationList::const_iterator it = list.begin();
    for (; it != list.end(); ++it) {
        if ((*it)->type() == Combinator::a_)
            return static_cast<const A*>((*it).data())->isWellFormed();
    }
    return true;
}

void cppInterpreter(const QString& string)
{
    Verbose::instance()->generateProgramString("program: " + string);
    Verbose::instance()->generateProgramString("begin", true /*replace*/);
    int postfixIndex = Verbose::instance()->addPostfix(string);

    if (string.isEmpty()) {
        Verbose::instance()->generateProgramString("end", true /*replace*/);
        return;
    }

    EvaluationList evaluationList;
    for (int x = 0; x < string.length(); x++) {
        CombinatorPtr term;
        QChar ch = string.at(x);
        switch (ch.unicode()) {
        case 'I': term = i(); break;
        case 'K': term = k(); break;
        case 'S': term = s(); break;
        case 'P': term = p(); break;
        case 'R': term = r(); break;
        case 'A': term = CombinatorPtr(new A); break;
        default:
            QString error = QString("Error: invalid char in hof program! ch=`%1`").arg(ch);
            Q_ASSERT_X(false, "hof", qPrintable(error));
            return;
        }

        if (evaluationList.isEmpty()) {
            evaluationList.append(term);
            continue;
        }

        if (term->type() == Combinator::a_) {
            if (evaluationList.back()->type() == Combinator::a_) {
                A* a = static_cast<A*>(evaluationList.back().data());
                Q_ASSERT(!a->isWellFormed());
                a->addCombinator(term);
            } else {
                evaluationList.append(term);
            }

            continue; // can't possibly be well formed at this point
        }

        if (evaluationList.back()->type() == Combinator::a_) {
            A* a = static_cast<A*>(evaluationList.back().data());
            Q_ASSERT(!a->isWellFormed());
            a->addCombinator(term);
            term = CombinatorPtr(); // remove the term since it was added
        }

        if (!evaluationListIsWellFormed(evaluationList))
            continue;

        CombinatorPtr evaluate = evaluationList.takeFirst();
        if (Verbose::instance()->isVerbose()) {
            Verbose::instance()->removePostfix(postfixIndex);
            postfixIndex = Verbose::instance()->addPostfix(string.right(string.length() - x - 1));

            SubEval subEval;
            subEval.addPostfix(!term.isNull() ? term->toString() : QString());

            EvaluationList::const_iterator it = evaluationList.begin();
            for (; it != evaluationList.end(); ++it)
                evaluate = eval(evaluate, *it);

            subEval.clear();
        } else {
            EvaluationList::const_iterator it = evaluationList.begin();
            for (; it != evaluationList.end(); ++it)
                evaluate = eval(evaluate, *it);
        }

        if (!term.isNull())
            evaluate = eval(evaluate, term);

        while (evaluate->type() == Combinator::a_) {<commit_msg>Fix verbose postfix of top-level evaluations.<commit_after>#include "hof.h"

#include "cache.h"
#include "combinators.h"
#include "colors.h"
#include "verbose.h"

bool evaluationListIsWellFormed(const EvaluationList& list)
{
    EvaluationList::const_iterator it = list.begin();
    for (; it != list.end(); ++it) {
        if ((*it)->type() == Combinator::a_)
            return static_cast<const A*>((*it).data())->isWellFormed();
    }
    return true;
}

void cppInterpreter(const QString& string)
{
    Verbose::instance()->generateProgramString("program: " + string);
    Verbose::instance()->generateProgramString("begin", true /*replace*/);
    int postfixIndex = Verbose::instance()->addPostfix(string);

    if (string.isEmpty()) {
        Verbose::instance()->generateProgramString("end", true /*replace*/);
        return;
    }

    EvaluationList evaluationList;
    for (int x = 0; x < string.length(); x++) {
        CombinatorPtr term;
        QChar ch = string.at(x);
        switch (ch.unicode()) {
        case 'I': term = i(); break;
        case 'K': term = k(); break;
        case 'S': term = s(); break;
        case 'P': term = p(); break;
        case 'R': term = r(); break;
        case 'A': term = CombinatorPtr(new A); break;
        default:
            QString error = QString("Error: invalid char in hof program! ch=`%1`").arg(ch);
            Q_ASSERT_X(false, "hof", qPrintable(error));
            return;
        }

        if (evaluationList.isEmpty()) {
            evaluationList.append(term);
            continue;
        }

        if (term->type() == Combinator::a_) {
            if (evaluationList.back()->type() == Combinator::a_) {
                A* a = static_cast<A*>(evaluationList.back().data());
                Q_ASSERT(!a->isWellFormed());
                a->addCombinator(term);
            } else {
                evaluationList.append(term);
            }

            continue; // can't possibly be well formed at this point
        }

        if (evaluationList.back()->type() == Combinator::a_) {
            A* a = static_cast<A*>(evaluationList.back().data());
            Q_ASSERT(!a->isWellFormed());
            a->addCombinator(term);
            term = CombinatorPtr(); // remove the term since it was added
        }

        if (!evaluationListIsWellFormed(evaluationList))
            continue;

        CombinatorPtr evaluate = evaluationList.takeFirst();
        if (Verbose::instance()->isVerbose()) {
            Verbose::instance()->removePostfix(postfixIndex);
            postfixIndex = Verbose::instance()->addPostfix(string.right(string.length() - x - 1));

            SubEval subEval;
            subEval.addPostfix(!term.isNull() ? term->toString() : QString());

            EvaluationList::const_iterator it = evaluationList.begin();
            for (; it != evaluationList.end(); ++it)
                evaluate = eval(evaluate, *it);
        } else {
            EvaluationList::const_iterator it = evaluationList.begin();
            for (; it != evaluationList.end(); ++it)
                evaluate = eval(evaluate, *it);
        }

        if (!term.isNull())
            evaluate = eval(evaluate, term);

        while (evaluate->type() == Combinator::a_) {
{'fraction_non_alphanumeric': 0.15727916281932902, 'fraction_numerical': 0.0009233610341643582, 'mean_word_length': 2.2707599396074483, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, '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': '1486354', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1841, 'n_words': 402}