blob: 911d908b1a0122e5e60a375655721a482eec9590 [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
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020031#if MICROPY_ENABLE_GC
Paul Sokolovskye7db8172014-02-11 00:44:37 +020032// Heap size of GC heap (if enabled)
Damien Georgebd17e1b2014-04-04 14:29:00 +010033// Make it larger on a 64 bit machine, because pointers are larger.
34long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020035#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020036
37// Stack top at the start of program
38void *stack_top;
39
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +020040void file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +020041void microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020042void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020043void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020044
Damien George136f6752014-01-07 14:54:15 +000045static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
46 if (lex == NULL) {
47 return;
48 }
49
50 if (0) {
51 // just tokenise
52 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
53 mp_token_show(mp_lexer_cur(lex));
54 mp_lexer_to_next(lex);
55 }
56 mp_lexer_free(lex);
57 return;
58 }
59
Damien Georgec5966122014-02-15 16:10:44 +000060 mp_parse_error_kind_t parse_error_kind;
61 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +000062
63 if (pn == MP_PARSE_NODE_NULL) {
64 // parse error
Damien Georgec5966122014-02-15 16:10:44 +000065 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +000066 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000067 return;
68 }
69
Damien George08335002014-01-18 23:24:36 +000070 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000071 mp_lexer_free(lex);
72
Damien Georgecbd2f742014-01-19 11:48:48 +000073 /*
74 printf("----------------\n");
75 mp_parse_node_print(pn, 0);
76 printf("----------------\n");
77 */
Damien George136f6752014-01-07 14:54:15 +000078
Damien George08335002014-01-18 23:24:36 +000079 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000080
81 if (module_fun == mp_const_none) {
82 // compile error
83 return;
84 }
85
86 // execute it
87 nlr_buf_t nlr;
88 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +010089 mp_call_function_0(module_fun);
Damien George136f6752014-01-07 14:54:15 +000090 nlr_pop();
91 } else {
92 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000093 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000094 }
95}
96
Paul Sokolovskya0757412014-02-11 12:19:06 +020097static char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010098 int l1 = strlen(s1);
99 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300100 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100101 memcpy(s, s1, l1);
102 if (sep_char != 0) {
103 s[l1] = sep_char;
104 l1 += 1;
105 }
106 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100107 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100108 return s;
109}
110
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200111static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200112#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200113 char *line = readline(p);
114 if (line) {
115 add_history(line);
116 }
117#else
118 static char buf[256];
119 fputs(p, stdout);
120 char *s = fgets(buf, sizeof(buf), stdin);
121 if (!s) {
122 return NULL;
123 }
124 int l = strlen(buf);
125 if (buf[l - 1] == '\n') {
126 buf[l - 1] = 0;
127 } else {
128 l++;
129 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200130 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200131 memcpy(line, buf, l);
132#endif
133 return line;
134}
135
Damiend99b0522013-12-21 18:17:45 +0000136static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100137 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200138 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100139 if (line == NULL) {
140 // EOF
141 return;
142 }
Damiend99b0522013-12-21 18:17:45 +0000143 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100144 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200145 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100146 if (line2 == NULL || strlen(line2) == 0) {
147 break;
148 }
Paul Sokolovskya0757412014-02-11 12:19:06 +0200149 char *line3 = strjoin(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000150 free(line);
151 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100152 line = line3;
153 }
154 }
Damienfa2162b2013-10-20 17:42:00 +0100155
Damien Georgeb829b5c2014-01-25 13:51:19 +0000156 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 +0000157 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200158 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100159 }
160}
161
Damien George136f6752014-01-07 14:54:15 +0000162static void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000163 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000164 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
165}
Damien429d7192013-10-04 19:53:11 +0100166
Damien George136f6752014-01-07 14:54:15 +0000167static void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000168 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 +0000169 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100170}
Damien429d7192013-10-04 19:53:11 +0100171
Damiend99b0522013-12-21 18:17:45 +0000172typedef struct _test_obj_t {
173 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000174 int value;
Damiend99b0522013-12-21 18:17:45 +0000175} test_obj_t;
176
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200177static 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 +0000178 test_obj_t *self = self_in;
179 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000180}
181
Damiend99b0522013-12-21 18:17:45 +0000182static mp_obj_t test_get(mp_obj_t self_in) {
183 test_obj_t *self = self_in;
184 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000185}
186
Damiend99b0522013-12-21 18:17:45 +0000187static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
188 test_obj_t *self = self_in;
189 self->value = mp_obj_get_int(arg);
190 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000191}
192
Damiend99b0522013-12-21 18:17:45 +0000193static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
194static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
195
Damien George9b196cd2014-03-26 21:47:19 +0000196static const mp_map_elem_t test_locals_dict_table[] = {
197 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&test_get_obj },
198 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&test_set_obj },
Damien George97209d32014-01-07 15:58:30 +0000199};
200
Damien George9b196cd2014-03-26 21:47:19 +0000201STATIC MP_DEFINE_CONST_DICT(test_locals_dict, test_locals_dict_table);
202
Damiend99b0522013-12-21 18:17:45 +0000203static const mp_obj_type_t test_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000204 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000205 .name = MP_QSTR_Test,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200206 .print = test_print,
Damien George9b196cd2014-03-26 21:47:19 +0000207 .locals_dict = (mp_obj_t)&test_locals_dict,
Damiena53f6942013-11-02 23:58:38 +0000208};
209
Damiend99b0522013-12-21 18:17:45 +0000210mp_obj_t test_obj_new(int value) {
211 test_obj_t *o = m_new_obj(test_obj_t);
212 o->base.type = &test_type;
213 o->value = value;
214 return o;
215}
216
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300217int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000218 printf(
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300219"usage: %s [-X <opt>] [-c <command>] [<filename>]\n"
Damien George5e349092014-03-08 19:04:47 +0000220"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300221"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300222);
223 int impl_opts_cnt = 0;
224#if MICROPY_ENABLE_GC
225 printf(
Damien George5e349092014-03-08 19:04:47 +0000226" heapsize=<n> -- set the heap size for the GC\n"
227);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300228 impl_opts_cnt++;
229#endif
230
231 if (impl_opts_cnt == 0) {
232 printf(" (none)\n");
233 }
234
Damien George136f6752014-01-07 14:54:15 +0000235 return 1;
236}
237
Damien George4d5b28c2014-01-29 18:56:46 +0000238mp_obj_t mem_info(void) {
239 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
240 return mp_const_none;
241}
242
243mp_obj_t qstr_info(void) {
244 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
245 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
246 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);
247 return mp_const_none;
248}
249
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200250#if MICROPY_ENABLE_GC
251// TODO: this doesn't belong here
252static mp_obj_t pyb_gc(void) {
253 gc_collect();
254 return mp_const_none;
255}
256MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
257#endif
258
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200259// Process options which set interpreter init options
260void pre_process_options(int argc, char **argv) {
261 for (int a = 1; a < argc; a++) {
262 if (argv[a][0] == '-') {
263 if (strcmp(argv[a], "-X") == 0) {
264 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300265 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200266 }
Damien George5e349092014-03-08 19:04:47 +0000267 if (0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200268#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000269 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200270 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200271#endif
Damien George5e349092014-03-08 19:04:47 +0000272 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300273 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000274 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200275 a++;
276 }
277 }
278 }
279}
280
Damien5ac1b2e2013-10-18 19:58:12 +0100281int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200282 volatile int stack_dummy;
283 stack_top = (void*)&stack_dummy;
284
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200285 pre_process_options(argc, argv);
286
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200287#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200288 char *heap = malloc(heap_size);
289 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200290#endif
291
Damien5ac1b2e2013-10-18 19:58:12 +0100292 qstr_init();
Damien Georged17926d2014-03-30 13:35:08 +0100293 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100294
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200295 char *home = getenv("HOME");
296 char *path = getenv("MICROPYPATH");
297 if (path == NULL) {
298 path = "~/.micropython/lib:/usr/lib/micropython";
299 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200300 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200301 for (char *p = path; p != NULL; p = strchr(p, ':')) {
302 path_num++;
303 if (p != NULL) {
304 p++;
305 }
306 }
Damien Georged17926d2014-03-30 13:35:08 +0100307 mp_sys_path = mp_obj_new_list(path_num, NULL);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200308 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100309 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200310 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200311 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200312 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200313 char *p1 = strchr(p, ':');
314 if (p1 == NULL) {
315 p1 = p + strlen(p);
316 }
317 if (p[0] == '~' && p[1] == '/' && home != NULL) {
318 // Expand standalone ~ to $HOME
319 CHECKBUF(buf, PATH_MAX);
320 CHECKBUF_APPEND(buf, home, strlen(home));
321 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200322 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200323 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200324 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200325 }
326 p = p1 + 1;
327 }
328
Damien George55baff42014-01-21 21:40:13 +0000329 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Damien Georged17926d2014-03-30 13:35:08 +0100330 mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200331 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien Georged17926d2014-03-30 13:35:08 +0100332 mp_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200333
Damien Georged17926d2014-03-30 13:35:08 +0100334 mp_store_name(qstr_from_str("test"), test_obj_new(42));
335 mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
336 mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200337#if MICROPY_ENABLE_GC
Damien Georged17926d2014-03-30 13:35:08 +0100338 mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200339#endif
Damien George12eacca2014-01-21 21:54:15 +0000340
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200341 file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +0200342 microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +0200343#if MICROPY_MOD_TIME
344 time_init();
345#endif
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200346#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200347 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200348#endif
Damiena53f6942013-11-02 23:58:38 +0000349
Damien George062478e2014-01-09 20:57:50 +0000350 // Here is some example code to create a class and instance of that class.
351 // First is the Python, then the C code.
352 //
353 // class TestClass:
354 // pass
355 // test_obj = TestClass()
356 // test_obj.attr = 42
357 mp_obj_t test_class_type, test_class_instance;
Damien Georgea71c83a2014-02-15 11:34:50 +0000358 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 +0100359 mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
360 mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000361
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000362 /*
363 printf("bytes:\n");
364 printf(" total %d\n", m_get_total_bytes_allocated());
365 printf(" cur %d\n", m_get_current_bytes_allocated());
366 printf(" peak %d\n", m_get_peak_bytes_allocated());
367 */
368
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200369 bool executed = false;
370 for (int a = 1; a < argc; a++) {
371 if (argv[a][0] == '-') {
372 if (strcmp(argv[a], "-c") == 0) {
373 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300374 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000375 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200376 do_str(argv[a + 1]);
377 executed = true;
378 a += 1;
379 } else if (strcmp(argv[a], "-X") == 0) {
380 a += 1;
Damien George136f6752014-01-07 14:54:15 +0000381 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300382 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000383 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200384 } else {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200385 char *basedir = realpath(argv[a], NULL);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300386 if (basedir == NULL) {
387 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno);
388 perror("");
389 // CPython exits with 2 in such case
390 exit(2);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200391 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300392
393 // Set base dir of the script as first entry in sys.path
394 char *p = strrchr(basedir, '/');
395 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
396 free(basedir);
397
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200398 for (int i = a; i < argc; i++) {
Damien George15d18062014-03-31 16:28:13 +0100399 mp_obj_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200400 }
401 do_file(argv[a]);
402 executed = true;
403 break;
Damien George136f6752014-01-07 14:54:15 +0000404 }
Damien5ac1b2e2013-10-18 19:58:12 +0100405 }
Damien George136f6752014-01-07 14:54:15 +0000406
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200407 if (!executed) {
408 do_repl();
409 }
410
Damien Georged17926d2014-03-30 13:35:08 +0100411 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100412
413 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
414 return 0;
415}
Damien087d2182013-11-09 20:14:30 +0000416
Damien Georgee09ffa12014-02-05 23:57:48 +0000417uint mp_import_stat(const char *path) {
418 struct stat st;
419 if (stat(path, &st) == 0) {
420 if (S_ISDIR(st.st_mode)) {
421 return MP_IMPORT_STAT_DIR;
422 } else if (S_ISREG(st.st_mode)) {
423 return MP_IMPORT_STAT_FILE;
424 }
425 }
426 return MP_IMPORT_STAT_NO_EXIST;
427}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200428
429int DEBUG_printf(const char *fmt, ...) {
430 va_list ap;
431 va_start(ap, fmt);
432 int ret = vfprintf(stderr, fmt, ap);
433 va_end(ap);
434 return ret;
435}