Mercurial > hg > index.cgi
comparison lwcc/cc-main.c @ 312:41118fb0a8f2 ccdev
Add no-op parser and parse tree infrastructure
author | William Astle <lost@l-w.ca> |
---|---|
date | Sat, 21 Sep 2013 18:43:39 -0600 |
parents | 7957e90d0a35 |
children | a38542cf4cc6 |
comparison
equal
deleted
inserted
replaced
311:7957e90d0a35 | 312:41118fb0a8f2 |
---|---|
28 #include <lw_stringlist.h> | 28 #include <lw_stringlist.h> |
29 #include <lw_cmdline.h> | 29 #include <lw_cmdline.h> |
30 #include <lw_string.h> | 30 #include <lw_string.h> |
31 | 31 |
32 #include "cpp.h" | 32 #include "cpp.h" |
33 | 33 #include "tree.h" |
34 int process_file(const char *); | 34 |
35 node_t *process_file(const char *); | |
35 static void do_error(const char *f, ...); | 36 static void do_error(const char *f, ...); |
37 extern node_t *parse_program(struct preproc_info *pp); | |
38 | |
39 node_t *program_tree = NULL; | |
36 | 40 |
37 /* command line option handling */ | 41 /* command line option handling */ |
38 #define PROGVER "lwcc-cc from " PACKAGE_STRING | 42 #define PROGVER "lwcc-cc from " PACKAGE_STRING |
39 char *program_name; | 43 char *program_name; |
40 | 44 |
110 | 114 |
111 int main(int argc, char **argv) | 115 int main(int argc, char **argv) |
112 { | 116 { |
113 program_name = argv[0]; | 117 program_name = argv[0]; |
114 int retval = 0; | 118 int retval = 0; |
119 node_t *n; | |
115 | 120 |
116 input_files = lw_stringlist_create(); | 121 input_files = lw_stringlist_create(); |
117 includedirs = lw_stringlist_create(); | 122 includedirs = lw_stringlist_create(); |
118 sysincludedirs = lw_stringlist_create(); | 123 sysincludedirs = lw_stringlist_create(); |
119 macrolist = lw_stringlist_create(); | 124 macrolist = lw_stringlist_create(); |
133 { | 138 { |
134 do_error("Failed to create output file %s: %s", output_file, strerror(errno)); | 139 do_error("Failed to create output file %s: %s", output_file, strerror(errno)); |
135 } | 140 } |
136 } | 141 } |
137 | 142 |
143 program_tree = node_create(NODE_PROGRAM); | |
144 | |
138 if (lw_stringlist_nstrings(input_files) == 0) | 145 if (lw_stringlist_nstrings(input_files) == 0) |
139 { | 146 { |
140 /* if no input files, work on stdin */ | 147 /* if no input files, work on stdin */ |
141 retval = process_file("-"); | 148 n = process_file("-"); |
149 if (!n) | |
150 retval = 1; | |
151 else | |
152 node_addchild(program_tree, n); | |
142 } | 153 } |
143 else | 154 else |
144 { | 155 { |
145 char *s; | 156 char *s; |
146 lw_stringlist_reset(input_files); | 157 lw_stringlist_reset(input_files); |
147 for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files)) | 158 for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files)) |
148 { | 159 { |
149 retval = process_file(s); | 160 n = process_file(s); |
161 if (!n) | |
162 retval = 1; | |
150 if (retval != 0) | 163 if (retval != 0) |
151 break; | 164 break; |
165 node_addchild(program_tree, n); | |
152 } | 166 } |
153 } | 167 } |
154 lw_stringlist_destroy(input_files); | 168 lw_stringlist_destroy(input_files); |
155 lw_stringlist_destroy(includedirs); | 169 lw_stringlist_destroy(includedirs); |
156 lw_stringlist_destroy(sysincludedirs); | 170 lw_stringlist_destroy(sysincludedirs); |
157 lw_stringlist_destroy(macrolist); | 171 lw_stringlist_destroy(macrolist); |
172 | |
173 node_display(program_tree, stdout); | |
174 node_destroy(program_tree); | |
158 exit(retval); | 175 exit(retval); |
159 } | 176 } |
160 | 177 |
161 static void print_line_marker(FILE *fp, int line, const char *fn, int flag) | 178 node_t *process_file(const char *fn) |
162 { | |
163 fprintf(fp, "\n# %d \"", line); | |
164 while (*fn) | |
165 { | |
166 if (*fn < 32 || *fn == 34 || *fn > 126) | |
167 { | |
168 fprintf(fp, "\\%03o", *fn); | |
169 } | |
170 else | |
171 { | |
172 fprintf(fp, "%c", *fn); | |
173 } | |
174 fn++; | |
175 } | |
176 fprintf(fp, "\" %d", flag); | |
177 } | |
178 | |
179 int process_file(const char *fn) | |
180 { | 179 { |
181 struct preproc_info *pp; | 180 struct preproc_info *pp; |
182 struct token *tok = NULL; | |
183 int last_line = 0; | |
184 char *last_fn = NULL; | |
185 char *tstr; | 181 char *tstr; |
186 | 182 node_t *n; |
183 | |
187 pp = preproc_init(fn); | 184 pp = preproc_init(fn); |
188 if (!pp) | 185 if (!pp) |
189 return -1; | 186 return NULL; |
190 | 187 |
191 /* set up the include paths */ | 188 /* set up the include paths */ |
192 lw_stringlist_reset(includedirs); | 189 lw_stringlist_reset(includedirs); |
193 for (tstr = lw_stringlist_current(includedirs); tstr; tstr = lw_stringlist_next(includedirs)) | 190 for (tstr = lw_stringlist_current(includedirs); tstr; tstr = lw_stringlist_next(includedirs)) |
194 { | 191 { |
206 for (tstr = lw_stringlist_current(macrolist); tstr; tstr = lw_stringlist_next(macrolist)) | 203 for (tstr = lw_stringlist_current(macrolist); tstr; tstr = lw_stringlist_next(macrolist)) |
207 { | 204 { |
208 preproc_add_macro(pp, tstr); | 205 preproc_add_macro(pp, tstr); |
209 } | 206 } |
210 | 207 |
211 print_line_marker(output_fp, 1, fn, 1); | 208 n = parse_program(pp); |
212 last_fn = lw_strdup(fn); | |
213 for (;;) | |
214 { | |
215 tok = preproc_next(pp); | |
216 if (tok -> ttype == TOK_EOF) | |
217 break; | |
218 if (strcmp(tok -> fn, last_fn) != 0) | |
219 { | |
220 int lt = 1; | |
221 if (tok -> lineno != 1) | |
222 { | |
223 lt = 2; | |
224 } | |
225 lw_free(last_fn); | |
226 last_fn = lw_strdup(tok -> fn); | |
227 last_line = tok -> lineno; | |
228 print_line_marker(output_fp, last_line, last_fn, lt); | |
229 } | |
230 else | |
231 { | |
232 while (tok -> lineno > last_line) | |
233 { | |
234 fprintf(output_fp, "\n"); | |
235 last_line++; | |
236 } | |
237 } | |
238 token_print(tok, output_fp); | |
239 if (tok -> ttype == TOK_EOL) | |
240 last_line++; | |
241 token_free(tok); | |
242 } | |
243 token_free(tok); | |
244 lw_free(last_fn); | |
245 // symtab_dump(pp); | |
246 preproc_finish(pp); | 209 preproc_finish(pp); |
247 return 0; | 210 return n; |
248 } | 211 } |
249 | 212 |
250 static void do_error(const char *f, ...) | 213 static void do_error(const char *f, ...) |
251 { | 214 { |
252 va_list args; | 215 va_list args; |