|
55 | 55 | #include "genhdr/mpversion.h" |
56 | 56 | #include "input.h" |
57 | 57 | #include "stack_size.h" |
| 58 | +#include "shared/runtime/pyexec.h" |
58 | 59 |
|
59 | 60 | // Command line options, with their defaults |
60 | 61 | static bool compile_only = false; |
@@ -195,95 +196,34 @@ static char *strjoin(const char *s1, int sep_char, const char *s2) { |
195 | 196 | #endif |
196 | 197 |
|
197 | 198 | static int do_repl(void) { |
198 | | - mp_hal_stdout_tx_str(MICROPY_BANNER_NAME_AND_VERSION); |
199 | | - mp_hal_stdout_tx_str("; " MICROPY_BANNER_MACHINE); |
200 | | - mp_hal_stdout_tx_str("\nUse Ctrl-D to exit, Ctrl-E for paste mode\n"); |
201 | | - |
202 | 199 | #if MICROPY_USE_READLINE == 1 |
203 | 200 |
|
204 | | - // use MicroPython supplied readline |
| 201 | + // use MicroPython supplied readline-based REPL |
205 | 202 |
|
206 | | - vstr_t line; |
207 | | - vstr_init(&line, 16); |
| 203 | + int ret = 0; |
| 204 | + mp_hal_stdio_mode_raw(); |
208 | 205 | for (;;) { |
209 | | - mp_hal_stdio_mode_raw(); |
210 | | - |
211 | | - input_restart: |
212 | | - // If the GC is locked at this point there is no way out except a reset, |
213 | | - // so force the GC to be unlocked to help the user debug what went wrong. |
214 | | - MP_STATE_THREAD(gc_lock_depth) = 0; |
215 | | - vstr_reset(&line); |
216 | | - int ret = readline(&line, mp_repl_get_ps1()); |
217 | | - mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT; |
218 | | - |
219 | | - if (ret == CHAR_CTRL_C) { |
220 | | - // cancel input |
221 | | - mp_hal_stdout_tx_str("\r\n"); |
222 | | - goto input_restart; |
223 | | - } else if (ret == CHAR_CTRL_D) { |
224 | | - // EOF |
225 | | - printf("\n"); |
226 | | - mp_hal_stdio_mode_orig(); |
227 | | - vstr_clear(&line); |
228 | | - return 0; |
229 | | - } else if (ret == CHAR_CTRL_E) { |
230 | | - // paste mode |
231 | | - mp_hal_stdout_tx_str("\npaste mode; Ctrl-C to cancel, Ctrl-D to finish\n=== "); |
232 | | - vstr_reset(&line); |
233 | | - for (;;) { |
234 | | - char c = mp_hal_stdin_rx_chr(); |
235 | | - if (c == CHAR_CTRL_C) { |
236 | | - // cancel everything |
237 | | - mp_hal_stdout_tx_str("\n"); |
238 | | - goto input_restart; |
239 | | - } else if (c == CHAR_CTRL_D) { |
240 | | - // end of input |
241 | | - mp_hal_stdout_tx_str("\n"); |
242 | | - break; |
243 | | - } else { |
244 | | - // add char to buffer and echo |
245 | | - vstr_add_byte(&line, c); |
246 | | - if (c == '\r') { |
247 | | - mp_hal_stdout_tx_str("\n=== "); |
248 | | - } else { |
249 | | - mp_hal_stdout_tx_strn(&c, 1); |
250 | | - } |
251 | | - } |
252 | | - } |
253 | | - parse_input_kind = MP_PARSE_FILE_INPUT; |
254 | | - } else if (line.len == 0) { |
255 | | - if (ret != 0) { |
256 | | - printf("\n"); |
| 206 | + if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { |
| 207 | + if ((ret = pyexec_raw_repl()) != 0) { |
| 208 | + break; |
257 | 209 | } |
258 | | - goto input_restart; |
259 | 210 | } else { |
260 | | - // got a line with non-zero length, see if it needs continuing |
261 | | - while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) { |
262 | | - vstr_add_byte(&line, '\n'); |
263 | | - ret = readline(&line, mp_repl_get_ps2()); |
264 | | - if (ret == CHAR_CTRL_C) { |
265 | | - // cancel everything |
266 | | - printf("\n"); |
267 | | - goto input_restart; |
268 | | - } else if (ret == CHAR_CTRL_D) { |
269 | | - // stop entering compound statement |
270 | | - break; |
271 | | - } |
| 211 | + if ((ret = pyexec_friendly_repl()) != 0) { |
| 212 | + break; |
272 | 213 | } |
273 | 214 | } |
274 | | - |
275 | | - mp_hal_stdio_mode_orig(); |
276 | | - |
277 | | - ret = execute_from_lexer(LEX_SRC_VSTR, &line, parse_input_kind, true); |
278 | | - if (ret & FORCED_EXIT) { |
279 | | - return ret; |
280 | | - } |
281 | 215 | } |
| 216 | + mp_hal_stdio_mode_orig(); |
| 217 | + return ret; |
282 | 218 |
|
283 | 219 | #else |
284 | 220 |
|
285 | 221 | // use simple readline |
286 | 222 |
|
| 223 | + mp_hal_stdout_tx_str(MICROPY_BANNER_NAME_AND_VERSION); |
| 224 | + mp_hal_stdout_tx_str("; " MICROPY_BANNER_MACHINE); |
| 225 | + mp_hal_stdout_tx_str("\nUse Ctrl-D to exit, Ctrl-E for paste mode\n"); |
| 226 | + |
287 | 227 | for (;;) { |
288 | 228 | char *line = prompt((char *)mp_repl_get_ps1()); |
289 | 229 | if (line == NULL) { |
|
0 commit comments