-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
70 lines (51 loc) · 1.38 KB
/
main.cpp
File metadata and controls
70 lines (51 loc) · 1.38 KB
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
#include "parser.h"
#include "utility.h"
void initializeLLVM() {
g_Module = std::make_unique<llvm::Module>("TEST", Context);
if (!g_Module)
std::cout << "[initializeLLVM] Module is failed!";
}
void compile_Run(const std::string& filename) {
initializeLLVM();
const auto code = readFile(filename);
auto tokens = lexer(code);
Parser parser(tokens);
while (parser.getCurrentToken().token_type != tok_eof) {
auto func = parser.parseFunction();
if (!func)
throw std::runtime_error("Function parsing failed!");
func->codegen();
}
std::string verifyOutput;
llvm::raw_string_ostream rso(verifyOutput);
if (llvm::verifyModule(*g_Module, &rso))
std::cerr << "[MODULE] ->" << verifyOutput << "\n";
}
void usage() {
std::cout << "\n****** LLVM based dalg language by d06i ***********\n" <<
"For LLVM IR code : dalg.exe input.dlag output.ll \n" <<
"For executable file: clang output.ll -o output.exe\n";
}
int main(int argc, const char* argv[]) {
try {
if (argc == 2) {
const auto src = readFile(argv[1]);
auto token = lexer(src);
write(token);
}
if (argc == 3) {
std::cout << "Compiling...\n";
compile_Run(argv[1]);
write2File(argv[2]);
std::cout << "LLVM IR writed!\n";
}
else
std::cerr << "Write failed!\n";
}
catch (const std::exception& err) {
std::cerr << "Error: " << err.what() << "\n";
usage();
return 1;
}
return 0;
}