-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
156 lines (150 loc) · 5.82 KB
/
parser.cpp
File metadata and controls
156 lines (150 loc) · 5.82 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// compiles tokens that correspond to commands
extern "C" {
#include <stdio.h>
}
#include <fstream>
#include <cstdlib>
#include "errors.h" // includes string
#ifdef __unix__
const char *format = " -f elf64 ";
const char *extraLD = "";
#else
const char *format = " -f win64 ";
const char *extraLD = " -subsystem console -entry _start"
#endif
void RemoveExtension(std::string &output_file) noexcept {
if (output_file.find(".") != std::string::npos) {
size_t index = output_file.find(".");
output_file = output_file.substr(0, index);
}
}
bool Compile(const std::string &nasm_cmds, std::string &output_file, const char &flag) {
ErrorMsg ManualReport;
RemoveExtension(output_file);
std::ofstream output_f((output_file + ".asm"), std::ios::out);
if (output_f.bad()) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot create a file";
ManualReport.PrintError(ManualReport);
return false;
}
for (const char &c : nasm_cmds) {
output_f.put(c);
}
output_f.close();
std::string exe_str {};
switch (flag) {
case ' ': {
exe_str = "nasm" + std::string(format) + output_file + ".asm" + " -o " + output_file + ".o";
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot compile the code";
ManualReport.PrintError(ManualReport);
return false;
}
exe_str = "ld " + output_file + ".o" " -o " + output_file + std::string(extraLD);
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot link the code";
ManualReport.PrintError(ManualReport);
return false;
}
exe_str = "rm " + output_file + ".asm " + output_file + ".o";
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot remove build files";
ManualReport.PrintError(ManualReport);
return false;
}
break;
}
case 'a': {
return true;
break;
}
case 's': {
exe_str = "nasm" + std::string(format) + output_file + ".asm" + " -o " + output_file + ".o";
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot compile the code";
ManualReport.PrintError(ManualReport);
return false;
}
break;
}
case 'c': {
exe_str = "nasm" + std::string(format) + output_file + ".asm" + " -o " + output_file + ".o";
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot compile the code";
ManualReport.PrintError(ManualReport);
return false;
}
exe_str = "ld " + output_file + ".o" " -o " + output_file + std::string(extraLD);
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot link the code";
ManualReport.PrintError(ManualReport);
return false;
}
break;
}
case 'e': {
exe_str = "nasm" + std::string(format) + output_file + ".asm" + " -o " + output_file + ".o";
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot compile the code";
ManualReport.PrintError(ManualReport);
return false;
}
exe_str = "ld " + output_file + ".o" " -o " + output_file + std::string(extraLD);
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot link the code";
ManualReport.PrintError(ManualReport);
return false;
}
exe_str = "rm " + output_file + ".asm " + output_file + ".o";
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot remove build files";
ManualReport.PrintError(ManualReport);
return false;
}
#ifdef __unix__
exe_str = "./" + output_file;
#else
exe_str = ".\\" + output_file;
#endif
int status = system(exe_str.c_str());
printf("\nError code: %i\n", WEXITSTATUS(status));
fflush(stdout);
break;
}
case 'g': {
exe_str = "nasm -g" + std::string(format) + output_file + ".asm" + " -o " + output_file + ".o";
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot compile the code";
ManualReport.PrintError(ManualReport);
return false;
}
exe_str = "ld -g " + output_file + ".o" " -o " + output_file + std::string(extraLD);
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot link the code";
ManualReport.PrintError(ManualReport);
return false;
}
exe_str = "rm " + output_file + ".asm " + output_file + ".o";
if (system(exe_str.c_str()) != 0) {
ManualReport.ErrorID = 0;
ManualReport.Message = "Cannot remove build files";
ManualReport.PrintError(ManualReport);
return false;
}
break;
}
}
return true;
}