Add list addition (fixes: #39)

This commit is contained in:
John R. Lenton 2014-01-03 02:27:08 +00:00
parent aae7847508
commit 4cb80582c4
2 changed files with 21 additions and 0 deletions

View File

@ -18,6 +18,7 @@ typedef struct _mp_obj_list_t {
} 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_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
static mp_obj_list_t *list_new(uint n);
/******************************************************************************/ /******************************************************************************/
/* list */ /* 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); uint index = mp_get_index(o->base.type, o->len, rhs);
return o->items[index]; 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: default:
// op not supported // op not supported
return NULL; return NULL;

View File

@ -0,0 +1,5 @@
# list addition
a = [1,2,3]
b = [4,5,6]
c = a + b
print(c)