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