blob: 3ebd9ab82b56fc05cb285e660b9a4e23b2ecfda7 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
xbec93a2212014-03-15 23:24:34 -07002#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01003#include <stdio.h>
4#include <string.h>
Edd Barrett8146aea2014-01-01 23:14:36 +00005#include <stdlib.h>
Paul Sokolovsky44739e22014-02-16 18:11:42 +02006#include <stdarg.h>
xbec93a2212014-03-15 23:24:34 -07007#include <sys/stat.h>
8#include <sys/types.h>
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +03009#include <errno.h>
Damien429d7192013-10-04 19:53:11 +010010
Damience89a212013-10-15 22:25:17 +010011#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010012#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000014#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010015#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010016#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010017#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000018#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000019#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000020#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000021#include "runtime0.h"
22#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010023#include "repl.h"
Paul Sokolovskye7db8172014-02-11 00:44:37 +020024#include "gc.h"
Damien429d7192013-10-04 19:53:11 +010025
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020026#if MICROPY_USE_READLINE
Damien5ac1b2e2013-10-18 19:58:12 +010027#include <readline/readline.h>
Paul Sokolovsky903b24f2014-01-01 14:54:39 +020028#include <readline/history.h>
Paul Sokolovskyfa027672014-01-01 18:28:01 +020029#endif
Damien429d7192013-10-04 19:53:11 +010030
Damien George65cad122014-04-06 11:48:15 +010031// Default emit options
32uint emit_opt = MP_EMIT_OPT_NONE;
33
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020034#if MICROPY_ENABLE_GC
Paul Sokolovskye7db8172014-02-11 00:44:37 +020035// Heap size of GC heap (if enabled)
Damien Georgebd17e1b2014-04-04 14:29:00 +010036// Make it larger on a 64 bit machine, because pointers are larger.
37long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020038#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020039
40// Stack top at the start of program
41void *stack_top;
42
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +020043void file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +020044void microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020045void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020046void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020047
Paul Sokolovskycd31d822014-04-04 20:25:53 +030048STATIC void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
Damien George136f6752014-01-07 14:54:15 +000049 if (lex == NULL) {
50 return;
51 }
52
53 if (0) {
54 // just tokenise
55 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
56 mp_token_show(mp_lexer_cur(lex));
57 mp_lexer_to_next(lex);
58 }
59 mp_lexer_free(lex);
60 return;
61 }
62
Damien Georgec5966122014-02-15 16:10:44 +000063 mp_parse_error_kind_t parse_error_kind;
64 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +000065
66 if (pn == MP_PARSE_NODE_NULL) {
67 // parse error
Damien Georgec5966122014-02-15 16:10:44 +000068 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +000069 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000070 return;
71 }
72
Damien George08335002014-01-18 23:24:36 +000073 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000074 mp_lexer_free(lex);
75
Damien Georgecbd2f742014-01-19 11:48:48 +000076 /*
77 printf("----------------\n");
78 mp_parse_node_print(pn, 0);
79 printf("----------------\n");
80 */
Damien George136f6752014-01-07 14:54:15 +000081
Damien George65cad122014-04-06 11:48:15 +010082 mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
Damien George136f6752014-01-07 14:54:15 +000083
84 if (module_fun == mp_const_none) {
85 // compile error
86 return;
87 }
88
89 // execute it
90 nlr_buf_t nlr;
91 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +010092 mp_call_function_0(module_fun);
Damien George136f6752014-01-07 14:54:15 +000093 nlr_pop();
94 } else {
95 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000096 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000097 }
98}
99
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300100STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100101 int l1 = strlen(s1);
102 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300103 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100104 memcpy(s, s1, l1);
105 if (sep_char != 0) {
106 s[l1] = sep_char;
107 l1 += 1;
108 }
109 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100110 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100111 return s;
112}
113
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300114STATIC char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200115#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200116 char *line = readline(p);
117 if (line) {
118 add_history(line);
119 }
120#else
121 static char buf[256];
122 fputs(p, stdout);
123 char *s = fgets(buf, sizeof(buf), stdin);
124 if (!s) {
125 return NULL;
126 }
127 int l = strlen(buf);
128 if (buf[l - 1] == '\n') {
129 buf[l - 1] = 0;
130 } else {
131 l++;
132 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200133 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200134 memcpy(line, buf, l);
135#endif
136 return line;
137}
138
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300139STATIC void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100140 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200141 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100142 if (line == NULL) {
143 // EOF
144 return;
145 }
Damiend99b0522013-12-21 18:17:45 +0000146 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100147 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200148 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100149 if (line2 == NULL || strlen(line2) == 0) {
150 break;
151 }
Paul Sokolovskya0757412014-02-11 12:19:06 +0200152 char *line3 = strjoin(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000153 free(line);
154 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100155 line = line3;
156 }
157 }
Damienfa2162b2013-10-20 17:42:00 +0100158
Damien Georgeb829b5c2014-01-25 13:51:19 +0000159 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 +0000160 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200161 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100162 }
163}
164
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300165STATIC void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000166 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000167 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
168}
Damien429d7192013-10-04 19:53:11 +0100169
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300170STATIC void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000171 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 +0000172 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100173}
Damien429d7192013-10-04 19:53:11 +0100174
Damiend99b0522013-12-21 18:17:45 +0000175typedef struct _test_obj_t {
176 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000177 int value;
Damiend99b0522013-12-21 18:17:45 +0000178} test_obj_t;
179
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300180STATIC 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 +0000181 test_obj_t *self = self_in;
182 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000183}
184
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300185STATIC mp_obj_t test_get(mp_obj_t self_in) {
Damiend99b0522013-12-21 18:17:45 +0000186 test_obj_t *self = self_in;
187 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000188}
189
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300190STATIC mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000191 test_obj_t *self = self_in;
192 self->value = mp_obj_get_int(arg);
193 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000194}
195
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300196STATIC MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
197STATIC MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
Damiend99b0522013-12-21 18:17:45 +0000198
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300199STATIC const mp_map_elem_t test_locals_dict_table[] = {
Damien George9b196cd2014-03-26 21:47:19 +0000200 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&test_get_obj },
201 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&test_set_obj },
Damien George97209d32014-01-07 15:58:30 +0000202};
203
Damien George9b196cd2014-03-26 21:47:19 +0000204STATIC MP_DEFINE_CONST_DICT(test_locals_dict, test_locals_dict_table);
205
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300206STATIC const mp_obj_type_t test_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000207 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000208 .name = MP_QSTR_Test,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200209 .print = test_print,
Damien George9b196cd2014-03-26 21:47:19 +0000210 .locals_dict = (mp_obj_t)&test_locals_dict,
Damiena53f6942013-11-02 23:58:38 +0000211};
212
Damiend99b0522013-12-21 18:17:45 +0000213mp_obj_t test_obj_new(int value) {
214 test_obj_t *o = m_new_obj(test_obj_t);
215 o->base.type = &test_type;
216 o->value = value;
217 return o;
218}
219
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300220int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000221 printf(
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300222"usage: %s [-X <opt>] [-c <command>] [<filename>]\n"
Damien George5e349092014-03-08 19:04:47 +0000223"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300224"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300225);
226 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100227 printf(
228" emit={bytecode,native,viper} -- set the default code emitter\n"
229);
230 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300231#if MICROPY_ENABLE_GC
232 printf(
Damien George5e349092014-03-08 19:04:47 +0000233" heapsize=<n> -- set the heap size for the GC\n"
234);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300235 impl_opts_cnt++;
236#endif
237
238 if (impl_opts_cnt == 0) {
239 printf(" (none)\n");
240 }
241
Damien George136f6752014-01-07 14:54:15 +0000242 return 1;
243}
244
Damien George4d5b28c2014-01-29 18:56:46 +0000245mp_obj_t mem_info(void) {
246 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
247 return mp_const_none;
248}
249
250mp_obj_t qstr_info(void) {
251 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
252 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
253 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);
254 return mp_const_none;
255}
256
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200257#if MICROPY_ENABLE_GC
258// TODO: this doesn't belong here
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300259STATIC mp_obj_t pyb_gc(void) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200260 gc_collect();
261 return mp_const_none;
262}
263MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
264#endif
265
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200266// Process options which set interpreter init options
267void pre_process_options(int argc, char **argv) {
268 for (int a = 1; a < argc; a++) {
269 if (argv[a][0] == '-') {
270 if (strcmp(argv[a], "-X") == 0) {
271 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300272 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200273 }
Damien George5e349092014-03-08 19:04:47 +0000274 if (0) {
Damien George65cad122014-04-06 11:48:15 +0100275 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
276 emit_opt = MP_EMIT_OPT_BYTE_CODE;
277 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
278 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
279 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
280 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200281#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000282 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200283 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200284#endif
Damien George5e349092014-03-08 19:04:47 +0000285 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300286 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000287 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200288 a++;
289 }
290 }
291 }
292}
293
Damien5ac1b2e2013-10-18 19:58:12 +0100294int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200295 volatile int stack_dummy;
296 stack_top = (void*)&stack_dummy;
297
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200298 pre_process_options(argc, argv);
299
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200300#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200301 char *heap = malloc(heap_size);
302 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200303#endif
304
Damien5ac1b2e2013-10-18 19:58:12 +0100305 qstr_init();
Damien Georged17926d2014-03-30 13:35:08 +0100306 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100307
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200308 char *home = getenv("HOME");
309 char *path = getenv("MICROPYPATH");
310 if (path == NULL) {
311 path = "~/.micropython/lib:/usr/lib/micropython";
312 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200313 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200314 for (char *p = path; p != NULL; p = strchr(p, ':')) {
315 path_num++;
316 if (p != NULL) {
317 p++;
318 }
319 }
Damien Georged17926d2014-03-30 13:35:08 +0100320 mp_sys_path = mp_obj_new_list(path_num, NULL);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200321 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100322 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200323 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200324 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200325 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200326 char *p1 = strchr(p, ':');
327 if (p1 == NULL) {
328 p1 = p + strlen(p);
329 }
330 if (p[0] == '~' && p[1] == '/' && home != NULL) {
331 // Expand standalone ~ to $HOME
332 CHECKBUF(buf, PATH_MAX);
333 CHECKBUF_APPEND(buf, home, strlen(home));
334 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200335 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200336 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200337 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200338 }
339 p = p1 + 1;
340 }
341
Damien George55baff42014-01-21 21:40:13 +0000342 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Damien Georged17926d2014-03-30 13:35:08 +0100343 mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200344 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien Georged17926d2014-03-30 13:35:08 +0100345 mp_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200346
Damien Georged17926d2014-03-30 13:35:08 +0100347 mp_store_name(qstr_from_str("test"), test_obj_new(42));
348 mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
349 mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200350#if MICROPY_ENABLE_GC
Damien Georged17926d2014-03-30 13:35:08 +0100351 mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200352#endif
Damien George12eacca2014-01-21 21:54:15 +0000353
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200354 file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +0200355 microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +0200356#if MICROPY_MOD_TIME
357 time_init();
358#endif
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200359#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200360 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200361#endif
Damiena53f6942013-11-02 23:58:38 +0000362
Damien George062478e2014-01-09 20:57:50 +0000363 // Here is some example code to create a class and instance of that class.
364 // First is the Python, then the C code.
365 //
366 // class TestClass:
367 // pass
368 // test_obj = TestClass()
369 // test_obj.attr = 42
370 mp_obj_t test_class_type, test_class_instance;
Damien Georgea71c83a2014-02-15 11:34:50 +0000371 test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
Damien Georged17926d2014-03-30 13:35:08 +0100372 mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
373 mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000374
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000375 /*
376 printf("bytes:\n");
377 printf(" total %d\n", m_get_total_bytes_allocated());
378 printf(" cur %d\n", m_get_current_bytes_allocated());
379 printf(" peak %d\n", m_get_peak_bytes_allocated());
380 */
381
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200382 bool executed = false;
383 for (int a = 1; a < argc; a++) {
384 if (argv[a][0] == '-') {
385 if (strcmp(argv[a], "-c") == 0) {
386 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300387 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000388 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200389 do_str(argv[a + 1]);
390 executed = true;
391 a += 1;
392 } else if (strcmp(argv[a], "-X") == 0) {
393 a += 1;
Damien George136f6752014-01-07 14:54:15 +0000394 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300395 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000396 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200397 } else {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200398 char *basedir = realpath(argv[a], NULL);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300399 if (basedir == NULL) {
400 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno);
401 perror("");
402 // CPython exits with 2 in such case
403 exit(2);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200404 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300405
406 // Set base dir of the script as first entry in sys.path
407 char *p = strrchr(basedir, '/');
408 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
409 free(basedir);
410
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200411 for (int i = a; i < argc; i++) {
Damien George15d18062014-03-31 16:28:13 +0100412 mp_obj_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200413 }
414 do_file(argv[a]);
415 executed = true;
416 break;
Damien George136f6752014-01-07 14:54:15 +0000417 }
Damien5ac1b2e2013-10-18 19:58:12 +0100418 }
Damien George136f6752014-01-07 14:54:15 +0000419
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200420 if (!executed) {
421 do_repl();
422 }
423
Damien Georged17926d2014-03-30 13:35:08 +0100424 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100425
426 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
427 return 0;
428}
Damien087d2182013-11-09 20:14:30 +0000429
Damien Georgee09ffa12014-02-05 23:57:48 +0000430uint mp_import_stat(const char *path) {
431 struct stat st;
432 if (stat(path, &st) == 0) {
433 if (S_ISDIR(st.st_mode)) {
434 return MP_IMPORT_STAT_DIR;
435 } else if (S_ISREG(st.st_mode)) {
436 return MP_IMPORT_STAT_FILE;
437 }
438 }
439 return MP_IMPORT_STAT_NO_EXIST;
440}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200441
442int DEBUG_printf(const char *fmt, ...) {
443 va_list ap;
444 va_start(ap, fmt);
445 int ret = vfprintf(stderr, fmt, ap);
446 va_end(ap);
447 return ret;
448}