py: Merge BINARY_OP_SUBSCR and store_subscr (w/ delete) into subscr.
mp_obj_t->subscr now does load/store/delete.
This commit is contained in:
parent
de7c425139
commit
729f7b42d6
1
py/bc0.h
1
py/bc0.h
@ -23,6 +23,7 @@
|
||||
#define MP_BC_LOAD_ATTR (0x28) // qstr
|
||||
#define MP_BC_LOAD_METHOD (0x29) // qstr
|
||||
#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_1 (0x31)
|
||||
|
@ -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]);
|
||||
if (assign_kind == ASSIGN_AUG_LOAD) {
|
||||
EMIT(dup_top_two);
|
||||
EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
|
||||
EMIT(load_subscr);
|
||||
} else {
|
||||
EMIT(store_subscr);
|
||||
}
|
||||
@ -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) {
|
||||
// object who's index we want is on top of stack
|
||||
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) {
|
||||
|
@ -50,6 +50,7 @@ typedef struct _emit_method_table_t {
|
||||
void (*load_attr)(emit_t *emit, qstr qstr);
|
||||
void (*load_method)(emit_t *emit, qstr qstr);
|
||||
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_deref)(emit_t *emit, qstr qstr, int local_num);
|
||||
void (*store_name)(emit_t *emit, qstr qstr);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
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) {
|
||||
assert(local_num >= 0);
|
||||
emit_bc_pre(emit, -1);
|
||||
@ -873,6 +878,7 @@ const emit_method_table_t emit_bc_method_table = {
|
||||
emit_bc_load_attr,
|
||||
emit_bc_load_method,
|
||||
emit_bc_load_build_class,
|
||||
emit_bc_load_subscr,
|
||||
emit_bc_store_fast,
|
||||
emit_bc_store_deref,
|
||||
emit_bc_store_name,
|
||||
|
@ -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) {
|
||||
emit_pre(emit, -1, 3);
|
||||
if (emit->pass == PASS_3) {
|
||||
@ -555,7 +562,6 @@ STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) {
|
||||
}
|
||||
if (emit->pass == PASS_3) {
|
||||
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_XOR: printf("BINARY_XOR\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 = {
|
||||
emit_cpy_load_attr,
|
||||
emit_cpy_load_method,
|
||||
emit_cpy_load_build_class,
|
||||
emit_cpy_load_subscr,
|
||||
emit_cpy_store_fast,
|
||||
emit_cpy_store_deref,
|
||||
emit_cpy_store_name,
|
||||
|
@ -778,6 +778,18 @@ STATIC void emit_native_load_build_class(emit_t *emit) {
|
||||
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) {
|
||||
vtype_kind_t vtype;
|
||||
#if N_X64
|
||||
@ -850,7 +862,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
|
||||
assert(vtype_index == VTYPE_PYOBJ);
|
||||
assert(vtype_base == 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) {
|
||||
@ -882,8 +894,11 @@ STATIC void emit_native_delete_attr(emit_t *emit, qstr qstr) {
|
||||
}
|
||||
|
||||
STATIC void emit_native_delete_subscr(emit_t *emit) {
|
||||
// not supported
|
||||
assert(0);
|
||||
vtype_kind_t vtype_index, vtype_base;
|
||||
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) {
|
||||
@ -1328,6 +1343,7 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
|
||||
emit_native_load_attr,
|
||||
emit_native_load_method,
|
||||
emit_native_load_build_class,
|
||||
emit_native_load_subscr,
|
||||
emit_native_store_fast,
|
||||
emit_native_store_deref,
|
||||
emit_native_store_name,
|
||||
|
@ -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,
|
||||
};
|
||||
|
18
py/obj.c
18
py/obj.c
@ -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
|
||||
// their own iterators, etc.
|
||||
mp_obj_t mp_identity(mp_obj_t self) {
|
||||
|
18
py/obj.h
18
py/obj.h
@ -28,6 +28,10 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
|
||||
|
||||
#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
|
||||
// - xxxx...xxx1: a small int, bits 1 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);
|
||||
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 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 {
|
||||
qstr name;
|
||||
@ -222,16 +226,13 @@ struct _mp_obj_type_t {
|
||||
mp_load_attr_fun_t load_attr;
|
||||
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.
|
||||
// Note that load_item is implemented by binary_op(RT_BINARY_OP_SUBSCR)
|
||||
mp_store_item_fun_t store_item;
|
||||
mp_subscr_fun_t subscr; // implements load, store, delete subscripting
|
||||
// value=MP_OBJ_NULL means delete, value=MP_OBJ_SENTINEL means load, else store
|
||||
// can return MP_OBJ_NOT_SUPPORTED
|
||||
|
||||
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)
|
||||
|
||||
// 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;
|
||||
const mp_stream_p_t *stream_p;
|
||||
|
||||
@ -242,8 +243,6 @@ struct _mp_obj_type_t {
|
||||
/*
|
||||
What we might need to add here:
|
||||
|
||||
store_subscr list dict
|
||||
|
||||
len str tuple list map
|
||||
abs float complex
|
||||
hash bool int none str
|
||||
@ -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);
|
||||
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_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
|
||||
|
||||
// bool
|
||||
// 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) {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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;
|
||||
@ -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 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) {
|
||||
// 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) {
|
||||
@ -173,8 +165,7 @@ const mp_obj_type_t mp_type_array = {
|
||||
.make_new = array_make_new,
|
||||
.getiter = array_iterator_new,
|
||||
.unary_op = array_unary_op,
|
||||
.binary_op = array_binary_op,
|
||||
.store_item = array_store_item,
|
||||
.subscr = array_subscr,
|
||||
.buffer_p = { .get_buffer = array_get_buffer },
|
||||
.locals_dict = (mp_obj_t)&array_locals_dict,
|
||||
};
|
||||
@ -186,8 +177,7 @@ const mp_obj_type_t mp_type_bytearray = {
|
||||
.make_new = bytearray_make_new,
|
||||
.getiter = array_iterator_new,
|
||||
.unary_op = array_unary_op,
|
||||
.binary_op = array_binary_op,
|
||||
.store_item = array_store_item,
|
||||
.subscr = array_subscr,
|
||||
.buffer_p = { .get_buffer = array_get_buffer },
|
||||
.locals_dict = (mp_obj_t)&array_locals_dict,
|
||||
};
|
||||
|
31
py/objdict.c
31
py/objdict.c
@ -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) {
|
||||
mp_obj_dict_t *o = lhs_in;
|
||||
switch (op) {
|
||||
case MP_BINARY_OP_SUBSCR:
|
||||
{
|
||||
// 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:
|
||||
{
|
||||
case MP_BINARY_OP_IN: {
|
||||
mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
|
||||
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) {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
// delete
|
||||
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 {
|
||||
// store
|
||||
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 = {
|
||||
.make_new = dict_make_new,
|
||||
.unary_op = dict_unary_op,
|
||||
.binary_op = dict_binary_op,
|
||||
.store_item = dict_store_item,
|
||||
.subscr = dict_subscr,
|
||||
.getiter = dict_getiter,
|
||||
.locals_dict = (mp_obj_t)&dict_locals_dict,
|
||||
};
|
||||
|
46
py/objlist.c
46
py/objlist.c
@ -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) {
|
||||
mp_obj_list_t *o = lhs;
|
||||
switch (op) {
|
||||
case MP_BINARY_OP_SUBSCR:
|
||||
{
|
||||
#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:
|
||||
{
|
||||
case MP_BINARY_OP_ADD: {
|
||||
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
|
||||
return NULL;
|
||||
}
|
||||
@ -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);
|
||||
return s;
|
||||
}
|
||||
case MP_BINARY_OP_INPLACE_ADD:
|
||||
{
|
||||
case MP_BINARY_OP_INPLACE_ADD: {
|
||||
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
|
||||
return NULL;
|
||||
}
|
||||
@ -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) {
|
||||
// delete
|
||||
mp_obj_t args[2] = {self_in, index};
|
||||
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 {
|
||||
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) {
|
||||
@ -360,7 +360,7 @@ const mp_obj_type_t mp_type_list = {
|
||||
.make_new = list_make_new,
|
||||
.unary_op = list_unary_op,
|
||||
.binary_op = list_binary_op,
|
||||
.store_item = list_store_item,
|
||||
.subscr = list_subscr,
|
||||
.getiter = list_getiter,
|
||||
.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) {
|
||||
o->base.binary_op = tuple_binary_op;
|
||||
o->base.load_attr = namedtuple_load_attr;
|
||||
o->base.store_attr = namedtuple_store_attr;
|
||||
o->base.subscr = tuple_subscr;
|
||||
o->base.bases_tuple = (mp_obj_t)&namedtuple_base_tuple;
|
||||
o->fields = fields;
|
||||
return o;
|
||||
|
46
py/objstr.c
46
py/objstr.c
@ -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) {
|
||||
GET_STR_DATA_LEN(lhs_in, lhs_data, lhs_len);
|
||||
switch (op) {
|
||||
case MP_BINARY_OP_SUBSCR: {
|
||||
#if MICROPY_ENABLE_SLICE
|
||||
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_slice)) {
|
||||
machine_uint_t start, stop;
|
||||
if (!m_seq_get_fast_slice_indexes(lhs_len, rhs_in, &start, &stop)) {
|
||||
assert(0);
|
||||
}
|
||||
return mp_obj_new_str(lhs_data + start, stop - start, false);
|
||||
}
|
||||
#endif
|
||||
mp_obj_type_t *type = mp_obj_get_type(lhs_in);
|
||||
uint index = mp_get_index(type, lhs_len, rhs_in, false);
|
||||
if (type == &mp_type_bytes) {
|
||||
return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)lhs_data[index]);
|
||||
} else {
|
||||
return mp_obj_new_str(lhs_data + index, 1, true);
|
||||
}
|
||||
}
|
||||
|
||||
case MP_BINARY_OP_ADD:
|
||||
case MP_BINARY_OP_INPLACE_ADD:
|
||||
if (MP_OBJ_IS_STR(rhs_in)) {
|
||||
@ -307,6 +288,31 @@ STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
||||
return MP_OBJ_NULL; // op not supported
|
||||
}
|
||||
|
||||
STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
||||
GET_STR_DATA_LEN(self_in, self_data, self_len);
|
||||
if (value == MP_OBJ_SENTINEL) {
|
||||
// load
|
||||
#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);
|
||||
}
|
||||
return mp_obj_new_str(self_data + start, stop - start, false);
|
||||
}
|
||||
#endif
|
||||
mp_obj_type_t *type = mp_obj_get_type(self_in);
|
||||
uint index_val = mp_get_index(type, self_len, index, false);
|
||||
if (type == &mp_type_bytes) {
|
||||
return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)self_data[index_val]);
|
||||
} else {
|
||||
return mp_obj_new_str(self_data + index_val, 1, true);
|
||||
}
|
||||
} else {
|
||||
return MP_OBJ_NOT_SUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
|
||||
assert(MP_OBJ_IS_STR(self_in));
|
||||
|
||||
@ -1380,6 +1386,7 @@ const mp_obj_type_t mp_type_str = {
|
||||
.print = str_print,
|
||||
.make_new = str_make_new,
|
||||
.binary_op = str_binary_op,
|
||||
.subscr = str_subscr,
|
||||
.getiter = mp_obj_new_str_iterator,
|
||||
.buffer_p = { .get_buffer = str_get_buffer },
|
||||
.locals_dict = (mp_obj_t)&str_locals_dict,
|
||||
@ -1392,6 +1399,7 @@ const mp_obj_type_t mp_type_bytes = {
|
||||
.print = str_print,
|
||||
.make_new = bytes_make_new,
|
||||
.binary_op = str_binary_op,
|
||||
.subscr = str_subscr,
|
||||
.getiter = mp_obj_new_bytes_iterator,
|
||||
.buffer_p = { .get_buffer = str_get_buffer },
|
||||
.locals_dict = (mp_obj_t)&str_locals_dict,
|
||||
|
@ -95,22 +95,6 @@ mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
|
||||
mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
|
||||
mp_obj_tuple_t *o = lhs;
|
||||
switch (op) {
|
||||
case MP_BINARY_OP_SUBSCR:
|
||||
{
|
||||
#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_tuple_t *res = mp_obj_new_tuple(stop - start, NULL);
|
||||
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_subclass_fast(mp_obj_get_type(rhs), (mp_obj_t)&mp_type_tuple)) {
|
||||
@ -144,6 +128,28 @@ mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
||||
if (value == MP_OBJ_SENTINEL) {
|
||||
// load
|
||||
mp_obj_tuple_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_tuple_t *res = mp_obj_new_tuple(stop - start, NULL);
|
||||
m_seq_copy(res->items, self->items + start, res->len, mp_obj_t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
uint index_value = mp_get_index(self->base.type, self->len, index, false);
|
||||
return self->items[index_value];
|
||||
} else {
|
||||
return MP_OBJ_NOT_SUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
STATIC mp_obj_t tuple_getiter(mp_obj_t o_in) {
|
||||
return mp_obj_new_tuple_iterator(o_in, 0);
|
||||
}
|
||||
@ -176,6 +182,7 @@ const mp_obj_type_t mp_type_tuple = {
|
||||
.make_new = mp_obj_tuple_make_new,
|
||||
.unary_op = tuple_unary_op,
|
||||
.binary_op = tuple_binary_op,
|
||||
.subscr = tuple_subscr,
|
||||
.getiter = tuple_getiter,
|
||||
.locals_dict = (mp_obj_t)&tuple_locals_dict,
|
||||
};
|
||||
|
@ -7,3 +7,4 @@ typedef struct _mp_obj_tuple_t {
|
||||
void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
|
||||
mp_obj_t tuple_unary_op(int op, mp_obj_t self_in);
|
||||
mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs);
|
||||
mp_obj_t tuple_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value);
|
||||
|
21
py/objtype.c
21
py/objtype.c
@ -144,7 +144,6 @@ STATIC mp_obj_t class_unary_op(int op, mp_obj_t self_in) {
|
||||
}
|
||||
|
||||
STATIC const qstr binary_op_method_name[] = {
|
||||
[MP_BINARY_OP_SUBSCR] = MP_QSTR___getitem__,
|
||||
/*
|
||||
MP_BINARY_OP_OR,
|
||||
MP_BINARY_OP_XOR,
|
||||
@ -308,7 +307,7 @@ STATIC bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
|
||||
}
|
||||
}
|
||||
|
||||
bool class_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
||||
STATIC mp_obj_t class_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
||||
mp_obj_class_t *self = self_in;
|
||||
mp_obj_t member;
|
||||
uint meth_args;
|
||||
@ -316,16 +315,26 @@ bool class_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
||||
// delete item
|
||||
member = mp_obj_class_lookup(self->base.type, MP_QSTR___delitem__);
|
||||
meth_args = 2;
|
||||
} else if (value == MP_OBJ_SENTINEL) {
|
||||
// load item
|
||||
member = mp_obj_class_lookup(self->base.type, MP_QSTR___getitem__);
|
||||
meth_args = 2;
|
||||
} else {
|
||||
// store item
|
||||
member = mp_obj_class_lookup(self->base.type, MP_QSTR___setitem__);
|
||||
meth_args = 3;
|
||||
}
|
||||
if (member != MP_OBJ_NULL) {
|
||||
mp_obj_t args[3] = {self_in, index, value};
|
||||
mp_call_function_n_kw(member, meth_args, 0, args);
|
||||
return true;
|
||||
// TODO probably need to call class_convert_return_attr, and use mp_call_method_n_kw
|
||||
mp_obj_t ret = mp_call_function_n_kw(member, meth_args, 0, args);
|
||||
if (value == MP_OBJ_SENTINEL) {
|
||||
return ret;
|
||||
} else {
|
||||
return mp_const_none;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
return MP_OBJ_NOT_SUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
@ -464,7 +473,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
|
||||
o->binary_op = class_binary_op;
|
||||
o->load_attr = class_load_attr;
|
||||
o->store_attr = class_store_attr;
|
||||
o->store_item = class_store_item;
|
||||
o->subscr = class_subscr;
|
||||
o->bases_tuple = bases_tuple;
|
||||
o->locals_dict = locals_dict;
|
||||
return o;
|
||||
|
@ -7,21 +7,21 @@
|
||||
#include "runtime0.h"
|
||||
#include "builtin.h"
|
||||
|
||||
STATIC mp_obj_t op_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
||||
mp_obj_type_t *type = mp_obj_get_type(lhs_in);
|
||||
return type->binary_op(MP_BINARY_OP_SUBSCR, lhs_in, rhs_in);
|
||||
STATIC mp_obj_t op_getitem(mp_obj_t self_in, mp_obj_t key_in) {
|
||||
mp_obj_type_t *type = mp_obj_get_type(self_in);
|
||||
return type->subscr(self_in, key_in, MP_OBJ_SENTINEL);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem);
|
||||
|
||||
STATIC mp_obj_t op_setitem(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) {
|
||||
mp_store_subscr(self_in, key_in, value_in);
|
||||
return mp_const_none;
|
||||
mp_obj_type_t *type = mp_obj_get_type(self_in);
|
||||
return type->subscr(self_in, key_in, value_in);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_3(mp_op_setitem_obj, op_setitem);
|
||||
|
||||
STATIC mp_obj_t op_delitem(mp_obj_t self_in, mp_obj_t key_in) {
|
||||
mp_store_subscr(self_in, key_in, MP_OBJ_NULL);
|
||||
return mp_const_none;
|
||||
mp_obj_type_t *type = mp_obj_get_type(self_in);
|
||||
return type->subscr(self_in, key_in, MP_OBJ_NULL);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_delitem_obj, op_delitem);
|
||||
|
||||
|
19
py/runtime.c
19
py/runtime.c
@ -840,23 +840,6 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
|
||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
|
||||
}
|
||||
|
||||
void mp_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
|
||||
DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
|
||||
mp_obj_type_t *type = mp_obj_get_type(base);
|
||||
if (type->store_item != NULL) {
|
||||
bool r = type->store_item(base, index, value);
|
||||
if (r) {
|
||||
return;
|
||||
}
|
||||
// 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 {
|
||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item assignment", 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) {
|
||||
@ -1120,7 +1103,7 @@ void *const mp_fun_table[MP_F_NUMBER_OF] = {
|
||||
mp_load_method,
|
||||
mp_store_name,
|
||||
mp_store_attr,
|
||||
mp_store_subscr,
|
||||
mp_obj_subscr,
|
||||
mp_obj_is_true,
|
||||
mp_unary_op,
|
||||
mp_binary_op,
|
||||
|
@ -48,7 +48,6 @@ mp_obj_t mp_load_attr(mp_obj_t base, qstr attr);
|
||||
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);
|
||||
|
||||
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()
|
||||
|
@ -24,7 +24,6 @@ typedef enum {
|
||||
} mp_unary_op_t;
|
||||
|
||||
typedef enum {
|
||||
MP_BINARY_OP_SUBSCR,
|
||||
MP_BINARY_OP_OR,
|
||||
MP_BINARY_OP_XOR,
|
||||
MP_BINARY_OP_AND,
|
||||
@ -75,7 +74,7 @@ typedef enum {
|
||||
MP_F_LOAD_METHOD,
|
||||
MP_F_STORE_NAME,
|
||||
MP_F_STORE_ATTR,
|
||||
MP_F_STORE_SUBSCR,
|
||||
MP_F_OBJ_SUBSCR,
|
||||
MP_F_OBJ_IS_TRUE,
|
||||
MP_F_UNARY_OP,
|
||||
MP_F_BINARY_OP,
|
||||
|
@ -182,6 +182,10 @@ void mp_byte_code_print(const byte *ip, int len) {
|
||||
printf("LOAD_BUILD_CLASS");
|
||||
break;
|
||||
|
||||
case MP_BC_LOAD_SUBSCR:
|
||||
printf("LOAD_SUBSCR");
|
||||
break;
|
||||
|
||||
case MP_BC_STORE_FAST_0:
|
||||
printf("STORE_FAST_0");
|
||||
break;
|
||||
|
7
py/vm.c
7
py/vm.c
@ -342,6 +342,11 @@ dispatch_loop:
|
||||
PUSH(mp_load_build_class());
|
||||
DISPATCH();
|
||||
|
||||
ENTRY(MP_BC_LOAD_SUBSCR):
|
||||
obj1 = POP();
|
||||
SET_TOP(mp_obj_subscr(TOP(), obj1, MP_OBJ_SENTINEL));
|
||||
DISPATCH();
|
||||
|
||||
ENTRY(MP_BC_STORE_FAST_0):
|
||||
fastn[0] = POP();
|
||||
DISPATCH();
|
||||
@ -381,7 +386,7 @@ dispatch_loop:
|
||||
DISPATCH();
|
||||
|
||||
ENTRY(MP_BC_STORE_SUBSCR):
|
||||
mp_store_subscr(sp[-1], sp[0], sp[-2]);
|
||||
mp_obj_subscr(sp[-1], sp[0], sp[-2]);
|
||||
sp -= 3;
|
||||
DISPATCH();
|
||||
|
||||
|
@ -21,6 +21,7 @@ static void* entry_table[256] = {
|
||||
[MP_BC_LOAD_ATTR] = &&entry_MP_BC_LOAD_ATTR,
|
||||
[MP_BC_LOAD_METHOD] = &&entry_MP_BC_LOAD_METHOD,
|
||||
[MP_BC_LOAD_BUILD_CLASS] = &&entry_MP_BC_LOAD_BUILD_CLASS,
|
||||
[MP_BC_LOAD_SUBSCR] = &&entry_MP_BC_LOAD_SUBSCR,
|
||||
[MP_BC_STORE_FAST_0] = &&entry_MP_BC_STORE_FAST_0,
|
||||
[MP_BC_STORE_FAST_1] = &&entry_MP_BC_STORE_FAST_1,
|
||||
[MP_BC_STORE_FAST_2] = &&entry_MP_BC_STORE_FAST_2,
|
||||
|
Loading…
Reference in New Issue
Block a user