annotate lwcc/cpp-main.c @ 301:6f7fe78bb868 ccdev

Add string -> number conversion for preproc expression evaluator Q&D conversion from string to signed number. It should be noted that this really should be done during tokenization and the type of number be set by the tokenizer, including parsing floating point values. Then the preprocessor can decide what to do with non-integer numbers.
author William Astle <lost@l-w.ca>
date Sun, 15 Sep 2013 14:22:10 -0600
parents 4b17780f2777
children d85d173ba120
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/cpp-main.c
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
3
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
5
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
7
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
12
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
17
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
21
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <errno.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <stdarg.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
24 #include <stdio.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <stdlib.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
26 #include <string.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
27
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include <lw_stringlist.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
29 #include <lw_cmdline.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
30
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
31 #include "cpp.h"
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
32
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
33 int process_file(const char *);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
34 static void do_error(const char *f, ...);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
35
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
36 /* command line option handling */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
37 #define PROGVER "lwcc-cpp from " PACKAGE_STRING
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
38 char *program_name;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
39
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
40 /* input files */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
41 lw_stringlist_t input_files;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
42
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
43 /* various flags */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
44 int trigraphs = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
45 char *output_file = NULL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
46 FILE *output_fp = NULL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
47
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
48 static struct lw_cmdline_options options[] =
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
49 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
50 { "output", 'o', "FILE", 0, "Output to FILE"},
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
51 { "include", 'i', "FILE", 0, "Pre-include FILE" },
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
52 { "includedir", 'I', "PATH", 0, "Add entry to the user include path" },
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
53 { "sincludedir", 'S', "PATH", 0, "Add entry to the system include path" },
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
54 { "define", 'D', "SYM[=VAL]",0, "Automatically define SYM to be VAL (or 1)"},
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
55 { "trigraphs", 0x100, NULL, 0, "Enable interpretation of trigraphs" },
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
56 { 0 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
57 };
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
58
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
59 static int parse_opts(int key, char *arg, void *state)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
60 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
61 switch (key)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
62 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
63 case 'o':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
64 if (output_file)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
65 do_error("Output file specified more than once.");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
66 output_file = arg;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
67 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
68
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
69 case 0x100:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
70 trigraphs = 1;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
71 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
72
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
73 case lw_cmdline_key_end:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
74 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
75
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
76 case lw_cmdline_key_arg:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
77 lw_stringlist_addstring(input_files, arg);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
78 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
79
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
80 default:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
81 return lw_cmdline_err_unknown;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
82 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
83 return 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
84 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
85
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
86 static struct lw_cmdline_parser cmdline_parser =
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
87 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
88 options,
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
89 parse_opts,
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
90 "INPUTFILE",
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
91 "lwcc-cpp - C preprocessor for lwcc",
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
92 PROGVER
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
93 };
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
94
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
95 int main(int argc, char **argv)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
96 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
97 program_name = argv[0];
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
98 int retval = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
99
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
100 input_files = lw_stringlist_create();
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
101
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
102 /* parse command line arguments */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
103 lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
104
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
105 /* set up output file */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
106 if (output_file == NULL || strcmp(output_file, "-") == 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
107 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
108 output_fp = stdout;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
109 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
110 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
111 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
112 output_fp = fopen(output_file, "wb");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
113 if (output_fp == NULL)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
114 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
115 do_error("Failed to create output file %s: %s", output_file, strerror(errno));
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
116 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
117 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
118
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
119 if (lw_stringlist_nstrings(input_files) == 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
120 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
121 /* if no input files, work on stdin */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
122 retval = process_file("-");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
123 retval = 1;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
124 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
125 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
126 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
127 char *s;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
128 lw_stringlist_reset(input_files);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
129 for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files))
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
130 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
131 retval = process_file(s);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
132 if (retval != 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
133 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
134 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
135 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
136 lw_stringlist_destroy(input_files);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
137
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
138 // symbol_dump();
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
139 exit(retval);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
140 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
141
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
142 int process_file(const char *fn)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
143 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
144 struct preproc_info *pp;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
145 struct token *tok;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
146
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
147 pp = preproc_init(fn);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
148 if (!pp)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
149 return -1;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
150
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
151 for (;;)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
152 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
153 tok = preproc_next_token(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
154 if (tok -> ttype == TOK_EOF)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
155 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
156 token_print(tok, output_fp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
157 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
158 preproc_finish(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
159 return 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
160 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
161
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
162 static void do_error(const char *f, ...)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
163 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
164 va_list args;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
165 va_start(args, f);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
166 fprintf(stderr, "ERROR: ");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
167 vfprintf(stderr, f, args);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
168 va_end(args);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
169 fprintf(stderr, "\n");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
170 exit(1);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
171 }