Research V7 development
[unix-history] / usr / src / cmd / cpp / yylex.c
CommitLineData
8150d190
KT
1#define isid(a) ((fastab+COFF)[a]&IB)
2#define IB 1
3/* #if '\377' < 0 it would be nice if this worked properly!!!!! */
4#if pdp11 | vax
5#define COFF 128
6#else
7#define COFF 0
8#endif
9
10yylex() {
11 static int ifdef=0;
12 static char *op2[]={"||", "&&" , ">>", "<<", ">=", "<=", "!=", "=="};
13 static int val2[]={OROR, ANDAND, RS, LS, GE, LE, NE, EQ};
14 static char *opc="b\bt\tn\nf\fr\r\\\\";
15 extern char fastab[];
16 extern char *outp,*inp,*newp; extern int flslvl;
17 register char savc, *s; char *skipbl(); int val;
18 register char **p2;
19 struct symtab {
20 char *name;
21 char *value;
22 } *sp;
23
24for (;;) {
25 newp=skipbl(newp);
26 if (*inp=='\n') return(stop); /* end of #if */
27 savc= *newp; *newp='\0';
28 for (p2=op2+8; --p2>=op2; ) /* check 2-char ops */
29 if (0==strcmp(*p2,inp)) {val=val2[p2-op2]; goto ret;}
30 s="+-*/%<>&^|?:!~(),"; /* check 1-char ops */
31 while (*s) if (*s++== *inp) {val= *--s; goto ret;}
32 if (*inp<='9' && *inp>='0') {/* a number */
33 if (*inp=='0') yylval= (inp[1]=='x' || inp[1]=='X') ?
34 tobinary(inp+2,16) : tobinary(inp+1,8);
35 else yylval=tobinary(inp,10);
36 val=number;
37 } else if (isid(*inp)) {
38 if (0==strcmp(inp,"defined")) {ifdef=1; ++flslvl; val=DEFINED;}
39 else {
40 sp=lookup(inp,-1); if (ifdef!=0) {ifdef=0; --flslvl;}
41 yylval= (sp->value==0) ? 0 : 1;
42 val=number;
43 }
44 } else if (*inp=='\'') {/* character constant */
45 val=number;
46 if (inp[1]=='\\') {/* escaped */
47 char c; if (newp[-1]=='\'') newp[-1]='\0';
48 s=opc;
49 while (*s) if (*s++!=inp[2]) ++s; else {yylval= *s; goto ret;}
50 if (inp[2]<='9' && inp[2]>='0') yylval=c=tobinary(inp+2,8);
51 else yylval=inp[2];
52 } else yylval=inp[1];
53 } else if (0==strcmp("\\\n",inp)) {*newp=savc; continue;}
54 else {
55 *newp=savc; pperror("Illegal character %c in preprocessor if", *inp);
56 continue;
57 }
58ret:
59 *newp=savc; outp=inp=newp; return(val);
60}
61}
62
63tobinary(st, b) char *st; {
64 int n, c, t;
65 char *s;
66 n=0;
67 s=st;
68 while (c = *s++) {
69 switch(c) {
70 case '0': case '1': case '2': case '3': case '4':
71 case '5': case '6': case '7': case '8': case '9':
72 t = c-'0'; break;
73 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
74 t = c-'a'; if (b>10) break;
75 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
76 t = c - 'A'; if (b>10) break;
77 default:
78 t = -1;
79 if ( c=='l' || c=='L') if (*s=='\0') break;
80 pperror("Illegal number %s", st);
81 }
82 if (t<0) break;
83 n = n*b+t;
84 }
85return(n);
86}