blob: fd5e37dfe06b723925efe45f133d8aa62f75b639 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#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 Georgeeb7bfcb2014-01-04 15:57:35 +00009#include "mpqstr.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "obj.h"
11#include "runtime0.h"
12#include "runtime.h"
13#include "map.h"
14
15typedef struct _mp_obj_dict_t {
16 mp_obj_base_t base;
17 mp_map_t map;
18} mp_obj_dict_t;
19
John R. Lentona41fe312014-01-06 17:17:02 +000020static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
21static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
22
Damien George71c51812014-01-04 20:21:15 +000023static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
Damiend99b0522013-12-21 18:17:45 +000024 mp_obj_dict_t *self = self_in;
25 bool first = true;
26 print(env, "{");
John R. Lentona41fe312014-01-06 17:17:02 +000027 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, ", ");
Damiend99b0522013-12-21 18:17:45 +000032 }
John R. Lentona41fe312014-01-06 17:17:02 +000033 first = false;
34 mp_obj_print_helper(print, env, next->key);
35 print(env, ": ");
36 mp_obj_print_helper(print, env, next->value);
Damiend99b0522013-12-21 18:17:45 +000037 }
38 print(env, "}");
39}
40
Damien George71c51812014-01-04 20:21:15 +000041// args are reverse in the array
42static 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
47static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damiend99b0522013-12-21 18:17:45 +000048 mp_obj_dict_t *o = lhs_in;
49 switch (op) {
50 case RT_BINARY_OP_SUBSCR:
51 {
52 // dict load
53 mp_map_elem_t *elem = mp_map_lookup_helper(&o->map, rhs_in, false);
54 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000055 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "<value>"));
Damiend99b0522013-12-21 18:17:45 +000056 } else {
57 return elem->value;
58 }
59 }
60 default:
61 // op not supported
62 return NULL;
63 }
64}
65
John R. Lentona41fe312014-01-06 17:17:02 +000066
67/******************************************************************************/
68/* dict iterator */
69
70typedef 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
76static 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
91mp_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
101static 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
108static 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
116static 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. Lenton4ce6cea2014-01-06 17:38:47 +0000123static 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. Lenton7d21d512014-01-06 17:54:04 +0000131static MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
132
John R. Lentond90b19e2014-01-06 18:11:20 +0000133static 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}
141static MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000142
John R. Lentoncd088732014-01-06 18:53:16 +0000143static mp_obj_t dict_get(int n_args, const mp_obj_t *args) {
144 assert(2 <= n_args && n_args <= 3);
145 assert(MP_OBJ_IS_TYPE(args[0], &dict_type));
146
147 mp_map_elem_t *elem = mp_map_lookup_helper(&((mp_obj_dict_t *)args[0])->map,
148 args[1], false);
149 if (elem == NULL) {
150 return n_args >= 3 ? args[2] : mp_const_none;
151 } else {
152 return elem->value;
153 }
154}
155static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
156
John R. Lentona41fe312014-01-06 17:17:02 +0000157/******************************************************************************/
158/* dict constructors & etc */
159
Damiend99b0522013-12-21 18:17:45 +0000160const mp_obj_type_t dict_type = {
161 { &mp_const_type },
162 "dict",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200163 .print = dict_print,
164 .make_new = dict_make_new,
165 .binary_op = dict_binary_op,
John R. Lentona41fe312014-01-06 17:17:02 +0000166 .getiter = dict_getiter,
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000167 .methods = {
168 { "clear", &dict_clear_obj },
John R. Lentond90b19e2014-01-06 18:11:20 +0000169 { "copy", &dict_copy_obj },
John R. Lentoncd088732014-01-06 18:53:16 +0000170 { "get", &dict_get_obj },
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000171 { NULL, NULL }, // end-of-list sentinel
172 },
Damiend99b0522013-12-21 18:17:45 +0000173};
174
175mp_obj_t mp_obj_new_dict(int n_args) {
176 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
177 o->base.type = &dict_type;
178 mp_map_init(&o->map, MP_MAP_OBJ, n_args);
179 return o;
180}
Damiendae7eb72013-12-29 22:32:51 +0000181
182uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000183 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000184}
185
186mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
187 assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
188 mp_obj_dict_t *self = self_in;
189 mp_map_lookup_helper(&self->map, key, true)->value = value;
190 return self_in;
191}