<reponame>FuSkrN/P6<filename>p6/reducer.py<gh_stars>1-10
#TODO: delete imports after
import generator
import python_reader

class reducer:
    """A collection (class) of methods used in graph reduction."""
    def __init__(self, stateArray):
        self.stateArray = stateArray
    
    def reduce_sequence(self):
        """Implements the function link_ingoing_outgoing using a state queue."""
        stateQueue = self.stateArray.copy()
        while len(stateQueue) != 0:

            #resultFlag is a flag indicating whether a state in the stateQueue is able to be reduced in first-order.
            resultFlag = stateQueue[0].link_ingoing_outgoing_one_to_one()
            
            # If True, remove the reduced state from the state array.
            if resultFlag:
                self.stateArray.pop(self.stateArray.index(stateQueue[0]))
            
            #Remove the state from the state queue.
            stateQueue.pop(0)

    def reduce_local_varsplit(self):
        stateQueue = self.stateArray.copy()
        resultFlag = False
        while len(stateQueue) != 0:
            parentState = stateQueue[0]
            if len(parentState.outgoing) > 1:
                for childState in parentState.outgoing:
                    parentGlobalVars = parentState.symboltable.find_global_vars()
                    childGlobalVars = childState.symboltable.find_global_vars()
                    if parentGlobalVars == childGlobalVars:
                        resultFlag = childState.link_ingoing_outgoing_one_to_many()

            # If True, remove the reduced state from the state array.
            if resultFlag:
                self.stateArray.pop(self.stateArray.index(stateQueue[0]))
            
            #Remove the state from the state queue.
            stateQueue.pop(0)


# Read and parse a C file, and then generate its resulting state array graph.
#a = python_reader.C_Reader('pthread_setting_variables.c')
#a.get_scopes(a.file)
#b = generator.graph(a.result)

#Perform graph reduction.
#r = reducer(b.stateArray)
#r.reduce_sequence()
#for r in a.result:
#    print(r)
#for x in r.stateArray:
#    print(f"stateName: {x.label}")
#    print(f"programCounters: {x.programCounters:}")
#    for p in x.symboltable.symboltable:
#        print(p)
#    for y in x.ingoing:
#        print(f"ingoing: {y.label}")
#    for z in x.outgoing:
#        print(f"outgoing: {z.label}")
#    print("\n")

#print(f"size before: {len(b.stateArray)}")
#print(f"size after: {len(r.stateArray)}")

#TODO: delete imports after
import generator
import python_reader

class reducer:
    """A collection (class) of methods used in graph reduction."""
    def __init__(self, stateArray):
        self.stateArray = stateArray
    
    def reduce_sequence(self):
        """Implements the function link_ingoing_outgoing using a state queue."""
        stateQueue = self.stateArray.copy()
        while len(stateQueue) != 0:

            #resultFlag is a flag indicating whether a state in the stateQueue is able to be reduced in first-order.
            resultFlag = stateQueue[0].link_ingoing_outgoing_one_to_one()
            
            # If True, remove the reduced state from the state array.
            if resultFlag:
                self.stateArray.pop(self.stateArray.index(stateQueue[0]))
            
            #Remove the state from the state queue.
            stateQueue.pop(0)

    def reduce_local_varsplit(self):
        stateQueue = self.stateArray.copy()
        resultFlag = False
        while len(stateQueue) != 0:
            parentState = stateQueue[0]
            if len(parentState.outgoing) > 1:
                for childState in parentState.outgoing:
                    parentGlobalVars = parentState.symboltable.find_global_vars()
                    childGlobalVars = childState.symboltable.find_global_vars()
                    if parentGlobalVars == childGlobalVars:
                        resultFlag = childState.link_ingoing_outgoing_one_to_many()

            # If True, remove the reduced state from the state array.
            if resultFlag:
                self.stateArray.pop(self.stateArray.index(stateQueue[0]))
            
            #Remove the state from the state queue.
            stateQueue.pop(0)


# Read and parse a C file, and then generate its resulting state array graph.
#a = python_reader.C_Reader('pthread_setting_variables.c')
#a.get_scopes(a.file)
#b = generator.graph(a.result)

#Perform graph reduction.
#r = reducer(b.stateArray)
#r.reduce_sequence()
#for r in a.result:
#    print(r)
#for x in r.stateArray:
#    print(f"stateName: {x.label}")
#    print(f"programCounters: {x.programCounters:}")
#    for p in x.symboltable.symboltable:
#        print(p)
#    for y in x.ingoing:
#        print(f"ingoing: {y.label}")
#    for z in x.outgoing:
#        print(f"outgoing: {z.label}")
#    print("\n")

#print(f"size before: {len(b.stateArray)}")
#print(f"size after: {len(r.stateArray)}")
#TODO: delete imports after A collection (class) of methods used in graph reduction. Implements the function link_ingoing_outgoing using a state queue. #resultFlag is a flag indicating whether a state in the stateQueue is able to be reduced in first-order. # If True, remove the reduced state from the state array. #Remove the state from the state queue. # If True, remove the reduced state from the state array. #Remove the state from the state queue. # Read and parse a C file, and then generate its resulting state array graph. #a = python_reader.C_Reader('pthread_setting_variables.c') #a.get_scopes(a.file) #b = generator.graph(a.result) #Perform graph reduction. #r = reducer(b.stateArray) #r.reduce_sequence() #for r in a.result: #    print(r) #for x in r.stateArray: #    print(f"stateName: {x.label}") #    print(f"programCounters: {x.programCounters:}") #    for p in x.symboltable.symboltable: #        print(p) #    for y in x.ingoing: #        print(f"ingoing: {y.label}") #    for z in x.outgoing: #        print(f"outgoing: {z.label}") #    print("\n") #print(f"size before: {len(b.stateArray)}") #print(f"size after: {len(r.stateArray)}")