-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
127 lines (106 loc) · 2.45 KB
/
main.cpp
File metadata and controls
127 lines (106 loc) · 2.45 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
#include "./FinalCodeGenerator/finalGen.hpp"
#include "./IROptimize/irOptimize.hpp"
#include "./IntermediateCodeGenerator/intermediateGen.hpp"
#include "./LexicalAnalysis/Lexer.hpp"
#include "./Preprocessor/Preprocessor.hpp"
#include "./SemanticAnalysis/semanticAnalysis.hpp"
#include "./SyntaxAnalysis/ParseNode.hpp"
#include <iostream>
#include <string>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
int main(int argc, char* argv[]) {
if (argc != 2) {
cout << "Format: erosion <filename>\n";
return 1;
}
string extension = string(argv[1]);
if (extension.size() < 4) {
cout << "Error: <filename> must be a .cor file\n";
return 1;
}
if (extension.substr(extension.size() - 4, 4) != ".cor") {
cout << "Error: <filename> must be a .cor file\n";
return 1;
}
stringstream* str = preprocess(argv[1]);
if (str == nullptr) {
return 1;
}
Lexer lexer = Lexer(str);
// lexer.expressify();
ParseNode* ast = new ParseNode(&lexer);
cerr << "\x1b[31m";
if (!ast->build()) {
cerr << "\x1b[0m";
ast->print();
delete ast;
return 1;
}
cerr << "\x1b[0m";
Diagnoser diagnoser = Diagnoser();
if (!diagnoser.diagnose(ast)) {
ast->print();
delete ast;
cerr << "\x1b[31m" << diagnoser.error << "\x1b[0m" << endl;
return 1;
}
ast->print();
CodeGenerator irGen = CodeGenerator();
deque<string> ir;
irGen.generateIR(ast, ir);
optimizeIR(ir);
deque<string> output;
irGen.generatex86(output, ir);
ofstream file("out.asm");
for (auto line : ir) {
cout << line << endl;
}
cout << endl;
optimize(output);
for (auto line : output) {
file << line << '\n';
cout << line << endl;
}
file.close();
delete ast;
pid_t pid = fork();
if (pid == -1) {
// error
cerr << "Could not make child" << endl;
remove("out.asm");
} else if (pid) {
// parent
int status;
waitpid(pid, &status, WUNTRACED);
remove("out.asm");
if (status) {
cerr << "Sedimentation assembler returned non-zero exit code " << status << endl;
return 1;
}
} else {
// child
execlp("sedimentation", "sedimentation", "-f", "elf", "out.asm", NULL);
return 0;
}
pid = fork();
if (pid == -1) {
// error
cerr << "Could not make child" << endl;
remove("out.o");
} else if (pid) {
// parent
int status;
waitpid(pid, &status, WUNTRACED);
remove("out.o");
if (status) {
cerr << "Linker returned non-zero exit code " << status << endl;
return 1;
}
} else {
// child
execlp("ld", "ld", "out.o", NULL);
}
return 0;
}