blob: 473313c1efd866e385d4131ac28d172dee93e271 [file] [log] [blame]
Damien92c06562013-10-22 22:32:27 +01001#include "misc.h"
Damien George136f6752014-01-07 14:54:15 +00002#include "mpconfig.h"
Damien92c06562013-10-22 22:32:27 +01003#include "repl.h"
4
Damien George136f6752014-01-07 14:54:15 +00005#if MICROPY_ENABLE_REPL_HELPERS
6
Damien92c06562013-10-22 22:32:27 +01007bool 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 George8cc96a32013-12-30 18:23:50 +000014 return head[i] == '\0' && (str[i] == '\0' || !unichar_isalpha(str[i]));
Damien92c06562013-10-22 22:32:27 +010015}
16
Damiend99b0522013-12-21 18:17:45 +000017bool mp_repl_is_compound_stmt(const char *line) {
Damien92c06562013-10-22 22:32:27 +010018 // 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")
23 || str_startswith_word(line, "true")
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 George136f6752014-01-07 14:54:15 +000048
49#endif // MICROPY_ENABLE_REPL_HELPERS