<commit_before>
# from tiles import Tile, Relay, Source, Flag, Generator, Switch, Counter,Pulsar,Timer,Sequencer

import tiles as tile_mod

class Compiler:

    def __init__(self,board):

        self.total_cycles=0

        self.board=board


        for x,col in enumerate(self.board.tiles):
            for y,tile in enumerate(col):

                if(type(tile)!=tile_mod.Tile and type(tile)!=tile_mod.Relay):

                    single={}


                    single['type']=tile
                    single['coords']=[x,y]
                    
                    single['options']=[]


                    if(type(tile)!=tile_mod.Flag and type(tile)!=tile_mod.Sequencer):
                        single['outputs']=[]
                        for ind, check, direction in zip(tile.adj_ind,tile.conector_checks,[2,3,0,1]):
                            if (ind!=None and check.get()==1):

                                if (type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Relay
                                    and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()==1):
                                    print('run relay seach')
                                    single['outputs'].extend(self.parse_relays(self.board.tiles[ind[0]][ind[1]]))

                                else:
                                    potencial_tile=self.parse_non_relay_conections(ind, check, direction)
                                    if(potencial_tile!=None):
                                        single['outputs'].append(potencial_tile)

                        print(single)





    def parse_relays(self,relay_example):
        i=relay_example.state_index

        out=[]
        for tile in self.board.relay_groups[i]:

            for ind, check, direction in zip(tile.adj_ind,tile.conector_checks,[2,3,0,1]):


                if(ind!=None and check.get()==1
                    and type(self.board.tiles[ind[0]][ind[1]])!=tile_mod.Tile 
                    and type(self.board.tiles[ind[0]][ind[1]])!=tile_mod.Relay):


                    potencial_tile=self.parse_non_relay_conections(ind, check, direction)
                    if(potencial_tile!=None):
                        out.append(potencial_tile)

        return out




    def parse_non_relay_conections(self,ind, check, direction):

        if(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Flag
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()!=0):

            return self.tile_coord_label(ind[0],ind[1],"con")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Switch
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()==2):

            return self.tile_coord_label(ind[0],ind[1],"con")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Counter
            and direction==3):

            return self.tile_coord_label(ind[0],ind[1],"con")

        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Counter
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()!=0
            and direction==2):

            return self.tile_coord_label(ind[0],ind[1],"reset")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Timer
            and direction==3):

            return self.tile_coord_label(ind[0],ind[1],"con")

        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Timer
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()!=0
            and direction==2):

            return self.tile_coord_label(ind[0],ind[1],"reset")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Pulsar
            and direction==3):

            return self.tile_coord_label(ind[0],ind[1],"con")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Sequencer
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()!=0):

            return self.tile_coord_label(ind[0],ind[1],"con")

        return None




    def tiles_outputed_to_by_relay(self,index,relay_group):
        pass

    def tile_coord_label(self,x,y,extra=""):
        return "tile_"+str(x)+"_"+str(y)


    def delay_code(self,cycles):

        assert(type(cycles)==int)
        if(cycles==0):
            return [""]

        if(0<cycles<4):
            return [" NOP"]*cycles

        if(3<cycles<7):
            return [" CALL small_delay_"+str(cycles)]

        if(int(cycles/3)-2>255):
            raise OverflowError

        if(6<cycles):
            return [" MOVLW "+str(int(cycles/3)-2)," CALL delay3_"+str( 3 if cycles%3==0 else cycles%3)]

    def delay_footer(self):
        return ["small_delay_6 nop",
        "small_delay_5 nop",
        "small_delay_4 return",
        "delay3_3 Nop",
        "delay3_2 Nop",
        "delay3_1 Decfsz w,w",
        "goto delay1_0",
        "return"]<commit_msg>Moved to Node object code organisation<commit_after>
# from tiles import Tile, Relay, Source, Flag, Generator, Switch, Counter,Pulsar,Timer,Sequencer

import tiles as tile_mod


