xref: /csrg-svn/contrib/dungeon/lex.c (revision 35973)
1*35973Sbostic #define FALSE	0
2*35973Sbostic #define TRUE	1
3*35973Sbostic 
lex_(inbuf,inlnt,outbuf,op,vbflag,lprscon)4*35973Sbostic lex_(inbuf, inlnt, outbuf, op, vbflag, lprscon)
5*35973Sbostic 	char inbuf[78];
6*35973Sbostic 	int outbuf[40], *inlnt, *op, *vbflag;
7*35973Sbostic 	int *lprscon;	/* added */
8*35973Sbostic {
9*35973Sbostic 	/*
10*35973Sbostic 	 * lex - lexical analyzer, converted from fortran
11*35973Sbostic 	 *
12*35973Sbostic 	 * input: one line of ascii characters
13*35973Sbostic 	 * output: tokenized input, packed in radix-50 format
14*35973Sbostic 	 */
15*35973Sbostic 
16*35973Sbostic 	char	j;
17*35973Sbostic 	int	cp, i, k, prsptr;
18*35973Sbostic 	static int	num601 = {601};
19*35973Sbostic 
20*35973Sbostic 	for (i=0; i<40; i++)
21*35973Sbostic 		outbuf[i] = 0;
22*35973Sbostic 	*op = -1;
23*35973Sbostic 	prsptr = *lprscon - 1;
24*35973Sbostic 	/* printf("lex: inbuf=%s, inlnt=%d\n", inbuf, *inlnt); */
25*35973Sbostic 
26*35973Sbostic toknlp:
27*35973Sbostic 	*op += 2;
28*35973Sbostic 	cp = 0;
29*35973Sbostic 	while ((*lprscon)++ <= *inlnt) {
30*35973Sbostic 		j = inbuf[prsptr++];
31*35973Sbostic 		/* printf("lex: chr=%c\n", j); */
32*35973Sbostic 		if ((j == '.') || (j == ','))
33*35973Sbostic 			break;
34*35973Sbostic 		else if (j == ' ')
35*35973Sbostic 			if (cp)		/* if (cp != 0) */
36*35973Sbostic 				goto toknlp;
37*35973Sbostic 			else
38*35973Sbostic 				continue;   /* first token */
39*35973Sbostic 		else if ((j >= 'A') && (j <= 'Z'))
40*35973Sbostic 			j -= '@';
41*35973Sbostic 		else if (((j >= '1') && (j <= '9')) || (j == '-'))
42*35973Sbostic 			j -= 0x12;
43*35973Sbostic 		else {
44*35973Sbostic 			if (*vbflag)
45*35973Sbostic 				rspeak_(&num601);
46*35973Sbostic 			return(FALSE);
47*35973Sbostic 		}
48*35973Sbostic 
49*35973Sbostic 		if (cp >= 6)
50*35973Sbostic 			/*
51*35973Sbostic 			 * ignore remainder of any token > 6 chars
52*35973Sbostic 			 */
53*35973Sbostic 			continue;
54*35973Sbostic 		/*
55*35973Sbostic 		 * pack three chars per word in radix-50 format
56*35973Sbostic 		 */
57*35973Sbostic 		k = *op + (cp/3) - 1;
58*35973Sbostic 		/* printf("*op=%d, cp=%d, k=%d\n", *op, cp, k); */
59*35973Sbostic 		switch (cp%3) {
60*35973Sbostic 			case 0:
61*35973Sbostic 				outbuf[k] += j * 1560;
62*35973Sbostic 			case 1:
63*35973Sbostic 				outbuf[k] += j * 39;
64*35973Sbostic 			case 2:
65*35973Sbostic 				outbuf[k] += j;
66*35973Sbostic 		}
67*35973Sbostic 		cp++;
68*35973Sbostic 	}
69*35973Sbostic 	if (*lprscon > *inlnt)
70*35973Sbostic 		*lprscon = 1;
71*35973Sbostic 	if (!cp)	/* if (cp == 0) */
72*35973Sbostic 		if (*op == 1)
73*35973Sbostic 			return(FALSE);   /* no valid tokens */
74*35973Sbostic 		else {
75*35973Sbostic 			*op -= 2;
76*35973Sbostic 			return(TRUE);
77*35973Sbostic 		};
78*35973Sbostic }
79