Implement tuple addition.

This commit is contained in:
Paul Sokolovsky 2014-02-08 23:17:51 +02:00
parent e827e98a6f
commit ee4aaf7cdd
4 changed files with 15 additions and 3 deletions

View File

@ -398,6 +398,7 @@ typedef struct _mp_obj_static_class_method_t {
// sequence helpers
void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest);
bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end);
#define m_seq_copy(dest, src, len, item_sz) memcpy(dest, src, len * sizeof(item_sz))
#define m_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
#define m_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, len1 * sizeof(item_t)); memcpy(dest + len1, src2, len2 * sizeof(item_t)); }
bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2);
bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2);

View File

@ -114,8 +114,7 @@ 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);
memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
return s;
}
case RT_BINARY_OP_INPLACE_ADD:

View File

@ -114,6 +114,16 @@ static mp_obj_t tuple_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, &tuple_type)) {
return NULL;
}
mp_obj_tuple_t *p = rhs;
mp_obj_tuple_t *s = mp_obj_new_tuple(o->len + p->len, NULL);
m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
return s;
}
case RT_BINARY_OP_EQUAL:
case RT_BINARY_OP_LESS:
case RT_BINARY_OP_LESS_EQUAL:

View File

@ -14,3 +14,5 @@ except AttributeError:
print(x[1:])
print(x[:-1])
print(x[2:3])
print(x + (10, 100, 10000))