-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_headers.cpp
49 lines (38 loc) · 1.06 KB
/
gen_headers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <fstream>
#include <sstream>
#include <string>
using std::ifstream;
using std::ofstream;
using std::string;
using std::stringstream;
using std::endl;
bool write_file(ofstream &out, const string &var_name, const string &filename) {
ifstream file(filename);
if (!file.good()) return false;
stringstream ss;
ss << file.rdbuf();
auto contents = ss.str();
out << "char " << var_name << "[] = {";
for (size_t i = 0; i < contents.size(); i++) {
out << static_cast<uint>(contents[i]);
if (i != contents.size() - 1) {
out << ", ";
}
}
out << ", 0};\n\n";
return true;
}
int main(int argc, char* argv[]) {
if (argc < 2) return 1;
ofstream out(argv[1]);
out << "#ifndef LISPY_GENERATED_HPP\n#define LISPY_GENERATED_HPP\n\n";
if (write_file(out, "language_grammar", "../language.txt")) {
if (!write_file(out, "prelude", "../prelude.lspy")) {
return 1;
}
} else {
return 1;
}
out << "#endif // LISPY_GENERATED_HPP" << endl;
return 0;
}