annotate lwcc/cpp/file.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 40ecbd5da481
children 048adfee2933
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/cpp/file.c
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
3
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
5
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
7
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
12
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
17
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
20
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
21 */
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
22
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <errno.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
24 #include <stdio.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <string.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
26
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
27 #include <lw_alloc.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
28
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
29 #include "cpp.h"
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
30
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
31 struct file_stack_e *file_stack = NULL;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
32
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
33 /* output a byte to the current output stream as long as we aren't in the
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
34 middle of a false conditional. CPP_EOL will be converted to '\n'
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
35 on output. */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
36 void outchr(int c)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
37 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
38 if (skip_level)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
39 return;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
40 if (c == CPP_EOL)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
41 c = '\n';
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
42 fputc(c, output_fp);
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
43 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
44
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
45 /* output a string to the current output stream as long as we aren't in the
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
46 middle of a false conditional */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
47 void outstr(char *s)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
48 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
49 if (skip_level)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
50 return;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
51 while (*s)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
52 outchr(*s++);
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
53 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
54
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
55 /* fetch a raw input byte from the current file. Will return CPP_EOF if
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
56 EOF is encountered and CPP_EOL if an end of line sequence is encountered.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
57 End of line is defined as either CR, CRLF, LF, or LFCR. CPP_EOL is
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
58 returned on the first CR or LF encountered. The complementary CR or LF
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
59 is munched, if present, when the *next* character is read. This always
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
60 operates on file_stack.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
61
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
62 This function also accounts for line numbers in input files and also
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
63 character columns.
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
64 */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
65 int fetch_byte_ll(void)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
66 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
67 int c;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
68
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
69 if (file_stack -> eolstate != 0)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
70 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
71 file_stack -> line++;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
72 file_stack -> col = 0;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
73 }
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
74 c = getc(file_stack -> fp);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
75 file_stack -> col++;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
76 if (file_stack -> eolstate == 1)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
77 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
78 // just saw CR, munch LF
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
79 if (c == 10)
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
80 c = getc(file_stack -> fp);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
81 file_stack -> eolstate = 0;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
82 }
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
83 else if (file_stack -> eolstate == 2)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
84 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
85 // just saw LF, much CR
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
86 if (c == 13)
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
87 c = getc(file_stack -> fp);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
88 file_stack -> eolstate = 0;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
89 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
90
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
91 if (c == 10)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
92 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
93 // we have LF - end of line, flag to munch CR
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
94 file_stack -> eolstate = 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
95 c = CPP_EOL;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
96 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
97 else if (c == 13)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
98 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
99 // we have CR - end of line, flag to munch LF
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
100 file_stack -> eolstate = 1;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
101 c = CPP_EOL;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
102 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
103 else if (c == EOF)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
104 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
105 c = CPP_EOF;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
106 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
107 return c;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
108 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
109
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
110 /* This function takes a sequence of bytes from the _ll function above
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
111 and does trigraph interpretation on it, but only if the global
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
112 trigraphs is nonzero. */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
113 int fetch_byte_tg(void)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
114 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
115 int c;
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
116
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
117 if (!trigraphs)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
118 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
119 c = fetch_byte_ll();
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
120 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
121 else
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
122 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
123 /* we have to do the trigraph shit here */
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
124 if (file_stack -> ra != CPP_NOUNG)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
125 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
126 if (file_stack -> qseen > 0)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
127 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
128 c = '?';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
129 file_stack -> qseen -= 1;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
130 return c;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
131 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
132 else
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
133 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
134 c = file_stack -> ra;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
135 file_stack -> ra = CPP_NOUNG;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
136 return c;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
137 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
138 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
139
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
140 c = fetch_byte_ll();
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
141 while (c == '?')
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
142 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
143 file_stack -> qseen++;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
144 c = fetch_byte_ll();
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
145 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
146
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
147 if (file_stack -> qseen >= 2)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
148 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
149 // we have a trigraph
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
150 switch (c)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
151 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
152 case '=':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
153 c = '#';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
154 file_stack -> qseen -= 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
155 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
156
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
157 case '/':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
158 c = '\\';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
159 file_stack -> qseen -= 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
160 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
161
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
162 case '\'':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
163 c = '^';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
164 file_stack -> qseen -= 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
165 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
166
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
167 case '(':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
168 c = '[';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
169 file_stack -> qseen -= 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
170 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
171
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
172 case ')':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
173 c = ']';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
174 file_stack -> qseen -= 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
175 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
176
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
177 case '!':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
178 c = '|';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
179 file_stack -> qseen -= 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
180 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
181
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
182 case '<':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
183 c = '{';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
184 file_stack -> qseen -= 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
185 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
186
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
187 case '>':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
188 c = '}';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
189 file_stack -> qseen -= 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
190 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
191
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
192 case '~':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
193 c = '~';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
194 file_stack -> qseen -= 2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
195 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
196 }
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
197 if (file_stack -> qseen > 0)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
198 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
199 file_stack -> ra = c;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
200 c = '?';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
201 file_stack -> qseen--;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
202 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
203 }
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
204 else if (file_stack -> qseen > 0)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
205 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
206 file_stack -> ra = c;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
207 c = '?';
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
208 file_stack -> qseen--;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
209 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
210 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
211 return c;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
212 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
213
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
214 /* This function puts a byte back onto the front of the input stream used
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
215 by fetch_byte(). Theoretically, an unlimited number of characters can
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
216 be unfetched. Line and column counting may be incorrect if unfetched
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
217 characters cross a token boundary. */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
218 void unfetch_byte(int c)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
219 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
220 if (file_stack -> ungetbufl >= file_stack -> ungetbufs)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
221 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
222 file_stack -> ungetbufs += 100;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
223 file_stack -> ungetbuf = lw_realloc(file_stack -> ungetbuf, file_stack -> ungetbufs);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
224 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
225 file_stack -> ungetbuf[file_stack -> ungetbufl++] = c;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
226 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
227
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
228 /* This function retrieves a byte from the input stream. It performs
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
229 backslash-newline splicing on the returned bytes. Any character
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
230 retrieved from the unfetch buffer is presumed to have already passed
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
231 the backslash-newline filter. */
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
232 int fetch_byte(void)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
233 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
234 int c;
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
235
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
236 if (file_stack -> ungetbufl > 0)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
237 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
238 file_stack -> ungetbufl--;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
239 c = file_stack -> ungetbuf[file_stack -> ungetbufl];
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
240 if (file_stack -> ungetbufl == 0)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
241 {
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
242 lw_free(file_stack -> ungetbuf);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
243 file_stack -> ungetbuf = NULL;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
244 file_stack -> ungetbufs = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
245 }
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
246 return c;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
247 }
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
248
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
249 again:
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
250 if (file_stack -> unget != CPP_NOUNG)
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
251 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
252 c = file_stack -> unget;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
253 file_stack -> unget = CPP_NOUNG;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
254 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
255 else
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
256 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
257 c = fetch_byte_tg();
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
258 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
259 if (c == '\\')
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
260 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
261 int c2;
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
262 c2 = fetch_byte_tg();
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
263 if (c2 == CPP_EOL)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
264 goto again;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
265 else
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
266 file_stack -> unget = c2;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
267 }
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
268 file_stack -> curc = c;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
269 return c;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
270 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
271
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
272 /* This function opens (if not stdin) the file f and pushes it onto the
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
273 top of the input file stack. It then proceeds to process the file
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
274 and return. Nonzero return means the file could not be opened. */
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
275 int process_file(const char *f)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
276 {
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
277 struct file_stack_e nf;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
278 FILE *fp;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
279
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
280 fprintf(stderr, "Processing %s\n", f);
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
281
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
282 if (strcmp(f, "-") == 0)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
283 fp = stdin;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
284 else
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
285 fp = fopen(f, "rb");
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
286 if (fp == NULL)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
287 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
288 do_warning("Cannot open %s: %s", f, strerror(errno));
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
289 return -1;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
290 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
291
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
292 /* push the file onto the file stack */
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
293 nf.fn = f;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
294 nf.fp = fp;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
295 nf.next = file_stack;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
296 nf.line = 1;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
297 nf.col = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
298 nf.qseen = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
299 nf.ra = CPP_NOUNG;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
300 nf.unget = CPP_NOUNG;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
301 file_stack = &nf;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
302 nf.ungetbuf = NULL;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
303 nf.ungetbufs = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
304 nf.ungetbufl = 0;
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
305
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
306 /* go preprocess the file */
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
307 preprocess_file();
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
308
293
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
309 if (nf.fp != stdin)
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
310 fclose(nf.fp);
c419b3b3d43f Checkpoint on lwcc-cpp development
William Astle <lost@l-w.ca>
parents: 292
diff changeset
311 file_stack = nf.next;
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
312 return 0;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
313 }