Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jandecaluwe committed Jan 30, 2016
1 parent 333928b commit 710821d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
15 changes: 4 additions & 11 deletions myhdl/_always.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,10 @@ def __init__(self, func, senslist):
self.func = func
self.senslist = tuple(senslist)
super(_Always, self).__init__(self.genfunc)
varnames = func.__code__.co_varnames
symdict = {}
for n, v in func.__globals__.items():
if n not in varnames:
symdict[n] = v
# handle free variables
freevars = func.__code__.co_freevars
if freevars:
closure = (c.cell_contents for c in func.__closure__)
symdict.update(zip(freevars, closure))
self.symdict = symdict

@property
def funcobj(self):
return self.func

def _waiter(self):
# infer appropriate waiter class
Expand Down
31 changes: 24 additions & 7 deletions myhdl/_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,45 @@ class _error:
_error.ArgType = "decorated object should be a generator function"


def instance(genFunc):
if not isinstance(genFunc, FunctionType):
def instance(genfunc):
if not isinstance(genfunc, FunctionType):
raise InstanceError(_error.ArgType)
if not _isGenFunc(genFunc):
if not _isGenFunc(genfunc):
raise InstanceError(_error.ArgType)
if genFunc.__code__.co_argcount > 0:
if genfunc.__code__.co_argcount > 0:
raise InstanceError(_error.NrOfArgs)
return _Instantiator(genFunc)
return _Instantiator(genfunc)

class _Instantiator(object):

def __init__(self, genfunc):
self.genfunc = genfunc
self.gen = genfunc()
# infer symdict
f = self.funcobj
varnames = f.__code__.co_varnames
symdict = {}
for n, v in f.__globals__.items():
if n not in varnames:
symdict[n] = v
# handle free variables
freevars = f.__code__.co_freevars
if freevars:
closure = (c.cell_contents for c in f.__closure__)
symdict.update(zip(freevars, closure))
self.symdict = symdict

@property
def funcobj(self):
return self.genfunc

@property
def waiter(self):
return self._waiter()(self.gen)

def _waiter(self):
return _inferWaiter

@property
def ast(self):
return _makeAST(self.gen.gi_frame)

0 comments on commit 710821d

Please sign in to comment.