OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [cpp/] [cpp.c] - Blame information for rev 319

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 hellwig
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <time.h>
5
#include <stdarg.h>
6
#include "cpp.h"
7
 
8
#define OUTS    16384
9
char    outbuf[OUTS];
10
char    *outp = outbuf;
11
Source  *cursource;
12
int     nerrs;
13
struct  token nltoken = { NL, 0, 0, 0, 1, (uchar*)"\n" };
14
char    *curtime;
15
int     incdepth;
16
int     ifdepth;
17
int     ifsatisfied[NIF];
18
int     skipping;
19
 
20
char rcsid[] = "$Revision: 1.6 $ $Date: 2001/03/27 19:37:59 $";
21
 
22
int
23
main(int argc, char **argv)
24
{
25
        Tokenrow tr;
26
        time_t t;
27
        char ebuf[BUFSIZ];
28
 
29
        setbuf(stderr, ebuf);
30
        t = time(NULL);
31
        curtime = ctime(&t);
32
        maketokenrow(3, &tr);
33
        expandlex();
34
        setup(argc, argv);
35
        fixlex();
36
        iniths();
37
        genline();
38
        process(&tr);
39
        flushout();
40
        fflush(stderr);
41
        exit(nerrs > 0);
42
        return 0;
43
}
44
 
45
void
46
process(Tokenrow *trp)
47
{
48
        int anymacros = 0;
49
 
50
        for (;;) {
51
                if (trp->tp >= trp->lp) {
52
                        trp->tp = trp->lp = trp->bp;
53
                        outp = outbuf;
54
                        anymacros |= gettokens(trp, 1);
55
                        trp->tp = trp->bp;
56
                }
57
                if (trp->tp->type == END) {
58
                        if (--incdepth>=0) {
59
                                if (cursource->ifdepth)
60
                                        error(ERROR,
61
                                         "Unterminated conditional in #include");
62
                                unsetsource();
63
                                cursource->line += cursource->lineinc;
64
                                trp->tp = trp->lp;
65
                                genline();
66
                                continue;
67
                        }
68
                        if (ifdepth)
69
                                error(ERROR, "Unterminated #if/#ifdef/#ifndef");
70
                        break;
71
                }
72
                if (trp->tp->type==SHARP) {
73
                        trp->tp += 1;
74
                        control(trp);
75
                } else if (!skipping && anymacros)
76
                        expandrow(trp, NULL);
77
                if (skipping)
78
                        setempty(trp);
79
                puttokens(trp);
80
                anymacros = 0;
81
                cursource->line += cursource->lineinc;
82
                if (cursource->lineinc>1) {
83
                        genline();
84
                }
85
        }
86
}
87
 
