Skip to content

Commit 3ca8402

Browse files
committed
unix: Enable REPL auto-indent.
1 parent 0af7301 commit 3ca8402

4 files changed

Lines changed: 63 additions & 4 deletions

File tree

tests/cmdline/repl_cont.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
2:'two'}
1717
print(d[2])
1818
def f(x):
19-
print(x)
20-
19+
print(x)
20+

2121
f(3)

tests/cmdline/repl_cont.py.exp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Micro Python \.\+ version
2525
>>> print(d[2])
2626
two
2727
>>> def f(x):
28-
... print(x)
29-
...
28+
... print(x)
29+
... 
3030
>>> f(3)
3131
3
3232
>>>

unix/main.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind,
134134
}
135135
}
136136

137+
#if MICROPY_USE_READLINE == 1
138+
#include "lib/mp-readline/readline.h"
139+
#else
137140
STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
138141
int l1 = strlen(s1);
139142
int l2 = strlen(s2);
@@ -147,10 +150,63 @@ STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
147150
s[l1 + l2] = 0;
148151
return s;
149152
}
153+
#endif
150154

151155
STATIC int do_repl(void) {
152156
mp_hal_stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
153157

158+
#if MICROPY_USE_READLINE == 1
159+
160+
// use MicroPython supplied readline
161+
162+
vstr_t line;
163+
vstr_init(&line, 16);
164+
for (;;) {
165+
input_restart:
166+
vstr_reset(&line);
167+
mp_hal_stdio_mode_raw();
168+
int ret = readline(&line, ">>> ");
169+
170+
if (ret == CHAR_CTRL_D) {
171+
// EOF
172+
printf("\n");
173+
mp_hal_stdio_mode_orig();
174+
vstr_clear(&line);
175+
return 0;
176+
} else if (line.len == 0) {
177+
if (ret != 0) {
178+
printf("\n");
179+
}
180+
mp_hal_stdio_mode_orig();
181+
continue;
182+
}
183+
184+
while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) {
185+
vstr_add_byte(&line, '\n');
186+
ret = readline(&line, "... ");
187+
if (ret == CHAR_CTRL_C) {
188+
// cancel everything
189+
printf("\n");
190+
mp_hal_stdio_mode_orig();
191+
goto input_restart;
192+
} else if (ret == CHAR_CTRL_D) {
193+
// stop entering compound statement
194+
break;
195+
}
196+
}
197+
mp_hal_stdio_mode_orig();
198+
199+
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, false);
200+
ret = execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
201+
if (ret & FORCED_EXIT) {
202+
return ret;
203+
}
204+
}
205+
206+
#else
207+
208+
// use GNU or simple readline
209+
154210
for (;;) {
155211
char *line = prompt(">>> ");
156212
if (line == NULL) {
@@ -175,6 +231,8 @@ STATIC int do_repl(void) {
175231
}
176232
free(line);
177233
}
234+
235+
#endif
178236
}
179237

180238
STATIC int do_file(const char *file) {

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#define MICROPY_USE_READLINE_HISTORY (1)
5454
#define MICROPY_HELPER_REPL (1)
5555
#define MICROPY_REPL_EMACS_KEYS (1)
56+
#define MICROPY_REPL_AUTO_INDENT (1)
5657
#define MICROPY_HELPER_LEXER_UNIX (1)
5758
#define MICROPY_ENABLE_SOURCE_LINE (1)
5859
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)

0 commit comments

Comments
 (0)