blob: faf8e5b276a82676c1574719541e32af035d26e1 [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
Damien8b3a7c22013-10-23 20:20:17 +010031void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +010032 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
Damiena53f6942013-11-02 23:58:38 +0000134void test_print(py_obj_t o_in) {
135 printf("<test>");
136}
137
138py_obj_t test_get(py_obj_t o_in) {
139 py_obj_t d1;
140 py_obj_t d2;
Damien014e19f2013-11-03 14:26:21 +0000141 py_user_get_data(o_in, (machine_uint_t*)&d1, (machine_uint_t*)&d2);
Damiena53f6942013-11-02 23:58:38 +0000142 return d1;
143}
144
145py_obj_t test_set(py_obj_t o_in, py_obj_t arg) {
146 py_user_set_data(o_in, (machine_uint_t)arg, (machine_uint_t)arg);
147 return py_const_none;
148}
149
150const py_user_info_t test_obj_info = {
151 "Test",
152 test_print,
153 {
154 { "get", 0, test_get },
155 { "set", 1, test_set },
156 { NULL, 0, NULL },
157 }
158};
159
Damien5ac1b2e2013-10-18 19:58:12 +0100160int main(int argc, char **argv) {
161 qstr_init();
162 rt_init();
163
Damien014e19f2013-11-03 14:26:21 +0000164 rt_store_name(qstr_from_str_static("test"), py_obj_new_user(&test_obj_info, (machine_uint_t)py_obj_new_int(42), 0));
Damiena53f6942013-11-02 23:58:38 +0000165
Damien5ac1b2e2013-10-18 19:58:12 +0100166 if (argc == 1) {
167 do_repl();
168 } else if (argc == 2) {
169 do_file(argv[1]);
170 } else {
171 printf("usage: py [<file>]\n");
172 return 1;
173 }
Damien429d7192013-10-04 19:53:11 +0100174 rt_deinit();
175
176 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
177 return 0;
178}
Damien087d2182013-11-09 20:14:30 +0000179
180// for sqrt
181#include <math.h>
182machine_float_t machine_sqrt(machine_float_t x) {
183 return sqrt(x);
184}