-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
102 lines (78 loc) · 2.39 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "tree.hpp"
#include "generated/parser.tab.hpp"
#include <iostream>
#include "preprocessor.hpp"
#include "codegen.hpp"
#include <fstream>
#include <cstdlib>
#include <fstream>
#include <filesystem>
void scan_string(const char* str);
extern int yydebug;
StatementNode* getRoot();
#define PREPROCESS
#define CODEGEN
int main(int argc, char* argv[]) {
if(argc < 2) {
std::cerr << "Usage: " << argv[0] << " <input_file.rcy>" << std::endl;
return 1;
}
std::fstream codeFile{};
codeFile.open(argv[1]);
yydebug = 0;
std::string code_content;
if(codeFile.is_open()) {
std::string line;
while(std::getline(codeFile, line)) {
code_content += line + "\n";
}
} else {
std::cerr << "Failed to open the file: " << argv[1] << std::endl;
return 1;
}
codeFile.close();
std::cout << code_content << std::endl;
scan_string(code_content.c_str());
yyparse();
auto root = getRoot();
#ifdef PREPROCESS
auto res = preprocessor::preprocess(root);
#endif
if(root == nullptr) {
std::cout << "root is null. crash will happen soon." << std::endl;
return 1;
}
std::cout << *root << std::endl;
#ifdef PREPROCESS
if(res.messages.size() > 0) {
for(auto& message : res.messages) {
std::cout << message << std::endl;
}
return 1;
} else {
std::cout << "No errors found." << std::endl;
}
#ifdef CODEGEN
CPPCodeGenerator codegen{};
std::string code = codegen.generate(root, res);
std::cout << code << std::endl;
// Write to out.cpp
std::ofstream file;
file.open("out.cpp");
file << code;
file.close();
// Extract filename without extension
std::string input_file(argv[1]);
std::string filename_without_extension = input_file.substr(0, input_file.find_last_of('.'));
// Create the output executable name with "_rcy.exe"
std::string output_executable = filename_without_extension + "_rcy.exe";
// Compile the generated code into the desired output executable
std::string compile_command = "g++ -O2 out.cpp -o " + output_executable;
system(compile_command.c_str());
// Delete the temporary out.cpp file
// std::filesystem::remove("out.cpp");
std::cout << "Executable generated: " << output_executable << std::endl;
#endif
#endif
return 0;
}