eb0c479ad8a1ba318bff2bc1087d3fd9936e82b9
[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 0
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 int main(int argc, char **argv)
65 {
66         init_symbol_table();
67         init_tokens();
68         init_lexer();
69         init_types();
70         init_typehash();
71         init_ast();
72         init_parser();
73
74         for(int i = 1; i < argc; ++i) {
75                 compile(argv[i]);
76         }
77
78         exit_parser();
79         exit_ast();
80         exit_typehash();
81         exit_types();
82         exit_lexer();
83         exit_tokens();
84         exit_symbol_table();
85         return 0;
86 }