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 | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 9 | #include "parse.h" |
| 10 | #include "compile.h" |
| 11 | #include "runtime.h" |
| 12 | |
| 13 | int main(int argc, char **argv) { |
| 14 | qstr_init(); |
| 15 | rt_init(); |
| 16 | |
| 17 | if (argc != 2) { |
| 18 | printf("usage: py <file>\n"); |
| 19 | return 1; |
| 20 | } |
| 21 | py_lexer_t *lex = py_lexer_from_file(argv[1]); |
| 22 | //const char *pysrc = "def f():\n x=x+1\n print(42)\n"; |
| 23 | //py_lexer_t *lex = py_lexer_from_str_len("<>", pysrc, strlen(pysrc), false); |
| 24 | if (lex == NULL) { |
| 25 | return 1; |
| 26 | } |
| 27 | |
| 28 | if (0) { |
| 29 | while (!py_lexer_is_kind(lex, PY_TOKEN_END)) { |
| 30 | py_token_show(py_lexer_cur(lex)); |
| 31 | py_lexer_to_next(lex); |
| 32 | } |
| 33 | } else { |
| 34 | py_parse_node_t pn = py_parse(lex, 0); |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 35 | if (pn != PY_PARSE_NODE_NULL) { |
| 36 | //printf("----------------\n"); |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 37 | //parse_node_show(pn, 0); |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 38 | //printf("----------------\n"); |
| 39 | py_compile(pn); |
| 40 | //printf("----------------\n"); |
| 41 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | py_lexer_free(lex); |
| 45 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 46 | #if !MICROPY_EMIT_CPYTHON |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 47 | if (1) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 48 | // execute it |
| 49 | py_obj_t module_fun = rt_make_function_from_id(1); |
| 50 | if (module_fun != py_const_none) { |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 51 | nlr_buf_t nlr; |
| 52 | if (nlr_push(&nlr) == 0) { |
| 53 | py_obj_t ret = rt_call_function_0(module_fun); |
| 54 | printf("done! got: "); |
| 55 | py_obj_print(ret); |
| 56 | printf("\n"); |
| 57 | nlr_pop(); |
| 58 | } else { |
| 59 | // uncaught exception |
| 60 | printf("exception: "); |
| 61 | py_obj_print((py_obj_t)nlr.ret_val); |
| 62 | printf("\n"); |
| 63 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 66 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 67 | |
| 68 | rt_deinit(); |
| 69 | |
| 70 | //printf("total bytes = %d\n", m_get_total_bytes_allocated()); |
| 71 | return 0; |
| 72 | } |