blob: 3e8de7902ba9fb5eac0c9c07db5d972d46dd200f [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"
Paul Sokolovskye5035122014-10-25 21:16:24 +030050#include "builtin.h"
Damien92c06562013-10-22 22:32:27 +010051#include "repl.h"
Paul Sokolovskye7db8172014-02-11 00:44:37 +020052#include "gc.h"
Damien Georged553be52014-04-17 18:03:27 +010053#include "genhdr/py-version.h"
Dave Hylands117c46d2014-05-07 07:15:00 -070054#include "input.h"
Paul Sokolovsky23668692014-06-25 03:03:34 +030055#include "stackctrl.h"
Damien429d7192013-10-04 19:53:11 +010056
Damien Georgef22626e2014-04-10 11:30:35 +010057// Command line options, with their defaults
Damien Georgeb0261342014-09-23 18:10:17 +010058STATIC bool compile_only = false;
59STATIC uint emit_opt = MP_EMIT_OPT_NONE;
60mp_uint_t mp_verbose_flag = 0;
Damien George65cad122014-04-06 11:48:15 +010061
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020062#if MICROPY_ENABLE_GC
Paul Sokolovskye7db8172014-02-11 00:44:37 +020063// Heap size of GC heap (if enabled)
Damien Georgebd17e1b2014-04-04 14:29:00 +010064// Make it larger on a 64 bit machine, because pointers are larger.
Damien George40f3c022014-07-03 13:25:24 +010065long heap_size = 128*1024 * (sizeof(mp_uint_t) / 4);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020066#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020067
Damien Georgec76af322014-10-26 00:42:41 +010068#ifndef _WIN32
69#include <signal.h>
70
Damien George124df6f2014-10-25 18:19:55 +010071STATIC mp_obj_t keyboard_interrupt_obj;
72
73STATIC void sighandler(int signum) {
74 if (signum == SIGINT) {
75 mp_obj_exception_clear_traceback(keyboard_interrupt_obj);
76 mp_pending_exception = keyboard_interrupt_obj;
77 // disable our handler so next we really die
78 struct sigaction sa;
79 sa.sa_handler = SIG_DFL;
80 sigemptyset(&sa.sa_mask);
81 sigaction(SIGINT, &sa, NULL);
82 }
83}
Damien Georgec76af322014-10-26 00:42:41 +010084#endif
Damien George124df6f2014-10-25 18:19:55 +010085
Damien George762d5752014-10-01 23:18:39 +010086#define FORCED_EXIT (0x100)
Paul Sokolovskye5035122014-10-25 21:16:24 +030087// If exc is SystemExit, return value where FORCED_EXIT bit set,
88// and lower 8 bits are SystemExit value. For all other exceptions,
89// return 1.
90STATIC int handle_uncaught_exception(mp_obj_t exc) {
91 // check for SystemExit
92 if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
93 // None is an exit value of 0; an int is its value; anything else is 1
94 mp_obj_t exit_val = mp_obj_exception_get_value(exc);
95 mp_int_t val = 0;
96 if (exit_val != mp_const_none && !mp_obj_get_int_maybe(exit_val, &val)) {
97 val = 1;
98 }
99 return FORCED_EXIT | (val & 255);
100 }
101
102 // Report all other exceptions
103 mp_obj_print_exception(exc);
104 return 1;
105}
106
107// Returns standard error codes: 0 for success, 1 for all other errors,
108// except if FORCED_EXIT bit is set then script raised SystemExit and the
Damien George762d5752014-10-01 23:18:39 +0100109// value of the exit is in the lower 8 bits of the return value
stijn9e040b72014-05-10 19:42:06 +0200110STATIC 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 +0000111 if (lex == NULL) {
stijn9e040b72014-05-10 19:42:06 +0200112 return 1;
Damien George136f6752014-01-07 14:54:15 +0000113 }
114
115 if (0) {
116 // just tokenise
117 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
118 mp_token_show(mp_lexer_cur(lex));
119 mp_lexer_to_next(lex);
120 }
121 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +0200122 return 0;
Damien George136f6752014-01-07 14:54:15 +0000123 }
124
Damien Georgec5966122014-02-15 16:10:44 +0000125 mp_parse_error_kind_t parse_error_kind;
126 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +0000127
128 if (pn == MP_PARSE_NODE_NULL) {
129 // parse error
Damien Georgec5966122014-02-15 16:10:44 +0000130 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +0000131 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +0200132 return 1;
Damien George136f6752014-01-07 14:54:15 +0000133 }
134
Damien George08335002014-01-18 23:24:36 +0000135 qstr source_name = mp_lexer_source_name(lex);
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300136 #if MICROPY_PY___FILE__
137 if (input_kind == MP_PARSE_FILE_INPUT) {
138 mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
139 }
140 #endif
Damien George9528cd62014-01-15 21:23:31 +0000141 mp_lexer_free(lex);
142
Damien Georgecbd2f742014-01-19 11:48:48 +0000143 /*
144 printf("----------------\n");
145 mp_parse_node_print(pn, 0);
146 printf("----------------\n");
147 */
Damien George136f6752014-01-07 14:54:15 +0000148
Damien George65cad122014-04-06 11:48:15 +0100149 mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
Damien George136f6752014-01-07 14:54:15 +0000150
Damien George854c8c02014-10-05 22:25:36 +0100151 if (mp_obj_is_exception_instance(module_fun)) {
Damien George136f6752014-01-07 14:54:15 +0000152 // compile error
Damien George854c8c02014-10-05 22:25:36 +0100153 mp_obj_print_exception(module_fun);
stijn9e040b72014-05-10 19:42:06 +0200154 return 1;
Damien George136f6752014-01-07 14:54:15 +0000155 }
156
Damien Georgef22626e2014-04-10 11:30:35 +0100157 if (compile_only) {
stijn9e040b72014-05-10 19:42:06 +0200158 return 0;
Damien Georgef22626e2014-04-10 11:30:35 +0100159 }
160
Damien Georgec76af322014-10-26 00:42:41 +0100161 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100162 // enable signal handler
163 struct sigaction sa;
164 sa.sa_handler = sighandler;
165 sigemptyset(&sa.sa_mask);
166 sigaction(SIGINT, &sa, NULL);
167 sa.sa_handler = SIG_DFL;
Damien Georgec76af322014-10-26 00:42:41 +0100168 #endif
Damien George124df6f2014-10-25 18:19:55 +0100169
Damien George136f6752014-01-07 14:54:15 +0000170 // execute it
171 nlr_buf_t nlr;
172 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100173 mp_call_function_0(module_fun);
Damien Georgec76af322014-10-26 00:42:41 +0100174 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100175 sigaction(SIGINT, &sa, NULL);
Damien Georgec76af322014-10-26 00:42:41 +0100176 #endif
Damien George136f6752014-01-07 14:54:15 +0000177 nlr_pop();
stijn9e040b72014-05-10 19:42:06 +0200178 return 0;
Damien George136f6752014-01-07 14:54:15 +0000179 } else {
180 // uncaught exception
Damien Georgec76af322014-10-26 00:42:41 +0100181 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100182 sigaction(SIGINT, &sa, NULL);
Damien Georgec76af322014-10-26 00:42:41 +0100183 #endif
Paul Sokolovskye5035122014-10-25 21:16:24 +0300184 return handle_uncaught_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +0000185 }
186}
187
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300188STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100189 int l1 = strlen(s1);
190 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300191 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100192 memcpy(s, s1, l1);
193 if (sep_char != 0) {
194 s[l1] = sep_char;
195 l1 += 1;
196 }
197 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100198 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100199 return s;
200}
201
Damien George762d5752014-10-01 23:18:39 +0100202STATIC int do_repl(void) {
stijnec6fa872014-06-28 21:04:20 +0200203 printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
Damien George6827f9f2014-04-07 13:27:50 +0100204
Damien5ac1b2e2013-10-18 19:58:12 +0100205 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200206 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100207 if (line == NULL) {
208 // EOF
Damien George762d5752014-10-01 23:18:39 +0100209 return 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100210 }
Damien George97790452014-04-08 11:04:29 +0000211 while (mp_repl_continue_with_input(line)) {
212 char *line2 = prompt("... ");
213 if (line2 == NULL) {
214 break;
Damien5ac1b2e2013-10-18 19:58:12 +0100215 }
Damien George97790452014-04-08 11:04:29 +0000216 char *line3 = strjoin(line, '\n', line2);
217 free(line);
218 free(line2);
219 line = line3;
Damien5ac1b2e2013-10-18 19:58:12 +0100220 }
Damienfa2162b2013-10-20 17:42:00 +0100221
Damien Georgeb829b5c2014-01-25 13:51:19 +0000222 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
Damien George762d5752014-10-01 23:18:39 +0100223 int ret = execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
224 if (ret & FORCED_EXIT) {
225 return ret;
226 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200227 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100228 }
229}
230
stijn9e040b72014-05-10 19:42:06 +0200231STATIC int do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000232 mp_lexer_t *lex = mp_lexer_new_from_file(file);
stijn9e040b72014-05-10 19:42:06 +0200233 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien George136f6752014-01-07 14:54:15 +0000234}
Damien429d7192013-10-04 19:53:11 +0100235
stijn9e040b72014-05-10 19:42:06 +0200236STATIC int do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000237 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
Damien Georgec92672d2014-10-17 23:51:39 +0100238 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100239}
Damien429d7192013-10-04 19:53:11 +0100240
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300241int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000242 printf(
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300243"usage: %s [<opts>] [-X <implopt>] [-c <command>] [<filename>]\n"
244"Options:\n"
245"-v : verbose (trace various operations); can be multiple\n"
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300246"-O[N] : apply bytecode optimizations of level N\n"
Damien George5e349092014-03-08 19:04:47 +0000247"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300248"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300249);
250 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100251 printf(
Damien Georgef22626e2014-04-10 11:30:35 +0100252" compile-only -- parse and compile only\n"
Damien George65cad122014-04-06 11:48:15 +0100253" emit={bytecode,native,viper} -- set the default code emitter\n"
254);
255 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300256#if MICROPY_ENABLE_GC
257 printf(
Paul Sokolovsky141df2d2014-06-24 16:58:00 +0300258" heapsize=<n> -- set the heap size for the GC (default %ld)\n"
259, heap_size);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300260 impl_opts_cnt++;
261#endif
262
263 if (impl_opts_cnt == 0) {
264 printf(" (none)\n");
265 }
266
Damien George136f6752014-01-07 14:54:15 +0000267 return 1;
268}
269
Damien Georgeb0261342014-09-23 18:10:17 +0100270#if MICROPY_MEM_STATS
Damien George0b13f3e2014-10-24 23:12:25 +0100271STATIC mp_obj_t mem_info(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeb0261342014-09-23 18:10:17 +0100272 printf("mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
Paul Sokolovsky914bcf12014-05-31 02:34:39 +0300273 m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
Damien George4abff752014-08-30 14:59:21 +0100274 printf("stack: " UINT_FMT "\n", mp_stack_usage());
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300275#if MICROPY_ENABLE_GC
Paul Sokolovsky59a2f482014-04-17 18:32:36 +0300276 gc_dump_info();
Damien George0b13f3e2014-10-24 23:12:25 +0100277 if (n_args == 1) {
278 // arg given means dump gc allocation table
279 gc_dump_alloc_table();
280 }
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300281#endif
Damien George4d5b28c2014-01-29 18:56:46 +0000282 return mp_const_none;
283}
Damien George0b13f3e2014-10-24 23:12:25 +0100284STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mem_info_obj, 0, 1, mem_info);
Damien Georgeb0261342014-09-23 18:10:17 +0100285#endif
Damien George4d5b28c2014-01-29 18:56:46 +0000286
Damien George3c658a42014-08-24 16:28:17 +0100287STATIC mp_obj_t qstr_info(void) {
Damien George39dc1452014-10-03 19:52:22 +0100288 mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
Damien George4d5b28c2014-01-29 18:56:46 +0000289 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
Damien George39dc1452014-10-03 19:52:22 +0100290 printf("qstr pool: n_pool=" UINT_FMT ", n_qstr=" UINT_FMT ", n_str_data_bytes=" UINT_FMT ", n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
Damien George4d5b28c2014-01-29 18:56:46 +0000291 return mp_const_none;
292}
Damien George3c658a42014-08-24 16:28:17 +0100293STATIC MP_DEFINE_CONST_FUN_OBJ_0(qstr_info_obj, qstr_info);
Damien George4d5b28c2014-01-29 18:56:46 +0000294
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200295// Process options which set interpreter init options
296void pre_process_options(int argc, char **argv) {
297 for (int a = 1; a < argc; a++) {
298 if (argv[a][0] == '-') {
299 if (strcmp(argv[a], "-X") == 0) {
300 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300301 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200302 }
Damien George5e349092014-03-08 19:04:47 +0000303 if (0) {
Damien Georgef22626e2014-04-10 11:30:35 +0100304 } else if (strcmp(argv[a + 1], "compile-only") == 0) {
305 compile_only = true;
Damien George65cad122014-04-06 11:48:15 +0100306 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
Damien George96f137b2014-05-12 22:35:37 +0100307 emit_opt = MP_EMIT_OPT_BYTECODE;
Damien George65cad122014-04-06 11:48:15 +0100308 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
309 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
310 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
311 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200312#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000313 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskyfcff4662014-10-25 04:43:08 +0300314 char *end;
315 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, &end, 0);
316 // Don't bring unneeded libc dependencies like tolower()
Paul Sokolovsky98d8d592014-11-05 02:08:38 +0200317 // If there's 'w' immediately after number, adjust it for
318 // target word size. Note that it should be *before* size
319 // suffix like K or M, to avoid confusion with kilowords,
320 // etc. the size is still in bytes, just can be adjusted
321 // for word size (taking 32bit as baseline).
322 bool word_adjust = false;
323 if ((*end | 0x20) == 'w') {
324 word_adjust = true;
325 end++;
326 }
Paul Sokolovskyfcff4662014-10-25 04:43:08 +0300327 if ((*end | 0x20) == 'k') {
328 heap_size *= 1024;
329 } else if ((*end | 0x20) == 'm') {
330 heap_size *= 1024 * 1024;
331 }
Paul Sokolovsky98d8d592014-11-05 02:08:38 +0200332 if (word_adjust) {
333 heap_size = heap_size * BYTES_PER_WORD / 4;
334 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200335#endif
Damien George5e349092014-03-08 19:04:47 +0000336 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300337 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000338 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200339 a++;
340 }
341 }
342 }
343}
344
Paul Sokolovskye5035122014-10-25 21:16:24 +0300345void set_sys_argv(char *argv[], int argc, int start_arg) {
346 for (int i = start_arg; i < argc; i++) {
347 mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
348 }
349}
350
stijndf3ab072014-06-05 12:33:55 +0200351#ifdef _WIN32
352#define PATHLIST_SEP_CHAR ';'
353#else
354#define PATHLIST_SEP_CHAR ':'
355#endif
356
Damien5ac1b2e2013-10-18 19:58:12 +0100357int main(int argc, char **argv) {
Paul Sokolovskycaa73342014-07-01 02:13:42 +0300358 mp_stack_set_limit(32768);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200359
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200360 pre_process_options(argc, argv);
361
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200362#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200363 char *heap = malloc(heap_size);
364 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200365#endif
366
Damien Georged17926d2014-03-30 13:35:08 +0100367 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100368
Damien Georgec76af322014-10-26 00:42:41 +0100369 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100370 // create keyboard interrupt object
371 keyboard_interrupt_obj = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
Damien Georgec76af322014-10-26 00:42:41 +0100372 #endif
Damien George124df6f2014-10-25 18:19:55 +0100373
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200374 char *home = getenv("HOME");
375 char *path = getenv("MICROPYPATH");
376 if (path == NULL) {
377 path = "~/.micropython/lib:/usr/lib/micropython";
378 }
Damien George9c4cbe22014-08-30 14:04:14 +0100379 mp_uint_t path_num = 1; // [0] is for current dir (or base dir of the script)
stijndf3ab072014-06-05 12:33:55 +0200380 for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200381 path_num++;
382 if (p != NULL) {
383 p++;
384 }
385 }
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300386 mp_obj_list_init(mp_sys_path, path_num);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200387 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100388 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200389 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200390 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200391 for (int i = 1; i < path_num; i++) {
stijndf3ab072014-06-05 12:33:55 +0200392 char *p1 = strchr(p, PATHLIST_SEP_CHAR);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200393 if (p1 == NULL) {
394 p1 = p + strlen(p);
395 }
396 if (p[0] == '~' && p[1] == '/' && home != NULL) {
397 // Expand standalone ~ to $HOME
398 CHECKBUF(buf, PATH_MAX);
399 CHECKBUF_APPEND(buf, home, strlen(home));
400 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200401 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200402 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200403 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200404 }
405 p = p1 + 1;
406 }
407
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300408 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200409
Damien Georgeb0261342014-09-23 18:10:17 +0100410 #if MICROPY_MEM_STATS
Damien George3c658a42014-08-24 16:28:17 +0100411 mp_store_name(qstr_from_str("mem_info"), (mp_obj_t*)&mem_info_obj);
Damien Georgeb0261342014-09-23 18:10:17 +0100412 #endif
Damien George3c658a42014-08-24 16:28:17 +0100413 mp_store_name(qstr_from_str("qstr_info"), (mp_obj_t*)&qstr_info_obj);
Damien George12eacca2014-01-21 21:54:15 +0000414
Damien George062478e2014-01-09 20:57:50 +0000415 // Here is some example code to create a class and instance of that class.
416 // First is the Python, then the C code.
417 //
418 // class TestClass:
419 // pass
420 // test_obj = TestClass()
421 // test_obj.attr = 42
Damien Georgec327c0d2014-05-04 12:24:26 +0100422 //
423 // mp_obj_t test_class_type, test_class_instance;
424 // test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
425 // mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
426 // mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000427
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000428 /*
429 printf("bytes:\n");
430 printf(" total %d\n", m_get_total_bytes_allocated());
431 printf(" cur %d\n", m_get_current_bytes_allocated());
432 printf(" peak %d\n", m_get_peak_bytes_allocated());
433 */
434
stijn9e040b72014-05-10 19:42:06 +0200435 const int NOTHING_EXECUTED = -2;
436 int ret = NOTHING_EXECUTED;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200437 for (int a = 1; a < argc; a++) {
438 if (argv[a][0] == '-') {
439 if (strcmp(argv[a], "-c") == 0) {
440 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300441 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000442 }
stijn9e040b72014-05-10 19:42:06 +0200443 ret = do_str(argv[a + 1]);
Damien George762d5752014-10-01 23:18:39 +0100444 if (ret & FORCED_EXIT) {
445 break;
446 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200447 a += 1;
Paul Sokolovskye5035122014-10-25 21:16:24 +0300448 } else if (strcmp(argv[a], "-m") == 0) {
449 if (a + 1 >= argc) {
450 return usage(argv);
451 }
452 mp_obj_t import_args[4];
453 import_args[0] = mp_obj_new_str(argv[a + 1], strlen(argv[a + 1]), false);
454 import_args[1] = import_args[2] = mp_const_none;
455 // Ask __import__ to handle imported module specially - set its __name__
456 // to __main__, and also return this leaf module, not top-level package
457 // containing it.
458 import_args[3] = mp_const_false;
459 // TODO: https://docs.python.org/3/using/cmdline.html#cmdoption-m :
460 // "the first element of sys.argv will be the full path to
461 // the module file (while the module file is being located,
462 // the first element will be set to "-m")."
463 set_sys_argv(argv, argc, a + 1);
464
465 mp_obj_t mod;
466 nlr_buf_t nlr;
467 if (nlr_push(&nlr) == 0) {
468 mod = mp_builtin___import__(MP_ARRAY_SIZE(import_args), import_args);
469 nlr_pop();
470 } else {
471 // uncaught exception
472 return handle_uncaught_exception((mp_obj_t)nlr.ret_val);
473 }
474
475 if (mp_obj_is_package(mod)) {
476 // TODO
477 fprintf(stderr, "%s: -m for packages not yet implemented\n", argv[0]);
478 exit(1);
479 }
480 ret = 0;
481 break;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200482 } else if (strcmp(argv[a], "-X") == 0) {
483 a += 1;
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300484 } else if (strcmp(argv[a], "-v") == 0) {
485 mp_verbose_flag++;
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300486 } else if (strncmp(argv[a], "-O", 2) == 0) {
487 if (isdigit(argv[a][2])) {
488 mp_optimise_value = argv[a][2] & 0xf;
489 } else {
490 mp_optimise_value = 0;
491 for (char *p = argv[a] + 1; *p && *p == 'O'; p++, mp_optimise_value++);
492 }
Damien George136f6752014-01-07 14:54:15 +0000493 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300494 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000495 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200496 } else {
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300497 char *pathbuf = malloc(PATH_MAX);
498 char *basedir = realpath(argv[a], pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300499 if (basedir == NULL) {
Paul Sokolovskydd0dee32014-06-03 01:01:39 +0300500 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[a], errno);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300501 perror("");
502 // CPython exits with 2 in such case
stijn9e040b72014-05-10 19:42:06 +0200503 ret = 2;
504 break;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200505 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300506
507 // Set base dir of the script as first entry in sys.path
508 char *p = strrchr(basedir, '/');
509 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300510 free(pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300511
Paul Sokolovskye5035122014-10-25 21:16:24 +0300512 set_sys_argv(argv, argc, a);
stijn9e040b72014-05-10 19:42:06 +0200513 ret = do_file(argv[a]);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200514 break;
Damien George136f6752014-01-07 14:54:15 +0000515 }
Damien5ac1b2e2013-10-18 19:58:12 +0100516 }
Damien George136f6752014-01-07 14:54:15 +0000517
stijn9e040b72014-05-10 19:42:06 +0200518 if (ret == NOTHING_EXECUTED) {
Damien George762d5752014-10-01 23:18:39 +0100519 ret = do_repl();
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200520 }
521
Paul Sokolovsky429e3f02014-10-26 23:20:22 +0200522 if (mp_verbose_flag) {
523 mem_info(0, NULL);
524 }
525
Damien Georged17926d2014-03-30 13:35:08 +0100526 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100527
Damien George762d5752014-10-01 23:18:39 +0100528#if MICROPY_ENABLE_GC && !defined(NDEBUG)
529 // We don't really need to free memory since we are about to exit the
530 // process, but doing so helps to find memory leaks.
531 free(heap);
532#endif
533
Damien429d7192013-10-04 19:53:11 +0100534 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
stijn9e040b72014-05-10 19:42:06 +0200535 return ret;
Damien429d7192013-10-04 19:53:11 +0100536}
Damien087d2182013-11-09 20:14:30 +0000537
Damien Georgee09ffa12014-02-05 23:57:48 +0000538uint mp_import_stat(const char *path) {
539 struct stat st;
540 if (stat(path, &st) == 0) {
541 if (S_ISDIR(st.st_mode)) {
542 return MP_IMPORT_STAT_DIR;
543 } else if (S_ISREG(st.st_mode)) {
544 return MP_IMPORT_STAT_FILE;
545 }
546 }
547 return MP_IMPORT_STAT_NO_EXIST;
548}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200549
550int DEBUG_printf(const char *fmt, ...) {
551 va_list ap;
552 va_start(ap, fmt);
553 int ret = vfprintf(stderr, fmt, ap);
554 va_end(ap);
555 return ret;
556}
Damien George26cf55a2014-04-08 14:08:14 +0000557
558void nlr_jump_fail(void *val) {
559 printf("FATAL: uncaught NLR %p\n", val);
560 exit(1);
561}