date and time created 82/01/18 19:21:33 by linton
authorMark Linton <linton@ucbvax.Berkeley.EDU>
Tue, 19 Jan 1982 11:21:33 +0000 (03:21 -0800)
committerMark Linton <linton@ucbvax.Berkeley.EDU>
Tue, 19 Jan 1982 11:21:33 +0000 (03:21 -0800)
SCCS-vsn: usr.bin/pascal/pdx/tree/tr_equal.c 1.1

usr/src/usr.bin/pascal/pdx/tree/tr_equal.c [new file with mode: 0644]

diff --git a/usr/src/usr.bin/pascal/pdx/tree/tr_equal.c b/usr/src/usr.bin/pascal/pdx/tree/tr_equal.c
new file mode 100644 (file)
index 0000000..ccf83e6
--- /dev/null
@@ -0,0 +1,65 @@
+/* Copyright (c) 1982 Regents of the University of California */
+
+static char sccsid[] = "@(#)tr_equal.c 1.1 %G%";
+
+/*
+ * A recursive tree search routine to test if two trees
+ * are structurally equivalent.
+ */
+
+#include "defs.h"
+#include "tree.h"
+#include "tree.rep"
+
+BOOLEAN tr_equal(t1, t2)
+register NODE *t1;
+register NODE *t2;
+{
+       if (t1 == NIL && t2 == NIL) {
+               return(TRUE);
+       }
+       if (t1 == NIL || t2 == NIL) {
+               return(FALSE);
+       }
+       if (t1->op != t2->op || degree(t1->op) != degree(t2->op)) {
+               return(FALSE);
+       }
+       switch(degree(t1->op)) {
+               case LEAF:
+                       switch(t1->op) {
+                               case O_NAME:
+                                       return(t1->nameval == t2->nameval);
+
+                               case O_QNAME:
+                                       if (!tr_equal(t1->right, t2->right)) {
+                                               return(FALSE);
+                                       }
+                                       return(tr_equal(t1->left, t2->left));
+
+                               case O_LCON:
+                                       return(t1->lconval == t2->lconval);
+
+                               case O_FCON:
+                                       return(t1->fconval == t2->fconval);
+
+                               case O_SCON:
+                                       return(t1->sconval == t2->sconval);
+
+                               default:
+                                       panic("tr_equal: leaf %d\n", t1->op);
+                       }
+                       /*NOTREACHED*/
+
+               case BINARY:
+                       if (!tr_equal(t1->right, t2->right)) {
+                               return(FALSE);
+                       }
+                       /* else fall through */
+               case UNARY:
+                       return(tr_equal(t1->left, t2->left));
+
+               default:
+                       panic("tr_equal: bad degree for op %d\n", t1->op);
+       }
+       /*NOTREACHED*/
+}