mirror of
https://github.com/myhdl/myhdl.git
synced 2025-01-24 21:52:56 +08:00
Added first take at enum type
This commit is contained in:
parent
0814811f42
commit
bc799276bd
@ -57,5 +57,6 @@ from util import downrange, Error, StopSimulation
|
|||||||
from bin import bin
|
from bin import bin
|
||||||
from misc import instances, processes
|
from misc import instances, processes
|
||||||
from always_comb import always_comb
|
from always_comb import always_comb
|
||||||
|
from enum import enum
|
||||||
from trace_sigs import trace_sigs
|
from trace_sigs import trace_sigs
|
||||||
|
|
||||||
|
48
myhdl/enum.py
Normal file
48
myhdl/enum.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
from types import StringType
|
||||||
|
|
||||||
|
|
||||||
|
def enum(*args):
|
||||||
|
|
||||||
|
args = list(args)
|
||||||
|
# only default encoding for now
|
||||||
|
argdict = {}
|
||||||
|
encoding = {}
|
||||||
|
i = 0
|
||||||
|
for arg in args:
|
||||||
|
if type(arg) is not StringType:
|
||||||
|
raise TypeError
|
||||||
|
if argdict.has_key(arg):
|
||||||
|
raise ValueError
|
||||||
|
argdict[i] = arg
|
||||||
|
encoding[arg] = i
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
class EnumItem(object):
|
||||||
|
def __init__(self, arg):
|
||||||
|
self._val = encoding[arg]
|
||||||
|
def __repr__(self):
|
||||||
|
return argdict[self._val]
|
||||||
|
__str__ = __repr__
|
||||||
|
|
||||||
|
class Enum(object):
|
||||||
|
def __init__(self):
|
||||||
|
for slot in args:
|
||||||
|
self.__dict__[slot] = EnumItem(slot)
|
||||||
|
def __setattr__(self, attr, val):
|
||||||
|
raise AttributeError
|
||||||
|
def __len__(self):
|
||||||
|
return len(args)
|
||||||
|
def __repr__(self):
|
||||||
|
s = ""
|
||||||
|
for arg in args:
|
||||||
|
s += "%s=%d, " % (arg, encoding[arg])
|
||||||
|
return "<Enum: %s>" % s[:-2]
|
||||||
|
__str__ = __repr__
|
||||||
|
|
||||||
|
return Enum()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user