annotate lwcc/cpp/preproc.c @ 293:c419b3b3d43f ccdev

Checkpoint on lwcc-cpp development This is a checkpoint with some substantial code cleanups on what is so far implemented. This should avoid substantial code duplication later.
author William Astle <lost@l-w.ca>
date Mon, 09 Sep 2013 23:07:19 -0600
parents
children 048adfee2933
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/cpp/preproc.c
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
3
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
5
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
7
c419b3b3d43f Checkpoint on lwcc-cpp 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
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
12
c419b3b3d43f Checkpoint on lwcc-cpp 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
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
17
c419b3b3d43f Checkpoint on lwcc-cpp 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
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
21
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <stdio.h>
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <stdlib.h>
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
24
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <lw_alloc.h>
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
26
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
27 #include "cpp.h"
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
28
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
29
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
30 int munch_comment(void);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
31 char *parse_str_lit(void);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
32 char *parse_chr_lit(void);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
33 char *parse_num_lit(int);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
34 void preprocess_identifier(int);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
35 void preprocess_directive(void);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
36
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
37
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
38 int skip_level;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
39
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
40 /*
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
41 Notes:
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
42
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
43 Rather than tokenize the entire file, we run through it interpreting
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
44 things only as much as we need to in order to identify the following:
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
45
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
46 preprocessing directives (#...)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
47 identifiers which might need to be replaced with macros
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
48
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
49 We have to interpret strings, character constants, and numbers to prevent
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
50 false positives in those situations.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
51
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
52 When we find a preprocessing directive, it is handled with a more
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
53 aggressive tokenization process and then intepreted accordingly.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
54
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
55 nlws is used to record the fact that only whitespace has occurred at the
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
56 start of a line. Whitespace is defined as comments or isspace(c). It gets
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
57 reset to 1 after each EOL character. If a non-whitespace character is
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
58 encountered, it is set to -1. If the character processing decides it really
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
59 is a whitespace character, it will set nlws back to 1 (block comment).
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
60 Elsewise, it will get set to 0 if it is still -1 when the loop starts again.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
61
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
62 This is needed so we can identify whitespace interposed before a
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
63 preprocessor directive. This is the only case where it matters for
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
64 the preprocessor.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
65
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
66 */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
67 void preprocess_file()
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
68 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
69 int c;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
70 int nlws = 1;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
71
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
72 preprocess_output_location(1);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
73 for (;;)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
74 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
75 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
76 // if we had non-whitespace that wasn't munched (comment), set flag correctly
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
77 if (nlws == -1)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
78 nlws = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
79 if (c == CPP_EOF)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
80 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
81 // end of input - make sure newline is present
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
82 outchr('\n');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
83 return;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
84 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
85 if (c == CPP_EOL)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
86 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
87 // flag that we just hit the start of a new line
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
88 nlws = 1;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
89 outchr(CPP_EOL);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
90 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
91 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
92
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
93 /* if we have a non-whitespace character, flag it as such */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
94 if (!is_whitespace(c))
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
95 nlws = -1;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
96
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
97 if (c == '#' && nlws)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
98 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
99 // we have a preprocessor directive here - this call will do
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
100 // everything including outputting the blank line, if appropriate
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
101 preprocess_directive();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
102 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
103 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
104 else if (c == '\'')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
105 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
106 // we have a character constant here
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
107 outstr(parse_chr_lit());
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
108 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
109 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
110 else if (c == '"')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
111 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
112 // we have a string constant here
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
113 outstr(parse_str_lit());
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
114 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
115 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
116 else if (c == '.')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
117 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
118 // we might have a number here
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
119 outchr('.');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
120 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
121 if (is_dec(c))
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
122 outstr(parse_num_lit(c));
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
123 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
124 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
125 else if (is_dec(c))
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
126 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
127 // we have a number here
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
128 outstr(parse_num_lit(c));
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
129 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
130 else if (c == '/')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
131 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
132 // we might have a comment here
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
133 c = munch_comment();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
134 if (c < 0)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
135 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
136 outchr('/');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
137 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
138 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
139 // comments are white space - count them as such at start of line
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
140 if (nlws == -1)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
141 nlws = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
142 /* c is the number of EOL characters the comment spanned */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
143 while (c--)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
144 outchr(CPP_EOL);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
145 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
146 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
147 else if (c == 'L')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
148 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
149 // wide character string or wide character constant, or identifier
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
150 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
151 if (c == '"')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
152 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
153 outchr('L');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
154 outstr(parse_str_lit());
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
155 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
156 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
157 else if (c == '\'')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
158 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
159 outchr('L');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
160 outstr(parse_chr_lit());
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
161 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
162 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
163 unfetch_byte(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
164 preprocess_identifier('L');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
165 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
166 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
167 else if (is_sidchr(c))
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
168 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
169 // identifier of some kind
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
170 preprocess_identifier(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
171 continue;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
172 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
173 else
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
174 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
175 // random character - pass through
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
176 outchr(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
177 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
178 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
179 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
180
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
181 void preprocess_identifier(int c)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
182 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
183 char *ident = NULL;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
184 int idlen = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
185 int idbufl = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
186
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
187 do
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
188 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
189 if (idlen >= idbufl)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
190 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
191 idbufl += 50;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
192 ident = lw_realloc(ident, idbufl);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
193 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
194 ident[idlen++] = c;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
195 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
196 } while (is_idchr(c));
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
197
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
198 ident[idlen++] = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
199 unfetch_byte(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
200
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
201 /* do something with the identifier here - macros, etc. */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
202 outstr(ident);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
203 lw_free(ident);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
204 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
205
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
206 #define to_buf(c) do { if (idlen >= idbufl) { idbufl += 100; ident = lw_realloc(ident, idbufl); } ident[idlen++] = (c); } while (0)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
207 char *parse_num_lit(int c)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
208 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
209 static char *ident = NULL;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
210 int idlen = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
211 static int idbufl = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
212
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
213 do
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
214 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
215 to_buf(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
216 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
217 if (is_ep(c))
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
218 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
219 to_buf(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
220 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
221 if (c == '-' || c == '+')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
222 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
223 to_buf(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
224 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
225 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
226 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
227 } while ((is_dec(c)) || (c == '.'));
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
228 to_buf(0);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
229
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
230 return ident;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
231 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
232
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
233 char *parse_chr_lit(void)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
234 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
235 static char *ident = NULL;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
236 int idlen = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
237 static int idbufl = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
238 int c;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
239
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
240 to_buf('\'');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
241 while ((c = fetch_byte()) != '\'')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
242 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
243 if (c == CPP_EOL || c == CPP_EOF)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
244 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
245 unfetch_byte(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
246 to_buf(0);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
247 do_warning("Unterminated character constant");
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
248 return ident;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
249 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
250 if (c == '\\')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
251 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
252 to_buf(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
253 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
254 if (c == CPP_EOL || c == CPP_EOF)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
255 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
256 unfetch_byte(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
257 to_buf(0);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
258 do_warning("Unterminated character constant");
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
259 return ident;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
260 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
261 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
262 to_buf(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
263 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
264 to_buf(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
265 to_buf(0);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
266 return ident;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
267 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
268
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
269 char *parse_str_lit(void)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
270 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
271 static char *ident = NULL;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
272 int idlen = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
273 static int idbufl = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
274 int c;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
275
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
276 to_buf('"');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
277 while ((c = fetch_byte()) != '"')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
278 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
279 if (c == CPP_EOL || c == CPP_EOF)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
280 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
281 unfetch_byte(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
282 to_buf(0);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
283 do_warning("Unterminated string literal");
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
284 return ident;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
285 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
286 if (c == '\\')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
287 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
288 to_buf(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
289 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
290 if (c == CPP_EOL || c == CPP_EOF)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
291 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
292 unfetch_byte(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
293 to_buf(0);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
294 do_warning("Unterminated string literal");
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
295 return ident;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
296 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
297 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
298 to_buf(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
299 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
300 to_buf(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
301 to_buf(0);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
302 return ident;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
303 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
304
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
305 int munch_comment(void)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
306 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
307 int nlc = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
308 int c;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
309
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
310 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
311 if (c == '/')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
312 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
313 // single line comment
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
314 for (;;)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
315 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
316 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
317 if (c == CPP_EOL)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
318 nlc = 1;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
319 if (c == CPP_EOL || c == CPP_EOF)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
320 return nlc;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
321 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
322 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
323 else if (c == '*')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
324 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
325 // block comment
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
326 for (;;)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
327 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
328 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
329 if (c == CPP_EOL)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
330 nlc++;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
331 if (c == CPP_EOF)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
332 return nlc;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
333 if (c == '*')
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
334 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
335 c = fetch_byte();
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
336 if (c == '/' || c == CPP_EOF)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
337 return nlc;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
338 if (c == CPP_EOL)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
339 nlc++;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
340 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
341 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
342 return nlc;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
343 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
344 else
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
345 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
346 unfetch_byte(c);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
347 return -1;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
348 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
349
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
350 return nlc;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
351 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
352
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
353 /* Output a location directive to synchronize the compiler with the correct
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
354 input line number and file. This is of the form:
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
355
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
356 # <linenum> <filename> <flag>
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
357
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
358 where <linenum> is the line number inside the file, <filename> is the
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
359 filename (as a C string), and <flag> is the specified flag argument which
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
360 should be 1 for the start of a new file or 2 for returning to the file from
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
361 another file. <linenum> is the line number the following line came from.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
362 */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
363 void preprocess_output_location(int flag)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
364 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
365 fprintf(output_fp, "# %d \"%s\" %d\n", file_stack -> line, file_stack -> fn, flag);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
366 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
367
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
368 /* process a preprocessor directive */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
369 void preprocess_directive(void)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
370 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
371 outchr('>');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
372 outchr('#');
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents:
diff changeset
373 }