annotate lwcc/cc-main.c @ 314:a3e277c58df9 ccdev

Checkpoint parser development for lwcc Beginning of lemon based parser for C including all the infrastructure for calling the lemon generated parser. This requires a translation layer from the preprocessor token numbers to the lemon parser token numbers due to the fact that lemon wants to control the token numbers. Eventually when the lemon parser gets replaced with a hand crafted recursive descent parser, this translation will no longer be required. However, in the interest of getting things working sooner rather than later, using something like lemon is beneficial.
author William Astle <lost@l-w.ca>
date Sun, 17 Nov 2013 11:59:36 -0700
parents 41118fb0a8f2
children a38542cf4cc6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/cpp-main.c
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
3
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
5
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
7
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
12
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
17
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
21
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <errno.h>
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <stdarg.h>
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
24 #include <stdio.h>
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <stdlib.h>
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
26 #include <string.h>
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
27
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include <lw_stringlist.h>
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
29 #include <lw_cmdline.h>
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
30 #include <lw_string.h>
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
31
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
32 #include "cpp.h"
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
33 #include "tree.h"
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
34
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
35 node_t *process_file(const char *);
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
36 static void do_error(const char *f, ...);
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
37 extern node_t *parse_program(struct preproc_info *pp);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
38
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
39 node_t *program_tree = NULL;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
40
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
41 /* command line option handling */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
42 #define PROGVER "lwcc-cc from " PACKAGE_STRING
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
43 char *program_name;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
44
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
45 /* input files */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
46 lw_stringlist_t input_files;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
47 lw_stringlist_t includedirs;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
48 lw_stringlist_t sysincludedirs;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
49 lw_stringlist_t macrolist;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
50
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
51 /* various flags */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
52 int trigraphs = 0;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
53 char *output_file = NULL;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
54 FILE *output_fp = NULL;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
55
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
56 static struct lw_cmdline_options options[] =
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
57 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
58 { "output", 'o', "FILE", 0, "Output to FILE"},
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
59 { "include", 'i', "FILE", 0, "Pre-include FILE" },
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
60 { "includedir", 'I', "PATH", 0, "Add entry to the user include path" },
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
61 { "sincludedir", 'S', "PATH", 0, "Add entry to the system include path" },
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
62 { "define", 'D', "SYM[=VAL]",0, "Automatically define SYM to be VAL (or 1)"},
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
63 { "trigraphs", 0x100, NULL, 0, "Enable interpretation of trigraphs" },
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
64 { 0 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
65 };
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
66
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
67 static int parse_opts(int key, char *arg, void *state)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
68 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
69 switch (key)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
70 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
71 case 'o':
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
72 if (output_file)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
73 do_error("Output file specified more than once.");
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
74 output_file = arg;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
75 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
76
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
77 case 0x100:
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
78 trigraphs = 1;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
79 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
80
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
81 case 'I':
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
82 lw_stringlist_addstring(includedirs, arg);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
83 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
84
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
85 case 'S':
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
86 lw_stringlist_addstring(sysincludedirs, arg);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
87 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
88
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
89 case 'D':
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
90 lw_stringlist_addstring(macrolist, arg);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
91 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
92
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
93 case lw_cmdline_key_end:
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
94 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
95
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
96 case lw_cmdline_key_arg:
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
97 lw_stringlist_addstring(input_files, arg);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
98 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
99
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
100 default:
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
101 return lw_cmdline_err_unknown;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
102 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
103 return 0;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
104 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
105
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
106 static struct lw_cmdline_parser cmdline_parser =
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
107 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
108 options,
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
109 parse_opts,
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
110 "INPUTFILE",
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
111 "lwcc-cc - C compiler for lwcc",
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
112 PROGVER
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
113 };
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
114
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
115 int main(int argc, char **argv)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
116 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
117 program_name = argv[0];
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
118 int retval = 0;
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
119 node_t *n;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
120
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
121 input_files = lw_stringlist_create();
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
122 includedirs = lw_stringlist_create();
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
123 sysincludedirs = lw_stringlist_create();
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
124 macrolist = lw_stringlist_create();
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
125
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
126 /* parse command line arguments */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
127 lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
128
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
129 /* set up output file */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
130 if (output_file == NULL || strcmp(output_file, "-") == 0)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
131 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
132 output_fp = stdout;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
133 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
134 else
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
135 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
136 output_fp = fopen(output_file, "wb");
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
137 if (output_fp == NULL)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
138 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
139 do_error("Failed to create output file %s: %s", output_file, strerror(errno));
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
140 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
141 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
142
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
143 program_tree = node_create(NODE_PROGRAM);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
144
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
145 if (lw_stringlist_nstrings(input_files) == 0)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
146 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
147 /* if no input files, work on stdin */
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
148 n = process_file("-");
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
149 if (!n)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
150 retval = 1;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
151 else
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
152 node_addchild(program_tree, n);
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
153 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
154 else
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
155 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
156 char *s;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
157 lw_stringlist_reset(input_files);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
158 for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files))
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
159 {
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
160 n = process_file(s);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
161 if (!n)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
162 retval = 1;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
163 if (retval != 0)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
164 break;
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
165 node_addchild(program_tree, n);
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
166 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
167 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
168 lw_stringlist_destroy(input_files);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
169 lw_stringlist_destroy(includedirs);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
170 lw_stringlist_destroy(sysincludedirs);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
171 lw_stringlist_destroy(macrolist);
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
172
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
173 node_display(program_tree, stdout);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
174 node_destroy(program_tree);
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
175 exit(retval);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
176 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
177
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
178 node_t *process_file(const char *fn)
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
179 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
180 struct preproc_info *pp;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
181 char *tstr;
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
182 node_t *n;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
183
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
184 pp = preproc_init(fn);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
185 if (!pp)
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
186 return NULL;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
187
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
188 /* set up the include paths */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
189 lw_stringlist_reset(includedirs);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
190 for (tstr = lw_stringlist_current(includedirs); tstr; tstr = lw_stringlist_next(includedirs))
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
191 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
192 preproc_add_include(pp, tstr, 0);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
193 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
194
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
195 lw_stringlist_reset(sysincludedirs);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
196 for (tstr = lw_stringlist_current(sysincludedirs); tstr; tstr = lw_stringlist_next(sysincludedirs))
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
197 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
198 preproc_add_include(pp, tstr, 1);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
199 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
200
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
201 /* set up pre-defined macros */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
202 lw_stringlist_reset(macrolist);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
203 for (tstr = lw_stringlist_current(macrolist); tstr; tstr = lw_stringlist_next(macrolist))
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
204 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
205 preproc_add_macro(pp, tstr);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
206 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
207
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
208 n = parse_program(pp);
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
209 preproc_finish(pp);
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
210 return n;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
211 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
212
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
213 static void do_error(const char *f, ...)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
214 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
215 va_list args;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
216 va_start(args, f);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
217 fprintf(stderr, "ERROR: ");
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
218 vfprintf(stderr, f, args);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
219 va_end(args);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
220 fprintf(stderr, "\n");
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
221 exit(1);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
222 }