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:
pythontech
Date:
Sat Apr 16 17:11:56 2016 +0000
Revision:
0:5868e8752d44
Split off library from repl

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-2015 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 <assert.h>
pythontech 0:5868e8752d44 28 #include <stdarg.h>
pythontech 0:5868e8752d44 29 #include <stdint.h>
pythontech 0:5868e8752d44 30 #include <stdio.h>
pythontech 0:5868e8752d44 31 #include <string.h>
pythontech 0:5868e8752d44 32
pythontech 0:5868e8752d44 33 #include "py/mphal.h"
pythontech 0:5868e8752d44 34 #include "py/mpprint.h"
pythontech 0:5868e8752d44 35 #include "py/obj.h"
pythontech 0:5868e8752d44 36 #include "py/objint.h"
pythontech 0:5868e8752d44 37 #include "py/runtime.h"
pythontech 0:5868e8752d44 38
pythontech 0:5868e8752d44 39 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 40 #include "py/formatfloat.h"
pythontech 0:5868e8752d44 41 #endif
pythontech 0:5868e8752d44 42
pythontech 0:5868e8752d44 43 static const char pad_spaces[] = " ";
pythontech 0:5868e8752d44 44 static const char pad_zeroes[] = "0000000000000000";
pythontech 0:5868e8752d44 45
pythontech 0:5868e8752d44 46 STATIC void plat_print_strn(void *env, const char *str, size_t len) {
pythontech 0:5868e8752d44 47 (void)env;
pythontech 0:5868e8752d44 48 MP_PLAT_PRINT_STRN(str, len);
pythontech 0:5868e8752d44 49 }
pythontech 0:5868e8752d44 50
pythontech 0:5868e8752d44 51 const mp_print_t mp_plat_print = {NULL, plat_print_strn};
pythontech 0:5868e8752d44 52
pythontech 0:5868e8752d44 53 int mp_print_str(const mp_print_t *print, const char *str) {
pythontech 0:5868e8752d44 54 size_t len = strlen(str);
pythontech 0:5868e8752d44 55 if (len) {
pythontech 0:5868e8752d44 56 print->print_strn(print->data, str, len);
pythontech 0:5868e8752d44 57 }
pythontech 0:5868e8752d44 58 return len;
pythontech 0:5868e8752d44 59 }
pythontech 0:5868e8752d44 60
pythontech 0:5868e8752d44 61 int mp_print_strn(const mp_print_t *print, const char *str, size_t len, int flags, char fill, int width) {
pythontech 0:5868e8752d44 62 int left_pad = 0;
pythontech 0:5868e8752d44 63 int right_pad = 0;
pythontech 0:5868e8752d44 64 int pad = width - len;
pythontech 0:5868e8752d44 65 int pad_size;
pythontech 0:5868e8752d44 66 int total_chars_printed = 0;
pythontech 0:5868e8752d44 67 const char *pad_chars;
pythontech 0:5868e8752d44 68
pythontech 0:5868e8752d44 69 if (!fill || fill == ' ') {
pythontech 0:5868e8752d44 70 pad_chars = pad_spaces;
pythontech 0:5868e8752d44 71 pad_size = sizeof(pad_spaces) - 1;
pythontech 0:5868e8752d44 72 } else if (fill == '0') {
pythontech 0:5868e8752d44 73 pad_chars = pad_zeroes;
pythontech 0:5868e8752d44 74 pad_size = sizeof(pad_zeroes) - 1;
pythontech 0:5868e8752d44 75 } else {
pythontech 0:5868e8752d44 76 // Other pad characters are fairly unusual, so we'll take the hit
pythontech 0:5868e8752d44 77 // and output them 1 at a time.
pythontech 0:5868e8752d44 78 pad_chars = &fill;
pythontech 0:5868e8752d44 79 pad_size = 1;
pythontech 0:5868e8752d44 80 }
pythontech 0:5868e8752d44 81
pythontech 0:5868e8752d44 82 if (flags & PF_FLAG_CENTER_ADJUST) {
pythontech 0:5868e8752d44 83 left_pad = pad / 2;
pythontech 0:5868e8752d44 84 right_pad = pad - left_pad;
pythontech 0:5868e8752d44 85 } else if (flags & PF_FLAG_LEFT_ADJUST) {
pythontech 0:5868e8752d44 86 right_pad = pad;
pythontech 0:5868e8752d44 87 } else {
pythontech 0:5868e8752d44 88 left_pad = pad;
pythontech 0:5868e8752d44 89 }
pythontech 0:5868e8752d44 90
pythontech 0:5868e8752d44 91 if (left_pad > 0) {
pythontech 0:5868e8752d44 92 total_chars_printed += left_pad;
pythontech 0:5868e8752d44 93 while (left_pad > 0) {
pythontech 0:5868e8752d44 94 int p = left_pad;
pythontech 0:5868e8752d44 95 if (p > pad_size) {
pythontech 0:5868e8752d44 96 p = pad_size;
pythontech 0:5868e8752d44 97 }
pythontech 0:5868e8752d44 98 print->print_strn(print->data, pad_chars, p);
pythontech 0:5868e8752d44 99 left_pad -= p;
pythontech 0:5868e8752d44 100 }
pythontech 0:5868e8752d44 101 }
pythontech 0:5868e8752d44 102 if (len) {
pythontech 0:5868e8752d44 103 print->print_strn(print->data, str, len);
pythontech 0:5868e8752d44 104 total_chars_printed += len;
pythontech 0:5868e8752d44 105 }
pythontech 0:5868e8752d44 106 if (right_pad > 0) {
pythontech 0:5868e8752d44 107 total_chars_printed += right_pad;
pythontech 0:5868e8752d44 108 while (right_pad > 0) {
pythontech 0:5868e8752d44 109 int p = right_pad;
pythontech 0:5868e8752d44 110 if (p > pad_size) {
pythontech 0:5868e8752d44 111 p = pad_size;
pythontech 0:5868e8752d44 112 }
pythontech 0:5868e8752d44 113 print->print_strn(print->data, pad_chars, p);
pythontech 0:5868e8752d44 114 right_pad -= p;
pythontech 0:5868e8752d44 115 }
pythontech 0:5868e8752d44 116 }
pythontech 0:5868e8752d44 117 return total_chars_printed;
pythontech 0:5868e8752d44 118 }
pythontech 0:5868e8752d44 119
pythontech 0:5868e8752d44 120 // 32-bits is 10 digits, add 3 for commas, 1 for sign, 1 for terminating null
pythontech 0:5868e8752d44 121 // We can use 16 characters for 32-bit and 32 characters for 64-bit
pythontech 0:5868e8752d44 122 #define INT_BUF_SIZE (sizeof(mp_int_t) * 4)
pythontech 0:5868e8752d44 123
pythontech 0:5868e8752d44 124 // Our mp_vprintf function below does not support the '#' format modifier to
pythontech 0:5868e8752d44 125 // print the prefix of a non-base-10 number, so we don't need code for this.
pythontech 0:5868e8752d44 126 #define SUPPORT_INT_BASE_PREFIX (0)
pythontech 0:5868e8752d44 127
pythontech 0:5868e8752d44 128 // This function is used exclusively by mp_vprintf to format ints.
pythontech 0:5868e8752d44 129 // It needs to be a separate function to mp_print_mp_int, since converting to a mp_int looses the MSB.
pythontech 0:5868e8752d44 130 STATIC int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base, int base_char, int flags, char fill, int width) {
pythontech 0:5868e8752d44 131 char sign = 0;
pythontech 0:5868e8752d44 132 if (sgn) {
pythontech 0:5868e8752d44 133 if ((mp_int_t)x < 0) {
pythontech 0:5868e8752d44 134 sign = '-';
pythontech 0:5868e8752d44 135 x = -x;
pythontech 0:5868e8752d44 136 } else if (flags & PF_FLAG_SHOW_SIGN) {
pythontech 0:5868e8752d44 137 sign = '+';
pythontech 0:5868e8752d44 138 } else if (flags & PF_FLAG_SPACE_SIGN) {
pythontech 0:5868e8752d44 139 sign = ' ';
pythontech 0:5868e8752d44 140 }
pythontech 0:5868e8752d44 141 }
pythontech 0:5868e8752d44 142
pythontech 0:5868e8752d44 143 char buf[INT_BUF_SIZE];
pythontech 0:5868e8752d44 144 char *b = buf + INT_BUF_SIZE;
pythontech 0:5868e8752d44 145
pythontech 0:5868e8752d44 146 if (x == 0) {
pythontech 0:5868e8752d44 147 *(--b) = '0';
pythontech 0:5868e8752d44 148 } else {
pythontech 0:5868e8752d44 149 do {
pythontech 0:5868e8752d44 150 int c = x % base;
pythontech 0:5868e8752d44 151 x /= base;
pythontech 0:5868e8752d44 152 if (c >= 10) {
pythontech 0:5868e8752d44 153 c += base_char - 10;
pythontech 0:5868e8752d44 154 } else {
pythontech 0:5868e8752d44 155 c += '0';
pythontech 0:5868e8752d44 156 }
pythontech 0:5868e8752d44 157 *(--b) = c;
pythontech 0:5868e8752d44 158 } while (b > buf && x != 0);
pythontech 0:5868e8752d44 159 }
pythontech 0:5868e8752d44 160
pythontech 0:5868e8752d44 161 #if SUPPORT_INT_BASE_PREFIX
pythontech 0:5868e8752d44 162 char prefix_char = '\0';
pythontech 0:5868e8752d44 163
pythontech 0:5868e8752d44 164 if (flags & PF_FLAG_SHOW_PREFIX) {
pythontech 0:5868e8752d44 165 if (base == 2) {
pythontech 0:5868e8752d44 166 prefix_char = base_char + 'b' - 'a';
pythontech 0:5868e8752d44 167 } else if (base == 8) {
pythontech 0:5868e8752d44 168 prefix_char = base_char + 'o' - 'a';
pythontech 0:5868e8752d44 169 } else if (base == 16) {
pythontech 0:5868e8752d44 170 prefix_char = base_char + 'x' - 'a';
pythontech 0:5868e8752d44 171 }
pythontech 0:5868e8752d44 172 }
pythontech 0:5868e8752d44 173 #endif
pythontech 0:5868e8752d44 174
pythontech 0:5868e8752d44 175 int len = 0;
pythontech 0:5868e8752d44 176 if (flags & PF_FLAG_PAD_AFTER_SIGN) {
pythontech 0:5868e8752d44 177 if (sign) {
pythontech 0:5868e8752d44 178 len += mp_print_strn(print, &sign, 1, flags, fill, 1);
pythontech 0:5868e8752d44 179 width--;
pythontech 0:5868e8752d44 180 }
pythontech 0:5868e8752d44 181 #if SUPPORT_INT_BASE_PREFIX
pythontech 0:5868e8752d44 182 if (prefix_char) {
pythontech 0:5868e8752d44 183 len += mp_print_strn(print, "0", 1, flags, fill, 1);
pythontech 0:5868e8752d44 184 len += mp_print_strn(print, &prefix_char, 1, flags, fill, 1);
pythontech 0:5868e8752d44 185 width -= 2;
pythontech 0:5868e8752d44 186 }
pythontech 0:5868e8752d44 187 #endif
pythontech 0:5868e8752d44 188 } else {
pythontech 0:5868e8752d44 189 #if SUPPORT_INT_BASE_PREFIX
pythontech 0:5868e8752d44 190 if (prefix_char && b > &buf[1]) {
pythontech 0:5868e8752d44 191 *(--b) = prefix_char;
pythontech 0:5868e8752d44 192 *(--b) = '0';
pythontech 0:5868e8752d44 193 }
pythontech 0:5868e8752d44 194 #endif
pythontech 0:5868e8752d44 195 if (sign && b > buf) {
pythontech 0:5868e8752d44 196 *(--b) = sign;
pythontech 0:5868e8752d44 197 }
pythontech 0:5868e8752d44 198 }
pythontech 0:5868e8752d44 199
pythontech 0:5868e8752d44 200 len += mp_print_strn(print, b, buf + INT_BUF_SIZE - b, flags, fill, width);
pythontech 0:5868e8752d44 201 return len;
pythontech 0:5868e8752d44 202 }
pythontech 0:5868e8752d44 203
pythontech 0:5868e8752d44 204 int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char, int flags, char fill, int width, int prec) {
pythontech 0:5868e8752d44 205 if (!MP_OBJ_IS_INT(x)) {
pythontech 0:5868e8752d44 206 // This will convert booleans to int, or raise an error for
pythontech 0:5868e8752d44 207 // non-integer types.
pythontech 0:5868e8752d44 208 x = MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(x));
pythontech 0:5868e8752d44 209 }
pythontech 0:5868e8752d44 210
pythontech 0:5868e8752d44 211 if ((flags & (PF_FLAG_LEFT_ADJUST | PF_FLAG_CENTER_ADJUST)) == 0 && fill == '0') {
pythontech 0:5868e8752d44 212 if (prec > width) {
pythontech 0:5868e8752d44 213 width = prec;
pythontech 0:5868e8752d44 214 }
pythontech 0:5868e8752d44 215 prec = 0;
pythontech 0:5868e8752d44 216 }
pythontech 0:5868e8752d44 217 char prefix_buf[4];
pythontech 0:5868e8752d44 218 char *prefix = prefix_buf;
pythontech 0:5868e8752d44 219
pythontech 0:5868e8752d44 220 if (mp_obj_int_sign(x) > 0) {
pythontech 0:5868e8752d44 221 if (flags & PF_FLAG_SHOW_SIGN) {
pythontech 0:5868e8752d44 222 *prefix++ = '+';
pythontech 0:5868e8752d44 223 } else if (flags & PF_FLAG_SPACE_SIGN) {
pythontech 0:5868e8752d44 224 *prefix++ = ' ';
pythontech 0:5868e8752d44 225 }
pythontech 0:5868e8752d44 226 }
pythontech 0:5868e8752d44 227
pythontech 0:5868e8752d44 228 if (flags & PF_FLAG_SHOW_PREFIX) {
pythontech 0:5868e8752d44 229 if (base == 2) {
pythontech 0:5868e8752d44 230 *prefix++ = '0';
pythontech 0:5868e8752d44 231 *prefix++ = base_char + 'b' - 'a';
pythontech 0:5868e8752d44 232 } else if (base == 8) {
pythontech 0:5868e8752d44 233 *prefix++ = '0';
pythontech 0:5868e8752d44 234 if (flags & PF_FLAG_SHOW_OCTAL_LETTER) {
pythontech 0:5868e8752d44 235 *prefix++ = base_char + 'o' - 'a';
pythontech 0:5868e8752d44 236 }
pythontech 0:5868e8752d44 237 } else if (base == 16) {
pythontech 0:5868e8752d44 238 *prefix++ = '0';
pythontech 0:5868e8752d44 239 *prefix++ = base_char + 'x' - 'a';
pythontech 0:5868e8752d44 240 }
pythontech 0:5868e8752d44 241 }
pythontech 0:5868e8752d44 242 *prefix = '\0';
pythontech 0:5868e8752d44 243 int prefix_len = prefix - prefix_buf;
pythontech 0:5868e8752d44 244 prefix = prefix_buf;
pythontech 0:5868e8752d44 245
pythontech 0:5868e8752d44 246 char comma = '\0';
pythontech 0:5868e8752d44 247 if (flags & PF_FLAG_SHOW_COMMA) {
pythontech 0:5868e8752d44 248 comma = ',';
pythontech 0:5868e8752d44 249 }
pythontech 0:5868e8752d44 250
pythontech 0:5868e8752d44 251 // The size of this buffer is rather arbitrary. If it's not large
pythontech 0:5868e8752d44 252 // enough, a dynamic one will be allocated.
pythontech 0:5868e8752d44 253 char stack_buf[sizeof(mp_int_t) * 4];
pythontech 0:5868e8752d44 254 char *buf = stack_buf;
pythontech 0:5868e8752d44 255 mp_uint_t buf_size = sizeof(stack_buf);
pythontech 0:5868e8752d44 256 mp_uint_t fmt_size = 0;
pythontech 0:5868e8752d44 257 char *str;
pythontech 0:5868e8752d44 258
pythontech 0:5868e8752d44 259 if (prec > 1) {
pythontech 0:5868e8752d44 260 flags |= PF_FLAG_PAD_AFTER_SIGN;
pythontech 0:5868e8752d44 261 }
pythontech 0:5868e8752d44 262 char sign = '\0';
pythontech 0:5868e8752d44 263 if (flags & PF_FLAG_PAD_AFTER_SIGN) {
pythontech 0:5868e8752d44 264 // We add the pad in this function, so since the pad goes after
pythontech 0:5868e8752d44 265 // the sign & prefix, we format without a prefix
pythontech 0:5868e8752d44 266 str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size,
pythontech 0:5868e8752d44 267 x, base, NULL, base_char, comma);
pythontech 0:5868e8752d44 268 if (*str == '-') {
pythontech 0:5868e8752d44 269 sign = *str++;
pythontech 0:5868e8752d44 270 fmt_size--;
pythontech 0:5868e8752d44 271 }
pythontech 0:5868e8752d44 272 } else {
pythontech 0:5868e8752d44 273 str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size,
pythontech 0:5868e8752d44 274 x, base, prefix, base_char, comma);
pythontech 0:5868e8752d44 275 }
pythontech 0:5868e8752d44 276
pythontech 0:5868e8752d44 277 int spaces_before = 0;
pythontech 0:5868e8752d44 278 int spaces_after = 0;
pythontech 0:5868e8752d44 279
pythontech 0:5868e8752d44 280 if (prec > 1) {
pythontech 0:5868e8752d44 281 // If prec was specified, then prec specifies the width to zero-pad the
pythontech 0:5868e8752d44 282 // the number to. This zero-padded number then gets left or right
pythontech 0:5868e8752d44 283 // aligned in width characters.
pythontech 0:5868e8752d44 284
pythontech 0:5868e8752d44 285 int prec_width = fmt_size; // The digits
pythontech 0:5868e8752d44 286 if (prec_width < prec) {
pythontech 0:5868e8752d44 287 prec_width = prec;
pythontech 0:5868e8752d44 288 }
pythontech 0:5868e8752d44 289 if (flags & PF_FLAG_PAD_AFTER_SIGN) {
pythontech 0:5868e8752d44 290 if (sign) {
pythontech 0:5868e8752d44 291 prec_width++;
pythontech 0:5868e8752d44 292 }
pythontech 0:5868e8752d44 293 prec_width += prefix_len;
pythontech 0:5868e8752d44 294 }
pythontech 0:5868e8752d44 295 if (prec_width < width) {
pythontech 0:5868e8752d44 296 if (flags & PF_FLAG_LEFT_ADJUST) {
pythontech 0:5868e8752d44 297 spaces_after = width - prec_width;
pythontech 0:5868e8752d44 298 } else {
pythontech 0:5868e8752d44 299 spaces_before = width - prec_width;
pythontech 0:5868e8752d44 300 }
pythontech 0:5868e8752d44 301 }
pythontech 0:5868e8752d44 302 fill = '0';
pythontech 0:5868e8752d44 303 flags &= ~PF_FLAG_LEFT_ADJUST;
pythontech 0:5868e8752d44 304 }
pythontech 0:5868e8752d44 305
pythontech 0:5868e8752d44 306 int len = 0;
pythontech 0:5868e8752d44 307 if (spaces_before) {
pythontech 0:5868e8752d44 308 len += mp_print_strn(print, "", 0, 0, ' ', spaces_before);
pythontech 0:5868e8752d44 309 }
pythontech 0:5868e8752d44 310 if (flags & PF_FLAG_PAD_AFTER_SIGN) {
pythontech 0:5868e8752d44 311 // pad after sign implies pad after prefix as well.
pythontech 0:5868e8752d44 312 if (sign) {
pythontech 0:5868e8752d44 313 len += mp_print_strn(print, &sign, 1, 0, 0, 1);
pythontech 0:5868e8752d44 314 width--;
pythontech 0:5868e8752d44 315 }
pythontech 0:5868e8752d44 316 if (prefix_len) {
pythontech 0:5868e8752d44 317 len += mp_print_strn(print, prefix, prefix_len, 0, 0, 1);
pythontech 0:5868e8752d44 318 width -= prefix_len;
pythontech 0:5868e8752d44 319 }
pythontech 0:5868e8752d44 320 }
pythontech 0:5868e8752d44 321 if (prec > 1) {
pythontech 0:5868e8752d44 322 width = prec;
pythontech 0:5868e8752d44 323 }
pythontech 0:5868e8752d44 324
pythontech 0:5868e8752d44 325 len += mp_print_strn(print, str, fmt_size, flags, fill, width);
pythontech 0:5868e8752d44 326
pythontech 0:5868e8752d44 327 if (spaces_after) {
pythontech 0:5868e8752d44 328 len += mp_print_strn(print, "", 0, 0, ' ', spaces_after);
pythontech 0:5868e8752d44 329 }
pythontech 0:5868e8752d44 330
pythontech 0:5868e8752d44 331 if (buf != stack_buf) {
pythontech 0:5868e8752d44 332 m_del(char, buf, buf_size);
pythontech 0:5868e8752d44 333 }
pythontech 0:5868e8752d44 334 return len;
pythontech 0:5868e8752d44 335 }
pythontech 0:5868e8752d44 336
pythontech 0:5868e8752d44 337 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 338 int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, char fill, int width, int prec) {
pythontech 0:5868e8752d44 339 char buf[32];
pythontech 0:5868e8752d44 340 char sign = '\0';
pythontech 0:5868e8752d44 341 int chrs = 0;
pythontech 0:5868e8752d44 342
pythontech 0:5868e8752d44 343 if (flags & PF_FLAG_SHOW_SIGN) {
pythontech 0:5868e8752d44 344 sign = '+';
pythontech 0:5868e8752d44 345 }
pythontech 0:5868e8752d44 346 else
pythontech 0:5868e8752d44 347 if (flags & PF_FLAG_SPACE_SIGN) {
pythontech 0:5868e8752d44 348 sign = ' ';
pythontech 0:5868e8752d44 349 }
pythontech 0:5868e8752d44 350
pythontech 0:5868e8752d44 351 int len = mp_format_float(f, buf, sizeof(buf), fmt, prec, sign);
pythontech 0:5868e8752d44 352 if (len < 0) {
pythontech 0:5868e8752d44 353 len = 0;
pythontech 0:5868e8752d44 354 }
pythontech 0:5868e8752d44 355
pythontech 0:5868e8752d44 356 char *s = buf;
pythontech 0:5868e8752d44 357
pythontech 0:5868e8752d44 358 if ((flags & PF_FLAG_ADD_PERCENT) && (size_t)(len + 1) < sizeof(buf)) {
pythontech 0:5868e8752d44 359 buf[len++] = '%';
pythontech 0:5868e8752d44 360 buf[len] = '\0';
pythontech 0:5868e8752d44 361 }
pythontech 0:5868e8752d44 362
pythontech 0:5868e8752d44 363 // buf[0] < '0' returns true if the first character is space, + or -
pythontech 0:5868e8752d44 364 if ((flags & PF_FLAG_PAD_AFTER_SIGN) && buf[0] < '0') {
pythontech 0:5868e8752d44 365 // We have a sign character
pythontech 0:5868e8752d44 366 s++;
pythontech 0:5868e8752d44 367 chrs += mp_print_strn(print, &buf[0], 1, 0, 0, 1);
pythontech 0:5868e8752d44 368 width--;
pythontech 0:5868e8752d44 369 len--;
pythontech 0:5868e8752d44 370 }
pythontech 0:5868e8752d44 371
pythontech 0:5868e8752d44 372 chrs += mp_print_strn(print, s, len, flags, fill, width);
pythontech 0:5868e8752d44 373
pythontech 0:5868e8752d44 374 return chrs;
pythontech 0:5868e8752d44 375 }
pythontech 0:5868e8752d44 376 #endif
pythontech 0:5868e8752d44 377
pythontech 0:5868e8752d44 378 int mp_printf(const mp_print_t *print, const char *fmt, ...) {
pythontech 0:5868e8752d44 379 va_list ap;
pythontech 0:5868e8752d44 380 va_start(ap, fmt);
pythontech 0:5868e8752d44 381 int ret = mp_vprintf(print, fmt, ap);
pythontech 0:5868e8752d44 382 va_end(ap);
pythontech 0:5868e8752d44 383 return ret;
pythontech 0:5868e8752d44 384 }
pythontech 0:5868e8752d44 385
pythontech 0:5868e8752d44 386 int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
pythontech 0:5868e8752d44 387 int chrs = 0;
pythontech 0:5868e8752d44 388 for (;;) {
pythontech 0:5868e8752d44 389 {
pythontech 0:5868e8752d44 390 const char *f = fmt;
pythontech 0:5868e8752d44 391 while (*f != '\0' && *f != '%') {
pythontech 0:5868e8752d44 392 ++f; // XXX UTF8 advance char
pythontech 0:5868e8752d44 393 }
pythontech 0:5868e8752d44 394 if (f > fmt) {
pythontech 0:5868e8752d44 395 print->print_strn(print->data, fmt, f - fmt);
pythontech 0:5868e8752d44 396 chrs += f - fmt;
pythontech 0:5868e8752d44 397 fmt = f;
pythontech 0:5868e8752d44 398 }
pythontech 0:5868e8752d44 399 }
pythontech 0:5868e8752d44 400
pythontech 0:5868e8752d44 401 if (*fmt == '\0') {
pythontech 0:5868e8752d44 402 break;
pythontech 0:5868e8752d44 403 }
pythontech 0:5868e8752d44 404
pythontech 0:5868e8752d44 405 // move past % character
pythontech 0:5868e8752d44 406 ++fmt;
pythontech 0:5868e8752d44 407
pythontech 0:5868e8752d44 408 // parse flags, if they exist
pythontech 0:5868e8752d44 409 int flags = 0;
pythontech 0:5868e8752d44 410 char fill = ' ';
pythontech 0:5868e8752d44 411 while (*fmt != '\0') {
pythontech 0:5868e8752d44 412 if (*fmt == '-') flags |= PF_FLAG_LEFT_ADJUST;
pythontech 0:5868e8752d44 413 else if (*fmt == '+') flags |= PF_FLAG_SHOW_SIGN;
pythontech 0:5868e8752d44 414 else if (*fmt == ' ') flags |= PF_FLAG_SPACE_SIGN;
pythontech 0:5868e8752d44 415 else if (*fmt == '!') flags |= PF_FLAG_NO_TRAILZ;
pythontech 0:5868e8752d44 416 else if (*fmt == '0') {
pythontech 0:5868e8752d44 417 flags |= PF_FLAG_PAD_AFTER_SIGN;
pythontech 0:5868e8752d44 418 fill = '0';
pythontech 0:5868e8752d44 419 } else break;
pythontech 0:5868e8752d44 420 ++fmt;
pythontech 0:5868e8752d44 421 }
pythontech 0:5868e8752d44 422
pythontech 0:5868e8752d44 423 // parse width, if it exists
pythontech 0:5868e8752d44 424 int width = 0;
pythontech 0:5868e8752d44 425 for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
pythontech 0:5868e8752d44 426 width = width * 10 + *fmt - '0';
pythontech 0:5868e8752d44 427 }
pythontech 0:5868e8752d44 428
pythontech 0:5868e8752d44 429 // parse precision, if it exists
pythontech 0:5868e8752d44 430 int prec = -1;
pythontech 0:5868e8752d44 431 if (*fmt == '.') {
pythontech 0:5868e8752d44 432 ++fmt;
pythontech 0:5868e8752d44 433 if (*fmt == '*') {
pythontech 0:5868e8752d44 434 ++fmt;
pythontech 0:5868e8752d44 435 prec = va_arg(args, int);
pythontech 0:5868e8752d44 436 } else {
pythontech 0:5868e8752d44 437 prec = 0;
pythontech 0:5868e8752d44 438 for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
pythontech 0:5868e8752d44 439 prec = prec * 10 + *fmt - '0';
pythontech 0:5868e8752d44 440 }
pythontech 0:5868e8752d44 441 }
pythontech 0:5868e8752d44 442 if (prec < 0) {
pythontech 0:5868e8752d44 443 prec = 0;
pythontech 0:5868e8752d44 444 }
pythontech 0:5868e8752d44 445 }
pythontech 0:5868e8752d44 446
pythontech 0:5868e8752d44 447 // parse long specifiers (current not used)
pythontech 0:5868e8752d44 448 //bool long_arg = false;
pythontech 0:5868e8752d44 449 if (*fmt == 'l') {
pythontech 0:5868e8752d44 450 ++fmt;
pythontech 0:5868e8752d44 451 //long_arg = true;
pythontech 0:5868e8752d44 452 }
pythontech 0:5868e8752d44 453
pythontech 0:5868e8752d44 454 if (*fmt == '\0') {
pythontech 0:5868e8752d44 455 break;
pythontech 0:5868e8752d44 456 }
pythontech 0:5868e8752d44 457
pythontech 0:5868e8752d44 458 switch (*fmt) {
pythontech 0:5868e8752d44 459 case 'b':
pythontech 0:5868e8752d44 460 if (va_arg(args, int)) {
pythontech 0:5868e8752d44 461 chrs += mp_print_strn(print, "true", 4, flags, fill, width);
pythontech 0:5868e8752d44 462 } else {
pythontech 0:5868e8752d44 463 chrs += mp_print_strn(print, "false", 5, flags, fill, width);
pythontech 0:5868e8752d44 464 }
pythontech 0:5868e8752d44 465 break;
pythontech 0:5868e8752d44 466 case 'c':
pythontech 0:5868e8752d44 467 {
pythontech 0:5868e8752d44 468 char str = va_arg(args, int);
pythontech 0:5868e8752d44 469 chrs += mp_print_strn(print, &str, 1, flags, fill, width);
pythontech 0:5868e8752d44 470 break;
pythontech 0:5868e8752d44 471 }
pythontech 0:5868e8752d44 472 case 'q':
pythontech 0:5868e8752d44 473 {
pythontech 0:5868e8752d44 474 qstr qst = va_arg(args, qstr);
pythontech 0:5868e8752d44 475 size_t len;
pythontech 0:5868e8752d44 476 const char *str = (const char*)qstr_data(qst, &len);
pythontech 0:5868e8752d44 477 if (prec < 0) {
pythontech 0:5868e8752d44 478 prec = len;
pythontech 0:5868e8752d44 479 }
pythontech 0:5868e8752d44 480 chrs += mp_print_strn(print, str, prec, flags, fill, width);
pythontech 0:5868e8752d44 481 break;
pythontech 0:5868e8752d44 482 }
pythontech 0:5868e8752d44 483 case 's':
pythontech 0:5868e8752d44 484 {
pythontech 0:5868e8752d44 485 const char *str = va_arg(args, const char*);
pythontech 0:5868e8752d44 486 if (str) {
pythontech 0:5868e8752d44 487 if (prec < 0) {
pythontech 0:5868e8752d44 488 prec = strlen(str);
pythontech 0:5868e8752d44 489 }
pythontech 0:5868e8752d44 490 chrs += mp_print_strn(print, str, prec, flags, fill, width);
pythontech 0:5868e8752d44 491 } else {
pythontech 0:5868e8752d44 492 chrs += mp_print_strn(print, "(null)", 6, flags, fill, width);
pythontech 0:5868e8752d44 493 }
pythontech 0:5868e8752d44 494 break;
pythontech 0:5868e8752d44 495 }
pythontech 0:5868e8752d44 496 case 'u':
pythontech 0:5868e8752d44 497 chrs += mp_print_int(print, va_arg(args, unsigned int), 0, 10, 'a', flags, fill, width);
pythontech 0:5868e8752d44 498 break;
pythontech 0:5868e8752d44 499 case 'd':
pythontech 0:5868e8752d44 500 chrs += mp_print_int(print, va_arg(args, int), 1, 10, 'a', flags, fill, width);
pythontech 0:5868e8752d44 501 break;
pythontech 0:5868e8752d44 502 case 'x':
pythontech 0:5868e8752d44 503 chrs += mp_print_int(print, va_arg(args, unsigned int), 0, 16, 'a', flags, fill, width);
pythontech 0:5868e8752d44 504 break;
pythontech 0:5868e8752d44 505 case 'X':
pythontech 0:5868e8752d44 506 chrs += mp_print_int(print, va_arg(args, unsigned int), 0, 16, 'A', flags, fill, width);
pythontech 0:5868e8752d44 507 break;
pythontech 0:5868e8752d44 508 case 'p':
pythontech 0:5868e8752d44 509 case 'P': // don't bother to handle upcase for 'P'
pythontech 0:5868e8752d44 510 chrs += mp_print_int(print, va_arg(args, unsigned int), 0, 16, 'a', flags, fill, width);
pythontech 0:5868e8752d44 511 break;
pythontech 0:5868e8752d44 512 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 513 case 'e':
pythontech 0:5868e8752d44 514 case 'E':
pythontech 0:5868e8752d44 515 case 'f':
pythontech 0:5868e8752d44 516 case 'F':
pythontech 0:5868e8752d44 517 case 'g':
pythontech 0:5868e8752d44 518 case 'G':
pythontech 0:5868e8752d44 519 {
pythontech 0:5868e8752d44 520 #if ((MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT) || (MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE))
pythontech 0:5868e8752d44 521 mp_float_t f = va_arg(args, double);
pythontech 0:5868e8752d44 522 chrs += mp_print_float(print, f, *fmt, flags, fill, width, prec);
pythontech 0:5868e8752d44 523 #else
pythontech 0:5868e8752d44 524 #error Unknown MICROPY FLOAT IMPL
pythontech 0:5868e8752d44 525 #endif
pythontech 0:5868e8752d44 526 break;
pythontech 0:5868e8752d44 527 }
pythontech 0:5868e8752d44 528 #endif
pythontech 0:5868e8752d44 529 // Because 'l' is eaten above, another 'l' means %ll. We need to support
pythontech 0:5868e8752d44 530 // this length specifier for OBJ_REPR_D (64-bit NaN boxing).
pythontech 0:5868e8752d44 531 // TODO Either enable this unconditionally, or provide a specific config var.
pythontech 0:5868e8752d44 532 #if (MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D) || defined(_WIN64)
pythontech 0:5868e8752d44 533 case 'l': {
pythontech 0:5868e8752d44 534 unsigned long long int arg_value = va_arg(args, unsigned long long int);
pythontech 0:5868e8752d44 535 ++fmt;
pythontech 0:5868e8752d44 536 if (*fmt == 'u' || *fmt == 'd') {
pythontech 0:5868e8752d44 537 chrs += mp_print_int(print, arg_value, *fmt == 'd', 10, 'a', flags, fill, width);
pythontech 0:5868e8752d44 538 break;
pythontech 0:5868e8752d44 539 }
pythontech 0:5868e8752d44 540 // fall through to default case to print unknown format char
pythontech 0:5868e8752d44 541 }
pythontech 0:5868e8752d44 542 #endif
pythontech 0:5868e8752d44 543 default:
pythontech 0:5868e8752d44 544 print->print_strn(print->data, fmt, 1);
pythontech 0:5868e8752d44 545 chrs += 1;
pythontech 0:5868e8752d44 546 break;
pythontech 0:5868e8752d44 547 }
pythontech 0:5868e8752d44 548 ++fmt;
pythontech 0:5868e8752d44 549 }
pythontech 0:5868e8752d44 550 return chrs;
pythontech 0:5868e8752d44 551 }