88
void
89
control(Tokenrow *trp)
90
{
91
        Nlist *np;
92
        Token *tp;
93
 
94
        tp = trp->tp;
95
        if (tp->type!=NAME) {
96
                if (tp->type==NUMBER)
97
                        goto kline;
98
                if (tp->type != NL)
99
                        error(ERROR, "Unidentifiable control line");
100
                return;                 /* else empty line */
101
        }
102
        if ((np = lookup(tp, 0))==NULL || (np->flag&ISKW)==0 && !skipping) {
103
                error(WARNING, "Unknown preprocessor control %t", tp);
104
                return;
105
        }
106
        if (skipping) {
107
                if ((np->flag&ISKW)==0)
108
                        return;
109
                switch (np->val) {
110
                case KENDIF:
111
                        if (--ifdepth<skipping)
112
                                skipping = 0;
113
                        --cursource->ifdepth;
114
                        setempty(trp);
115
                        return;
116
 
117
                case KIFDEF:
118
                case KIFNDEF:
119
                case KIF:
120
                        if (++ifdepth >= NIF)
121
                                error(FATAL, "#if too deeply nested");
122
                        ++cursource->ifdepth;
123
                        return;
124
 
125
                case KELIF:
126
                case KELSE:
127
                        if (ifdepth<=skipping)
128
                                break;
129
                        return;
130
 
131
                default:
132
                        return;
133
                }
134
        }
135
        switch (np->val) {
136
        case KDEFINE:
137
                dodefine(trp);
138
                break;
139
 
140
        case KUNDEF:
141
                tp += 1;
142
                if (tp->type!=NAME || trp->lp - trp->bp != 4) {
143
                        error(ERROR, "Syntax error in #undef");
144
                        break;
145
                }
146
                if ((np = lookup(tp, 0)) != NULL)
147
                        np->flag &= ~ISDEFINED;
148
                break;
149
 
150
        case KPRAGMA:
151
                return;
152
 
153
        case KIFDEF:
154
        case KIFNDEF:
155
        case KIF:
156
                if (++ifdepth >= NIF)
157
                        error(FATAL, "#if too deeply nested");
158
                ++cursource->ifdepth;
159
                ifsatisfied[ifdepth] = 0;
160
                if (eval(trp, np->val))
161
                        ifsatisfied[ifdepth] = 1;
162
                else
163
                        skipping = ifdepth;
164
                break;
165
 
166
        case KELIF:
167
                if (ifdepth==0) {
168
                        error(ERROR, "#elif with no #if");
169
                        return;
170
                }
171
                if (ifsatisfied[ifdepth]==2)
172
                        error(ERROR, "#elif after #else");
173
                if (eval(trp, np->val)) {
174
                        if (ifsatisfied[ifdepth])
175
                                skipping = ifdepth;
176
                        else {
177
                                skipping = 0;
178
                                ifsatisfied[ifdepth] = 1;
179
                        }
180
                } else
181
                        skipping = ifdepth;
182
                break;
183
 
184
        case KELSE:
185
                if (ifdepth==0 || cursource->ifdepth==0) {
186
                        error(ERROR, "#else with no #if");
187
                        return;
188
                }
189
                if (ifsatisfied[ifdepth]==2)
190
                        error(ERROR, "#else after #else");
191
                if (trp->lp - trp->bp != 3)
192
                        error(ERROR, "Syntax error in #else");
193
                skipping = ifsatisfied[ifdepth]? ifdepth: 0;
194
                ifsatisfied[ifdepth] = 2;
195
                break;
196
 
197
        case KENDIF:
198
                if (ifdepth==0 || cursource->ifdepth==0) {
199
                        error(ERROR, "#endif with no #if");
200
                        return;
201
                }
202
                --ifdepth;
203
                --cursource->ifdepth;
204
                if (trp->lp - trp->bp != 3)
205
                        error(WARNING, "Syntax error in #endif");
206
                break;
207
 
208
        case KERROR:
209
                trp->tp = tp+1;
210
                error(WARNING, "#error directive: %r", trp);
211
                break;
212
 
213
        case KLINE:
214
                trp->tp = tp+1;
215
                expandrow(trp, "<line>");
216
                tp = trp->bp+2;
217
        kline:
218
                if (tp+1>=trp->lp || tp->type!=NUMBER || tp+3<trp->lp
219
                 || (tp+3==trp->lp && ((tp+1)->type!=STRING)||*(tp+1)->t=='L')){
220
                        error(ERROR, "Syntax error in #line");
221
                        return;
222
                }
223
                cursource->line = atol((char*)tp->t)-1;
224
                if (cursource->line<0 || cursource->line>=32768)
225
                        error(WARNING, "#line specifies number out of range");
226
                tp = tp+1;
227
                if (tp+1<trp->lp)
228
                        cursource->filename=(char*)newstring(tp->t+1,tp->len-2,0);
229
                return;
230
 
231
        case KDEFINED:
232
                error(ERROR, "Bad syntax for control line");
233
                break;
234
 
235
        case KINCLUDE:
236
                doinclude(trp);
237
                trp->lp = trp->bp;
238
                return;
239
 
240
        case KEVAL:
241
                eval(trp, np->val);
242
                break;
243
 
244
        default:
245
                error(ERROR, "Preprocessor control `%t' not yet implemented", tp);
246
                break;
247
        }
248
        setempty(trp);
249
        return;
250
}
251
 
252
void *
253
domalloc(int size)
254
{
255
        void *p = malloc(size);
256
 
257
        if (p==NULL)
258
                error(FATAL, "Out of memory from malloc");
259
        return p;
260
}
261
 
262
void
263
dofree(void *p)
264
{
265
        free(p);
266
}
267
 
268
void
269
error(enum errtype type, char *string, ...)
270
{
271
        va_list ap;
272
        char *cp, *ep;
273
        Token *tp;
274
        Tokenrow *trp;
275
        Source *s;
276
        int i;
277
 
278
        fprintf(stderr, "cpp: ");
279
        for (s=cursource; s; s=s->next)
280
                if (*s->filename)
281
                        fprintf(stderr, "%s:%d ", s->filename, s->line);
282
        va_start(ap, string);
283
        for (ep=string; *ep; ep++) {
284
                if (*ep=='%') {
285
                        switch (*++ep) {
286
 
287
                        case 's':
288
                                cp = va_arg(ap, char *);
289
                                fprintf(stderr, "%s", cp);
290
                                break;
291
                        case 'd':
292
                                i = va_arg(ap, int);
293
                                fprintf(stderr, "%d", i);
294
                                break;
295
                        case 't':
296
                                tp = va_arg(ap, Token *);
297
                                fprintf(stderr, "%.*s", tp->len, tp->t);
298
                                break;
299
 
300
                        case 'r':
301
                                trp = va_arg(ap, Tokenrow *);
302
                                for (tp=trp->tp; tp<trp->lp&&tp->type!=NL; tp++) {
303
                                        if (tp>trp->tp && tp->wslen)
304
                                                fputc(' ', stderr);
305
                                        fprintf(stderr, "%.*s", tp->len, tp->t);
306
                                }
307
                                break;
308
 
309
                        default:
310
                                fputc(*ep, stderr);
311
                                break;
312
                        }
313
                } else
314
                        fputc(*ep, stderr);
315
        }
316
        va_end(ap);
317
        fputc('\n', stderr);
318
        if (type==FATAL)
319
                exit(1);
320
        if (type!=WARNING)
321
                nerrs = 1;
322
        fflush(stderr);
323
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.