blob: 9fb25a43e275383abe7a231f4593186ad0e75401 [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) {
Damiend99b0522013-12-21 18:17:45 +0000150 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000151 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
152}
Damien429d7192013-10-04 19:53:11 +0100153
Damien George136f6752014-01-07 14:54:15 +0000154static void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000155 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 +0000156 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100157}
Damien429d7192013-10-04 19:53:11 +0100158
Damiend99b0522013-12-21 18:17:45 +0000159typedef struct _test_obj_t {
160 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000161 int value;
Damiend99b0522013-12-21 18:17:45 +0000162} test_obj_t;
163
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200164static 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 +0000165 test_obj_t *self = self_in;
166 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000167}
168
Damiend99b0522013-12-21 18:17:45 +0000169static mp_obj_t test_get(mp_obj_t self_in) {
170 test_obj_t *self = self_in;
171 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000172}
173
Damiend99b0522013-12-21 18:17:45 +0000174static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
175 test_obj_t *self = self_in;
176 self->value = mp_obj_get_int(arg);
177 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000178}
179
Damiend99b0522013-12-21 18:17:45 +0000180static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
181static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
182
Damien George97209d32014-01-07 15:58:30 +0000183static const mp_method_t test_methods[] = {
184 { "get", &test_get_obj },
185 { "set", &test_set_obj },
186 { NULL, NULL },
187};
188
Damiend99b0522013-12-21 18:17:45 +0000189static const mp_obj_type_t test_type = {
190 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000191 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200192 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000193 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000194};
195
Damiend99b0522013-12-21 18:17:45 +0000196mp_obj_t test_obj_new(int value) {
197 test_obj_t *o = m_new_obj(test_obj_t);
198 o->base.type = &test_type;
199 o->value = value;
200 return o;
201}
202
Damien George136f6752014-01-07 14:54:15 +0000203int usage(void) {
204 printf("usage: py [-c <command>] [<filename>]\n");
205 return 1;
206}
207
Damien George4d5b28c2014-01-29 18:56:46 +0000208mp_obj_t mem_info(void) {
209 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
210 return mp_const_none;
211}
212
213mp_obj_t qstr_info(void) {
214 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
215 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
216 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);
217 return mp_const_none;
218}
219
Damien5ac1b2e2013-10-18 19:58:12 +0100220int main(int argc, char **argv) {
221 qstr_init();
222 rt_init();
223
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200224 char *home = getenv("HOME");
225 char *path = getenv("MICROPYPATH");
226 if (path == NULL) {
227 path = "~/.micropython/lib:/usr/lib/micropython";
228 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200229 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200230 for (char *p = path; p != NULL; p = strchr(p, ':')) {
231 path_num++;
232 if (p != NULL) {
233 p++;
234 }
235 }
236 sys_path = mp_obj_new_list(path_num, NULL);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200237 mp_obj_t *path_items;
238 mp_obj_list_get(sys_path, &path_num, &path_items);
239 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200240 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200241 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200242 char *p1 = strchr(p, ':');
243 if (p1 == NULL) {
244 p1 = p + strlen(p);
245 }
246 if (p[0] == '~' && p[1] == '/' && home != NULL) {
247 // Expand standalone ~ to $HOME
248 CHECKBUF(buf, PATH_MAX);
249 CHECKBUF_APPEND(buf, home, strlen(home));
250 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200251 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200252 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200253 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200254 }
255 p = p1 + 1;
256 }
257
Damien George55baff42014-01-21 21:40:13 +0000258 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200259 rt_store_attr(m_sys, MP_QSTR_path, sys_path);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200260 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien George55baff42014-01-21 21:40:13 +0000261 rt_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200262
Damien George55baff42014-01-21 21:40:13 +0000263 rt_store_name(qstr_from_str("test"), test_obj_new(42));
Damien George4d5b28c2014-01-29 18:56:46 +0000264 rt_store_name(qstr_from_str("mem_info"), rt_make_function_n(0, mem_info));
265 rt_store_name(qstr_from_str("qstr_info"), rt_make_function_n(0, qstr_info));
Damien George12eacca2014-01-21 21:54:15 +0000266
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200267 file_init();
Paul Sokolovskyfc926082014-01-18 23:47:44 +0200268 rawsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +0200269#if MICROPY_MOD_TIME
270 time_init();
271#endif
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200272#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200273 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200274#endif
Damiena53f6942013-11-02 23:58:38 +0000275
Damien George062478e2014-01-09 20:57:50 +0000276 // Here is some example code to create a class and instance of that class.
277 // First is the Python, then the C code.
278 //
279 // class TestClass:
280 // pass
281 // test_obj = TestClass()
282 // test_obj.attr = 42
283 mp_obj_t test_class_type, test_class_instance;
Damien George5fa93b62014-01-22 14:35:10 +0000284 test_class_type = mp_obj_new_type("TestClass", mp_const_empty_tuple, mp_obj_new_dict(0));
Damien George55baff42014-01-21 21:40:13 +0000285 rt_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
286 rt_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000287
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000288 /*
289 printf("bytes:\n");
290 printf(" total %d\n", m_get_total_bytes_allocated());
291 printf(" cur %d\n", m_get_current_bytes_allocated());
292 printf(" peak %d\n", m_get_peak_bytes_allocated());
293 */
294
Damien5ac1b2e2013-10-18 19:58:12 +0100295 if (argc == 1) {
296 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100297 } else {
Damien George136f6752014-01-07 14:54:15 +0000298 for (int a = 1; a < argc; a++) {
299 if (argv[a][0] == '-') {
300 if (strcmp(argv[a], "-c") == 0) {
301 if (a + 1 >= argc) {
302 return usage();
303 }
304 do_str(argv[a + 1]);
305 a += 1;
306 } else {
307 return usage();
308 }
309 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200310 // Set base dir of the script as first entry in sys.path
311 char *basedir = realpath(argv[a], NULL);
312 if (basedir != NULL) {
313 char *p = strrchr(basedir, '/');
314 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
315 free(basedir);
316 }
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200317 for (int i = a; i < argc; i++) {
Damien George55baff42014-01-21 21:40:13 +0000318 rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200319 }
Damien George136f6752014-01-07 14:54:15 +0000320 do_file(argv[a]);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200321 break;
Damien George136f6752014-01-07 14:54:15 +0000322 }
323 }
Damien5ac1b2e2013-10-18 19:58:12 +0100324 }
Damien George136f6752014-01-07 14:54:15 +0000325
Damien429d7192013-10-04 19:53:11 +0100326 rt_deinit();
327
328 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
329 return 0;
330}
Damien087d2182013-11-09 20:14:30 +0000331
332// for sqrt
333#include <math.h>
334machine_float_t machine_sqrt(machine_float_t x) {
335 return sqrt(x);
336}
Damien Georgee09ffa12014-02-05 23:57:48 +0000337
338#include <sys/stat.h>
339
340uint mp_import_stat(const char *path) {
341 struct stat st;
342 if (stat(path, &st) == 0) {
343 if (S_ISDIR(st.st_mode)) {
344 return MP_IMPORT_STAT_DIR;
345 } else if (S_ISREG(st.st_mode)) {
346 return MP_IMPORT_STAT_FILE;
347 }
348 }
349 return MP_IMPORT_STAT_NO_EXIST;
350}