'''
Created on 29.04.2017

@author: Mr. Jones
'''
import Queue
from visitors import constantvisitor
     
class PrintTraversal(object):
    
    def __init__(self,nodes):
        self.node_list = Queue.Queue()
        self.temp_blacklist = []
        if isinstance(nodes, list):
            for i in nodes:
                self.node_list.put(i)
        else:
            self.node_list.put(nodes)
        
        
    def traverse(self):
        '''avoid printing the program node which would contain the complete output'''
        next_node = self.node_list.get()
        self.temp_blacklist.append(next_node)
        print next_node,next_node.fchildren_list
        for i in next_node.fchildren_list:
            if i not in self.temp_blacklist:
                self.node_list.put(i)
                self.temp_blacklist.append(i)
        while(not self.node_list.empty()):
            next_node = self.node_list.get()
            self.temp_blacklist.append(next_node)
            if next_node is not None:
                print next_node.to_ecma(),next_node.fchildren_list
                for i in next_node.fchildren_list:
                    print 'edge to',i.to_ecma()
                    if i not in self.temp_blacklist:
                        self.node_list.put(i)
                        self.temp_blacklist.append(i)

                
class ConstantTraversal(object):
    
    def __init__(self,nodes):
        self.node_list = Queue.Queue()
        self.constant_visitor = constantvisitor.ConstantVisitor()
        if isinstance(nodes, list):
            for i in nodes:
                self.node_list.put(i)
        else:
            self.node_list.put(nodes)      
        
    def traverse(self):
        #TODO avoid loops
        while(not self.node_list.empty()):
            next_node = self.node_list.get()
            for i in next_node.fchildren_list:
                self.node_list.put(i)
            self.constant_visitor.do(next_node.node_list)
