xref: /original-bsd/old/cpp/yylex.c (revision 8ac030d2)
1 #ifndef lint
2 static char sccsid[] = "@(#)yylex.c 1.1 08/30/82";
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
9 #define COFF 128
10 #else
11 #define COFF 0
12 #endif
13 
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 	newp=skipbl(newp);
30 	if (*inp=='\n') return(stop);	/* end of #if */
31 	savc= *newp; *newp='\0';
32 	for (p2=op2+8; --p2>=op2; )	/* check 2-char ops */
33 		if (0==strcmp(*p2,inp)) {val=val2[p2-op2]; goto ret;}
34 	s="+-*/%<>&^|?:!~(),";	/* check 1-char ops */
35 	while (*s) if (*s++== *inp) {val= *--s; goto ret;}
36 	if (*inp<='9' && *inp>='0') {/* a number */
37 		if (*inp=='0') yylval= (inp[1]=='x' || inp[1]=='X') ?
38 			tobinary(inp+2,16) : tobinary(inp+1,8);
39 		else yylval=tobinary(inp,10);
40 		val=number;
41 	} else if (isid(*inp)) {
42 		if (0==strcmp(inp,"defined")) {ifdef=1; ++flslvl; val=DEFINED;}
43 		else {
44 			sp=lookup(inp,-1); if (ifdef!=0) {ifdef=0; --flslvl;}
45 			yylval= (sp->value==0) ? 0 : 1;
46 			val=number;
47 		}
48 	} else 	if (*inp=='\'') {/* character constant */
49 		val=number;
50 		if (inp[1]=='\\') {/* escaped */
51 			char c; if (newp[-1]=='\'') newp[-1]='\0';
52 			s=opc;
53 			while (*s) if (*s++!=inp[2]) ++s; else {yylval= *s; goto ret;}
54 			if (inp[2]<='9' && inp[2]>='0') yylval=c=tobinary(inp+2,8);
55 			else yylval=inp[2];
56 		} else yylval=inp[1];
57 	} else if (0==strcmp("\\\n",inp)) {*newp=savc; continue;}
58 	else {
59 		*newp=savc; pperror("Illegal character %c in preprocessor if", *inp);
60 		continue;
61 	}
62 ret:
63 	*newp=savc; outp=inp=newp; return(val);
64 }
65 }
66 
67 tobinary(st, b) char *st; {
68 	int n, c, t;
69 	char *s;
70 	n=0;
71 	s=st;
72 	while (c = *s++) {
73 	switch(c) {
74 		case '0': case '1': case '2': case '3': case '4':
75 		case '5': case '6': case '7': case '8': case '9':
76 			t = c-'0'; break;
77 		case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
78 			t = c-'a'; if (b>10) break;
79 		case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
80 			t = c - 'A'; if (b>10) break;
81 		default:
82 			t = -1;
83 			if ( c=='l' || c=='L') if (*s=='\0') break;
84 			pperror("Illegal number %s", st);
85 	}
86 	if (t<0) break;
87 	n = n*b+t;
88 	}
89 return(n);
90 }
91