blob: 8ceaf42646fef626a945283f5cdd815a1901180d [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
4
Damience89a212013-10-15 22:25:17 +01005#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01006#include "misc.h"
Damienc025ebb2013-10-12 14:30:21 +01007#include "mpyconfig.h"
Damien429d7192013-10-04 19:53:11 +01008#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +01009#include "parse.h"
10#include "compile.h"
11#include "runtime.h"
12
13int 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);
Damien91d387d2013-10-09 15:09:52 +010035 if (pn != PY_PARSE_NODE_NULL) {
36 //printf("----------------\n");
Damiena3977762013-10-09 23:10:10 +010037 //parse_node_show(pn, 0);
Damien91d387d2013-10-09 15:09:52 +010038 //printf("----------------\n");
39 py_compile(pn);
40 //printf("----------------\n");
41 }
Damien429d7192013-10-04 19:53:11 +010042 }
43
44 py_lexer_free(lex);
45
Damien3ef4abb2013-10-12 16:53:13 +010046#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +010047 if (1) {
Damien429d7192013-10-04 19:53:11 +010048 // execute it
49 py_obj_t module_fun = rt_make_function_from_id(1);
50 if (module_fun != py_const_none) {
Damience89a212013-10-15 22:25:17 +010051 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 }
Damien429d7192013-10-04 19:53:11 +010064 }
65 }
Damienc025ebb2013-10-12 14:30:21 +010066#endif
Damien429d7192013-10-04 19:53:11 +010067
68 rt_deinit();
69
70 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
71 return 0;
72}