generators work again (deleted some necessary stuff)

This commit is contained in:
David Halter
2012-05-22 01:46:08 +02:00
parent 547c952ba1
commit da7ef3ba4b
4 changed files with 25 additions and 16 deletions

View File

@@ -284,7 +284,6 @@ class Execution(Executable):
elif isinstance(self.base, Generator): elif isinstance(self.base, Generator):
return Execution(self.base.func).get_return_types(True) return Execution(self.base.func).get_return_types(True)
else: else:
# set the callback function to get the var_args
# don't do this with exceptions, as usual, because some deeper # don't do this with exceptions, as usual, because some deeper
# exceptions could be catched - and I wouldn't know what happened. # exceptions could be catched - and I wouldn't know what happened.
if hasattr(self.base, 'returns'): if hasattr(self.base, 'returns'):
@@ -315,13 +314,16 @@ class Execution(Executable):
which act the same way as normal functions. which act the same way as normal functions.
""" """
def gen_param_name_copy(param, keys=[], values=[], array_type=None): def gen_param_name_copy(param, keys=[], values=[], array_type=None):
""" Create a param with self as parent. """ """
calls = parsing.Array(parsing.Array.NOARRAY, parent_stmt=self) Create a param with the original scope (of varargs) as parent.
"""
calls = parsing.Array(parsing.Array.NOARRAY,
self.var_args.parent_stmt)
calls.values = values calls.values = values
calls.keys = keys calls.keys = keys
calls.type = array_type calls.type = array_type
new_param = copy.copy(param) new_param = copy.copy(param)
new_param.parent = self new_param.parent = self.var_args.parent_stmt
new_param._assignment_calls_calculated = True new_param._assignment_calls_calculated = True
new_param._assignment_calls = calls new_param._assignment_calls = calls
name = copy.copy(param.get_name()) name = copy.copy(param.get_name())
@@ -513,14 +515,15 @@ class Execution(Executable):
class Generator(object): class Generator(object):
# TODO bring next(iter, default) to work - default works not
def __init__(self, func): def __init__(self, func):
super(Generator, self).__init__() super(Generator, self).__init__()
self.func = func self.func = func
def get_defined_names(self): def get_defined_names(self):
""" """
Returns a list of GeneratorObject, which can return the content of a Returns a list of names that define a generator, which can return the
generator content of a generator.
""" """
names = [] names = []
for n in ['__next__', 'send']: for n in ['__next__', 'send']:
@@ -533,6 +536,7 @@ class Generator(object):
name = parsing.Name([n], 0, 0, 0) name = parsing.Name([n], 0, 0, 0)
name.parent = None name.parent = None
names.append(name) names.append(name)
debug.dbg('generator names', names)
return names return names
@property @property

View File

@@ -1,5 +1,5 @@
def next(iterator, default=1): def next(iterator, default=None):
if hasattr("next"): if hasattr("next"):
return iterator.next() return iterator.next()
else: else:

View File

@@ -332,14 +332,6 @@ class Function(Scope):
def get_set_vars(self): def get_set_vars(self):
n = super(Function, self).get_set_vars() n = super(Function, self).get_set_vars()
"""
if self.param_cb:
# This is the really ugly part, where the functional style of this
# get methods is broken, it executes a callback.
# This is important, because something has to inject the params
# into the functions, with the right values.
n += self.param_cb()
"""
for p in self.params: for p in self.params:
try: try:
n.append(p.get_name()) n.append(p.get_name())

View File

@@ -63,7 +63,7 @@ def recursion(a, b):
else: else:
return recursion(a+".", b+1) return recursion(a+".", b+1)
##? int() float() #? int() float()
recursion("a", 1.0) recursion("a", 1.0)
# ----------------- # -----------------
@@ -82,6 +82,16 @@ exe[0].index
#? ['append'] #? ['append']
exe[1].append exe[1].append
# -----------------
# default arguments
# -----------------
##? int() str()
func()
##? float() str()
func(1.0)
# ----------------- # -----------------
# closures # closures
# ----------------- # -----------------
@@ -255,3 +265,6 @@ next(gen_exe).upper
next(gen_exe).real next(gen_exe).real
#? int() str() #? int() str()
next(gen_exe) next(gen_exe)
#? int() str() list
next(gen_exe, list)