Skip to content

Instantly share code, notes, and snippets.

@JamesWP
Created December 11, 2017 00:19
Show Gist options
  • Save JamesWP/3c1c19be84f623cd191e231fc9155de9 to your computer and use it in GitHub Desktop.
Save JamesWP/3c1c19be84f623cd191e231fc9155de9 to your computer and use it in GitHub Desktop.
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>
std::set<std::string> variables;
std::ostringstream proc;
int main(int argc, char* argv[])
{
if (argc < 2) return 1;
std::ifstream input(argv[1]);
if (!input) return 2;
std::cout <<"#include <stdio.h>\n";
std::cout <<"\n";
std::cout <<"int main() {\n";
std::cout <<" int maxmax = 0;\n";
std::string instmt;
while (std::getline(input, instmt)) {
std::istringstream inss(instmt);
std::string var;
std::string op;
int arg;
std::string f;
std::string cndvar;
std::string cndop;
int cndval;
inss >> var;
inss >> op;
inss >> arg;
inss >> f;
inss >> cndvar;
inss >> cndop;
inss >> cndval;
if (variables.insert(cndvar).second)
std::cout << " int " << cndvar << " = 0;\n";
if (variables.insert(var).second)
std::cout << " int " << var << " = 0;\n";
std::cout << " if(" << cndvar << " " << cndop << " " << cndval << ") {\n";
std::cout << " " << var << (op == "inc"? " += " : " -= ") << arg << ";\n";
std::cout << " " << "if(" << var << " > maxmax) maxmax = " << var << ";\n";
std::cout << " }\n";
}
std::cout << "int max = " << *variables.begin() << ";\n";
for(const std::string& var : variables) {
std::cout << "if(max < " << var << ") max = " << var << ";\n";
}
std::cout << "printf(\"max: %d\\n\", max);\n";
std::cout << "printf(\"maxmax: %d\\n\", maxmax);\n";
std::cout << "}\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment