<commit_before>import types

from hdl_toolkit.hdlObjects.specialValues import DIRECTION
from hdl_toolkit.hdlObjects.typeShortcuts import hInt, vec
from hdl_toolkit.hdlObjects.types.defs import BIT
from hdl_toolkit.hdlObjects.types.typeCast import toHVal
from hdl_toolkit.synthesizer.interfaceLevel.interface.utils import walkPhysInterfaces
from hdl_toolkit.synthesizer.interfaceLevel.mainBases import InterfaceBase
from hdl_toolkit.synthesizer.rtlLevel.signalUtils.walkers import discoverEventDependency
from hdl_toolkit.synthesizer.vectorUtils import getWidthExpr, fitTo
from operator import and_, or_
from hdl_toolkit.hdlObjects.operatorDefs import concatFn


def _intfToSig(obj):
    if isinstance(obj, InterfaceBase):
        return obj._sig
    else:
        return obj

class StmCntx(list):
    """
    Base class of statement contexts
    """
    pass

def flaten(iterables):
    if isinstance(iterables, (list, tuple, types.GeneratorType)):
        for i in iterables:
            yield from flaten(i)
    else:
        yield iterables
        
class If(StmCntx):
    """
    If statement generator
    
    @ivar nowIsEventDependent: flag if current scope of if is event dependent
    """
    def __init__(self, cond, *statements):
        """
        @param cond: condition in if
        @param statements: list of statements which should be active if condition is met   
        """
        self.cond = _intfToSig(cond)
        self.nowIsEventDependent = bool(list(discoverEventDependency(cond)))
        self.elifConds = []
        self._appendStatements(set([self.cond, ]), statements)
        
    def Else(self, *statements):
        ncond = set()
        ncond.add(~self.cond)
        
        for ec in self.elifConds:
            ncond.add(~ec)

        self._appendStatements(ncond, statements)
            
        # convert self to StmCntx to prevent any other else/elif
        stml = StmCntx()
        stml.extend(self)
        return stml
    
    def _appendStatements(self, condSet, statements):
        for stm in flaten(statements):
            stm.isEventDependent = stm.isEventDependent or self.nowIsEventDependent
            for c in condSet:
                c.endpoints.append(stm)
            stm.cond.update(condSet)
            self.append(stm)
    
    def Elif(self, cond, *statements):
        cond = _intfToSig(cond)
        self.nowIsEventDependent = self.nowIsEventDependent or bool(list(discoverEventDependency(cond)))
        thisCond = set()
        thisCond.add(~self.cond)
        for c in self.elifConds:
            thisCond.add(~c)
        thisCond.add(cond)
        
        self._appendStatements(thisCond, statements)
        
        self.elifConds.append(cond)
        
        return self

class Switch(StmCntx):
    """
    Switch statement generator
    """
    def __init__(self, switchOn):
        self.switchOn = switchOn
        self.cond = None
    
    _appendStatements = If._appendStatements
    
    def Case(self, caseVal, *statements):
        cond = self.switchOn._eq(caseVal)
        if self.cond is None:
            If.__init__(self, cond, *statements) 
        else:
            If.Elif(self, cond, *statements)
        return self
    
    def addCases(self, tupesValStmnts):
        s = self
        for val, statements in tupesValStmnts:
            if val is None:
                s = s.Default(*statements)
            else:
                s = s.Case(val, *statements)
        return s
    
    def Default(self, *statements):
        return If.Else(self, *statements)


def In(sigOrVal, iterable):
    """
    Hdl conversible in operator, check if any of items in "iterable" equals "sigOrVal"
    """
    res = None
    for i in iterable:
        i = toHVal(i)
        if res is None:
            res = sigOrVal._eq(i)
        else:
            res = res | sigOrVal._eq(i)

    return res

class FsmBuilder(StmCntx):
    """
    @ivar stateReg: register with state
    """
    
    def __init__(self, parent, stateT, stateRegName="st"):
        """
        @param parent: parent unit where fsm should be builded
        @param stateT: enum type of state
        @param stateRegName: name of register where sate is stored
        """
        self.stateReg = parent._reg(stateRegName, stateT, stateT.fromPy(stateT._allValues[0]))
        Switch.__init__(self, self.stateReg)
    
    _appendStatements = Switch._appendStatements
    def Trans(self, stateFrom, *condAndNextState):
        """
        @param stateFrom: apply when FSM is in this state
        @param condAndNextState: tupes (condition, newState),
                        last does not to have condition
        
        @attention: transitions has priority, first has the biggest 
        @attention: if stateFrom is None it is evaluated as default
        """
        top = None
        last = True<commit_msg>fsm state now do not have to be enum<commit_after>import types

from hdl_toolkit.hdlObjects.specialValues import DIRECTION
from hdl_toolkit.hdlObjects.typeShortcuts import hInt, vec
from hdl_toolkit.hdlObjects.types.defs import BIT
from hdl_toolkit.hdlObjects.types.typeCast import toHVal
from hdl_toolkit.synthesizer.interfaceLevel.interface.utils import walkPhysInterfaces
from hdl_toolkit.synthesizer.interfaceLevel.mainBases import InterfaceBase
from hdl_toolkit.synthesizer.rtlLevel.signalUtils.walkers import discoverEventDependency
from hdl_toolkit.synthesizer.vectorUtils import getWidthExpr, fitTo
from operator import and_, or_
from hdl_toolkit.hdlObjects.operatorDefs import concatFn
from hdl_toolkit.hdlObjects.types.enum import Enum


def _intfToSig(obj):
    if isinstance(obj, InterfaceBase):
        return obj._sig
    else:
        return obj

class StmCntx(list):
    """
    Base class of statement contexts
    """
    pass

def flaten(iterables):
    if isinstance(iterables, (list, tuple, types.GeneratorType)):
        for i in iterables:
            yield from flaten(i)
    else:
        yield iterables
        
class If(StmCntx):
    """
    If statement generator
    
    @ivar nowIsEventDependent: flag if current scope of if is event dependent
    """
    def __init__(self, cond, *statements):
        """
        @param cond: condition in if
        @param statements: list of statements which should be active if condition is met   
        """
        self.cond = _intfToSig(cond)
        self.nowIsEventDependent = bool(list(discoverEventDependency(cond)))
        self.elifConds = []
        self._appendStatements(set([self.cond, ]), statements)
        
    def Else(self, *statements):
        ncond = set()
        ncond.add(~self.cond)
        
        for ec in self.elifConds:
            ncond.add(~ec)

        self._appendStatements(ncond, statements)
            
        # convert self to StmCntx to prevent any other else/elif
        stml = StmCntx()
        stml.extend(self)
        return stml
    
    def _appendStatements(self, condSet, statements):
        for stm in flaten(statements):
            stm.isEventDependent = stm.isEventDependent or self.nowIsEventDependent
            for c in condSet:
                c.endpoints.append(stm)
            stm.cond.update(condSet)
            self.append(stm)
    
    def Elif(self, cond, *statements):
        cond = _intfToSig(cond)
        self.nowIsEventDependent = self.nowIsEventDependent or bool(list(discoverEventDependency(cond)))
        thisCond = set()
        thisCond.add(~self.cond)
        for c in self.elifConds:
            thisCond.add(~c)
        thisCond.add(cond)
        
        self._appendStatements(thisCond, statements)
        
        self.elifConds.append(cond)
        
        return self

class Switch(StmCntx):
    """
    Switch statement generator
    """
    def __init__(self, switchOn):
        self.switchOn = switchOn
        self.cond = None
    
    _appendStatements = If._appendStatements
    
    def Case(self, caseVal, *statements):
        cond = self.switchOn._eq(caseVal)
        if self.cond is None:
            If.__init__(self, cond, *statements) 
        else:
            If.Elif(self, cond, *statements)
        return self
    
    def addCases(self, tupesValStmnts):
        s = self
        for val, statements in tupesValStmnts:
            if val is None:
                s = s.Default(*statements)
            else:
                s = s.Case(val, *statements)
        return s
    
    def Default(self, *statements):
        return If.Else(self, *statements)


def In(sigOrVal, iterable):
    """
    Hdl conversible in operator, check if any of items in "iterable" equals "sigOrVal"
    """
    res = None
    for i in iterable:
        i = toHVal(i)
        if res is None:
            res = sigOrVal._eq(i)
        else:
            res = res | sigOrVal._eq(i)

    return res

class FsmBuilder(StmCntx):
    """
    @ivar stateReg: register with state
    """
    
    def __init__(self, parent, stateT, stateRegName="st"):
        """
        @param parent: parent unit where fsm should be builded
        @param stateT: enum type of state
        @param stateRegName: name of register where sate is stored
        """
        if isinstance(stateT, Enum):
            beginVal = stateT.fromPy(stateT._allValues[0])
        else:
            beginVal = 0
        
        self.stateReg = parent._reg(stateRegName, stateT, beginVal)
        Switch.__init__(self, self.stateReg)
    
    _appendStatements = Switch._appendStatements
    def Trans(self, stateFrom, *condAndNextState):
        """
        @param stateFrom: apply when FSM is in this state
        @param condAndNextState: tupes (condition, newState),
                        last does not to have condition
        
        @attention: transitions has priority, first has the biggest 
        @attention: if stateFrom is None it is evaluated as default
        """
        top = None
        last = True
{'fraction_non_alphanumeric': 0.08201191798808201, 'fraction_numerical': 0.000302999697000303, 'mean_word_length': 2.273388429752066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4493115', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2896, 'n_words': 783}