-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
56 lines (48 loc) · 1.08 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
#include "9ccs.h"
/**
* Globals
* */
char *source;
char *filename;
Token *token;
Program *program;
Map *lvars;
Map *gvars;
Map *funcs;
Vector *found_strs;
int static_count;
char *read_file(char *path) {
FILE *fp = fopen(path, "r");
if (!fp) {
error("Cannot open %s: %s", path, strerror(errno));
}
if (fseek(fp, 0, SEEK_END) == -1) {
error("%s: fseek: %s", path, strerror(errno));
}
size_t size = ftell(fp);
if (fseek(fp, 0, SEEK_SET) == -1) {
error("%s: fseek: %s", path, strerror(errno));
}
char *buf = calloc(1, size + 2);
fread(buf, size, 1, fp);
if (size == 0 || buf[size - 1] != '\n') {
buf[size++] = '\n';
}
buf[size] = '\0';
fclose(fp);
return buf;
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "The number of arguments is incorrect.\n");
return 1;
}
static_count = 0;
filename = argv[1];
source = read_file(filename);
token = tokenize(source);
found_strs = new_vec();
program = parse();
gen_program(program);
return 0;
}