blob: b5f7a82f189b0b314ddf07e30c84ff71561364c4 [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)
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020033long heap_size = 128*1024;
34#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020035
36// Stack top at the start of program
37void *stack_top;
38
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +020039void file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +020040void microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020041void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020042void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020043
Damien George136f6752014-01-07 14:54:15 +000044static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
45 if (lex == NULL) {
46 return;
47 }
48
49 if (0) {
50 // just tokenise
51 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
52 mp_token_show(mp_lexer_cur(lex));
53 mp_lexer_to_next(lex);
54 }
55 mp_lexer_free(lex);
56 return;
57 }
58
Damien Georgec5966122014-02-15 16:10:44 +000059 mp_parse_error_kind_t parse_error_kind;
60 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +000061
62 if (pn == MP_PARSE_NODE_NULL) {
63 // parse error
Damien Georgec5966122014-02-15 16:10:44 +000064 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +000065 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000066 return;
67 }
68
Damien George08335002014-01-18 23:24:36 +000069 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000070 mp_lexer_free(lex);
71
Damien Georgecbd2f742014-01-19 11:48:48 +000072 /*
73 printf("----------------\n");
74 mp_parse_node_print(pn, 0);
75 printf("----------------\n");
76 */
Damien George136f6752014-01-07 14:54:15 +000077
Damien George08335002014-01-18 23:24:36 +000078 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000079
80 if (module_fun == mp_const_none) {
81 // compile error
82 return;
83 }
84
85 // execute it
86 nlr_buf_t nlr;
87 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +010088 mp_call_function_0(module_fun);
Damien George136f6752014-01-07 14:54:15 +000089 nlr_pop();
90 } else {
91 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000092 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000093 }
94}
95
Paul Sokolovskya0757412014-02-11 12:19:06 +020096static char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010097 int l1 = strlen(s1);
98 int l2 = strlen(s2);
99 char *s = m_new(char, l1 + l2 + 2);
100 memcpy(s, s1, l1);
101 if (sep_char != 0) {
102 s[l1] = sep_char;
103 l1 += 1;
104 }
105 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100106 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100107 return s;
108}
109
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200110static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200111#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200112 char *line = readline(p);
113 if (line) {
114 add_history(line);
115 }
116#else
117 static char buf[256];
118 fputs(p, stdout);
119 char *s = fgets(buf, sizeof(buf), stdin);
120 if (!s) {
121 return NULL;
122 }
123 int l = strlen(buf);
124 if (buf[l - 1] == '\n') {
125 buf[l - 1] = 0;
126 } else {
127 l++;
128 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200129 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200130 memcpy(line, buf, l);
131#endif
132 return line;
133}
134
Damiend99b0522013-12-21 18:17:45 +0000135static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100136 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200137 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100138 if (line == NULL) {
139 // EOF
140 return;
141 }
Damiend99b0522013-12-21 18:17:45 +0000142 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100143 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200144 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100145 if (line2 == NULL || strlen(line2) == 0) {
146 break;
147 }
Paul Sokolovskya0757412014-02-11 12:19:06 +0200148 char *line3 = strjoin(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000149 free(line);
150 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100151 line = line3;
152 }
153 }
Damienfa2162b2013-10-20 17:42:00 +0100154
Damien Georgeb829b5c2014-01-25 13:51:19 +0000155 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 +0000156 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200157 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100158 }
159}
160
Damien George136f6752014-01-07 14:54:15 +0000161static void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000162 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000163 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
164}
Damien429d7192013-10-04 19:53:11 +0100165
Damien George136f6752014-01-07 14:54:15 +0000166static void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000167 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 +0000168 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100169}
Damien429d7192013-10-04 19:53:11 +0100170
Damiend99b0522013-12-21 18:17:45 +0000171typedef struct _test_obj_t {
172 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000173 int value;
Damiend99b0522013-12-21 18:17:45 +0000174} test_obj_t;
175
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200176static 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 +0000177 test_obj_t *self = self_in;
178 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000179}
180
Damiend99b0522013-12-21 18:17:45 +0000181static mp_obj_t test_get(mp_obj_t self_in) {
182 test_obj_t *self = self_in;
183 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000184}
185
Damiend99b0522013-12-21 18:17:45 +0000186static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
187 test_obj_t *self = self_in;
188 self->value = mp_obj_get_int(arg);
189 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000190}
191
Damiend99b0522013-12-21 18:17:45 +0000192static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
193static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
194
Damien George9b196cd2014-03-26 21:47:19 +0000195static const mp_map_elem_t test_locals_dict_table[] = {
196 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&test_get_obj },
197 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&test_set_obj },
Damien George97209d32014-01-07 15:58:30 +0000198};
199
Damien George9b196cd2014-03-26 21:47:19 +0000200STATIC MP_DEFINE_CONST_DICT(test_locals_dict, test_locals_dict_table);
201
Damiend99b0522013-12-21 18:17:45 +0000202static const mp_obj_type_t test_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000203 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000204 .name = MP_QSTR_Test,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200205 .print = test_print,
Damien George9b196cd2014-03-26 21:47:19 +0000206 .locals_dict = (mp_obj_t)&test_locals_dict,
Damiena53f6942013-11-02 23:58:38 +0000207};
208
Damiend99b0522013-12-21 18:17:45 +0000209mp_obj_t test_obj_new(int value) {
210 test_obj_t *o = m_new_obj(test_obj_t);
211 o->base.type = &test_type;
212 o->value = value;
213 return o;
214}
215
Damien George136f6752014-01-07 14:54:15 +0000216int usage(void) {
Damien George5e349092014-03-08 19:04:47 +0000217 printf(
218"usage: py [-X <opt>] [-c <command>] [<filename>]\n"
219"\n"
220"Implementation specific options:\n"
221" heapsize=<n> -- set the heap size for the GC\n"
222);
Damien George136f6752014-01-07 14:54:15 +0000223 return 1;
224}
225
Damien George4d5b28c2014-01-29 18:56:46 +0000226mp_obj_t mem_info(void) {
227 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
228 return mp_const_none;
229}
230
231mp_obj_t qstr_info(void) {
232 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
233 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
234 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);
235 return mp_const_none;
236}
237
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200238#if MICROPY_ENABLE_GC
239// TODO: this doesn't belong here
240static mp_obj_t pyb_gc(void) {
241 gc_collect();
242 return mp_const_none;
243}
244MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
245#endif
246
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200247// Process options which set interpreter init options
248void pre_process_options(int argc, char **argv) {
249 for (int a = 1; a < argc; a++) {
250 if (argv[a][0] == '-') {
251 if (strcmp(argv[a], "-X") == 0) {
252 if (a + 1 >= argc) {
253 exit(usage());
254 }
Damien George5e349092014-03-08 19:04:47 +0000255 if (0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200256#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000257 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200258 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200259#endif
Damien George5e349092014-03-08 19:04:47 +0000260 } else {
261 exit(usage());
262 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200263 a++;
264 }
265 }
266 }
267}
268
Damien5ac1b2e2013-10-18 19:58:12 +0100269int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200270 volatile int stack_dummy;
271 stack_top = (void*)&stack_dummy;
272
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200273 pre_process_options(argc, argv);
274
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200275#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200276 char *heap = malloc(heap_size);
277 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200278#endif
279
Damien5ac1b2e2013-10-18 19:58:12 +0100280 qstr_init();
Damien Georged17926d2014-03-30 13:35:08 +0100281 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100282
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200283 char *home = getenv("HOME");
284 char *path = getenv("MICROPYPATH");
285 if (path == NULL) {
286 path = "~/.micropython/lib:/usr/lib/micropython";
287 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200288 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200289 for (char *p = path; p != NULL; p = strchr(p, ':')) {
290 path_num++;
291 if (p != NULL) {
292 p++;
293 }
294 }
Damien Georged17926d2014-03-30 13:35:08 +0100295 mp_sys_path = mp_obj_new_list(path_num, NULL);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200296 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100297 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200298 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200299 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200300 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200301 char *p1 = strchr(p, ':');
302 if (p1 == NULL) {
303 p1 = p + strlen(p);
304 }
305 if (p[0] == '~' && p[1] == '/' && home != NULL) {
306 // Expand standalone ~ to $HOME
307 CHECKBUF(buf, PATH_MAX);
308 CHECKBUF_APPEND(buf, home, strlen(home));
309 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200310 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200311 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200312 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200313 }
314 p = p1 + 1;
315 }
316
Damien George55baff42014-01-21 21:40:13 +0000317 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Damien Georged17926d2014-03-30 13:35:08 +0100318 mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200319 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien Georged17926d2014-03-30 13:35:08 +0100320 mp_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200321
Damien Georged17926d2014-03-30 13:35:08 +0100322 mp_store_name(qstr_from_str("test"), test_obj_new(42));
323 mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
324 mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200325#if MICROPY_ENABLE_GC
Damien Georged17926d2014-03-30 13:35:08 +0100326 mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200327#endif
Damien George12eacca2014-01-21 21:54:15 +0000328
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200329 file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +0200330 microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +0200331#if MICROPY_MOD_TIME
332 time_init();
333#endif
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200334#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200335 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200336#endif
Damiena53f6942013-11-02 23:58:38 +0000337
Damien George062478e2014-01-09 20:57:50 +0000338 // Here is some example code to create a class and instance of that class.
339 // First is the Python, then the C code.
340 //
341 // class TestClass:
342 // pass
343 // test_obj = TestClass()
344 // test_obj.attr = 42
345 mp_obj_t test_class_type, test_class_instance;
Damien Georgea71c83a2014-02-15 11:34:50 +0000346 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 +0100347 mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
348 mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000349
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000350 /*
351 printf("bytes:\n");
352 printf(" total %d\n", m_get_total_bytes_allocated());
353 printf(" cur %d\n", m_get_current_bytes_allocated());
354 printf(" peak %d\n", m_get_peak_bytes_allocated());
355 */
356
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200357 bool executed = false;
358 for (int a = 1; a < argc; a++) {
359 if (argv[a][0] == '-') {
360 if (strcmp(argv[a], "-c") == 0) {
361 if (a + 1 >= argc) {
Damien George136f6752014-01-07 14:54:15 +0000362 return usage();
363 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200364 do_str(argv[a + 1]);
365 executed = true;
366 a += 1;
367 } else if (strcmp(argv[a], "-X") == 0) {
368 a += 1;
Damien George136f6752014-01-07 14:54:15 +0000369 } else {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200370 return usage();
Damien George136f6752014-01-07 14:54:15 +0000371 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200372 } else {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200373 char *basedir = realpath(argv[a], NULL);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300374 if (basedir == NULL) {
375 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno);
376 perror("");
377 // CPython exits with 2 in such case
378 exit(2);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200379 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300380
381 // Set base dir of the script as first entry in sys.path
382 char *p = strrchr(basedir, '/');
383 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
384 free(basedir);
385
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200386 for (int i = a; i < argc; i++) {
Damien George15d18062014-03-31 16:28:13 +0100387 mp_obj_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200388 }
389 do_file(argv[a]);
390 executed = true;
391 break;
Damien George136f6752014-01-07 14:54:15 +0000392 }
Damien5ac1b2e2013-10-18 19:58:12 +0100393 }
Damien George136f6752014-01-07 14:54:15 +0000394
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200395 if (!executed) {
396 do_repl();
397 }
398
Damien Georged17926d2014-03-30 13:35:08 +0100399 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100400
401 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
402 return 0;
403}
Damien087d2182013-11-09 20:14:30 +0000404
Damien Georgee09ffa12014-02-05 23:57:48 +0000405uint mp_import_stat(const char *path) {
406 struct stat st;
407 if (stat(path, &st) == 0) {
408 if (S_ISDIR(st.st_mode)) {
409 return MP_IMPORT_STAT_DIR;
410 } else if (S_ISREG(st.st_mode)) {
411 return MP_IMPORT_STAT_FILE;
412 }
413 }
414 return MP_IMPORT_STAT_NO_EXIST;
415}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200416
417int DEBUG_printf(const char *fmt, ...) {
418 va_list ap;
419 va_start(ap, fmt);
420 int ret = vfprintf(stderr, fmt, ap);
421 va_end(ap);
422 return ret;
423}