Commit 729f7b42 authored by Damien George's avatar Damien George
Browse files

py: Merge BINARY_OP_SUBSCR and store_subscr (w/ delete) into subscr.

mp_obj_t->subscr now does load/store/delete.
parent de7c4251
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#define MP_BC_LOAD_ATTR (0x28) // qstr #define MP_BC_LOAD_ATTR (0x28) // qstr
#define MP_BC_LOAD_METHOD (0x29) // qstr #define MP_BC_LOAD_METHOD (0x29) // qstr
#define MP_BC_LOAD_BUILD_CLASS (0x2a) #define MP_BC_LOAD_BUILD_CLASS (0x2a)
#define MP_BC_LOAD_SUBSCR (0x2b)
#define MP_BC_STORE_FAST_0 (0x30) #define MP_BC_STORE_FAST_0 (0x30)
#define MP_BC_STORE_FAST_1 (0x31) #define MP_BC_STORE_FAST_1 (0x31)
......
...@@ -670,7 +670,7 @@ void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t ...@@ -670,7 +670,7 @@ void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t
compile_node(comp, pns1->nodes[0]); compile_node(comp, pns1->nodes[0]);
if (assign_kind == ASSIGN_AUG_LOAD) { if (assign_kind == ASSIGN_AUG_LOAD) {
EMIT(dup_top_two); EMIT(dup_top_two);
EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR); EMIT(load_subscr);
} else { } else {
EMIT(store_subscr); EMIT(store_subscr);
} }
...@@ -2599,7 +2599,7 @@ void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) { ...@@ -2599,7 +2599,7 @@ void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) { void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
// object who's index we want is on top of stack // object who's index we want is on top of stack
compile_node(comp, pns->nodes[0]); // the index compile_node(comp, pns->nodes[0]); // the index
EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR); EMIT(load_subscr);
} }
void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) { void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
......
...@@ -50,6 +50,7 @@ typedef struct _emit_method_table_t { ...@@ -50,6 +50,7 @@ typedef struct _emit_method_table_t {
void (*load_attr)(emit_t *emit, qstr qstr); void (*load_attr)(emit_t *emit, qstr qstr);
void (*load_method)(emit_t *emit, qstr qstr); void (*load_method)(emit_t *emit, qstr qstr);
void (*load_build_class)(emit_t *emit); void (*load_build_class)(emit_t *emit);
void (*load_subscr)(emit_t *emit);
void (*store_fast)(emit_t *emit, qstr qstr, int local_num); void (*store_fast)(emit_t *emit, qstr qstr, int local_num);
void (*store_deref)(emit_t *emit, qstr qstr, int local_num); void (*store_deref)(emit_t *emit, qstr qstr, int local_num);
void (*store_name)(emit_t *emit, qstr qstr); void (*store_name)(emit_t *emit, qstr qstr);
......
...@@ -477,6 +477,11 @@ STATIC void emit_bc_load_build_class(emit_t *emit) { ...@@ -477,6 +477,11 @@ STATIC void emit_bc_load_build_class(emit_t *emit) {
emit_write_byte_code_byte(emit, MP_BC_LOAD_BUILD_CLASS); emit_write_byte_code_byte(emit, MP_BC_LOAD_BUILD_CLASS);
} }
STATIC void emit_bc_load_subscr(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte(emit, MP_BC_LOAD_SUBSCR);
}
STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) { STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
assert(local_num >= 0); assert(local_num >= 0);
emit_bc_pre(emit, -1); emit_bc_pre(emit, -1);
...@@ -873,6 +878,7 @@ const emit_method_table_t emit_bc_method_table = { ...@@ -873,6 +878,7 @@ const emit_method_table_t emit_bc_method_table = {
emit_bc_load_attr, emit_bc_load_attr,
emit_bc_load_method, emit_bc_load_method,
emit_bc_load_build_class, emit_bc_load_build_class,
emit_bc_load_subscr,
emit_bc_store_fast, emit_bc_store_fast,
emit_bc_store_deref, emit_bc_store_deref,
emit_bc_store_name, emit_bc_store_name,
......
...@@ -281,6 +281,13 @@ STATIC void emit_cpy_load_build_class(emit_t *emit) { ...@@ -281,6 +281,13 @@ STATIC void emit_cpy_load_build_class(emit_t *emit) {
} }
} }
STATIC void emit_cpy_load_subscr(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
printf("BINARY_SUBSCR\n");
}
}
STATIC void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) { STATIC void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, -1, 3); emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) { if (emit->pass == PASS_3) {
...@@ -555,7 +562,6 @@ STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) { ...@@ -555,7 +562,6 @@ STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) {
} }
if (emit->pass == PASS_3) { if (emit->pass == PASS_3) {
switch (op) { switch (op) {
case MP_BINARY_OP_SUBSCR: printf("BINARY_SUBSCR\n"); break;
case MP_BINARY_OP_OR: printf("BINARY_OR\n"); break; case MP_BINARY_OP_OR: printf("BINARY_OR\n"); break;
case MP_BINARY_OP_XOR: printf("BINARY_XOR\n"); break; case MP_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
case MP_BINARY_OP_AND: printf("BINARY_AND\n"); break; case MP_BINARY_OP_AND: printf("BINARY_AND\n"); break;
...@@ -817,6 +823,7 @@ const emit_method_table_t emit_cpython_method_table = { ...@@ -817,6 +823,7 @@ const emit_method_table_t emit_cpython_method_table = {
emit_cpy_load_attr, emit_cpy_load_attr,
emit_cpy_load_method, emit_cpy_load_method,
emit_cpy_load_build_class, emit_cpy_load_build_class,
emit_cpy_load_subscr,
emit_cpy_store_fast, emit_cpy_store_fast,
emit_cpy_store_deref, emit_cpy_store_deref,
emit_cpy_store_name, emit_cpy_store_name,
......
...@@ -778,6 +778,18 @@ STATIC void emit_native_load_build_class(emit_t *emit) { ...@@ -778,6 +778,18 @@ STATIC void emit_native_load_build_class(emit_t *emit) {
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
} }
STATIC void emit_native_load_subscr(emit_t *emit) {
vtype_kind_t vtype_lhs, vtype_rhs;
emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr, (machine_uint_t)MP_OBJ_SENTINEL, REG_ARG_3);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
} else {
printf("ViperTypeError: can't do subscr of types %d and %d\n", vtype_lhs, vtype_rhs);
assert(0);
}
}
STATIC void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) { STATIC void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) {
vtype_kind_t vtype; vtype_kind_t vtype;
#if N_X64 #if N_X64
...@@ -850,7 +862,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) { ...@@ -850,7 +862,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
assert(vtype_index == VTYPE_PYOBJ); assert(vtype_index == VTYPE_PYOBJ);
assert(vtype_base == VTYPE_PYOBJ); assert(vtype_base == VTYPE_PYOBJ);
assert(vtype_value == VTYPE_PYOBJ); assert(vtype_value == VTYPE_PYOBJ);
emit_call(emit, MP_F_STORE_SUBSCR, mp_store_subscr); emit_call(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr);
} }
STATIC void emit_native_delete_fast(emit_t *emit, qstr qstr, int local_num) { STATIC void emit_native_delete_fast(emit_t *emit, qstr qstr, int local_num) {
...@@ -882,8 +894,11 @@ STATIC void emit_native_delete_attr(emit_t *emit, qstr qstr) { ...@@ -882,8 +894,11 @@ STATIC void emit_native_delete_attr(emit_t *emit, qstr qstr) {
} }
STATIC void emit_native_delete_subscr(emit_t *emit) { STATIC void emit_native_delete_subscr(emit_t *emit) {
// not supported vtype_kind_t vtype_index, vtype_base;
assert(0); emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
assert(vtype_index == VTYPE_PYOBJ);
assert(vtype_base == VTYPE_PYOBJ);
emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr, (machine_uint_t)MP_OBJ_NULL, REG_ARG_3);
} }
STATIC void emit_native_dup_top(emit_t *emit) { STATIC void emit_native_dup_top(emit_t *emit) {
...@@ -1328,6 +1343,7 @@ const emit_method_table_t EXPORT_FUN(method_table) = { ...@@ -1328,6 +1343,7 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
emit_native_load_attr, emit_native_load_attr,
emit_native_load_method, emit_native_load_method,
emit_native_load_build_class, emit_native_load_build_class,
emit_native_load_subscr,
emit_native_store_fast, emit_native_store_fast,
emit_native_store_deref, emit_native_store_deref,
emit_native_store_name, emit_native_store_name,
......
...@@ -190,4 +190,5 @@ const emit_method_table_t emit_pass1_method_table = { ...@@ -190,4 +190,5 @@ const emit_method_table_t emit_pass1_method_table = {
(void*)emit_pass1_dummy, (void*)emit_pass1_dummy,
(void*)emit_pass1_dummy, (void*)emit_pass1_dummy,
(void*)emit_pass1_dummy, (void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
}; };
...@@ -332,6 +332,24 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) { ...@@ -332,6 +332,24 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
} }
} }
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
mp_obj_type_t *type = mp_obj_get_type(base);
if (type->subscr != NULL) {
mp_obj_t ret = type->subscr(base, index, value);
if (ret != MP_OBJ_NOT_SUPPORTED) {
return ret;
}
// TODO: call base classes here?
}
if (value == MP_OBJ_NULL) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item deletion", mp_obj_get_type_str(base)));
} else if (value == MP_OBJ_SENTINEL) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not subscriptable", mp_obj_get_type_str(base)));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
}
}
// Return input argument. Useful as .getiter for objects which are // Return input argument. Useful as .getiter for objects which are
// their own iterators, etc. // their own iterators, etc.
mp_obj_t mp_identity(mp_obj_t self) { mp_obj_t mp_identity(mp_obj_t self) {
......
...@@ -28,6 +28,10 @@ typedef struct _mp_obj_base_t mp_obj_base_t; ...@@ -28,6 +28,10 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
#define MP_OBJ_SENTINEL ((mp_obj_t)8) #define MP_OBJ_SENTINEL ((mp_obj_t)8)
// The NOT_SUPPORTED object is a return value that indicates an unsupported operation.
#define MP_OBJ_NOT_SUPPORTED ((mp_obj_t)16)
// These macros check for small int, qstr or object, and access small int and qstr values // These macros check for small int, qstr or object, and access small int and qstr values
// - xxxx...xxx1: a small int, bits 1 and above are the value // - xxxx...xxx1: a small int, bits 1 and above are the value
// - xxxx...xx10: a qstr, bits 2 and above are the value // - xxxx...xx10: a qstr, bits 2 and above are the value
...@@ -165,7 +169,7 @@ typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t); ...@@ -165,7 +169,7 @@ typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t);
typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t); typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
typedef void (*mp_load_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); // for fail, do nothing; for attr, dest[0] = value; for method, dest[0] = method, dest[1] = self typedef void (*mp_load_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); // for fail, do nothing; for attr, dest[0] = value; for method, dest[0] = method, dest[1] = self
typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded; if value==MP_OBJ_NULL then delete typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded; if value==MP_OBJ_NULL then delete
typedef bool (*mp_store_item_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); // return true if store succeeded; if value==MP_OBJ_NULL then delete typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
typedef struct _mp_method_t { typedef struct _mp_method_t {
qstr name; qstr name;
...@@ -222,16 +226,13 @@ struct _mp_obj_type_t { ...@@ -222,16 +226,13 @@ struct _mp_obj_type_t {
mp_load_attr_fun_t load_attr; mp_load_attr_fun_t load_attr;
mp_store_attr_fun_t store_attr; // if value is MP_OBJ_NULL, then delete that attribute mp_store_attr_fun_t store_attr; // if value is MP_OBJ_NULL, then delete that attribute
// Implements container[index] = val. If val == MP_OBJ_NULL, then it's a delete. mp_subscr_fun_t subscr; // implements load, store, delete subscripting
// Note that load_item is implemented by binary_op(RT_BINARY_OP_SUBSCR) // value=MP_OBJ_NULL means delete, value=MP_OBJ_SENTINEL means load, else store
mp_store_item_fun_t store_item; // can return MP_OBJ_NOT_SUPPORTED
mp_fun_1_t getiter; mp_fun_1_t getiter;
mp_fun_1_t iternext; // may return MP_OBJ_NULL as an optimisation instead of raising StopIteration() (with no args) mp_fun_1_t iternext; // may return MP_OBJ_NULL as an optimisation instead of raising StopIteration() (with no args)
// Alternatively, pointer(s) to interfaces to save space
// in mp_obj_type_t at the expense of extra pointer and extra dereference
// when actually used.
mp_buffer_p_t buffer_p; mp_buffer_p_t buffer_p;
const mp_stream_p_t *stream_p; const mp_stream_p_t *stream_p;
...@@ -242,8 +243,6 @@ struct _mp_obj_type_t { ...@@ -242,8 +243,6 @@ struct _mp_obj_type_t {
/* /*
What we might need to add here: What we might need to add here:
store_subscr list dict
len str tuple list map len str tuple list map
abs float complex abs float complex
hash bool int none str hash bool int none str
...@@ -395,6 +394,7 @@ void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items); ...@@ -395,6 +394,7 @@ void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items);
void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items); void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items);
uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice); uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice);
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return MP_OBJ_NULL */ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return MP_OBJ_NULL */
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
// bool // bool
// TODO make lower case when it has proven itself // TODO make lower case when it has proven itself
......
...@@ -112,21 +112,6 @@ STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) { ...@@ -112,21 +112,6 @@ STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
} }
} }
STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
mp_obj_array_t *o = lhs;
switch (op) {
case MP_BINARY_OP_SUBSCR:
{
uint index = mp_get_index(o->base.type, o->len, rhs, false);
return mp_binary_get_val_array(o->typecode, o->items, index);
}
default:
// op not supported
return MP_OBJ_NULL;
}
}
STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) { STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray)); assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray));
mp_obj_array_t *self = self_in; mp_obj_array_t *self = self_in;
...@@ -142,15 +127,22 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) { ...@@ -142,15 +127,22 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append); STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
STATIC bool array_store_item(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) { STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
if (value == MP_OBJ_NULL) { if (value == MP_OBJ_NULL) {
// delete item; does this need to be implemented? // delete item; does this need to be implemented?
return false; return MP_OBJ_NOT_SUPPORTED;
} else {
mp_obj_array_t *o = self_in;
uint index = mp_get_index(o->base.type, o->len, index_in, false);
if (value == MP_OBJ_SENTINEL) {
// load
return mp_binary_get_val_array(o->typecode, o->items, index);
} else {
// store
mp_binary_set_val_array(o->typecode, o->items, index, value);
return mp_const_none;
}
} }
mp_obj_array_t *o = self_in;
uint index = mp_get_index(o->base.type, o->len, index_in, false);
mp_binary_set_val_array(o->typecode, o->items, index, value);
return true;
} }
STATIC machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) { STATIC machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) {
...@@ -173,8 +165,7 @@ const mp_obj_type_t mp_type_array = { ...@@ -173,8 +165,7 @@ const mp_obj_type_t mp_type_array = {
.make_new = array_make_new, .make_new = array_make_new,
.getiter = array_iterator_new, .getiter = array_iterator_new,
.unary_op = array_unary_op, .unary_op = array_unary_op,
.binary_op = array_binary_op, .subscr = array_subscr,
.store_item = array_store_item,
.buffer_p = { .get_buffer = array_get_buffer }, .buffer_p = { .get_buffer = array_get_buffer },
.locals_dict = (mp_obj_t)&array_locals_dict, .locals_dict = (mp_obj_t)&array_locals_dict,
}; };
...@@ -186,8 +177,7 @@ const mp_obj_type_t mp_type_bytearray = { ...@@ -186,8 +177,7 @@ const mp_obj_type_t mp_type_bytearray = {
.make_new = bytearray_make_new, .make_new = bytearray_make_new,
.getiter = array_iterator_new, .getiter = array_iterator_new,
.unary_op = array_unary_op, .unary_op = array_unary_op,
.binary_op = array_binary_op, .subscr = array_subscr,
.store_item = array_store_item,
.buffer_p = { .get_buffer = array_get_buffer }, .buffer_p = { .get_buffer = array_get_buffer },
.locals_dict = (mp_obj_t)&array_locals_dict, .locals_dict = (mp_obj_t)&array_locals_dict,
}; };
......
...@@ -84,18 +84,7 @@ STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) { ...@@ -84,18 +84,7 @@ STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
mp_obj_dict_t *o = lhs_in; mp_obj_dict_t *o = lhs_in;
switch (op) { switch (op) {
case MP_BINARY_OP_SUBSCR: case MP_BINARY_OP_IN: {
{
// dict load
mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
if (elem == NULL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
} else {
return elem->value;
}
}
case MP_BINARY_OP_IN:
{
mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP); mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
return MP_BOOL(elem != NULL); return MP_BOOL(elem != NULL);
} }
...@@ -129,13 +118,25 @@ STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { ...@@ -129,13 +118,25 @@ STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
} }
} }
STATIC bool dict_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
if (value == MP_OBJ_NULL) { if (value == MP_OBJ_NULL) {
// delete
mp_obj_dict_delete(self_in, index); mp_obj_dict_delete(self_in, index);
return mp_const_none;
} else if (value == MP_OBJ_SENTINEL) {
// load
mp_obj_dict_t *self = self_in;
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
if (elem == NULL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
} else {
return elem->value;
}
} else { } else {
// store
mp_obj_dict_store(self_in, index, value); mp_obj_dict_store(self_in, index, value);
return mp_const_none;
} }
return true;
} }
/******************************************************************************/ /******************************************************************************/
...@@ -510,7 +511,7 @@ const mp_obj_type_t mp_type_dict = { ...@@ -510,7 +511,7 @@ const mp_obj_type_t mp_type_dict = {
.make_new = dict_make_new, .make_new = dict_make_new,
.unary_op = dict_unary_op, .unary_op = dict_unary_op,
.binary_op = dict_binary_op, .binary_op = dict_binary_op,
.store_item = dict_store_item, .subscr = dict_subscr,
.getiter = dict_getiter, .getiter = dict_getiter,
.locals_dict = (mp_obj_t)&dict_locals_dict, .locals_dict = (mp_obj_t)&dict_locals_dict,
}; };
......
...@@ -88,24 +88,7 @@ STATIC mp_obj_t list_unary_op(int op, mp_obj_t self_in) { ...@@ -88,24 +88,7 @@ STATIC mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
mp_obj_list_t *o = lhs; mp_obj_list_t *o = lhs;
switch (op) { switch (op) {
case MP_BINARY_OP_SUBSCR: case MP_BINARY_OP_ADD: {
{
#if MICROPY_ENABLE_SLICE
if (MP_OBJ_IS_TYPE(rhs, &mp_type_slice)) {
machine_uint_t start, stop;
if (!m_seq_get_fast_slice_indexes(o->len, rhs, &start, &stop)) {
assert(0);
}
mp_obj_list_t *res = list_new(stop - start);
m_seq_copy(res->items, o->items + start, res->len, mp_obj_t);
return res;
}
#endif
uint index = mp_get_index(o->base.type, o->len, rhs, false);
return o->items[index];
}
case MP_BINARY_OP_ADD:
{
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
return NULL; return NULL;
} }
...@@ -114,8 +97,7 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { ...@@ -114,8 +97,7 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t); m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
return s; return s;
} }
case MP_BINARY_OP_INPLACE_ADD: case MP_BINARY_OP_INPLACE_ADD: {
{
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
return NULL; return NULL;
} }
...@@ -144,14 +126,32 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { ...@@ -144,14 +126,32 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
} }
} }
STATIC bool list_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
if (value == MP_OBJ_NULL) { if (value == MP_OBJ_NULL) {
// delete
mp_obj_t args[2] = {self_in, index}; mp_obj_t args[2] = {self_in, index};
list_pop(2, args); list_pop(2, args);
return mp_const_none;
} else if (value == MP_OBJ_SENTINEL) {
// load
mp_obj_list_t *self = self_in;
#if MICROPY_ENABLE_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
machine_uint_t start, stop;
if (!m_seq_get_fast_slice_indexes(self->len, index, &start, &stop)) {
assert(0);
}
mp_obj_list_t *res = list_new(stop - start);
m_seq_copy(res->items, self->items + start, res->len, mp_obj_t);
return res;
}
#endif
uint index_val = mp_get_index(self->base.type, self->len, index, false);
return self->items[index_val];
} else { } else {
mp_obj_list_store(self_in, index, value); mp_obj_list_store(self_in, index, value);
return mp_const_none;
} }
return true;
} }
STATIC mp_obj_t list_getiter(mp_obj_t o_in) { STATIC mp_obj_t list_getiter(mp_obj_t o_in) {
...@@ -360,7 +360,7 @@ const mp_obj_type_t mp_type_list = { ...@@ -360,7 +360,7 @@ const mp_obj_type_t mp_type_list = {
.make_new = list_make_new, .make_new = list_make_new,
.unary_op = list_unary_op, .unary_op = list_unary_op,
.binary_op = list_binary_op, .binary_op = list_binary_op,
.store_item = list_store_item, .subscr = list_subscr,
.getiter = list_getiter, .getiter = list_getiter,
.locals_dict = (mp_obj_t)&list_locals_dict, .locals_dict = (mp_obj_t)&list_locals_dict,
}; };
......
...@@ -131,6 +131,7 @@ mp_obj_t mp_obj_new_namedtuple_type(qstr name, const char *fields) { ...@@ -131,6 +131,7 @@ mp_obj_t mp_obj_new_namedtuple_type(qstr name, const char *fields) {
o->base.binary_op = tuple_binary_op; o->base.binary_op = tuple_binary_op;
o->base.load_attr = namedtuple_load_attr; o->base.load_attr = namedtuple_load_attr;
o->base.store_attr = namedtuple_store_attr; o->base.store_attr = namedtuple_store_attr;
o->base.subscr = tuple_subscr;
o->base.bases_tuple = (mp_obj_t)&namedtuple_base_tuple; o->base.bases_tuple = (mp_obj_t)&namedtuple_base_tuple;
o->fields = fields; o->fields = fields;
return o; return o;
......
...@@ -214,25 +214,6 @@ STATIC const byte *find_subbytes(const byte *haystack, machine_uint_t hlen, cons ...@@ -214,25 +214,6 @@ STATIC const byte *find_subbytes(const byte *haystack, machine_uint_t hlen, cons
STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
GET_STR_DATA_LEN(lhs_in, lhs_data, lhs_len); GET_STR_DATA_LEN(lhs_in, lhs_data, lhs_len);
switch (op) { switch (op) {
case MP_BINARY_OP_SUBSCR: {