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