array indexing works now also with variables

This commit is contained in:
David Halter
2012-08-06 17:47:43 +02:00
parent 8780199a33
commit c154bdad0e
5 changed files with 56 additions and 10 deletions

View File

@@ -727,17 +727,21 @@ class Array(object):
def __init__(self, array):
self._array = array
def get_index_types(self, index=None):
values = self._array.values
if index is not None:
if [x for x in index if ':' in x]:
def get_index_types(self, index_call_list=None):
""" Tries to get the only element (key) of a TODO doc"""
# array slicing
if index_call_list is not None:
if index_call_list and [x for x in index_call_list if ':' in x]:
return [self]
else:
index_possibilities = list(follow_call_list(index_call_list))
if len(index_possibilities) == 1:
# This is indexing only one element, with a fixed index number,
# otherwise it just ignores the index (e.g. [1+1]).
try:
# Multiple elements in the array.
i = index.get_only_subelement().name
# Multiple elements in the array are not wanted. var_args
# and get_only_subelement can raise AttributeErrors.
i = index_possibilities[0].var_args.get_only_subelement()
except AttributeError:
pass
else:
@@ -745,9 +749,10 @@ class Array(object):
return self.get_exact_index_types(i)
except (IndexError, KeyError):
pass
return self.follow_values(values)
return self.follow_values(self._array.values)
def get_exact_index_types(self, index):
""" Here the index is an int. Raises IndexError/KeyError """
if self._array.type == parsing.Array.DICT:
old_index = index
index = None
@@ -1155,7 +1160,8 @@ def follow_call(call):
debug.warning('unknown type:', current.type, current)
scopes = []
# Make instances of those number/string objects.
scopes = [Instance(s) for s in scopes]
arr = helpers.generate_param_array([current.name])
scopes = [Instance(s, arr) for s in scopes]
else:
# This is the first global lookup.
scopes = get_scopes_for_name(scope, current, position=position,

View File

@@ -77,3 +77,10 @@ def fast_parent_copy(obj):
copied_list[i] = list_rec(el)
return copied_list
return recursion(obj)
def generate_param_array(args_tuple, parent_stmt=None):
""" This generates an array, that can be used as a param """
values = []
for arg in args_tuple:
values.append([arg])
return parsing.Array(parsing.Array.TUPLE, parent_stmt, values=values)

View File

@@ -99,3 +99,12 @@ def iter(collection, sentinel=None):
yield collection()
else:
yield next(collection)
# basic types
class int():
def __init__(self, x, base=None):
self.x = x
class str():
def __init__(self, obj):
self.obj = obj

View File

@@ -830,7 +830,8 @@ class Array(Call):
DICT = 'dict'
SET = 'set'
def __init__(self, arr_type, parent_stmt=None, parent=None, values=None):
def __init__(self, arr_type=NOARRAY, parent_stmt=None, parent=None,
values=None):
super(Array, self).__init__(None, arr_type, parent_stmt, parent)
self.values = values if values else []

View File

@@ -139,6 +139,29 @@ dic2[r'asdf']
#? int() str()
dic2['just_something']
# -----------------
# with variable as index
# -----------------
a = (1, "")
index = 1
#? str()
a[index]
# these should just ouput the whole array
index = int
#? int() str()
a[index]
index = int()
#? int() str()
a[index]
# dicts
index = 'asdf'
dic2 = {'asdf': 3, 'b': 'str'}
#? int()
dic2[index]
# -----------------
# __getitem__
# -----------------