-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
121 lines (106 loc) · 2.72 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "m99cc.h"
// Store variable look up table
// Put variables into vector, and make a list of variable for each code block.
Map *global_symbols;
Vector *local_symbols;
Vector *string_literals;
Map *global_struct_table;
// for debugging.
void dump_symbols(Map *);
void dump_tree(Vector *code);
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Arguments number not right.\n");
fprintf(stderr, "Usage: %s [file] [-test] [-test_token]"
" [-test_parse] [-dump_tree] "
"[-dump_symbols][-dump_tokens]\n", argv[0]);
return 1;
}
bool dump_tokens_enable = false;
bool dump_tree_enable = false;
bool dump_symbols_enable = false;
FILE *srcfile;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-test") == 0) {
runtest();
return 0;
}
if (strcmp(argv[i], "-test_token") == 0) {
runtest_tokenize();
return 0;
}
if (strcmp(argv[i], "-test_parse") == 0) {
runtest_parse();
return 0;
}
if (strcmp(argv[i], "-test_dtype") == 0) {
runtest_data_type();
return 0;
}
if (strcmp(argv[i], "-dump_tokens") == 0) {
dump_tokens_enable = true;
continue;
}
if (strcmp(argv[i], "-dump_tree") == 0) {
dump_tree_enable = true;
continue;
}
if (strcmp(argv[i], "-dump_symbols") == 0) {
dump_symbols_enable = true;
continue;
}
srcfile = fopen(argv[i], "r");
}
// initialize
// Store nodes in this vector.
Vector *program_code;
// program_code = new_vector();
global_symbols = new_map();
local_symbols = new_vector();
string_literals = new_vector();
global_struct_table = new_map();
// open input
if (srcfile == NULL) {
fprintf(stderr, "Can't open file %s\n", argv[1]);
exit(1);
}
fseek( srcfile , 0L , SEEK_END);
long size = ftell( srcfile );
rewind( srcfile );
char *src = malloc(size + 1);
char *sp = src;
while(!feof(srcfile)) {
*(sp++) = fgetc(srcfile);
}
*(sp-1) = '\0';
fclose(srcfile);
// Tokenize
Vector *tokens;
tokens = tokenize(src);
if (dump_tokens_enable) {
dump_token();
}
// Parse
program_code = parse(tokens);
if (dump_tree_enable) {
dump_tree(program_code);
}
program_code = analysis(program_code);
if (dump_symbols_enable) {
fprintf(stderr, "Global Symbols: \n");
dump_symbols(global_symbols);
for(int i = 0; i < local_symbols->len; i++) {
if (local_symbols->data[i]) {
fprintf(stderr, "Local Symbols [%d]: \n", i);
dump_symbols((Map *)(local_symbols->data[i]));
}
}
dump_struct_table(global_struct_table);
}
gen_program(program_code);
return 0;
}