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