Mercurial > hg > index.cgi
annotate lwcc/cc-main.c @ 583:000381ee2d5c default tip
Guard against single operand multiplication when detecting like terms
This *shouldn't* happen, but it apparently does in some pathological cases
so guard against a single operand multiplication to prevent a crash.
author | William Astle <lost@l-w.ca> |
---|---|
date | Mon, 04 Nov 2024 23:48:23 -0700 |
parents | c3099c5d9d3e |
children |
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); |
499 | 40 extern void generate_code(node_t *n, FILE *of); |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
41 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
42 node_t *program_tree = NULL; |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
43 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
44 /* command line option handling */ |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
45 #define PROGVER "lwcc-cc from " PACKAGE_STRING |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
46 char *program_name; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
47 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
48 /* input files */ |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
49 lw_stringlist_t input_files; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
50 lw_stringlist_t includedirs; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
51 lw_stringlist_t sysincludedirs; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
52 lw_stringlist_t macrolist; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
53 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
54 /* various flags */ |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
55 int trigraphs = 0; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
56 char *output_file = NULL; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
57 FILE *output_fp = NULL; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
58 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
59 static struct lw_cmdline_options options[] = |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
60 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
61 { "output", 'o', "FILE", 0, "Output to FILE"}, |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
62 { "include", 'i', "FILE", 0, "Pre-include FILE" }, |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
63 { "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
|
64 { "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
|
65 { "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
|
66 { "trigraphs", 0x100, NULL, 0, "Enable interpretation of trigraphs" }, |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
67 { 0 } |
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 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
70 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
|
71 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
72 switch (key) |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
73 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
74 case 'o': |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
75 if (output_file) |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
76 do_error("Output file specified more than once."); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
77 output_file = arg; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
78 break; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
79 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
80 case 0x100: |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
81 trigraphs = 1; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
82 break; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
83 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
84 case 'I': |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
85 lw_stringlist_addstring(includedirs, arg); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
86 break; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
87 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
88 case 'S': |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
89 lw_stringlist_addstring(sysincludedirs, arg); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
90 break; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
91 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
92 case 'D': |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
93 lw_stringlist_addstring(macrolist, arg); |
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_end: |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
97 break; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
98 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
99 case lw_cmdline_key_arg: |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
100 lw_stringlist_addstring(input_files, arg); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
101 break; |
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 default: |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
104 return lw_cmdline_err_unknown; |
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 return 0; |
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 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
109 static struct lw_cmdline_parser cmdline_parser = |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
110 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
111 options, |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
112 parse_opts, |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
113 "INPUTFILE", |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
114 "lwcc-cc - C compiler for lwcc", |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
115 PROGVER |
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 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
118 int main(int argc, char **argv) |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
119 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
120 program_name = argv[0]; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
121 int retval = 0; |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
122 node_t *n; |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
123 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
124 input_files = lw_stringlist_create(); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
125 includedirs = lw_stringlist_create(); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
126 sysincludedirs = lw_stringlist_create(); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
127 macrolist = lw_stringlist_create(); |
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 /* parse command line arguments */ |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
130 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
|
131 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
132 /* set up output file */ |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
133 if (output_file == NULL || strcmp(output_file, "-") == 0) |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
134 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
135 output_fp = stdout; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
136 } |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
137 else |
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 output_fp = fopen(output_file, "wb"); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
140 if (output_fp == NULL) |
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 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
|
143 } |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
144 } |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
145 |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
146 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
|
147 |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
148 if (lw_stringlist_nstrings(input_files) == 0) |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
149 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
150 /* 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
|
151 n = process_file("-"); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
152 if (!n) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
153 retval = 1; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
154 else |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
155 node_addchild(program_tree, n); |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
156 } |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
157 else |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
158 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
159 char *s; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
160 lw_stringlist_reset(input_files); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
161 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
|
162 { |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
163 n = process_file(s); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
164 if (!n) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
165 retval = 1; |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
166 if (retval != 0) |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
167 break; |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
168 node_addchild(program_tree, n); |
311
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 } |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
171 lw_stringlist_destroy(input_files); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
172 lw_stringlist_destroy(includedirs); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
173 lw_stringlist_destroy(sysincludedirs); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
174 lw_stringlist_destroy(macrolist); |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
175 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
176 node_display(program_tree, stdout); |
499 | 177 |
178 // generate output | |
179 generate_code(program_tree, output_fp); | |
180 | |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
181 node_destroy(program_tree); |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
182 exit(retval); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
183 } |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
184 |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
185 node_t *process_file(const char *fn) |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
186 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
187 struct preproc_info *pp; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
188 char *tstr; |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
189 node_t *n; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
190 |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
191 pp = preproc_init(fn); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
192 if (!pp) |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
193 return NULL; |
311
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 /* set up the include paths */ |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
196 lw_stringlist_reset(includedirs); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
197 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
|
198 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
199 preproc_add_include(pp, tstr, 0); |
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 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
202 lw_stringlist_reset(sysincludedirs); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
203 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
|
204 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
205 preproc_add_include(pp, tstr, 1); |
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 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
208 /* set up pre-defined macros */ |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
209 lw_stringlist_reset(macrolist); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
210 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
|
211 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
212 preproc_add_macro(pp, tstr); |
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 |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
215 n = parse_program(pp); |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
216 preproc_finish(pp); |
499 | 217 |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
311
diff
changeset
|
218 return n; |
311
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
219 } |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
220 |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
221 static void do_error(const char *f, ...) |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
222 { |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
223 va_list args; |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
224 va_start(args, f); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
225 fprintf(stderr, "ERROR: "); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
226 vfprintf(stderr, f, args); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
227 va_end(args); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
228 fprintf(stderr, "\n"); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
229 exit(1); |
7957e90d0a35
Add skeleton compiler target to build
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
230 } |