blob: 78454dd73c61a3d3d80d2dbaf9b16689aeb3394d [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien429d7192013-10-04 19:53:11 +010027#include <stdint.h>
xbec93a2212014-03-15 23:24:34 -070028#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010029#include <stdio.h>
30#include <string.h>
Edd Barrett8146aea2014-01-01 23:14:36 +000031#include <stdlib.h>
Paul Sokolovsky44739e22014-02-16 18:11:42 +020032#include <stdarg.h>
Paul Sokolovskyd3439d02014-06-02 19:37:55 +030033#include <ctype.h>
xbec93a2212014-03-15 23:24:34 -070034#include <sys/stat.h>
35#include <sys/types.h>
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +030036#include <errno.h>
Damien429d7192013-10-04 19:53:11 +010037
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030038#include "mpconfig.h"
Damience89a212013-10-15 22:25:17 +010039#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010040#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000041#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010042#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010043#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010044#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000045#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000046#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000047#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000048#include "runtime0.h"
49#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010050#include "repl.h"
Paul Sokolovskye7db8172014-02-11 00:44:37 +020051#include "gc.h"
Damien Georged553be52014-04-17 18:03:27 +010052#include "genhdr/py-version.h"
Dave Hylands117c46d2014-05-07 07:15:00 -070053#include "input.h"
Paul Sokolovsky23668692014-06-25 03:03:34 +030054#include "stackctrl.h"
Damien429d7192013-10-04 19:53:11 +010055
Damien Georgef22626e2014-04-10 11:30:35 +010056// Command line options, with their defaults
57bool compile_only = false;
Damien George65cad122014-04-06 11:48:15 +010058uint emit_opt = MP_EMIT_OPT_NONE;
Paul Sokolovsky6b344d72014-05-05 00:50:05 +030059uint mp_verbose_flag;
Damien George65cad122014-04-06 11:48:15 +010060
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020061#if MICROPY_ENABLE_GC
Paul Sokolovskye7db8172014-02-11 00:44:37 +020062// Heap size of GC heap (if enabled)
Damien Georgebd17e1b2014-04-04 14:29:00 +010063// Make it larger on a 64 bit machine, because pointers are larger.
Damien George40f3c022014-07-03 13:25:24 +010064long heap_size = 128*1024 * (sizeof(mp_uint_t) / 4);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020065#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020066
Paul Sokolovsky9945f332014-02-08 21:10:18 +020067void microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020068void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020069void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020070
stijn9e040b72014-05-10 19:42:06 +020071// returns standard error codes: 0 for success, 1 for all other errors
72STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
Damien George136f6752014-01-07 14:54:15 +000073 if (lex == NULL) {
stijn9e040b72014-05-10 19:42:06 +020074 return 1;
Damien George136f6752014-01-07 14:54:15 +000075 }
76
77 if (0) {
78 // just tokenise
79 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
80 mp_token_show(mp_lexer_cur(lex));
81 mp_lexer_to_next(lex);
82 }
83 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +020084 return 0;
Damien George136f6752014-01-07 14:54:15 +000085 }
86
Damien Georgec5966122014-02-15 16:10:44 +000087 mp_parse_error_kind_t parse_error_kind;
88 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +000089
90 if (pn == MP_PARSE_NODE_NULL) {
91 // parse error
Damien Georgec5966122014-02-15 16:10:44 +000092 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +000093 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +020094 return 1;
Damien George136f6752014-01-07 14:54:15 +000095 }
96
Damien George08335002014-01-18 23:24:36 +000097 qstr source_name = mp_lexer_source_name(lex);
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +030098 #if MICROPY_PY___FILE__
99 if (input_kind == MP_PARSE_FILE_INPUT) {
100 mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
101 }
102 #endif
Damien George9528cd62014-01-15 21:23:31 +0000103 mp_lexer_free(lex);
104
Damien Georgecbd2f742014-01-19 11:48:48 +0000105 /*
106 printf("----------------\n");
107 mp_parse_node_print(pn, 0);
108 printf("----------------\n");
109 */
Damien George136f6752014-01-07 14:54:15 +0000110
Damien George65cad122014-04-06 11:48:15 +0100111 mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
Damien George136f6752014-01-07 14:54:15 +0000112
113 if (module_fun == mp_const_none) {
114 // compile error
stijn9e040b72014-05-10 19:42:06 +0200115 return 1;
Damien George136f6752014-01-07 14:54:15 +0000116 }
117
Damien Georgef22626e2014-04-10 11:30:35 +0100118 if (compile_only) {
stijn9e040b72014-05-10 19:42:06 +0200119 return 0;
Damien Georgef22626e2014-04-10 11:30:35 +0100120 }
121
Damien George136f6752014-01-07 14:54:15 +0000122 // execute it
123 nlr_buf_t nlr;
124 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100125 mp_call_function_0(module_fun);
Damien George136f6752014-01-07 14:54:15 +0000126 nlr_pop();
stijn9e040b72014-05-10 19:42:06 +0200127 return 0;
Damien George136f6752014-01-07 14:54:15 +0000128 } else {
129 // uncaught exception
Damien George7a4ddd22014-05-24 23:32:19 +0100130 // check for SystemExit
131 mp_obj_t exc = (mp_obj_t)nlr.ret_val;
132 if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
Damien George8f81b5c2014-08-16 14:32:06 +0100133 mp_obj_t exit_val = mp_obj_exception_get_value(exc);
134 mp_int_t val;
135 if (!mp_obj_get_int_maybe(exit_val, &val)) {
136 val = 0;
137 }
138 exit(val);
Damien George7a4ddd22014-05-24 23:32:19 +0100139 }
Damien George136b1492014-01-19 12:38:49 +0000140 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
stijn9e040b72014-05-10 19:42:06 +0200141 return 1;
Damien George136f6752014-01-07 14:54:15 +0000142 }
143}
144
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300145STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100146 int l1 = strlen(s1);
147 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300148 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100149 memcpy(s, s1, l1);
150 if (sep_char != 0) {
151 s[l1] = sep_char;
152 l1 += 1;
153 }
154 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100155 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100156 return s;
157}
158
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300159STATIC void do_repl(void) {
stijnec6fa872014-06-28 21:04:20 +0200160 printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
Damien George6827f9f2014-04-07 13:27:50 +0100161
Damien5ac1b2e2013-10-18 19:58:12 +0100162 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200163 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100164 if (line == NULL) {
165 // EOF
166 return;
167 }
Damien George97790452014-04-08 11:04:29 +0000168 while (mp_repl_continue_with_input(line)) {
169 char *line2 = prompt("... ");
170 if (line2 == NULL) {
171 break;
Damien5ac1b2e2013-10-18 19:58:12 +0100172 }
Damien George97790452014-04-08 11:04:29 +0000173 char *line3 = strjoin(line, '\n', line2);
174 free(line);
175 free(line2);
176 line = line3;
Damien5ac1b2e2013-10-18 19:58:12 +0100177 }
Damienfa2162b2013-10-20 17:42:00 +0100178
Damien Georgeb829b5c2014-01-25 13:51:19 +0000179 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 +0000180 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200181 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100182 }
183}
184
stijn9e040b72014-05-10 19:42:06 +0200185STATIC int do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000186 mp_lexer_t *lex = mp_lexer_new_from_file(file);
stijn9e040b72014-05-10 19:42:06 +0200187 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien George136f6752014-01-07 14:54:15 +0000188}
Damien429d7192013-10-04 19:53:11 +0100189
stijn9e040b72014-05-10 19:42:06 +0200190STATIC int do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000191 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
stijn9e040b72014-05-10 19:42:06 +0200192 return execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100193}
Damien429d7192013-10-04 19:53:11 +0100194
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300195int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000196 printf(
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300197"usage: %s [<opts>] [-X <implopt>] [-c <command>] [<filename>]\n"
198"Options:\n"
199"-v : verbose (trace various operations); can be multiple\n"
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300200"-O[N] : apply bytecode optimizations of level N\n"
Damien George5e349092014-03-08 19:04:47 +0000201"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300202"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300203);
204 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100205 printf(
Damien Georgef22626e2014-04-10 11:30:35 +0100206" compile-only -- parse and compile only\n"
Damien George65cad122014-04-06 11:48:15 +0100207" emit={bytecode,native,viper} -- set the default code emitter\n"
208);
209 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300210#if MICROPY_ENABLE_GC
211 printf(
Paul Sokolovsky141df2d2014-06-24 16:58:00 +0300212" heapsize=<n> -- set the heap size for the GC (default %ld)\n"
213, heap_size);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300214 impl_opts_cnt++;
215#endif
216
217 if (impl_opts_cnt == 0) {
218 printf(" (none)\n");
219 }
220
Damien George136f6752014-01-07 14:54:15 +0000221 return 1;
222}
223
Damien George3c658a42014-08-24 16:28:17 +0100224STATIC mp_obj_t mem_info(void) {
Paul Sokolovsky914bcf12014-05-31 02:34:39 +0300225 printf("mem: total=%d, current=%d, peak=%d\n",
226 m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
Paul Sokolovskycaa73342014-07-01 02:13:42 +0300227 printf("stack: %u\n", mp_stack_usage());
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300228#if MICROPY_ENABLE_GC
Paul Sokolovsky59a2f482014-04-17 18:32:36 +0300229 gc_dump_info();
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300230#endif
Damien George4d5b28c2014-01-29 18:56:46 +0000231 return mp_const_none;
232}
Damien George3c658a42014-08-24 16:28:17 +0100233STATIC MP_DEFINE_CONST_FUN_OBJ_0(mem_info_obj, mem_info);
Damien George4d5b28c2014-01-29 18:56:46 +0000234
Damien George3c658a42014-08-24 16:28:17 +0100235STATIC mp_obj_t qstr_info(void) {
Damien George4d5b28c2014-01-29 18:56:46 +0000236 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
237 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
238 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);
239 return mp_const_none;
240}
Damien George3c658a42014-08-24 16:28:17 +0100241STATIC MP_DEFINE_CONST_FUN_OBJ_0(qstr_info_obj, qstr_info);
Damien George4d5b28c2014-01-29 18:56:46 +0000242
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200243// Process options which set interpreter init options
244void pre_process_options(int argc, char **argv) {
245 for (int a = 1; a < argc; a++) {
246 if (argv[a][0] == '-') {
247 if (strcmp(argv[a], "-X") == 0) {
248 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300249 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200250 }
Damien George5e349092014-03-08 19:04:47 +0000251 if (0) {
Damien Georgef22626e2014-04-10 11:30:35 +0100252 } else if (strcmp(argv[a + 1], "compile-only") == 0) {
253 compile_only = true;
Damien George65cad122014-04-06 11:48:15 +0100254 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
Damien George96f137b2014-05-12 22:35:37 +0100255 emit_opt = MP_EMIT_OPT_BYTECODE;
Damien George65cad122014-04-06 11:48:15 +0100256 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
257 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
258 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
259 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200260#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000261 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200262 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200263#endif
Damien George5e349092014-03-08 19:04:47 +0000264 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300265 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000266 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200267 a++;
268 }
269 }
270 }
271}
272
stijndf3ab072014-06-05 12:33:55 +0200273#ifdef _WIN32
274#define PATHLIST_SEP_CHAR ';'
275#else
276#define PATHLIST_SEP_CHAR ':'
277#endif
278
Damien5ac1b2e2013-10-18 19:58:12 +0100279int main(int argc, char **argv) {
Paul Sokolovskycaa73342014-07-01 02:13:42 +0300280 mp_stack_set_limit(32768);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200281
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200282 pre_process_options(argc, argv);
283
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200284#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200285 char *heap = malloc(heap_size);
286 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200287#endif
288
Damien Georged17926d2014-03-30 13:35:08 +0100289 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100290
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200291 char *home = getenv("HOME");
292 char *path = getenv("MICROPYPATH");
293 if (path == NULL) {
294 path = "~/.micropython/lib:/usr/lib/micropython";
295 }
Damien George9c4cbe22014-08-30 14:04:14 +0100296 mp_uint_t path_num = 1; // [0] is for current dir (or base dir of the script)
stijndf3ab072014-06-05 12:33:55 +0200297 for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200298 path_num++;
299 if (p != NULL) {
300 p++;
301 }
302 }
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300303 mp_obj_list_init(mp_sys_path, path_num);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200304 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100305 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200306 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200307 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200308 for (int i = 1; i < path_num; i++) {
stijndf3ab072014-06-05 12:33:55 +0200309 char *p1 = strchr(p, PATHLIST_SEP_CHAR);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200310 if (p1 == NULL) {
311 p1 = p + strlen(p);
312 }
313 if (p[0] == '~' && p[1] == '/' && home != NULL) {
314 // Expand standalone ~ to $HOME
315 CHECKBUF(buf, PATH_MAX);
316 CHECKBUF_APPEND(buf, home, strlen(home));
317 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200318 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200319 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200320 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200321 }
322 p = p1 + 1;
323 }
324
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300325 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200326
Damien George3c658a42014-08-24 16:28:17 +0100327 mp_store_name(qstr_from_str("mem_info"), (mp_obj_t*)&mem_info_obj);
328 mp_store_name(qstr_from_str("qstr_info"), (mp_obj_t*)&qstr_info_obj);
Damien George12eacca2014-01-21 21:54:15 +0000329
Damien George062478e2014-01-09 20:57:50 +0000330 // Here is some example code to create a class and instance of that class.
331 // First is the Python, then the C code.
332 //
333 // class TestClass:
334 // pass
335 // test_obj = TestClass()
336 // test_obj.attr = 42
Damien Georgec327c0d2014-05-04 12:24:26 +0100337 //
338 // mp_obj_t test_class_type, test_class_instance;
339 // test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
340 // mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
341 // mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000342
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000343 /*
344 printf("bytes:\n");
345 printf(" total %d\n", m_get_total_bytes_allocated());
346 printf(" cur %d\n", m_get_current_bytes_allocated());
347 printf(" peak %d\n", m_get_peak_bytes_allocated());
348 */
349
stijn9e040b72014-05-10 19:42:06 +0200350 const int NOTHING_EXECUTED = -2;
351 int ret = NOTHING_EXECUTED;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200352 for (int a = 1; a < argc; a++) {
353 if (argv[a][0] == '-') {
354 if (strcmp(argv[a], "-c") == 0) {
355 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300356 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000357 }
stijn9e040b72014-05-10 19:42:06 +0200358 ret = do_str(argv[a + 1]);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200359 a += 1;
360 } else if (strcmp(argv[a], "-X") == 0) {
361 a += 1;
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300362 } else if (strcmp(argv[a], "-v") == 0) {
363 mp_verbose_flag++;
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300364 } else if (strncmp(argv[a], "-O", 2) == 0) {
365 if (isdigit(argv[a][2])) {
366 mp_optimise_value = argv[a][2] & 0xf;
367 } else {
368 mp_optimise_value = 0;
369 for (char *p = argv[a] + 1; *p && *p == 'O'; p++, mp_optimise_value++);
370 }
Damien George136f6752014-01-07 14:54:15 +0000371 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300372 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000373 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200374 } else {
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300375 char *pathbuf = malloc(PATH_MAX);
376 char *basedir = realpath(argv[a], pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300377 if (basedir == NULL) {
Paul Sokolovskydd0dee32014-06-03 01:01:39 +0300378 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[a], errno);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300379 perror("");
380 // CPython exits with 2 in such case
stijn9e040b72014-05-10 19:42:06 +0200381 ret = 2;
382 break;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200383 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300384
385 // Set base dir of the script as first entry in sys.path
386 char *p = strrchr(basedir, '/');
387 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300388 free(pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300389
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200390 for (int i = a; i < argc; i++) {
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300391 mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200392 }
stijn9e040b72014-05-10 19:42:06 +0200393 ret = do_file(argv[a]);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200394 break;
Damien George136f6752014-01-07 14:54:15 +0000395 }
Damien5ac1b2e2013-10-18 19:58:12 +0100396 }
Damien George136f6752014-01-07 14:54:15 +0000397
stijn9e040b72014-05-10 19:42:06 +0200398 if (ret == NOTHING_EXECUTED) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200399 do_repl();
stijn9e040b72014-05-10 19:42:06 +0200400 ret = 0;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200401 }
402
Damien Georged17926d2014-03-30 13:35:08 +0100403 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100404
405 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
stijn9e040b72014-05-10 19:42:06 +0200406 return ret;
Damien429d7192013-10-04 19:53:11 +0100407}
Damien087d2182013-11-09 20:14:30 +0000408
Paul Sokolovskyd99e9082014-05-10 16:50:45 +0300409STATIC mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
410 int rc = 0;
411 if (n_args > 0) {
412 rc = mp_obj_get_int(args[0]);
413 }
Damien George7a4ddd22014-05-24 23:32:19 +0100414 nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc)));
Paul Sokolovskyd99e9082014-05-10 16:50:45 +0300415}
416MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
417
Damien Georgee09ffa12014-02-05 23:57:48 +0000418uint mp_import_stat(const char *path) {
419 struct stat st;
420 if (stat(path, &st) == 0) {
421 if (S_ISDIR(st.st_mode)) {
422 return MP_IMPORT_STAT_DIR;
423 } else if (S_ISREG(st.st_mode)) {
424 return MP_IMPORT_STAT_FILE;
425 }
426 }
427 return MP_IMPORT_STAT_NO_EXIST;
428}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200429
430int DEBUG_printf(const char *fmt, ...) {
431 va_list ap;
432 va_start(ap, fmt);
433 int ret = vfprintf(stderr, fmt, ap);
434 va_end(ap);
435 return ret;
436}
Damien George26cf55a2014-04-08 14:08:14 +0000437
438void nlr_jump_fail(void *val) {
439 printf("FATAL: uncaught NLR %p\n", val);
440 exit(1);
441}