-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathynicc.c
72 lines (58 loc) · 1.51 KB
/
ynicc.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
#include "ynicc.h"
// 現在着目しているトークン
Token *token;
// 入力ファイル名
char *filename;
// 入力プログラム
char *user_input;
int main(int argc, char **argv) {
bool f_dump_ast = false;
bool f_dump_ast_only = false;
bool f_dump_tokens = false;
if (argc < 2) {
fprintf(stderr, "引数の個数が正しくありません\n");
return 1;
}
if (argc > 2) {
for (int i = 1; i < argc - 1; i++) {
if (strcmp(argv[i], "--ast") == 0) {
f_dump_ast = true;
}
if (strcmp(argv[i], "--ast-only") == 0) {
f_dump_ast = true;
f_dump_ast_only = true;
}
if (strcmp(argv[i], "--tokens") == 0) {
f_dump_tokens = true;
}
}
}
// プログラム全体を保存
filename = argv[argc - 1];
user_input = read_file(filename);
// head はfree用
Token *head = token = tokenize(user_input);
// fprintf(stderr, "-------------------------------- tokenized\n");
if (f_dump_tokens) {
printf("##-----------------------------\n");
printf("## tokens\n");
dump_tokens(head);
}
// パースする(結果は グローバル変数のfunctionsに入る)
Program *pgm = program();
if (f_dump_ast) {
printf("##-----------------------------\n");
printf("## ast\n");
dump_globals(pgm->global_var);
for (Function *f = pgm->functions; f; f = f->next) {
if (f_dump_ast) {
dump_function(f);
}
}
}
if (!f_dump_ast_only) {
codegen(pgm);
}
free_tokens(head);
return 0;
}