Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.
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.
py/lexer.h@2:c89e95946844, 2016-04-16 (annotated)
- Committer:
- Colin Hogben
- Date:
- Sat Apr 16 22:43:41 2016 +0100
- Revision:
- 2:c89e95946844
- Parent:
- 0:5868e8752d44
py: update to upstream master
Who changed what in which revision?
User | Revision | Line number | New 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_LEXER_H__ |
pythontech | 0:5868e8752d44 | 27 | #define __MICROPY_INCLUDED_PY_LEXER_H__ |
pythontech | 0:5868e8752d44 | 28 | |
pythontech | 0:5868e8752d44 | 29 | #include <stdint.h> |
pythontech | 0:5868e8752d44 | 30 | |
pythontech | 0:5868e8752d44 | 31 | #include "py/mpconfig.h" |
pythontech | 0:5868e8752d44 | 32 | #include "py/qstr.h" |
pythontech | 0:5868e8752d44 | 33 | |
pythontech | 0:5868e8752d44 | 34 | /* lexer.h -- simple tokeniser for Micro Python |
pythontech | 0:5868e8752d44 | 35 | * |
pythontech | 0:5868e8752d44 | 36 | * Uses (byte) length instead of null termination. |
pythontech | 0:5868e8752d44 | 37 | * Tokens are the same - UTF-8 with (byte) length. |
pythontech | 0:5868e8752d44 | 38 | */ |
pythontech | 0:5868e8752d44 | 39 | |
pythontech | 0:5868e8752d44 | 40 | typedef enum _mp_token_kind_t { |
pythontech | 0:5868e8752d44 | 41 | MP_TOKEN_END, // 0 |
pythontech | 0:5868e8752d44 | 42 | |
pythontech | 0:5868e8752d44 | 43 | MP_TOKEN_INVALID, |
pythontech | 0:5868e8752d44 | 44 | MP_TOKEN_DEDENT_MISMATCH, |
pythontech | 0:5868e8752d44 | 45 | MP_TOKEN_LONELY_STRING_OPEN, |
pythontech | 0:5868e8752d44 | 46 | MP_TOKEN_BAD_LINE_CONTINUATION, |
pythontech | 0:5868e8752d44 | 47 | |
pythontech | 0:5868e8752d44 | 48 | MP_TOKEN_NEWLINE, // 5 |
pythontech | 0:5868e8752d44 | 49 | MP_TOKEN_INDENT, // 6 |
pythontech | 0:5868e8752d44 | 50 | MP_TOKEN_DEDENT, // 7 |
pythontech | 0:5868e8752d44 | 51 | |
pythontech | 0:5868e8752d44 | 52 | MP_TOKEN_NAME, // 8 |
pythontech | 0:5868e8752d44 | 53 | MP_TOKEN_INTEGER, |
pythontech | 0:5868e8752d44 | 54 | MP_TOKEN_FLOAT_OR_IMAG, |
pythontech | 0:5868e8752d44 | 55 | MP_TOKEN_STRING, |
pythontech | 0:5868e8752d44 | 56 | MP_TOKEN_BYTES, |
pythontech | 0:5868e8752d44 | 57 | |
pythontech | 0:5868e8752d44 | 58 | MP_TOKEN_ELLIPSIS, |
pythontech | 0:5868e8752d44 | 59 | |
pythontech | 0:5868e8752d44 | 60 | MP_TOKEN_KW_FALSE, // 14 |
pythontech | 0:5868e8752d44 | 61 | MP_TOKEN_KW_NONE, |
pythontech | 0:5868e8752d44 | 62 | MP_TOKEN_KW_TRUE, |
pythontech | 0:5868e8752d44 | 63 | MP_TOKEN_KW_AND, |
pythontech | 0:5868e8752d44 | 64 | MP_TOKEN_KW_AS, |
pythontech | 0:5868e8752d44 | 65 | MP_TOKEN_KW_ASSERT, |
Colin Hogben |
2:c89e95946844 | 66 | #if MICROPY_PY_ASYNC_AWAIT |
Colin Hogben |
2:c89e95946844 | 67 | MP_TOKEN_KW_ASYNC, |
Colin Hogben |
2:c89e95946844 | 68 | MP_TOKEN_KW_AWAIT, |
Colin Hogben |
2:c89e95946844 | 69 | #endif |
pythontech | 0:5868e8752d44 | 70 | MP_TOKEN_KW_BREAK, |
pythontech | 0:5868e8752d44 | 71 | MP_TOKEN_KW_CLASS, |
pythontech | 0:5868e8752d44 | 72 | MP_TOKEN_KW_CONTINUE, |
pythontech | 0:5868e8752d44 | 73 | MP_TOKEN_KW_DEF, // 23 |
pythontech | 0:5868e8752d44 | 74 | MP_TOKEN_KW_DEL, |
pythontech | 0:5868e8752d44 | 75 | MP_TOKEN_KW_ELIF, |
pythontech | 0:5868e8752d44 | 76 | MP_TOKEN_KW_ELSE, |
pythontech | 0:5868e8752d44 | 77 | MP_TOKEN_KW_EXCEPT, |
pythontech | 0:5868e8752d44 | 78 | MP_TOKEN_KW_FINALLY, |
pythontech | 0:5868e8752d44 | 79 | MP_TOKEN_KW_FOR, |
pythontech | 0:5868e8752d44 | 80 | MP_TOKEN_KW_FROM, |
pythontech | 0:5868e8752d44 | 81 | MP_TOKEN_KW_GLOBAL, |
pythontech | 0:5868e8752d44 | 82 | MP_TOKEN_KW_IF, |
pythontech | 0:5868e8752d44 | 83 | MP_TOKEN_KW_IMPORT, // 33 |
pythontech | 0:5868e8752d44 | 84 | MP_TOKEN_KW_IN, |
pythontech | 0:5868e8752d44 | 85 | MP_TOKEN_KW_IS, |
pythontech | 0:5868e8752d44 | 86 | MP_TOKEN_KW_LAMBDA, |
pythontech | 0:5868e8752d44 | 87 | MP_TOKEN_KW_NONLOCAL, |
pythontech | 0:5868e8752d44 | 88 | MP_TOKEN_KW_NOT, |
pythontech | 0:5868e8752d44 | 89 | MP_TOKEN_KW_OR, |
pythontech | 0:5868e8752d44 | 90 | MP_TOKEN_KW_PASS, |
pythontech | 0:5868e8752d44 | 91 | MP_TOKEN_KW_RAISE, |
pythontech | 0:5868e8752d44 | 92 | MP_TOKEN_KW_RETURN, |
pythontech | 0:5868e8752d44 | 93 | MP_TOKEN_KW_TRY, // 43 |
pythontech | 0:5868e8752d44 | 94 | MP_TOKEN_KW_WHILE, |
pythontech | 0:5868e8752d44 | 95 | MP_TOKEN_KW_WITH, |
pythontech | 0:5868e8752d44 | 96 | MP_TOKEN_KW_YIELD, |
pythontech | 0:5868e8752d44 | 97 | |
pythontech | 0:5868e8752d44 | 98 | MP_TOKEN_OP_PLUS, // 47 |
pythontech | 0:5868e8752d44 | 99 | MP_TOKEN_OP_MINUS, |
pythontech | 0:5868e8752d44 | 100 | MP_TOKEN_OP_STAR, |
pythontech | 0:5868e8752d44 | 101 | MP_TOKEN_OP_DBL_STAR, |
pythontech | 0:5868e8752d44 | 102 | MP_TOKEN_OP_SLASH, |
pythontech | 0:5868e8752d44 | 103 | MP_TOKEN_OP_DBL_SLASH, |
pythontech | 0:5868e8752d44 | 104 | MP_TOKEN_OP_PERCENT, |
pythontech | 0:5868e8752d44 | 105 | MP_TOKEN_OP_LESS, |
pythontech | 0:5868e8752d44 | 106 | MP_TOKEN_OP_DBL_LESS, |
pythontech | 0:5868e8752d44 | 107 | MP_TOKEN_OP_MORE, |
pythontech | 0:5868e8752d44 | 108 | MP_TOKEN_OP_DBL_MORE, // 57 |
pythontech | 0:5868e8752d44 | 109 | MP_TOKEN_OP_AMPERSAND, |
pythontech | 0:5868e8752d44 | 110 | MP_TOKEN_OP_PIPE, |
pythontech | 0:5868e8752d44 | 111 | MP_TOKEN_OP_CARET, |
pythontech | 0:5868e8752d44 | 112 | MP_TOKEN_OP_TILDE, |
pythontech | 0:5868e8752d44 | 113 | MP_TOKEN_OP_LESS_EQUAL, |
pythontech | 0:5868e8752d44 | 114 | MP_TOKEN_OP_MORE_EQUAL, |
pythontech | 0:5868e8752d44 | 115 | MP_TOKEN_OP_DBL_EQUAL, |
pythontech | 0:5868e8752d44 | 116 | MP_TOKEN_OP_NOT_EQUAL, |
pythontech | 0:5868e8752d44 | 117 | |
pythontech | 0:5868e8752d44 | 118 | MP_TOKEN_DEL_PAREN_OPEN, // 66 |
pythontech | 0:5868e8752d44 | 119 | MP_TOKEN_DEL_PAREN_CLOSE, |
pythontech | 0:5868e8752d44 | 120 | MP_TOKEN_DEL_BRACKET_OPEN, |
pythontech | 0:5868e8752d44 | 121 | MP_TOKEN_DEL_BRACKET_CLOSE, |
pythontech | 0:5868e8752d44 | 122 | MP_TOKEN_DEL_BRACE_OPEN, |
pythontech | 0:5868e8752d44 | 123 | MP_TOKEN_DEL_BRACE_CLOSE, |
pythontech | 0:5868e8752d44 | 124 | MP_TOKEN_DEL_COMMA, |
pythontech | 0:5868e8752d44 | 125 | MP_TOKEN_DEL_COLON, |
pythontech | 0:5868e8752d44 | 126 | MP_TOKEN_DEL_PERIOD, |
pythontech | 0:5868e8752d44 | 127 | MP_TOKEN_DEL_SEMICOLON, |
pythontech | 0:5868e8752d44 | 128 | MP_TOKEN_DEL_AT, // 76 |
pythontech | 0:5868e8752d44 | 129 | MP_TOKEN_DEL_EQUAL, |
pythontech | 0:5868e8752d44 | 130 | MP_TOKEN_DEL_PLUS_EQUAL, |
pythontech | 0:5868e8752d44 | 131 | MP_TOKEN_DEL_MINUS_EQUAL, |
pythontech | 0:5868e8752d44 | 132 | MP_TOKEN_DEL_STAR_EQUAL, |
pythontech | 0:5868e8752d44 | 133 | MP_TOKEN_DEL_SLASH_EQUAL, |
pythontech | 0:5868e8752d44 | 134 | MP_TOKEN_DEL_DBL_SLASH_EQUAL, |
pythontech | 0:5868e8752d44 | 135 | MP_TOKEN_DEL_PERCENT_EQUAL, |
pythontech | 0:5868e8752d44 | 136 | MP_TOKEN_DEL_AMPERSAND_EQUAL, |
pythontech | 0:5868e8752d44 | 137 | MP_TOKEN_DEL_PIPE_EQUAL, |
pythontech | 0:5868e8752d44 | 138 | MP_TOKEN_DEL_CARET_EQUAL, // 86 |
pythontech | 0:5868e8752d44 | 139 | MP_TOKEN_DEL_DBL_MORE_EQUAL, |
pythontech | 0:5868e8752d44 | 140 | MP_TOKEN_DEL_DBL_LESS_EQUAL, |
pythontech | 0:5868e8752d44 | 141 | MP_TOKEN_DEL_DBL_STAR_EQUAL, |
pythontech | 0:5868e8752d44 | 142 | MP_TOKEN_DEL_MINUS_MORE, |
pythontech | 0:5868e8752d44 | 143 | } mp_token_kind_t; |
pythontech | 0:5868e8752d44 | 144 | |
pythontech | 0:5868e8752d44 | 145 | // the next-byte function must return the next byte in the stream |
pythontech | 0:5868e8752d44 | 146 | // it must return MP_LEXER_EOF if end of stream |
pythontech | 0:5868e8752d44 | 147 | // it can be called again after returning MP_LEXER_EOF, and in that case must return MP_LEXER_EOF |
pythontech | 0:5868e8752d44 | 148 | #define MP_LEXER_EOF ((unichar)(-1)) |
pythontech | 0:5868e8752d44 | 149 | |
pythontech | 0:5868e8752d44 | 150 | typedef mp_uint_t (*mp_lexer_stream_next_byte_t)(void*); |
pythontech | 0:5868e8752d44 | 151 | typedef void (*mp_lexer_stream_close_t)(void*); |
pythontech | 0:5868e8752d44 | 152 | |
pythontech | 0:5868e8752d44 | 153 | // this data structure is exposed for efficiency |
pythontech | 0:5868e8752d44 | 154 | // public members are: source_name, tok_line, tok_column, tok_kind, vstr |
pythontech | 0:5868e8752d44 | 155 | typedef struct _mp_lexer_t { |
pythontech | 0:5868e8752d44 | 156 | qstr source_name; // name of source |
pythontech | 0:5868e8752d44 | 157 | void *stream_data; // data for stream |
pythontech | 0:5868e8752d44 | 158 | mp_lexer_stream_next_byte_t stream_next_byte; // stream callback to get next byte |
pythontech | 0:5868e8752d44 | 159 | mp_lexer_stream_close_t stream_close; // stream callback to free |
pythontech | 0:5868e8752d44 | 160 | |
pythontech | 0:5868e8752d44 | 161 | unichar chr0, chr1, chr2; // current cached characters from source |
pythontech | 0:5868e8752d44 | 162 | |
pythontech | 0:5868e8752d44 | 163 | mp_uint_t line; // current source line |
pythontech | 0:5868e8752d44 | 164 | mp_uint_t column; // current source column |
pythontech | 0:5868e8752d44 | 165 | |
pythontech | 0:5868e8752d44 | 166 | mp_int_t emit_dent; // non-zero when there are INDENT/DEDENT tokens to emit |
pythontech | 0:5868e8752d44 | 167 | mp_int_t nested_bracket_level; // >0 when there are nested brackets over multiple lines |
pythontech | 0:5868e8752d44 | 168 | |
pythontech | 0:5868e8752d44 | 169 | mp_uint_t alloc_indent_level; |
pythontech | 0:5868e8752d44 | 170 | mp_uint_t num_indent_level; |
pythontech | 0:5868e8752d44 | 171 | uint16_t *indent_level; |
pythontech | 0:5868e8752d44 | 172 | |
pythontech | 0:5868e8752d44 | 173 | mp_uint_t tok_line; // token source line |
pythontech | 0:5868e8752d44 | 174 | mp_uint_t tok_column; // token source column |
pythontech | 0:5868e8752d44 | 175 | mp_token_kind_t tok_kind; // token kind |
pythontech | 0:5868e8752d44 | 176 | vstr_t vstr; // token data |
pythontech | 0:5868e8752d44 | 177 | } mp_lexer_t; |
pythontech | 0:5868e8752d44 | 178 | |
pythontech | 0:5868e8752d44 | 179 | mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_byte_t stream_next_byte, mp_lexer_stream_close_t stream_close); |
pythontech | 0:5868e8752d44 | 180 | mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len); |
pythontech | 0:5868e8752d44 | 181 | |
pythontech | 0:5868e8752d44 | 182 | void mp_lexer_free(mp_lexer_t *lex); |
pythontech | 0:5868e8752d44 | 183 | void mp_lexer_to_next(mp_lexer_t *lex); |
pythontech | 0:5868e8752d44 | 184 | void mp_lexer_show_token(const mp_lexer_t *lex); |
pythontech | 0:5868e8752d44 | 185 | |
pythontech | 0:5868e8752d44 | 186 | /******************************************************************/ |
pythontech | 0:5868e8752d44 | 187 | // platform specific import function; must be implemented for a specific port |
pythontech | 0:5868e8752d44 | 188 | // TODO tidy up, rename, or put elsewhere |
pythontech | 0:5868e8752d44 | 189 | |
pythontech | 0:5868e8752d44 | 190 | //mp_lexer_t *mp_import_open_file(qstr mod_name); |
pythontech | 0:5868e8752d44 | 191 | |
pythontech | 0:5868e8752d44 | 192 | typedef enum { |
pythontech | 0:5868e8752d44 | 193 | MP_IMPORT_STAT_NO_EXIST, |
pythontech | 0:5868e8752d44 | 194 | MP_IMPORT_STAT_DIR, |
pythontech | 0:5868e8752d44 | 195 | MP_IMPORT_STAT_FILE, |
pythontech | 0:5868e8752d44 | 196 | } mp_import_stat_t; |
pythontech | 0:5868e8752d44 | 197 | |
pythontech | 0:5868e8752d44 | 198 | mp_import_stat_t mp_import_stat(const char *path); |
pythontech | 0:5868e8752d44 | 199 | mp_lexer_t *mp_lexer_new_from_file(const char *filename); |
pythontech | 0:5868e8752d44 | 200 | |
pythontech | 0:5868e8752d44 | 201 | #if MICROPY_HELPER_LEXER_UNIX |
pythontech | 0:5868e8752d44 | 202 | mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd); |
pythontech | 0:5868e8752d44 | 203 | #endif |
pythontech | 0:5868e8752d44 | 204 | |
pythontech | 0:5868e8752d44 | 205 | #endif // __MICROPY_INCLUDED_PY_LEXER_H__ |