53
53
#include "extmod/vfs_posix.h"
54
54
#include "genhdr/mpversion.h"
55
55
#include "input.h"
56
+ #include "shared/runtime/pyexec.h"
56
57
57
58
// Command line options, with their defaults
58
59
STATIC bool compile_only = false;
@@ -193,91 +194,27 @@ STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
193
194
#endif
194
195
195
196
STATIC int do_repl (void ) {
196
- mp_hal_stdout_tx_str (MICROPY_BANNER_NAME_AND_VERSION );
197
- mp_hal_stdout_tx_str ("; " MICROPY_BANNER_MACHINE );
198
- mp_hal_stdout_tx_str ("\nUse Ctrl-D to exit, Ctrl-E for paste mode\n" );
199
-
197
+ int ret = 0 ;
200
198
#if MICROPY_USE_READLINE == 1
201
-
202
- // use MicroPython supplied readline
203
-
204
- vstr_t line ;
205
- vstr_init (& line , 16 );
199
+ // use MicroPython supplied readline based repl
200
+ mp_hal_stdio_mode_raw ();
206
201
for (;;) {
207
- mp_hal_stdio_mode_raw ();
208
-
209
- input_restart :
210
- vstr_reset (& line );
211
- int ret = readline (& line , mp_repl_get_ps1 ());
212
- mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT ;
213
-
214
- if (ret == CHAR_CTRL_C ) {
215
- // cancel input
216
- mp_hal_stdout_tx_str ("\r\n" );
217
- goto input_restart ;
218
- } else if (ret == CHAR_CTRL_D ) {
219
- // EOF
220
- printf ("\n" );
221
- mp_hal_stdio_mode_orig ();
222
- vstr_clear (& line );
223
- return 0 ;
224
- } else if (ret == CHAR_CTRL_E ) {
225
- // paste mode
226
- mp_hal_stdout_tx_str ("\npaste mode; Ctrl-C to cancel, Ctrl-D to finish\n=== " );
227
- vstr_reset (& line );
228
- for (;;) {
229
- char c = mp_hal_stdin_rx_chr ();
230
- if (c == CHAR_CTRL_C ) {
231
- // cancel everything
232
- mp_hal_stdout_tx_str ("\n" );
233
- goto input_restart ;
234
- } else if (c == CHAR_CTRL_D ) {
235
- // end of input
236
- mp_hal_stdout_tx_str ("\n" );
237
- break ;
238
- } else {
239
- // add char to buffer and echo
240
- vstr_add_byte (& line , c );
241
- if (c == '\r' ) {
242
- mp_hal_stdout_tx_str ("\n=== " );
243
- } else {
244
- mp_hal_stdout_tx_strn (& c , 1 );
245
- }
246
- }
247
- }
248
- parse_input_kind = MP_PARSE_FILE_INPUT ;
249
- } else if (line .len == 0 ) {
250
- if (ret != 0 ) {
251
- printf ("\n" );
202
+ if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL ) {
203
+ if ((ret = pyexec_raw_repl ()) != 0 ) {
204
+ break ;
252
205
}
253
- goto input_restart ;
254
206
} else {
255
- // got a line with non-zero length, see if it needs continuing
256
- while (mp_repl_continue_with_input (vstr_null_terminated_str (& line ))) {
257
- vstr_add_byte (& line , '\n' );
258
- ret = readline (& line , mp_repl_get_ps2 ());
259
- if (ret == CHAR_CTRL_C ) {
260
- // cancel everything
261
- printf ("\n" );
262
- goto input_restart ;
263
- } else if (ret == CHAR_CTRL_D ) {
264
- // stop entering compound statement
265
- break ;
266
- }
207
+ if ((ret = pyexec_friendly_repl ()) != 0 ) {
208
+ break ;
267
209
}
268
210
}
269
-
270
- mp_hal_stdio_mode_orig ();
271
-
272
- ret = execute_from_lexer (LEX_SRC_VSTR , & line , parse_input_kind , true);
273
- if (ret & FORCED_EXIT ) {
274
- return ret ;
275
- }
276
211
}
277
-
212
+ mp_hal_stdio_mode_orig ();
278
213
#else
279
-
280
214
// use simple readline
215
+ mp_hal_stdout_tx_str (MICROPY_BANNER_NAME_AND_VERSION );
216
+ mp_hal_stdout_tx_str ("; " MICROPY_BANNER_MACHINE );
217
+ mp_hal_stdout_tx_str ("\nUse Ctrl-D to exit, Ctrl-E for paste mode\n" );
281
218
282
219
for (;;) {
283
220
char * line = prompt ((char * )mp_repl_get_ps1 ());
@@ -296,16 +233,14 @@ STATIC int do_repl(void) {
296
233
line = line3 ;
297
234
}
298
235
299
- int ret = execute_from_lexer (LEX_SRC_STR , line , MP_PARSE_SINGLE_INPUT , true);
236
+ ret = execute_from_lexer (LEX_SRC_STR , line , MP_PARSE_SINGLE_INPUT , true);
300
237
free (line );
301
- if (ret & FORCED_EXIT ) {
302
- return ret ;
303
- }
304
238
}
305
-
306
239
#endif
240
+ return ret ;
307
241
}
308
242
243
+
309
244
STATIC int do_file (const char * file ) {
310
245
return execute_from_lexer (LEX_SRC_FILENAME , file , MP_PARSE_FILE_INPUT , false);
311
246
}
0 commit comments