for function works with tuples now / refactoring

This commit is contained in:
David Halter
2012-04-25 00:01:15 +02:00
parent 1e7680ed1d
commit c78d05fa12
5 changed files with 53 additions and 61 deletions

View File

@@ -219,7 +219,8 @@ def get_names_for_scope(scope, star_search=True):
start_scope = scope start_scope = scope
while scope: while scope:
# class variables/functions are only availabe # class variables/functions are only availabe
if not isinstance(scope, parsing.Class) or scope == start_scope: if (not isinstance(scope, parsing.Class) or scope == start_scope) \
and not isinstance(scope, parsing.Flow):
compl += scope.get_defined_names() compl += scope.get_defined_names()
scope = scope.parent scope = scope.parent
@@ -249,8 +250,8 @@ def get_scopes_for_name(scope, name, search_global=False):
res_new = [] res_new = []
for r in result: for r in result:
if isinstance(r, parsing.Statement): if isinstance(r, parsing.Statement):
# global variables handling
if r.is_global(): if r.is_global():
res_new += []
for token_name in r.token_list[1:]: for token_name in r.token_list[1:]:
if isinstance(token_name, parsing.Name): if isinstance(token_name, parsing.Name):
res_new += get_scopes_for_name(r.parent, res_new += get_scopes_for_name(r.parent,
@@ -265,12 +266,9 @@ def get_scopes_for_name(scope, name, search_global=False):
def filter_name(scopes): def filter_name(scopes):
# the name is already given in the parent function # the name is already given in the parent function
def handle_non_arrays():
result = [] result = []
for scope in scopes:
if [name] == list(scope.names):
if isinstance(scope, ArrayElement):
result.append(scope)
else:
par = scope.parent par = scope.parent
if isinstance(par, parsing.Flow): if isinstance(par, parsing.Flow):
if par.command == 'for': if par.command == 'for':
@@ -278,9 +276,13 @@ def get_scopes_for_name(scope, name, search_global=False):
# one, remember `in`). And follow it. After that, # one, remember `in`). And follow it. After that,
# get the types which are in the array # get the types which are in the array
arrays = follow_statement(par.inits[0]) arrays = follow_statement(par.inits[0])
# TODO for loops can have tuples as set_vars
for array in arrays: for array in arrays:
result += array.get_index_types() for_vars = array.get_index_types()
if len(par.set_vars) > 1:
var_arr = par.set_stmt.get_assignment_calls()
result += assign_tuples(var_arr, for_vars, name)
else:
result += for_vars
else: else:
debug.warning('Why are you here? %s' % par.command) debug.warning('Why are you here? %s' % par.command)
elif isinstance(par, parsing.Param) \ elif isinstance(par, parsing.Param) \
@@ -291,6 +293,15 @@ def get_scopes_for_name(scope, name, search_global=False):
result.append(par) result.append(par)
else: else:
result.append(par) result.append(par)
return result
result = []
for scope in scopes:
if [name] == list(scope.names):
if isinstance(scope, ArrayElement):
result.append(scope)
else:
result += handle_non_arrays()
debug.dbg('sfn filter', result) debug.dbg('sfn filter', result)
return result return result
@@ -319,6 +330,7 @@ def strip_imports(scopes):
result.append(s) result.append(s)
return result return result
def assign_tuples(tup, results, seek_name): def assign_tuples(tup, results, seek_name):
""" """
This is a normal assignment checker. In python functions and other things This is a normal assignment checker. In python functions and other things
@@ -359,6 +371,7 @@ def assign_tuples(tup, results, seek_name):
result += eval_results(i) result += eval_results(i)
return result return result
@memoize(default=[]) @memoize(default=[])
def follow_statement(stmt, scope=None, seek_name=None): def follow_statement(stmt, scope=None, seek_name=None):
""" """

View File

@@ -2,7 +2,7 @@
import functions import functions
functions.debug.debug_function = functions.debug.print_to_stdout #functions.debug.debug_function = functions.debug.print_to_stdout
functions.debug.ignored_modules = ['parsing', 'builtin'] functions.debug.ignored_modules = ['parsing', 'builtin']
#functions.debug.ignored_modules = ['parsing', 'builtin', 'evaluate', 'modules'] #functions.debug.ignored_modules = ['parsing', 'builtin', 'evaluate', 'modules']
functions.modules.module_find_path.insert(0, '.') functions.modules.module_find_path.insert(0, '.')

View File

@@ -154,7 +154,7 @@ def globalfunc():
global globalvar, globalvar2 global globalvar, globalvar2
globalvar = 3 globalvar = 3
for abcde, efgh in [(1,"")]:
abcde.real
abcde.
globalvar.

View File

@@ -431,6 +431,14 @@ class Flow(Scope):
next.parent = self.parent next.parent = self.parent
return next return next
class ForFlow(Flow):
"""
Used for the for loop, because there are two statement parts.
"""
def __init__(self, command, inits, indent, line_nr, set_stmt):
super(ForFlow, self).__init__(command, inits, indent, line_nr,
set_stmt.used_vars)
self.set_stmt = set_stmt
class Import(Simple): class Import(Simple):
""" """
@@ -942,35 +950,6 @@ class PyFuzzyParser(object):
names.append(tok) names.append(tok)
return (names, token_type, tok, start_indent, start_line) return (names, token_type, tok, start_indent, start_line)
def _parse_value_list(self, pre_used_token=None):
"""
A value list is a comma separated list. This is used for:
>>> for a,b,self.c in enumerate(test)
TODO there may be multiple "sub" value lists e.g. (a,(b,c)).
"""
value_list = []
if pre_used_token:
token_type, tok, indent = pre_used_token
n, token_type, tok, start_indent, start_line = \
self._parsedotname(tok)
if n:
temp = Name(n, start_indent, start_line, self.line_nr)
value_list.append()
token_type, tok, indent = self.next()
while tok != 'in' and token_type != tokenize.NEWLINE:
n, token_type, tok, start_indent, start_line = \
self._parsedotname(self.current)
if n:
temp = Name(n, start_indent, start_line, self.line_nr)
value_list.append(temp)
if tok == 'in':
break
token_type, tok, indent = self.next()
return (value_list, tok)
def _parseimportlist(self): def _parseimportlist(self):
""" """
The parser for the imports. Unlike the class and function parse The parser for the imports. Unlike the class and function parse
@@ -1329,12 +1308,12 @@ class PyFuzzyParser(object):
self.freshscope = False self.freshscope = False
#loops #loops
elif tok == 'for': elif tok == 'for':
value_list, tok = self._parse_value_list() set_stmt, tok = self._parse_statement(added_breaks=['in'])
if tok == 'in': if tok == 'in':
statement, tok = self._parse_statement() statement, tok = self._parse_statement()
if tok == ':': if tok == ':':
f = Flow('for', [statement], indent, f = ForFlow('for', [statement], indent,
self.line_nr, value_list) self.line_nr, set_stmt)
debug.dbg("new scope: flow for@%s" % (f.line_nr)) debug.dbg("new scope: flow for@%s" % (f.line_nr))
self.scope = self.scope.add_statement(f) self.scope = self.scope.add_statement(f)

View File

@@ -12,7 +12,7 @@ for a1 in 1,"":
#? ['upper'] #? ['upper']
a1.upper a1.upper
for a3, b3 in (1,""), (1,""): for a3, b3 in (1,""), (1,""), (1,""):
#? ['real'] #? ['real']
a3.real a3.real
#? [] #? []