1 | /* |
2 | * This file is part of the MicroPython project, http://micropython.org/ |
3 | * |
4 | * The MIT License (MIT) |
5 | * |
6 | * Copyright (c) 2013, 2014 Damien P. George |
7 | * |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal |
10 | * in the Software without restriction, including without limitation the rights |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 | * copies of the Software, and to permit persons to whom the Software is |
13 | * furnished to do so, subject to the following conditions: |
14 | * |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. |
17 | * |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 | * THE SOFTWARE. |
25 | */ |
26 | |
27 | #include <stdlib.h> |
28 | |
29 | #include "py/runtime.h" |
30 | |
31 | /******************************************************************************/ |
32 | /* range iterator */ |
33 | |
34 | typedef struct _mp_obj_range_it_t { |
35 | mp_obj_base_t base; |
36 | // TODO make these values generic objects or something |
37 | mp_int_t cur; |
38 | mp_int_t stop; |
39 | mp_int_t step; |
40 | } mp_obj_range_it_t; |
41 | |
42 | STATIC mp_obj_t range_it_iternext(mp_obj_t o_in) { |
43 | mp_obj_range_it_t *o = MP_OBJ_TO_PTR(o_in); |
44 | if ((o->step > 0 && o->cur < o->stop) || (o->step < 0 && o->cur > o->stop)) { |
45 | mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT(o->cur); |
46 | o->cur += o->step; |
47 | return o_out; |
48 | } else { |
49 | return MP_OBJ_STOP_ITERATION; |
50 | } |
51 | } |
52 | |
53 | STATIC const mp_obj_type_t mp_type_range_it = { |
54 | { &mp_type_type }, |
55 | .name = MP_QSTR_iterator, |
56 | .getiter = mp_identity_getiter, |
57 | .iternext = range_it_iternext, |
58 | }; |
59 | |
60 | STATIC mp_obj_t mp_obj_new_range_iterator(mp_int_t cur, mp_int_t stop, mp_int_t step, mp_obj_iter_buf_t *iter_buf) { |
61 | assert(sizeof(mp_obj_range_it_t) <= sizeof(mp_obj_iter_buf_t)); |
62 | mp_obj_range_it_t *o = (mp_obj_range_it_t *)iter_buf; |
63 | o->base.type = &mp_type_range_it; |
64 | o->cur = cur; |
65 | o->stop = stop; |
66 | o->step = step; |
67 | return MP_OBJ_FROM_PTR(o); |
68 | } |
69 | |
70 | /******************************************************************************/ |
71 | /* range */ |
72 | |
73 | typedef struct _mp_obj_range_t { |
74 | mp_obj_base_t base; |
75 | // TODO make these values generic objects or something |
76 | mp_int_t start; |
77 | mp_int_t stop; |
78 | mp_int_t step; |
79 | } mp_obj_range_t; |
80 | |
81 | STATIC void range_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
82 | (void)kind; |
83 | mp_obj_range_t *self = MP_OBJ_TO_PTR(self_in); |
84 | mp_printf(print, "range(" INT_FMT ", " INT_FMT "" , self->start, self->stop); |
85 | if (self->step == 1) { |
86 | mp_print_str(print, ")" ); |
87 | } else { |
88 | mp_printf(print, ", " INT_FMT ")" , self->step); |
89 | } |
90 | } |
91 | |
92 | STATIC mp_obj_t range_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
93 | mp_arg_check_num(n_args, n_kw, 1, 3, false); |
94 | |
95 | mp_obj_range_t *o = m_new_obj(mp_obj_range_t); |
96 | o->base.type = type; |
97 | o->start = 0; |
98 | o->step = 1; |
99 | |
100 | if (n_args == 1) { |
101 | o->stop = mp_obj_get_int(args[0]); |
102 | } else { |
103 | o->start = mp_obj_get_int(args[0]); |
104 | o->stop = mp_obj_get_int(args[1]); |
105 | if (n_args == 3) { |
106 | o->step = mp_obj_get_int(args[2]); |
107 | if (o->step == 0) { |
108 | mp_raise_ValueError(MP_ERROR_TEXT("zero step" )); |
109 | } |
110 | } |
111 | } |
112 | |
113 | return MP_OBJ_FROM_PTR(o); |
114 | } |
115 | |
116 | STATIC mp_int_t range_len(mp_obj_range_t *self) { |
117 | // When computing length, need to take into account step!=1 and step<0. |
118 | mp_int_t len = self->stop - self->start + self->step; |
119 | if (self->step > 0) { |
120 | len -= 1; |
121 | } else { |
122 | len += 1; |
123 | } |
124 | len = len / self->step; |
125 | if (len < 0) { |
126 | len = 0; |
127 | } |
128 | return len; |
129 | } |
130 | |
131 | STATIC mp_obj_t range_unary_op(mp_unary_op_t op, mp_obj_t self_in) { |
132 | mp_obj_range_t *self = MP_OBJ_TO_PTR(self_in); |
133 | mp_int_t len = range_len(self); |
134 | switch (op) { |
135 | case MP_UNARY_OP_BOOL: |
136 | return mp_obj_new_bool(len > 0); |
137 | case MP_UNARY_OP_LEN: |
138 | return MP_OBJ_NEW_SMALL_INT(len); |
139 | default: |
140 | return MP_OBJ_NULL; // op not supported |
141 | } |
142 | } |
143 | |
144 | #if MICROPY_PY_BUILTINS_RANGE_BINOP |
145 | STATIC mp_obj_t range_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
146 | if (!mp_obj_is_type(rhs_in, &mp_type_range) || op != MP_BINARY_OP_EQUAL) { |
147 | return MP_OBJ_NULL; // op not supported |
148 | } |
149 | mp_obj_range_t *lhs = MP_OBJ_TO_PTR(lhs_in); |
150 | mp_obj_range_t *rhs = MP_OBJ_TO_PTR(rhs_in); |
151 | mp_int_t lhs_len = range_len(lhs); |
152 | mp_int_t rhs_len = range_len(rhs); |
153 | return mp_obj_new_bool( |
154 | lhs_len == rhs_len |
155 | && (lhs_len == 0 |
156 | || (lhs->start == rhs->start |
157 | && (lhs_len == 1 || lhs->step == rhs->step))) |
158 | ); |
159 | } |
160 | #endif |
161 | |
162 | STATIC mp_obj_t range_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { |
163 | if (value == MP_OBJ_SENTINEL) { |
164 | // load |
165 | mp_obj_range_t *self = MP_OBJ_TO_PTR(self_in); |
166 | mp_int_t len = range_len(self); |
167 | #if MICROPY_PY_BUILTINS_SLICE |
168 | if (mp_obj_is_type(index, &mp_type_slice)) { |
169 | mp_bound_slice_t slice; |
170 | mp_seq_get_fast_slice_indexes(len, index, &slice); |
171 | mp_obj_range_t *o = m_new_obj(mp_obj_range_t); |
172 | o->base.type = &mp_type_range; |
173 | o->start = self->start + slice.start * self->step; |
174 | o->stop = self->start + slice.stop * self->step; |
175 | o->step = slice.step * self->step; |
176 | if (slice.step < 0) { |
177 | // Negative slice steps have inclusive stop, so adjust for exclusive |
178 | o->stop -= self->step; |
179 | } |
180 | return MP_OBJ_FROM_PTR(o); |
181 | } |
182 | #endif |
183 | size_t index_val = mp_get_index(self->base.type, len, index, false); |
184 | return MP_OBJ_NEW_SMALL_INT(self->start + index_val * self->step); |
185 | } else { |
186 | return MP_OBJ_NULL; // op not supported |
187 | } |
188 | } |
189 | |
190 | STATIC mp_obj_t range_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { |
191 | mp_obj_range_t *o = MP_OBJ_TO_PTR(o_in); |
192 | return mp_obj_new_range_iterator(o->start, o->stop, o->step, iter_buf); |
193 | } |
194 | |
195 | |
196 | #if MICROPY_PY_BUILTINS_RANGE_ATTRS |
197 | STATIC void range_attr(mp_obj_t o_in, qstr attr, mp_obj_t *dest) { |
198 | if (dest[0] != MP_OBJ_NULL) { |
199 | // not load attribute |
200 | return; |
201 | } |
202 | mp_obj_range_t *o = MP_OBJ_TO_PTR(o_in); |
203 | if (attr == MP_QSTR_start) { |
204 | dest[0] = mp_obj_new_int(o->start); |
205 | } else if (attr == MP_QSTR_stop) { |
206 | dest[0] = mp_obj_new_int(o->stop); |
207 | } else if (attr == MP_QSTR_step) { |
208 | dest[0] = mp_obj_new_int(o->step); |
209 | } |
210 | } |
211 | #endif |
212 | |
213 | const mp_obj_type_t mp_type_range = { |
214 | { &mp_type_type }, |
215 | .name = MP_QSTR_range, |
216 | .print = range_print, |
217 | .make_new = range_make_new, |
218 | .unary_op = range_unary_op, |
219 | #if MICROPY_PY_BUILTINS_RANGE_BINOP |
220 | .binary_op = range_binary_op, |
221 | #endif |
222 | .subscr = range_subscr, |
223 | .getiter = range_getiter, |
224 | #if MICROPY_PY_BUILTINS_RANGE_ATTRS |
225 | .attr = range_attr, |
226 | #endif |
227 | }; |
228 | |