blob: 44542af51b44f3bdae59687b8f8c5848af86b82b [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>
Damien George124df6f2014-10-25 18:19:55 +010037#include <signal.h>
Damien429d7192013-10-04 19:53:11 +010038
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030039#include "mpconfig.h"
Damience89a212013-10-15 22:25:17 +010040#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010041#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000042#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010043#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010044#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010045#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000046#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000047#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000048#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000049#include "runtime0.h"
50#include "runtime.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 George124df6f2014-10-25 18:19:55 +010068STATIC mp_obj_t keyboard_interrupt_obj;
69
70STATIC void sighandler(int signum) {
71 if (signum == SIGINT) {
72 mp_obj_exception_clear_traceback(keyboard_interrupt_obj);
73 mp_pending_exception = keyboard_interrupt_obj;
74 // disable our handler so next we really die
75 struct sigaction sa;
76 sa.sa_handler = SIG_DFL;
77 sigemptyset(&sa.sa_mask);
78 sigaction(SIGINT, &sa, NULL);
79 }
80}
81
Damien George762d5752014-10-01 23:18:39 +010082#define FORCED_EXIT (0x100)
stijn9e040b72014-05-10 19:42:06 +020083// returns standard error codes: 0 for success, 1 for all other errors
Damien George762d5752014-10-01 23:18:39 +010084// if FORCED_EXIT bit is set then script raised SystemExit and the
85// value of the exit is in the lower 8 bits of the return value
stijn9e040b72014-05-10 19:42:06 +020086STATIC 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 +000087 if (lex == NULL) {
stijn9e040b72014-05-10 19:42:06 +020088 return 1;
Damien George136f6752014-01-07 14:54:15 +000089 }
90
91 if (0) {
92 // just tokenise
93 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
94 mp_token_show(mp_lexer_cur(lex));
95 mp_lexer_to_next(lex);
96 }
97 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +020098 return 0;
Damien George136f6752014-01-07 14:54:15 +000099 }
100
Damien Georgec5966122014-02-15 16:10:44 +0000101 mp_parse_error_kind_t parse_error_kind;
102 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +0000103
104 if (pn == MP_PARSE_NODE_NULL) {
105 // parse error
Damien Georgec5966122014-02-15 16:10:44 +0000106 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +0000107 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +0200108 return 1;
Damien George136f6752014-01-07 14:54:15 +0000109 }
110
Damien George08335002014-01-18 23:24:36 +0000111 qstr source_name = mp_lexer_source_name(lex);
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300112 #if MICROPY_PY___FILE__
113 if (input_kind == MP_PARSE_FILE_INPUT) {
114 mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
115 }
116 #endif
Damien George9528cd62014-01-15 21:23:31 +0000117 mp_lexer_free(lex);
118
Damien Georgecbd2f742014-01-19 11:48:48 +0000119 /*
120 printf("----------------\n");
121 mp_parse_node_print(pn, 0);
122 printf("----------------\n");
123 */
Damien George136f6752014-01-07 14:54:15 +0000124
Damien George65cad122014-04-06 11:48:15 +0100125 mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
Damien George136f6752014-01-07 14:54:15 +0000126
Damien George854c8c02014-10-05 22:25:36 +0100127 if (mp_obj_is_exception_instance(module_fun)) {
Damien George136f6752014-01-07 14:54:15 +0000128 // compile error
Damien George854c8c02014-10-05 22:25:36 +0100129 mp_obj_print_exception(module_fun);
stijn9e040b72014-05-10 19:42:06 +0200130 return 1;
Damien George136f6752014-01-07 14:54:15 +0000131 }
132
Damien Georgef22626e2014-04-10 11:30:35 +0100133 if (compile_only) {
stijn9e040b72014-05-10 19:42:06 +0200134 return 0;
Damien Georgef22626e2014-04-10 11:30:35 +0100135 }
136
Damien George124df6f2014-10-25 18:19:55 +0100137 // enable signal handler
138 struct sigaction sa;
139 sa.sa_handler = sighandler;
140 sigemptyset(&sa.sa_mask);
141 sigaction(SIGINT, &sa, NULL);
142 sa.sa_handler = SIG_DFL;
143
Damien George136f6752014-01-07 14:54:15 +0000144 // execute it
145 nlr_buf_t nlr;
146 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100147 mp_call_function_0(module_fun);
Damien George124df6f2014-10-25 18:19:55 +0100148 sigaction(SIGINT, &sa, NULL);
Damien George136f6752014-01-07 14:54:15 +0000149 nlr_pop();
stijn9e040b72014-05-10 19:42:06 +0200150 return 0;
Damien George136f6752014-01-07 14:54:15 +0000151 } else {
152 // uncaught exception
Damien George124df6f2014-10-25 18:19:55 +0100153 sigaction(SIGINT, &sa, NULL);
Damien George7a4ddd22014-05-24 23:32:19 +0100154 // check for SystemExit
155 mp_obj_t exc = (mp_obj_t)nlr.ret_val;
156 if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
Damien Georgeb92cbe62014-09-15 15:53:09 +0100157 // None is an exit value of 0; an int is its value; anything else is 1
Damien George8f81b5c2014-08-16 14:32:06 +0100158 mp_obj_t exit_val = mp_obj_exception_get_value(exc);
Damien Georgeb92cbe62014-09-15 15:53:09 +0100159 mp_int_t val = 0;
160 if (exit_val != mp_const_none && !mp_obj_get_int_maybe(exit_val, &val)) {
161 val = 1;
Damien George8f81b5c2014-08-16 14:32:06 +0100162 }
Damien George762d5752014-10-01 23:18:39 +0100163 return FORCED_EXIT | (val & 255);
Damien George7a4ddd22014-05-24 23:32:19 +0100164 }
Damien George136b1492014-01-19 12:38:49 +0000165 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
stijn9e040b72014-05-10 19:42:06 +0200166 return 1;
Damien George136f6752014-01-07 14:54:15 +0000167 }
168}
169
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300170STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100171 int l1 = strlen(s1);
172 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300173 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100174 memcpy(s, s1, l1);
175 if (sep_char != 0) {
176 s[l1] = sep_char;
177 l1 += 1;
178 }
179 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100180 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100181 return s;
182}
183
Damien George762d5752014-10-01 23:18:39 +0100184STATIC int do_repl(void) {
stijnec6fa872014-06-28 21:04:20 +0200185 printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
Damien George6827f9f2014-04-07 13:27:50 +0100186
Damien5ac1b2e2013-10-18 19:58:12 +0100187 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200188 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100189 if (line == NULL) {
190 // EOF
Damien George762d5752014-10-01 23:18:39 +0100191 return 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100192 }
Damien George97790452014-04-08 11:04:29 +0000193 while (mp_repl_continue_with_input(line)) {
194 char *line2 = prompt("... ");
195 if (line2 == NULL) {
196 break;
Damien5ac1b2e2013-10-18 19:58:12 +0100197 }
Damien George97790452014-04-08 11:04:29 +0000198 char *line3 = strjoin(line, '\n', line2);
199 free(line);
200 free(line2);
201 line = line3;
Damien5ac1b2e2013-10-18 19:58:12 +0100202 }
Damienfa2162b2013-10-20 17:42:00 +0100203
Damien Georgeb829b5c2014-01-25 13:51:19 +0000204 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 +0100205 int ret = execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
206 if (ret & FORCED_EXIT) {
207 return ret;
208 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200209 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100210 }
211}
212
stijn9e040b72014-05-10 19:42:06 +0200213STATIC int do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000214 mp_lexer_t *lex = mp_lexer_new_from_file(file);
stijn9e040b72014-05-10 19:42:06 +0200215 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien George136f6752014-01-07 14:54:15 +0000216}
Damien429d7192013-10-04 19:53:11 +0100217
stijn9e040b72014-05-10 19:42:06 +0200218STATIC int do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000219 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 +0100220 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100221}
Damien429d7192013-10-04 19:53:11 +0100222
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300223int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000224 printf(
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300225"usage: %s [<opts>] [-X <implopt>] [-c <command>] [<filename>]\n"
226"Options:\n"
227"-v : verbose (trace various operations); can be multiple\n"
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300228"-O[N] : apply bytecode optimizations of level N\n"
Damien George5e349092014-03-08 19:04:47 +0000229"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300230"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300231);
232 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100233 printf(
Damien Georgef22626e2014-04-10 11:30:35 +0100234" compile-only -- parse and compile only\n"
Damien George65cad122014-04-06 11:48:15 +0100235" emit={bytecode,native,viper} -- set the default code emitter\n"
236);
237 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300238#if MICROPY_ENABLE_GC
239 printf(
Paul Sokolovsky141df2d2014-06-24 16:58:00 +0300240" heapsize=<n> -- set the heap size for the GC (default %ld)\n"
241, heap_size);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300242 impl_opts_cnt++;
243#endif
244
245 if (impl_opts_cnt == 0) {
246 printf(" (none)\n");
247 }
248
Damien George136f6752014-01-07 14:54:15 +0000249 return 1;
250}
251
Damien Georgeb0261342014-09-23 18:10:17 +0100252#if MICROPY_MEM_STATS
Damien George0b13f3e2014-10-24 23:12:25 +0100253STATIC mp_obj_t mem_info(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeb0261342014-09-23 18:10:17 +0100254 printf("mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
Paul Sokolovsky914bcf12014-05-31 02:34:39 +0300255 m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
Damien George4abff752014-08-30 14:59:21 +0100256 printf("stack: " UINT_FMT "\n", mp_stack_usage());
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300257#if MICROPY_ENABLE_GC
Paul Sokolovsky59a2f482014-04-17 18:32:36 +0300258 gc_dump_info();
Damien George0b13f3e2014-10-24 23:12:25 +0100259 if (n_args == 1) {
260 // arg given means dump gc allocation table
261 gc_dump_alloc_table();
262 }
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300263#endif
Damien George4d5b28c2014-01-29 18:56:46 +0000264 return mp_const_none;
265}
Damien George0b13f3e2014-10-24 23:12:25 +0100266STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mem_info_obj, 0, 1, mem_info);
Damien Georgeb0261342014-09-23 18:10:17 +0100267#endif
Damien George4d5b28c2014-01-29 18:56:46 +0000268
Damien George3c658a42014-08-24 16:28:17 +0100269STATIC mp_obj_t qstr_info(void) {
Damien George39dc1452014-10-03 19:52:22 +0100270 mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
Damien George4d5b28c2014-01-29 18:56:46 +0000271 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
Damien George39dc1452014-10-03 19:52:22 +0100272 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 +0000273 return mp_const_none;
274}
Damien George3c658a42014-08-24 16:28:17 +0100275STATIC MP_DEFINE_CONST_FUN_OBJ_0(qstr_info_obj, qstr_info);
Damien George4d5b28c2014-01-29 18:56:46 +0000276
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200277// Process options which set interpreter init options
278void pre_process_options(int argc, char **argv) {
279 for (int a = 1; a < argc; a++) {
280 if (argv[a][0] == '-') {
281 if (strcmp(argv[a], "-X") == 0) {
282 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300283 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200284 }
Damien George5e349092014-03-08 19:04:47 +0000285 if (0) {
Damien Georgef22626e2014-04-10 11:30:35 +0100286 } else if (strcmp(argv[a + 1], "compile-only") == 0) {
287 compile_only = true;
Damien George65cad122014-04-06 11:48:15 +0100288 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
Damien George96f137b2014-05-12 22:35:37 +0100289 emit_opt = MP_EMIT_OPT_BYTECODE;
Damien George65cad122014-04-06 11:48:15 +0100290 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
291 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
292 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
293 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200294#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000295 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskyfcff4662014-10-25 04:43:08 +0300296 char *end;
297 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, &end, 0);
298 // Don't bring unneeded libc dependencies like tolower()
299 if ((*end | 0x20) == 'k') {
300 heap_size *= 1024;
301 } else if ((*end | 0x20) == 'm') {
302 heap_size *= 1024 * 1024;
303 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200304#endif
Damien George5e349092014-03-08 19:04:47 +0000305 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300306 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000307 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200308 a++;
309 }
310 }
311 }
312}
313
stijndf3ab072014-06-05 12:33:55 +0200314#ifdef _WIN32
315#define PATHLIST_SEP_CHAR ';'
316#else
317#define PATHLIST_SEP_CHAR ':'
318#endif
319
Damien5ac1b2e2013-10-18 19:58:12 +0100320int main(int argc, char **argv) {
Paul Sokolovskycaa73342014-07-01 02:13:42 +0300321 mp_stack_set_limit(32768);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200322
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200323 pre_process_options(argc, argv);
324
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200325#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200326 char *heap = malloc(heap_size);
327 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200328#endif
329
Damien Georged17926d2014-03-30 13:35:08 +0100330 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100331
Damien George124df6f2014-10-25 18:19:55 +0100332 // create keyboard interrupt object
333 keyboard_interrupt_obj = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
334
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200335 char *home = getenv("HOME");
336 char *path = getenv("MICROPYPATH");
337 if (path == NULL) {
338 path = "~/.micropython/lib:/usr/lib/micropython";
339 }
Damien George9c4cbe22014-08-30 14:04:14 +0100340 mp_uint_t path_num = 1; // [0] is for current dir (or base dir of the script)
stijndf3ab072014-06-05 12:33:55 +0200341 for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200342 path_num++;
343 if (p != NULL) {
344 p++;
345 }
346 }
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300347 mp_obj_list_init(mp_sys_path, path_num);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200348 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100349 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200350 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200351 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200352 for (int i = 1; i < path_num; i++) {
stijndf3ab072014-06-05 12:33:55 +0200353 char *p1 = strchr(p, PATHLIST_SEP_CHAR);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200354 if (p1 == NULL) {
355 p1 = p + strlen(p);
356 }
357 if (p[0] == '~' && p[1] == '/' && home != NULL) {
358 // Expand standalone ~ to $HOME
359 CHECKBUF(buf, PATH_MAX);
360 CHECKBUF_APPEND(buf, home, strlen(home));
361 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200362 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200363 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200364 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200365 }
366 p = p1 + 1;
367 }
368
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300369 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200370
Damien Georgeb0261342014-09-23 18:10:17 +0100371 #if MICROPY_MEM_STATS
Damien George3c658a42014-08-24 16:28:17 +0100372 mp_store_name(qstr_from_str("mem_info"), (mp_obj_t*)&mem_info_obj);
Damien Georgeb0261342014-09-23 18:10:17 +0100373 #endif
Damien George3c658a42014-08-24 16:28:17 +0100374 mp_store_name(qstr_from_str("qstr_info"), (mp_obj_t*)&qstr_info_obj);
Damien George12eacca2014-01-21 21:54:15 +0000375
Damien George062478e2014-01-09 20:57:50 +0000376 // Here is some example code to create a class and instance of that class.
377 // First is the Python, then the C code.
378 //
379 // class TestClass:
380 // pass
381 // test_obj = TestClass()
382 // test_obj.attr = 42
Damien Georgec327c0d2014-05-04 12:24:26 +0100383 //
384 // mp_obj_t test_class_type, test_class_instance;
385 // test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
386 // mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
387 // mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000388
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000389 /*
390 printf("bytes:\n");
391 printf(" total %d\n", m_get_total_bytes_allocated());
392 printf(" cur %d\n", m_get_current_bytes_allocated());
393 printf(" peak %d\n", m_get_peak_bytes_allocated());
394 */
395
stijn9e040b72014-05-10 19:42:06 +0200396 const int NOTHING_EXECUTED = -2;
397 int ret = NOTHING_EXECUTED;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200398 for (int a = 1; a < argc; a++) {
399 if (argv[a][0] == '-') {
400 if (strcmp(argv[a], "-c") == 0) {
401 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300402 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000403 }
stijn9e040b72014-05-10 19:42:06 +0200404 ret = do_str(argv[a + 1]);
Damien George762d5752014-10-01 23:18:39 +0100405 if (ret & FORCED_EXIT) {
406 break;
407 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200408 a += 1;
409 } else if (strcmp(argv[a], "-X") == 0) {
410 a += 1;
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300411 } else if (strcmp(argv[a], "-v") == 0) {
412 mp_verbose_flag++;
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300413 } else if (strncmp(argv[a], "-O", 2) == 0) {
414 if (isdigit(argv[a][2])) {
415 mp_optimise_value = argv[a][2] & 0xf;
416 } else {
417 mp_optimise_value = 0;
418 for (char *p = argv[a] + 1; *p && *p == 'O'; p++, mp_optimise_value++);
419 }
Damien George136f6752014-01-07 14:54:15 +0000420 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300421 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000422 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200423 } else {
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300424 char *pathbuf = malloc(PATH_MAX);
425 char *basedir = realpath(argv[a], pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300426 if (basedir == NULL) {
Paul Sokolovskydd0dee32014-06-03 01:01:39 +0300427 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[a], errno);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300428 perror("");
429 // CPython exits with 2 in such case
stijn9e040b72014-05-10 19:42:06 +0200430 ret = 2;
431 break;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200432 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300433
434 // Set base dir of the script as first entry in sys.path
435 char *p = strrchr(basedir, '/');
436 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300437 free(pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300438
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200439 for (int i = a; i < argc; i++) {
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300440 mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200441 }
stijn9e040b72014-05-10 19:42:06 +0200442 ret = do_file(argv[a]);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200443 break;
Damien George136f6752014-01-07 14:54:15 +0000444 }
Damien5ac1b2e2013-10-18 19:58:12 +0100445 }
Damien George136f6752014-01-07 14:54:15 +0000446
stijn9e040b72014-05-10 19:42:06 +0200447 if (ret == NOTHING_EXECUTED) {
Damien George762d5752014-10-01 23:18:39 +0100448 ret = do_repl();
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200449 }
450
Damien Georged17926d2014-03-30 13:35:08 +0100451 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100452
Damien George762d5752014-10-01 23:18:39 +0100453#if MICROPY_ENABLE_GC && !defined(NDEBUG)
454 // We don't really need to free memory since we are about to exit the
455 // process, but doing so helps to find memory leaks.
456 free(heap);
457#endif
458
Damien429d7192013-10-04 19:53:11 +0100459 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
stijn9e040b72014-05-10 19:42:06 +0200460 return ret;
Damien429d7192013-10-04 19:53:11 +0100461}
Damien087d2182013-11-09 20:14:30 +0000462
Damien Georgee09ffa12014-02-05 23:57:48 +0000463uint mp_import_stat(const char *path) {
464 struct stat st;
465 if (stat(path, &st) == 0) {
466 if (S_ISDIR(st.st_mode)) {
467 return MP_IMPORT_STAT_DIR;
468 } else if (S_ISREG(st.st_mode)) {
469 return MP_IMPORT_STAT_FILE;
470 }
471 }
472 return MP_IMPORT_STAT_NO_EXIST;
473}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200474
475int DEBUG_printf(const char *fmt, ...) {
476 va_list ap;
477 va_start(ap, fmt);
478 int ret = vfprintf(stderr, fmt, ap);
479 va_end(ap);
480 return ret;
481}
Damien George26cf55a2014-04-08 14:08:14 +0000482
483void nlr_jump_fail(void *val) {
484 printf("FATAL: uncaught NLR %p\n", val);
485 exit(1);
486}