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