From 4cb80582c4154f59118eab902872ab0c3a496303 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Fri, 3 Jan 2014 02:27:08 +0000 Subject: [PATCH 1/4] Add list addition (fixes: #39) --- py/objlist.c | 16 ++++++++++++++++ tests/basics/tests/list2.py | 5 +++++ 2 files changed, 21 insertions(+) create mode 100644 tests/basics/tests/list2.py diff --git a/py/objlist.c b/py/objlist.c index ce16ab6fe..b488a1964 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -18,6 +18,7 @@ typedef struct _mp_obj_list_t { } mp_obj_list_t; static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur); +static mp_obj_list_t *list_new(uint n); /******************************************************************************/ /* list */ @@ -43,6 +44,21 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { uint index = mp_get_index(o->base.type, o->len, rhs); return o->items[index]; } + case RT_BINARY_OP_ADD: + { + if (!MP_OBJ_IS_TYPE(rhs, &list_type)) { + return NULL; + } + mp_obj_list_t *p = rhs; + mp_obj_list_t *s = list_new(o->len + p->len); + for (int i = 0; i < o->len; i++) { + s->items[i] = o->items[i]; + } + for (int i = 0; i < p->len; i++) { + s->items[i+o->len] = p->items[i]; + } + return s; + } default: // op not supported return NULL; diff --git a/tests/basics/tests/list2.py b/tests/basics/tests/list2.py new file mode 100644 index 000000000..e46042b86 --- /dev/null +++ b/tests/basics/tests/list2.py @@ -0,0 +1,5 @@ +# list addition +a = [1,2,3] +b = [4,5,6] +c = a + b +print(c) From 81ad89c46e5b53c7a0e91e56f972e43751ddeb3d Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Fri, 3 Jan 2014 02:32:40 +0000 Subject: [PATCH 2/4] untabified --- py/objlist.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/py/objlist.c b/py/objlist.c index b488a1964..e3596a5c7 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -46,18 +46,18 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { } case RT_BINARY_OP_ADD: { - if (!MP_OBJ_IS_TYPE(rhs, &list_type)) { - return NULL; - } - mp_obj_list_t *p = rhs; - mp_obj_list_t *s = list_new(o->len + p->len); - for (int i = 0; i < o->len; i++) { - s->items[i] = o->items[i]; - } - for (int i = 0; i < p->len; i++) { - s->items[i+o->len] = p->items[i]; - } - return s; + if (!MP_OBJ_IS_TYPE(rhs, &list_type)) { + return NULL; + } + mp_obj_list_t *p = rhs; + mp_obj_list_t *s = list_new(o->len + p->len); + for (int i = 0; i < o->len; i++) { + s->items[i] = o->items[i]; + } + for (int i = 0; i < p->len; i++) { + s->items[i+o->len] = p->items[i]; + } + return s; } default: // op not supported From aeb16c36b0f77acd10e8228b7e9e5582c9e0d0ee Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Fri, 3 Jan 2014 02:36:35 +0000 Subject: [PATCH 3/4] Add a bit of whitespace. --- py/objlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/objlist.c b/py/objlist.c index e3596a5c7..31f04f2ce 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -55,7 +55,7 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { s->items[i] = o->items[i]; } for (int i = 0; i < p->len; i++) { - s->items[i+o->len] = p->items[i]; + s->items[i + o->len] = p->items[i]; } return s; } From 9bc56d933f885d4231e047a25af2becbb1354ce3 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Fri, 3 Jan 2014 10:13:38 +0000 Subject: [PATCH 4/4] Changed to use memcpy. --- py/objlist.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/py/objlist.c b/py/objlist.c index 31f04f2ce..5d4e8c03a 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -51,12 +51,8 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { } mp_obj_list_t *p = rhs; mp_obj_list_t *s = list_new(o->len + p->len); - for (int i = 0; i < o->len; i++) { - s->items[i] = o->items[i]; - } - for (int i = 0; i < p->len; i++) { - s->items[i + o->len] = p->items[i]; - } + memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len); + memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len); return s; } default: