Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.

Dependents:   micropython-repl

This a port of MicroPython to the mbed Classic platform.

This provides an interpreter running on the board's USB serial connection.

Getting Started

Import the micropython-repl program into your IDE workspace on developer.mbed.org. Compile and download to your board. Connect to the USB serial port in your usual manner. You should get a startup message similar to the following:

  MicroPython v1.7-155-gdddcdd8 on 2016-04-23; K64F with ARM
  Type "help()" for more information.
  >>>

Then you can start using micropython. For example:

  >>> from mbed import DigitalOut
  >>> from pins import LED1
  >>> led = DigitalOut(LED1)
  >>> led.write(1)

Requirements

You need approximately 100K of flash memory, so this will be no good for boards with smaller amounts of storage.

Caveats

This can be considered an alpha release of the port; things may not work; APIs may change in later releases. It is NOT an official part part the micropython project, so if anything doesn't work, blame me. If it does work, most of the credit is due to micropython.

  • Only a few of the mbed classes are available in micropython so far, and not all methods of those that are.
  • Only a few boards have their full range of pin names available; for others, only a few standard ones (USBTX, USBRX, LED1) are implemented.
  • The garbage collector is not yet implemented. The interpreter will gradually consume memory and then fail.
  • Exceptions from the mbed classes are not yet handled.
  • Asynchronous processing (e.g. events on inputs) is not supported.

Credits

  • Damien P. George and other contributors who created micropython.
  • Colin Hogben, author of this port.
