xref: /csrg-svn/old/cpp/yylex.c (revision 29876)
17980Srrh #ifndef lint
2*29876Ssam static char sccsid[] = "@(#)yylex.c	1.4 10/15/86";
37980Srrh #endif lint
47980Srrh 
57980Srrh #define isid(a)  ((fastab+COFF)[a]&IB)
67980Srrh #define IB 1
77980Srrh /*	#if '\377' < 0		it would be nice if this worked properly!!!!! */
8*29876Ssam #if pdp11 | vax | mc68000 | tahoe
97980Srrh #define COFF 128
107980Srrh #else
117980Srrh #define COFF 0
127980Srrh #endif
137980Srrh 
yylex()147980Srrh yylex() {
157980Srrh 	static int ifdef=0;
167980Srrh 	static char *op2[]={"||",  "&&" , ">>", "<<", ">=", "<=", "!=", "=="};
177980Srrh 	static int  val2[]={OROR, ANDAND,  RS,   LS,   GE,   LE,   NE,   EQ};
187980Srrh 	static char *opc="b\bt\tn\nf\fr\r\\\\";
197980Srrh 	extern char fastab[];
207980Srrh 	extern char *outp,*inp,*newp; extern int flslvl;
217980Srrh 	register char savc, *s; char *skipbl(); int val;
227980Srrh 	register char **p2;
237980Srrh 	struct symtab {
247980Srrh 		char *name;
257980Srrh 		char *value;
267980Srrh 	} *sp, *lookup();
277980Srrh 
287980Srrh for (;;) {
2912978Ssam 	extern int passcom;		/* this crap makes #if's work */
3012978Ssam 	int opt_passcom = passcom;	/* even with -C option */
3112978Ssam 	passcom = 0;			/* (else comments make syntax errs) */
327980Srrh 	newp=skipbl(newp);
3312978Ssam 	passcom = opt_passcom;		/* nb: lint uses -C so its useful! */
347980Srrh 	if (*inp=='\n') return(stop);	/* end of #if */
357980Srrh 	savc= *newp; *newp='\0';
367980Srrh 	for (p2=op2+8; --p2>=op2; )	/* check 2-char ops */
377980Srrh 		if (0==strcmp(*p2,inp)) {val=val2[p2-op2]; goto ret;}
387980Srrh 	s="+-*/%<>&^|?:!~(),";	/* check 1-char ops */
397980Srrh 	while (*s) if (*s++== *inp) {val= *--s; goto ret;}
407980Srrh 	if (*inp<='9' && *inp>='0') {/* a number */
417980Srrh 		if (*inp=='0') yylval= (inp[1]=='x' || inp[1]=='X') ?
427980Srrh 			tobinary(inp+2,16) : tobinary(inp+1,8);
437980Srrh 		else yylval=tobinary(inp,10);
447980Srrh 		val=number;
457980Srrh 	} else if (isid(*inp)) {
467980Srrh 		if (0==strcmp(inp,"defined")) {ifdef=1; ++flslvl; val=DEFINED;}
477980Srrh 		else {
487980Srrh 			sp=lookup(inp,-1); if (ifdef!=0) {ifdef=0; --flslvl;}
497980Srrh 			yylval= (sp->value==0) ? 0 : 1;
507980Srrh 			val=number;
517980Srrh 		}
527980Srrh 	} else 	if (*inp=='\'') {/* character constant */
537980Srrh 		val=number;
547980Srrh 		if (inp[1]=='\\') {/* escaped */
557980Srrh 			char c; if (newp[-1]=='\'') newp[-1]='\0';
567980Srrh 			s=opc;
577980Srrh 			while (*s) if (*s++!=inp[2]) ++s; else {yylval= *s; goto ret;}
587980Srrh 			if (inp[2]<='9' && inp[2]>='0') yylval=c=tobinary(inp+2,8);
597980Srrh 			else yylval=inp[2];
607980Srrh 		} else yylval=inp[1];
617980Srrh 	} else if (0==strcmp("\\\n",inp)) {*newp=savc; continue;}
627980Srrh 	else {
637980Srrh 		*newp=savc; pperror("Illegal character %c in preprocessor if", *inp);
647980Srrh 		continue;
657980Srrh 	}
667980Srrh ret:
677980Srrh 	*newp=savc; outp=inp=newp; return(val);
687980Srrh }
697980Srrh }
707980Srrh 
tobinary(st,b)717980Srrh tobinary(st, b) char *st; {
727980Srrh 	int n, c, t;
737980Srrh 	char *s;
747980Srrh 	n=0;
757980Srrh 	s=st;
767980Srrh 	while (c = *s++) {
777980Srrh 	switch(c) {
787980Srrh 		case '0': case '1': case '2': case '3': case '4':
797980Srrh 		case '5': case '6': case '7': case '8': case '9':
807980Srrh 			t = c-'0'; break;
817980Srrh 		case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
8212978Ssam 			t = c-'a'+10; if (b>10) break;
837980Srrh 		case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
8412978Ssam 			t = c - 'A'+10; if (b>10) break;
857980Srrh 		default:
867980Srrh 			t = -1;
877980Srrh 			if ( c=='l' || c=='L') if (*s=='\0') break;
887980Srrh 			pperror("Illegal number %s", st);
897980Srrh 	}
907980Srrh 	if (t<0) break;
917980Srrh 	n = n*b+t;
927980Srrh 	}
937980Srrh return(n);
947980Srrh }
95