Initial import of c parser
[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
11 #if 0
12 static
13 void get_output_name(char *buf, size_t buflen, const char *inputname,
14                      const char *newext)
15 {
16         size_t last_dot = 0xffffffff;
17         size_t i = 0;
18         for(const char *c = inputname; *c != 0; ++c) {
19                 if(*c == '.')
20                         last_dot = i;
21                 ++i;
22         }
23         if(last_dot == 0xffffffff)
24                 last_dot = i;
25
26         if(last_dot >= buflen)
27                 panic("filename too long");
28         memcpy(buf, inputname, last_dot);
29
30         size_t extlen = strlen(newext) + 1;
31         if(extlen + last_dot >= buflen)
32                 panic("filename too long");
33         memcpy(buf+last_dot, newext, extlen);
34 }
35 #endif
36
37 static
38 void compile(const char *fname)
39 {
40         lexer_t         lexer;
41         token_t         token;
42
43         FILE *in = fopen(fname, "r");
44         if(in == NULL) {
45                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
46                 exit(1);
47         }
48
49         lexer_init(&lexer, in, fname);
50
51         do {
52                 lexer_next_token(&lexer, &token);
53                 print_token(stdout, &token);
54                 puts("");
55         } while(token.type != T_EOF);
56
57         lexer_destroy(&lexer);
58         fclose(in);
59 }
60
61 int main(int argc, char **argv)
62 {
63         init_symbol_table();
64         init_tokens();
65
66         for(int i = 1; i < argc; ++i) {
67                 compile(argv[i]);
68         }
69
70         exit_tokens();
71         exit_symbol_table();
72         return 0;
73 }