blob: cc942163f9871a548f01d8bafb96e3b8a6cbbb18 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
Edd Barrett8146aea2014-01-01 23:14:36 +00004#include <stdlib.h>
Damien429d7192013-10-04 19:53:11 +01005
Damience89a212013-10-15 22:25:17 +01006#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01007#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010010#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010011#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000013#include "obj.h"
Damien George1fb03172014-01-03 14:22:03 +000014#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "runtime0.h"
16#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010017#include "repl.h"
Damien429d7192013-10-04 19:53:11 +010018
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020019#if MICROPY_USE_READLINE
Damien5ac1b2e2013-10-18 19:58:12 +010020#include <readline/readline.h>
Paul Sokolovsky903b24f2014-01-01 14:54:39 +020021#include <readline/history.h>
Paul Sokolovskyfa027672014-01-01 18:28:01 +020022#endif
Damien429d7192013-10-04 19:53:11 +010023
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020024extern const mp_obj_fun_native_t mp_builtin_open_obj;
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +020025void file_init();
Paul Sokolovskyfc926082014-01-18 23:47:44 +020026void rawsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020027void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020028void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020029
Damien George136f6752014-01-07 14:54:15 +000030static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
31 if (lex == NULL) {
32 return;
33 }
34
35 if (0) {
36 // just tokenise
37 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
38 mp_token_show(mp_lexer_cur(lex));
39 mp_lexer_to_next(lex);
40 }
41 mp_lexer_free(lex);
42 return;
43 }
44
Damien George9528cd62014-01-15 21:23:31 +000045 qstr parse_exc_id;
46 const char *parse_exc_msg;
47 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_exc_id, &parse_exc_msg);
Damien George136f6752014-01-07 14:54:15 +000048
49 if (pn == MP_PARSE_NODE_NULL) {
50 // parse error
Damien George9528cd62014-01-15 21:23:31 +000051 mp_lexer_show_error_pythonic_prefix(lex);
52 printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
53 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000054 return;
55 }
56
Damien George08335002014-01-18 23:24:36 +000057 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000058 mp_lexer_free(lex);
59
Damien Georgecbd2f742014-01-19 11:48:48 +000060 /*
61 printf("----------------\n");
62 mp_parse_node_print(pn, 0);
63 printf("----------------\n");
64 */
Damien George136f6752014-01-07 14:54:15 +000065
Damien George08335002014-01-18 23:24:36 +000066 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000067
68 if (module_fun == mp_const_none) {
69 // compile error
70 return;
71 }
72
73 // execute it
74 nlr_buf_t nlr;
75 if (nlr_push(&nlr) == 0) {
76 rt_call_function_0(module_fun);
77 nlr_pop();
78 } else {
79 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000080 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000081 }
82}
83
Damiend99b0522013-12-21 18:17:45 +000084static char *str_join(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010085 int l1 = strlen(s1);
86 int l2 = strlen(s2);
87 char *s = m_new(char, l1 + l2 + 2);
88 memcpy(s, s1, l1);
89 if (sep_char != 0) {
90 s[l1] = sep_char;
91 l1 += 1;
92 }
93 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +010094 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +010095 return s;
96}
97
Paul Sokolovskyfa027672014-01-01 18:28:01 +020098static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020099#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200100 char *line = readline(p);
101 if (line) {
102 add_history(line);
103 }
104#else
105 static char buf[256];
106 fputs(p, stdout);
107 char *s = fgets(buf, sizeof(buf), stdin);
108 if (!s) {
109 return NULL;
110 }
111 int l = strlen(buf);
112 if (buf[l - 1] == '\n') {
113 buf[l - 1] = 0;
114 } else {
115 l++;
116 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200117 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200118 memcpy(line, buf, l);
119#endif
120 return line;
121}
122
Damiend99b0522013-12-21 18:17:45 +0000123static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100124 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200125 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100126 if (line == NULL) {
127 // EOF
128 return;
129 }
Damiend99b0522013-12-21 18:17:45 +0000130 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100131 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200132 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100133 if (line2 == NULL || strlen(line2) == 0) {
134 break;
135 }
136 char *line3 = str_join(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000137 free(line);
138 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100139 line = line3;
140 }
141 }
Damienfa2162b2013-10-20 17:42:00 +0100142
Damien Georgeb829b5c2014-01-25 13:51:19 +0000143 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000144 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200145 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100146 }
147}
148
Damien George136f6752014-01-07 14:54:15 +0000149static void do_file(const char *file) {
Damien George66028ab2014-01-03 14:03:48 +0000150 // hack: set dir for import based on where this file is
151 {
152 const char * s = strrchr(file, '/');
153 if (s != NULL) {
154 int len = s - file;
155 char *dir = m_new(char, len + 1);
156 memcpy(dir, file, len);
157 dir[len] = '\0';
158 mp_import_set_directory(dir);
159 }
160 }
161
Damiend99b0522013-12-21 18:17:45 +0000162 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000163 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
164}
Damien429d7192013-10-04 19:53:11 +0100165
Damien George136f6752014-01-07 14:54:15 +0000166static void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000167 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
Damien George136f6752014-01-07 14:54:15 +0000168 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100169}
Damien429d7192013-10-04 19:53:11 +0100170
Damiend99b0522013-12-21 18:17:45 +0000171typedef struct _test_obj_t {
172 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000173 int value;
Damiend99b0522013-12-21 18:17:45 +0000174} test_obj_t;
175
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200176static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +0000177 test_obj_t *self = self_in;
178 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000179}
180
Damiend99b0522013-12-21 18:17:45 +0000181static mp_obj_t test_get(mp_obj_t self_in) {
182 test_obj_t *self = self_in;
183 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000184}
185
Damiend99b0522013-12-21 18:17:45 +0000186static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
187 test_obj_t *self = self_in;
188 self->value = mp_obj_get_int(arg);
189 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000190}
191
Damiend99b0522013-12-21 18:17:45 +0000192static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
193static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
194
Damien George97209d32014-01-07 15:58:30 +0000195static const mp_method_t test_methods[] = {
196 { "get", &test_get_obj },
197 { "set", &test_set_obj },
198 { NULL, NULL },
199};
200
Damiend99b0522013-12-21 18:17:45 +0000201static const mp_obj_type_t test_type = {
202 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000203 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200204 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000205 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000206};
207
Damiend99b0522013-12-21 18:17:45 +0000208mp_obj_t test_obj_new(int value) {
209 test_obj_t *o = m_new_obj(test_obj_t);
210 o->base.type = &test_type;
211 o->value = value;
212 return o;
213}
214
Damien George136f6752014-01-07 14:54:15 +0000215int usage(void) {
216 printf("usage: py [-c <command>] [<filename>]\n");
217 return 1;
218}
219
Damien George4d5b28c2014-01-29 18:56:46 +0000220mp_obj_t mem_info(void) {
221 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
222 return mp_const_none;
223}
224
225mp_obj_t qstr_info(void) {
226 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
227 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
228 printf("qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
229 return mp_const_none;
230}
231
Damien5ac1b2e2013-10-18 19:58:12 +0100232int main(int argc, char **argv) {
233 qstr_init();
234 rt_init();
235
Damien George55baff42014-01-21 21:40:13 +0000236 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200237 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien George55baff42014-01-21 21:40:13 +0000238 rt_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200239
Damien George55baff42014-01-21 21:40:13 +0000240 rt_store_name(qstr_from_str("test"), test_obj_new(42));
Damien George4d5b28c2014-01-29 18:56:46 +0000241 rt_store_name(qstr_from_str("mem_info"), rt_make_function_n(0, mem_info));
242 rt_store_name(qstr_from_str("qstr_info"), rt_make_function_n(0, qstr_info));
Damien George12eacca2014-01-21 21:54:15 +0000243
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200244 file_init();
Paul Sokolovskyfc926082014-01-18 23:47:44 +0200245 rawsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +0200246#if MICROPY_MOD_TIME
247 time_init();
248#endif
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200249#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200250 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200251#endif
Damiena53f6942013-11-02 23:58:38 +0000252
Damien George062478e2014-01-09 20:57:50 +0000253 // Here is some example code to create a class and instance of that class.
254 // First is the Python, then the C code.
255 //
256 // class TestClass:
257 // pass
258 // test_obj = TestClass()
259 // test_obj.attr = 42
260 mp_obj_t test_class_type, test_class_instance;
Damien George5fa93b62014-01-22 14:35:10 +0000261 test_class_type = mp_obj_new_type("TestClass", mp_const_empty_tuple, mp_obj_new_dict(0));
Damien George55baff42014-01-21 21:40:13 +0000262 rt_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
263 rt_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000264
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000265 /*
266 printf("bytes:\n");
267 printf(" total %d\n", m_get_total_bytes_allocated());
268 printf(" cur %d\n", m_get_current_bytes_allocated());
269 printf(" peak %d\n", m_get_peak_bytes_allocated());
270 */
271
Damien5ac1b2e2013-10-18 19:58:12 +0100272 if (argc == 1) {
273 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100274 } else {
Damien George136f6752014-01-07 14:54:15 +0000275 for (int a = 1; a < argc; a++) {
276 if (argv[a][0] == '-') {
277 if (strcmp(argv[a], "-c") == 0) {
278 if (a + 1 >= argc) {
279 return usage();
280 }
281 do_str(argv[a + 1]);
282 a += 1;
283 } else {
284 return usage();
285 }
286 } else {
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200287 for (int i = a; i < argc; i++) {
Damien George55baff42014-01-21 21:40:13 +0000288 rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200289 }
Damien George136f6752014-01-07 14:54:15 +0000290 do_file(argv[a]);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200291 break;
Damien George136f6752014-01-07 14:54:15 +0000292 }
293 }
Damien5ac1b2e2013-10-18 19:58:12 +0100294 }
Damien George136f6752014-01-07 14:54:15 +0000295
Damien429d7192013-10-04 19:53:11 +0100296 rt_deinit();
297
298 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
299 return 0;
300}
Damien087d2182013-11-09 20:14:30 +0000301
302// for sqrt
303#include <math.h>
304machine_float_t machine_sqrt(machine_float_t x) {
305 return sqrt(x);
306}