Mercurial > hg > index.cgi
annotate lwcc/cpp.c @ 304:d85d173ba120 ccdev
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
The preprocessor is currently runnable but doesn't actually do anything
useful. This is just a checkpoint.
author | William Astle <lost@l-w.ca> |
---|---|
date | Tue, 17 Sep 2013 19:33:41 -0600 |
parents | 83fcc1ed6ad6 |
children | 54f213c8fb81 |
rev | line source |
---|---|
295 | 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" | |
304
d85d173ba120
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
296
diff
changeset
|
31 #include "strpool.h" |
295 | 32 |
33 struct token *preproc_lex_next_token(struct preproc_info *); | |
34 | |
35 struct preproc_info *preproc_init(const char *fn) | |
36 { | |
37 FILE *fp; | |
38 struct preproc_info *pp; | |
39 | |
40 if (!fn || (fn[0] == '-' && fn[1] == '0')) | |
41 { | |
42 fp = stdin; | |
43 } | |
44 else | |
45 { | |
46 fp = fopen(fn, "rb"); | |
47 } | |
48 if (!fp) | |
49 return NULL; | |
50 | |
51 pp = lw_alloc(sizeof(struct preproc_info)); | |
52 memset(pp, 0, sizeof(struct preproc_info)); | |
304
d85d173ba120
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
296
diff
changeset
|
53 pp -> strpool = strpool_create(); |
d85d173ba120
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
296
diff
changeset
|
54 pp -> fn = strpool_strdup(pp -> strpool, fn); |
295 | 55 pp -> fp = fp; |
56 pp -> ra = CPP_NOUNG; | |
296 | 57 pp -> ppeolseen = 1; |
295 | 58 return pp; |
59 } | |
60 | |
61 struct token *preproc_next_token(struct preproc_info *pp) | |
62 { | |
63 struct token *t; | |
64 | |
296 | 65 if (pp -> curtok) |
66 token_free(pp -> curtok); | |
67 | |
68 /* | |
69 If there is a list of tokens to process, move it to the "unget" queue | |
70 with an EOF marker at the end of it. | |
71 */ | |
72 if (pp -> sourcelist) | |
73 { | |
74 for (t = pp -> sourcelist; t -> next; t = t -> next) | |
75 /* do nothing */ ; | |
76 t -> next = token_create(TOK_EOF, NULL, -1, -1, ""); | |
77 t -> next -> next = pp -> tokqueue; | |
78 pp -> tokqueue = pp -> sourcelist; | |
79 pp -> sourcelist = NULL; | |
80 } | |
81 again: | |
295 | 82 if (pp -> tokqueue) |
83 { | |
84 t = pp -> tokqueue; | |
85 pp -> tokqueue = t -> next; | |
86 if (pp -> tokqueue) | |
87 pp -> tokqueue -> prev = NULL; | |
88 t -> next = NULL; | |
89 t -> prev = NULL; | |
296 | 90 pp -> curtok = t; |
91 goto ret; | |
295 | 92 } |
296 | 93 pp -> curtok = preproc_lex_next_token(pp); |
94 t = pp -> curtok; | |
95 ret: | |
96 if (t -> ttype == TOK_ENDEXPAND) | |
97 { | |
98 struct expand_e *e; | |
99 e = pp -> expand_list; | |
100 pp -> expand_list = e -> next; | |
101 lw_free(e); | |
102 goto again; | |
103 } | |
104 return t; | |
105 } | |
106 | |
107 void preproc_unget_token(struct preproc_info *pp, struct token *t) | |
108 { | |
109 t -> next = pp -> tokqueue; | |
110 pp -> tokqueue = t; | |
111 if (pp -> curtok == t) | |
112 pp -> curtok = NULL; | |
295 | 113 } |
114 | |
115 void preproc_finish(struct preproc_info *pp) | |
116 { | |
117 fclose(pp -> fp); | |
296 | 118 if (pp -> curtok) |
119 token_free(pp -> curtok); | |
120 while (pp -> tokqueue) | |
121 { | |
122 preproc_next_token(pp); | |
123 token_free(pp -> curtok); | |
124 } | |
304
d85d173ba120
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
296
diff
changeset
|
125 strpool_free(pp -> strpool); |
295 | 126 lw_free(pp); |
127 } | |
128 | |
129 void preproc_register_error_callback(struct preproc_info *pp, void (*cb)(const char *)) | |
130 { | |
131 pp -> errorcb = cb; | |
132 } | |
133 | |
134 void preproc_register_warning_callback(struct preproc_info *pp, void (*cb)(const char *)) | |
135 { | |
136 pp -> warningcb = cb; | |
137 } | |
138 | |
139 static void preproc_throw_error_default(const char *m) | |
140 { | |
141 fprintf(stderr, "ERROR: %s\n", m); | |
142 } | |
143 | |
144 static void preproc_throw_warning_default(const char *m) | |
145 { | |
146 fprintf(stderr, "WARNING: %s\n", m); | |
147 } | |
148 | |
149 static void preproc_throw_message(void (*cb)(const char *), const char *m, va_list args) | |
150 { | |
151 int s; | |
152 char *b; | |
153 | |
154 s = vsnprintf(NULL, 0, m, args); | |
155 b = lw_alloc(s + 1); | |
156 vsnprintf(b, s + 1, m, args); | |
157 (*cb)(b); | |
158 lw_free(b); | |
159 } | |
160 | |
161 void preproc_throw_error(struct preproc_info *pp, const char *m, ...) | |
162 { | |
163 va_list args; | |
164 va_start(args, m); | |
165 preproc_throw_message(pp -> errorcb ? pp -> errorcb : preproc_throw_error_default, m, args); | |
166 va_end(args); | |
167 exit(1); | |
168 } | |
169 | |
170 void preproc_throw_warning(struct preproc_info *pp, const char *m, ...) | |
171 { | |
172 va_list args; | |
173 va_start(args, m); | |
174 preproc_throw_message(pp -> warningcb ? pp -> warningcb : preproc_throw_warning_default, m, args); | |
175 va_end(args); | |
176 } |