-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
53 lines (44 loc) · 1.34 KB
/
Copy pathcompiler.cpp
File metadata and controls
53 lines (44 loc) · 1.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
#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#endif
#include "compiler.h"
#include "lexer.h"
#include "parser.h"
#include "codegen.h"
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
bool compile(const std::string& source, const std::string& outputName) {
Lexer lexer(source);
std::vector<Token> tokens = lexer.tokenize();
Parser parser(std::move(tokens));
auto program = parser.parse();
std::ostringstream asmSrc;
AsmCodeGen codegen(asmSrc);
codegen.generate(program.get());
std::string tmpObj = outputName + ".iris.o";
{
std::string asCmd = "as --64 -o \"" + tmpObj + "\" -";
FILE* proc = popen(asCmd.c_str(), "w");
if (!proc) {
std::cerr << "Cannot run assembler\n";
return false;
}
const std::string& asmStr = asmSrc.str();
fwrite(asmStr.c_str(), 1, asmStr.size(), proc);
if (pclose(proc) != 0) {
std::cerr << "Assembly failed\n";
return false;
}
}
std::string cmd = "gcc -o \"" + outputName + "\" \"" + tmpObj + "\" -no-pie";
int ret = std::system(cmd.c_str());
std::remove(tmpObj.c_str());
if (ret != 0) {
std::cerr << "Link failed\n";
return false;
}
return true;
}