-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
137 lines (111 loc) · 3.34 KB
/
Copy pathmain.cpp
File metadata and controls
137 lines (111 loc) · 3.34 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "all.hpp"
using namespace zust;
int main(int argc, char *argv[]) {
if (argc < 2) {
CommandLine::printUsage(argv[0]);
return 0;
}
CommandLine cli(argc, argv);
if (cli.hasError()) {
return 1;
}
if (cli.showHelp()) {
CommandLine::printUsage(argv[0]);
return 0;
}
if (cli.showFormats()) {
CommandLine::printFormats();
return 0;
}
const std::string inputFile = cli.getInputFile();
if (!inputFile.ends_with(".zz")) {
logError(zust::Error(zust::ErrorType::Generic, "Input file must have .zz extension."));
CommandLine::printUsage(argv[0]);
return 1;
}
if (inputFile.empty()) {
logError(zust::Error(zust::ErrorType::Generic, "No input files."));
CommandLine::printUsage(argv[0]);
return 1;
}
std::optional<std::string> source = zust::File::readAllText(inputFile);
if (!source) {
logError(zust::Error(zust::ErrorType::Generic,
"Failed to read from " + inputFile));
return 1;
}
// Parse source
Lexer lexer(source.value());
Parser parser(lexer);
logMessage("Parsing");
std::unique_ptr<ASTNode> program = parser.parse();
if (!parser.isCorrect()) {
return 1;
}
if (!program.get()) {
zust::logError(Error(ErrorType::Generic, "Parsing Failed"));
return 1;
}
if (cli.printAST()) {
program.get()->print(std::cout);
}
logMessage("TypeChecking");
// Type checking
TypeChecker typeChecker;
typeChecker.check(program);
logMessage("Code Genning");
if (!typeChecker.shouldCodegen())
return 1;
try {
program->scope->lookupFunction("main");
} catch (...) {
logError(Error(ErrorType::Generic, "Main Function does'nt exist in program scope (GLOBALLY)"));
exit(1);
}
std::ostream *outstream = &std::cout;
std::ofstream ofs;
// Only open the file if requested:
if (!cli.getOutputFile().empty()) {
ofs.open(cli.getOutputFile());
if (!ofs) {
std::cerr << "Error: cannot open output file: "
<< cli.getOutputFile() << "\n";
std::exit(1);
}
outstream = &ofs; // now point at the file
}
std::unique_ptr<zust::CodeGen> cg =
CodeGen::create(TargetTriple::X86_64_LINUX, *outstream);
switch (cli.getFormat()) {
case CodegenOutputFormat::Default:
#ifdef _WIN64
cg = CodeGen::create(TargetTriple::X86_64_WINDOWS, *outstream);
#endif
#ifdef __linux__
cg = CodeGen::create(TargetTriple::X86_64_LINUX, *outstream);
#endif
break;
case CodegenOutputFormat::X86_64_MSWIN: {
cg = CodeGen::create(TargetTriple::X86_64_WINDOWS, *outstream);
break;
}
case CodegenOutputFormat::X86_64_LINUX: {
cg = CodeGen::create(TargetTriple::X86_64_LINUX, *outstream);
break;
}
case CodegenOutputFormat::LLVM_IR: {
cg = CodeGen::create(TargetTriple::LLVM_IR, *outstream);
break;
}
default:
std::cerr << "This should not happen, ACP Pradhyumn...\n";
exit(1);
}
try {
cg->generate(std::move(program));
} catch (std::exception const &exc) {
std::cerr << "ERROR: " << exc.what() << "\n";
return 1;
}
return 0;
}