py: Implement DELETE_SUBSCR bytecode; implement mp_obj_dict_delete.

This commit is contained in:
Damien George 2014-04-05 13:25:13 +01:00
parent 3c8aecf62c
commit 66edc5d899
5 changed files with 30 additions and 0 deletions

View File

@ -440,6 +440,7 @@ typedef struct _mp_obj_dict_t {
} mp_obj_dict_t;
uint mp_obj_dict_len(mp_obj_t self_in);
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in);
// set

View File

@ -481,6 +481,13 @@ mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
return self_in;
}
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
mp_obj_dict_t *self = self_in;
dict_get_helper(&self->map, key, NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
return self_in;
}
mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
mp_obj_dict_t *self = self_in;

View File

@ -150,6 +150,7 @@ void mp_store_name(qstr qstr, mp_obj_t obj) {
void mp_delete_name(qstr qstr) {
DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
// TODO raise NameError if qstr not found
mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
}
@ -807,6 +808,21 @@ void mp_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
}
}
void mp_delete_subscr(mp_obj_t base, mp_obj_t index) {
DEBUG_OP_printf("delete subscr %p[%p]\n", base, index);
/* list delete not implemented
if (MP_OBJ_IS_TYPE(base, &mp_type_list)) {
// list delete
mp_obj_list_delete(base, index);
} else */
if (MP_OBJ_IS_TYPE(base, &mp_type_dict)) {
// dict delete
mp_obj_dict_delete(base, index);
} else {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item deletion", mp_obj_get_type_str(base)));
}
}
mp_obj_t mp_getiter(mp_obj_t o_in) {
mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->getiter != NULL) {

View File

@ -49,6 +49,7 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest);
void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest);
void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val);
void mp_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
void mp_delete_subscr(mp_obj_t base, mp_obj_t index);
mp_obj_t mp_getiter(mp_obj_t o);
mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_NULL instead of raising StopIteration()

View File

@ -324,6 +324,11 @@ dispatch_loop:
mp_delete_name(qst);
break;
case MP_BC_DELETE_SUBSCR:
mp_delete_subscr(sp[-1], sp[0]);
sp -= 3;
break;
case MP_BC_DUP_TOP:
obj1 = TOP();
PUSH(obj1);