1.1 --- a/lib/__builtins__/list.py Wed Nov 23 17:23:52 2016 +0100
1.2 +++ b/lib/__builtins__/list.py Wed Nov 23 17:32:58 2016 +0100
1.3 @@ -34,13 +34,12 @@
1.4 # Reserve an attribute for a fragment reference along with some space
1.5 # for elements.
1.6
1.7 - self.__data__ = native._list_init(len(args))
1.8 + self.__data__ = native._list_init(args is not None and len(args) or 0)
1.9
1.10 if args is not None:
1.11 self.extend(args)
1.12
1.13 def __contains__(self, value): pass
1.14 - def __setitem__(self, index, value): pass
1.15 def __delitem__(self, index): pass
1.16 def __setslice__(self, start, end, slice): pass
1.17 def __delslice__(self, start, end): pass
1.18 @@ -84,26 +83,9 @@
1.19
1.20 def __str__(self):
1.21
1.22 - "Return a string representation of the list."
1.23 -
1.24 - b = buffer()
1.25 - i = 0
1.26 - l = self.__len__()
1.27 - first = True
1.28 -
1.29 - # NOTE: Should really show quoted forms of the items.
1.30 + "Return a string representation."
1.31
1.32 - b.append("[")
1.33 - while i < l:
1.34 - if first:
1.35 - first = False
1.36 - else:
1.37 - b.append(", ")
1.38 - b.append(repr(self.__get_single_item__(i)))
1.39 - i += 1
1.40 - b.append("]")
1.41 -
1.42 - return str(b)
1.43 + return self._str("[", "]")
1.44
1.45 __repr__ = __str__
1.46
1.47 @@ -130,4 +112,13 @@
1.48
1.49 return native._list_element(self, index)
1.50
1.51 + def __set_single_item__(self, index, value):
1.52 +
1.53 + "Set at the normalised (positive) 'index' the given 'value'."
1.54 +
1.55 + if index >= len(self):
1.56 + raise IndexError(index)
1.57 +
1.58 + return native._list_setelement(self, index, value)
1.59 +
1.60 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/lib/__builtins__/sequence.py Wed Nov 23 17:23:52 2016 +0100
2.2 +++ b/lib/__builtins__/sequence.py Wed Nov 23 17:32:58 2016 +0100
2.3 @@ -25,6 +25,27 @@
2.4
2.5 "A common base class for sequence types."
2.6
2.7 + def _str(self, opening, closing):
2.8 +
2.9 + "Serialise this object with the given 'opening' and 'closing' strings."
2.10 +
2.11 + b = buffer()
2.12 + i = 0
2.13 + l = self.__len__()
2.14 + first = True
2.15 +
2.16 + b.append(opening)
2.17 + while i < l:
2.18 + if first:
2.19 + first = False
2.20 + else:
2.21 + b.append(", ")
2.22 + b.append(repr(self.__get_single_item__(i)))
2.23 + i += 1
2.24 + b.append(closing)
2.25 +
2.26 + return str(b)
2.27 +
2.28 def __getitem__(self, index):
2.29
2.30 "Return the item or slice specified by 'index'."
2.31 @@ -46,6 +67,27 @@
2.32 else:
2.33 raise TypeError
2.34
2.35 + def __setitem__(self, index, value):
2.36 +
2.37 + "Set at 'index' the given 'value'."
2.38 +
2.39 + # Normalise any integer indexes, converting negative indexes to positive
2.40 + # ones.
2.41 +
2.42 + if _isinstance(index, int):
2.43 + index = _normalise_index(index, self.__len__())
2.44 + return self.__set_single_item__(index, value)
2.45 +
2.46 + # Handle slices separately.
2.47 +
2.48 + elif _isinstance(index, slice):
2.49 + return self.__setslice__(index.start, index.end, value)
2.50 +
2.51 + # No other kinds of objects are supported as indexes.
2.52 +
2.53 + else:
2.54 + raise TypeError
2.55 +
2.56 def __getslice__(self, start, end=None):
2.57
2.58 "Return a slice starting from 'start', with the optional 'end'."
3.1 --- a/lib/__builtins__/str.py Wed Nov 23 17:23:52 2016 +0100
3.2 +++ b/lib/__builtins__/str.py Wed Nov 23 17:32:58 2016 +0100
3.3 @@ -79,7 +79,7 @@
3.4 return self
3.5
3.6 def __repr__(self):
3.7 - "Return a string literal representation."
3.8 + "Return a program representation."
3.9 # NOTE: To be implemented with proper quoting.
3.10 b = buffer(['"', self, '"'])
3.11 return str(b)
4.1 --- a/lib/__builtins__/tuple.py Wed Nov 23 17:23:52 2016 +0100
4.2 +++ b/lib/__builtins__/tuple.py Wed Nov 23 17:32:58 2016 +0100
4.3 @@ -31,23 +31,40 @@
4.4
4.5 "Initialise the tuple."
4.6
4.7 - self.__data__ = native._tuple_init(args, len(args)) # allocate and copy elements
4.8 + # Reserve an attribute for a fragment reference along with some space
4.9 + # for elements.
4.10 +
4.11 + size = args is not None and len(args) or 0
4.12 + self.__data__ = native._list_init(size)
4.13 + native._list_setsize(self, size)
4.14 +
4.15 + # Populate the tuple.
4.16 +
4.17 + if args is not None:
4.18 + i = 0
4.19 + for arg in args:
4.20 + native._list_setelement(self, i, arg)
4.21 + i += 1
4.22
4.23 def __getslice__(self, start, end=None):
4.24
4.25 "Return a slice starting from 'start', with the optional 'end'."
4.26
4.27 - return native._list_to_tuple(get_using(sequence.__getslice__, self)(start, end))
4.28 + return tuple(get_using(sequence.__getslice__, self)(start, end))
4.29
4.30 def __len__(self):
4.31
4.32 "Return the length of the tuple."
4.33
4.34 - return native._tuple_len(self)
4.35 + return native._list_len(self)
4.36
4.37 def __add__(self, other): pass
4.38
4.39 - def __str__(self): pass
4.40 + def __str__(self):
4.41 +
4.42 + "Return a string representation."
4.43 +
4.44 + return self._str("(", ")")
4.45
4.46 __repr__ = __str__
4.47
4.48 @@ -66,6 +83,18 @@
4.49 # Special implementation methods.
4.50
4.51 def __get_single_item__(self, index):
4.52 - return native._tuple_element(self, index)
4.53 +
4.54 + "Return the item at the normalised (positive) 'index'."
4.55 +
4.56 + if index >= len(self):
4.57 + raise IndexError(index)
4.58 +
4.59 + return native._list_element(self, index)
4.60 +
4.61 + def __set_single_item__(self, index, value):
4.62 +
4.63 + "Set at the normalised (positive) 'index' the given 'value'."
4.64 +
4.65 + raise TypeError(self)
4.66
4.67 # vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/lib/native.py Wed Nov 23 17:23:52 2016 +0100
5.2 +++ b/lib/native.py Wed Nov 23 17:32:58 2016 +0100
5.3 @@ -53,20 +53,16 @@
5.4 def _str_nonempty(self): pass
5.5
5.6 def _list_init(size): pass
5.7 +def _list_setsize(self, size): pass
5.8 def _list_append(self, value): pass
5.9 def _list_concat(self, other): pass
5.10 def _list_len(self): pass
5.11 def _list_nonempty(self): pass
5.12 def _list_element(self, index): pass
5.13 +def _list_setelement(self, index, value): pass
5.14
5.15 def _buffer_str(self): pass
5.16
5.17 -def _list_to_tuple(l): pass
5.18 -
5.19 -def _tuple_init(args, size): pass
5.20 -def _tuple_len(self): pass
5.21 -def _tuple_element(self, index): pass
5.22 -
5.23 def _isinstance(obj, cls): pass
5.24
5.25 def _read(fd, n): pass
6.1 --- a/templates/native.c Wed Nov 23 17:23:52 2016 +0100
6.2 +++ b/templates/native.c Wed Nov 23 17:32:58 2016 +0100
6.3 @@ -349,6 +349,19 @@
6.4 return attr;
6.5 }
6.6
6.7 +__attr __fn_native__list_setsize(__attr __args[])
6.8 +{
6.9 + __attr * const self = &__args[1];
6.10 + __attr * const size = &__args[2];
6.11 + /* self.__data__ interpreted as list */
6.12 + __fragment *data = __load_via_object(self->value, __pos___data__).data;
6.13 + /* size.__data__ interpreted as int */
6.14 + unsigned int n = __load_via_object(size->value, __pos___data__).intvalue;
6.15 +
6.16 + data->size = n;
6.17 + return __builtins___none_None;
6.18 +}
6.19 +
6.20 __attr __fn_native__list_append(__attr __args[])
6.21 {
6.22 __attr * const self = &__args[1];
6.23 @@ -438,11 +451,18 @@
6.24 return elements[i];
6.25 }
6.26
6.27 -__attr __fn_native__list_to_tuple(__attr __args[])
6.28 +__attr __fn_native__list_setelement(__attr __args[])
6.29 {
6.30 - __attr * const l = &__args[1];
6.31 + __attr * const self = &__args[1];
6.32 + __attr * const index = &__args[2];
6.33 + __attr * const value = &__args[3];
6.34 + /* self.__data__ interpreted as fragment */
6.35 + __attr *elements = __load_via_object(self->value, __pos___data__).data->attrs;
6.36 + /* index.__data__ interpreted as int */
6.37 + int i = __load_via_object(index->value, __pos___data__).intvalue;
6.38
6.39 - /* NOTE: To be written. */
6.40 + /* Set the element. */
6.41 + elements[i] = *value;
6.42 return __builtins___none_None;
6.43 }
6.44
6.45 @@ -474,38 +494,6 @@
6.46 return __new_str(s);
6.47 }
6.48
6.49 -__attr __fn_native__tuple_init(__attr __args[])
6.50 -{
6.51 - __attr * const size = &__args[1];
6.52 - /* size.__data__ interpreted as fragment */
6.53 - __fragment *data = calloc(__load_via_object(size->value, __pos___data__).intvalue, sizeof(__attr));
6.54 - __attr attr = {0, .data=data};
6.55 -
6.56 - return attr;
6.57 -}
6.58 -
6.59 -__attr __fn_native__tuple_len(__attr __args[])
6.60 -{
6.61 - __attr * const self = &__args[1];
6.62 - /* self.__data__ interpreted as fragment */
6.63 - unsigned int size = __load_via_object(self->value, __pos___data__).data->size;
6.64 -
6.65 - /* Return the new integer. */
6.66 - return __new_int(size);
6.67 -}
6.68 -
6.69 -__attr __fn_native__tuple_element(__attr __args[])
6.70 -{
6.71 - __attr * const self = &__args[1];
6.72 - __attr * const index = &__args[2];
6.73 - /* self.__data__ interpreted as fragment */
6.74 - __attr *elements = __load_via_object(self->value, __pos___data__).data->attrs;
6.75 - /* index.__data__ interpreted as int */
6.76 - int i = __load_via_object(index->value, __pos___data__).intvalue;
6.77 -
6.78 - return elements[i];
6.79 -}
6.80 -
6.81 __attr __fn_native__isinstance(__attr __args[])
6.82 {
6.83 __attr * const obj = &__args[1];
7.1 --- a/templates/native.h Wed Nov 23 17:23:52 2016 +0100
7.2 +++ b/templates/native.h Wed Nov 23 17:32:58 2016 +0100
7.3 @@ -42,19 +42,16 @@
7.4 __attr __fn_native__str_nonempty(__attr __args[]);
7.5
7.6 __attr __fn_native__list_init(__attr __args[]);
7.7 +__attr __fn_native__list_setsize(__attr __args[]);
7.8 __attr __fn_native__list_append(__attr __args[]);
7.9 __attr __fn_native__list_concat(__attr __args[]);
7.10 __attr __fn_native__list_len(__attr __args[]);
7.11 __attr __fn_native__list_nonempty(__attr __args[]);
7.12 __attr __fn_native__list_element(__attr __args[]);
7.13 -__attr __fn_native__list_to_tuple(__attr __args[]);
7.14 +__attr __fn_native__list_setelement(__attr __args[]);
7.15
7.16 __attr __fn_native__buffer_str(__attr __args[]);
7.17
7.18 -__attr __fn_native__tuple_init(__attr __args[]);
7.19 -__attr __fn_native__tuple_len(__attr __args[]);
7.20 -__attr __fn_native__tuple_element(__attr __args[]);
7.21 -
7.22 __attr __fn_native__isinstance(__attr __args[]);
7.23
7.24 __attr __fn_native__read(__attr __args[]);
8.1 --- a/tests/list.py Wed Nov 23 17:23:52 2016 +0100
8.2 +++ b/tests/list.py Wed Nov 23 17:32:58 2016 +0100
8.3 @@ -10,3 +10,7 @@
8.4 print l[-3]
8.5 print l[-4]
8.6 print l
8.7 +
8.8 +t = (1, 2, 3, "four")
8.9 +l = list(t)
8.10 +print l
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/tests/tuple.py Wed Nov 23 17:32:58 2016 +0100
9.3 @@ -0,0 +1,15 @@
9.4 +l = (1, 2, 3, "four")
9.5 +print len(l)
9.6 +print l[0]
9.7 +print l[1]
9.8 +print l[2]
9.9 +print l[3]
9.10 +print l[-1]
9.11 +print l[-2]
9.12 +print l[-3]
9.13 +print l[-4]
9.14 +print l
9.15 +
9.16 +l = [1, 2, 3, "four"]
9.17 +t = tuple(l)
9.18 +print t