Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 27 | #include <stdlib.h> |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
Dave Hylands | 4f1b7fe | 2014-06-15 22:33:14 -0700 | [diff] [blame] | 29 | #include <stdint.h> |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 30 | |
Paul Sokolovsky | 9b71b16 | 2014-05-02 18:03:04 +0300 | [diff] [blame] | 31 | #include "mpconfig.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 32 | #include "nlr.h" |
| 33 | #include "misc.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 34 | #include "qstr.h" |
| 35 | #include "misc.h" |
| 36 | #include "lexer.h" |
| 37 | #include "parse.h" |
| 38 | #include "obj.h" |
| 39 | #include "parsehelper.h" |
| 40 | #include "compile.h" |
| 41 | #include "runtime.h" |
| 42 | #include "repl.h" |
| 43 | #include "gc.h" |
| 44 | #include "gccollect.h" |
Damien George | 7a37f64 | 2014-07-02 13:42:37 +0100 | [diff] [blame] | 45 | #include MICROPY_HAL_H |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 46 | #include "systick.h" |
Damien George | c9fd664 | 2014-03-29 14:20:05 +0000 | [diff] [blame] | 47 | #include "readline.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 48 | #include "pyexec.h" |
Damien George | e285511 | 2014-03-15 11:52:29 +0000 | [diff] [blame] | 49 | #include "usb.h" |
Damien George | 951ed9d | 2014-07-20 13:57:43 +0100 | [diff] [blame] | 50 | #include "uart.h" |
| 51 | #include "pybstdio.h" |
Damien George | d553be5 | 2014-04-17 18:03:27 +0100 | [diff] [blame] | 52 | #include "genhdr/py-version.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 53 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 54 | pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL; |
| 55 | STATIC bool repl_display_debugging_info = 0; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 56 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 57 | // parses, compiles and executes the code in the lexer |
| 58 | // frees the lexer before returning |
| 59 | bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) { |
| 60 | mp_parse_error_kind_t parse_error_kind; |
| 61 | mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind); |
| 62 | qstr source_name = mp_lexer_source_name(lex); |
| 63 | |
| 64 | if (pn == MP_PARSE_NODE_NULL) { |
| 65 | // parse error |
| 66 | mp_parse_show_exception(lex, parse_error_kind); |
| 67 | mp_lexer_free(lex); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | mp_lexer_free(lex); |
| 72 | |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 73 | mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, is_repl); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 74 | |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 75 | if (mp_obj_is_exception_instance(module_fun)) { |
| 76 | mp_obj_print_exception(module_fun); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
| 80 | nlr_buf_t nlr; |
| 81 | bool ret; |
| 82 | uint32_t start = HAL_GetTick(); |
| 83 | if (nlr_push(&nlr) == 0) { |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 84 | usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C); // allow ctrl-C to interrupt us |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 85 | mp_call_function_0(module_fun); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 86 | usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 87 | nlr_pop(); |
| 88 | ret = true; |
| 89 | } else { |
| 90 | // uncaught exception |
| 91 | // FIXME it could be that an interrupt happens just before we disable it here |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 92 | usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 93 | mp_obj_print_exception((mp_obj_t)nlr.ret_val); |
| 94 | ret = false; |
| 95 | } |
| 96 | |
| 97 | // display debugging info if wanted |
| 98 | if (is_repl && repl_display_debugging_info) { |
| 99 | uint32_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly |
| 100 | printf("took %lu ms\n", ticks); |
| 101 | gc_collect(); |
| 102 | // qstr info |
| 103 | { |
Damien George | d03c681 | 2014-10-05 21:51:54 +0100 | [diff] [blame^] | 104 | mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 105 | qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); |
Damien George | d03c681 | 2014-10-05 21:51:54 +0100 | [diff] [blame^] | 106 | printf("qstr:\n n_pool=" UINT_FMT "\n n_qstr=" UINT_FMT "\n n_str_data_bytes=" UINT_FMT "\n n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // GC info |
| 110 | { |
| 111 | gc_info_t info; |
| 112 | gc_info(&info); |
| 113 | printf("GC:\n"); |
Damien George | 5874c1c | 2014-05-03 13:24:21 +0100 | [diff] [blame] | 114 | printf(" " UINT_FMT " total\n", info.total); |
| 115 | printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free); |
| 116 | printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 123 | int pyexec_raw_repl(void) { |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 124 | vstr_t line; |
| 125 | vstr_init(&line, 32); |
| 126 | |
| 127 | raw_repl_reset: |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 128 | stdout_tx_str("raw REPL; CTRL-B to exit\r\n"); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 129 | |
| 130 | for (;;) { |
| 131 | vstr_reset(&line); |
| 132 | stdout_tx_str(">"); |
| 133 | for (;;) { |
| 134 | char c = stdin_rx_chr(); |
| 135 | if (c == VCP_CHAR_CTRL_A) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 136 | // reset raw REPL |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 137 | goto raw_repl_reset; |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 138 | } else if (c == VCP_CHAR_CTRL_B) { |
| 139 | // change to friendly REPL |
| 140 | stdout_tx_str("\r\n"); |
| 141 | vstr_clear(&line); |
| 142 | pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL; |
| 143 | return 0; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 144 | } else if (c == VCP_CHAR_CTRL_C) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 145 | // clear line |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 146 | vstr_reset(&line); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 147 | } else if (c == VCP_CHAR_CTRL_D) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 148 | // input finished |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 149 | break; |
Damien George | d8b47d3 | 2014-04-03 22:11:43 +0100 | [diff] [blame] | 150 | } else if (c <= 127) { |
| 151 | // let through any other ASCII character |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 152 | vstr_add_char(&line, c); |
| 153 | } |
| 154 | } |
| 155 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 156 | // indicate reception of command |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 157 | stdout_tx_str("OK"); |
| 158 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 159 | if (line.len == 0) { |
| 160 | // exit for a soft reset |
| 161 | stdout_tx_str("\r\n"); |
| 162 | vstr_clear(&line); |
| 163 | return 1; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 166 | mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, 0); |
Damien George | b0edec6 | 2014-05-10 17:48:46 +0100 | [diff] [blame] | 167 | if (lex == NULL) { |
| 168 | printf("MemoryError\n"); |
| 169 | } else { |
| 170 | parse_compile_execute(lex, MP_PARSE_FILE_INPUT, false); |
| 171 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 172 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 173 | // indicate end of output with EOF character |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 174 | stdout_tx_str("\004"); |
| 175 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 178 | int pyexec_friendly_repl(void) { |
| 179 | vstr_t line; |
| 180 | vstr_init(&line, 32); |
| 181 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 182 | #if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD |
| 183 | // in host mode, we enable the LCD for the repl |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 184 | mp_obj_t lcd_o = mp_call_function_0(mp_load_name(qstr_from_str("LCD"))); |
| 185 | mp_call_function_1(mp_load_attr(lcd_o, qstr_from_str("light")), mp_const_true); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 186 | #endif |
| 187 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 188 | friendly_repl_reset: |
mux | a75e382 | 2014-06-27 00:35:53 +0200 | [diff] [blame] | 189 | stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n"); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 190 | stdout_tx_str("Type \"help()\" for more information.\r\n"); |
| 191 | |
| 192 | // to test ctrl-C |
| 193 | /* |
| 194 | { |
| 195 | uint32_t x[4] = {0x424242, 0xdeaddead, 0x242424, 0xdeadbeef}; |
| 196 | for (;;) { |
| 197 | nlr_buf_t nlr; |
| 198 | printf("pyexec_repl: %p\n", x); |
| 199 | usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C); |
| 200 | if (nlr_push(&nlr) == 0) { |
| 201 | for (;;) { |
| 202 | } |
| 203 | } else { |
| 204 | printf("break\n"); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | */ |
| 209 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 210 | for (;;) { |
| 211 | vstr_reset(&line); |
| 212 | int ret = readline(&line, ">>> "); |
| 213 | |
| 214 | if (ret == VCP_CHAR_CTRL_A) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 215 | // change to raw REPL |
| 216 | stdout_tx_str("\r\n"); |
| 217 | vstr_clear(&line); |
| 218 | pyexec_mode_kind = PYEXEC_MODE_RAW_REPL; |
| 219 | return 0; |
| 220 | } else if (ret == VCP_CHAR_CTRL_B) { |
| 221 | // reset friendly REPL |
| 222 | stdout_tx_str("\r\n"); |
| 223 | goto friendly_repl_reset; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 224 | } else if (ret == VCP_CHAR_CTRL_C) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 225 | // break |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 226 | stdout_tx_str("\r\n"); |
| 227 | continue; |
| 228 | } else if (ret == VCP_CHAR_CTRL_D) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 229 | // exit for a soft reset |
| 230 | stdout_tx_str("\r\n"); |
| 231 | vstr_clear(&line); |
| 232 | return 1; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 233 | } else if (vstr_len(&line) == 0) { |
| 234 | continue; |
| 235 | } |
| 236 | |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 237 | while (mp_repl_continue_with_input(vstr_str(&line))) { |
| 238 | vstr_add_char(&line, '\n'); |
| 239 | int ret = readline(&line, "... "); |
| 240 | if (ret == VCP_CHAR_CTRL_D) { |
| 241 | // stop entering compound statement |
| 242 | break; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0); |
Damien George | b0edec6 | 2014-05-10 17:48:46 +0100 | [diff] [blame] | 247 | if (lex == NULL) { |
| 248 | printf("MemoryError\n"); |
| 249 | } else { |
| 250 | parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, true); |
| 251 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 252 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | bool pyexec_file(const char *filename) { |
| 256 | mp_lexer_t *lex = mp_lexer_new_from_file(filename); |
| 257 | |
| 258 | if (lex == NULL) { |
| 259 | printf("could not open file '%s' for reading\n", filename); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, false); |
| 264 | } |
| 265 | |
| 266 | mp_obj_t pyb_set_repl_info(mp_obj_t o_value) { |
| 267 | repl_display_debugging_info = mp_obj_get_int(o_value); |
| 268 | return mp_const_none; |
| 269 | } |
| 270 | |
| 271 | MP_DEFINE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj, pyb_set_repl_info); |