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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [src/] [lex.c.ORIG] - Blame information for rev 55

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

Line No. Rev Author Line
1 4 hellwig
#include "c.h"
2
#include 
3
#include 
4
 
5
static char rcsid[] = "$Id: lex.c,v 1.1 2002/08/28 23:12:44 drh Exp $";
6
 
7
#define MAXTOKEN 32
8
 
9
enum { BLANK=01,  NEWLINE=02, LETTER=04,
10
       DIGIT=010, HEX=020,    OTHER=040 };
11
 
12
static unsigned char map[256] = { /* 000 nul */ 0,
13
                                   /* 001 soh */        0,
14
                                   /* 002 stx */        0,
15
                                   /* 003 etx */        0,
16
                                   /* 004 eot */        0,
17
                                   /* 005 enq */        0,
18
                                   /* 006 ack */        0,
19
                                   /* 007 bel */        0,
20
                                   /* 010 bs  */        0,
21
                                   /* 011 ht  */        BLANK,
22
                                   /* 012 nl  */        NEWLINE,
23
                                   /* 013 vt  */        BLANK,
24
                                   /* 014 ff  */        BLANK,
25
                                   /* 015 cr  */        0,
26
                                   /* 016 so  */        0,
27
                                   /* 017 si  */        0,
28
                                   /* 020 dle */        0,
29
                                   /* 021 dc1 */        0,
30
                                   /* 022 dc2 */        0,
31
                                   /* 023 dc3 */        0,
32
                                   /* 024 dc4 */        0,
33
                                   /* 025 nak */        0,
34
                                   /* 026 syn */        0,
35
                                   /* 027 etb */        0,
36
                                   /* 030 can */        0,
37
                                   /* 031 em  */        0,
38
                                   /* 032 sub */        0,
39
                                   /* 033 esc */        0,
40
                                   /* 034 fs  */        0,
41
                                   /* 035 gs  */        0,
42
                                   /* 036 rs  */        0,
43
                                   /* 037 us  */        0,
44
                                   /* 040 sp  */        BLANK,
45
                                   /* 041 !   */        OTHER,
46
                                   /* 042 "   */        OTHER,
47
                                   /* 043 #   */        OTHER,
48
                                   /* 044 $   */        0,
49
                                   /* 045 %   */        OTHER,
50
                                   /* 046 &   */        OTHER,
51
                                   /* 047 '   */        OTHER,
52
                                   /* 050 (   */        OTHER,
53
                                   /* 051 )   */        OTHER,
54
                                   /* 052 *   */        OTHER,
55
                                   /* 053 +   */        OTHER,
56
                                   /* 054 ,   */        OTHER,
57
                                   /* 055 -   */        OTHER,
58
                                   /* 056 .   */        OTHER,
59
                                   /* 057 /   */        OTHER,
60
                                   /* 060 0   */        DIGIT,
61
                                   /* 061 1   */        DIGIT,
62
                                   /* 062 2   */        DIGIT,
63
                                   /* 063 3   */        DIGIT,
64
                                   /* 064 4   */        DIGIT,
65
                                   /* 065 5   */        DIGIT,
66
                                   /* 066 6   */        DIGIT,
67
                                   /* 067 7   */        DIGIT,
68
                                   /* 070 8   */        DIGIT,
69
                                   /* 071 9   */        DIGIT,
70
                                   /* 072 :   */        OTHER,
71
                                   /* 073 ;   */        OTHER,
72
                                   /* 074 <   */        OTHER,
73
                                   /* 075 =   */        OTHER,
74
                                   /* 076 >   */        OTHER,
75
                                   /* 077 ?   */        OTHER,
76
                                   /* 100 @   */        0,
77
                                   /* 101 A   */        LETTER|HEX,
78
                                   /* 102 B   */        LETTER|HEX,
79
                                   /* 103 C   */        LETTER|HEX,
80
                                   /* 104 D   */        LETTER|HEX,
81
                                   /* 105 E   */        LETTER|HEX,
82
                                   /* 106 F   */        LETTER|HEX,
83
                                   /* 107 G   */        LETTER,
84
                                   /* 110 H   */        LETTER,
85
                                   /* 111 I   */        LETTER,
86
                                   /* 112 J   */        LETTER,
87
                                   /* 113 K   */        LETTER,
88
                                   /* 114 L   */        LETTER,
89
                                   /* 115 M   */        LETTER,
90
                                   /* 116 N   */        LETTER,
91
                                   /* 117 O   */        LETTER,
92
                                   /* 120 P   */        LETTER,
93
                                   /* 121 Q   */        LETTER,
94
                                   /* 122 R   */        LETTER,
95
                                   /* 123 S   */        LETTER,
96
                                   /* 124 T   */        LETTER,
97
                                   /* 125 U   */        LETTER,
98
                                   /* 126 V   */        LETTER,
99
                                   /* 127 W   */        LETTER,
100
                                   /* 130 X   */        LETTER,
101
                                   /* 131 Y   */        LETTER,
102
                                   /* 132 Z   */        LETTER,
103
                                   /* 133 [   */        OTHER,
104
                                   /* 134 \   */        OTHER,
105
                                   /* 135 ]   */        OTHER,
106
                                   /* 136 ^   */        OTHER,
107
                                   /* 137 _   */        LETTER,
108
                                   /* 140 `   */        0,
109
                                   /* 141 a   */        LETTER|HEX,
110
                                   /* 142 b   */        LETTER|HEX,
111
                                   /* 143 c   */        LETTER|HEX,
112
                                   /* 144 d   */        LETTER|HEX,
113
                                   /* 145 e   */        LETTER|HEX,
114
                                   /* 146 f   */        LETTER|HEX,
115
                                   /* 147 g   */        LETTER,
116
                                   /* 150 h   */        LETTER,
117
                                   /* 151 i   */        LETTER,
118
                                   /* 152 j   */        LETTER,
119
                                   /* 153 k   */        LETTER,
120
                                   /* 154 l   */        LETTER,
121
                                   /* 155 m   */        LETTER,
122
                                   /* 156 n   */        LETTER,
123
                                   /* 157 o   */        LETTER,
124
                                   /* 160 p   */        LETTER,
125
                                   /* 161 q   */        LETTER,
126
                                   /* 162 r   */        LETTER,
127
                                   /* 163 s   */        LETTER,
128
                                   /* 164 t   */        LETTER,
129
                                   /* 165 u   */        LETTER,
130
                                   /* 166 v   */        LETTER,
131
                                   /* 167 w   */        LETTER,
132
                                   /* 170 x   */        LETTER,
133
                                   /* 171 y   */        LETTER,
134
                                   /* 172 z   */        LETTER,
135
                                   /* 173 {   */        OTHER,
136
                                   /* 174 |   */        OTHER,
137
                                   /* 175 }   */        OTHER,
138
                                   /* 176 ~   */        OTHER, };
