-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
73 lines (60 loc) · 1.75 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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "tokenizer.h"
#include "parser.h"
#include "codegen.h"
extern tokenizer *current_tokenizer;
void debug_tokens() {
while(tokenizer_peek()->type != TOK_EOF) {
token *tok = tokenizer_get();
char *s1 = get_string_from_toktype(tok->type);
char *s2 = token_to_string(tok);
if(strcmp(s1, s2) == 0) {
printf("%s\n", s2);
} else {
int n = printf("%s", s2);
printf("%*s%s\n", 20 - n, "", s1);
}
}
exit(0);
}
int main(int argc, char *argv[]) {
char *input_filename = "stdin";
char *output_filename = "stdout";
FILE *input = stdin;
FILE *output = stdout;
if(argc == 2) {
input_filename = argv[1];
input = fopen(input_filename, "r");
if(input == NULL) {
fprintf(stderr, "%s\n", strerror(errno));
return 1;
}
} else if(argc == 3) {
input_filename = argv[1];
output_filename = argv[2];
input = fopen(input_filename, "r");
output = fopen(output_filename, "w");
if(input == NULL || output == NULL) {
fprintf(stderr, "%s\n", strerror(errno));
return 1;
}
} else if(argc > 3) {
fprintf(stderr, "Usage: %s [input] [output]\n", argv[0]);
return 1;
}
init_tokenizer();
current_tokenizer = tokenizer_create(NULL, input, input_filename);
//debug_tokens();
tree *AST = parse();
FILE *outputf = fopen("output.sall", "w");
if(outputf == NULL) {
printf("Cannot open file for writing.\n");
return 1;
}
generate(outputf, AST);
// TODO: free reader
return 0;
}