]> git.mdlowis.com Git - archive/parse-utils.git/commitdiff
Added comparison operators and process method for AST class.
authorMike D. Lowis <mike@mdlowis.com>
Thu, 7 Jun 2012 23:18:21 +0000 (19:18 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Thu, 7 Jun 2012 23:18:21 +0000 (19:18 -0400)
source/parser/ast/ast.cpp
source/parser/ast/ast.h

index 172b16cf8b3c43d9fb85612a2d7e76e44dbf1a62..d7c715a3d4cd1184ec1e98653e2abad95bd5f97e 100644 (file)
@@ -1,4 +1,5 @@
 #include "ast.h"
+#include "ivisitor.h"
 #include <sstream>
 #include <string.h>
 #include <iostream>
@@ -98,7 +99,7 @@ void AST::type(ASTNodeType typ)
     node_type = typ;
 }
 
-list<AST*>* AST::children(void)
+list<AST*>* AST::children(void) const
 {
     return node_children;
 }
@@ -129,3 +130,48 @@ AST* AST::clone(void) const
     return new_clone;
 }
 
+bool AST::operator ==(const AST& rhs) const
+{
+    bool ret = true;
+    std::list<AST*>* l_children;
+    std::list<AST*>* r_children;
+    std::list<AST*>::iterator lit;
+    std::list<AST*>::iterator rit;
+
+    // Setup our locals
+    l_children = children();
+    r_children = rhs.children();
+    lit = l_children->begin();
+    rit = r_children->begin();
+
+    // Check this node for equality
+    ret &= (type() == rhs.type());
+    ret &= ( 0 == text().compare( rhs.text() ) );
+    ret &= (l_children->size() == r_children->size());
+
+    // If we are still equal then check the children nodes
+    while( (lit != l_children->end()) && (rit != r_children->end()) )
+    {
+        ret &= ((NULL != *lit) && (NULL != *rit));
+        if( ret )
+        {
+            AST& left = *(*lit);
+            AST& right = *(*rit);
+            ret &= (left == right);
+        }
+        lit++;
+        rit++;
+    }
+    return ret;
+}
+
+bool AST::operator !=(const AST& rhs) const
+{
+    return !( *this == rhs );
+}
+
+void AST::process(IVisitor& visitor)
+{
+    visitor.visit( this );
+}
+
index 5661ce103ac3374eb1bc46d08d90120ffb4b5e22..ae4a479ca667720787465bc2bf73c846c3c51da9 100644 (file)
@@ -8,6 +8,9 @@
 
 typedef unsigned int ASTNodeType;
 
+// This is defined elsewhere. We need to declare it here so we can use it
+class IVisitor;
+
 class AST
 {
     protected:
@@ -22,16 +25,17 @@ class AST
         AST(ASTNodeType type, int child_count, ...);
         AST(ASTNodeType type, std::string text, int child_count, ...);
         virtual ~AST();
-
         AST& operator = (AST& rhs);
-
         ASTNodeType type(void) const;
         void type(ASTNodeType typ);
         std::string text(void) const;
         void text(std::string& txt);
-        std::list<AST*>* children(void);
+        std::list<AST*>* children(void) const;
         void addChild(AST* node);
         AST* clone(void) const;
+        bool operator ==(const AST &other) const;
+        bool operator !=(const AST &other) const;
+        void process(IVisitor& visitor);
 };
 
 #endif