Committer:
Colin Hogben
Date:
Wed Apr 27 22:11:29 2016 +0100
Revision:
10:33521d742af1
Parent:
0:5868e8752d44
Update README and version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pythontech 0:5868e8752d44 1 /*
pythontech 0:5868e8752d44 2 * This file is part of the Micro Python project, http://micropython.org/
pythontech 0:5868e8752d44 3 *
pythontech 0:5868e8752d44 4 * The MIT License (MIT)
pythontech 0:5868e8752d44 5 *
pythontech 0:5868e8752d44 6 * Copyright (c) 2013, 2014 Damien P. George
pythontech 0:5868e8752d44 7 *
pythontech 0:5868e8752d44 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 9 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 10 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 12 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 13 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 14 *
pythontech 0:5868e8752d44 15 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 16 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 17 *
pythontech 0:5868e8752d44 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 24 * THE SOFTWARE.
pythontech 0:5868e8752d44 25 */
pythontech 0:5868e8752d44 26
pythontech 0:5868e8752d44 27 #include <string.h>
pythontech 0:5868e8752d44 28 #include <assert.h>
pythontech 0:5868e8752d44 29
pythontech 0:5868e8752d44 30 #include "py/nlr.h"
pythontech 0:5868e8752d44 31 #include "py/objtuple.h"
pythontech 0:5868e8752d44 32 #include "py/runtime0.h"
pythontech 0:5868e8752d44 33 #include "py/runtime.h"
pythontech 0:5868e8752d44 34
pythontech 0:5868e8752d44 35 STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, mp_uint_t cur);
pythontech 0:5868e8752d44 36
pythontech 0:5868e8752d44 37 /******************************************************************************/
pythontech 0:5868e8752d44 38 /* tuple */
pythontech 0:5868e8752d44 39
pythontech 0:5868e8752d44 40 void mp_obj_tuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
pythontech 0:5868e8752d44 41 mp_obj_tuple_t *o = MP_OBJ_TO_PTR(o_in);
pythontech 0:5868e8752d44 42 if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
pythontech 0:5868e8752d44 43 mp_print_str(print, "[");
pythontech 0:5868e8752d44 44 } else {
pythontech 0:5868e8752d44 45 mp_print_str(print, "(");
pythontech 0:5868e8752d44 46 kind = PRINT_REPR;
pythontech 0:5868e8752d44 47 }
pythontech 0:5868e8752d44 48 for (mp_uint_t i = 0; i < o->len; i++) {
pythontech 0:5868e8752d44 49 if (i > 0) {
pythontech 0:5868e8752d44 50 mp_print_str(print, ", ");
pythontech 0:5868e8752d44 51 }
pythontech 0:5868e8752d44 52 mp_obj_print_helper(print, o->items[i], kind);
pythontech 0:5868e8752d44 53 }
pythontech 0:5868e8752d44 54 if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
pythontech 0:5868e8752d44 55 mp_print_str(print, "]");
pythontech 0:5868e8752d44 56 } else {
pythontech 0:5868e8752d44 57 if (o->len == 1) {
pythontech 0:5868e8752d44 58 mp_print_str(print, ",");
pythontech 0:5868e8752d44 59 }
pythontech 0:5868e8752d44 60 mp_print_str(print, ")");
pythontech 0:5868e8752d44 61 }
pythontech 0:5868e8752d44 62 }
pythontech 0:5868e8752d44 63
pythontech 0:5868e8752d44 64 STATIC mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
pythontech 0:5868e8752d44 65 (void)type_in;
pythontech 0:5868e8752d44 66
pythontech 0:5868e8752d44 67 mp_arg_check_num(n_args, n_kw, 0, 1, false);
pythontech 0:5868e8752d44 68
pythontech 0:5868e8752d44 69 switch (n_args) {
pythontech 0:5868e8752d44 70 case 0:
pythontech 0:5868e8752d44 71 // return a empty tuple
pythontech 0:5868e8752d44 72 return mp_const_empty_tuple;
pythontech 0:5868e8752d44 73
pythontech 0:5868e8752d44 74 case 1:
pythontech 0:5868e8752d44 75 default: {
pythontech 0:5868e8752d44 76 // 1 argument, an iterable from which we make a new tuple
pythontech 0:5868e8752d44 77 if (MP_OBJ_IS_TYPE(args[0], &mp_type_tuple)) {
pythontech 0:5868e8752d44 78 return args[0];
pythontech 0:5868e8752d44 79 }
pythontech 0:5868e8752d44 80
pythontech 0:5868e8752d44 81 // TODO optimise for cases where we know the length of the iterator
pythontech 0:5868e8752d44 82
pythontech 0:5868e8752d44 83 mp_uint_t alloc = 4;
pythontech 0:5868e8752d44 84 mp_uint_t len = 0;
pythontech 0:5868e8752d44 85 mp_obj_t *items = m_new(mp_obj_t, alloc);
pythontech 0:5868e8752d44 86
pythontech 0:5868e8752d44 87 mp_obj_t iterable = mp_getiter(args[0]);
pythontech 0:5868e8752d44 88 mp_obj_t item;
pythontech 0:5868e8752d44 89 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
pythontech 0:5868e8752d44 90 if (len >= alloc) {
pythontech 0:5868e8752d44 91 items = m_renew(mp_obj_t, items, alloc, alloc * 2);
pythontech 0:5868e8752d44 92 alloc *= 2;
pythontech 0:5868e8752d44 93 }
pythontech 0:5868e8752d44 94 items[len++] = item;
pythontech 0:5868e8752d44 95 }
pythontech 0:5868e8752d44 96
pythontech 0:5868e8752d44 97 mp_obj_t tuple = mp_obj_new_tuple(len, items);
pythontech 0:5868e8752d44 98 m_del(mp_obj_t, items, alloc);
pythontech 0:5868e8752d44 99
pythontech 0:5868e8752d44 100 return tuple;
pythontech 0:5868e8752d44 101 }
pythontech 0:5868e8752d44 102 }
pythontech 0:5868e8752d44 103 }
pythontech 0:5868e8752d44 104
pythontech 0:5868e8752d44 105 // Don't pass MP_BINARY_OP_NOT_EQUAL here
pythontech 0:5868e8752d44 106 STATIC bool tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) {
pythontech 0:5868e8752d44 107 mp_obj_type_t *self_type = mp_obj_get_type(self_in);
pythontech 0:5868e8752d44 108 if (self_type->getiter != mp_obj_tuple_getiter) {
pythontech 0:5868e8752d44 109 assert(0);
pythontech 0:5868e8752d44 110 }
pythontech 0:5868e8752d44 111 mp_obj_type_t *another_type = mp_obj_get_type(another_in);
pythontech 0:5868e8752d44 112 mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 113 if (another_type->getiter != mp_obj_tuple_getiter) {
pythontech 0:5868e8752d44 114 // Slow path for user subclasses
pythontech 0:5868e8752d44 115 another_in = mp_instance_cast_to_native_base(another_in, MP_OBJ_FROM_PTR(&mp_type_tuple));
pythontech 0:5868e8752d44 116 if (another_in == MP_OBJ_NULL) {
pythontech 0:5868e8752d44 117 return false;
pythontech 0:5868e8752d44 118 }
pythontech 0:5868e8752d44 119 }
pythontech 0:5868e8752d44 120 mp_obj_tuple_t *another = MP_OBJ_TO_PTR(another_in);
pythontech 0:5868e8752d44 121
pythontech 0:5868e8752d44 122 return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
pythontech 0:5868e8752d44 123 }
pythontech 0:5868e8752d44 124
pythontech 0:5868e8752d44 125 mp_obj_t mp_obj_tuple_unary_op(mp_uint_t op, mp_obj_t self_in) {
pythontech 0:5868e8752d44 126 mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 127 switch (op) {
pythontech 0:5868e8752d44 128 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
pythontech 0:5868e8752d44 129 case MP_UNARY_OP_HASH: {
pythontech 0:5868e8752d44 130 // start hash with pointer to empty tuple, to make it fairly unique
pythontech 0:5868e8752d44 131 mp_int_t hash = (mp_int_t)mp_const_empty_tuple;
pythontech 0:5868e8752d44 132 for (mp_uint_t i = 0; i < self->len; i++) {
pythontech 0:5868e8752d44 133 hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, self->items[i]));
pythontech 0:5868e8752d44 134 }
pythontech 0:5868e8752d44 135 return MP_OBJ_NEW_SMALL_INT(hash);
pythontech 0:5868e8752d44 136 }
pythontech 0:5868e8752d44 137 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
pythontech 0:5868e8752d44 138 default: return MP_OBJ_NULL; // op not supported
pythontech 0:5868e8752d44 139 }
pythontech 0:5868e8752d44 140 }
pythontech 0:5868e8752d44 141
pythontech 0:5868e8752d44 142 mp_obj_t mp_obj_tuple_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
pythontech 0:5868e8752d44 143 mp_obj_tuple_t *o = MP_OBJ_TO_PTR(lhs);
pythontech 0:5868e8752d44 144 switch (op) {
pythontech 0:5868e8752d44 145 case MP_BINARY_OP_ADD: {
pythontech 0:5868e8752d44 146 if (!mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(rhs)), MP_OBJ_FROM_PTR(&mp_type_tuple))) {
pythontech 0:5868e8752d44 147 return MP_OBJ_NULL; // op not supported
pythontech 0:5868e8752d44 148 }
pythontech 0:5868e8752d44 149 mp_obj_tuple_t *p = MP_OBJ_TO_PTR(rhs);
pythontech 0:5868e8752d44 150 mp_obj_tuple_t *s = MP_OBJ_TO_PTR(mp_obj_new_tuple(o->len + p->len, NULL));
pythontech 0:5868e8752d44 151 mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
pythontech 0:5868e8752d44 152 return MP_OBJ_FROM_PTR(s);
pythontech 0:5868e8752d44 153 }
pythontech 0:5868e8752d44 154 case MP_BINARY_OP_MULTIPLY: {
pythontech 0:5868e8752d44 155 mp_int_t n;
pythontech 0:5868e8752d44 156 if (!mp_obj_get_int_maybe(rhs, &n)) {
pythontech 0:5868e8752d44 157 return MP_OBJ_NULL; // op not supported
pythontech 0:5868e8752d44 158 }
pythontech 0:5868e8752d44 159 if (n <= 0) {
pythontech 0:5868e8752d44 160 return mp_const_empty_tuple;
pythontech 0:5868e8752d44 161 }
pythontech 0:5868e8752d44 162 mp_obj_tuple_t *s = MP_OBJ_TO_PTR(mp_obj_new_tuple(o->len * n, NULL));
pythontech 0:5868e8752d44 163 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
pythontech 0:5868e8752d44 164 return MP_OBJ_FROM_PTR(s);
pythontech 0:5868e8752d44 165 }
pythontech 0:5868e8752d44 166 case MP_BINARY_OP_EQUAL:
pythontech 0:5868e8752d44 167 case MP_BINARY_OP_LESS:
pythontech 0:5868e8752d44 168 case MP_BINARY_OP_LESS_EQUAL:
pythontech 0:5868e8752d44 169 case MP_BINARY_OP_MORE:
pythontech 0:5868e8752d44 170 case MP_BINARY_OP_MORE_EQUAL:
pythontech 0:5868e8752d44 171 return mp_obj_new_bool(tuple_cmp_helper(op, lhs, rhs));
pythontech 0:5868e8752d44 172
pythontech 0:5868e8752d44 173 default:
pythontech 0:5868e8752d44 174 return MP_OBJ_NULL; // op not supported
pythontech 0:5868e8752d44 175 }
pythontech 0:5868e8752d44 176 }
pythontech 0:5868e8752d44 177
pythontech 0:5868e8752d44 178 mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
pythontech 0:5868e8752d44 179 if (value == MP_OBJ_SENTINEL) {
pythontech 0:5868e8752d44 180 // load
pythontech 0:5868e8752d44 181 mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 182 #if MICROPY_PY_BUILTINS_SLICE
pythontech 0:5868e8752d44 183 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
pythontech 0:5868e8752d44 184 mp_bound_slice_t slice;
pythontech 0:5868e8752d44 185 if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
pythontech 0:5868e8752d44 186 mp_not_implemented("only slices with step=1 (aka None) are supported");
pythontech 0:5868e8752d44 187 }
pythontech 0:5868e8752d44 188 mp_obj_tuple_t *res = MP_OBJ_TO_PTR(mp_obj_new_tuple(slice.stop - slice.start, NULL));
pythontech 0:5868e8752d44 189 mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
pythontech 0:5868e8752d44 190 return MP_OBJ_FROM_PTR(res);
pythontech 0:5868e8752d44 191 }
pythontech 0:5868e8752d44 192 #endif
pythontech 0:5868e8752d44 193 mp_uint_t index_value = mp_get_index(self->base.type, self->len, index, false);
pythontech 0:5868e8752d44 194 return self->items[index_value];
pythontech 0:5868e8752d44 195 } else {
pythontech 0:5868e8752d44 196 return MP_OBJ_NULL; // op not supported
pythontech 0:5868e8752d44 197 }
pythontech 0:5868e8752d44 198 }
pythontech 0:5868e8752d44 199
pythontech 0:5868e8752d44 200 mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in) {
pythontech 0:5868e8752d44 201 return mp_obj_new_tuple_iterator(MP_OBJ_TO_PTR(o_in), 0);
pythontech 0:5868e8752d44 202 }
pythontech 0:5868e8752d44 203
pythontech 0:5868e8752d44 204 STATIC mp_obj_t tuple_count(mp_obj_t self_in, mp_obj_t value) {
pythontech 0:5868e8752d44 205 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
pythontech 0:5868e8752d44 206 mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 207 return mp_seq_count_obj(self->items, self->len, value);
pythontech 0:5868e8752d44 208 }
pythontech 0:5868e8752d44 209 STATIC MP_DEFINE_CONST_FUN_OBJ_2(tuple_count_obj, tuple_count);
pythontech 0:5868e8752d44 210
pythontech 0:5868e8752d44 211 STATIC mp_obj_t tuple_index(size_t n_args, const mp_obj_t *args) {
pythontech 0:5868e8752d44 212 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_tuple));
pythontech 0:5868e8752d44 213 mp_obj_tuple_t *self = MP_OBJ_TO_PTR(args[0]);
pythontech 0:5868e8752d44 214 return mp_seq_index_obj(self->items, self->len, n_args, args);
pythontech 0:5868e8752d44 215 }
pythontech 0:5868e8752d44 216 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tuple_index_obj, 2, 4, tuple_index);
pythontech 0:5868e8752d44 217
pythontech 0:5868e8752d44 218 STATIC const mp_rom_map_elem_t tuple_locals_dict_table[] = {
pythontech 0:5868e8752d44 219 { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&tuple_count_obj) },
pythontech 0:5868e8752d44 220 { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&tuple_index_obj) },
pythontech 0:5868e8752d44 221 };
pythontech 0:5868e8752d44 222
pythontech 0:5868e8752d44 223 STATIC MP_DEFINE_CONST_DICT(tuple_locals_dict, tuple_locals_dict_table);
pythontech 0:5868e8752d44 224
pythontech 0:5868e8752d44 225 const mp_obj_type_t mp_type_tuple = {
pythontech 0:5868e8752d44 226 { &mp_type_type },
pythontech 0:5868e8752d44 227 .name = MP_QSTR_tuple,
pythontech 0:5868e8752d44 228 .print = mp_obj_tuple_print,
pythontech 0:5868e8752d44 229 .make_new = mp_obj_tuple_make_new,
pythontech 0:5868e8752d44 230 .unary_op = mp_obj_tuple_unary_op,
pythontech 0:5868e8752d44 231 .binary_op = mp_obj_tuple_binary_op,
pythontech 0:5868e8752d44 232 .subscr = mp_obj_tuple_subscr,
pythontech 0:5868e8752d44 233 .getiter = mp_obj_tuple_getiter,
pythontech 0:5868e8752d44 234 .locals_dict = (mp_obj_dict_t*)&tuple_locals_dict,
pythontech 0:5868e8752d44 235 };
pythontech 0:5868e8752d44 236
pythontech 0:5868e8752d44 237 // the zero-length tuple
pythontech 0:5868e8752d44 238 const mp_obj_tuple_t mp_const_empty_tuple_obj = {{&mp_type_tuple}, 0};
pythontech 0:5868e8752d44 239
pythontech 0:5868e8752d44 240 mp_obj_t mp_obj_new_tuple(mp_uint_t n, const mp_obj_t *items) {
pythontech 0:5868e8752d44 241 if (n == 0) {
pythontech 0:5868e8752d44 242 return mp_const_empty_tuple;
pythontech 0:5868e8752d44 243 }
pythontech 0:5868e8752d44 244 mp_obj_tuple_t *o = m_new_obj_var(mp_obj_tuple_t, mp_obj_t, n);
pythontech 0:5868e8752d44 245 o->base.type = &mp_type_tuple;
pythontech 0:5868e8752d44 246 o->len = n;
pythontech 0:5868e8752d44 247 if (items) {
pythontech 0:5868e8752d44 248 for (mp_uint_t i = 0; i < n; i++) {
pythontech 0:5868e8752d44 249 o->items[i] = items[i];
pythontech 0:5868e8752d44 250 }
pythontech 0:5868e8752d44 251 }
pythontech 0:5868e8752d44 252 return MP_OBJ_FROM_PTR(o);
pythontech 0:5868e8752d44 253 }
pythontech 0:5868e8752d44 254
pythontech 0:5868e8752d44 255 void mp_obj_tuple_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items) {
pythontech 0:5868e8752d44 256 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
pythontech 0:5868e8752d44 257 mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 258 *len = self->len;
pythontech 0:5868e8752d44 259 *items = &self->items[0];
pythontech 0:5868e8752d44 260 }
pythontech 0:5868e8752d44 261
pythontech 0:5868e8752d44 262 void mp_obj_tuple_del(mp_obj_t self_in) {
pythontech 0:5868e8752d44 263 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
pythontech 0:5868e8752d44 264 mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 265 m_del_var(mp_obj_tuple_t, mp_obj_t, self->len, self);
pythontech 0:5868e8752d44 266 }
pythontech 0:5868e8752d44 267
pythontech 0:5868e8752d44 268 /******************************************************************************/
pythontech 0:5868e8752d44 269 /* tuple iterator */
pythontech 0:5868e8752d44 270
pythontech 0:5868e8752d44 271 typedef struct _mp_obj_tuple_it_t {
pythontech 0:5868e8752d44 272 mp_obj_base_t base;
pythontech 0:5868e8752d44 273 mp_fun_1_t iternext;
pythontech 0:5868e8752d44 274 mp_obj_tuple_t *tuple;
pythontech 0:5868e8752d44 275 mp_uint_t cur;
pythontech 0:5868e8752d44 276 } mp_obj_tuple_it_t;
pythontech 0:5868e8752d44 277
pythontech 0:5868e8752d44 278 STATIC mp_obj_t tuple_it_iternext(mp_obj_t self_in) {
pythontech 0:5868e8752d44 279 mp_obj_tuple_it_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 280 if (self->cur < self->tuple->len) {
pythontech 0:5868e8752d44 281 mp_obj_t o_out = self->tuple->items[self->cur];
pythontech 0:5868e8752d44 282 self->cur += 1;
pythontech 0:5868e8752d44 283 return o_out;
pythontech 0:5868e8752d44 284 } else {
pythontech 0:5868e8752d44 285 return MP_OBJ_STOP_ITERATION;
pythontech 0:5868e8752d44 286 }
pythontech 0:5868e8752d44 287 }
pythontech 0:5868e8752d44 288
pythontech 0:5868e8752d44 289 STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, mp_uint_t cur) {
pythontech 0:5868e8752d44 290 mp_obj_tuple_it_t *o = m_new_obj(mp_obj_tuple_it_t);
pythontech 0:5868e8752d44 291 o->base.type = &mp_type_polymorph_iter;
pythontech 0:5868e8752d44 292 o->iternext = tuple_it_iternext;
pythontech 0:5868e8752d44 293 o->tuple = tuple;
pythontech 0:5868e8752d44 294 o->cur = cur;
pythontech 0:5868e8752d44 295 return MP_OBJ_FROM_PTR(o);
pythontech 0:5868e8752d44 296 }