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:
2:c89e95946844
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 #ifndef __MICROPY_INCLUDED_PY_OBJ_H__
pythontech 0:5868e8752d44 27 #define __MICROPY_INCLUDED_PY_OBJ_H__
pythontech 0:5868e8752d44 28
pythontech 0:5868e8752d44 29 #include "py/mpconfig.h"
pythontech 0:5868e8752d44 30 #include "py/misc.h"
pythontech 0:5868e8752d44 31 #include "py/qstr.h"
pythontech 0:5868e8752d44 32 #include "py/mpprint.h"
pythontech 0:5868e8752d44 33
pythontech 0:5868e8752d44 34 // All Micro Python objects are at least this type
pythontech 0:5868e8752d44 35 // The bit-size must be at least pointer size
pythontech 0:5868e8752d44 36
pythontech 0:5868e8752d44 37 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
pythontech 0:5868e8752d44 38 typedef uint64_t mp_obj_t;
pythontech 0:5868e8752d44 39 typedef uint64_t mp_const_obj_t;
pythontech 0:5868e8752d44 40 #else
pythontech 0:5868e8752d44 41 typedef machine_ptr_t mp_obj_t;
pythontech 0:5868e8752d44 42 typedef machine_const_ptr_t mp_const_obj_t;
pythontech 0:5868e8752d44 43 #endif
pythontech 0:5868e8752d44 44
pythontech 0:5868e8752d44 45 // This mp_obj_type_t struct is a concrete MicroPython object which holds info
pythontech 0:5868e8752d44 46 // about a type. See below for actual definition of the struct.
pythontech 0:5868e8752d44 47 typedef struct _mp_obj_type_t mp_obj_type_t;
pythontech 0:5868e8752d44 48
pythontech 0:5868e8752d44 49 // Anything that wants to be a concrete MicroPython object must have mp_obj_base_t
pythontech 0:5868e8752d44 50 // as its first member (small ints, qstr objs and inline floats are not concrete).
pythontech 0:5868e8752d44 51 struct _mp_obj_base_t {
pythontech 0:5868e8752d44 52 const mp_obj_type_t *type MICROPY_OBJ_BASE_ALIGNMENT;
pythontech 0:5868e8752d44 53 };
pythontech 0:5868e8752d44 54 typedef struct _mp_obj_base_t mp_obj_base_t;
pythontech 0:5868e8752d44 55
pythontech 0:5868e8752d44 56 // These fake objects are used to indicate certain things in arguments or return
pythontech 0:5868e8752d44 57 // values, and should only be used when explicitly allowed.
pythontech 0:5868e8752d44 58 //
pythontech 0:5868e8752d44 59 // - MP_OBJ_NULL : used to indicate the absence of an object, or unsupported operation.
pythontech 0:5868e8752d44 60 // - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
pythontech 0:5868e8752d44 61 // - MP_OBJ_SENTINEL : used for various internal purposes where one needs
pythontech 0:5868e8752d44 62 // an object which is unique from all other objects, including MP_OBJ_NULL.
pythontech 0:5868e8752d44 63 //
pythontech 0:5868e8752d44 64 // For debugging purposes they are all different. For non-debug mode, we alias
pythontech 0:5868e8752d44 65 // as many as we can to MP_OBJ_NULL because it's cheaper to load/compare 0.
pythontech 0:5868e8752d44 66
pythontech 0:5868e8752d44 67 #ifdef NDEBUG
pythontech 0:5868e8752d44 68 #define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void*)0))
pythontech 0:5868e8752d44 69 #define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void*)0))
pythontech 0:5868e8752d44 70 #define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void*)4))
pythontech 0:5868e8752d44 71 #else
pythontech 0:5868e8752d44 72 #define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void*)0))
pythontech 0:5868e8752d44 73 #define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void*)4))
pythontech 0:5868e8752d44 74 #define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void*)8))
pythontech 0:5868e8752d44 75 #endif
pythontech 0:5868e8752d44 76
pythontech 0:5868e8752d44 77 // These macros/inline functions operate on objects and depend on the
pythontech 0:5868e8752d44 78 // particular object representation. They are used to query, pack and
pythontech 0:5868e8752d44 79 // unpack small ints, qstrs and full object pointers.
pythontech 0:5868e8752d44 80
pythontech 0:5868e8752d44 81 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
pythontech 0:5868e8752d44 82
pythontech 0:5868e8752d44 83 static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
pythontech 0:5868e8752d44 84 { return ((((mp_int_t)(o)) & 1) != 0); }
pythontech 0:5868e8752d44 85 #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
pythontech 0:5868e8752d44 86 #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_int_t)(small_int)) << 1) | 1))
pythontech 0:5868e8752d44 87
pythontech 0:5868e8752d44 88 static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
pythontech 0:5868e8752d44 89 { return ((((mp_int_t)(o)) & 3) == 2); }
pythontech 0:5868e8752d44 90 #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
pythontech 0:5868e8752d44 91 #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))
pythontech 0:5868e8752d44 92
pythontech 0:5868e8752d44 93 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 94 #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
pythontech 0:5868e8752d44 95 #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
pythontech 0:5868e8752d44 96 extern const struct _mp_obj_float_t mp_const_float_e_obj;
pythontech 0:5868e8752d44 97 extern const struct _mp_obj_float_t mp_const_float_pi_obj;
pythontech 0:5868e8752d44 98
pythontech 0:5868e8752d44 99 #define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
pythontech 0:5868e8752d44 100 mp_float_t mp_obj_float_get(mp_obj_t self_in);
pythontech 0:5868e8752d44 101 mp_obj_t mp_obj_new_float(mp_float_t value);
pythontech 0:5868e8752d44 102 #endif
pythontech 0:5868e8752d44 103
pythontech 0:5868e8752d44 104 static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
pythontech 0:5868e8752d44 105 { return ((((mp_int_t)(o)) & 3) == 0); }
pythontech 0:5868e8752d44 106
pythontech 0:5868e8752d44 107 #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
pythontech 0:5868e8752d44 108
pythontech 0:5868e8752d44 109 static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
pythontech 0:5868e8752d44 110 { return ((((mp_int_t)(o)) & 3) == 1); }
pythontech 0:5868e8752d44 111 #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 2)
pythontech 0:5868e8752d44 112 #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_int_t)(small_int)) << 2) | 1))
pythontech 0:5868e8752d44 113
pythontech 0:5868e8752d44 114 static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
pythontech 0:5868e8752d44 115 { return ((((mp_int_t)(o)) & 3) == 3); }
pythontech 0:5868e8752d44 116 #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
pythontech 0:5868e8752d44 117 #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 3))
pythontech 0:5868e8752d44 118
pythontech 0:5868e8752d44 119 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 120 #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
pythontech 0:5868e8752d44 121 #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
pythontech 0:5868e8752d44 122 extern const struct _mp_obj_float_t mp_const_float_e_obj;
pythontech 0:5868e8752d44 123 extern const struct _mp_obj_float_t mp_const_float_pi_obj;
pythontech 0:5868e8752d44 124
pythontech 0:5868e8752d44 125 #define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
pythontech 0:5868e8752d44 126 mp_float_t mp_obj_float_get(mp_obj_t self_in);
pythontech 0:5868e8752d44 127 mp_obj_t mp_obj_new_float(mp_float_t value);
pythontech 0:5868e8752d44 128 #endif
pythontech 0:5868e8752d44 129
pythontech 0:5868e8752d44 130 static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
pythontech 0:5868e8752d44 131 { return ((((mp_int_t)(o)) & 1) == 0); }
pythontech 0:5868e8752d44 132
pythontech 0:5868e8752d44 133 #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
pythontech 0:5868e8752d44 134
pythontech 0:5868e8752d44 135 static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
pythontech 0:5868e8752d44 136 { return ((((mp_int_t)(o)) & 1) != 0); }
pythontech 0:5868e8752d44 137 #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
pythontech 0:5868e8752d44 138 #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_int_t)(small_int)) << 1) | 1))
pythontech 0:5868e8752d44 139
pythontech 0:5868e8752d44 140 #define mp_const_float_e MP_ROM_PTR((mp_obj_t)(((0x402df854 & ~3) | 2) + 0x80800000))
pythontech 0:5868e8752d44 141 #define mp_const_float_pi MP_ROM_PTR((mp_obj_t)(((0x40490fdb & ~3) | 2) + 0x80800000))
pythontech 0:5868e8752d44 142
pythontech 0:5868e8752d44 143 static inline bool mp_obj_is_float(mp_const_obj_t o)
pythontech 0:5868e8752d44 144 { return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0xff800007) != 0x00000006; }
pythontech 0:5868e8752d44 145 static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
pythontech 0:5868e8752d44 146 union {
pythontech 0:5868e8752d44 147 mp_float_t f;
pythontech 0:5868e8752d44 148 mp_uint_t u;
pythontech 0:5868e8752d44 149 } num = {.u = ((mp_uint_t)o - 0x80800000) & ~3};
pythontech 0:5868e8752d44 150 return num.f;
pythontech 0:5868e8752d44 151 }
pythontech 0:5868e8752d44 152 static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
pythontech 0:5868e8752d44 153 union {
pythontech 0:5868e8752d44 154 mp_float_t f;
pythontech 0:5868e8752d44 155 mp_uint_t u;
pythontech 0:5868e8752d44 156 } num = {.f = f};
pythontech 0:5868e8752d44 157 return (mp_obj_t)(((num.u & ~0x3) | 2) + 0x80800000);
pythontech 0:5868e8752d44 158 }
pythontech 0:5868e8752d44 159
pythontech 0:5868e8752d44 160 static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
pythontech 0:5868e8752d44 161 { return (((mp_uint_t)(o)) & 0xff800007) == 0x00000006; }
pythontech 0:5868e8752d44 162 #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
pythontech 0:5868e8752d44 163 #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 0x00000006))
pythontech 0:5868e8752d44 164
pythontech 0:5868e8752d44 165 static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
pythontech 0:5868e8752d44 166 { return ((((mp_int_t)(o)) & 3) == 0); }
pythontech 0:5868e8752d44 167
pythontech 0:5868e8752d44 168 #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
pythontech 0:5868e8752d44 169
pythontech 0:5868e8752d44 170 static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
pythontech 0:5868e8752d44 171 { return ((((mp_int_t)(o)) & 0xffff000000000000) == 0x0001000000000000); }
pythontech 0:5868e8752d44 172 #define MP_OBJ_SMALL_INT_VALUE(o) (((intptr_t)(o)) >> 1)
pythontech 0:5868e8752d44 173 #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)(((uintptr_t)(small_int)) << 1) | 0x0001000000000001)
pythontech 0:5868e8752d44 174
pythontech 0:5868e8752d44 175 static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
pythontech 0:5868e8752d44 176 { return ((((mp_int_t)(o)) & 0xffff000000000000) == 0x0002000000000000); }
pythontech 0:5868e8752d44 177 #define MP_OBJ_QSTR_VALUE(o) ((((uint32_t)(o)) >> 1) & 0xffffffff)
pythontech 0:5868e8752d44 178 #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 1) | 0x0002000000000001))
pythontech 0:5868e8752d44 179
pythontech 0:5868e8752d44 180 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 181 #define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b125769 + 0x8004000000000000))}
pythontech 0:5868e8752d44 182 #define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
pythontech 0:5868e8752d44 183
pythontech 0:5868e8752d44 184 static inline bool mp_obj_is_float(mp_const_obj_t o) {
pythontech 0:5868e8752d44 185 return ((uint64_t)(o) & 0xfffc000000000000) != 0;
pythontech 0:5868e8752d44 186 }
pythontech 0:5868e8752d44 187 static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
pythontech 0:5868e8752d44 188 union {
pythontech 0:5868e8752d44 189 mp_float_t f;
pythontech 0:5868e8752d44 190 uint64_t r;
pythontech 0:5868e8752d44 191 } num = {.r = o - 0x8004000000000000};
pythontech 0:5868e8752d44 192 return num.f;
pythontech 0:5868e8752d44 193 }
pythontech 0:5868e8752d44 194 static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
pythontech 0:5868e8752d44 195 union {
pythontech 0:5868e8752d44 196 mp_float_t f;
pythontech 0:5868e8752d44 197 uint64_t r;
pythontech 0:5868e8752d44 198 } num = {.f = f};
pythontech 0:5868e8752d44 199 return num.r + 0x8004000000000000;
pythontech 0:5868e8752d44 200 }
pythontech 0:5868e8752d44 201 #endif
pythontech 0:5868e8752d44 202
pythontech 0:5868e8752d44 203 static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
pythontech 0:5868e8752d44 204 { return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000); }
pythontech 0:5868e8752d44 205 #define MP_OBJ_TO_PTR(o) ((void*)(uintptr_t)(o))
pythontech 0:5868e8752d44 206 #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p)))
pythontech 0:5868e8752d44 207
pythontech 0:5868e8752d44 208 // rom object storage needs special handling to widen 32-bit pointer to 64-bits
pythontech 0:5868e8752d44 209 typedef union _mp_rom_obj_t { uint64_t u64; struct { const void *lo, *hi; } u32; } mp_rom_obj_t;
pythontech 0:5868e8752d44 210 #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
pythontech 0:5868e8752d44 211 #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
pythontech 0:5868e8752d44 212 #if MP_ENDIANNESS_LITTLE
pythontech 0:5868e8752d44 213 #define MP_ROM_PTR(p) {.u32 = {.lo = (p), .hi = NULL}}
pythontech 0:5868e8752d44 214 #else
pythontech 0:5868e8752d44 215 #define MP_ROM_PTR(p) {.u32 = {.lo = NULL, .hi = (p)}}
pythontech 0:5868e8752d44 216 #endif
pythontech 0:5868e8752d44 217
pythontech 0:5868e8752d44 218 #endif
pythontech 0:5868e8752d44 219
pythontech 0:5868e8752d44 220 // Macros to convert between mp_obj_t and concrete object types.
pythontech 0:5868e8752d44 221 // These are identity operations in MicroPython, but ability to override
pythontech 0:5868e8752d44 222 // these operations are provided to experiment with other methods of
pythontech 0:5868e8752d44 223 // object representation and memory management.
pythontech 0:5868e8752d44 224
pythontech 0:5868e8752d44 225 // Cast mp_obj_t to object pointer
pythontech 0:5868e8752d44 226 #ifndef MP_OBJ_TO_PTR
pythontech 0:5868e8752d44 227 #define MP_OBJ_TO_PTR(o) ((void*)o)
pythontech 0:5868e8752d44 228 #endif
pythontech 0:5868e8752d44 229
pythontech 0:5868e8752d44 230 // Cast object pointer to mp_obj_t
pythontech 0:5868e8752d44 231 #ifndef MP_OBJ_FROM_PTR
pythontech 0:5868e8752d44 232 #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)p)
pythontech 0:5868e8752d44 233 #endif
pythontech 0:5868e8752d44 234
pythontech 0:5868e8752d44 235 // Macros to create objects that are stored in ROM.
pythontech 0:5868e8752d44 236
pythontech 0:5868e8752d44 237 #ifndef MP_ROM_INT
pythontech 0:5868e8752d44 238 typedef mp_const_obj_t mp_rom_obj_t;
pythontech 0:5868e8752d44 239 #define MP_ROM_INT(i) MP_OBJ_NEW_SMALL_INT(i)
pythontech 0:5868e8752d44 240 #define MP_ROM_QSTR(q) MP_OBJ_NEW_QSTR(q)
pythontech 0:5868e8752d44 241 #define MP_ROM_PTR(p) (p)
pythontech 0:5868e8752d44 242 /* for testing
pythontech 0:5868e8752d44 243 typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
pythontech 0:5868e8752d44 244 #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
pythontech 0:5868e8752d44 245 #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
pythontech 0:5868e8752d44 246 #define MP_ROM_PTR(p) {.o = p}
pythontech 0:5868e8752d44 247 */
pythontech 0:5868e8752d44 248 #endif
pythontech 0:5868e8752d44 249
pythontech 0:5868e8752d44 250 // The macros below are derived from the ones above and are used to
pythontech 0:5868e8752d44 251 // check for more specific object types.
pythontech 0:5868e8752d44 252
pythontech 0:5868e8752d44 253 #define MP_OBJ_IS_TYPE(o, t) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type == (t))) // this does not work for checking int, str or fun; use below macros for that
pythontech 0:5868e8752d44 254 #define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
pythontech 0:5868e8752d44 255 #define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
pythontech 0:5868e8752d44 256 #define MP_OBJ_IS_STR_OR_BYTES(o) (MP_OBJ_IS_QSTR(o) || (MP_OBJ_IS_OBJ(o) && ((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type->binary_op == mp_obj_str_binary_op))
pythontech 0:5868e8752d44 257 #define MP_OBJ_IS_FUN(o) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function))
pythontech 0:5868e8752d44 258
pythontech 0:5868e8752d44 259 // Note: inline functions sometimes use much more code space than the
pythontech 0:5868e8752d44 260 // equivalent macros, depending on the compiler.
pythontech 0:5868e8752d44 261 //static inline bool MP_OBJ_IS_TYPE(mp_const_obj_t o, const mp_obj_type_t *t) { return (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))); } // this does not work for checking a string, use below macro for that
pythontech 0:5868e8752d44 262 //static inline bool MP_OBJ_IS_INT(mp_const_obj_t o) { return (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int)); } // returns true if o is a small int or long int
pythontech 0:5868e8752d44 263 // Need to forward declare these for the inline function to compile.
pythontech 0:5868e8752d44 264 extern const mp_obj_type_t mp_type_int;
pythontech 0:5868e8752d44 265 extern const mp_obj_type_t mp_type_bool;
pythontech 0:5868e8752d44 266 static inline bool mp_obj_is_integer(mp_const_obj_t o) { return MP_OBJ_IS_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_bool); } // returns true if o is bool, small int or long int
pythontech 0:5868e8752d44 267 //static inline bool MP_OBJ_IS_STR(mp_const_obj_t o) { return (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str)); }
pythontech 0:5868e8752d44 268
pythontech 0:5868e8752d44 269
pythontech 0:5868e8752d44 270 // These macros are used to declare and define constant function objects
pythontech 0:5868e8752d44 271 // You can put "static" in front of the definitions to make them local
pythontech 0:5868e8752d44 272
pythontech 0:5868e8752d44 273 #define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_builtin_t obj_name
pythontech 0:5868e8752d44 274
pythontech 0:5868e8752d44 275 #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
pythontech 0:5868e8752d44 276 const mp_obj_fun_builtin_t obj_name = \
pythontech 0:5868e8752d44 277 {{&mp_type_fun_builtin}, false, 0, 0, .fun._0 = fun_name}
pythontech 0:5868e8752d44 278 #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
pythontech 0:5868e8752d44 279 const mp_obj_fun_builtin_t obj_name = \
pythontech 0:5868e8752d44 280 {{&mp_type_fun_builtin}, false, 1, 1, .fun._1 = fun_name}
pythontech 0:5868e8752d44 281 #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
pythontech 0:5868e8752d44 282 const mp_obj_fun_builtin_t obj_name = \
pythontech 0:5868e8752d44 283 {{&mp_type_fun_builtin}, false, 2, 2, .fun._2 = fun_name}
pythontech 0:5868e8752d44 284 #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
pythontech 0:5868e8752d44 285 const mp_obj_fun_builtin_t obj_name = \
pythontech 0:5868e8752d44 286 {{&mp_type_fun_builtin}, false, 3, 3, .fun._3 = fun_name}
pythontech 0:5868e8752d44 287 #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
pythontech 0:5868e8752d44 288 const mp_obj_fun_builtin_t obj_name = \
pythontech 0:5868e8752d44 289 {{&mp_type_fun_builtin}, false, n_args_min, MP_OBJ_FUN_ARGS_MAX, .fun.var = fun_name}
pythontech 0:5868e8752d44 290 #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
pythontech 0:5868e8752d44 291 const mp_obj_fun_builtin_t obj_name = \
pythontech 0:5868e8752d44 292 {{&mp_type_fun_builtin}, false, n_args_min, n_args_max, .fun.var = fun_name}
pythontech 0:5868e8752d44 293 #define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
pythontech 0:5868e8752d44 294 const mp_obj_fun_builtin_t obj_name = \
pythontech 0:5868e8752d44 295 {{&mp_type_fun_builtin}, true, n_args_min, MP_OBJ_FUN_ARGS_MAX, .fun.kw = fun_name}
pythontech 0:5868e8752d44 296
pythontech 0:5868e8752d44 297 // These macros are used to define constant map/dict objects
pythontech 0:5868e8752d44 298 // You can put "static" in front of the definition to make it local
pythontech 0:5868e8752d44 299
pythontech 0:5868e8752d44 300 #define MP_DEFINE_CONST_MAP(map_name, table_name) \
pythontech 0:5868e8752d44 301 const mp_map_t map_name = { \
pythontech 0:5868e8752d44 302 .all_keys_are_qstrs = 1, \
pythontech 0:5868e8752d44 303 .is_fixed = 1, \
pythontech 0:5868e8752d44 304 .is_ordered = 1, \
pythontech 0:5868e8752d44 305 .used = MP_ARRAY_SIZE(table_name), \
pythontech 0:5868e8752d44 306 .alloc = MP_ARRAY_SIZE(table_name), \
pythontech 0:5868e8752d44 307 .table = (mp_map_elem_t*)(mp_rom_map_elem_t*)table_name, \
pythontech 0:5868e8752d44 308 }
pythontech 0:5868e8752d44 309
pythontech 0:5868e8752d44 310 #define MP_DEFINE_CONST_DICT(dict_name, table_name) \
pythontech 0:5868e8752d44 311 const mp_obj_dict_t dict_name = { \
pythontech 0:5868e8752d44 312 .base = {&mp_type_dict}, \
pythontech 0:5868e8752d44 313 .map = { \
pythontech 0:5868e8752d44 314 .all_keys_are_qstrs = 1, \
pythontech 0:5868e8752d44 315 .is_fixed = 1, \
pythontech 0:5868e8752d44 316 .is_ordered = 1, \
pythontech 0:5868e8752d44 317 .used = MP_ARRAY_SIZE(table_name), \
pythontech 0:5868e8752d44 318 .alloc = MP_ARRAY_SIZE(table_name), \
pythontech 0:5868e8752d44 319 .table = (mp_map_elem_t*)(mp_rom_map_elem_t*)table_name, \
pythontech 0:5868e8752d44 320 }, \
pythontech 0:5868e8752d44 321 }
pythontech 0:5868e8752d44 322
pythontech 0:5868e8752d44 323 // These macros are used to declare and define constant staticmethond and classmethod objects
pythontech 0:5868e8752d44 324 // You can put "static" in front of the definitions to make them local
pythontech 0:5868e8752d44 325
pythontech 0:5868e8752d44 326 #define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
pythontech 0:5868e8752d44 327 #define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
pythontech 0:5868e8752d44 328
pythontech 0:5868e8752d44 329 #define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_rom_obj_static_class_method_t obj_name = {{&mp_type_staticmethod}, fun_name}
pythontech 0:5868e8752d44 330 #define MP_DEFINE_CONST_CLASSMETHOD_OBJ(obj_name, fun_name) const mp_rom_obj_static_class_method_t obj_name = {{&mp_type_classmethod}, fun_name}
pythontech 0:5868e8752d44 331
pythontech 0:5868e8752d44 332 // Underlying map/hash table implementation (not dict object or map function)
pythontech 0:5868e8752d44 333
pythontech 0:5868e8752d44 334 typedef struct _mp_map_elem_t {
pythontech 0:5868e8752d44 335 mp_obj_t key;
pythontech 0:5868e8752d44 336 mp_obj_t value;
pythontech 0:5868e8752d44 337 } mp_map_elem_t;
pythontech 0:5868e8752d44 338
pythontech 0:5868e8752d44 339 typedef struct _mp_rom_map_elem_t {
pythontech 0:5868e8752d44 340 mp_rom_obj_t key;
pythontech 0:5868e8752d44 341 mp_rom_obj_t value;
pythontech 0:5868e8752d44 342 } mp_rom_map_elem_t;
pythontech 0:5868e8752d44 343
pythontech 0:5868e8752d44 344 // TODO maybe have a truncated mp_map_t for fixed tables, since alloc=used
pythontech 0:5868e8752d44 345 // put alloc last in the structure, so the truncated version does not need it
pythontech 0:5868e8752d44 346 // this would save 1 ROM word for all ROM objects that have a locals_dict
pythontech 0:5868e8752d44 347 // would also need a trucated dict structure
pythontech 0:5868e8752d44 348
pythontech 0:5868e8752d44 349 typedef struct _mp_map_t {
pythontech 0:5868e8752d44 350 mp_uint_t all_keys_are_qstrs : 1;
pythontech 0:5868e8752d44 351 mp_uint_t is_fixed : 1; // a fixed array that can't be modified; must also be ordered
pythontech 0:5868e8752d44 352 mp_uint_t is_ordered : 1; // an ordered array
pythontech 0:5868e8752d44 353 mp_uint_t used : (8 * sizeof(mp_uint_t) - 3);
pythontech 0:5868e8752d44 354 mp_uint_t alloc;
pythontech 0:5868e8752d44 355 mp_map_elem_t *table;
pythontech 0:5868e8752d44 356 } mp_map_t;
pythontech 0:5868e8752d44 357
pythontech 0:5868e8752d44 358 // mp_set_lookup requires these constants to have the values they do
pythontech 0:5868e8752d44 359 typedef enum _mp_map_lookup_kind_t {
pythontech 0:5868e8752d44 360 MP_MAP_LOOKUP = 0,
pythontech 0:5868e8752d44 361 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND = 1,
pythontech 0:5868e8752d44 362 MP_MAP_LOOKUP_REMOVE_IF_FOUND = 2,
pythontech 0:5868e8752d44 363 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND = 3, // only valid for mp_set_lookup
pythontech 0:5868e8752d44 364 } mp_map_lookup_kind_t;
pythontech 0:5868e8752d44 365
pythontech 0:5868e8752d44 366 extern const mp_map_t mp_const_empty_map;
pythontech 0:5868e8752d44 367
pythontech 0:5868e8752d44 368 static inline bool MP_MAP_SLOT_IS_FILLED(const mp_map_t *map, mp_uint_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); }
pythontech 0:5868e8752d44 369
pythontech 0:5868e8752d44 370 void mp_map_init(mp_map_t *map, mp_uint_t n);
pythontech 0:5868e8752d44 371 void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table);
pythontech 0:5868e8752d44 372 mp_map_t *mp_map_new(mp_uint_t n);
pythontech 0:5868e8752d44 373 void mp_map_deinit(mp_map_t *map);
pythontech 0:5868e8752d44 374 void mp_map_free(mp_map_t *map);
pythontech 0:5868e8752d44 375 mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
pythontech 0:5868e8752d44 376 void mp_map_clear(mp_map_t *map);
pythontech 0:5868e8752d44 377 void mp_map_dump(mp_map_t *map);
pythontech 0:5868e8752d44 378
pythontech 0:5868e8752d44 379 // Underlying set implementation (not set object)
pythontech 0:5868e8752d44 380
pythontech 0:5868e8752d44 381 typedef struct _mp_set_t {
pythontech 0:5868e8752d44 382 mp_uint_t alloc;
pythontech 0:5868e8752d44 383 mp_uint_t used;
pythontech 0:5868e8752d44 384 mp_obj_t *table;
pythontech 0:5868e8752d44 385 } mp_set_t;
pythontech 0:5868e8752d44 386
pythontech 0:5868e8752d44 387 static inline bool MP_SET_SLOT_IS_FILLED(const mp_set_t *set, mp_uint_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); }
pythontech 0:5868e8752d44 388
pythontech 0:5868e8752d44 389 void mp_set_init(mp_set_t *set, mp_uint_t n);
pythontech 0:5868e8752d44 390 mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
pythontech 0:5868e8752d44 391 mp_obj_t mp_set_remove_first(mp_set_t *set);
pythontech 0:5868e8752d44 392 void mp_set_clear(mp_set_t *set);
pythontech 0:5868e8752d44 393
pythontech 0:5868e8752d44 394 // Type definitions for methods
pythontech 0:5868e8752d44 395
pythontech 0:5868e8752d44 396 typedef mp_obj_t (*mp_fun_0_t)(void);
pythontech 0:5868e8752d44 397 typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
pythontech 0:5868e8752d44 398 typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
pythontech 0:5868e8752d44 399 typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
pythontech 0:5868e8752d44 400 typedef mp_obj_t (*mp_fun_var_t)(size_t n, const mp_obj_t *);
pythontech 0:5868e8752d44 401 // mp_fun_kw_t takes mp_map_t* (and not const mp_map_t*) to ease passing
pythontech 0:5868e8752d44 402 // this arg to mp_map_lookup().
pythontech 0:5868e8752d44 403 typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
pythontech 0:5868e8752d44 404
pythontech 0:5868e8752d44 405 typedef enum {
pythontech 0:5868e8752d44 406 PRINT_STR = 0,
pythontech 0:5868e8752d44 407 PRINT_REPR = 1,
pythontech 0:5868e8752d44 408 PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
pythontech 0:5868e8752d44 409 PRINT_JSON = 3,
pythontech 0:5868e8752d44 410 PRINT_RAW = 4, // Special format for printing bytes as an undercorated string
pythontech 0:5868e8752d44 411 PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses
pythontech 0:5868e8752d44 412 } mp_print_kind_t;
pythontech 0:5868e8752d44 413
pythontech 0:5868e8752d44 414 typedef void (*mp_print_fun_t)(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind);
pythontech 0:5868e8752d44 415 typedef mp_obj_t (*mp_make_new_fun_t)(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
pythontech 0:5868e8752d44 416 typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args);
pythontech 0:5868e8752d44 417 typedef mp_obj_t (*mp_unary_op_fun_t)(mp_uint_t op, mp_obj_t);
pythontech 0:5868e8752d44 418 typedef mp_obj_t (*mp_binary_op_fun_t)(mp_uint_t op, mp_obj_t, mp_obj_t);
pythontech 0:5868e8752d44 419 typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
pythontech 0:5868e8752d44 420 typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
pythontech 0:5868e8752d44 421
pythontech 0:5868e8752d44 422 // Buffer protocol
pythontech 0:5868e8752d44 423 typedef struct _mp_buffer_info_t {
pythontech 0:5868e8752d44 424 // if we'd bother to support various versions of structure
pythontech 0:5868e8752d44 425 // (with different number of fields), we can distinguish
pythontech 0:5868e8752d44 426 // them with ver = sizeof(struct). Cons: overkill for *micro*?
pythontech 0:5868e8752d44 427 //int ver; // ?
pythontech 0:5868e8752d44 428
pythontech 0:5868e8752d44 429 void *buf; // can be NULL if len == 0
pythontech 0:5868e8752d44 430 size_t len; // in bytes
pythontech 0:5868e8752d44 431 int typecode; // as per binary.h
pythontech 0:5868e8752d44 432
pythontech 0:5868e8752d44 433 // Rationale: to load arbitrary-sized sprites directly to LCD
pythontech 0:5868e8752d44 434 // Cons: a bit adhoc usecase
pythontech 0:5868e8752d44 435 // int stride;
pythontech 0:5868e8752d44 436 } mp_buffer_info_t;
pythontech 0:5868e8752d44 437 #define MP_BUFFER_READ (1)
pythontech 0:5868e8752d44 438 #define MP_BUFFER_WRITE (2)
pythontech 0:5868e8752d44 439 #define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
pythontech 0:5868e8752d44 440 typedef struct _mp_buffer_p_t {
pythontech 0:5868e8752d44 441 mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
pythontech 0:5868e8752d44 442 } mp_buffer_p_t;
pythontech 0:5868e8752d44 443 bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
pythontech 0:5868e8752d44 444 void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
pythontech 0:5868e8752d44 445
pythontech 0:5868e8752d44 446 // Stream protocol
pythontech 0:5868e8752d44 447 typedef struct _mp_stream_p_t {
pythontech 0:5868e8752d44 448 // On error, functions should return MP_STREAM_ERROR and fill in *errcode (values
pythontech 0:5868e8752d44 449 // are implementation-dependent, but will be exposed to user, e.g. via exception).
pythontech 0:5868e8752d44 450 mp_uint_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
pythontech 0:5868e8752d44 451 mp_uint_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode);
pythontech 0:5868e8752d44 452 mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode);
pythontech 0:5868e8752d44 453 mp_uint_t is_text : 1; // default is bytes, set this for text stream
pythontech 0:5868e8752d44 454 } mp_stream_p_t;
pythontech 0:5868e8752d44 455
pythontech 0:5868e8752d44 456 struct _mp_obj_type_t {
pythontech 0:5868e8752d44 457 mp_obj_base_t base;
pythontech 0:5868e8752d44 458 qstr name;
pythontech 0:5868e8752d44 459 mp_print_fun_t print;
pythontech 0:5868e8752d44 460 mp_make_new_fun_t make_new; // to make an instance of the type
pythontech 0:5868e8752d44 461
pythontech 0:5868e8752d44 462 mp_call_fun_t call;
pythontech 0:5868e8752d44 463 mp_unary_op_fun_t unary_op; // can return MP_OBJ_NULL if op not supported
pythontech 0:5868e8752d44 464 mp_binary_op_fun_t binary_op; // can return MP_OBJ_NULL if op not supported
pythontech 0:5868e8752d44 465
pythontech 0:5868e8752d44 466 // implements load, store and delete attribute
pythontech 0:5868e8752d44 467 //
pythontech 0:5868e8752d44 468 // dest[0] = MP_OBJ_NULL means load
pythontech 0:5868e8752d44 469 // return: for fail, do nothing
pythontech 0:5868e8752d44 470 // for attr, dest[0] = value
pythontech 0:5868e8752d44 471 // for method, dest[0] = method, dest[1] = self
pythontech 0:5868e8752d44 472 //
pythontech 0:5868e8752d44 473 // dest[0,1] = {MP_OBJ_SENTINEL, MP_OBJ_NULL} means delete
pythontech 0:5868e8752d44 474 // dest[0,1] = {MP_OBJ_SENTINEL, object} means store
pythontech 0:5868e8752d44 475 // return: for fail, do nothing
pythontech 0:5868e8752d44 476 // for success set dest[0] = MP_OBJ_NULL
pythontech 0:5868e8752d44 477 mp_attr_fun_t attr;
pythontech 0:5868e8752d44 478
pythontech 0:5868e8752d44 479 mp_subscr_fun_t subscr; // implements load, store, delete subscripting
pythontech 0:5868e8752d44 480 // value=MP_OBJ_NULL means delete, value=MP_OBJ_SENTINEL means load, else store
pythontech 0:5868e8752d44 481 // can return MP_OBJ_NULL if op not supported
pythontech 0:5868e8752d44 482
pythontech 0:5868e8752d44 483 mp_fun_1_t getiter; // corresponds to __iter__ special method
pythontech 0:5868e8752d44 484 mp_fun_1_t iternext; // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration() (with no args)
pythontech 0:5868e8752d44 485
pythontech 0:5868e8752d44 486 mp_buffer_p_t buffer_p;
pythontech 0:5868e8752d44 487 const mp_stream_p_t *stream_p;
pythontech 0:5868e8752d44 488
pythontech 0:5868e8752d44 489 // these are for dynamically created types (classes)
pythontech 0:5868e8752d44 490 struct _mp_obj_tuple_t *bases_tuple;
pythontech 0:5868e8752d44 491 struct _mp_obj_dict_t *locals_dict;
pythontech 0:5868e8752d44 492
pythontech 0:5868e8752d44 493 /*
pythontech 0:5868e8752d44 494 What we might need to add here:
pythontech 0:5868e8752d44 495
pythontech 0:5868e8752d44 496 len str tuple list map
pythontech 0:5868e8752d44 497 abs float complex
pythontech 0:5868e8752d44 498 hash bool int none str
pythontech 0:5868e8752d44 499 equal int str
pythontech 0:5868e8752d44 500
pythontech 0:5868e8752d44 501 unpack seq list tuple
pythontech 0:5868e8752d44 502 */
pythontech 0:5868e8752d44 503 };
pythontech 0:5868e8752d44 504
pythontech 0:5868e8752d44 505 // Constant types, globally accessible
pythontech 0:5868e8752d44 506 extern const mp_obj_type_t mp_type_type;
pythontech 0:5868e8752d44 507 extern const mp_obj_type_t mp_type_object;
pythontech 0:5868e8752d44 508 extern const mp_obj_type_t mp_type_NoneType;
pythontech 0:5868e8752d44 509 extern const mp_obj_type_t mp_type_bool;
pythontech 0:5868e8752d44 510 extern const mp_obj_type_t mp_type_int;
pythontech 0:5868e8752d44 511 extern const mp_obj_type_t mp_type_str;
pythontech 0:5868e8752d44 512 extern const mp_obj_type_t mp_type_bytes;
pythontech 0:5868e8752d44 513 extern const mp_obj_type_t mp_type_bytearray;
pythontech 0:5868e8752d44 514 extern const mp_obj_type_t mp_type_memoryview;
pythontech 0:5868e8752d44 515 extern const mp_obj_type_t mp_type_float;
pythontech 0:5868e8752d44 516 extern const mp_obj_type_t mp_type_complex;
pythontech 0:5868e8752d44 517 extern const mp_obj_type_t mp_type_tuple;
pythontech 0:5868e8752d44 518 extern const mp_obj_type_t mp_type_list;
pythontech 0:5868e8752d44 519 extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail)
pythontech 0:5868e8752d44 520 extern const mp_obj_type_t mp_type_enumerate;
pythontech 0:5868e8752d44 521 extern const mp_obj_type_t mp_type_filter;
pythontech 0:5868e8752d44 522 extern const mp_obj_type_t mp_type_dict;
pythontech 0:5868e8752d44 523 extern const mp_obj_type_t mp_type_ordereddict;
pythontech 0:5868e8752d44 524 extern const mp_obj_type_t mp_type_range;
pythontech 0:5868e8752d44 525 extern const mp_obj_type_t mp_type_set;
pythontech 0:5868e8752d44 526 extern const mp_obj_type_t mp_type_frozenset;
pythontech 0:5868e8752d44 527 extern const mp_obj_type_t mp_type_slice;
pythontech 0:5868e8752d44 528 extern const mp_obj_type_t mp_type_zip;
pythontech 0:5868e8752d44 529 extern const mp_obj_type_t mp_type_array;
pythontech 0:5868e8752d44 530 extern const mp_obj_type_t mp_type_super;
pythontech 0:5868e8752d44 531 extern const mp_obj_type_t mp_type_gen_instance;
pythontech 0:5868e8752d44 532 extern const mp_obj_type_t mp_type_fun_builtin;
pythontech 0:5868e8752d44 533 extern const mp_obj_type_t mp_type_fun_bc;
pythontech 0:5868e8752d44 534 extern const mp_obj_type_t mp_type_module;
pythontech 0:5868e8752d44 535 extern const mp_obj_type_t mp_type_staticmethod;
pythontech 0:5868e8752d44 536 extern const mp_obj_type_t mp_type_classmethod;
pythontech 0:5868e8752d44 537 extern const mp_obj_type_t mp_type_property;
pythontech 0:5868e8752d44 538 extern const mp_obj_type_t mp_type_stringio;
pythontech 0:5868e8752d44 539 extern const mp_obj_type_t mp_type_bytesio;
pythontech 0:5868e8752d44 540 extern const mp_obj_type_t mp_type_reversed;
pythontech 0:5868e8752d44 541 extern const mp_obj_type_t mp_type_polymorph_iter;
pythontech 0:5868e8752d44 542
pythontech 0:5868e8752d44 543 // Exceptions
pythontech 0:5868e8752d44 544 extern const mp_obj_type_t mp_type_BaseException;
pythontech 0:5868e8752d44 545 extern const mp_obj_type_t mp_type_ArithmeticError;
pythontech 0:5868e8752d44 546 extern const mp_obj_type_t mp_type_AssertionError;
pythontech 0:5868e8752d44 547 extern const mp_obj_type_t mp_type_AttributeError;
pythontech 0:5868e8752d44 548 extern const mp_obj_type_t mp_type_EOFError;
pythontech 0:5868e8752d44 549 extern const mp_obj_type_t mp_type_Exception;
pythontech 0:5868e8752d44 550 extern const mp_obj_type_t mp_type_GeneratorExit;
pythontech 0:5868e8752d44 551 extern const mp_obj_type_t mp_type_ImportError;
pythontech 0:5868e8752d44 552 extern const mp_obj_type_t mp_type_IndentationError;
pythontech 0:5868e8752d44 553 extern const mp_obj_type_t mp_type_IndexError;
pythontech 0:5868e8752d44 554 extern const mp_obj_type_t mp_type_KeyboardInterrupt;
pythontech 0:5868e8752d44 555 extern const mp_obj_type_t mp_type_KeyError;
pythontech 0:5868e8752d44 556 extern const mp_obj_type_t mp_type_LookupError;
pythontech 0:5868e8752d44 557 extern const mp_obj_type_t mp_type_MemoryError;
pythontech 0:5868e8752d44 558 extern const mp_obj_type_t mp_type_NameError;
pythontech 0:5868e8752d44 559 extern const mp_obj_type_t mp_type_NotImplementedError;
pythontech 0:5868e8752d44 560 extern const mp_obj_type_t mp_type_OSError;
pythontech 0:5868e8752d44 561 extern const mp_obj_type_t mp_type_TimeoutError;
pythontech 0:5868e8752d44 562 extern const mp_obj_type_t mp_type_OverflowError;
pythontech 0:5868e8752d44 563 extern const mp_obj_type_t mp_type_RuntimeError;
Colin Hogben 2:c89e95946844 564 extern const mp_obj_type_t mp_type_StopAsyncIteration;
pythontech 0:5868e8752d44 565 extern const mp_obj_type_t mp_type_StopIteration;
pythontech 0:5868e8752d44 566 extern const mp_obj_type_t mp_type_SyntaxError;
pythontech 0:5868e8752d44 567 extern const mp_obj_type_t mp_type_SystemExit;
pythontech 0:5868e8752d44 568 extern const mp_obj_type_t mp_type_TypeError;
pythontech 0:5868e8752d44 569 extern const mp_obj_type_t mp_type_UnicodeError;
pythontech 0:5868e8752d44 570 extern const mp_obj_type_t mp_type_ValueError;
pythontech 0:5868e8752d44 571 extern const mp_obj_type_t mp_type_ViperTypeError;
pythontech 0:5868e8752d44 572 extern const mp_obj_type_t mp_type_ZeroDivisionError;
pythontech 0:5868e8752d44 573
pythontech 0:5868e8752d44 574 // Constant objects, globally accessible
pythontech 0:5868e8752d44 575 // The macros are for convenience only
pythontech 0:5868e8752d44 576 #define mp_const_none (MP_OBJ_FROM_PTR(&mp_const_none_obj))
pythontech 0:5868e8752d44 577 #define mp_const_false (MP_OBJ_FROM_PTR(&mp_const_false_obj))
pythontech 0:5868e8752d44 578 #define mp_const_true (MP_OBJ_FROM_PTR(&mp_const_true_obj))
pythontech 0:5868e8752d44 579 #define mp_const_empty_bytes (MP_OBJ_FROM_PTR(&mp_const_empty_bytes_obj))
pythontech 0:5868e8752d44 580 #define mp_const_empty_tuple (MP_OBJ_FROM_PTR(&mp_const_empty_tuple_obj))
pythontech 0:5868e8752d44 581 extern const struct _mp_obj_none_t mp_const_none_obj;
pythontech 0:5868e8752d44 582 extern const struct _mp_obj_bool_t mp_const_false_obj;
pythontech 0:5868e8752d44 583 extern const struct _mp_obj_bool_t mp_const_true_obj;
pythontech 0:5868e8752d44 584 extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
pythontech 0:5868e8752d44 585 extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
pythontech 0:5868e8752d44 586 extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj;
pythontech 0:5868e8752d44 587 extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj;
pythontech 0:5868e8752d44 588 extern const struct _mp_obj_exception_t mp_const_MemoryError_obj;
pythontech 0:5868e8752d44 589 extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
pythontech 0:5868e8752d44 590
pythontech 0:5868e8752d44 591 // General API for objects
pythontech 0:5868e8752d44 592
pythontech 0:5868e8752d44 593 mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
pythontech 0:5868e8752d44 594 mp_obj_t mp_obj_new_none(void);
pythontech 0:5868e8752d44 595 static inline mp_obj_t mp_obj_new_bool(mp_int_t x) { return x ? mp_const_true : mp_const_false; }
pythontech 0:5868e8752d44 596 mp_obj_t mp_obj_new_cell(mp_obj_t obj);
pythontech 0:5868e8752d44 597 mp_obj_t mp_obj_new_int(mp_int_t value);
pythontech 0:5868e8752d44 598 mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
pythontech 0:5868e8752d44 599 mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base);
pythontech 0:5868e8752d44 600 mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
pythontech 0:5868e8752d44 601 mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception)
pythontech 0:5868e8752d44 602 mp_obj_t mp_obj_new_str(const char* data, mp_uint_t len, bool make_qstr_if_not_already);
pythontech 0:5868e8752d44 603 mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr);
pythontech 0:5868e8752d44 604 mp_obj_t mp_obj_new_bytes(const byte* data, mp_uint_t len);
pythontech 0:5868e8752d44 605 mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items);
pythontech 0:5868e8752d44 606 mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items);
pythontech 0:5868e8752d44 607 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 608 mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
pythontech 0:5868e8752d44 609 mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
pythontech 0:5868e8752d44 610 #endif
pythontech 0:5868e8752d44 611 mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
pythontech 0:5868e8752d44 612 mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
pythontech 0:5868e8752d44 613 mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, mp_uint_t n_args, const mp_obj_t *args);
pythontech 0:5868e8752d44 614 mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg);
pythontech 0:5868e8752d44 615 mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
pythontech 0:5868e8752d44 616 mp_obj_t mp_obj_new_fun_bc(mp_obj_t def_args, mp_obj_t def_kw_args, const byte *code, const mp_uint_t *const_table);
pythontech 0:5868e8752d44 617 mp_obj_t mp_obj_new_fun_native(mp_obj_t def_args_in, mp_obj_t def_kw_args, const void *fun_data, const mp_uint_t *const_table);
pythontech 0:5868e8752d44 618 mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig);
pythontech 0:5868e8752d44 619 mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig);
pythontech 0:5868e8752d44 620 mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
pythontech 0:5868e8752d44 621 mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_uint_t n_closed, const mp_obj_t *closed);
pythontech 0:5868e8752d44 622 mp_obj_t mp_obj_new_tuple(mp_uint_t n, const mp_obj_t *items);
pythontech 0:5868e8752d44 623 mp_obj_t mp_obj_new_list(mp_uint_t n, mp_obj_t *items);
pythontech 0:5868e8752d44 624 mp_obj_t mp_obj_new_dict(mp_uint_t n_args);
pythontech 0:5868e8752d44 625 mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items);
pythontech 0:5868e8752d44 626 mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
pythontech 0:5868e8752d44 627 mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj);
pythontech 0:5868e8752d44 628 mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
pythontech 0:5868e8752d44 629 mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args);
pythontech 0:5868e8752d44 630 mp_obj_t mp_obj_new_module(qstr module_name);
pythontech 0:5868e8752d44 631 mp_obj_t mp_obj_new_memoryview(byte typecode, mp_uint_t nitems, void *items);
pythontech 0:5868e8752d44 632
pythontech 0:5868e8752d44 633 mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
pythontech 0:5868e8752d44 634 const char *mp_obj_get_type_str(mp_const_obj_t o_in);
pythontech 0:5868e8752d44 635 bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
pythontech 0:5868e8752d44 636 mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type);
pythontech 0:5868e8752d44 637
pythontech 0:5868e8752d44 638 void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
pythontech 0:5868e8752d44 639 void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
pythontech 0:5868e8752d44 640 void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
pythontech 0:5868e8752d44 641
pythontech 0:5868e8752d44 642 bool mp_obj_is_true(mp_obj_t arg);
pythontech 0:5868e8752d44 643 bool mp_obj_is_callable(mp_obj_t o_in);
pythontech 0:5868e8752d44 644 bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
pythontech 0:5868e8752d44 645
pythontech 0:5868e8752d44 646 mp_int_t mp_obj_get_int(mp_const_obj_t arg);
pythontech 0:5868e8752d44 647 mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
pythontech 0:5868e8752d44 648 bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
pythontech 0:5868e8752d44 649 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 650 mp_float_t mp_obj_get_float(mp_obj_t self_in);
pythontech 0:5868e8752d44 651 void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
pythontech 0:5868e8752d44 652 #endif
pythontech 0:5868e8752d44 653 //qstr mp_obj_get_qstr(mp_obj_t arg);
pythontech 0:5868e8752d44 654 void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items);
pythontech 0:5868e8752d44 655 void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items);
pythontech 0:5868e8752d44 656 mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice);
pythontech 0:5868e8752d44 657 mp_obj_t mp_obj_id(mp_obj_t o_in);
pythontech 0:5868e8752d44 658 mp_obj_t mp_obj_len(mp_obj_t o_in);
pythontech 0:5868e8752d44 659 mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
pythontech 0:5868e8752d44 660 mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
pythontech 0:5868e8752d44 661 mp_obj_t mp_generic_unary_op(mp_uint_t op, mp_obj_t o_in);
pythontech 0:5868e8752d44 662
pythontech 0:5868e8752d44 663 // cell
pythontech 0:5868e8752d44 664 mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
pythontech 0:5868e8752d44 665 void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
pythontech 0:5868e8752d44 666
pythontech 0:5868e8752d44 667 // int
pythontech 0:5868e8752d44 668 // For long int, returns value truncated to mp_int_t
pythontech 0:5868e8752d44 669 mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
pythontech 0:5868e8752d44 670 // Will raise exception if value doesn't fit into mp_int_t
pythontech 0:5868e8752d44 671 mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
pythontech 0:5868e8752d44 672 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 673 mp_float_t mp_obj_int_as_float(mp_obj_t self_in);
pythontech 0:5868e8752d44 674 #endif
pythontech 0:5868e8752d44 675
pythontech 0:5868e8752d44 676 // exception
pythontech 0:5868e8752d44 677 #define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
pythontech 0:5868e8752d44 678 bool mp_obj_is_exception_type(mp_obj_t self_in);
pythontech 0:5868e8752d44 679 bool mp_obj_is_exception_instance(mp_obj_t self_in);
pythontech 0:5868e8752d44 680 bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
pythontech 0:5868e8752d44 681 void mp_obj_exception_clear_traceback(mp_obj_t self_in);
pythontech 0:5868e8752d44 682 void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
pythontech 0:5868e8752d44 683 void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
pythontech 0:5868e8752d44 684 mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
pythontech 0:5868e8752d44 685 mp_obj_t mp_obj_exception_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 686 mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
pythontech 0:5868e8752d44 687 void mp_init_emergency_exception_buf(void);
pythontech 0:5868e8752d44 688
pythontech 0:5868e8752d44 689 // str
pythontech 0:5868e8752d44 690 bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
pythontech 0:5868e8752d44 691 qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
pythontech 0:5868e8752d44 692 const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
pythontech 0:5868e8752d44 693 const char *mp_obj_str_get_data(mp_obj_t self_in, mp_uint_t *len);
pythontech 0:5868e8752d44 694 mp_obj_t mp_obj_str_intern(mp_obj_t str);
pythontech 0:5868e8752d44 695 void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, mp_uint_t str_len, bool is_bytes);
pythontech 0:5868e8752d44 696
pythontech 0:5868e8752d44 697 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 698 // float
pythontech 0:5868e8752d44 699 mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
pythontech 0:5868e8752d44 700
pythontech 0:5868e8752d44 701 // complex
pythontech 0:5868e8752d44 702 void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
pythontech 0:5868e8752d44 703 mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NULL if op not supported
pythontech 0:5868e8752d44 704 #else
pythontech 0:5868e8752d44 705 #define mp_obj_is_float(o) (false)
pythontech 0:5868e8752d44 706 #endif
pythontech 0:5868e8752d44 707
pythontech 0:5868e8752d44 708 // tuple
pythontech 0:5868e8752d44 709 void mp_obj_tuple_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items);
pythontech 0:5868e8752d44 710 void mp_obj_tuple_del(mp_obj_t self_in);
pythontech 0:5868e8752d44 711 mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
pythontech 0:5868e8752d44 712
pythontech 0:5868e8752d44 713 // list
pythontech 0:5868e8752d44 714 struct _mp_obj_list_t;
pythontech 0:5868e8752d44 715 void mp_obj_list_init(struct _mp_obj_list_t *o, mp_uint_t n);
pythontech 0:5868e8752d44 716 mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
pythontech 0:5868e8752d44 717 mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value);
pythontech 0:5868e8752d44 718 void mp_obj_list_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items);
pythontech 0:5868e8752d44 719 void mp_obj_list_set_len(mp_obj_t self_in, mp_uint_t len);
pythontech 0:5868e8752d44 720 void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
pythontech 0:5868e8752d44 721 mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
pythontech 0:5868e8752d44 722
pythontech 0:5868e8752d44 723 // dict
pythontech 0:5868e8752d44 724 typedef struct _mp_obj_dict_t {
pythontech 0:5868e8752d44 725 mp_obj_base_t base;
pythontech 0:5868e8752d44 726 mp_map_t map;
pythontech 0:5868e8752d44 727 } mp_obj_dict_t;
pythontech 0:5868e8752d44 728 void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args);
pythontech 0:5868e8752d44 729 mp_uint_t mp_obj_dict_len(mp_obj_t self_in);
pythontech 0:5868e8752d44 730 mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
pythontech 0:5868e8752d44 731 mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
pythontech 0:5868e8752d44 732 mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
pythontech 0:5868e8752d44 733 mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in);
pythontech 0:5868e8752d44 734
pythontech 0:5868e8752d44 735 // set
pythontech 0:5868e8752d44 736 void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
pythontech 0:5868e8752d44 737
pythontech 0:5868e8752d44 738 // slice
pythontech 0:5868e8752d44 739 void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step);
pythontech 0:5868e8752d44 740
pythontech 0:5868e8752d44 741 // functions
pythontech 0:5868e8752d44 742 #define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
pythontech 0:5868e8752d44 743 typedef struct _mp_obj_fun_builtin_t { // use this to make const objects that go in ROM
pythontech 0:5868e8752d44 744 mp_obj_base_t base;
pythontech 0:5868e8752d44 745 bool is_kw : 1;
pythontech 0:5868e8752d44 746 mp_uint_t n_args_min : 15; // inclusive
pythontech 0:5868e8752d44 747 mp_uint_t n_args_max : 16; // inclusive
pythontech 0:5868e8752d44 748 union {
pythontech 0:5868e8752d44 749 mp_fun_0_t _0;
pythontech 0:5868e8752d44 750 mp_fun_1_t _1;
pythontech 0:5868e8752d44 751 mp_fun_2_t _2;
pythontech 0:5868e8752d44 752 mp_fun_3_t _3;
pythontech 0:5868e8752d44 753 mp_fun_var_t var;
pythontech 0:5868e8752d44 754 mp_fun_kw_t kw;
pythontech 0:5868e8752d44 755 } fun;
pythontech 0:5868e8752d44 756 } mp_obj_fun_builtin_t;
pythontech 0:5868e8752d44 757
pythontech 0:5868e8752d44 758 qstr mp_obj_fun_get_name(mp_const_obj_t fun);
pythontech 0:5868e8752d44 759 qstr mp_obj_code_get_name(const byte *code_info);
pythontech 0:5868e8752d44 760
pythontech 0:5868e8752d44 761 mp_obj_t mp_identity(mp_obj_t self);
pythontech 0:5868e8752d44 762 MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj);
pythontech 0:5868e8752d44 763
pythontech 0:5868e8752d44 764 // module
pythontech 0:5868e8752d44 765 typedef struct _mp_obj_module_t {
pythontech 0:5868e8752d44 766 mp_obj_base_t base;
pythontech 0:5868e8752d44 767 qstr name;
pythontech 0:5868e8752d44 768 mp_obj_dict_t *globals;
pythontech 0:5868e8752d44 769 } mp_obj_module_t;
pythontech 0:5868e8752d44 770 mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in);
pythontech 0:5868e8752d44 771 // check if given module object is a package
pythontech 0:5868e8752d44 772 bool mp_obj_is_package(mp_obj_t module);
pythontech 0:5868e8752d44 773
pythontech 0:5868e8752d44 774 // staticmethod and classmethod types; defined here so we can make const versions
pythontech 0:5868e8752d44 775 // this structure is used for instances of both staticmethod and classmethod
pythontech 0:5868e8752d44 776 typedef struct _mp_obj_static_class_method_t {
pythontech 0:5868e8752d44 777 mp_obj_base_t base;
pythontech 0:5868e8752d44 778 mp_obj_t fun;
pythontech 0:5868e8752d44 779 } mp_obj_static_class_method_t;
pythontech 0:5868e8752d44 780 typedef struct _mp_rom_obj_static_class_method_t {
pythontech 0:5868e8752d44 781 mp_obj_base_t base;
pythontech 0:5868e8752d44 782 mp_rom_obj_t fun;
pythontech 0:5868e8752d44 783 } mp_rom_obj_static_class_method_t;
pythontech 0:5868e8752d44 784
pythontech 0:5868e8752d44 785 // property
pythontech 0:5868e8752d44 786 const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
pythontech 0:5868e8752d44 787
pythontech 0:5868e8752d44 788 // sequence helpers
pythontech 0:5868e8752d44 789
pythontech 0:5868e8752d44 790 // slice indexes resolved to particular sequence
pythontech 0:5868e8752d44 791 typedef struct {
pythontech 0:5868e8752d44 792 mp_uint_t start;
pythontech 0:5868e8752d44 793 mp_uint_t stop;
pythontech 0:5868e8752d44 794 mp_int_t step;
pythontech 0:5868e8752d44 795 } mp_bound_slice_t;
pythontech 0:5868e8752d44 796
pythontech 0:5868e8752d44 797 void mp_seq_multiply(const void *items, mp_uint_t item_sz, mp_uint_t len, mp_uint_t times, void *dest);
pythontech 0:5868e8752d44 798 #if MICROPY_PY_BUILTINS_SLICE
pythontech 0:5868e8752d44 799 bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
pythontech 0:5868e8752d44 800 #endif
pythontech 0:5868e8752d44 801 #define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
pythontech 0:5868e8752d44 802 #define mp_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, (len1) * sizeof(item_t)); memcpy(dest + (len1), src2, (len2) * sizeof(item_t)); }
pythontech 0:5868e8752d44 803 bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, mp_uint_t len1, const byte *data2, mp_uint_t len2);
pythontech 0:5868e8752d44 804 bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, mp_uint_t len1, const mp_obj_t *items2, mp_uint_t len2);
pythontech 0:5868e8752d44 805 mp_obj_t mp_seq_index_obj(const mp_obj_t *items, mp_uint_t len, mp_uint_t n_args, const mp_obj_t *args);
pythontech 0:5868e8752d44 806 mp_obj_t mp_seq_count_obj(const mp_obj_t *items, mp_uint_t len, mp_obj_t value);
pythontech 0:5868e8752d44 807 mp_obj_t mp_seq_extract_slice(mp_uint_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
pythontech 0:5868e8752d44 808 // Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
pythontech 0:5868e8752d44 809 #define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte*)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
pythontech 0:5868e8752d44 810 #define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \
pythontech 0:5868e8752d44 811 /*printf("memcpy(%p, %p, %d)\n", dest + beg, slice, slice_len * (item_sz));*/ \
pythontech 0:5868e8752d44 812 memcpy(((char*)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
pythontech 0:5868e8752d44 813 /*printf("memmove(%p, %p, %d)\n", dest + (beg + slice_len), dest + end, (dest_len - end) * (item_sz));*/ \
pythontech 0:5868e8752d44 814 memmove(((char*)dest) + (beg + slice_len) * (item_sz), ((char*)dest) + (end) * (item_sz), (dest_len - end) * (item_sz));
pythontech 0:5868e8752d44 815
pythontech 0:5868e8752d44 816 #define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \
pythontech 0:5868e8752d44 817 /*printf("memmove(%p, %p, %d)\n", dest + beg + len_adj, dest + beg, (dest_len - beg) * (item_sz));*/ \
pythontech 0:5868e8752d44 818 memmove(((char*)dest) + (beg + len_adj) * (item_sz), ((char*)dest) + (beg) * (item_sz), (dest_len - beg) * (item_sz)); \
pythontech 0:5868e8752d44 819 memcpy(((char*)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
pythontech 0:5868e8752d44 820
pythontech 0:5868e8752d44 821 #endif // __MICROPY_INCLUDED_PY_OBJ_H__