面向下划线编程

· · 科技·工程

你以为的下划线编程:

#include<iostream>
using namespace std;
int main()
{
    int _, __;
    cin >> _ >> __;
    cout << _ + __;
}

其实, 你们没有得到 kkk 的真传

真正的面向下划线编程往往只要下划线

图灵奖获得者图灵曾惊说过:"昨天, 你写的代码只有你和上底懂, 今天, 就只有编译器懂了"

我们改造一下, 就成了

#define ______________________________________________________ cin
#define _____________________________________________________ int
#define ____________________________________________________ std
#define ___________________________________________________ cout
#define __________________________________________________ main
#define _________________________________________________ using
#define ________________________________________________ namespace
#define ______________________________________________ ;
#define _____________________________________________ )
#define ____________________________________________ (
#define ___________________________________________ }
#define __________________________________________ {
#define _________________________________________ ]
#define ________________________________________ [
#define _______________________________________ |
#define ______________________________________ ,
#define _____________________________________ :
#define ____________________________________ ?
#define ___________________________________ =
#define __________________________________ /
#define _________________________________ +
#define ________________________________ -
#define _______________________________ ^
#define ______________________________ *
#define _____________________________ &
#define ____________________________ ^
#define ___________________________ %
#define __________________________ ~
#define _________________________ !
#define ________________________ ->
#define _______________________ --
#define ______________________ ++
#define _____________________ >>
#define ____________________ <<
#define ___________________ >>=
#define __________________ <<=
#define _________________ ==
#define ________________ >=
#define _______________ <=
#define ______________ /=
#define _____________ *=
#define ____________ -=
#define ___________ +=
#define __________ *=
#define _________ &=
#define ________ |=
#define _______ ^=
#define ______ %=
#define _____ !=
#define ____ ~=
#include <iostream>
 _________________________________________________   ________________________________________________   ____________________________________________________  ______________________________________________ 
 _____________________________________________________   __________________________________________________  ____________________________________________  _____________________________________________ 
 __________________________________________ 
     _____________________________________________________  _ ______________________________________  __ ______________________________________________ 
     ______________________________________________________   _____________________  _  _____________________  __ ______________________________________________ 
     ___________________________________________________   ____________________  _  _________________________________  __ ______________________________________________ 
 ___________________________________________ 

怎么实现的呢?

当然要打一个机器, 自动面向下划线编程只因:

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

int counts(string str, char ch)
{
    int count = 0;
    for (size_t i = 0; i < str.size(); ++i)
    {
        if (str[i] == ch && (i == 0 || str[i - 1] != '\\'))
        {
            count++;
        }
    }
    return count;
}

bool isInQuotes(const string &str, size_t pos)
{
    int count = 0;
    for (size_t i = 0; i < pos; ++i)
    {
        if (str[i] == '"' && (i == 0 || str[i - 1] != '\\'))
        {
            count++;
        }
    }
    return count % 2 != 0;
}

bool isInSmallQuotes(const string &str, size_t pos)
{
    int count = 0;
    for (size_t i = 0; i < pos; ++i)
    {
        if (str[i] == '\'' && (i == 0 || str[i - 1] != '\\'))
        {
            count++;
        }
    }
    return count % 2 != 0;
}

bool isLineStartsWithHash(const string &line)
{
    istringstream iss(line);
    string word;
    iss >> word;
    return !word.empty() && word[0] == '#';
}

void replaceAll(string &str, const string &search, const string &replace)
{
    istringstream iss(str);
    ostringstream oss;
    string line;
    while (getline(iss, line))
    {
        line += '\n';
        if (isLineStartsWithHash(line))
        {
            oss << line; // Keep lines starting with '#'
            continue;
        }
        size_t pos = 0;
        while ((pos = line.find(search, pos)) != string::npos)
        {
            if (!isInQuotes(line, pos) && !isInSmallQuotes(line, pos))
            {
                line.replace(pos, search.length(), replace);
                pos += replace.length();
            }
            else
            {
                pos += search.length();
            }
        }
        oss << line;
    }
    str = oss.str();
}

string turn(vector<pair<string, string>> tables, string contant)
{
    for (auto table : tables)
    {
        replaceAll(contant, table.first, table.second);
    }
    return contant;
}

string read(string file)
{
    ifstream in(file);
    stringstream buffer;
    buffer << in.rdbuf();
    in.close();
    return buffer.str();
}

bool comp(pair<string, string> __a, pair<string, string> __b)
{
    return __a.first.size() > __b.first.size();
}

void get_tables(vector<pair<string, string>> &tables, string str)
{
    int cnt = 2;
    while (!tables.empty())
    {
        tables.pop_back();
    }
    istringstream iss(str);
    string line;
    while (getline(iss, line))
    {
        if (isLineStartsWithHash(line))
        {
            continue;
        }
        string word, all;
        istringstream ls(line);
        char ch = 0, tmp = 0;
        while (ls >> word)
        {
            all.clear();
            ch = 0;
            for (char cc : word)
            {
                if (!ch && (cc == '"' || cc == '\''))
                {
                    ch = cc;
                }
                if (ch)
                {
                    all += cc;
                }
            }
            if (ch)
            {
                while (counts(all, ch) & 1)
                {
                    tmp = ls.get();
                    if (tmp == EOF)
                    {
                        cerr << "FUCK!";
                        exit(-1);
                    }
                    all += tmp;
                }
                bool flag = 1;
                for (auto table : tables)
                {
                    if (table.first == all)
                    {
                        flag = 0;
                        break;
                    }
                }
                if (flag)
                {
                    tables.push_back({all, string(++cnt, '_')});
                }
            }

            bool flag = 1;
            for (auto table : tables)
            {
                if (table.first == word)
                {
                    flag = 0;
                    break;
                }
            }
            if (flag)
            {
                tables.push_back({word, string(++cnt, '_')});
            }
        }
    }
    sort(tables.begin(), tables.end(), comp);
    for (auto &table : tables)
    {
        table.second = string(cnt--, '_');
    }
}

string clear_notes(string &str)
{
    istringstream iss(str);
    string line, res;
    unsigned pos;
    while (getline(iss, line))
    {
        if ((pos = line.find("//")) != string::npos)
        {
            res += line.substr(0, pos) + "\n";
        }
        else
        {
            res += line + "\n";
        }
    }
    return res;
}

int main(int argc, char *argv[])
{
    vector<pair<string, string>> tables;
    tables.push_back({">>=", " _1 "});
    tables.push_back({"<<=", " _2 "});
    tables.push_back({"->", " _3 "});
    tables.push_back({"==", " _4 "});
    tables.push_back({"+=", " _5 "});
    tables.push_back({"-=", " _6 "});
    tables.push_back({"*=", " _7 "});
    tables.push_back({"/=", " _8 "});
    tables.push_back({"%=", " _9 "});
    tables.push_back({"~=", " _10 "});
    tables.push_back({"!=", " _11 "});
    tables.push_back({"^=", " _12 "});
    tables.push_back({"&=", " _13 "});
    tables.push_back({"<=", " _14 "});
    tables.push_back({">=", " _15 "});
    tables.push_back({"|=", " _16 "});
    tables.push_back({"||", " _17 "});
    tables.push_back({"&&", " _18 "});
    tables.push_back({"++", " _19 "});
    tables.push_back({"--", " _20 "});
    tables.push_back({"...", " _21 "});
    tables.push_back({">>", " _22 "});
    tables.push_back({"<<", " _23 "});
    tables.push_back({"::", " _24 "});
    tables.push_back({"[", " [ "});
    tables.push_back({"]", " ] "});
    tables.push_back({"(", " ( "});
    tables.push_back({")", " ) "});
    tables.push_back({"{", " { "});
    tables.push_back({"}", " } "});
    tables.push_back({":", " : "});
    tables.push_back({";", " ; "});
    tables.push_back({",", " , "});
    tables.push_back({"?", " ? "});
    tables.push_back({"=", " = "});
    tables.push_back({"+", " + "});
    tables.push_back({"-", " - "});
    tables.push_back({"*", " * "});
    tables.push_back({"/", " / "});
    tables.push_back({"%", " % "});
    tables.push_back({"~", " ~ "});
    tables.push_back({"!", " ! "});
    tables.push_back({"^", " ^ "});
    tables.push_back({"&", " & "});
    tables.push_back({"<", " < "});
    tables.push_back({">", " > "});
    tables.push_back({"|", " | "});

    sort(tables.begin(), tables.end(), comp);

    string filein, fileout, contant;
    if (argc == 1)
    {
        cout << "Input file's name: ";
        getline(cin, filein);
        cout << "Output file's name: ";
        getline(cin, fileout);
    }
    else if (argc == 2)
    {
        filein = argv[1];
        cout << "Output file's name: ";
        getline(cin, fileout);
    }
    else
    {
        filein = argv[1];
        fileout = argv[2];
    }
    contant = read(filein);
    contant = clear_notes(contant);

    contant = turn(tables, contant);
    tables.clear();
    tables.push_back({"_1", " >>= "});
    tables.push_back({"_2", " <<= "});
    tables.push_back({"_3", " -> "});
    tables.push_back({"_4", " == "});
    tables.push_back({"_5", " += "});
    tables.push_back({"_6", " -= "});
    tables.push_back({"_7", " *= "});
    tables.push_back({"_8", " /= "});
    tables.push_back({"_9", " %= "});
    tables.push_back({"_10", " ~= "});
    tables.push_back({"_11", " != "});
    tables.push_back({"_12", " ^= "});
    tables.push_back({"_13", " &= "});
    tables.push_back({"_14", " <= "});
    tables.push_back({"_15", " >= "});
    tables.push_back({"_16", " |= "});
    tables.push_back({"_17", " || "});
    tables.push_back({"_18", " && "});
    tables.push_back({"_19", " ++ "});
    tables.push_back({"_20", " -- "});
    tables.push_back({"_21", " ... "});
    tables.push_back({"_22", " >> "});
    tables.push_back({"_23", " << "});
    tables.push_back({"_24", " :: "});
    sort(tables.begin(), tables.end(), comp);
    contant = turn(tables, contant);

    get_tables(tables, contant);
    contant = turn(tables, contant);
    string header;
    for (auto x : tables)
    {
        header += "#define " + x.second + " " + x.first + "\n";
    }
    contant = header + contant;
    ofstream out(fileout);
    out << contant;
    out.close();

    return 0;
}