139
static struct symbol tval;
140
static char cbuf[BUFSIZE+1];
141
static unsigned int wcbuf[BUFSIZE+1];
142
 
143
Coordinate src;         /* current source coordinate */
144
int t;
145
char *token;            /* current token */
146
Symbol tsym;            /* symbol table entry for current token */
147
 
148
static void *cput(int c, void *cl);
149
static void *wcput(int c, void *cl);
150
static void *scon(int q, void *put(int c, void *cl), void *cl);
151
static int backslash(int q);
152
static Symbol fcon(void);
153
static Symbol icon(unsigned long, int, int);
154
static void ppnumber(char *);
155
 
156
int gettok(void) {
157
        for (;;) {
158
                register unsigned char *rcp = cp;
159
                while (map[*rcp]&BLANK)
160
                        rcp++;
161
                if (limit - rcp < MAXTOKEN) {
162
                        cp = rcp;
163
                        fillbuf();
164
                        rcp = cp;
165
                }
166
                src.file = file;
167
                src.x = (char *)rcp - line;
168
                src.y = lineno;
169
                cp = rcp + 1;
170
                switch (*rcp++) {
171
                case '/': if (*rcp == '*') {
172
                                int c = 0;
173
                                for (rcp++; *rcp != '/' || c != '*'; )
174
                                        if (map[*rcp]&NEWLINE) {
175
                                                if (rcp < limit)
176
                                                        c = *rcp;
177
                                                cp = rcp + 1;
178
                                                nextline();
179
                                                rcp = cp;
180
                                                if (rcp == limit)
181
                                                        break;
182
                                        } else
183
                                                c = *rcp++;
184
                                if (rcp < limit)
185
                                        rcp++;
186
                                else
187
                                        error("unclosed comment\n");
188
                                cp = rcp;
189
                                continue;
190
                          }
191
                          return '/';
192
                case '<':
193
                        if (*rcp == '=') return cp++, LEQ;
194
                        if (*rcp == '<') return cp++, LSHIFT;
195
                        return '<';
196
                case '>':
197
                        if (*rcp == '=') return cp++, GEQ;
198
                        if (*rcp == '>') return cp++, RSHIFT;
199
                        return '>';
200
                case '-':
201
                        if (*rcp == '>') return cp++, DEREF;
202
                        if (*rcp == '-') return cp++, DECR;
203
                        return '-';
204
                case '=': return *rcp == '=' ? cp++, EQL    : '=';
205
                case '!': return *rcp == '=' ? cp++, NEQ    : '!';
206
                case '|': return *rcp == '|' ? cp++, OROR   : '|';
207
                case '&': return *rcp == '&' ? cp++, ANDAND : '&';
208
                case '+': return *rcp == '+' ? cp++, INCR   : '+';
209
                case ';': case ',': case ':':
210
                case '*': case '~': case '%': case '^': case '?':
211
                case '[': case ']': case '{': case '}': case '(': case ')':
212
                        return rcp[-1];
213
                case '\n': case '\v': case '\r': case '\f':
214
                        nextline();
215
                        if (cp == limit) {
216
                                tsym = NULL;
217
                                return EOI;
218
                        }
219
                        continue;
220
 
221
                case 'i':
222
                        if (rcp[0] == 'f'
223
                        && !(map[rcp[1]]&(DIGIT|LETTER))) {
224
                                cp = rcp + 1;
225
                                return IF;
226
                        }
227
                        if (rcp[0] == 'n'
228
                        &&  rcp[1] == 't'
229
                        && !(map[rcp[2]]&(DIGIT|LETTER))) {
230
                                cp = rcp + 2;
231
                                tsym = inttype->u.sym;
232
                                return INT;
233
                        }
234
                        goto id;
235
                case 'h': case 'j': case 'k': case 'm': case 'n': case 'o':
236
                case 'p': case 'q': case 'x': case 'y': case 'z':
237
                case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
238
                case 'G': case 'H': case 'I': case 'J': case 'K':
239
                case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
240
                case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
241
                case 'Y': case 'Z':
242
                id:
243
                        if (limit - rcp < MAXLINE) {
244
                                cp = rcp - 1;
245
                                fillbuf();
246
                                rcp = ++cp;
247
                        }
248
                        assert(cp == rcp);
249
                        token = (char *)rcp - 1;
250
                        while (map[*rcp]&(DIGIT|LETTER))
251
                                rcp++;
252
                        token = stringn(token, (char *)rcp - token);
253
                        tsym = lookup(token, identifiers);
254
                        cp = rcp;
255
                        return ID;
256
                case '0': case '1': case '2': case '3': case '4':
257
                case '5': case '6': case '7': case '8': case '9': {
258
                        unsigned long n = 0;
259
                        if (limit - rcp < MAXLINE) {
260
                                cp = rcp - 1;
261
                                fillbuf();
262
                                rcp = ++cp;
263
                        }
264
                        assert(cp == rcp);
265
                        token = (char *)rcp - 1;
266
                        if (*token == '0' && (*rcp == 'x' || *rcp == 'X')) {
267
                                int d, overflow = 0;
268
                                while (*++rcp) {
269
                                        if (map[*rcp]&DIGIT)
270
                                                d = *rcp - '0';
271
                                        else if (*rcp >= 'a' && *rcp <= 'f')
272
                                                d = *rcp - 'a' + 10;
273
                                        else if (*rcp >= 'A' && *rcp <= 'F')
274
                                                d = *rcp - 'A' + 10;
275
                                        else
276
                                                break;
277
                                        if (n&~(~0UL >> 4))
278
                                                overflow = 1;
279
                                        else
280
                                                n = (n<<4) + d;
281
                                }
282
                                if ((char *)rcp - token <= 2)
283
                                        error("invalid hexadecimal constant `%S'\n", token, (char *)rcp-token);
284
                                cp = rcp;
285
                                tsym = icon(n, overflow, 16);
286
                        } else if (*token == '0') {
287
                                int err = 0, overflow = 0;
288
                                for ( ; map[*rcp]&DIGIT; rcp++) {
289
                                        if (*rcp == '8' || *rcp == '9')
290
                                                err = 1;
291
                                        if (n&~(~0UL >> 3))
292
                                                overflow = 1;
293
                                        else
294
                                                n = (n<<3) + (*rcp - '0');
295
                                }
296
                                if (*rcp == '.' || *rcp == 'e' || *rcp == 'E') {
297
                                        cp = rcp;
298
                                        tsym = fcon();
299
                                        return FCON;
300
                                }
301
                                cp = rcp;
302
                                tsym = icon(n, overflow, 8);
303
                                if (err)
304
                                        error("invalid octal constant `%S'\n", token, (char*)cp-token);
305
                        } else {
306
                                int overflow = 0;
307
                                for (n = *token - '0'; map[*rcp]&DIGIT; ) {
308
                                        int d = *rcp++ - '0';
309
                                        if (n > (ULONG_MAX - d)/10)
310
                                                overflow = 1;
311
                                        else
312
                                                n = 10*n + d;
313
                                }
314
                                if (*rcp == '.' || *rcp == 'e' || *rcp == 'E') {
315
                                        cp = rcp;
316
                                        tsym = fcon();
317
                                        return FCON;
318
                                }
319
                                cp = rcp;
320
                                tsym = icon(n, overflow, 10);
321
                        }
322
                        return ICON;
323
                }
324
                case '.':
325
                        if (rcp[0] == '.' && rcp[1] == '.') {
326
                                cp += 2;
327
                                return ELLIPSIS;
328
                        }
329
                        if ((map[*rcp]&DIGIT) == 0)
330
                                return '.';
331
                        if (limit - rcp < MAXLINE) {
332
                                cp = rcp - 1;
333
                                fillbuf();
334
                                rcp = ++cp;
335
                        }
336
                        assert(cp == rcp);
337
                        cp = rcp - 1;
338
                        token = (char *)cp;
339
                        tsym = fcon();
340
                        return FCON;
341
                case 'L':
342
                        if (*rcp == '\'') {
343
                                unsigned int *s = scon(*cp, wcput, wcbuf);
344
                                if (s - wcbuf > 2)
345
                                        warning("excess characters in wide-character literal ignored\n");
346
                                tval.type = widechar;
347
                                tval.u.c.v.u = wcbuf[0];
348
                                tsym = &tval;
349
                                return ICON;
350
                        } else if (*rcp == '"') {
351
                                unsigned int *s = scon(*cp, wcput, wcbuf);
352
                                tval.type = array(widechar, s - wcbuf, 0);
353
                                tval.u.c.v.p = wcbuf;
354
                                tsym = &tval;
355
                                return SCON;
356
                        } else
357
                                goto id;
358
                case '\'': {
359
                        char *s = scon(*--cp, cput, cbuf);
360
                        if (s - cbuf > 2)
361
                                warning("excess characters in multibyte character literal ignored\n");
362
                        tval.type = inttype;
363
                        if (chartype->op == INT)
364
                                tval.u.c.v.i = extend(cbuf[0], chartype);
365
                        else
366
                                tval.u.c.v.i = cbuf[0]&0xFF;
367
                        tsym = &tval;
368
                        return ICON;
369
                        }
370
                case '"': {
371
                        char *s = scon(*--cp, cput, cbuf);
372
                        tval.type = array(chartype, s - cbuf, 0);
373
                        tval.u.c.v.p = cbuf;
374
                        tsym = &tval;
375
                        return SCON;
376
                        }
377
                case 'a':
378
                        if (rcp[0] == 'u'
379
                        &&  rcp[1] == 't'
380
                        &&  rcp[2] == 'o'
381
                        && !(map[rcp[3]]&(DIGIT|LETTER))) {
382
                                cp = rcp + 3;
383
                                return AUTO;
384
                        }
385
                        goto id;
386
                case 'b':
387
                        if (rcp[0] == 'r'
388
                        &&  rcp[1] == 'e'
389
                        &&  rcp[2] == 'a'
390
                        &&  rcp[3] == 'k'
391
                        && !(map[rcp[4]]&(DIGIT|LETTER))) {
392
                                cp = rcp + 4;
393
                                return BREAK;
394
                        }
395
                        goto id;
396
                case 'c':
397
                        if (rcp[0] == 'a'
398
                        &&  rcp[1] == 's'
399
                        &&  rcp[2] == 'e'
400
                        && !(map[rcp[3]]&(DIGIT|LETTER))) {
401
                                cp = rcp + 3;
402
                                return CASE;
403
                        }
404
                        if (rcp[0] == 'h'
405
                        &&  rcp[1] == 'a'
406
                        &&  rcp[2] == 'r'
407
                        && !(map[rcp[3]]&(DIGIT|LETTER))) {
408
                                cp = rcp + 3;
409
                                tsym = chartype->u.sym;
410
                                return CHAR;
411
                        }
412
                        if (rcp[0] == 'o'
413
                        &&  rcp[1] == 'n'
414
                        &&  rcp[2] == 's'
415
                        &&  rcp[3] == 't'
416
                        && !(map[rcp[4]]&(DIGIT|LETTER))) {
417
                                cp = rcp + 4;
418
                                return CONST;
419
                        }
420
                        if (rcp[0] == 'o'
421
                        &&  rcp[1] == 'n'
422
                        &&  rcp[2] == 't'
423
                        &&  rcp[3] == 'i'
424
                        &&  rcp[4] == 'n'
425
                        &&  rcp[5] == 'u'
426
                        &&  rcp[6] == 'e'
427
                        && !(map[rcp[7]]&(DIGIT|LETTER))) {
428
                                cp = rcp + 7;
429
                                return CONTINUE;
430
                        }
431
                        goto id;
432
                case 'd':
433
                        if (rcp[0] == 'e'
434
                        &&  rcp[1] == 'f'
435
                        &&  rcp[2] == 'a'
436
                        &&  rcp[3] == 'u'
437
                        &&  rcp[4] == 'l'
438
                        &&  rcp[5] == 't'
439
                        && !(map[rcp[6]]&(DIGIT|LETTER))) {
440
                                cp = rcp + 6;
441
                                return DEFAULT;
442
                        }
443
                        if (rcp[0] == 'o'
444
                        &&  rcp[1] == 'u'
445
                        &&  rcp[2] == 'b'
446
                        &&  rcp[3] == 'l'
447
                        &&  rcp[4] == 'e'
448
                        && !(map[rcp[5]]&(DIGIT|LETTER))) {
449
                                cp = rcp + 5;
450
                                tsym = doubletype->u.sym;
451
                                return DOUBLE;
452
                        }
453
                        if (rcp[0] == 'o'
454
                        && !(map[rcp[1]]&(DIGIT|LETTER))) {
455
                                cp = rcp + 1;
456
                                return DO;
457
                        }
458
                        goto id;
459
                case 'e':
460
                        if (rcp[0] == 'l'
461
                        &&  rcp[1] == 's'
462
                        &&  rcp[2] == 'e'
463
                        && !(map[rcp[3]]&(DIGIT|LETTER))) {
464
                                cp = rcp + 3;
465
                                return ELSE;
466
                        }
467
                        if (rcp[0] == 'n'
468
                        &&  rcp[1] == 'u'
469
                        &&  rcp[2] == 'm'
470
                        && !(map[rcp[3]]&(DIGIT|LETTER))) {
471
                                cp = rcp + 3;
472
                                return ENUM;
473
                        }
474
                        if (rcp[0] == 'x'
475
                        &&  rcp[1] == 't'
476
                        &&  rcp[2] == 'e'
477
                        &&  rcp[3] == 'r'
478
                        &&  rcp[4] == 'n'
479
                        && !(map[rcp[5]]&(DIGIT|LETTER))) {
480
                                cp = rcp + 5;
481
                                return EXTERN;
482
                        }
483
                        goto id;
484
                case 'f':
485
                        if (rcp[0] == 'l'
486
                        &&  rcp[1] == 'o'
487
                        &&  rcp[2] == 'a'
488
                        &&  rcp[3] == 't'
489
                        && !(map[rcp[4]]&(DIGIT|LETTER))) {
490
                                cp = rcp + 4;
491
                                tsym = floattype->u.sym;
492
                                return FLOAT;
493
                        }
494
                        if (rcp[0] == 'o'
495
                        &&  rcp[1] == 'r'
496
                        && !(map[rcp[2]]&(DIGIT|LETTER))) {
497
                                cp = rcp + 2;
498
                                return FOR;
499
                        }
500
                        goto id;
501
                case 'g':
502
                        if (rcp[0] == 'o'
503
                        &&  rcp[1] == 't'
504
                        &&  rcp[2] == 'o'
505
                        && !(map[rcp[3]]&(DIGIT|LETTER))) {
506
                                cp = rcp + 3;
507
                                return GOTO;
508
                        }
509
                        goto id;
510
                case 'l':
511
                        if (rcp[0] == 'o'
512
                        &&  rcp[1] == 'n'
513
                        &&  rcp[2] == 'g'
514
                        && !(map[rcp[3]]&(DIGIT|LETTER))) {
515
                                cp = rcp + 3;
516
                                return LONG;
517
                        }
518
                        goto id;
519
                case 'r':
520
                        if (rcp[0] == 'e'
521
                        &&  rcp[1] == 'g'
522
                        &&  rcp[2] == 'i'
523
                        &&  rcp[3] == 's'
524
                        &&  rcp[4] == 't'
525
                        &&  rcp[5] == 'e'
526
                        &&  rcp[6] == 'r'
527
                        && !(map[rcp[7]]&(DIGIT|LETTER))) {
528
                                cp = rcp + 7;
529
                                return REGISTER;
530
                        }
531
                        if (rcp[0] == 'e'
532
                        &&  rcp[1] == 't'
533
                        &&  rcp[2] == 'u'
534
                        &&  rcp[3] == 'r'
535
                        &&  rcp[4] == 'n'
536
                        && !(map[rcp[5]]&(DIGIT|LETTER))) {
537
                                cp = rcp + 5;
538
                                return RETURN;
539
                        }
540
                        goto id;
541
                case 's':
542
                        if (rcp[0] == 'h'
543
                        &&  rcp[1] == 'o'
544
                        &&  rcp[2] == 'r'
545
                        &&  rcp[3] == 't'
546
                        && !(map[rcp[4]]&(DIGIT|LETTER))) {
547
                                cp = rcp + 4;
548
                                return SHORT;
549
                        }
550
                        if (rcp[0] == 'i'
551
                        &&  rcp[1] == 'g'
552
                        &&  rcp[2] == 'n'
553
                        &&  rcp[3] == 'e'
554
                        &&  rcp[4] == 'd'
555
                        && !(map[rcp[5]]&(DIGIT|LETTER))) {
556
                                cp = rcp + 5;
557
                                return SIGNED;
558
                        }
559
                        if (rcp[0] == 'i'
560
                        &&  rcp[1] == 'z'
561
                        &&  rcp[2] == 'e'
562
                        &&  rcp[3] == 'o'
563
                        &&  rcp[4] == 'f'
564
                        && !(map[rcp[5]]&(DIGIT|LETTER))) {
565
                                cp = rcp + 5;
566
                                return SIZEOF;
567
                        }
568
                        if (rcp[0] == 't'
569
                        &&  rcp[1] == 'a'
570
                        &&  rcp[2] == 't'
571
                        &&  rcp[3] == 'i'
572
                        &&  rcp[4] == 'c'
573
                        && !(map[rcp[5]]&(DIGIT|LETTER))) {
574
                                cp = rcp + 5;
575
                                return STATIC;
576
                        }
577
                        if (rcp[0] == 't'
578
                        &&  rcp[1] == 'r'
579
                        &&  rcp[2] == 'u'
580
                        &&  rcp[3] == 'c'
581
                        &&  rcp[4] == 't'
582
                        && !(map[rcp[5]]&(DIGIT|LETTER))) {
583
                                cp = rcp + 5;
584
                                return STRUCT;
585
                        }
586
                        if (rcp[0] == 'w'
587
                        &&  rcp[1] == 'i'
588
                        &&  rcp[2] == 't'
589
                        &&  rcp[3] == 'c'
590
                        &&  rcp[4] == 'h'
591
                        && !(map[rcp[5]]&(DIGIT|LETTER))) {
592
                                cp = rcp + 5;
593
                                return SWITCH;
594
                        }
595
                        goto id;
596
                case 't':
597
                        if (rcp[0] == 'y'
598
                        &&  rcp[1] == 'p'
599
                        &&  rcp[2] == 'e'
600
                        &&  rcp[3] == 'd'
601
                        &&  rcp[4] == 'e'
602
                        &&  rcp[5] == 'f'
603
                        && !(map[rcp[6]]&(DIGIT|LETTER))) {
604
                                cp = rcp + 6;
605
                                return TYPEDEF;
606
                        }
607
                        goto id;
608
                case 'u':
609
                        if (rcp[0] == 'n'
610
                        &&  rcp[1] == 'i'
611
                        &&  rcp[2] == 'o'
612
                        &&  rcp[3] == 'n'
613
                        && !(map[rcp[4]]&(DIGIT|LETTER))) {
614
                                cp = rcp + 4;
615
                                return UNION;
616
                        }
617
                        if (rcp[0] == 'n'
618
                        &&  rcp[1] == 's'
619
                        &&  rcp[2] == 'i'
620
                        &&  rcp[3] == 'g'
621
                        &&  rcp[4] == 'n'
622
                        &&  rcp[5] == 'e'
623
                        &&  rcp[6] == 'd'
624
                        && !(map[rcp[7]]&(DIGIT|LETTER))) {
625
                                cp = rcp + 7;
626
                                return UNSIGNED;
627
                        }
628
                        goto id;
629
                case 'v':
630
                        if (rcp[0] == 'o'
631
                        &&  rcp[1] == 'i'
632
                        &&  rcp[2] == 'd'
633
                        && !(map[rcp[3]]&(DIGIT|LETTER))) {
634
                                cp = rcp + 3;
635
                                tsym = voidtype->u.sym;
636
                                return VOID;
637
                        }
638
                        if (rcp[0] == 'o'
639
                        &&  rcp[1] == 'l'
640
                        &&  rcp[2] == 'a'
641
                        &&  rcp[3] == 't'
642
                        &&  rcp[4] == 'i'
643
                        &&  rcp[5] == 'l'
644
                        &&  rcp[6] == 'e'
645
                        && !(map[rcp[7]]&(DIGIT|LETTER))) {
646
                                cp = rcp + 7;
647
                                return VOLATILE;
648
                        }
649
                        goto id;
650
                case 'w':
651
                        if (rcp[0] == 'h'
652
                        &&  rcp[1] == 'i'
653
                        &&  rcp[2] == 'l'
654
                        &&  rcp[3] == 'e'
655
                        && !(map[rcp[4]]&(DIGIT|LETTER))) {
656
                                cp = rcp + 4;
657
                                return WHILE;
658
                        }
659
                        goto id;
660
                case '_':
661
                        if (rcp[0] == '_'
662
                        &&  rcp[1] == 't'
663
                        &&  rcp[2] == 'y'
664
                        &&  rcp[3] == 'p'
665
                        &&  rcp[4] == 'e'
666
                        &&  rcp[5] == 'c'
667
                        &&  rcp[6] == 'o'
668
                        &&  rcp[7] == 'd'
669
                        &&  rcp[8] == 'e'
670
                        && !(map[rcp[9]]&(DIGIT|LETTER))) {
671
                                cp = rcp + 9;
672
                                return TYPECODE;
673
                        }
674
                        if (rcp[0] == '_'
675
                        &&  rcp[1] == 'f'
676
                        &&  rcp[2] == 'i'
677
                        &&  rcp[3] == 'r'
678
                        &&  rcp[4] == 's'
679
                        &&  rcp[5] == 't'
680
                        &&  rcp[6] == 'a'
681
                        &&  rcp[7] == 'r'
682
                        &&  rcp[8] == 'g'
683
                        && !(map[rcp[9]]&(DIGIT|LETTER))) {
684
                                cp = rcp + 9;
685
                                return FIRSTARG;
686
                        }
687
                        goto id;
688
                default:
689
                        if ((map[cp[-1]]&BLANK) == 0)
690
                                if (cp[-1] < ' ' || cp[-1] >= 0177)
691
                                        error("illegal character `\\0%o'\n", cp[-1]);
692
                                else
693
                                        error("illegal character `%c'\n", cp[-1]);
694
                }
695
        }
696
}
697
static Symbol icon(unsigned long n, int overflow, int base) {
698
        if ((*cp=='u'||*cp=='U') && (cp[1]=='l'||cp[1]=='L')
699
        ||  (*cp=='l'||*cp=='L') && (cp[1]=='u'||cp[1]=='U')) {
700
                tval.type = unsignedlong;
701
                cp += 2;
702
        } else if (*cp == 'u' || *cp == 'U') {
703
                if (overflow || n > unsignedtype->u.sym->u.limits.max.i)
704
                        tval.type = unsignedlong;
705
                else
706
                        tval.type = unsignedtype;
707
                cp += 1;
708
        } else if (*cp == 'l' || *cp == 'L') {
709
                if (overflow || n > longtype->u.sym->u.limits.max.i)
710
                        tval.type = unsignedlong;
711
                else
712
                        tval.type = longtype;
713
                cp += 1;
714
        } else if (overflow || n > longtype->u.sym->u.limits.max.i)
715
                tval.type = unsignedlong;
716
        else if (n > inttype->u.sym->u.limits.max.i)
717
                tval.type = longtype;
718
        else if (base != 10 && n > inttype->u.sym->u.limits.max.i)
719
                tval.type = unsignedtype;
720
        else
721
                tval.type = inttype;
722
        switch (tval.type->op) {
723
        case INT:
724
                if (overflow || n > tval.type->u.sym->u.limits.max.i) {
725
                        warning("overflow in constant `%S'\n", token,
726
                                (char*)cp - token);
727
                        tval.u.c.v.i = tval.type->u.sym->u.limits.max.i;
728
                } else
729
                        tval.u.c.v.i = n;
730
                break;
731
        case UNSIGNED:
732
                if (overflow || n > tval.type->u.sym->u.limits.max.u) {
733
                        warning("overflow in constant `%S'\n", token,
734
                                (char*)cp - token);
735
                        tval.u.c.v.u = tval.type->u.sym->u.limits.max.u;
736
                } else
737
                        tval.u.c.v.u = n;
738
                break;
739
        default: assert(0);
740
        }
741
        ppnumber("integer");
742
        return &tval;
743
}
744
static void ppnumber(char *which) {
745
        unsigned char *rcp = cp--;
746
 
747
        for ( ; (map[*cp]&(DIGIT|LETTER)) || *cp == '.'; cp++)
748
                if ((cp[0] == 'E' || cp[0] == 'e')
749
                &&  (cp[1] == '-' || cp[1] == '+'))
750
                        cp++;
751
        if (cp > rcp)
752
                error("`%S' is a preprocessing number but an invalid %s constant\n", token,
753
 
754
                        (char*)cp-token, which);
755
}
756
static Symbol fcon(void) {
757
        if (*cp == '.')
758
                do
759
                        cp++;
760
                while (map[*cp]&DIGIT);
761
        if (*cp == 'e' || *cp == 'E') {
762
                if (*++cp == '-' || *cp == '+')
763
                        cp++;
764
                if (map[*cp]&DIGIT)
765
                        do
766
                                cp++;
767
                        while (map[*cp]&DIGIT);
768
                else
769
                        error("invalid floating constant `%S'\n", token,
770
                                (char*)cp - token);
771
        }
772
 
773
        errno = 0;
774
        tval.u.c.v.d = strtod(token, NULL);
775
        if (errno == ERANGE)
776
                warning("overflow in floating constant `%S'\n", token,
777
                        (char*)cp - token);
778
        if (*cp == 'f' || *cp == 'F') {
779
                ++cp;
780
                if (tval.u.c.v.d > floattype->u.sym->u.limits.max.d)
781
                        warning("overflow in floating constant `%S'\n", token,
782
                                (char*)cp - token);
783
                tval.type = floattype;
784
        } else if (*cp == 'l' || *cp == 'L') {
785
                cp++;
786
                tval.type = longdouble;
787
        } else {
788
                if (tval.u.c.v.d > doubletype->u.sym->u.limits.max.d)
789
                        warning("overflow in floating constant `%S'\n", token,
790
                                (char*)cp - token);
791
                tval.type = doubletype;
792
        }
793
        ppnumber("floating");
794
        return &tval;
795
}
796
 
