Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "nlr.h" |
| 7 | #include "misc.h" |
| 8 | #include "mpconfig.h" |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 9 | #include "mpqstr.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 10 | #include "obj.h" |
| 11 | #include "runtime0.h" |
| 12 | #include "runtime.h" |
| 13 | #include "map.h" |
| 14 | |
| 15 | typedef struct _mp_obj_dict_t { |
| 16 | mp_obj_base_t base; |
| 17 | mp_map_t map; |
| 18 | } mp_obj_dict_t; |
| 19 | |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 20 | static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur); |
| 21 | static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in); |
| 22 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 23 | static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 24 | mp_obj_dict_t *self = self_in; |
| 25 | bool first = true; |
| 26 | print(env, "{"); |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 27 | mp_obj_t *dict_iter = mp_obj_new_dict_iterator(self, 0); |
| 28 | mp_map_elem_t *next = NULL; |
| 29 | while ((next = dict_it_iternext_elem(dict_iter)) != NULL) { |
| 30 | if (!first) { |
| 31 | print(env, ", "); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 32 | } |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 33 | first = false; |
| 34 | mp_obj_print_helper(print, env, next->key); |
| 35 | print(env, ": "); |
| 36 | mp_obj_print_helper(print, env, next->value); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 37 | } |
| 38 | print(env, "}"); |
| 39 | } |
| 40 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 41 | // args are reverse in the array |
| 42 | static mp_obj_t dict_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { |
| 43 | // TODO create from an iterable! |
| 44 | return rt_build_map(0); |
| 45 | } |
| 46 | |
| 47 | static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 48 | mp_obj_dict_t *o = lhs_in; |
| 49 | switch (op) { |
| 50 | case RT_BINARY_OP_SUBSCR: |
| 51 | { |
| 52 | // dict load |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame^] | 53 | mp_map_elem_t *elem = mp_map_lookup_helper(&o->map, rhs_in, false, false); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 54 | if (elem == NULL) { |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 55 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "<value>")); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 56 | } else { |
| 57 | return elem->value; |
| 58 | } |
| 59 | } |
| 60 | default: |
| 61 | // op not supported |
| 62 | return NULL; |
| 63 | } |
| 64 | } |
| 65 | |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 66 | |
| 67 | /******************************************************************************/ |
| 68 | /* dict iterator */ |
| 69 | |
| 70 | typedef struct _mp_obj_dict_it_t { |
| 71 | mp_obj_base_t base; |
| 72 | mp_obj_dict_t *dict; |
| 73 | machine_uint_t cur; |
| 74 | } mp_obj_dict_it_t; |
| 75 | |
| 76 | static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) { |
| 77 | mp_obj_dict_it_t *self = self_in; |
| 78 | machine_uint_t max = self->dict->map.alloc; |
| 79 | mp_map_elem_t *table = self->dict->map.table; |
| 80 | |
| 81 | for (int i = self->cur; i < max; i++) { |
| 82 | if (table[i].key != NULL) { |
| 83 | self->cur = i + 1; |
| 84 | return &(table[i]); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | mp_obj_t dict_it_iternext(mp_obj_t self_in) { |
| 92 | mp_map_elem_t *next = dict_it_iternext_elem(self_in); |
| 93 | |
| 94 | if (next != NULL) { |
| 95 | return next->key; |
| 96 | } else { |
| 97 | return mp_const_stop_iteration; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static const mp_obj_type_t dict_it_type = { |
| 102 | { &mp_const_type }, |
| 103 | "dict_iterator", |
| 104 | .iternext = dict_it_iternext, |
| 105 | .methods = { { NULL, NULL }, }, |
| 106 | }; |
| 107 | |
| 108 | static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) { |
| 109 | mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t); |
| 110 | o->base.type = &dict_it_type; |
| 111 | o->dict = dict; |
| 112 | o->cur = cur; |
| 113 | return o; |
| 114 | } |
| 115 | |
| 116 | static mp_obj_t dict_getiter(mp_obj_t o_in) { |
| 117 | return mp_obj_new_dict_iterator(o_in, 0); |
| 118 | } |
| 119 | |
| 120 | /******************************************************************************/ |
| 121 | /* dict methods */ |
| 122 | |
John R. Lenton | 4ce6cea | 2014-01-06 17:38:47 +0000 | [diff] [blame] | 123 | static mp_obj_t dict_clear(mp_obj_t self_in) { |
| 124 | assert(MP_OBJ_IS_TYPE(self_in, &dict_type)); |
| 125 | mp_obj_dict_t *self = self_in; |
| 126 | |
| 127 | mp_map_clear(&self->map); |
| 128 | |
| 129 | return mp_const_none; |
| 130 | } |
John R. Lenton | 7d21d51 | 2014-01-06 17:54:04 +0000 | [diff] [blame] | 131 | static MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear); |
| 132 | |
John R. Lenton | d90b19e | 2014-01-06 18:11:20 +0000 | [diff] [blame] | 133 | static mp_obj_t dict_copy(mp_obj_t self_in) { |
| 134 | assert(MP_OBJ_IS_TYPE(self_in, &dict_type)); |
| 135 | mp_obj_dict_t *self = self_in; |
| 136 | mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc); |
| 137 | other->map.used = self->map.used; |
| 138 | memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t)); |
| 139 | return other; |
| 140 | } |
| 141 | static MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy); |
John R. Lenton | 4ce6cea | 2014-01-06 17:38:47 +0000 | [diff] [blame] | 142 | |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame^] | 143 | static mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, bool pop) { |
| 144 | mp_map_elem_t *elem = mp_map_lookup_helper(self, key, false, pop); |
| 145 | if (elem == NULL) { |
| 146 | if (deflt == NULL) { |
| 147 | if (pop) { |
| 148 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "<value>")); |
| 149 | } else { |
| 150 | return mp_const_none; |
| 151 | } |
| 152 | } else { |
| 153 | return deflt; |
| 154 | } |
| 155 | } else { |
| 156 | mp_obj_t value = elem->value; |
| 157 | if (pop) { |
| 158 | /* catch the leak (from mp_map_lookup_helper) */ |
| 159 | m_free(elem, 2 * sizeof(mp_obj_t)); |
| 160 | } |
| 161 | return value; |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | |
John R. Lenton | cd08873 | 2014-01-06 18:53:16 +0000 | [diff] [blame] | 166 | static mp_obj_t dict_get(int n_args, const mp_obj_t *args) { |
| 167 | assert(2 <= n_args && n_args <= 3); |
| 168 | assert(MP_OBJ_IS_TYPE(args[0], &dict_type)); |
| 169 | |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame^] | 170 | return dict_get_helper(&((mp_obj_dict_t *)args[0])->map, |
| 171 | args[1], |
| 172 | n_args == 3 ? args[2] : NULL, |
| 173 | false); |
John R. Lenton | cd08873 | 2014-01-06 18:53:16 +0000 | [diff] [blame] | 174 | } |
| 175 | static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get); |
| 176 | |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame^] | 177 | static mp_obj_t dict_pop(int n_args, const mp_obj_t *args) { |
| 178 | assert(2 <= n_args && n_args <= 3); |
| 179 | assert(MP_OBJ_IS_TYPE(args[0], &dict_type)); |
| 180 | |
| 181 | return dict_get_helper(&((mp_obj_dict_t *)args[0])->map, |
| 182 | args[1], |
| 183 | n_args == 3 ? args[2] : NULL, |
| 184 | true); |
| 185 | } |
| 186 | static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop); |
| 187 | |
| 188 | |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 189 | /******************************************************************************/ |
| 190 | /* dict constructors & etc */ |
| 191 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 192 | const mp_obj_type_t dict_type = { |
| 193 | { &mp_const_type }, |
| 194 | "dict", |
Paul Sokolovsky | 860ffb0 | 2014-01-05 22:34:09 +0200 | [diff] [blame] | 195 | .print = dict_print, |
| 196 | .make_new = dict_make_new, |
| 197 | .binary_op = dict_binary_op, |
John R. Lenton | a41fe31 | 2014-01-06 17:17:02 +0000 | [diff] [blame] | 198 | .getiter = dict_getiter, |
John R. Lenton | 4ce6cea | 2014-01-06 17:38:47 +0000 | [diff] [blame] | 199 | .methods = { |
| 200 | { "clear", &dict_clear_obj }, |
John R. Lenton | d90b19e | 2014-01-06 18:11:20 +0000 | [diff] [blame] | 201 | { "copy", &dict_copy_obj }, |
John R. Lenton | cd08873 | 2014-01-06 18:53:16 +0000 | [diff] [blame] | 202 | { "get", &dict_get_obj }, |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame^] | 203 | { "pop", &dict_pop_obj }, |
John R. Lenton | 4ce6cea | 2014-01-06 17:38:47 +0000 | [diff] [blame] | 204 | { NULL, NULL }, // end-of-list sentinel |
| 205 | }, |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | mp_obj_t mp_obj_new_dict(int n_args) { |
| 209 | mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t); |
| 210 | o->base.type = &dict_type; |
| 211 | mp_map_init(&o->map, MP_MAP_OBJ, n_args); |
| 212 | return o; |
| 213 | } |
Damien | dae7eb7 | 2013-12-29 22:32:51 +0000 | [diff] [blame] | 214 | |
| 215 | uint mp_obj_dict_len(mp_obj_t self_in) { |
John R. Lenton | cd08873 | 2014-01-06 18:53:16 +0000 | [diff] [blame] | 216 | return ((mp_obj_dict_t *)self_in)->map.used; |
Damien | dae7eb7 | 2013-12-29 22:32:51 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) { |
| 220 | assert(MP_OBJ_IS_TYPE(self_in, &dict_type)); |
| 221 | mp_obj_dict_t *self = self_in; |
John R. Lenton | 0fcbaa4 | 2014-01-06 19:48:34 +0000 | [diff] [blame^] | 222 | mp_map_lookup_helper(&self->map, key, true, false)->value = value; |
Damien | dae7eb7 | 2013-12-29 22:32:51 +0000 | [diff] [blame] | 223 | return self_in; |
| 224 | } |