-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
78 lines (74 loc) · 1.69 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
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.tab.h"
#include "flags.hpp"
extern FILE *yyin;
extern void yyerror(const char*);
extern std::vector<char> Program;
bool flags::annotations = false;
bool flags::instructions = false;
bool flags::help = false;
static inline void usage_error(const char *msg, const char *bin_name)
{
fprintf(stderr, "%s\nuse '%s -h' for help\n", msg, bin_name);
exit(1);
}
int main(int argc, char **argv)
{
if (argc <= 0) {
fprintf(stderr, "argc = 0 ?????\n");
return 1;
}
const char *filename = nullptr;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
if (argv[i][1] != '\0' && argv[i][2] == '\0') {
switch (argv[i][1]) {
case 'h':
flags::help = true;
break;
case 'a':
flags::annotations = true;
break;
case 'i':
flags::instructions = true;
break;
default:
fprintf(stderr, "flag '%s' is not known", argv[i]);
usage_error("", argv[0]);
}
} else {
fprintf(stderr, "flag '%s' is not known", argv[i]);
usage_error("", argv[0]);
}
} else if (filename) {
usage_error("more than one input file given", argv[0]);
} else {
filename = argv[i];
}
}
if (flags::help) {
printf("usage: %s [-a] [-i]\n", argv[0]);
puts("-h show this help");
puts("-a include annotations in compiler output");
puts("-i include debug instructions in compiler output");
return 0;
}
if (filename == nullptr) {
usage_error("no input file", argv[0]);
return 1;
}
yyin = fopen(filename, "r");
if (yyin == nullptr) {
perror("cannot open file");
return 1;
}
yyparse();
fclose(yyin);
for (auto c : Program) {
putchar(c);
}
putchar('\n');
}