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