797
static void *cput(int c, void *cl) {
798
        char *s = cl;
799
 
800
        if (c < 0 || c > 255)
801
                warning("overflow in escape sequence with resulting value `%d'\n", c);
802
        *s++ = c;
803
        return s;
804
}
805
 
806
static void *wcput(int c, void *cl) {
807
        unsigned int *s = cl;
808
 
809
        *s++ = c;
810
        return s;
811
}
812
 
813
static void *scon(int q, void *put(int c, void *cl), void *cl) {
814
        int n = 0, nbad = 0;
815
 
816
        do {
817
                cp++;
818
                while (*cp != q) {
819
                        int c;
820
                        if (map[*cp]&NEWLINE) {
821
                                if (cp < limit)
822
                                        break;
823
                                cp++;
824
                                nextline();
825
                                if (cp == limit)
826
                                        break;
827
                                continue;
828
                        }
829
                        c = *cp++;
830
                        if (c == '\\') {
831
                                if (map[*cp]&NEWLINE) {
832
                                        if (cp++ < limit)
833
                                                continue;
834
                                        nextline();
835
                                }
836
                                if (limit - cp < MAXTOKEN)
837
                                        fillbuf();
838
                                c = backslash(q);
839
                        } else if (c < 0 || c > 255 || map[c] == 0)
840
                                nbad++;
841
                        if (n++ < BUFSIZE)
842
                                cl = put(c, cl);
843
                }
844
                if (*cp == q)
845
                        cp++;
846
                else
847
                        error("missing %c\n", q);
848
                if (q == '"' && put == wcput && getchr() == 'L') {
849
                        if (limit - cp < 2)
850
                                fillbuf();
851
                        if (cp[1] == '"')
852
                                cp++;
853
                }
854
        } while (q == '"' && getchr() == '"');
855
        cl = put(0, cl);
856
        if (n >= BUFSIZE)
857
                error("%s literal too long\n", q == '"' ? "string" : "character");
858
        if (Aflag >= 2 && q == '"' && n > 509)
859
                warning("more than 509 characters in a string literal\n");
860
        if (Aflag >= 2 && nbad > 0)
861
                warning("%s literal contains non-portable characters\n",
862
                        q == '"' ? "string" : "character");
863
        return cl;
864
}
865
int getchr(void) {
866
        for (;;) {
867
                while (map[*cp]&BLANK)
868
                        cp++;
869
                if (!(map[*cp]&NEWLINE))
870
                        return *cp;
871
                cp++;
872
                nextline();
873
                if (cp == limit)
874
                        return EOI;
875
        }
876
}
877
static int backslash(int q) {
878
        unsigned int c;
879
 
880
        switch (*cp++) {
881
        case 'a': return 7;
882
        case 'b': return '\b';
883
        case 'f': return '\f';
884
        case 'n': return '\n';
885
        case 'r': return '\r';
886
        case 't': return '\t';
887
        case 'v': return '\v';
888
        case '\'': case '"': case '\\': case '\?': break;
889
        case 'x': {
890
                int overflow = 0;
891
                if ((map[*cp]&(DIGIT|HEX)) == 0) {
892
                        if (*cp < ' ' || *cp == 0177)
893
                                error("ill-formed hexadecimal escape sequence\n");
894
                        else
895
                                error("ill-formed hexadecimal escape sequence `\\x%c'\n", *cp);
896
                        if (*cp != q)
897
                                cp++;
898
                        return 0;
899
                }
900
                for (c = 0; map[*cp]&(DIGIT|HEX); cp++) {
901
                        if (c >> (8*widechar->size - 4))
902
                                overflow = 1;
903
                        if (map[*cp]&DIGIT)
904
                                c = (c<<4) + *cp - '0';
905
                        else
906
                                c = (c<<4) + (*cp&~040) - 'A' + 10;
907
                }
908
                if (overflow)
909
                        warning("overflow in hexadecimal escape sequence\n");
910
                return c&ones(8*widechar->size);
911
                }
912
        case '0': case '1': case '2': case '3':
913
        case '4': case '5': case '6': case '7':
914
                c = *(cp-1) - '0';
915
                if (*cp >= '0' && *cp <= '7') {
916
                        c = (c<<3) + *cp++ - '0';
917
                        if (*cp >= '0' && *cp <= '7')
918
                                c = (c<<3) + *cp++ - '0';
919
                }
920
                return c;
921
        default:
922
                if (cp[-1] < ' ' || cp[-1] >= 0177)
923
                        warning("unrecognized character escape sequence\n");
924
                else
925
                        warning("unrecognized character escape sequence `\\%c'\n", cp[-1]);
926
        }
927
        return cp[-1];
928
}

powered by: WebSVN 2.1.0

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