This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·127 lines (101 loc) · 2.09 KB
/
main.c
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
/*
* main.c
*
* Ian Shearin
* CS480
*/
#include "main.h"
/*
* Command-line front end for compiler.
*/
int main(int argc, char **argv)
{
int action;
int arg;
buffer_t *in_buffer;
buffer_t *out_buffer;
int is_done;
parser_t *parser;
lexer_t *lexer;
token_t *token;
/* Set default settings. */
action = ACT_TRANS;
in_buffer = buffer_create(stdin);
out_buffer = buffer_create(stdout);
/* Parse command-line arguments. */
for (arg = 1; arg < argc; arg++)
{
if ((strcmp(argv[arg], "--help") == 0)
|| (strcmp(argv[arg], "-h") == 0))
{
action = ACT_USAGE;
}
else if ((strcmp(argv[arg], "--lex") == 0) && (action <= ACT_LEX))
{
action = ACT_LEX;
}
else if ((strcmp(argv[arg], "--parse") == 0) && (action <= ACT_PARSE))
{
action = ACT_PARSE;
}
else if ((strcmp(argv[arg], "--translate") == 0)
&& (action <= ACT_TRANS))
{
action = ACT_TRANS;
}
else
{
fprintf(stderr, "Invalid argument: %s\n", argv[arg]);
/* Stop parsing command-line. */
arg = argc;
action = ACT_USAGE;
}
}
/* Take action. */
if (action == ACT_USAGE)
{
printf(
"Usage: compiler [option...]\n"
"\n"
" Options:\n"
"\n"
" -h, --help Display this help text.\n"
" --lex Run the lexer.\n"
" --parse Run the parser. (Calls the lexer.)\n"
" --translate Run the translator. (Calls the parser.)\n"
);
}
else if (action == ACT_LEX)
{
is_done = 0;
lexer = lexer_create(in_buffer);
token = token_create();
while (!is_done)
{
lexer_lex(lexer, token);
token_print(token, stdout);
printf("\n");
if (token_get_class(token) == T_EOF)
is_done = 1;
}
token_destroy(token);
lexer_destroy(lexer);
return EXIT_SUCCESS;
}
else if (action == ACT_PARSE)
{
parser = parser_create(in_buffer);
parser_parse(parser);
parser_destroy(parser);
return EXIT_SUCCESS;
}
else if (action == ACT_TRANS)
{
parser = parser_create(in_buffer);
parser_parse(parser);
translator_translate(parser_get_tree(parser));
parser_destroy(parser);
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}