blob: dc3662b1037710a2a3e55ffb41453eeb1f345822 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python 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
Damien George66eaf842014-03-26 19:27:58 +000027#include <stdlib.h>
Damiend99b0522013-12-21 18:17:45 +000028
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030029#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000030#include "nlr.h"
31#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000032#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000033#include "obj.h"
Damien Georgebb91f112014-08-12 19:41:18 +010034#include "runtime0.h"
Damien George71d31122014-04-17 18:18:55 +010035#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000036
37/******************************************************************************/
38/* range iterator */
39
40typedef struct _mp_obj_range_it_t {
41 mp_obj_base_t base;
42 // TODO make these values generic objects or something
Damien George40f3c022014-07-03 13:25:24 +010043 mp_int_t cur;
44 mp_int_t stop;
45 mp_int_t step;
Damiend99b0522013-12-21 18:17:45 +000046} mp_obj_range_it_t;
47
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020048STATIC mp_obj_t range_it_iternext(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000049 mp_obj_range_it_t *o = o_in;
50 if ((o->step > 0 && o->cur < o->stop) || (o->step < 0 && o->cur > o->stop)) {
51 mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT(o->cur);
52 o->cur += o->step;
53 return o_out;
54 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +010055 return MP_OBJ_STOP_ITERATION;
Damiend99b0522013-12-21 18:17:45 +000056 }
57}
58
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020059STATIC const mp_obj_type_t range_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +000060 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000061 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +030062 .getiter = mp_identity,
Damien George97209d32014-01-07 15:58:30 +000063 .iternext = range_it_iternext,
Damiend99b0522013-12-21 18:17:45 +000064};
65
66mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step) {
67 mp_obj_range_it_t *o = m_new_obj(mp_obj_range_it_t);
68 o->base.type = &range_it_type;
69 o->cur = cur;
70 o->stop = stop;
71 o->step = step;
72 return o;
73}
Damien George71d31122014-04-17 18:18:55 +010074
75/******************************************************************************/
76/* range */
77
78typedef struct _mp_obj_range_t {
79 mp_obj_base_t base;
80 // TODO make these values generic objects or something
Damien George40f3c022014-07-03 13:25:24 +010081 mp_int_t start;
82 mp_int_t stop;
83 mp_int_t step;
Damien George71d31122014-04-17 18:18:55 +010084} mp_obj_range_t;
85
Damien Georgebb91f112014-08-12 19:41:18 +010086STATIC void range_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
87 mp_obj_range_t *self = self_in;
88 print(env, "range(%d, %d", self->start, self->stop);
89 if (self->step == 1) {
90 print(env, ")");
91 } else {
92 print(env, ", %d)", self->step);
93 }
94}
95
Damien Georgeecc88e92014-08-30 00:35:11 +010096STATIC mp_obj_t range_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien Georgea3f94e02014-04-20 00:13:22 +010097 mp_arg_check_num(n_args, n_kw, 1, 3, false);
Damien George71d31122014-04-17 18:18:55 +010098
99 mp_obj_range_t *o = m_new_obj(mp_obj_range_t);
100 o->base.type = &mp_type_range;
101 o->start = 0;
102 o->step = 1;
103
104 if (n_args == 1) {
105 o->stop = mp_obj_get_int(args[0]);
106 } else {
107 o->start = mp_obj_get_int(args[0]);
108 o->stop = mp_obj_get_int(args[1]);
109 if (n_args == 3) {
Damien Georgebb91f112014-08-12 19:41:18 +0100110 // TODO check step is non-zero
Damien George71d31122014-04-17 18:18:55 +0100111 o->step = mp_obj_get_int(args[2]);
112 }
113 }
114
115 return o;
116}
117
Damien Georgebb91f112014-08-12 19:41:18 +0100118STATIC mp_int_t range_len(mp_obj_range_t *self) {
119 // When computing length, need to take into account step!=1 and step<0.
120 mp_int_t len = self->stop - self->start + self->step;
121 if (self->step > 0) {
122 len -= 1;
123 } else {
124 len += 1;
125 }
126 len = len / self->step;
127 if (len < 0) {
128 len = 0;
129 }
130 return len;
131}
132
Damien Georgeecc88e92014-08-30 00:35:11 +0100133STATIC mp_obj_t range_unary_op(mp_uint_t op, mp_obj_t self_in) {
Damien Georgebb91f112014-08-12 19:41:18 +0100134 mp_obj_range_t *self = self_in;
135 mp_int_t len = range_len(self);
136 switch (op) {
137 case MP_UNARY_OP_BOOL: return MP_BOOL(len > 0);
138 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len);
139 default: return MP_OBJ_NULL; // op not supported
140 }
141}
142
143STATIC mp_obj_t range_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
144 if (value == MP_OBJ_SENTINEL) {
145 // load
146 mp_obj_range_t *self = self_in;
147 mp_int_t len = range_len(self);
148#if MICROPY_PY_BUILTINS_SLICE
149 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
150 mp_bound_slice_t slice;
151 mp_seq_get_fast_slice_indexes(len, index, &slice);
152 mp_obj_range_t *o = m_new_obj(mp_obj_range_t);
153 o->base.type = &mp_type_range;
154 o->start = slice.start;
155 o->stop = slice.stop;
156 o->step = slice.step;
157 return o;
158 }
159#endif
160 uint index_val = mp_get_index(self->base.type, len, index, false);
161 return MP_OBJ_NEW_SMALL_INT(self->start + index_val * self->step);
162 } else {
163 return MP_OBJ_NULL; // op not supported
164 }
165}
166
Damien George71d31122014-04-17 18:18:55 +0100167STATIC mp_obj_t range_getiter(mp_obj_t o_in) {
168 mp_obj_range_t *o = o_in;
169 return mp_obj_new_range_iterator(o->start, o->stop, o->step);
170}
171
172const mp_obj_type_t mp_type_range = {
173 { &mp_type_type },
174 .name = MP_QSTR_range,
Damien Georgebb91f112014-08-12 19:41:18 +0100175 .print = range_print,
Damien George71d31122014-04-17 18:18:55 +0100176 .make_new = range_make_new,
Damien Georgebb91f112014-08-12 19:41:18 +0100177 .unary_op = range_unary_op,
178 .subscr = range_subscr,
Damien George71d31122014-04-17 18:18:55 +0100179 .getiter = range_getiter,
180};