1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-25 17:40:36 +00:00
hyperrogue/makeh.cpp

114 lines
2.8 KiB
C++
Raw Normal View History

2019-08-09 13:07:43 +00:00
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
2019-08-09 17:10:41 +00:00
#include <vector>
2019-08-09 13:07:43 +00:00
using namespace std;
int indent = 2;
string ind() { string s; for(int i=0; i<indent; i++) s += ' '; return s; }
string which_file;
2019-08-09 17:10:41 +00:00
vector<string> if_stack;
int ifs_level;
2019-08-09 13:07:43 +00:00
void mark_file() {
if(which_file != "") {
cout << "\n" << ind() << "// implemented in: " << which_file << "\n\n";
which_file = "";
}
2019-08-09 17:10:41 +00:00
while(ifs_level < (int) if_stack.size())
cout << ind() << if_stack[ifs_level++] << "\n";
while(ifs_level > (int) if_stack.size())
cout << ind() << "#endif\n", ifs_level--;
2019-08-09 13:07:43 +00:00
}
2019-08-09 19:05:54 +00:00
int in_hdr;
2019-08-09 13:07:43 +00:00
void gen(string s) {
which_file = s;
ifstream in(s);
while(getline(in, s)) {
while(s != "" && s[0] == ' ') s = s.substr(1);
2019-08-09 13:26:18 +00:00
while(s.back() == 10 || s.back() == 13) s = s.substr(0, s.size() - 1);
if(in_hdr) {
2019-08-09 19:05:54 +00:00
if(s == "#endif")
in_hdr--;
if(s.substr(0, 3) == "#if")
in_hdr++;
if(in_hdr)
cout << ind() << s << "\n";
continue;
2019-08-09 13:07:43 +00:00
}
if(s == "#if HDR") {
in_hdr = true;
2019-08-09 17:10:41 +00:00
continue;
}
2019-08-09 19:00:52 +00:00
if(s == "#if CU_INIT") {
if_stack.push_back("#if 1");
continue;
}
2019-08-09 17:10:41 +00:00
if(s.substr(0, 3) == "#if" || s.substr(0, 4) == "# if") {
if_stack.push_back(s);
}
if(s.substr(0, 6) == "#endif") {
if(if_stack.empty()) { cerr << "if_stack error " << which_file << ", " << s << "\n"; exit(1); }
if_stack.pop_back();
2019-08-09 13:26:18 +00:00
}
if(s.substr(0, 4) == "EX }") {
mark_file();
2019-08-09 13:26:18 +00:00
cout << ind() << "}\n";
indent -= 2;
}
else if(s.substr(0, 3) == "EX ") {
2019-08-09 13:07:43 +00:00
string t = s.substr(3);
if(t.substr(0, 10) == "namespace ") {
mark_file();
cout << ind() << t << "\n";
indent += 2;
}
else {
mark_file();
2019-08-09 13:07:43 +00:00
for(int i=0;; i++) {
2019-08-09 13:26:18 +00:00
if(i == int(t.size())) { cerr << "Error: unrecognizable EX: " << s << "\n"; }
2019-08-09 13:07:43 +00:00
else if(t[i] == '{') {
while(i && t[i-1] == ' ') i--;
cout << ind() << t.substr(0, i) << ";\n";
break;
}
else if(t[i] == ';') {
cout << ind() << "extern " << t << "\n";
break;
}
else if(t[i] == '=') {
while(i && t[i-1] == ' ') i--;
cout << ind() << "extern " << t.substr(0, i) << ";\n";
break;
}
}
}
}
}
2019-08-09 17:10:41 +00:00
while(ifs_level > (int) if_stack.size())
cout << ind() << "#endif\n", ifs_level--;
2019-08-09 13:07:43 +00:00
while(indent > 2) {
cout << ind() << "}\n";
indent -= 2;
}
}
int main(int argc, char ** argv) {
printf("// This file is generated automatically by makeh.cpp.\n\nnamespace hr {\n");
indent = 2;
for(int i=1; i<argc; i++)
gen(argv[i]);
printf(" }\n");
}