Created
October 23, 2018 14:35
-
-
Save bobpaw/a636bc8968c11e4c285ef3de807b16b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
class hold { | |
private: | |
std::vector<hold> children; | |
std::string::size_type begin, end; | |
std::string quotes; | |
std::string str; | |
bool bigness; | |
public: | |
hold () : hold("") {} | |
hold (char *x, char *y, int start = 0) : hold(std::string(x), std::string(y), start) {} | |
hold (const std::string &init, std::string quotestr = "()", std::string::size_type find = 0, bool big = true) : quotes(quotestr), bigness(big) { | |
if (quotes.size() != 2) throw "Wrong size quotestring"; | |
children.reserve(init.size()/2); | |
begin = find; | |
if (init.size() != 0) { | |
while (true) { | |
find = init.find_first_of(quotes, big && find == 0 ? find : find + 1); | |
if (init[find] == quotes[0]) { | |
children.push_back(hold(init, quotestr, find, false)); | |
find = children.back().end; | |
} else if (init[find] == quotes[1]) { | |
end = find; | |
if (!big || end == init.length() - 1) break; | |
} else if (find == std::string::npos) { | |
break; | |
} else { | |
throw "Error"; | |
} | |
} | |
} | |
if (!big) { | |
str = init.substr(begin, end - begin); | |
} else { | |
str = init; | |
} | |
} | |
std::string toFunString (std::string tab = " ", int depth = 0) { | |
std::string ret = ""; | |
for (int i = 0; i < depth; ++i) ret.append(tab); | |
if (!bigness) ret += quotes[0]; | |
if (children.size() != 0) { | |
if (!bigness) ret += '\n'; | |
if (!bigness) depth++; | |
for (auto s : children) ret.append(s.toFunString(tab, depth)); | |
if (!bigness) depth--; | |
for (int i = 0; i < depth; ++i) ret.append(tab); | |
} | |
if (!bigness) ret += quotes[1]; | |
if (!bigness) ret += '\n'; | |
return ret; | |
} | |
}; | |
int main () { | |
std::string line; | |
try { | |
while (true) { | |
std::cout << "> " << std::flush; | |
std::getline(std::cin, line); | |
if (line.size() == 0 || std::string("exit").find(line) == 0 || std::string("quit").find(line) == 0) break; | |
std::cout << hold(line).toFunString() << std::flush; | |
} | |
} catch (const char *e) { | |
std::cerr << e << std::endl; | |
return -1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment