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