-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (68 loc) · 1.85 KB
/
main.cpp
File metadata and controls
78 lines (68 loc) · 1.85 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
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include <libgen.h>
#include <unistd.h>
#include "astree.h"
#include "auxlib.h"
#include "debug.h"
#include "lyutils.h"
#include "symbols.h"
FILE* token_file;
struct usage_error: exception {
usage_error() {
exec::status (EXIT_FAILURE);
}
};
struct options {
int lex_debug {0};
int parse_debug {0};
const char* oc_filename {nullptr};
string defines;
options (int argc, char** argv);
};
options::options (int argc, char** argv) {
opterr = 0;
for(;;) {
int opt = getopt (argc, argv, "@:lyD:");
if (opt == EOF) break;
switch (opt) {
case '@': debugflags::setflags (optarg); break;
case 'l': lex_debug = 1; break;
case 'y': parse_debug = 1; break;
case 'D': defines = "-D" + string(optarg); break;
default: exec::error() << "invalid option ("
<< char (optopt) << ")" << endl;
}
}
if (optind + 1 < argc) throw usage_error();
oc_filename = optind == argc ? "-" : argv[optind];
}
int main (int argc, char** argv) {
ios_base::sync_with_stdio (true);
exec::name (argv[0]);
try {
options opts (argc, argv);
if (strcmp(opts.oc_filename, "-") != 0) {
if (!(lexer.open_tokens(opts.oc_filename))) {
return 1;
};
}
parse_util parser (opts.oc_filename,
opts.parse_debug, opts.lex_debug,
opts.defines);
parser.parse();
if (strcmp(opts.oc_filename, "-") != 0) {
if (!(lexer.close_tokens())) {
return 1;
}
}
}catch (usage_error&) {
cerr << "Usage: " << exec::name() << " [-ly@] [program]" << endl;
}catch (fatal_error& reason) {
exec::error() << reason.what() << endl;
}
return exec::status();
}