Skip to content

Commit 4cb8058

Browse files
committed
Add list addition (fixes: adafruit#39)
1 parent aae7847 commit 4cb8058

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

py/objlist.c

Lines changed: 16 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,21 @@ 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+
for (int i = 0; i < o->len; i++) {
55+
s->items[i] = o->items[i];
56+
}
57+
for (int i = 0; i < p->len; i++) {
58+
s->items[i+o->len] = p->items[i];
59+
}
60+
return s;
61+
}
4662
default:
4763
// op not supported
4864
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)