-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
333 lines (289 loc) · 8.94 KB
/
main.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <cstdio>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "main.h"
string g_infilename; // Input file name
string g_outfilename; // Output file name
bool g_quiet = false; // Be quiet
bool g_tokenizeonly = false; // Show tokenization and quit
bool g_parsingonly = false; // Show parsing result and quit
bool g_validationonly = false; // Show validation result and quit
bool g_showgeneration = false;
SourceModel g_source;
FinalModel g_final;
static int g_errorcount = 0;
void RegisterError()
{
g_errorcount++;
}
void PrintExpression(ExpressionModel& expr, int number, int indent = 1)
{
std::cout << std::endl << std::setw(indent * 2) << " exp" << number << ":";
if (expr.IsEmpty())
{
std::cout << " empty";
return;
}
std::cout << " root:" << expr.root;
std::cout << " nodes(" << expr.nodes.size() << "): [";
for (size_t j = 0; j < expr.nodes.size(); j++)
{
ExpressionNode& node = expr.nodes[j];
std::cout << std::endl << std::setw(indent * 2 + 2) << " " << j << ": ";
if (!node.node.text.empty())
std::cout << std::left << std::setw(6) << node.node.text;
if (node.node.type == TokenTypeSymbol)
std::cout << std::left << std::setw(6) << node.node.symbol;
std::cout << " ";
node.Dump(std::cout);
if ((int)j == expr.root)
std::cout << " root";
if (node.args.size() > 0)
{
std::cout << " args(" << node.args.size() << "): [";
for (size_t i = 0; i < node.args.size(); i++)
{
ExpressionModel& exprin = node.args[i];
PrintExpression(exprin, i, indent + 2);
}
std::cout << " ]";
}
}
std::cout << " ]";
}
void PrintLineModel(SourceLineModel& line)
{
std::cout << "Line " << line.number << " " << line.statement.token.text << line.statement.token.symbol;
if (line.statement.paramline > 0)
std::cout << " " << line.statement.paramline;
if (line.statement.ident.type != TokenTypeNone)
std::cout << " ident:" << line.statement.ident.text;
if (line.statement.args.size() > 0)
std::cout << " args(" << line.statement.args.size() << ")";
if (line.statement.params.size() > 0)
std::cout << " params(" << line.statement.params.size() << ")";
if (line.statement.variables.size() > 0)
std::cout << " vars(" << line.statement.variables.size() << ")";
if (line.statement.varexprs.size() > 0)
std::cout << " varexs(" << line.statement.varexprs.size() << ")";
if (line.statement.args.size() > 0)
{
for (size_t i = 0; i < line.statement.args.size(); i++)
{
ExpressionModel& expr = line.statement.args[i];
PrintExpression(expr, i);
}
}
if (line.statement.params.size() > 0)
{
for (size_t i = 0; i < line.statement.params.size(); i++)
{
Token& token = line.statement.params[i];
std::cout << std::endl << std::setw(2) << " par" << i << ": ";
token.Dump(std::cout);
}
}
if (line.statement.variables.size() > 0)
{
for (size_t i = 0; i < line.statement.variables.size(); i++)
{
VariableModel& var = line.statement.variables[i];
std::cout << std::endl << std::setw(2) << " var" << i << ": ";
std::cout << var.name;
if (var.indices.size() > 0)
{
std::cout << "(";
for (size_t j = 0; j < var.indices.size(); j++)
{
if (j > 0) std::cout << ",";
std::cout << var.indices[j];
}
std::cout << ")";
}
}
}
if (line.statement.varexprs.size() > 0)
{
for (size_t i = 0; i < line.statement.varexprs.size(); i++)
{
VariableExpressionModel& var = line.statement.varexprs[i];
std::cout << std::endl << std::setw(2) << " varex" << i << ": ";
std::cout << var.name;
if (!var.args.empty())
{
for (size_t j = 0; j < line.statement.args.size(); j++)
{
ExpressionModel& expr = var.args[j];
PrintExpression(expr, j, 2);
}
}
}
}
std::cout << std::endl;
}
void ShowTokenization(Tokenizer& tokenizer)
{
while (true)
{
Token token = tokenizer.GetNextToken();
token.Dump(std::cout);
std::cout << std::endl;
if (token.type == TokenTypeEOT)
break;
}
}
void ShowParsing(Parser& parser)
{
while (true)
{
SourceLineModel line = parser.ParseNextLine();
if (line.number == 0)
break;
if (!line.error)
std::cout << line.text << std::endl;
PrintLineModel(line);
g_source.lines.push_back(line);
}
}
void ShowValidation(Validator& validator)
{
for (size_t i = 0; i < g_source.lines.size(); i++)
{
validator.ProcessLine();
SourceLineModel& line = g_source.lines[i];
std::cout << line.text << std::endl;
PrintLineModel(line);
}
}
void ProcessFiles()
{
std::ifstream instream;
instream.open(g_infilename);
if (!instream.is_open())
{
std::cerr << "Failed to open the input file " + g_infilename << std::endl;
exit(EXIT_FAILURE);
}
Tokenizer tokenizer(&instream);
if (g_tokenizeonly)
{
ShowTokenization(tokenizer);
instream.close();
return;
}
Parser parser(&tokenizer);
if (g_parsingonly)
{
ShowParsing(parser);
instream.close();
return;
}
g_errorcount = 0;
while (true)
{
SourceLineModel line = parser.ParseNextLine();
if (line.number == 0)
break;
g_source.lines.push_back(line);
}
if (g_errorcount > 0)
{
std::cerr << "Parsing ERRORS: " << g_errorcount << std::endl;
exit(EXIT_FAILURE);
}
instream.close();
Validator validator(&g_source);
if (g_validationonly)
{
ShowValidation(validator);
return;
}
g_errorcount = 0;
while (validator.ProcessLine())
;
if (g_errorcount > 0)
{
std::cerr << "Validation ERRORS: " << g_errorcount << std::endl;
exit(EXIT_FAILURE);
}
std::ofstream outstream;
outstream.open(g_outfilename, std::ofstream::out | std::ofstream::trunc);
if (!outstream.is_open())
{
std::cerr << "Failed to open the output file " << g_outfilename << std::endl;
exit(EXIT_FAILURE);
}
outstream << "; Generated with vibasc [" << __DATE__ << "] on " << g_infilename << std::endl;
outstream << ";" << std::endl;
Generator generator(&g_source, &g_final);
g_errorcount = 0;
while (generator.ProcessLine())
;
for (size_t i = 0; i < g_final.lines.size(); i++)
{
string& intermed = g_final.lines[i];
outstream << intermed << std::endl;
if (g_showgeneration)
std::cout << intermed << std::endl;
}
if (g_errorcount > 0)
{
std::cerr << "Generation ERRORS: " << g_errorcount << std::endl;
exit(EXIT_FAILURE);
}
}
void ParseCommandLine(int argc, char** argv)
{
for (int argn = 1; argn < argc; argn++)
{
const char* arg = argv[argn];
if (*arg == '-'
#ifdef _MSC_VER
|| *arg == '/'
#endif
) // Parse options
{
if (_stricmp(arg + 1, "q") == 0 || _stricmp(arg, "--quiet") == 0)
g_quiet = true;
else if (_stricmp(arg + 1, "t") == 0 || _stricmp(arg, "--tokenizeonly") == 0)
g_tokenizeonly = true;
else if (_stricmp(arg + 1, "p") == 0 || _stricmp(arg, "--parsingonly") == 0)
g_parsingonly = true;
else if (_stricmp(arg + 1, "v") == 0 || _stricmp(arg, "--validationonly") == 0)
g_validationonly = true;
else if (_stricmp(arg + 1, "g") == 0 || _stricmp(arg, "--showgeneration") == 0)
g_showgeneration = true;
else
{
std::cerr << "Unknown option: " << arg << std::endl;
exit(EXIT_FAILURE);
}
}
else
{
g_infilename = arg;
}
}
// Validate command line params
if (g_infilename.empty())
{
//print_help();
std::cerr << "Input file not specified." << std::endl;
exit(EXIT_FAILURE);
}
}
int main(int argc, char* argv[])
{
ParseCommandLine(argc, argv);
size_t dotpos = g_infilename.find_last_of('.');
if (dotpos == string::npos)
g_outfilename = g_infilename + ".MAC";
else
g_outfilename = g_infilename.substr(0, dotpos) + ".MAC";
if (!g_quiet)
std::cout << "vibasc " << __DATE__ << std::endl;
std::cout << std::endl;
ProcessFiles();
return EXIT_SUCCESS;
}