add --print_ast option
[cparser] / main.c
1 #include <config.h>
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7
8 #include "lexer.h"
9 #include "token_t.h"
10 #include "type_hash.h"
11 #include "parser.h"
12
13 #if 0
14 static
15 void get_output_name(char *buf, size_t buflen, const char *inputname,
16                      const char *newext)
17 {
18         size_t last_dot = 0xffffffff;
19         size_t i = 0;
20         for(const char *c = inputname; *c != 0; ++c) {
21                 if(*c == '.')
22                         last_dot = i;
23                 ++i;
24         }
25         if(last_dot == 0xffffffff)
26                 last_dot = i;
27
28         if(last_dot >= buflen)
29                 panic("filename too long");
30         memcpy(buf, inputname, last_dot);
31
32         size_t extlen = strlen(newext) + 1;
33         if(extlen + last_dot >= buflen)
34                 panic("filename too long");
35         memcpy(buf+last_dot, newext, extlen);
36 }
37 #endif
38
39 static
40 translation_unit_t *do_parsing(const char *fname)
41 {
42         FILE *in = fopen(fname, "r");
43         if(in == NULL) {
44                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
45                 exit(1);
46         }
47
48         lexer_open_stream(in, fname);
49
50         translation_unit_t *unit = parse();
51
52         fclose(in);
53
54         return unit;
55 }
56
57 static
58 void lextest(const char *fname)
59 {
60         FILE *in = fopen(fname, "r");
61         if(in == NULL) {
62                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
63                 exit(1);
64         }
65
66         lexer_open_stream(in, fname);
67
68         do {
69                 lexer_next_preprocessing_token();
70                 print_token(stdout, &lexer_token);
71                 puts("");
72         } while(lexer_token.type != T_EOF);
73
74         fclose(in);
75 }
76
77 void write_fluffy_decls(translation_unit_t *unit);
78
79 int main(int argc, char **argv)
80 {
81         init_symbol_table();
82         init_tokens();
83         init_lexer();
84         init_types();
85         init_typehash();
86         init_ast();
87         init_parser();
88
89         if(argc > 2 && strcmp(argv[1], "--lextest") == 0) {
90                 lextest(argv[2]);
91                 return 0;
92         }
93
94         if(argc > 2 && strcmp(argv[1], "--print-ast") == 0) {
95                 translation_unit_t *unit = do_parsing(argv[2]);
96                 ast_set_output(stdout);
97                 print_ast(unit);
98                 return 0;
99         }
100
101         for(int i = 1; i < argc; ++i) {
102                 translation_unit_t *unit = do_parsing(argv[i]);
103                 write_fluffy_decls(unit);
104         }
105
106         exit_parser();
107         exit_ast();
108         exit_typehash();
109         exit_types();
110         exit_lexer();
111         exit_tokens();
112         exit_symbol_table();
113         return 0;
114 }