-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassemblerMain.cpp
205 lines (177 loc) · 4.23 KB
/
assemblerMain.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Jordan Dehmel
jdehmel@outlook.com
github.com/jorbDehmel
2023 - present
MIT licence via mit-license.org held by author
*/
#include "src/assembler.hpp"
#include "src/tags.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;
struct options
{
string inFile;
string outFile;
string shebang;
bool debug;
};
options parseArgs(const int argc, const char *argv[])
{
options out;
out.inFile = "";
out.outFile = "";
out.shebang = "";
out.debug = false;
for (int i = 1; i < argc; i++)
{
string cur(argv[i]);
if (cur == "-sh")
{
if (i + 1 >= argc)
{
throw runtime_error("Error: -sh must be followed by an argument");
}
i++;
out.shebang = argv[i];
}
else if (cur == "-o")
{
if (i + 1 >= argc)
{
throw runtime_error("-o must be followed by a file name");
}
out.outFile = argv[i + 1];
i++;
}
else if (cur == "-d")
{
out.debug = true;
}
else if (cur[0] != '-')
{
out.inFile = cur;
}
}
if (out.inFile == "")
{
throw runtime_error("No input file provided");
}
else if (out.outFile == "")
{
throw runtime_error("No output file provided");
}
return out;
}
int main(const int argc, const char *argv[])
{
options opts;
try
{
opts = parseArgs(argc, argv);
}
catch (runtime_error &e)
{
cout << tags::red_bold
<< "Error parsing arguments.\n"
<< e.what() << '\n'
<< tags::reset;
return 7;
}
string inputFileName = opts.inFile;
string outputFileName = opts.outFile;
// Look for .tasl suffix on input
if (inputFileName.size() < 5 || inputFileName.substr(inputFileName.size() - 5) != ".tasl")
{
cout << tags::yellow_bold
<< "Warning! This assembler takes only .tasl files\n"
<< tags::reset;
}
// Look for .ter suffix on output
if (outputFileName.size() < 4 || outputFileName.substr(outputFileName.size() - 4) != ".ven")
{
// Ensure it's not a venus-shebang file
if (outputFileName.size() < 6 || outputFileName.substr(outputFileName.size() - 6) != ".vensh")
{
cout << tags::yellow_bold
<< "Warning! The proper file suffix for ternary compiled files is .ven\n"
<< tags::reset;
}
}
ifstream in(inputFileName);
if (!in.is_open())
{
in.close();
cout << tags::red_bold
<< "Error: Could not open input file.\n"
<< tags::reset;
return 2;
}
// Get source code to assemble
string toAssemble, line;
while (getline(in, line))
{
toAssemble += line + '\n';
}
in.close();
cout << tags::green_bold
<< "Assembling...\n"
<< tags::reset;
// Assemble
short_assembly assembled;
try
{
Assembler a;
if (opts.debug)
{
a.options.insert("dump_vars");
}
assembled = a.assemble(toAssemble);
}
catch (macro_error &e)
{
cout << tags::red_bold
<< "Error: Macro failed.\n"
<< e.what() << '\n'
<< tags::reset;
return 6;
}
catch (runtime_error &e)
{
cout << tags::red_bold
<< "Error: Assembly failed.\n"
<< e.what() << '\n'
<< tags::reset;
return 4;
}
catch (...)
{
cout << tags::red_bold
<< "Error: Fatal error occured.\n"
<< tags::reset;
return 5;
}
cout << tags::green_bold
<< "Assembled.\n"
<< tags::reset;
// Write to file
ofstream out(outputFileName);
if (!out.is_open())
{
out.close();
cout << tags::red_bold
<< "Error: Could not open output file.\n"
<< tags::reset;
return 3;
}
if (opts.shebang != "")
{
out << "#!" << opts.shebang << '\n';
}
out << assembled;
out.close();
return 0;
}