Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 1 | #include "misc.h" |
Damien George | 136f675 | 2014-01-07 14:54:15 +0000 | [diff] [blame] | 2 | #include "mpconfig.h" |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 3 | #include "repl.h" |
| 4 | |
Damien George | 136f675 | 2014-01-07 14:54:15 +0000 | [diff] [blame] | 5 | #if MICROPY_ENABLE_REPL_HELPERS |
| 6 | |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 7 | bool str_startswith_word(const char *str, const char *head) { |
| 8 | int i; |
| 9 | for (i = 0; str[i] && head[i]; i++) { |
| 10 | if (str[i] != head[i]) { |
| 11 | return false; |
| 12 | } |
| 13 | } |
Damien George | 8cc96a3 | 2013-12-30 18:23:50 +0000 | [diff] [blame] | 14 | return head[i] == '\0' && (str[i] == '\0' || !unichar_isalpha(str[i])); |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 15 | } |
| 16 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 17 | bool mp_repl_is_compound_stmt(const char *line) { |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 18 | // compound if line starts with a certain keyword |
| 19 | if ( |
| 20 | str_startswith_word(line, "if") |
| 21 | || str_startswith_word(line, "while") |
| 22 | || str_startswith_word(line, "for") |
Paul Sokolovsky | e06edce | 2014-01-10 16:13:06 +0200 | [diff] [blame] | 23 | || str_startswith_word(line, "try") |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 24 | || str_startswith_word(line, "with") |
| 25 | || str_startswith_word(line, "def") |
| 26 | || str_startswith_word(line, "class") |
| 27 | || str_startswith_word(line, "@") |
| 28 | ) { |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | // also "compound" if unmatched open bracket |
| 33 | int n_paren = 0; |
| 34 | int n_brack = 0; |
| 35 | int n_brace = 0; |
| 36 | for (const char *l = line; *l; l++) { |
| 37 | switch (*l) { |
| 38 | case '(': n_paren += 1; break; |
| 39 | case ')': n_paren -= 1; break; |
| 40 | case '[': n_brack += 1; break; |
| 41 | case ']': n_brack -= 1; break; |
| 42 | case '{': n_brace += 1; break; |
| 43 | case '}': n_brace -= 1; break; |
| 44 | } |
| 45 | } |
| 46 | return n_paren > 0 || n_brack > 0 || n_brace > 0; |
| 47 | } |
Damien George | 136f675 | 2014-01-07 14:54:15 +0000 | [diff] [blame] | 48 | |
| 49 | #endif // MICROPY_ENABLE_REPL_HELPERS |