xref: /original-bsd/usr.bin/struct/struct/3.then.c (revision c3e32dec)
1 /*-
2  * %sccs.include.proprietary.c%
3  */
4 
5 #ifndef lint
6 static char sccsid[] = "@(#)3.then.c	8.1 (Berkeley) 06/06/93";
7 #endif /* not lint */
8 
9 #include <stdio.h>
10 #include "def.h"
11 #include "3.def.h"
12 
13 #define BRANCHTYPE(t)	(t == STOPVX || t == RETVX || t == BRKVX || t == NXTVX || t == GOVX)
14 #define MAXCHUNK	20
15 		/* if else clause smaller than MAXCHUNK and smaller than then clause,
16 			and there is no reason not to negate the if, negate the if */
17 
18 getthen(v)		/* turn IFVX into THEN when appropriate, create else ifs where possible  */
19 VERT v;
20 	{
21 	VERT tch, fch;
22 	int tn,fn;
23 	int recvar;
24 
25 	if (NTYPE(v) == IFVX)
26 		{
27 		tch = LCHILD(v,THEN);
28 		fch = LCHILD(v,ELSE);
29 		if (!DEFINED(fch))
30 			mkthen(v);
31 		else if (!DEFINED(tch))
32 			{
33 			negate(v);
34 			mkthen(v);
35 			}
36 		else if (BRANCHTYPE(NTYPE(tch)))
37 			mkthen(v);
38 		else if (BRANCHTYPE(NTYPE(fch)))
39 			{
40 			negate(v);
41 			mkthen(v);
42 			}
43 		else if (NTYPE(fch) != IFVX || DEFINED(RSIB(fch)))	/* not an else if */
44 			if ( NTYPE(tch) == IFVX && !DEFINED(RSIB(tch)))
45 					/* invert into else if */
46 				negate(v);
47 			else
48 				{
49 				/* asoc(v,n) returns number of statements associated with v
50 					if <= n, -1 otherwise */
51 				tn = asoc(tch,MAXCHUNK);
52 				fn = asoc(fch,MAXCHUNK);
53 				if (fn >= 0 && (tn < 0 || fn < tn))
54 					/* else clause smaller */
55 					negate(v);
56 				}
57 		}
58 	RECURSE(getthen,v,recvar);
59 	}
60 
61 mkthen(v)
62 VERT v;
63 	{
64 	VERT w,tc;
65 	w = LCHILD(v,ELSE);
66 	tc = LCHILD(v,THEN);
67 	ASSERT(!DEFINED(w) || (DEFINED(tc) && BRANCHTYPE(NTYPE(tc)) ),mkthen);
68 	if (DEFINED(w))
69 		{
70 		insib(v,w);
71 		LCHILD(v,ELSE) = UNDEFINED;
72 		}
73 	ASSERT(IFTHEN(v),mkthen);
74 	}
75 
76 
77 negate(v)
78 VERT v;
79 	{
80 	ASSERT(NTYPE(v) == IFVX,negate);
81 	exchange(&LCHILD(v,THEN), &LCHILD(v,ELSE));
82 	NEG(v) = !NEG(v);
83 	}
84