summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-03 07:11:42 -0800
committerDamien George <damien.p.george@gmail.com>2014-01-03 07:11:42 -0800
commit4b57fac1c8080f30c09156a99463544b3242d4ff (patch)
tree4d57c8700260cce9c5aaf0fd62962b9558c23c75
parentc2e21bb7d992afcfc92c5b6daa62491569d762e6 (diff)
parent9bc56d933f885d4231e047a25af2becbb1354ce3 (diff)
downloadmicropython-4b57fac1c8080f30c09156a99463544b3242d4ff.tar.gz
micropython-4b57fac1c8080f30c09156a99463544b3242d4ff.zip
Merge pull request #43 from chipaca/master
Implement list addition.
-rw-r--r--py/objlist.c12
-rw-r--r--tests/basics/tests/list2.py5
2 files changed, 17 insertions, 0 deletions
diff --git a/py/objlist.c b/py/objlist.c
index ce16ab6fe9..5d4e8c03ab 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,17 @@ 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);
+ 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:
// op not supported
return NULL;
diff --git a/tests/basics/tests/list2.py b/tests/basics/tests/list2.py
new file mode 100644
index 0000000000..e46042b86a
--- /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)