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 | |
Damien George | 2cf6dfa | 2015-01-01 21:06:20 +0000 | [diff] [blame] | 31 | #include "py/nlr.h" |
| 32 | #include "py/parsehelper.h" |
| 33 | #include "py/compile.h" |
| 34 | #include "py/runtime.h" |
| 35 | #include "py/repl.h" |
| 36 | #include "py/gc.h" |
| 37 | #include "py/pfenv.h" |
Paul Sokolovsky | bf19586 | 2014-12-20 20:45:01 +0200 | [diff] [blame] | 38 | #ifdef MICROPY_HAL_H |
Damien George | 7a37f64 | 2014-07-02 13:42:37 +0100 | [diff] [blame] | 39 | #include MICROPY_HAL_H |
Paul Sokolovsky | bf19586 | 2014-12-20 20:45:01 +0200 | [diff] [blame] | 40 | #endif |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 41 | #include "systick.h" |
Damien George | c9fd664 | 2014-03-29 14:20:05 +0000 | [diff] [blame] | 42 | #include "readline.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 43 | #include "pyexec.h" |
Damien George | 951ed9d | 2014-07-20 13:57:43 +0100 | [diff] [blame] | 44 | #include "pybstdio.h" |
Damien George | d553be5 | 2014-04-17 18:03:27 +0100 | [diff] [blame] | 45 | #include "genhdr/py-version.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 46 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 47 | pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL; |
| 48 | STATIC bool repl_display_debugging_info = 0; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 49 | |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 50 | #define EXEC_FLAG_PRINT_EOF (1) |
| 51 | #define EXEC_FLAG_ALLOW_DEBUGGING (2) |
| 52 | #define EXEC_FLAG_IS_REPL (4) |
| 53 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 54 | // parses, compiles and executes the code in the lexer |
| 55 | // frees the lexer before returning |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 56 | // EXEC_FLAG_PRINT_EOF prints 2 EOF chars: 1 after normal output, 1 after exception output |
| 57 | // EXEC_FLAG_ALLOW_DEBUGGING allows debugging info to be printed after executing the code |
| 58 | // EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile) |
| 59 | STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, int exec_flags) { |
| 60 | int ret = 0; |
| 61 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 62 | mp_parse_error_kind_t parse_error_kind; |
| 63 | mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind); |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 64 | qstr source_name = lex->source_name; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 65 | |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 66 | // check for parse error |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 67 | if (pn == MP_PARSE_NODE_NULL) { |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 68 | if (exec_flags & EXEC_FLAG_PRINT_EOF) { |
| 69 | stdout_tx_strn("\x04", 1); |
| 70 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 71 | mp_parse_show_exception(lex, parse_error_kind); |
| 72 | mp_lexer_free(lex); |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 73 | goto finish; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | mp_lexer_free(lex); |
| 77 | |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 78 | mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 79 | |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 80 | // check for compile error |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 81 | if (mp_obj_is_exception_instance(module_fun)) { |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 82 | if (exec_flags & EXEC_FLAG_PRINT_EOF) { |
| 83 | stdout_tx_strn("\x04", 1); |
| 84 | } |
Paul Sokolovsky | 46c3ab2 | 2014-12-06 14:29:09 +0200 | [diff] [blame] | 85 | mp_obj_print_exception(printf_wrapper, NULL, module_fun); |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 86 | goto finish; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 89 | // execute code |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 90 | nlr_buf_t nlr; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 91 | uint32_t start = HAL_GetTick(); |
| 92 | if (nlr_push(&nlr) == 0) { |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 93 | mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 94 | mp_call_function_0(module_fun); |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 95 | mp_hal_set_interrupt_char(-1); // disable interrupt |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 96 | nlr_pop(); |
Dave Hylands | 8d62bbd | 2014-10-21 23:04:38 -0700 | [diff] [blame] | 97 | ret = 1; |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 98 | if (exec_flags & EXEC_FLAG_PRINT_EOF) { |
| 99 | stdout_tx_strn("\x04", 1); |
| 100 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 101 | } else { |
| 102 | // uncaught exception |
| 103 | // FIXME it could be that an interrupt happens just before we disable it here |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 104 | mp_hal_set_interrupt_char(-1); // disable interrupt |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 105 | // print EOF after normal output |
| 106 | if (exec_flags & EXEC_FLAG_PRINT_EOF) { |
| 107 | stdout_tx_strn("\x04", 1); |
| 108 | } |
Dave Hylands | 8d62bbd | 2014-10-21 23:04:38 -0700 | [diff] [blame] | 109 | // check for SystemExit |
Damien George | 3be6984 | 2014-10-22 19:13:28 +0100 | [diff] [blame] | 110 | if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_SystemExit)) { |
| 111 | // at the moment, the value of SystemExit is unused |
| 112 | ret = PYEXEC_FORCED_EXIT; |
| 113 | } else { |
Paul Sokolovsky | 46c3ab2 | 2014-12-06 14:29:09 +0200 | [diff] [blame] | 114 | mp_obj_print_exception(printf_wrapper, NULL, (mp_obj_t)nlr.ret_val); |
Damien George | 3be6984 | 2014-10-22 19:13:28 +0100 | [diff] [blame] | 115 | ret = 0; |
Dave Hylands | 8d62bbd | 2014-10-21 23:04:38 -0700 | [diff] [blame] | 116 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // display debugging info if wanted |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 120 | if ((exec_flags & EXEC_FLAG_ALLOW_DEBUGGING) && repl_display_debugging_info) { |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 121 | mp_uint_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly |
| 122 | printf("took " UINT_FMT " ms\n", ticks); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 123 | gc_collect(); |
| 124 | // qstr info |
| 125 | { |
Damien George | d03c681 | 2014-10-05 21:51:54 +0100 | [diff] [blame] | 126 | 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] | 127 | 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] | 128 | 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] | 129 | } |
| 130 | |
| 131 | // GC info |
Paul Sokolovsky | 6aaccc4 | 2014-12-21 00:24:58 +0200 | [diff] [blame] | 132 | gc_dump_info(); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 135 | finish: |
| 136 | if (exec_flags & EXEC_FLAG_PRINT_EOF) { |
| 137 | stdout_tx_strn("\x04", 1); |
| 138 | } |
| 139 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 140 | return ret; |
| 141 | } |
| 142 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 143 | int pyexec_raw_repl(void) { |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 144 | vstr_t line; |
| 145 | vstr_init(&line, 32); |
| 146 | |
| 147 | raw_repl_reset: |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 148 | stdout_tx_str("raw REPL; CTRL-B to exit\r\n"); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 149 | |
| 150 | for (;;) { |
| 151 | vstr_reset(&line); |
| 152 | stdout_tx_str(">"); |
| 153 | for (;;) { |
| 154 | char c = stdin_rx_chr(); |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 155 | if (c == CHAR_CTRL_A) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 156 | // reset raw REPL |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 157 | goto raw_repl_reset; |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 158 | } else if (c == CHAR_CTRL_B) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 159 | // change to friendly REPL |
| 160 | stdout_tx_str("\r\n"); |
| 161 | vstr_clear(&line); |
| 162 | pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL; |
| 163 | return 0; |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 164 | } else if (c == CHAR_CTRL_C) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 165 | // clear line |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 166 | vstr_reset(&line); |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 167 | } else if (c == CHAR_CTRL_D) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 168 | // input finished |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 169 | break; |
Damien George | d8b47d3 | 2014-04-03 22:11:43 +0100 | [diff] [blame] | 170 | } else if (c <= 127) { |
| 171 | // let through any other ASCII character |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 172 | vstr_add_char(&line, c); |
| 173 | } |
| 174 | } |
| 175 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 176 | // indicate reception of command |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 177 | stdout_tx_str("OK"); |
| 178 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 179 | if (line.len == 0) { |
| 180 | // exit for a soft reset |
| 181 | stdout_tx_str("\r\n"); |
| 182 | vstr_clear(&line); |
Dave Hylands | 8d62bbd | 2014-10-21 23:04:38 -0700 | [diff] [blame] | 183 | return PYEXEC_FORCED_EXIT; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 186 | 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] | 187 | if (lex == NULL) { |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 188 | printf("\x04MemoryError\n\x04"); |
Damien George | b0edec6 | 2014-05-10 17:48:46 +0100 | [diff] [blame] | 189 | } else { |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 190 | int ret = parse_compile_execute(lex, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF); |
Dave Hylands | 8d62bbd | 2014-10-21 23:04:38 -0700 | [diff] [blame] | 191 | if (ret & PYEXEC_FORCED_EXIT) { |
| 192 | return ret; |
| 193 | } |
Damien George | b0edec6 | 2014-05-10 17:48:46 +0100 | [diff] [blame] | 194 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 195 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Paul Sokolovsky | 87bc8e2 | 2015-01-15 10:46:27 +0200 | [diff] [blame^] | 198 | #if MICROPY_REPL_EVENT_DRIVEN |
| 199 | |
| 200 | typedef struct _friendly_repl_t { |
| 201 | vstr_t line; |
| 202 | bool cont_line; |
| 203 | } friendly_repl_t; |
| 204 | |
| 205 | friendly_repl_t repl; |
| 206 | |
| 207 | void pyexec_friendly_repl_init(void) { |
| 208 | vstr_init(&repl.line, 32); |
| 209 | repl.cont_line = false; |
| 210 | readline_init(&repl.line); |
| 211 | stdout_tx_str(">>> "); |
| 212 | } |
| 213 | |
| 214 | void pyexec_friendly_repl_reset() { |
| 215 | repl.cont_line = false; |
| 216 | vstr_reset(&repl.line); |
| 217 | readline_init(&repl.line); |
| 218 | } |
| 219 | |
| 220 | int pyexec_friendly_repl_process_char(int c) { |
| 221 | int ret = readline_process_char(c); |
| 222 | |
| 223 | if (!repl.cont_line) { |
| 224 | |
| 225 | if (ret == CHAR_CTRL_A) { |
| 226 | // change to raw REPL |
| 227 | pyexec_mode_kind = PYEXEC_MODE_RAW_REPL; |
| 228 | stdout_tx_str("\r\n"); |
| 229 | vstr_clear(&repl.line); |
| 230 | return PYEXEC_SWITCH_MODE; |
| 231 | } else if (ret == CHAR_CTRL_B) { |
| 232 | // reset friendly REPL |
| 233 | stdout_tx_str("\r\n"); |
| 234 | goto friendly_repl_reset; |
| 235 | } else if (ret == CHAR_CTRL_C) { |
| 236 | // break |
| 237 | stdout_tx_str("\r\n"); |
| 238 | goto input_restart; |
| 239 | } else if (ret == CHAR_CTRL_D) { |
| 240 | // exit for a soft reset |
| 241 | stdout_tx_str("\r\n"); |
| 242 | vstr_clear(&repl.line); |
| 243 | return PYEXEC_FORCED_EXIT; |
| 244 | } else if (vstr_len(&repl.line) == 0) { |
| 245 | //goto input_restart; |
| 246 | } |
| 247 | |
| 248 | if (ret < 0) { |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | if (!mp_repl_continue_with_input(vstr_str(&repl.line))) { |
| 253 | goto exec; |
| 254 | } |
| 255 | |
| 256 | vstr_add_char(&repl.line, '\n'); |
| 257 | repl.cont_line = true; |
| 258 | stdout_tx_str("... "); |
| 259 | readline_note_newline(); |
| 260 | return 0; |
| 261 | |
| 262 | } else { |
| 263 | |
| 264 | if (ret == CHAR_CTRL_C) { |
| 265 | // cancel everything |
| 266 | stdout_tx_str("\r\n"); |
| 267 | repl.cont_line = false; |
| 268 | goto input_restart; |
| 269 | } else if (ret == CHAR_CTRL_D) { |
| 270 | // stop entering compound statement |
| 271 | goto exec; |
| 272 | } |
| 273 | |
| 274 | if (ret < 0) { |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | if (mp_repl_continue_with_input(vstr_str(&repl.line))) { |
| 279 | vstr_add_char(&repl.line, '\n'); |
| 280 | stdout_tx_str("... "); |
| 281 | readline_note_newline(); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | exec: ; |
| 286 | mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&repl.line), vstr_len(&repl.line), 0); |
| 287 | if (lex == NULL) { |
| 288 | printf("MemoryError\n"); |
| 289 | } else { |
| 290 | int ret = parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL); |
| 291 | if (ret & PYEXEC_FORCED_EXIT) { |
| 292 | return ret; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | friendly_repl_reset: // TODO |
| 297 | input_restart: |
| 298 | pyexec_friendly_repl_reset(); |
| 299 | stdout_tx_str(">>> "); |
| 300 | return 0; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | #else //MICROPY_REPL_EVENT_DRIVEN |
| 305 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 306 | int pyexec_friendly_repl(void) { |
| 307 | vstr_t line; |
| 308 | vstr_init(&line, 32); |
| 309 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 310 | #if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD |
| 311 | // in host mode, we enable the LCD for the repl |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 312 | mp_obj_t lcd_o = mp_call_function_0(mp_load_name(qstr_from_str("LCD"))); |
| 313 | 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] | 314 | #endif |
| 315 | |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 316 | friendly_repl_reset: |
mux | a75e382 | 2014-06-27 00:35:53 +0200 | [diff] [blame] | 317 | 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] | 318 | stdout_tx_str("Type \"help()\" for more information.\r\n"); |
| 319 | |
| 320 | // to test ctrl-C |
| 321 | /* |
| 322 | { |
| 323 | uint32_t x[4] = {0x424242, 0xdeaddead, 0x242424, 0xdeadbeef}; |
| 324 | for (;;) { |
| 325 | nlr_buf_t nlr; |
| 326 | printf("pyexec_repl: %p\n", x); |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 327 | mp_hal_set_interrupt_char(CHAR_CTRL_C); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 328 | if (nlr_push(&nlr) == 0) { |
| 329 | for (;;) { |
| 330 | } |
| 331 | } else { |
| 332 | printf("break\n"); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | */ |
| 337 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 338 | for (;;) { |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 339 | input_restart: |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 340 | vstr_reset(&line); |
| 341 | int ret = readline(&line, ">>> "); |
| 342 | |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 343 | if (ret == CHAR_CTRL_A) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 344 | // change to raw REPL |
| 345 | stdout_tx_str("\r\n"); |
| 346 | vstr_clear(&line); |
| 347 | pyexec_mode_kind = PYEXEC_MODE_RAW_REPL; |
| 348 | return 0; |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 349 | } else if (ret == CHAR_CTRL_B) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 350 | // reset friendly REPL |
| 351 | stdout_tx_str("\r\n"); |
| 352 | goto friendly_repl_reset; |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 353 | } else if (ret == CHAR_CTRL_C) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 354 | // break |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 355 | stdout_tx_str("\r\n"); |
| 356 | continue; |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 357 | } else if (ret == CHAR_CTRL_D) { |
Damien George | f704e7f | 2014-03-24 12:23:37 +0000 | [diff] [blame] | 358 | // exit for a soft reset |
| 359 | stdout_tx_str("\r\n"); |
| 360 | vstr_clear(&line); |
Dave Hylands | 8d62bbd | 2014-10-21 23:04:38 -0700 | [diff] [blame] | 361 | return PYEXEC_FORCED_EXIT; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 362 | } else if (vstr_len(&line) == 0) { |
| 363 | continue; |
| 364 | } |
| 365 | |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 366 | while (mp_repl_continue_with_input(vstr_str(&line))) { |
| 367 | vstr_add_char(&line, '\n'); |
| 368 | int ret = readline(&line, "... "); |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 369 | if (ret == CHAR_CTRL_C) { |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 370 | // cancel everything |
| 371 | stdout_tx_str("\r\n"); |
| 372 | goto input_restart; |
Damien George | 5cbc9e0 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 373 | } else if (ret == CHAR_CTRL_D) { |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 374 | // stop entering compound statement |
| 375 | break; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | 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] | 380 | if (lex == NULL) { |
| 381 | printf("MemoryError\n"); |
| 382 | } else { |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 383 | int ret = parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL); |
Dave Hylands | 8d62bbd | 2014-10-21 23:04:38 -0700 | [diff] [blame] | 384 | if (ret & PYEXEC_FORCED_EXIT) { |
| 385 | return ret; |
| 386 | } |
Damien George | b0edec6 | 2014-05-10 17:48:46 +0100 | [diff] [blame] | 387 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 388 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Paul Sokolovsky | 87bc8e2 | 2015-01-15 10:46:27 +0200 | [diff] [blame^] | 391 | #endif //MICROPY_REPL_EVENT_DRIVEN |
| 392 | |
Damien George | 3be6984 | 2014-10-22 19:13:28 +0100 | [diff] [blame] | 393 | int pyexec_file(const char *filename) { |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 394 | mp_lexer_t *lex = mp_lexer_new_from_file(filename); |
| 395 | |
| 396 | if (lex == NULL) { |
| 397 | printf("could not open file '%s' for reading\n", filename); |
| 398 | return false; |
| 399 | } |
| 400 | |
Damien George | bc1488a | 2014-10-26 15:39:22 +0000 | [diff] [blame] | 401 | return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | mp_obj_t pyb_set_repl_info(mp_obj_t o_value) { |
| 405 | repl_display_debugging_info = mp_obj_get_int(o_value); |
| 406 | return mp_const_none; |
| 407 | } |
| 408 | |
| 409 | MP_DEFINE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj, pyb_set_repl_info); |