1 /* Copyright (c) 1982 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)tr_equal.c 1.1 01/18/82";
4 
5 /*
6  * A recursive tree search routine to test if two trees
7  * are structurally equivalent.
8  */
9 
10 #include "defs.h"
11 #include "tree.h"
12 #include "tree.rep"
13 
14 BOOLEAN tr_equal(t1, t2)
15 register NODE *t1;
16 register NODE *t2;
17 {
18 	if (t1 == NIL && t2 == NIL) {
19 		return(TRUE);
20 	}
21 	if (t1 == NIL || t2 == NIL) {
22 		return(FALSE);
23 	}
24 	if (t1->op != t2->op || degree(t1->op) != degree(t2->op)) {
25 		return(FALSE);
26 	}
27 	switch(degree(t1->op)) {
28 		case LEAF:
29 			switch(t1->op) {
30 				case O_NAME:
31 					return(t1->nameval == t2->nameval);
32 
33 				case O_QNAME:
34 					if (!tr_equal(t1->right, t2->right)) {
35 						return(FALSE);
36 					}
37 					return(tr_equal(t1->left, t2->left));
38 
39 				case O_LCON:
40 					return(t1->lconval == t2->lconval);
41 
42 				case O_FCON:
43 					return(t1->fconval == t2->fconval);
44 
45 				case O_SCON:
46 					return(t1->sconval == t2->sconval);
47 
48 				default:
49 					panic("tr_equal: leaf %d\n", t1->op);
50 			}
51 			/*NOTREACHED*/
52 
53 		case BINARY:
54 			if (!tr_equal(t1->right, t2->right)) {
55 				return(FALSE);
56 			}
57 			/* else fall through */
58 		case UNARY:
59 			return(tr_equal(t1->left, t2->left));
60 
61 		default:
62 			panic("tr_equal: bad degree for op %d\n", t1->op);
63 	}
64 	/*NOTREACHED*/
65 }
66