class Node:

    def __init__(self,tile,x,y):

        self.cycles=0
        self.info={}


        self.info['type']=type(tile)
        self.info['coords']=[x,y]
        
        self.info['options']=[]


        if(type(tile)!=tile_mod.Flag and type(tile)!=tile_mod.Sequencer):
            self.info['outputs']=[]
            for ind, check, direction in zip(tile.adj_ind,tile.conector_checks,[2,3,0,1]):
                if (ind!=None and check.get()==1):

                    if (type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Relay
                        and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()==1):
                        self.info['outputs'].extend(self.parse_relays(self.board.tiles[ind[0]][ind[1]]))

                    else:
                        potencial_tile=self.parse_non_relay_conections(ind, check, direction)
                        if(potencial_tile!=None):
                            self.info['outputs'].append(potencial_tile)

            print(self.info)


        if(self.info['type']==tile_mod.Flag):
            self.info['options'].append(tile.self.name)

        elif(self.info['type']==tile_mod.Generator):
            self.info['options'].append(self.subscribe_name.get())
            self.info['options'].append(self.invert.get())



    def generate_code(self,total_cycles):

        pass



    def parse_relays(self,relay_example):
        i=relay_example.state_index

        out=[]
        for tile in self.board.relay_groups[i]:

            for ind, check, direction in zip(tile.adj_ind,tile.conector_checks,[2,3,0,1]):


                if(ind!=None and check.get()==1
                    and type(self.board.tiles[ind[0]][ind[1]])!=tile_mod.Tile 
                    and type(self.board.tiles[ind[0]][ind[1]])!=tile_mod.Relay):


                    potencial_tile=self.parse_non_relay_conections(ind, check, direction)
                    if(potencial_tile!=None):
                        out.append(potencial_tile)

        return out




    def parse_non_relay_conections(self,ind, check, direction):

        if(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Flag
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()!=0):

            return self.tile_coord_label(ind[0],ind[1],"con")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Switch
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()==2):

            return self.tile_coord_label(ind[0],ind[1],"con")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Counter
            and direction==3):

            return self.tile_coord_label(ind[0],ind[1],"con")

        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Counter
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()!=0
            and direction==2):

            return self.tile_coord_label(ind[0],ind[1],"reset")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Timer
            and direction==3):

            return self.tile_coord_label(ind[0],ind[1],"con")

        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Timer
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()!=0
            and direction==2):

            return self.tile_coord_label(ind[0],ind[1],"reset")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Pulsar
            and direction==3):

            return self.tile_coord_label(ind[0],ind[1],"con")


        elif(type(self.board.tiles[ind[0]][ind[1]])==tile_mod.Sequencer
            and self.board.tiles[ind[0]][ind[1]].conector_checks[direction].get()!=0):

            return self.tile_coord_label(ind[0],ind[1],"con")

        return None




    def tiles_outputed_to_by_relay(self,index,relay_group):
        pass

    def tile_coord_label(self,x,y,extra=""):
        return "tile_"+str(x)+"_"+str(y)


    def delay_code(self,cycles):

        assert(type(cycles)==int)
        if(cycles==0):
            return [""]

        if(0<cycles<4):
            return [" NOP"]*cycles

        if(3<cycles<7):
            return [" CALL small_delay_"+str(cycles)]

        if(int(cycles/3)-2>255):
            raise OverflowError

        if(6<cycles):
            return [" MOVLW "+str(int(cycles/3)-2)," CALL delay3_"+str( 3 if cycles%3==0 else cycles%3)]


class Compiler:

    def __init__(self,board):

        self.total_cycles=0
        self.board=board

        self.tiles_linear=[]




        for x,col in enumerate(self.board.tiles):
            for y,tile in enumerate(col):

                if(type(tile)!=tile_mod.Tile and type(tile)!=tile_mod.Relay):

                    self.tiles_linear.append(Node(tile,x,y))




    def get_code(self,ioinfo):

        pass





    def delay_footer(self):
        return ["small_delay_6 nop",
        "small_delay_5 nop",
        "small_delay_4 return",
        "delay3_3 Nop",
        "delay3_2 Nop",
        "delay3_1 Decfsz w,w",
        "goto delay1_0",
        "return"]
{'fraction_non_alphanumeric': 0.15574257425742574, 'fraction_numerical': 0.020495049504950496, 'mean_word_length': 2.2395766516998075, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2721990', 'n_tokens_mistral': 3514, 'n_tokens_neox': 3277, 'n_words': 409}