blob: 940fe48c14b9fec6f77d03736f65c4b8768384f7 [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"
Damien Georged553be52014-04-17 18:03:27 +010025#include "genhdr/py-version.h"
Damien429d7192013-10-04 19:53:11 +010026
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020027#if MICROPY_USE_READLINE
Damien5ac1b2e2013-10-18 19:58:12 +010028#include <readline/readline.h>
Paul Sokolovsky903b24f2014-01-01 14:54:39 +020029#include <readline/history.h>
Paul Sokolovskyfa027672014-01-01 18:28:01 +020030#endif
Damien429d7192013-10-04 19:53:11 +010031
Damien Georgef22626e2014-04-10 11:30:35 +010032// Command line options, with their defaults
33bool compile_only = false;
Damien George65cad122014-04-06 11:48:15 +010034uint emit_opt = MP_EMIT_OPT_NONE;
35
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020036#if MICROPY_ENABLE_GC
Paul Sokolovskye7db8172014-02-11 00:44:37 +020037// Heap size of GC heap (if enabled)
Damien Georgebd17e1b2014-04-04 14:29:00 +010038// Make it larger on a 64 bit machine, because pointers are larger.
39long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020040#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020041
42// Stack top at the start of program
43void *stack_top;
44
Paul Sokolovsky9945f332014-02-08 21:10:18 +020045void microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020046void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020047void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020048
Paul Sokolovskycd31d822014-04-04 20:25:53 +030049STATIC 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 +000050 if (lex == NULL) {
51 return;
52 }
53
54 if (0) {
55 // just tokenise
56 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
57 mp_token_show(mp_lexer_cur(lex));
58 mp_lexer_to_next(lex);
59 }
60 mp_lexer_free(lex);
61 return;
62 }
63
Damien Georgec5966122014-02-15 16:10:44 +000064 mp_parse_error_kind_t parse_error_kind;
65 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +000066
67 if (pn == MP_PARSE_NODE_NULL) {
68 // parse error
Damien Georgec5966122014-02-15 16:10:44 +000069 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +000070 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000071 return;
72 }
73
Damien George08335002014-01-18 23:24:36 +000074 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000075 mp_lexer_free(lex);
76
Damien Georgecbd2f742014-01-19 11:48:48 +000077 /*
78 printf("----------------\n");
79 mp_parse_node_print(pn, 0);
80 printf("----------------\n");
81 */
Damien George136f6752014-01-07 14:54:15 +000082
Damien George65cad122014-04-06 11:48:15 +010083 mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
Damien George136f6752014-01-07 14:54:15 +000084
85 if (module_fun == mp_const_none) {
86 // compile error
87 return;
88 }
89
Damien Georgef22626e2014-04-10 11:30:35 +010090 if (compile_only) {
91 return;
92 }
93
Damien George136f6752014-01-07 14:54:15 +000094 // execute it
95 nlr_buf_t nlr;
96 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +010097 mp_call_function_0(module_fun);
Damien George136f6752014-01-07 14:54:15 +000098 nlr_pop();
99 } else {
100 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +0000101 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +0000102 }
103}
104
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300105STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100106 int l1 = strlen(s1);
107 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300108 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100109 memcpy(s, s1, l1);
110 if (sep_char != 0) {
111 s[l1] = sep_char;
112 l1 += 1;
113 }
114 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100115 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100116 return s;
117}
118
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300119STATIC char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200120#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200121 char *line = readline(p);
122 if (line) {
123 add_history(line);
124 }
125#else
126 static char buf[256];
127 fputs(p, stdout);
128 char *s = fgets(buf, sizeof(buf), stdin);
129 if (!s) {
130 return NULL;
131 }
132 int l = strlen(buf);
133 if (buf[l - 1] == '\n') {
134 buf[l - 1] = 0;
135 } else {
136 l++;
137 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200138 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200139 memcpy(line, buf, l);
140#endif
141 return line;
142}
143
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300144STATIC void do_repl(void) {
Damien George6827f9f2014-04-07 13:27:50 +0100145 printf("Micro Python build " MICROPY_GIT_HASH " on " MICROPY_BUILD_DATE "; UNIX version\n");
Damien George6827f9f2014-04-07 13:27:50 +0100146
Damien5ac1b2e2013-10-18 19:58:12 +0100147 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200148 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100149 if (line == NULL) {
150 // EOF
151 return;
152 }
Damien George97790452014-04-08 11:04:29 +0000153 while (mp_repl_continue_with_input(line)) {
154 char *line2 = prompt("... ");
155 if (line2 == NULL) {
156 break;
Damien5ac1b2e2013-10-18 19:58:12 +0100157 }
Damien George97790452014-04-08 11:04:29 +0000158 char *line3 = strjoin(line, '\n', line2);
159 free(line);
160 free(line2);
161 line = line3;
Damien5ac1b2e2013-10-18 19:58:12 +0100162 }
Damienfa2162b2013-10-20 17:42:00 +0100163
Damien Georgeb829b5c2014-01-25 13:51:19 +0000164 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 +0000165 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200166 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100167 }
168}
169
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300170STATIC void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000171 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000172 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
173}
Damien429d7192013-10-04 19:53:11 +0100174
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300175STATIC void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000176 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 +0000177 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100178}
Damien429d7192013-10-04 19:53:11 +0100179
Damiend99b0522013-12-21 18:17:45 +0000180typedef struct _test_obj_t {
181 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000182 int value;
Damiend99b0522013-12-21 18:17:45 +0000183} test_obj_t;
184
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300185STATIC 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 +0000186 test_obj_t *self = self_in;
187 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000188}
189
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300190STATIC mp_obj_t test_get(mp_obj_t self_in) {
Damiend99b0522013-12-21 18:17:45 +0000191 test_obj_t *self = self_in;
192 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000193}
194
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300195STATIC mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000196 test_obj_t *self = self_in;
197 self->value = mp_obj_get_int(arg);
198 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000199}
200
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300201STATIC MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
202STATIC MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
Damiend99b0522013-12-21 18:17:45 +0000203
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300204STATIC const mp_map_elem_t test_locals_dict_table[] = {
Damien George9b196cd2014-03-26 21:47:19 +0000205 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&test_get_obj },
206 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&test_set_obj },
Damien George97209d32014-01-07 15:58:30 +0000207};
208
Damien George9b196cd2014-03-26 21:47:19 +0000209STATIC MP_DEFINE_CONST_DICT(test_locals_dict, test_locals_dict_table);
210
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300211STATIC const mp_obj_type_t test_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000212 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000213 .name = MP_QSTR_Test,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200214 .print = test_print,
Damien George9b196cd2014-03-26 21:47:19 +0000215 .locals_dict = (mp_obj_t)&test_locals_dict,
Damiena53f6942013-11-02 23:58:38 +0000216};
217
Damiend99b0522013-12-21 18:17:45 +0000218mp_obj_t test_obj_new(int value) {
219 test_obj_t *o = m_new_obj(test_obj_t);
220 o->base.type = &test_type;
221 o->value = value;
222 return o;
223}
224
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300225int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000226 printf(
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300227"usage: %s [-X <opt>] [-c <command>] [<filename>]\n"
Damien George5e349092014-03-08 19:04:47 +0000228"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300229"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300230);
231 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100232 printf(
Damien Georgef22626e2014-04-10 11:30:35 +0100233" compile-only -- parse and compile only\n"
Damien George65cad122014-04-06 11:48:15 +0100234" emit={bytecode,native,viper} -- set the default code emitter\n"
235);
236 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300237#if MICROPY_ENABLE_GC
238 printf(
Damien George5e349092014-03-08 19:04:47 +0000239" heapsize=<n> -- set the heap size for the GC\n"
240);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300241 impl_opts_cnt++;
242#endif
243
244 if (impl_opts_cnt == 0) {
245 printf(" (none)\n");
246 }
247
Damien George136f6752014-01-07 14:54:15 +0000248 return 1;
249}
250
Damien George4d5b28c2014-01-29 18:56:46 +0000251mp_obj_t mem_info(void) {
252 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
Paul Sokolovsky59a2f482014-04-17 18:32:36 +0300253 gc_dump_info();
Damien George4d5b28c2014-01-29 18:56:46 +0000254 return mp_const_none;
255}
256
257mp_obj_t qstr_info(void) {
258 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
259 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
260 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);
261 return mp_const_none;
262}
263
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200264#if MICROPY_ENABLE_GC
265// TODO: this doesn't belong here
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300266STATIC mp_obj_t pyb_gc(void) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200267 gc_collect();
268 return mp_const_none;
269}
270MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
271#endif
272
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200273// Process options which set interpreter init options
274void pre_process_options(int argc, char **argv) {
275 for (int a = 1; a < argc; a++) {
276 if (argv[a][0] == '-') {
277 if (strcmp(argv[a], "-X") == 0) {
278 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300279 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200280 }
Damien George5e349092014-03-08 19:04:47 +0000281 if (0) {
Damien Georgef22626e2014-04-10 11:30:35 +0100282 } else if (strcmp(argv[a + 1], "compile-only") == 0) {
283 compile_only = true;
Damien George65cad122014-04-06 11:48:15 +0100284 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
285 emit_opt = MP_EMIT_OPT_BYTE_CODE;
286 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
287 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
288 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
289 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200290#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000291 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200292 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200293#endif
Damien George5e349092014-03-08 19:04:47 +0000294 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300295 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000296 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200297 a++;
298 }
299 }
300 }
301}
302
Damien5ac1b2e2013-10-18 19:58:12 +0100303int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200304 volatile int stack_dummy;
305 stack_top = (void*)&stack_dummy;
306
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200307 pre_process_options(argc, argv);
308
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200309#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200310 char *heap = malloc(heap_size);
311 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200312#endif
313
Damien5ac1b2e2013-10-18 19:58:12 +0100314 qstr_init();
Damien Georged17926d2014-03-30 13:35:08 +0100315 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100316
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200317 char *home = getenv("HOME");
318 char *path = getenv("MICROPYPATH");
319 if (path == NULL) {
320 path = "~/.micropython/lib:/usr/lib/micropython";
321 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200322 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200323 for (char *p = path; p != NULL; p = strchr(p, ':')) {
324 path_num++;
325 if (p != NULL) {
326 p++;
327 }
328 }
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300329 mp_obj_list_init(mp_sys_path, path_num);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200330 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100331 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200332 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200333 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200334 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200335 char *p1 = strchr(p, ':');
336 if (p1 == NULL) {
337 p1 = p + strlen(p);
338 }
339 if (p[0] == '~' && p[1] == '/' && home != NULL) {
340 // Expand standalone ~ to $HOME
341 CHECKBUF(buf, PATH_MAX);
342 CHECKBUF_APPEND(buf, home, strlen(home));
343 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200344 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200345 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200346 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200347 }
348 p = p1 + 1;
349 }
350
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300351 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200352
Damien Georged17926d2014-03-30 13:35:08 +0100353 mp_store_name(qstr_from_str("test"), test_obj_new(42));
354 mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
355 mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200356#if MICROPY_ENABLE_GC
Damien Georged17926d2014-03-30 13:35:08 +0100357 mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200358#endif
Damien George12eacca2014-01-21 21:54:15 +0000359
Damien George062478e2014-01-09 20:57:50 +0000360 // Here is some example code to create a class and instance of that class.
361 // First is the Python, then the C code.
362 //
363 // class TestClass:
364 // pass
365 // test_obj = TestClass()
366 // test_obj.attr = 42
367 mp_obj_t test_class_type, test_class_instance;
Damien Georgea71c83a2014-02-15 11:34:50 +0000368 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 +0100369 mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
370 mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000371
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000372 /*
373 printf("bytes:\n");
374 printf(" total %d\n", m_get_total_bytes_allocated());
375 printf(" cur %d\n", m_get_current_bytes_allocated());
376 printf(" peak %d\n", m_get_peak_bytes_allocated());
377 */
378
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200379 bool executed = false;
380 for (int a = 1; a < argc; a++) {
381 if (argv[a][0] == '-') {
382 if (strcmp(argv[a], "-c") == 0) {
383 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300384 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000385 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200386 do_str(argv[a + 1]);
387 executed = true;
388 a += 1;
389 } else if (strcmp(argv[a], "-X") == 0) {
390 a += 1;
Damien George136f6752014-01-07 14:54:15 +0000391 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300392 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000393 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200394 } else {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200395 char *basedir = realpath(argv[a], NULL);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300396 if (basedir == NULL) {
397 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno);
398 perror("");
399 // CPython exits with 2 in such case
400 exit(2);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200401 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300402
403 // Set base dir of the script as first entry in sys.path
404 char *p = strrchr(basedir, '/');
405 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
406 free(basedir);
407
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200408 for (int i = a; i < argc; i++) {
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300409 mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200410 }
411 do_file(argv[a]);
412 executed = true;
413 break;
Damien George136f6752014-01-07 14:54:15 +0000414 }
Damien5ac1b2e2013-10-18 19:58:12 +0100415 }
Damien George136f6752014-01-07 14:54:15 +0000416
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200417 if (!executed) {
418 do_repl();
419 }
420
Damien Georged17926d2014-03-30 13:35:08 +0100421 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100422
423 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
424 return 0;
425}
Damien087d2182013-11-09 20:14:30 +0000426
Damien Georgee09ffa12014-02-05 23:57:48 +0000427uint mp_import_stat(const char *path) {
428 struct stat st;
429 if (stat(path, &st) == 0) {
430 if (S_ISDIR(st.st_mode)) {
431 return MP_IMPORT_STAT_DIR;
432 } else if (S_ISREG(st.st_mode)) {
433 return MP_IMPORT_STAT_FILE;
434 }
435 }
436 return MP_IMPORT_STAT_NO_EXIST;
437}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200438
439int DEBUG_printf(const char *fmt, ...) {
440 va_list ap;
441 va_start(ap, fmt);
442 int ret = vfprintf(stderr, fmt, ap);
443 va_end(ap);
444 return ret;
445}
Damien George26cf55a2014-04-08 14:08:14 +0000446
447void nlr_jump_fail(void *val) {
448 printf("FATAL: uncaught NLR %p\n", val);
449 exit(1);
450}