xref: /csrg-svn/usr.bin/f77/pass1.tahoe/fmt.c (revision 47951)
1*47951Sbostic /*-
2*47951Sbostic  * Copyright (c) 1980 The Regents of the University of California.
3*47951Sbostic  * All rights reserved.
4*47951Sbostic  *
5*47951Sbostic  * %sccs.include.proprietary.c%
643209Sbostic  */
743209Sbostic 
843209Sbostic #ifndef lint
9*47951Sbostic static char sccsid[] = "@(#)fmt.c	5.2 (Berkeley) 04/12/91";
10*47951Sbostic #endif /* not lint */
1143209Sbostic 
1243209Sbostic /*
1343209Sbostic  *
1443209Sbostic  * fortran format parser
1543209Sbostic  * corresponds to fmt.c in /usr/lib/libI77
1643209Sbostic  */
1743209Sbostic 
1843209Sbostic /* define ERROR, OK, GLITCH, NO, YES
1943209Sbostic  * from /usr/src/usr.lib/libI77/fiodefs.h
2043209Sbostic  */
2143209Sbostic 
2243209Sbostic #define GLITCH '\2'	/* special quote for Stu, generated in f77pass1 */
2343209Sbostic #define ERROR	1
2443209Sbostic #define OK	0
2543209Sbostic #define YES	1
2643209Sbostic #define NO	0
2743209Sbostic 
2843209Sbostic /* define struct syl[] and lots of defines for format terms */
2943209Sbostic #include "format.h"
3043209Sbostic 
3143209Sbostic #define isdigit(x)	(x>='0' && x<='9')
3243209Sbostic #define isspace(s)	(s==' ')
3343209Sbostic #define skip(s)		while(isspace(*s)) s++
3443209Sbostic 
3543209Sbostic #ifdef interdata
3643209Sbostic #define SYLMX 300
3743209Sbostic #endif
3843209Sbostic 
3943209Sbostic #ifdef pdp11
4043209Sbostic #define SYLMX 300
4143209Sbostic #endif
4243209Sbostic 
4343209Sbostic #ifdef tahoe
4443209Sbostic #define SYLMX 300
4543209Sbostic #endif
4643209Sbostic 
4743209Sbostic struct syl syl[SYLMX];
4843209Sbostic int parenlvl,revloc, low_case[256];
4943209Sbostic short pc;
5043209Sbostic char *f_s(), *f_list(), *i_tem(), *gt_num(), *ap_end();
5143209Sbostic char *s_init, *fmtptr;
5243209Sbostic int fmt_strings;		/* tells if have hollerith or string in format*/
5343209Sbostic 
pars_f(s)5443209Sbostic pars_f(s) char *s;
5543209Sbostic {
5643209Sbostic 	int i;
5743209Sbostic 
5843209Sbostic 	/* first time, initialize low_case[] */
5943209Sbostic 	if( low_case[1] == 0 ) {
6043209Sbostic 	    for(i = 0; i<256; i++) low_case[i]=i;
6143209Sbostic 	    for(i = 'A'; i<='Z'; i++) low_case[i]=i-'A'+'a';
6243209Sbostic 	}
6343209Sbostic 
6443209Sbostic 	fmt_strings = 0;
6543209Sbostic 	parenlvl=revloc=pc=0;
6643209Sbostic 	s_init = s;	/* save beginning location of format */
6743209Sbostic 	return((f_s(s,0)==FMTERR)? ERROR : OK);
6843209Sbostic }
6943209Sbostic 
f_s(s,curloc)7043209Sbostic char *f_s(s,curloc) char *s;
7143209Sbostic {
7243209Sbostic 	skip(s);
7343209Sbostic 	if(*s++!='(')
7443209Sbostic 	{
7543209Sbostic 		fmtptr = s;
7643209Sbostic 		return(FMTERR);
7743209Sbostic 	}
7843209Sbostic 	if(parenlvl++ ==1) revloc=curloc;
7943209Sbostic 	op_gen(RET,curloc,0,0,s);
8043209Sbostic 	if((s=f_list(s))==FMTERR)
8143209Sbostic 	{
8243209Sbostic 		return(FMTERR);
8343209Sbostic 	}
8443209Sbostic 	skip(s);
8543209Sbostic 	return(s);
8643209Sbostic }
8743209Sbostic 
f_list(s)8843209Sbostic char *f_list(s) char *s;
8943209Sbostic {
9043209Sbostic 	while (*s)
9143209Sbostic 	{	skip(s);
9243209Sbostic 		if((s=i_tem(s))==FMTERR) return(FMTERR);
9343209Sbostic 		skip(s);
9443209Sbostic 		if(*s==',') s++;
9543209Sbostic 		else if(*s==')')
9643209Sbostic 		{	if(--parenlvl==0)
9743209Sbostic 				op_gen(REVERT,revloc,0,0,s);
9843209Sbostic 			else
9943209Sbostic 				op_gen(GOTO,0,0,0,s);
10043209Sbostic 			return(++s);
10143209Sbostic 		}
10243209Sbostic 	}
10343209Sbostic 	fmtptr = s;
10443209Sbostic 	return(FMTERR);
10543209Sbostic }
10643209Sbostic 
i_tem(s)10743209Sbostic char *i_tem(s) char *s;
10843209Sbostic {	char *t;
10943209Sbostic 	int n,curloc;
11043209Sbostic 	if(*s==')') return(s);
11143209Sbostic 	if ((n=ne_d(s,&t))==FMTOK)
11243209Sbostic 		return(t);
11343209Sbostic 	else if (n==FMTERR)
11443209Sbostic 		return(FMTERR);
11543209Sbostic 	if ((n=e_d(s,&t))==FMTOK)
11643209Sbostic 		return(t);
11743209Sbostic 	else if (n==FMTERR)
11843209Sbostic 		return(FMTERR);
11943209Sbostic 	s=gt_num(s,&n);
12043209Sbostic 	if (n == 0) { fmtptr = s; return(FMTERR); }
12143209Sbostic 	curloc = op_gen(STACK,n,0,0,s);
12243209Sbostic 	return(f_s(s,curloc));
12343209Sbostic }
12443209Sbostic 
ne_d(s,p)12543209Sbostic ne_d(s,p) char *s,**p;
12643209Sbostic {	int n,x,sign=0,pp1,pp2;
12743209Sbostic 	switch(low_case[*s])
12843209Sbostic 	{
12943209Sbostic 	case ':': op_gen(COLON,(int)('\n'),0,0,s); break;
13043209Sbostic #ifndef KOSHER
13143209Sbostic 	case '$': op_gen(DOLAR,(int)('\0'),0,0,s); break;  /*** NOT STANDARD FORTRAN ***/
13243209Sbostic #endif
13343209Sbostic 	case 'b':
13443209Sbostic 		switch(low_case[*(s+1)])
13543209Sbostic 		{
13643209Sbostic 			case 'n': s++; op_gen(BNZ,0,0,0,s); break;
13743209Sbostic 			case 'z': s++; op_gen(BNZ,1,0,0,s); break;
13843209Sbostic #ifndef KOSHER
13943209Sbostic 			default: op_gen(B,0,0,0,s); break;  /*** NOT STANDARD FORTRAN ***/
14043209Sbostic #else
14143209Sbostic 			default: fmtptr = s; return(FMTUNKN);
14243209Sbostic #endif
14343209Sbostic 		}
14443209Sbostic 		break;
14543209Sbostic 	case 's':
14643209Sbostic 		switch(low_case[*(s+1)])
14743209Sbostic 		{
14843209Sbostic 			case 'p': s++; x=SP; pp1=1; pp2=1; break;
14943209Sbostic #ifndef KOSHER
15043209Sbostic 			case 'u': s++; x=SU; pp1=0; pp2=0; break;  /*** NOT STANDARD FORTRAN ***/
15143209Sbostic #endif
15243209Sbostic 			case 's': s++; x=SS; pp1=0; pp2=1; break;
15343209Sbostic 			default:  x=S; pp1=0; pp2=1; break;
15443209Sbostic 		}
15543209Sbostic 		op_gen(x,pp1,pp2,0,s);
15643209Sbostic 		break;
15743209Sbostic 	case '/': op_gen(SLASH,0,0,0,s); break;
15843209Sbostic 
15943209Sbostic 	case '-': sign=1;	/* OUTRAGEOUS CODING */
16043209Sbostic 	case '+': s++;		/* OUTRAGEOUS CODING */
16143209Sbostic 	case '0': case '1': case '2': case '3': case '4':
16243209Sbostic 	case '5': case '6': case '7': case '8': case '9':
16343209Sbostic 		s=gt_num(s,&n);
16443209Sbostic 		switch(low_case[*s])
16543209Sbostic 		{
16643209Sbostic 		case 'p': if(sign) n= -n; op_gen(P,n,0,0,s); break;
16743209Sbostic #ifndef KOSHER
16843209Sbostic 		case 'r': if(n<=1)		/*** NOT STANDARD FORTRAN ***/
16943209Sbostic 			{	fmtptr = --s; return(FMTERR); }
17043209Sbostic 			op_gen(R,n,0,0,s); break;
17143209Sbostic 		case 't': op_gen(T,0,n,0,s); break;	/* NOT STANDARD FORT */
17243209Sbostic #endif
17343209Sbostic 		case 'x': op_gen(X,n,0,0,s); break;
17443209Sbostic 		case 'h': op_gen(H,n,(s+1)-s_init,0,s);
17543209Sbostic 			s+=n;
17643209Sbostic 			fmt_strings = 1;
17743209Sbostic 			break;
17843209Sbostic 		default: fmtptr = s; return(FMTUNKN);
17943209Sbostic 		}
18043209Sbostic 		break;
18143209Sbostic 	case GLITCH:
18243209Sbostic 	case '"':
18343209Sbostic 	case '\'': op_gen(APOS,s-s_init,0,0,s);
18443209Sbostic 		*p = ap_end(s);
18543209Sbostic 		fmt_strings = 1;
18643209Sbostic 		return(FMTOK);
18743209Sbostic 	case 't':
18843209Sbostic 		switch(low_case[*(s+1)])
18943209Sbostic 		{
19043209Sbostic 			case 'l': s++; x=TL; break;
19143209Sbostic 			case 'r': s++; x=TR; break;
19243209Sbostic 			default:  x=T; break;
19343209Sbostic 		}
19443209Sbostic 		if(isdigit(*(s+1))) {s=gt_num(s+1,&n); s--;}
19543209Sbostic #ifdef KOSHER
19643209Sbostic 		else { fmtptr = s; return(FMTERR); }
19743209Sbostic #else
19843209Sbostic 		else n = 0;	/* NOT STANDARD FORTRAN, should be error */
19943209Sbostic #endif
20043209Sbostic 		op_gen(x,n,1,0,s);
20143209Sbostic 		break;
20243209Sbostic 	case 'x': op_gen(X,1,0,0,s); break;
20343209Sbostic 	case 'p': op_gen(P,0,0,0,s); break;
20443209Sbostic #ifndef KOSHER
20543209Sbostic 	case 'r': op_gen(R,10,1,0,s); break;  /*** NOT STANDARD FORTRAN ***/
20643209Sbostic #endif
20743209Sbostic 
20843209Sbostic 	default: fmtptr = s; return(FMTUNKN);
20943209Sbostic 	}
21043209Sbostic 	s++;
21143209Sbostic 	*p=s;
21243209Sbostic 	return(FMTOK);
21343209Sbostic }
21443209Sbostic 
e_d(s,p)21543209Sbostic e_d(s,p) char *s,**p;
21643209Sbostic {	int n,w,d,e,x=0, rep_count;
21743209Sbostic 	char *sv=s;
21843209Sbostic 	char c;
21943209Sbostic 	s=gt_num(s,&rep_count);
22043209Sbostic 	if (rep_count == 0) goto ed_err;
22143209Sbostic 	c = low_case[*s]; s++;
22243209Sbostic 	switch(c)
22343209Sbostic 	{
22443209Sbostic 	case 'd':
22543209Sbostic 	case 'e':
22643209Sbostic 	case 'g':
22743209Sbostic 		s = gt_num(s, &w);
22843209Sbostic 		if (w==0) goto ed_err;
22943209Sbostic 		if(*s=='.')
23043209Sbostic 		{	s++;
23143209Sbostic 			s=gt_num(s,&d);
23243209Sbostic 		}
23343209Sbostic 		else d=0;
23443209Sbostic 		if(low_case[*s] == 'e'
23543209Sbostic #ifndef KOSHER
23643209Sbostic 		|| *s == '.'		 /*** '.' is NOT STANDARD FORTRAN ***/
23743209Sbostic #endif
23843209Sbostic 		)
23943209Sbostic 		{	s++;
24043209Sbostic 			s=gt_num(s,&e);
24143209Sbostic 			if (e==0 || e>127 || d>127 ) goto ed_err;
24243209Sbostic 			if(c=='e') n=EE; else if(c=='d') n=DE; else n=GE;
24343209Sbostic 			op_gen(n,w,d + (e<<8),rep_count,s);
24443209Sbostic 		}
24543209Sbostic 		else
24643209Sbostic 		{
24743209Sbostic 			if(c=='e') n=E; else if(c=='d') n=D; else n=G;
24843209Sbostic 			op_gen(n,w,d,rep_count,s);
24943209Sbostic 		}
25043209Sbostic 		break;
25143209Sbostic 	case 'l':
25243209Sbostic 		s = gt_num(s, &w);
25343209Sbostic 		if (w==0) goto ed_err;
25443209Sbostic 		op_gen(L,w,0,rep_count,s);
25543209Sbostic 		break;
25643209Sbostic 	case 'a':
25743209Sbostic 		skip(s);
25843209Sbostic 		if(isdigit(*s))
25943209Sbostic 		{	s=gt_num(s,&w);
26043209Sbostic #ifdef	KOSHER
26143209Sbostic 			if (w==0) goto ed_err;
26243209Sbostic #else
26343209Sbostic 			if (w==0) op_gen(A,0,0,rep_count,s);
26443209Sbostic 			else
26543209Sbostic #endif
26643209Sbostic 			op_gen(AW,w,0,rep_count,s);
26743209Sbostic 			break;
26843209Sbostic 		}
26943209Sbostic 		op_gen(A,0,0,rep_count,s);
27043209Sbostic 		break;
27143209Sbostic 	case 'f':
27243209Sbostic 		s = gt_num(s, &w);
27343209Sbostic 		if (w==0) goto ed_err;
27443209Sbostic 		if(*s=='.')
27543209Sbostic 		{	s++;
27643209Sbostic 			s=gt_num(s,&d);
27743209Sbostic 		}
27843209Sbostic 		else d=0;
27943209Sbostic 		op_gen(F,w,d,rep_count,s);
28043209Sbostic 		break;
28143209Sbostic #ifndef	KOSHER
28243209Sbostic 	case 'o':	/*** octal format - NOT STANDARD FORTRAN ***/
28343209Sbostic 	case 'z':	/*** hex   format - NOT STANDARD FORTRAN ***/
28443209Sbostic #endif
28543209Sbostic 	case 'i':
28643209Sbostic 		s = gt_num(s, &w);
28743209Sbostic 		if (w==0) goto ed_err;
28843209Sbostic 		if(*s =='.')
28943209Sbostic 		{
29043209Sbostic 			s++;
29143209Sbostic 			s=gt_num(s,&d);
29243209Sbostic 			x = IM;
29343209Sbostic 		}
29443209Sbostic 		else
29543209Sbostic 		{	d = 1;
29643209Sbostic 			x = I;
29743209Sbostic 		}
29843209Sbostic #ifndef KOSHER
29943209Sbostic 		if (c == 'o')
30043209Sbostic 			op_gen(R,8,1,rep_count,s);
30143209Sbostic 		else if (c == 'z')
30243209Sbostic 			op_gen(R,16,1,rep_count,s);
30343209Sbostic #endif
30443209Sbostic 		op_gen(x,w,d,rep_count,s);
30543209Sbostic #ifndef KOSHER
30643209Sbostic 		if (c == 'o' || c == 'z')
30743209Sbostic 			op_gen(R,10,1,rep_count,s);
30843209Sbostic #endif
30943209Sbostic 		break;
31043209Sbostic 	default:
31143209Sbostic 		*p = sv;
31243209Sbostic 		fmtptr = s;
31343209Sbostic 		return(FMTUNKN);
31443209Sbostic 	}
31543209Sbostic 	*p = s;
31643209Sbostic 	return(FMTOK);
31743209Sbostic ed_err:
31843209Sbostic 	fmtptr = --s;
31943209Sbostic 	return(FMTERR);
32043209Sbostic }
32143209Sbostic 
op_gen(a,b,c,rep,s)32243209Sbostic op_gen(a,b,c,rep,s) char *s;
32343209Sbostic {	struct syl *p= &syl[pc];
32443209Sbostic 	if(pc>=SYLMX)
32543209Sbostic 	{	fmtptr = s;
32643209Sbostic 		err("format too complex");
32743209Sbostic 	}
32843209Sbostic 	if( b>32767 || c>32767 || rep>32767 )
32943209Sbostic 	{	fmtptr = s;
33043209Sbostic 		err("field width or repeat count too large");
33143209Sbostic 	}
33243209Sbostic #ifdef DEBUG
33343209Sbostic 	fprintf(stderr,"%3d opgen: %d %d %d %d %c\n",
33443209Sbostic 		pc,a,b,c,rep,*s==GLITCH?'"':*s); /* for debug */
33543209Sbostic #endif
33643209Sbostic 	p->op=a;
33743209Sbostic 	p->p1=b;
33843209Sbostic 	p->p2=c;
33943209Sbostic 	p->rpcnt=rep;
34043209Sbostic 	return(pc++);
34143209Sbostic }
34243209Sbostic 
gt_num(s,n)34343209Sbostic char *gt_num(s,n) char *s; int *n;
34443209Sbostic {	int m=0,a_digit=NO;
34543209Sbostic 	skip(s);
34643209Sbostic 	while(isdigit(*s) || isspace(*s))
34743209Sbostic 	{
34843209Sbostic 		if (isdigit(*s))
34943209Sbostic 		{
35043209Sbostic 			m = 10*m + (*s)-'0';
35143209Sbostic 			a_digit = YES;
35243209Sbostic 		}
35343209Sbostic 		s++;
35443209Sbostic 	}
35543209Sbostic 	if(a_digit) *n=m;
35643209Sbostic 	else *n=1;
35743209Sbostic 	return(s);
35843209Sbostic }
35943209Sbostic 
ap_end(s)36043209Sbostic char *ap_end(s) char *s;
36143209Sbostic {
36243209Sbostic 	char quote;
36343209Sbostic 	quote = *s++;
36443209Sbostic 	for(;*s;s++)
36543209Sbostic 	{
36643209Sbostic 		if(*s==quote && *++s!=quote) return(s);
36743209Sbostic 	}
36843209Sbostic 	fmtptr = s;
36943209Sbostic 	err("bad string");
37043209Sbostic }
371