comparison lwcc/cpp.c @ 295:4b17780f2777 ccdev

Checkpoint lwcc development Changed tactics with the preprocessor. Instead of getting clever and trying to do things the "fast" way, instead, just tokenize the whole input and process it that way. Also, set up so the preprocessor and compiler can be integrated instead of having to have a specifically correct output for the preprocessed file. Also removed the subdirectories in the lwcc directory. It made things more complicated than they needed to be.
author William Astle <lost@l-w.ca>
date Thu, 12 Sep 2013 22:06:26 -0600
parents
children 83fcc1ed6ad6
comparison
equal deleted inserted replaced
294:048adfee2933 295:4b17780f2777
1 /*
2 lwcc/cpp.c
3
4 Copyright © 2013 William Astle
5
6 This file is part of LWTOOLS.
7
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <lw_alloc.h>
28 #include <lw_string.h>
29
30 #include "cpp.h"
31
32 struct token *preproc_lex_next_token(struct preproc_info *);
33
34 struct preproc_info *preproc_init(const char *fn)
35 {
36 FILE *fp;
37 struct preproc_info *pp;
38
39 if (!fn || (fn[0] == '-' && fn[1] == '0'))
40 {
41 fp = stdin;
42 }
43 else
44 {
45 fp = fopen(fn, "rb");
46 }
47 if (!fp)
48 return NULL;
49
50 pp = lw_alloc(sizeof(struct preproc_info));
51 memset(pp, 0, sizeof(struct preproc_info));
52 pp -> fn = lw_strdup(fn);
53 pp -> fp = fp;
54 pp -> ra = CPP_NOUNG;
55 return pp;
56 }
57
58 struct token *preproc_next_token(struct preproc_info *pp)
59 {
60 struct token *t;
61
62 if (pp -> tokqueue)
63 {
64 t = pp -> tokqueue;
65 pp -> tokqueue = t -> next;
66 if (pp -> tokqueue)
67 pp -> tokqueue -> prev = NULL;
68 t -> next = NULL;
69 t -> prev = NULL;
70 return t;
71 }
72 return(preproc_lex_next_token(pp));
73 }
74
75 void preproc_finish(struct preproc_info *pp)
76 {
77 lw_free((void *)(pp -> fn));
78 fclose(pp -> fp);
79 lw_free(pp);
80 }
81
82 void preproc_register_error_callback(struct preproc_info *pp, void (*cb)(const char *))
83 {
84 pp -> errorcb = cb;
85 }
86
87 void preproc_register_warning_callback(struct preproc_info *pp, void (*cb)(const char *))
88 {
89 pp -> warningcb = cb;
90 }
91
92 static void preproc_throw_error_default(const char *m)
93 {
94 fprintf(stderr, "ERROR: %s\n", m);
95 }
96
97 static void preproc_throw_warning_default(const char *m)
98 {
99 fprintf(stderr, "WARNING: %s\n", m);
100 }
101
102 static void preproc_throw_message(void (*cb)(const char *), const char *m, va_list args)
103 {
104 int s;
105 char *b;
106
107 s = vsnprintf(NULL, 0, m, args);
108 b = lw_alloc(s + 1);
109 vsnprintf(b, s + 1, m, args);
110 (*cb)(b);
111 lw_free(b);
112 }
113
114 void preproc_throw_error(struct preproc_info *pp, const char *m, ...)
115 {
116 va_list args;
117 va_start(args, m);
118 preproc_throw_message(pp -> errorcb ? pp -> errorcb : preproc_throw_error_default, m, args);
119 va_end(args);
120 exit(1);
121 }
122
123 void preproc_throw_warning(struct preproc_info *pp, const char *m, ...)
124 {
125 va_list args;
126 va_start(args, m);
127 preproc_throw_message(pp -> warningcb ? pp -> warningcb : preproc_throw_warning_default, m, args);
128 va_end(args);
129 }