generators are now working with arguments (yield stmts)

This commit is contained in:
David Halter
2012-07-10 13:36:36 +02:00
parent 37f2b8ff56
commit e4739fddf9
3 changed files with 29 additions and 13 deletions

View File

@@ -211,6 +211,9 @@ class Instance(Executable):
return res return res
def __getattr__(self, name): def __getattr__(self, name):
if name == 'get_index_types':
# todo call __getitem__ in such cases?
return lambda: []
if name not in ['line_nr', 'indent', 'name', 'get_imports']: if name not in ['line_nr', 'indent', 'name', 'get_imports']:
raise AttributeError("Instance %s: Don't touch this (%s)!" raise AttributeError("Instance %s: Don't touch this (%s)!"
% (self, name)) % (self, name))
@@ -387,7 +390,8 @@ class Execution(Executable):
# there maybe executions of executions # there maybe executions of executions
stmts = [Instance(self.base, self.var_args)] stmts = [Instance(self.base, self.var_args)]
elif isinstance(self.base, Generator): elif isinstance(self.base, Generator):
return Execution(self.base.func).get_return_types(True) print 'blubedi', self.base
return self.base.execute()
else: else:
# 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.
@@ -415,7 +419,7 @@ class Execution(Executable):
def _get_function_returns(self, evaluate_generator): def _get_function_returns(self, evaluate_generator):
func = self.base func = self.base
if func.is_generator and not evaluate_generator: if func.is_generator and not evaluate_generator:
return [Generator(func)] return [Generator(func, self.var_args)]
else: else:
stmts = [] stmts = []
for r in self.returns: for r in self.returns:
@@ -638,10 +642,10 @@ class Execution(Executable):
class Generator(object): class Generator(object):
# TODO bring next(iter, default) to work - default works not def __init__(self, func, var_args):
def __init__(self, func):
super(Generator, self).__init__() super(Generator, self).__init__()
self.func = func self.func = func
self.var_args = var_args
def get_defined_names(self): def get_defined_names(self):
""" """
@@ -662,15 +666,17 @@ class Generator(object):
debug.dbg('generator names', names) debug.dbg('generator names', names)
return names return names
def execute(self):
return Execution(self.func, self.var_args).get_return_types(True)
@property @property
def parent(self): def parent(self):
return self.func.parent return self.func.parent
#self.execution.get_return_types()
def get_index_types(self, index=None): def get_index_types(self, index=None):
# TODO check if this method is right here, this means that Generators # TODO check if this method is right here, this means that Generators
# can be indexed, which is not the Python way. # can be indexed, which is not the Python way.
return Execution(self.func).get_return_types(True) return self.execute()
def __repr__(self): def __repr__(self):
return "<%s of %s>" % (self.__class__.__name__, self.func) return "<%s of %s>" % (self.__class__.__name__, self.func)
@@ -866,6 +872,7 @@ def get_scopes_for_name(scope, name_str, position=None, search_global=False):
def handle_non_arrays(name): def handle_non_arrays(name):
result = [] result = []
par = name.parent par = name.parent
print name, par, par.parent
if isinstance(par, parsing.Flow): if isinstance(par, parsing.Flow):
if par.command == 'for': if par.command == 'for':
# take the first statement (for has always only # take the first statement (for has always only

View File

@@ -7,18 +7,23 @@ def gen():
yield "" yield ""
gen_exe = gen() gen_exe = gen()
#? ['upper']
next(gen_exe).upper
#? ['real']
next(gen_exe).real
#? int() str() #? int() str()
next(gen_exe) next(gen_exe)
#? int() str() list #? int() str() list
next(gen_exe, list) next(gen_exe, list)
def gen_ret(value):
yield value
#? int()
next(gen_ret(1))
#? []
next(gen_ret())
# ----------------- # -----------------
# generators should be indexable? # generators should be indexable!???
# ----------------- # -----------------
def get(self): def get(self):
yield 1 yield 1

View File

@@ -17,5 +17,9 @@ scopes, path, dot, like = \
#? set() #? set()
el = scopes. el = scopes.
#? str() <--- recursion # get_names_for_scope is also recursion stuff
el = evaluate.get_names_for_scope()[0]. #? tuple()
el = evaluate.get_names_for_scope()[0]
#? tuple()
el = evaluate.get_names_for_scope(1,2)[0][0]