Start fixing issues with for loops and += operations.

This commit is contained in:
Dave Halter
2014-11-12 14:54:52 +01:00
parent 13c2279fea
commit 408eee50dd
4 changed files with 27 additions and 16 deletions

View File

@@ -136,26 +136,24 @@ class Evaluator(object):
if seek_name:
types = finder.check_tuple_assignments(types, seek_name)
#ass_details = stmt.assignment_details
if False and stmt.assignment_details and ass_details[0][1] != '=' and not isinstance(stmt, er.InstanceElement): # TODO don't check for this.
expr_list, _operator = ass_details[0]
ass_details = stmt.assignment_details
first_operation = stmt.first_operation()
if first_operation not in ('=', None) and not isinstance(stmt, er.InstanceElement): # TODO don't check for this.
# `=` is always the last character in aug assignments -> -1
operator = copy.copy(_operator)
operator.string = operator.string[:-1]
name = str(expr_list[0].name)
parent = stmt.parent.get_parent_until(pr.Flow, reverse=True)
if isinstance(parent, (pr.SubModule, fast.Module)):
parent = er.ModuleWrapper(self, parent)
operator = copy.copy(first_operation)
operator.value = operator.value[:-1]
name = str(stmt.get_defined_names()[0])
parent = er.wrap(self, stmt.get_parent_scope())
left = self.find_types(parent, name, stmt.start_pos)
if isinstance(stmt.parent, pr.ForFlow):
# iterate through result and add the values, that's possible
# only in for loops without clutter, because they are
# predictable.
for r in result:
for r in types:
left = precedence.calculate(self, left, operator, [r])
result = left
types = left
else:
result = precedence.calculate(self, left, operator, result)
types = precedence.calculate(self, left, operator, types)
elif False and len(stmt.get_defined_names()) > 1 and seek_name and ass_details:
# Assignment checking is only important if the statement defines
# multiple variables.

View File

@@ -82,10 +82,14 @@ class NameFinder(object):
# Only the names defined in the last position are valid definitions.
last_names = []
for name in reversed(sorted(names, key=lambda name: name.start_pos)):
if isinstance(self.name_str, pr.Name):
origin_scope = self.name_str.get_definition().parent
else:
origin_scope = None
check = flow_analysis.break_check(self._evaluator,
scope,
name.get_definition(),
self.name_str.get_definition().parent)
origin_scope)
if check is not flow_analysis.UNREACHABLE:
last_names.append(name)
if check is flow_analysis.REACHABLE:

View File

@@ -330,10 +330,10 @@ class FakeDict(Array):
class MergedArray(Array):
def __init__(self, evaluator, arrays):
super(MergedArray, self).__init__(evaluator, arrays[-1]._array)
super(MergedArray, self).__init__(evaluator, arrays[-1]._atom)
self._arrays = arrays
def get_index_types(self, mixed_index):
def get_index_types(self, evaluator, mixed_index):
return list(chain(*(a.values() for a in self._arrays)))
def get_exact_index_types(self, mixed_index):

View File

@@ -1204,7 +1204,7 @@ class Statement(Simple, DocstringMixin):
def get_defined_names(self):
return list(chain.from_iterable(_defined_names(self.children[i])
for i in range(0, len(self.children) - 2, 2)
if self.children[i + 1].value == '='))
if '=' in self.children[i + 1].value))
"""Get the names for the statement."""
@@ -1266,6 +1266,15 @@ class Statement(Simple, DocstringMixin):
as_name.start_pos, as_name.end_pos, self))
return dct
def first_operation(self):
"""
Returns `+=`, `=`, etc or None if there is no operation.
"""
try:
return self.children[1]
except IndexError:
return None
@property
def assignment_details(self):
"""