no enviornment passing around anymore in lexer, more work on lexer, import expression...
[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         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_open_stream(in, fname);
50
51         do {
52                 lexer_next_token(&token);
53                 print_token(stdout, &token);
54                 puts("");
55         } while(token.type != T_EOF);
56
57         fclose(in);
58 }
59
60 int main(int argc, char **argv)
61 {
62         init_symbol_table();
63         init_tokens();
64         init_lexer();
65         init_parser();
66
67         for(int i = 1; i < argc; ++i) {
68                 compile(argv[i]);
69         }
70
71         exit_parser();
72         exit_lexer();
73         exit_tokens();
74         exit_symbol_table();
75         return 0;
76 }