/
main.c
174 lines (137 loc) · 3.99 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/time.h>
#include <unistd.h>
#include "array.h"
#include "lex.h"
#include "parse.h"
#include "comp_unit.h"
#include "token.h"
static bool DUMP_TOK_STREAM = false;
static bool
has_suffix(const char* str, const char* suffix) {
size_t str_len = strlen(str);
size_t suffix_len = strlen(suffix);
if (suffix_len > str_len) {
return false;
}
return !strcmp(&str[str_len - suffix_len], suffix);
}
static void
read_comp_unit(struct compilation_unit* unit) {
if (access(unit->path, F_OK) == -1) {
fprintf(stderr, "error: no such file '%s'\n", unit->path);
return;
}
// til i write my own pre-processor use the
// existing preprocessor from GCC
char pp_file_path[512];
sprintf(pp_file_path, "%s.ppc", unit->path);
{
char cmd[512] = {0};
sprintf(cmd, "cc -E %s > %s", unit->path, pp_file_path);
system(cmd);
}
FILE* file = fopen(pp_file_path, "r");
if (file == NULL) {
char error_msg[2048];
sprintf(error_msg, "error: failed to read file '%s'\n", pp_file_path);
perror(error_msg);
return;
}
fseek(file, 0, SEEK_END);
unit->length = ftell(file);
rewind(file);
printf(" - file '%s' is %zd bytes\n", unit->path, unit->length);
unit->contents = malloc(unit->length + 1);
if (unit->contents == NULL) {
fprintf(stderr, "error: failed to allocate memory for file '%s'\n", unit->path);
return;
}
fread(unit->contents, sizeof(*unit->contents), unit->length, file);
unit->contents[unit->length] = '\0';
fclose(file);
// remove the generated pp file
// once it has been read into memory.
remove(pp_file_path);
}
long long
curr_time_ms() {
struct timeval timer;
gettimeofday(&timer, NULL);
long long time = timer.tv_sec * 1000LL + timer.tv_usec / 1000;
return time;
}
int
main(int argc, char** argv) {
long long start_time = curr_time_ms();
if (argc <= 1) {
fprintf(stderr, "error: no input files\n");
return -1;
}
struct compilation_unit units[argc - 1];
memset(&units, 0, argc - 1);
printf("Loading compilation units:\n");
printf("- ");
int num_units = 0;
for (int i = 1; i < argc; i++) {
if (i > 1) printf(", ");
if (i % 5 == 0) putchar('\n');
if (has_suffix(argv[i], ".c")) {
printf("'%s'", argv[i]);
units[num_units++] = (struct compilation_unit){.path = argv[i]};
}
}
putchar('\n');
struct array* TOKEN_STREAMS[num_units];
struct array* AS_TREES[num_units];
// how many lines of code in the
// program we're compiling.
uint64_t program_loc = 0;
printf("Lex/parse on %d compilation unit(s)\n", num_units);
for (int i = 0; i < num_units; i++) {
struct compilation_unit* current_unit = &units[i];
// we do the file reading here just before we lex
// this is because we dont want to load all the files
// before we do anything, and if the lexer fails we wont
// have loads of memory allocated and it can just exit.
read_comp_unit(current_unit);
struct lexer lex_inst = {0};
struct lexer_info lex = tokenize(&lex_inst, current_unit);;
TOKEN_STREAMS[i] = lex.token_stream;
program_loc += lex.lines_lexed;
if (DUMP_TOK_STREAM) {
for (int i = 0; i < TOKEN_STREAMS[i]->size; i++) {
struct token* tok = array_get(TOKEN_STREAMS[i], i);
print_tok(tok);
}
}
printf(" - Tokenized %llu lines of code\n", program_loc);
AS_TREES[i] = parse(TOKEN_STREAMS[i]);
}
// cleanup stuff
{
for (int i = 0; i < num_units; i++) {
struct compilation_unit* unit = &units[i];
struct array* as_tree = AS_TREES[i];
for (size_t j = 0; j < as_tree->size; j++) {
struct ast_node* node = array_get(as_tree, j);
free(node);
}
destroy_array(as_tree);
struct array* token_set = TOKEN_STREAMS[i];
for (size_t j = 0; j < token_set->size; j++) {
struct token* tok = array_get(token_set, j);
free(tok);
}
destroy_array(token_set);
free(unit->contents);
}
}
long long end_time = curr_time_ms();
int time_taken_ms = end_time - start_time;
printf("compiled %zd/LOC in %d/ms\n", program_loc, time_taken_ms);
return 0;
}