1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-09 15:39:55 +00:00
hyperrogue/makeh.cpp

128 lines
3.2 KiB
C++
Raw Normal View History

// generate autohdr.h based on the `EX` and `#if HDR` in *.cpp files
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 20:37:11 +00:00
#include <set>
2019-09-13 16:30:05 +00:00
#include <cstdlib>
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";
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
2019-08-09 20:37:11 +00:00
set<string> seen;
int lineid;
void gen(string sf) {
if(seen.count(sf)) return;
seen.insert(sf);
2019-08-18 18:56:59 +00:00
which_file = sf; lineid = 1;
2019-09-13 16:33:15 +00:00
ifstream in(sf.c_str());
string s;
2019-08-09 13:07:43 +00:00
while(getline(in, s)) {
lineid++;
2019-08-09 13:07:43 +00:00
while(s != "" && s[0] == ' ') s = s.substr(1);
2019-09-13 16:30:05 +00:00
while(s != "" && (s[s.size()-1] == 10 || s[s.size()-1] == 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") {
2019-08-10 08:57:14 +00:00
mark_file();
cout << "#line " << lineid << " \"" << sf << "\"\n";
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-10 08:57:14 +00:00
while(ifs_level > (int) if_stack.size())
cout << ind() << "#endif\n", ifs_level--;
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-26 07:10:28 +00:00
cout << "#line " << lineid-1 << " \"" << sf << "\"\n";
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");
}