Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 5 | #include "nlr.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 6 | #include "misc.h" |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 7 | #include "mpyconfig.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 8 | #include "lexer.h" |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame^] | 9 | #include "lexerunix.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 10 | #include "parse.h" |
| 11 | #include "compile.h" |
| 12 | #include "runtime.h" |
| 13 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 14 | #include <readline/readline.h> |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 15 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 16 | bool str_startswith_word(const char *str, const char *head) { |
| 17 | int i; |
| 18 | for (i = 0; str[i] && head[i]; i++) { |
| 19 | if (str[i] != head[i]) { |
| 20 | return false; |
| 21 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 22 | } |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 23 | return head[i] == '\0' && (str[i] == '\0' || !g_unichar_isalpha(str[i])); |
| 24 | } |
| 25 | |
| 26 | bool is_compound_stmt(const char *line) { |
| 27 | // TODO also "compound" if unmatched open bracket |
| 28 | return |
| 29 | str_startswith_word(line, "if") |
| 30 | || str_startswith_word(line, "while") |
| 31 | || str_startswith_word(line, "for") |
| 32 | || str_startswith_word(line, "true") |
| 33 | || str_startswith_word(line, "with") |
| 34 | || str_startswith_word(line, "def") |
| 35 | || str_startswith_word(line, "class") |
| 36 | || str_startswith_word(line, "@"); |
| 37 | } |
| 38 | |
| 39 | char *str_join(const char *s1, int sep_char, const char *s2) { |
| 40 | int l1 = strlen(s1); |
| 41 | int l2 = strlen(s2); |
| 42 | char *s = m_new(char, l1 + l2 + 2); |
| 43 | memcpy(s, s1, l1); |
| 44 | if (sep_char != 0) { |
| 45 | s[l1] = sep_char; |
| 46 | l1 += 1; |
| 47 | } |
| 48 | memcpy(s + l1, s2, l2); |
| 49 | return s; |
| 50 | } |
| 51 | |
| 52 | void do_repl() { |
| 53 | for (;;) { |
| 54 | char *line = readline(">>> "); |
| 55 | if (line == NULL) { |
| 56 | // EOF |
| 57 | return; |
| 58 | } |
| 59 | if (is_compound_stmt(line)) { |
| 60 | for (;;) { |
| 61 | char *line2 = readline("... "); |
| 62 | if (line2 == NULL || strlen(line2) == 0) { |
| 63 | break; |
| 64 | } |
| 65 | char *line3 = str_join(line, '\n', line2); |
| 66 | m_free(line); |
| 67 | m_free(line2); |
| 68 | line = line3; |
| 69 | } |
| 70 | } |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame^] | 71 | py_lexer_t *lex = py_lexer_new_from_str_len("<stdin>", line, strlen(line), false); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 72 | py_parse_node_t pn = py_parse(lex, PY_PARSE_SINGLE_INPUT); |
| 73 | if (pn != PY_PARSE_NODE_NULL) { |
| 74 | //py_parse_node_show(pn, 0); |
| 75 | bool comp_ok = py_compile(pn, true); |
| 76 | if (comp_ok) { |
| 77 | py_obj_t module_fun = rt_make_function_from_id(1); |
| 78 | if (module_fun != py_const_none) { |
| 79 | nlr_buf_t nlr; |
| 80 | if (nlr_push(&nlr) == 0) { |
| 81 | rt_call_function_0(module_fun); |
| 82 | nlr_pop(); |
| 83 | } else { |
| 84 | // uncaught exception |
| 85 | py_obj_print((py_obj_t)nlr.ret_val); |
| 86 | printf("\n"); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void do_file(const char *file) { |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame^] | 95 | py_lexer_t *lex = py_lexer_new_from_file(file); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 96 | //const char *pysrc = "def f():\n x=x+1\n print(42)\n"; |
| 97 | //py_lexer_t *lex = py_lexer_from_str_len("<>", pysrc, strlen(pysrc), false); |
| 98 | if (lex == NULL) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 99 | return; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | if (0) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 103 | // just tokenise |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 104 | while (!py_lexer_is_kind(lex, PY_TOKEN_END)) { |
| 105 | py_token_show(py_lexer_cur(lex)); |
| 106 | py_lexer_to_next(lex); |
| 107 | } |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 108 | py_lexer_free(lex); |
| 109 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 110 | } else { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 111 | // compile |
| 112 | |
| 113 | py_parse_node_t pn = py_parse(lex, PY_PARSE_FILE_INPUT); |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 114 | if (pn != PY_PARSE_NODE_NULL) { |
| 115 | //printf("----------------\n"); |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 116 | //parse_node_show(pn, 0); |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 117 | //printf("----------------\n"); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 118 | bool comp_ok = py_compile(pn, false); |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 119 | //printf("----------------\n"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 120 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 121 | py_lexer_free(lex); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 122 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame^] | 123 | #if MICROPY_EMIT_CPYTHON |
| 124 | if (!comp_ok) { |
| 125 | printf("compile error\n"); |
| 126 | } |
| 127 | #else |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 128 | if (1 && comp_ok) { |
| 129 | // execute it |
| 130 | py_obj_t module_fun = rt_make_function_from_id(1); |
| 131 | if (module_fun != py_const_none) { |
| 132 | nlr_buf_t nlr; |
| 133 | if (nlr_push(&nlr) == 0) { |
| 134 | py_obj_t ret = rt_call_function_0(module_fun); |
| 135 | printf("done! got: "); |
| 136 | py_obj_print(ret); |
| 137 | printf("\n"); |
| 138 | nlr_pop(); |
| 139 | } else { |
| 140 | // uncaught exception |
| 141 | printf("exception: "); |
| 142 | py_obj_print((py_obj_t)nlr.ret_val); |
| 143 | printf("\n"); |
| 144 | } |
| 145 | } |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 146 | } |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 147 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 148 | } |
| 149 | } |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 150 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 151 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 152 | int main(int argc, char **argv) { |
| 153 | qstr_init(); |
| 154 | rt_init(); |
| 155 | |
| 156 | if (argc == 1) { |
| 157 | do_repl(); |
| 158 | } else if (argc == 2) { |
| 159 | do_file(argv[1]); |
| 160 | } else { |
| 161 | printf("usage: py [<file>]\n"); |
| 162 | return 1; |
| 163 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 164 | rt_deinit(); |
| 165 | |
| 166 | //printf("total bytes = %d\n", m_get_total_bytes_allocated()); |
| 167 | return 0; |
| 168 | } |