blob: 1549054f04624d45dd118734dad3882baa5d47fc [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 George6827f9f2014-04-07 13:27:50 +010025#include "build/py/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 George65cad122014-04-06 11:48:15 +010032// Default emit options
33uint emit_opt = MP_EMIT_OPT_NONE;
34
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020035#if MICROPY_ENABLE_GC
Paul Sokolovskye7db8172014-02-11 00:44:37 +020036// Heap size of GC heap (if enabled)
Damien Georgebd17e1b2014-04-04 14:29:00 +010037// Make it larger on a 64 bit machine, because pointers are larger.
38long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020039#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020040
41// Stack top at the start of program
42void *stack_top;
43
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +020044void file_init();
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
90 // execute it
91 nlr_buf_t nlr;
92 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +010093 mp_call_function_0(module_fun);
Damien George136f6752014-01-07 14:54:15 +000094 nlr_pop();
95 } else {
96 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000097 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000098 }
99}
100
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300101STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100102 int l1 = strlen(s1);
103 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300104 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100105 memcpy(s, s1, l1);
106 if (sep_char != 0) {
107 s[l1] = sep_char;
108 l1 += 1;
109 }
110 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100111 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100112 return s;
113}
114
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300115STATIC char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200116#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200117 char *line = readline(p);
118 if (line) {
119 add_history(line);
120 }
121#else
122 static char buf[256];
123 fputs(p, stdout);
124 char *s = fgets(buf, sizeof(buf), stdin);
125 if (!s) {
126 return NULL;
127 }
128 int l = strlen(buf);
129 if (buf[l - 1] == '\n') {
130 buf[l - 1] = 0;
131 } else {
132 l++;
133 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200134 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200135 memcpy(line, buf, l);
136#endif
137 return line;
138}
139
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300140STATIC void do_repl(void) {
Damien George6827f9f2014-04-07 13:27:50 +0100141 printf("Micro Python build " MICROPY_GIT_HASH " on " MICROPY_BUILD_DATE "; UNIX version\n");
Damien George6827f9f2014-04-07 13:27:50 +0100142
Damien5ac1b2e2013-10-18 19:58:12 +0100143 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200144 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100145 if (line == NULL) {
146 // EOF
147 return;
148 }
Damiend99b0522013-12-21 18:17:45 +0000149 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100150 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200151 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100152 if (line2 == NULL || strlen(line2) == 0) {
153 break;
154 }
Paul Sokolovskya0757412014-02-11 12:19:06 +0200155 char *line3 = strjoin(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000156 free(line);
157 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100158 line = line3;
159 }
160 }
Damienfa2162b2013-10-20 17:42:00 +0100161
Damien Georgeb829b5c2014-01-25 13:51:19 +0000162 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 +0000163 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200164 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100165 }
166}
167
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300168STATIC void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000169 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000170 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
171}
Damien429d7192013-10-04 19:53:11 +0100172
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300173STATIC void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000174 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 +0000175 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100176}
Damien429d7192013-10-04 19:53:11 +0100177
Damiend99b0522013-12-21 18:17:45 +0000178typedef struct _test_obj_t {
179 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000180 int value;
Damiend99b0522013-12-21 18:17:45 +0000181} test_obj_t;
182
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300183STATIC 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 +0000184 test_obj_t *self = self_in;
185 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000186}
187
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300188STATIC mp_obj_t test_get(mp_obj_t self_in) {
Damiend99b0522013-12-21 18:17:45 +0000189 test_obj_t *self = self_in;
190 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000191}
192
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300193STATIC mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000194 test_obj_t *self = self_in;
195 self->value = mp_obj_get_int(arg);
196 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000197}
198
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300199STATIC MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
200STATIC MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
Damiend99b0522013-12-21 18:17:45 +0000201
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300202STATIC const mp_map_elem_t test_locals_dict_table[] = {
Damien George9b196cd2014-03-26 21:47:19 +0000203 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&test_get_obj },
204 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&test_set_obj },
Damien George97209d32014-01-07 15:58:30 +0000205};
206
Damien George9b196cd2014-03-26 21:47:19 +0000207STATIC MP_DEFINE_CONST_DICT(test_locals_dict, test_locals_dict_table);
208
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300209STATIC const mp_obj_type_t test_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000210 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000211 .name = MP_QSTR_Test,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200212 .print = test_print,
Damien George9b196cd2014-03-26 21:47:19 +0000213 .locals_dict = (mp_obj_t)&test_locals_dict,
Damiena53f6942013-11-02 23:58:38 +0000214};
215
Damiend99b0522013-12-21 18:17:45 +0000216mp_obj_t test_obj_new(int value) {
217 test_obj_t *o = m_new_obj(test_obj_t);
218 o->base.type = &test_type;
219 o->value = value;
220 return o;
221}
222
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300223int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000224 printf(
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300225"usage: %s [-X <opt>] [-c <command>] [<filename>]\n"
Damien George5e349092014-03-08 19:04:47 +0000226"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300227"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300228);
229 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100230 printf(
231" emit={bytecode,native,viper} -- set the default code emitter\n"
232);
233 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300234#if MICROPY_ENABLE_GC
235 printf(
Damien George5e349092014-03-08 19:04:47 +0000236" heapsize=<n> -- set the heap size for the GC\n"
237);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300238 impl_opts_cnt++;
239#endif
240
241 if (impl_opts_cnt == 0) {
242 printf(" (none)\n");
243 }
244
Damien George136f6752014-01-07 14:54:15 +0000245 return 1;
246}
247
Damien George4d5b28c2014-01-29 18:56:46 +0000248mp_obj_t mem_info(void) {
249 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
250 return mp_const_none;
251}
252
253mp_obj_t qstr_info(void) {
254 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
255 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
256 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);
257 return mp_const_none;
258}
259
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200260#if MICROPY_ENABLE_GC
261// TODO: this doesn't belong here
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300262STATIC mp_obj_t pyb_gc(void) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200263 gc_collect();
264 return mp_const_none;
265}
266MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
267#endif
268
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200269// Process options which set interpreter init options
270void pre_process_options(int argc, char **argv) {
271 for (int a = 1; a < argc; a++) {
272 if (argv[a][0] == '-') {
273 if (strcmp(argv[a], "-X") == 0) {
274 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300275 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200276 }
Damien George5e349092014-03-08 19:04:47 +0000277 if (0) {
Damien George65cad122014-04-06 11:48:15 +0100278 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
279 emit_opt = MP_EMIT_OPT_BYTE_CODE;
280 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
281 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
282 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
283 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200284#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000285 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200286 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200287#endif
Damien George5e349092014-03-08 19:04:47 +0000288 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300289 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000290 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200291 a++;
292 }
293 }
294 }
295}
296
Damien5ac1b2e2013-10-18 19:58:12 +0100297int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200298 volatile int stack_dummy;
299 stack_top = (void*)&stack_dummy;
300
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200301 pre_process_options(argc, argv);
302
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200303#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200304 char *heap = malloc(heap_size);
305 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200306#endif
307
Damien5ac1b2e2013-10-18 19:58:12 +0100308 qstr_init();
Damien Georged17926d2014-03-30 13:35:08 +0100309 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100310
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200311 char *home = getenv("HOME");
312 char *path = getenv("MICROPYPATH");
313 if (path == NULL) {
314 path = "~/.micropython/lib:/usr/lib/micropython";
315 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200316 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200317 for (char *p = path; p != NULL; p = strchr(p, ':')) {
318 path_num++;
319 if (p != NULL) {
320 p++;
321 }
322 }
Damien Georged17926d2014-03-30 13:35:08 +0100323 mp_sys_path = mp_obj_new_list(path_num, NULL);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200324 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100325 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200326 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200327 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200328 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200329 char *p1 = strchr(p, ':');
330 if (p1 == NULL) {
331 p1 = p + strlen(p);
332 }
333 if (p[0] == '~' && p[1] == '/' && home != NULL) {
334 // Expand standalone ~ to $HOME
335 CHECKBUF(buf, PATH_MAX);
336 CHECKBUF_APPEND(buf, home, strlen(home));
337 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200338 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200339 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200340 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200341 }
342 p = p1 + 1;
343 }
344
Damien George55baff42014-01-21 21:40:13 +0000345 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Damien Georged17926d2014-03-30 13:35:08 +0100346 mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200347 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien Georged17926d2014-03-30 13:35:08 +0100348 mp_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200349
Damien Georged17926d2014-03-30 13:35:08 +0100350 mp_store_name(qstr_from_str("test"), test_obj_new(42));
351 mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
352 mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200353#if MICROPY_ENABLE_GC
Damien Georged17926d2014-03-30 13:35:08 +0100354 mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200355#endif
Damien George12eacca2014-01-21 21:54:15 +0000356
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200357 file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +0200358 microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +0200359#if MICROPY_MOD_TIME
360 time_init();
361#endif
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200362#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200363 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200364#endif
Damiena53f6942013-11-02 23:58:38 +0000365
Damien George062478e2014-01-09 20:57:50 +0000366 // Here is some example code to create a class and instance of that class.
367 // First is the Python, then the C code.
368 //
369 // class TestClass:
370 // pass
371 // test_obj = TestClass()
372 // test_obj.attr = 42
373 mp_obj_t test_class_type, test_class_instance;
Damien Georgea71c83a2014-02-15 11:34:50 +0000374 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 +0100375 mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
376 mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000377
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000378 /*
379 printf("bytes:\n");
380 printf(" total %d\n", m_get_total_bytes_allocated());
381 printf(" cur %d\n", m_get_current_bytes_allocated());
382 printf(" peak %d\n", m_get_peak_bytes_allocated());
383 */
384
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200385 bool executed = false;
386 for (int a = 1; a < argc; a++) {
387 if (argv[a][0] == '-') {
388 if (strcmp(argv[a], "-c") == 0) {
389 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300390 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000391 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200392 do_str(argv[a + 1]);
393 executed = true;
394 a += 1;
395 } else if (strcmp(argv[a], "-X") == 0) {
396 a += 1;
Damien George136f6752014-01-07 14:54:15 +0000397 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300398 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000399 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200400 } else {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200401 char *basedir = realpath(argv[a], NULL);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300402 if (basedir == NULL) {
403 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno);
404 perror("");
405 // CPython exits with 2 in such case
406 exit(2);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200407 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300408
409 // Set base dir of the script as first entry in sys.path
410 char *p = strrchr(basedir, '/');
411 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
412 free(basedir);
413
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200414 for (int i = a; i < argc; i++) {
Damien George15d18062014-03-31 16:28:13 +0100415 mp_obj_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200416 }
417 do_file(argv[a]);
418 executed = true;
419 break;
Damien George136f6752014-01-07 14:54:15 +0000420 }
Damien5ac1b2e2013-10-18 19:58:12 +0100421 }
Damien George136f6752014-01-07 14:54:15 +0000422
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200423 if (!executed) {
424 do_repl();
425 }
426
Damien Georged17926d2014-03-30 13:35:08 +0100427 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100428
429 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
430 return 0;
431}
Damien087d2182013-11-09 20:14:30 +0000432
Damien Georgee09ffa12014-02-05 23:57:48 +0000433uint mp_import_stat(const char *path) {
434 struct stat st;
435 if (stat(path, &st) == 0) {
436 if (S_ISDIR(st.st_mode)) {
437 return MP_IMPORT_STAT_DIR;
438 } else if (S_ISREG(st.st_mode)) {
439 return MP_IMPORT_STAT_FILE;
440 }
441 }
442 return MP_IMPORT_STAT_NO_EXIST;
443}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200444
445int DEBUG_printf(const char *fmt, ...) {
446 va_list ap;
447 va_start(ap, fmt);
448 int ret = vfprintf(stderr, fmt, ap);
449 va_end(ap);
450 return ret;
451}