blob: 11df4cadf29fe1da73045bd8727729bddbef8637 [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 }
Damien George97790452014-04-08 11:04:29 +0000149 while (mp_repl_continue_with_input(line)) {
150 char *line2 = prompt("... ");
151 if (line2 == NULL) {
152 break;
Damien5ac1b2e2013-10-18 19:58:12 +0100153 }
Damien George97790452014-04-08 11:04:29 +0000154 char *line3 = strjoin(line, '\n', line2);
155 free(line);
156 free(line2);
157 line = line3;
Damien5ac1b2e2013-10-18 19:58:12 +0100158 }
Damienfa2162b2013-10-20 17:42:00 +0100159
Damien Georgeb829b5c2014-01-25 13:51:19 +0000160 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 +0000161 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200162 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100163 }
164}
165
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300166STATIC void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000167 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000168 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
169}
Damien429d7192013-10-04 19:53:11 +0100170
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300171STATIC void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000172 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 +0000173 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100174}
Damien429d7192013-10-04 19:53:11 +0100175
Damiend99b0522013-12-21 18:17:45 +0000176typedef struct _test_obj_t {
177 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000178 int value;
Damiend99b0522013-12-21 18:17:45 +0000179} test_obj_t;
180
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300181STATIC 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 +0000182 test_obj_t *self = self_in;
183 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000184}
185
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300186STATIC mp_obj_t test_get(mp_obj_t self_in) {
Damiend99b0522013-12-21 18:17:45 +0000187 test_obj_t *self = self_in;
188 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000189}
190
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300191STATIC mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000192 test_obj_t *self = self_in;
193 self->value = mp_obj_get_int(arg);
194 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000195}
196
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300197STATIC MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
198STATIC MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
Damiend99b0522013-12-21 18:17:45 +0000199
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300200STATIC const mp_map_elem_t test_locals_dict_table[] = {
Damien George9b196cd2014-03-26 21:47:19 +0000201 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&test_get_obj },
202 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&test_set_obj },
Damien George97209d32014-01-07 15:58:30 +0000203};
204
Damien George9b196cd2014-03-26 21:47:19 +0000205STATIC MP_DEFINE_CONST_DICT(test_locals_dict, test_locals_dict_table);
206
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300207STATIC const mp_obj_type_t test_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000208 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000209 .name = MP_QSTR_Test,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200210 .print = test_print,
Damien George9b196cd2014-03-26 21:47:19 +0000211 .locals_dict = (mp_obj_t)&test_locals_dict,
Damiena53f6942013-11-02 23:58:38 +0000212};
213
Damiend99b0522013-12-21 18:17:45 +0000214mp_obj_t test_obj_new(int value) {
215 test_obj_t *o = m_new_obj(test_obj_t);
216 o->base.type = &test_type;
217 o->value = value;
218 return o;
219}
220
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300221int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000222 printf(
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300223"usage: %s [-X <opt>] [-c <command>] [<filename>]\n"
Damien George5e349092014-03-08 19:04:47 +0000224"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300225"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300226);
227 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100228 printf(
229" emit={bytecode,native,viper} -- set the default code emitter\n"
230);
231 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300232#if MICROPY_ENABLE_GC
233 printf(
Damien George5e349092014-03-08 19:04:47 +0000234" heapsize=<n> -- set the heap size for the GC\n"
235);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300236 impl_opts_cnt++;
237#endif
238
239 if (impl_opts_cnt == 0) {
240 printf(" (none)\n");
241 }
242
Damien George136f6752014-01-07 14:54:15 +0000243 return 1;
244}
245
Damien George4d5b28c2014-01-29 18:56:46 +0000246mp_obj_t mem_info(void) {
247 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
248 return mp_const_none;
249}
250
251mp_obj_t qstr_info(void) {
252 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
253 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
254 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);
255 return mp_const_none;
256}
257
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200258#if MICROPY_ENABLE_GC
259// TODO: this doesn't belong here
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300260STATIC mp_obj_t pyb_gc(void) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200261 gc_collect();
262 return mp_const_none;
263}
264MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
265#endif
266
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200267// Process options which set interpreter init options
268void pre_process_options(int argc, char **argv) {
269 for (int a = 1; a < argc; a++) {
270 if (argv[a][0] == '-') {
271 if (strcmp(argv[a], "-X") == 0) {
272 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300273 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200274 }
Damien George5e349092014-03-08 19:04:47 +0000275 if (0) {
Damien George65cad122014-04-06 11:48:15 +0100276 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
277 emit_opt = MP_EMIT_OPT_BYTE_CODE;
278 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
279 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
280 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
281 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200282#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000283 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200284 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200285#endif
Damien George5e349092014-03-08 19:04:47 +0000286 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300287 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000288 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200289 a++;
290 }
291 }
292 }
293}
294
Damien5ac1b2e2013-10-18 19:58:12 +0100295int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200296 volatile int stack_dummy;
297 stack_top = (void*)&stack_dummy;
298
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200299 pre_process_options(argc, argv);
300
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200301#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200302 char *heap = malloc(heap_size);
303 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200304#endif
305
Damien5ac1b2e2013-10-18 19:58:12 +0100306 qstr_init();
Damien Georged17926d2014-03-30 13:35:08 +0100307 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100308
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200309 char *home = getenv("HOME");
310 char *path = getenv("MICROPYPATH");
311 if (path == NULL) {
312 path = "~/.micropython/lib:/usr/lib/micropython";
313 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200314 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200315 for (char *p = path; p != NULL; p = strchr(p, ':')) {
316 path_num++;
317 if (p != NULL) {
318 p++;
319 }
320 }
Damien Georged17926d2014-03-30 13:35:08 +0100321 mp_sys_path = mp_obj_new_list(path_num, NULL);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200322 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100323 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200324 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200325 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200326 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200327 char *p1 = strchr(p, ':');
328 if (p1 == NULL) {
329 p1 = p + strlen(p);
330 }
331 if (p[0] == '~' && p[1] == '/' && home != NULL) {
332 // Expand standalone ~ to $HOME
333 CHECKBUF(buf, PATH_MAX);
334 CHECKBUF_APPEND(buf, home, strlen(home));
335 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200336 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200337 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200338 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200339 }
340 p = p1 + 1;
341 }
342
Damien George55baff42014-01-21 21:40:13 +0000343 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Damien Georged17926d2014-03-30 13:35:08 +0100344 mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200345 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien Georged17926d2014-03-30 13:35:08 +0100346 mp_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200347
Damien Georged17926d2014-03-30 13:35:08 +0100348 mp_store_name(qstr_from_str("test"), test_obj_new(42));
349 mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
350 mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200351#if MICROPY_ENABLE_GC
Damien Georged17926d2014-03-30 13:35:08 +0100352 mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200353#endif
Damien George12eacca2014-01-21 21:54:15 +0000354
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200355 file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +0200356 microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +0200357#if MICROPY_MOD_TIME
358 time_init();
359#endif
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200360#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200361 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200362#endif
Damiena53f6942013-11-02 23:58:38 +0000363
Damien George062478e2014-01-09 20:57:50 +0000364 // Here is some example code to create a class and instance of that class.
365 // First is the Python, then the C code.
366 //
367 // class TestClass:
368 // pass
369 // test_obj = TestClass()
370 // test_obj.attr = 42
371 mp_obj_t test_class_type, test_class_instance;
Damien Georgea71c83a2014-02-15 11:34:50 +0000372 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 +0100373 mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
374 mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000375
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000376 /*
377 printf("bytes:\n");
378 printf(" total %d\n", m_get_total_bytes_allocated());
379 printf(" cur %d\n", m_get_current_bytes_allocated());
380 printf(" peak %d\n", m_get_peak_bytes_allocated());
381 */
382
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200383 bool executed = false;
384 for (int a = 1; a < argc; a++) {
385 if (argv[a][0] == '-') {
386 if (strcmp(argv[a], "-c") == 0) {
387 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300388 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000389 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200390 do_str(argv[a + 1]);
391 executed = true;
392 a += 1;
393 } else if (strcmp(argv[a], "-X") == 0) {
394 a += 1;
Damien George136f6752014-01-07 14:54:15 +0000395 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300396 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000397 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200398 } else {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200399 char *basedir = realpath(argv[a], NULL);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300400 if (basedir == NULL) {
401 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno);
402 perror("");
403 // CPython exits with 2 in such case
404 exit(2);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200405 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300406
407 // Set base dir of the script as first entry in sys.path
408 char *p = strrchr(basedir, '/');
409 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
410 free(basedir);
411
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200412 for (int i = a; i < argc; i++) {
Damien George15d18062014-03-31 16:28:13 +0100413 mp_obj_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200414 }
415 do_file(argv[a]);
416 executed = true;
417 break;
Damien George136f6752014-01-07 14:54:15 +0000418 }
Damien5ac1b2e2013-10-18 19:58:12 +0100419 }
Damien George136f6752014-01-07 14:54:15 +0000420
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200421 if (!executed) {
422 do_repl();
423 }
424
Damien Georged17926d2014-03-30 13:35:08 +0100425 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100426
427 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
428 return 0;
429}
Damien087d2182013-11-09 20:14:30 +0000430
Damien Georgee09ffa12014-02-05 23:57:48 +0000431uint mp_import_stat(const char *path) {
432 struct stat st;
433 if (stat(path, &st) == 0) {
434 if (S_ISDIR(st.st_mode)) {
435 return MP_IMPORT_STAT_DIR;
436 } else if (S_ISREG(st.st_mode)) {
437 return MP_IMPORT_STAT_FILE;
438 }
439 }
440 return MP_IMPORT_STAT_NO_EXIST;
441}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200442
443int DEBUG_printf(const char *fmt, ...) {
444 va_list ap;
445 va_start(ap, fmt);
446 int ret = vfprintf(stderr, fmt, ap);
447 va_end(ap);
448 return ret;
449}