Skip to content

Commit 4b57fac

Browse files
committed
Merge pull request adafruit#43 from chipaca/master
Implement list addition.
2 parents c2e21bb + 9bc56d9 commit 4b57fac

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

py/objlist.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ typedef struct _mp_obj_list_t {
1818
} mp_obj_list_t;
1919

2020
static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
21+
static mp_obj_list_t *list_new(uint n);
2122

2223
/******************************************************************************/
2324
/* list */
@@ -43,6 +44,17 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
4344
uint index = mp_get_index(o->base.type, o->len, rhs);
4445
return o->items[index];
4546
}
47+
case RT_BINARY_OP_ADD:
48+
{
49+
if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
50+
return NULL;
51+
}
52+
mp_obj_list_t *p = rhs;
53+
mp_obj_list_t *s = list_new(o->len + p->len);
54+
memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
55+
memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
56+
return s;
57+
}
4658
default:
4759
// op not supported
4860
return NULL;

tests/basics/tests/list2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# list addition
2+
a = [1,2,3]
3+
b = [4,5,6]
4+
c = a + b
5+
print(c)

0 commit comments

Comments
 (0)