blob: 12aca6ddf29fa10b79c2f42dd6bc44d7809d9cfa [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"
Damiena5185f42013-10-20 14:41:27 +01009#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010010#include "parse.h"
11#include "compile.h"
12#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010013#include "repl.h"
Damien429d7192013-10-04 19:53:11 +010014
Damien5ac1b2e2013-10-18 19:58:12 +010015#include <readline/readline.h>
Damien429d7192013-10-04 19:53:11 +010016
Damien5ac1b2e2013-10-18 19:58:12 +010017char *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);
Damien92c06562013-10-22 22:32:27 +010027 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010028 return s;
29}
30
31void do_repl() {
32 for (;;) {
33 char *line = readline(">>> ");
34 if (line == NULL) {
35 // EOF
36 return;
37 }
Damien92c06562013-10-22 22:32:27 +010038 if (py_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +010039 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 }
Damienfa2162b2013-10-20 17:42:00 +010050
Damiena5185f42013-10-20 14:41:27 +010051 py_lexer_t *lex = py_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
Damien5ac1b2e2013-10-18 19:58:12 +010052 py_parse_node_t pn = py_parse(lex, PY_PARSE_SINGLE_INPUT);
Damienfa2162b2013-10-20 17:42:00 +010053 py_lexer_free(lex);
54
Damien5ac1b2e2013-10-18 19:58:12 +010055 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
76void do_file(const char *file) {
Damiena5185f42013-10-20 14:41:27 +010077 py_lexer_t *lex = py_lexer_new_from_file(file);
Damien429d7192013-10-04 19:53:11 +010078 //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) {
Damien5ac1b2e2013-10-18 19:58:12 +010081 return;
Damien429d7192013-10-04 19:53:11 +010082 }
83
84 if (0) {
Damien5ac1b2e2013-10-18 19:58:12 +010085 // just tokenise
Damien429d7192013-10-04 19:53:11 +010086 while (!py_lexer_is_kind(lex, PY_TOKEN_END)) {
87 py_token_show(py_lexer_cur(lex));
88 py_lexer_to_next(lex);
89 }
Damien5ac1b2e2013-10-18 19:58:12 +010090 py_lexer_free(lex);
91
Damien429d7192013-10-04 19:53:11 +010092 } else {
Damien5ac1b2e2013-10-18 19:58:12 +010093 // compile
94
95 py_parse_node_t pn = py_parse(lex, PY_PARSE_FILE_INPUT);
Damienfa2162b2013-10-20 17:42:00 +010096 py_lexer_free(lex);
97
Damien91d387d2013-10-09 15:09:52 +010098 if (pn != PY_PARSE_NODE_NULL) {
99 //printf("----------------\n");
Damiena3977762013-10-09 23:10:10 +0100100 //parse_node_show(pn, 0);
Damien91d387d2013-10-09 15:09:52 +0100101 //printf("----------------\n");
Damien5ac1b2e2013-10-18 19:58:12 +0100102 bool comp_ok = py_compile(pn, false);
Damien91d387d2013-10-09 15:09:52 +0100103 //printf("----------------\n");
Damien429d7192013-10-04 19:53:11 +0100104
Damiena5185f42013-10-20 14:41:27 +0100105#if MICROPY_EMIT_CPYTHON
106 if (!comp_ok) {
107 printf("compile error\n");
108 }
109#else
Damien5ac1b2e2013-10-18 19:58:12 +0100110 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 }
Damience89a212013-10-15 22:25:17 +0100128 }
Damien5ac1b2e2013-10-18 19:58:12 +0100129#endif
Damien429d7192013-10-04 19:53:11 +0100130 }
131 }
Damien5ac1b2e2013-10-18 19:58:12 +0100132}
Damien429d7192013-10-04 19:53:11 +0100133
Damien5ac1b2e2013-10-18 19:58:12 +0100134int 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 }
Damien429d7192013-10-04 19:53:11 +0100146 rt_deinit();
147
148 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
149 return 0;
150}