Last active
September 27, 2015 18:19
-
-
Save LordAro/826404f844b095950937 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 <algorithm> | |
#include <functional> | |
#include <iostream> | |
#include <map> | |
#include <string> | |
// Temporary stuff. Replace with whatever you have elsewhere. | |
class Const { | |
public: | |
std::string label; | |
int value; | |
}; | |
class Chunk { | |
public: | |
std::vector<Const> mLabelValues; | |
}; | |
std::vector<Const> mDefineValues; | |
std::vector<Chunk> mChunks; | |
void Error(std::string errmsg) { | |
std::cerr << errmsg << "\n"; | |
} | |
int getNumber(std::string expr, bool &isNum) { | |
isNum = std::all_of(expr.begin(), expr.end(), ::isdigit); | |
return isNum ? stoi(expr, 0, 0) : 0; | |
} | |
int Evaluate(std::string expression) { | |
if(expression.length() == 0) return 0; | |
static const std::map<std::string, std::function<int(int, int)>> delims = { | |
{"\\+", std::plus<int>()}, | |
{"-", std::minus<int>()}, | |
{"\\*", std::multiplies<int>()}, | |
{"/", std::divides<int>()}, | |
{"&", std::bit_and<int>()}, | |
{"\\|", std::bit_or<int>()}, | |
}; | |
for (const auto &delimPair : delims) { | |
std::string delim = delimPair.first; | |
auto delimFunc = delimPair.second; | |
size_t pos = expression.find(delim); | |
if (pos == std::string::npos) continue; | |
std::string p1 = expression.substr(0, pos); | |
std::string p2 = expression.substr(pos + delim.length()); | |
if (p1.length() == 0 || p2.length() == 0) { | |
return 0; | |
} else { | |
return delimFunc(Evaluate(p1), Evaluate(p2)); | |
} | |
} | |
// Get values | |
bool number; | |
int result = getNumber(expression, number); // Nasty function signature | |
if (number) return result; | |
// Get values | |
if (std::all_of(expression.begin(), expression.end(), ::isdigit)) { | |
} | |
// Constant evaluation | |
for (const auto &v : mDefineValues) { | |
if (expression == v.label) return v.value; | |
} | |
// Whatever this does | |
for (const auto &c : mChunks) { | |
for (const auto &v : c.mLabelValues) { | |
if (expression == v.label) return v.value; | |
} | |
} | |
Error("Could not evaluate expression:" + expression); | |
return 0; | |
} | |
int main() { | |
std::vector<std::string> tests = { | |
"1\\+1", | |
"1\\*1\\+2", | |
"+1", | |
}; | |
for (const auto & test : tests) { | |
std::cout << Evaluate(test) << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment