annotate lwcc/cc-main.c @ 496:a38542cf4cc6

Make lwcc stuff compile (on Linux anyway)
author William Astle <lost@l-w.ca>
date Mon, 05 Aug 2019 21:30:50 -0600
parents 41118fb0a8f2
children c3099c5d9d3e
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
496
a38542cf4cc6 Make lwcc stuff compile (on Linux anyway)
William Astle <lost@l-w.ca>
parents: 312
diff changeset
32 #include <version.h>
a38542cf4cc6 Make lwcc stuff compile (on Linux anyway)
William Astle <lost@l-w.ca>
parents: 312
diff changeset
33
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
34 #include "cpp.h"
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
35 #include "tree.h"
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
36
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
37 node_t *process_file(const char *);
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
38 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
39 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
40
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
41 node_t *program_tree = NULL;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
42
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
43 /* command line option handling */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
44 #define PROGVER "lwcc-cc from " PACKAGE_STRING
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
45 char *program_name;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
46
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
47 /* input files */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
48 lw_stringlist_t input_files;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
49 lw_stringlist_t includedirs;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
50 lw_stringlist_t sysincludedirs;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
51 lw_stringlist_t macrolist;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
52
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
53 /* various flags */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
54 int trigraphs = 0;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
55 char *output_file = NULL;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
56 FILE *output_fp = NULL;
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 static struct lw_cmdline_options options[] =
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
59 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
60 { "output", 'o', "FILE", 0, "Output to FILE"},
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
61 { "include", 'i', "FILE", 0, "Pre-include FILE" },
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
62 { "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
63 { "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
64 { "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
65 { "trigraphs", 0x100, NULL, 0, "Enable interpretation of trigraphs" },
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
66 { 0 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
67 };
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 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
70 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
71 switch (key)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
72 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
73 case 'o':
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
74 if (output_file)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
75 do_error("Output file specified more than once.");
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
76 output_file = arg;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
77 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
78
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
79 case 0x100:
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
80 trigraphs = 1;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
81 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
82
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
83 case 'I':
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
84 lw_stringlist_addstring(includedirs, arg);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
85 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
86
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
87 case 'S':
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
88 lw_stringlist_addstring(sysincludedirs, arg);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
89 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
90
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
91 case 'D':
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
92 lw_stringlist_addstring(macrolist, arg);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
93 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
94
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
95 case lw_cmdline_key_end:
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
96 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
97
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
98 case lw_cmdline_key_arg:
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
99 lw_stringlist_addstring(input_files, arg);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
100 break;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
101
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
102 default:
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
103 return lw_cmdline_err_unknown;
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 return 0;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
106 }
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 static struct lw_cmdline_parser cmdline_parser =
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
109 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
110 options,
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
111 parse_opts,
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
112 "INPUTFILE",
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
113 "lwcc-cc - C compiler for lwcc",
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
114 PROGVER
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
115 };
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 int main(int argc, char **argv)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
118 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
119 program_name = argv[0];
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
120 int retval = 0;
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
121 node_t *n;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
122
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
123 input_files = lw_stringlist_create();
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
124 includedirs = lw_stringlist_create();
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
125 sysincludedirs = lw_stringlist_create();
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
126 macrolist = lw_stringlist_create();
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
127
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
128 /* parse command line arguments */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
129 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
130
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
131 /* set up output file */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
132 if (output_file == NULL || strcmp(output_file, "-") == 0)
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 output_fp = stdout;
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 else
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
137 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
138 output_fp = fopen(output_file, "wb");
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
139 if (output_fp == NULL)
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 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
142 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
143 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
144
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
145 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
146
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
147 if (lw_stringlist_nstrings(input_files) == 0)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
148 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
149 /* 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
150 n = process_file("-");
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
151 if (!n)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
152 retval = 1;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
153 else
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
154 node_addchild(program_tree, n);
311
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 else
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
157 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
158 char *s;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
159 lw_stringlist_reset(input_files);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
160 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
161 {
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
162 n = process_file(s);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
163 if (!n)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
164 retval = 1;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
165 if (retval != 0)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
166 break;
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
167 node_addchild(program_tree, n);
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
168 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
169 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
170 lw_stringlist_destroy(input_files);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
171 lw_stringlist_destroy(includedirs);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
172 lw_stringlist_destroy(sysincludedirs);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
173 lw_stringlist_destroy(macrolist);
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
174
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
175 node_display(program_tree, stdout);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
176 node_destroy(program_tree);
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
177 exit(retval);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
178 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
179
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
180 node_t *process_file(const char *fn)
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
181 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
182 struct preproc_info *pp;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
183 char *tstr;
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
184 node_t *n;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
185
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
186 pp = preproc_init(fn);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
187 if (!pp)
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
188 return NULL;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
189
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
190 /* set up the include paths */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
191 lw_stringlist_reset(includedirs);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
192 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
193 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
194 preproc_add_include(pp, tstr, 0);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
195 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
196
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
197 lw_stringlist_reset(sysincludedirs);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
198 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
199 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
200 preproc_add_include(pp, tstr, 1);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
201 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
202
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
203 /* set up pre-defined macros */
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
204 lw_stringlist_reset(macrolist);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
205 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
206 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
207 preproc_add_macro(pp, tstr);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
208 }
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
209
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
210 n = parse_program(pp);
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
211 preproc_finish(pp);
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents: 311
diff changeset
212 return n;
311
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
213 }
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 static void do_error(const char *f, ...)
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
216 {
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
217 va_list args;
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
218 va_start(args, f);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
219 fprintf(stderr, "ERROR: ");
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
220 vfprintf(stderr, f, args);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
221 va_end(args);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
222 fprintf(stderr, "\n");
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
223 exit(1);
7957e90d0a35 Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff changeset
224 }