-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
179 lines (147 loc) · 3.95 KB
/
Copy pathMain.cpp
File metadata and controls
179 lines (147 loc) · 3.95 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma warning(disable : 4996)
#include <stdio.h>
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
bool check_for_errors_in_code();
bool check_argv_errors(int argc, char* argv[]);
bool check_input_errors(int argc, char* argv[], std::string& out, std::string& in);
int main(int argc, char* argv[])
{
if (check_argv_errors(argc, argv))
{
return 0;
}
std::string file_name = argv[1];
std::string gcc_commend = file_name;
std::string exe_string = "e";
gcc_commend.replace(file_name.size() - 1, 1, exe_string);
gcc_commend += "xe";
if (check_input_errors(argc, argv, gcc_commend, file_name))
{
return 0;
}
std::string end_Command = "start gcc.exe out.s -o " + gcc_commend;
Sleep(1000);
std::cout << "Stage 1: Converting The File To LLVM Code: " << file_name << std::endl;
std::string command = "start Flex_BisonV6.exe " + file_name + " out.ll";
system(command.c_str());
Sleep(500);
if (!check_for_errors_in_code())
{
return 0;
}
Sleep(500);
std::cout << "Stage 2: Converting The LLVM File To Assembly Code: " << std::endl;
system("start llc.exe -march=x86 --x86-asm-syntax=intel out.ll");
std::cout << "Stage 2: Done!" << std::endl;
Sleep(500);
std::cout << "Stage 3: Converting The Assembly File To An EXE file: " << std::endl;
system(end_Command.c_str());
std::cout << "Stage 3: Done!" << std::endl;
Sleep(1000);
std::cout << "EXE file result:" << std::endl;
system(gcc_commend.c_str());
return 0;
}
bool check_input_errors(int argc, char* argv[], std::string& out, std::string& in)
{
if (argc == 4 && std::string(argv[2]) == "-o")
{
out = std::string(argv[3]);
if (in.find(".c") == std::string::npos)
{
std::cout << "Ctron: fatal error: file type not supported\n";
std::cout << "compilation terminated.\n";
return true;
}
if (out.find(".exe") == std::string::npos)
{
out += ".exe";
}
}
else if (argc == 3 && std::string(argv[2]) == "-o")
{
if (in.find(".c") == std::string::npos)
{
std::cout << "Ctron: fatal error: file type not supported\n";
std::cout << "compilation terminated.\n";
return true;
}
}
else
{
std::cout << "Ctron: error: unrecognized command line option " << argv[2] << "\n";
std::cout << "compilation terminated.\n";
return true;
}
return false;
}
bool check_argv_errors(int argc, char* argv[])
{
if (argc == 1)
{
std::cout << "Ctron: fatal error: no input files\n";
std::cout << "compilation terminated.\n";
return true;
}
if (argc > 2)
{
return false;
}
if (argc > 4)
{
std::cout << "Ctron: fatal error: too many arguments\n";
std::cout << "compilation terminated.\n";
return true;
}
if (argc == 2 && std::string(argv[1]) == "-help" || std::string(argv[1]) == "-h")
{
std::cout << "Usage:\n";
std::cout << " Ctron [options]\n";
std::cout << " Ctron [source_file] [command]\n";
std::cout << "options:\n";
std::cout << " -h / -help Display this information.\n";
std::cout << "commands:\n";
std::cout << " -o <file> Place the output into <file>.\n";
std::cout << " -o Place the output into [source_file.exe].\n";
return true;
}
else
{
std::cout << "Ctron: error: unrecognized command line option " << argv[1] << "\n";
std::cout << "compilation terminated.\n";
return true;
}
return false;
}
bool check_for_errors_in_code()
{
std::string line;
std::string errors = "";
std::ifstream myfile("dump.txt");
if (myfile.is_open())
{
while (std::getline(myfile, line))
{
if (line != "")
{
errors += line;
errors += "\n";
}
}
myfile.close();
}
if (errors.size() == 0)
{
std::cout << "Stage 1: Done!" << std::endl;
return true;
}
else
{
std::cout << "Stage 1: Errors Found!" << std::endl;
std::cout << errors << std::endl;
return false;
}
}