changed the way preprocessing directives are parsed
[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_t.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 void compile(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 #if 1
51         token_t token;
52         do {
53                 lexer_next_token(&token);
54                 print_token(stdout, &token);
55                 puts("");
56         } while(token.type != T_EOF);
57 #else
58         parse();
59 #endif
60
61         fclose(in);
62 }
63
64 static
65 void lextest(const char *fname)
66 {
67         FILE *in = fopen(fname, "r");
68         if(in == NULL) {
69                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
70                 exit(1);
71         }
72
73         lexer_open_stream(in, fname);
74
75         token_t token;
76         do {
77                 lexer_next_preprocessing_token(&token);
78                 print_token(stdout, &token);
79                 puts("");
80         } while(token.type != T_EOF);
81
82         fclose(in);
83 }
84
85 int main(int argc, char **argv)
86 {
87         init_symbol_table();
88         init_tokens();
89         init_lexer();
90         init_types();
91         init_typehash();
92         init_ast();
93         init_parser();
94
95         if(argc > 2 && strcmp(argv[1], "--lextest") == 0) {
96                 lextest(argv[2]);
97                 return 0;
98         }
99
100         for(int i = 1; i < argc; ++i) {
101                 compile(argv[i]);
102         }
103
104         exit_parser();
105         exit_ast();
106         exit_typehash();
107         exit_types();
108         exit_lexer();
109         exit_tokens();
110         exit_symbol_table();
111         return 0;
112 }