blob: ca5dea99acfa494148f54aba77c3a9e62a86f746 [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
Damien Georgeb0261342014-09-23 18:10:17 +010057STATIC bool compile_only = false;
58STATIC uint emit_opt = MP_EMIT_OPT_NONE;
59mp_uint_t mp_verbose_flag = 0;
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
Damien Georgec76af322014-10-26 00:42:41 +010067#ifndef _WIN32
68#include <signal.h>
69
Damien George124df6f2014-10-25 18:19:55 +010070STATIC mp_obj_t keyboard_interrupt_obj;
71
72STATIC void sighandler(int signum) {
73 if (signum == SIGINT) {
74 mp_obj_exception_clear_traceback(keyboard_interrupt_obj);
75 mp_pending_exception = keyboard_interrupt_obj;
76 // disable our handler so next we really die
77 struct sigaction sa;
78 sa.sa_handler = SIG_DFL;
79 sigemptyset(&sa.sa_mask);
80 sigaction(SIGINT, &sa, NULL);
81 }
82}
Damien Georgec76af322014-10-26 00:42:41 +010083#endif
Damien George124df6f2014-10-25 18:19:55 +010084
Damien George762d5752014-10-01 23:18:39 +010085#define FORCED_EXIT (0x100)
stijn9e040b72014-05-10 19:42:06 +020086// returns standard error codes: 0 for success, 1 for all other errors
Damien George762d5752014-10-01 23:18:39 +010087// if FORCED_EXIT bit is set then script raised SystemExit and the
88// value of the exit is in the lower 8 bits of the return value
stijn9e040b72014-05-10 19:42:06 +020089STATIC 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 +000090 if (lex == NULL) {
stijn9e040b72014-05-10 19:42:06 +020091 return 1;
Damien George136f6752014-01-07 14:54:15 +000092 }
93
94 if (0) {
95 // just tokenise
96 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
97 mp_token_show(mp_lexer_cur(lex));
98 mp_lexer_to_next(lex);
99 }
100 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +0200101 return 0;
Damien George136f6752014-01-07 14:54:15 +0000102 }
103
Damien Georgec5966122014-02-15 16:10:44 +0000104 mp_parse_error_kind_t parse_error_kind;
105 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +0000106
107 if (pn == MP_PARSE_NODE_NULL) {
108 // parse error
Damien Georgec5966122014-02-15 16:10:44 +0000109 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +0000110 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +0200111 return 1;
Damien George136f6752014-01-07 14:54:15 +0000112 }
113
Damien George08335002014-01-18 23:24:36 +0000114 qstr source_name = mp_lexer_source_name(lex);
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300115 #if MICROPY_PY___FILE__
116 if (input_kind == MP_PARSE_FILE_INPUT) {
117 mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
118 }
119 #endif
Damien George9528cd62014-01-15 21:23:31 +0000120 mp_lexer_free(lex);
121
Damien Georgecbd2f742014-01-19 11:48:48 +0000122 /*
123 printf("----------------\n");
124 mp_parse_node_print(pn, 0);
125 printf("----------------\n");
126 */
Damien George136f6752014-01-07 14:54:15 +0000127
Damien George65cad122014-04-06 11:48:15 +0100128 mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
Damien George136f6752014-01-07 14:54:15 +0000129
Damien George854c8c02014-10-05 22:25:36 +0100130 if (mp_obj_is_exception_instance(module_fun)) {
Damien George136f6752014-01-07 14:54:15 +0000131 // compile error
Damien George854c8c02014-10-05 22:25:36 +0100132 mp_obj_print_exception(module_fun);
stijn9e040b72014-05-10 19:42:06 +0200133 return 1;
Damien George136f6752014-01-07 14:54:15 +0000134 }
135
Damien Georgef22626e2014-04-10 11:30:35 +0100136 if (compile_only) {
stijn9e040b72014-05-10 19:42:06 +0200137 return 0;
Damien Georgef22626e2014-04-10 11:30:35 +0100138 }
139
Damien Georgec76af322014-10-26 00:42:41 +0100140 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100141 // enable signal handler
142 struct sigaction sa;
143 sa.sa_handler = sighandler;
144 sigemptyset(&sa.sa_mask);
145 sigaction(SIGINT, &sa, NULL);
146 sa.sa_handler = SIG_DFL;
Damien Georgec76af322014-10-26 00:42:41 +0100147 #endif
Damien George124df6f2014-10-25 18:19:55 +0100148
Damien George136f6752014-01-07 14:54:15 +0000149 // execute it
150 nlr_buf_t nlr;
151 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100152 mp_call_function_0(module_fun);
Damien Georgec76af322014-10-26 00:42:41 +0100153 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100154 sigaction(SIGINT, &sa, NULL);
Damien Georgec76af322014-10-26 00:42:41 +0100155 #endif
Damien George136f6752014-01-07 14:54:15 +0000156 nlr_pop();
stijn9e040b72014-05-10 19:42:06 +0200157 return 0;
Damien George136f6752014-01-07 14:54:15 +0000158 } else {
159 // uncaught exception
Damien Georgec76af322014-10-26 00:42:41 +0100160 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100161 sigaction(SIGINT, &sa, NULL);
Damien Georgec76af322014-10-26 00:42:41 +0100162 #endif
Damien George7a4ddd22014-05-24 23:32:19 +0100163 // check for SystemExit
164 mp_obj_t exc = (mp_obj_t)nlr.ret_val;
165 if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
Damien Georgeb92cbe62014-09-15 15:53:09 +0100166 // None is an exit value of 0; an int is its value; anything else is 1
Damien George8f81b5c2014-08-16 14:32:06 +0100167 mp_obj_t exit_val = mp_obj_exception_get_value(exc);
Damien Georgeb92cbe62014-09-15 15:53:09 +0100168 mp_int_t val = 0;
169 if (exit_val != mp_const_none && !mp_obj_get_int_maybe(exit_val, &val)) {
170 val = 1;
Damien George8f81b5c2014-08-16 14:32:06 +0100171 }
Damien George762d5752014-10-01 23:18:39 +0100172 return FORCED_EXIT | (val & 255);
Damien George7a4ddd22014-05-24 23:32:19 +0100173 }
Damien George136b1492014-01-19 12:38:49 +0000174 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
stijn9e040b72014-05-10 19:42:06 +0200175 return 1;
Damien George136f6752014-01-07 14:54:15 +0000176 }
177}
178
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300179STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100180 int l1 = strlen(s1);
181 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300182 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100183 memcpy(s, s1, l1);
184 if (sep_char != 0) {
185 s[l1] = sep_char;
186 l1 += 1;
187 }
188 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100189 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100190 return s;
191}
192
Damien George762d5752014-10-01 23:18:39 +0100193STATIC int do_repl(void) {
stijnec6fa872014-06-28 21:04:20 +0200194 printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
Damien George6827f9f2014-04-07 13:27:50 +0100195
Damien5ac1b2e2013-10-18 19:58:12 +0100196 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200197 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100198 if (line == NULL) {
199 // EOF
Damien George762d5752014-10-01 23:18:39 +0100200 return 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100201 }
Damien George97790452014-04-08 11:04:29 +0000202 while (mp_repl_continue_with_input(line)) {
203 char *line2 = prompt("... ");
204 if (line2 == NULL) {
205 break;
Damien5ac1b2e2013-10-18 19:58:12 +0100206 }
Damien George97790452014-04-08 11:04:29 +0000207 char *line3 = strjoin(line, '\n', line2);
208 free(line);
209 free(line2);
210 line = line3;
Damien5ac1b2e2013-10-18 19:58:12 +0100211 }
Damienfa2162b2013-10-20 17:42:00 +0100212
Damien Georgeb829b5c2014-01-25 13:51:19 +0000213 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 +0100214 int ret = execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
215 if (ret & FORCED_EXIT) {
216 return ret;
217 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200218 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100219 }
220}
221
stijn9e040b72014-05-10 19:42:06 +0200222STATIC int do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000223 mp_lexer_t *lex = mp_lexer_new_from_file(file);
stijn9e040b72014-05-10 19:42:06 +0200224 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien George136f6752014-01-07 14:54:15 +0000225}
Damien429d7192013-10-04 19:53:11 +0100226
stijn9e040b72014-05-10 19:42:06 +0200227STATIC int do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000228 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 +0100229 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100230}
Damien429d7192013-10-04 19:53:11 +0100231
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300232int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000233 printf(
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300234"usage: %s [<opts>] [-X <implopt>] [-c <command>] [<filename>]\n"
235"Options:\n"
236"-v : verbose (trace various operations); can be multiple\n"
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300237"-O[N] : apply bytecode optimizations of level N\n"
Damien George5e349092014-03-08 19:04:47 +0000238"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300239"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300240);
241 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100242 printf(
Damien Georgef22626e2014-04-10 11:30:35 +0100243" compile-only -- parse and compile only\n"
Damien George65cad122014-04-06 11:48:15 +0100244" emit={bytecode,native,viper} -- set the default code emitter\n"
245);
246 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300247#if MICROPY_ENABLE_GC
248 printf(
Paul Sokolovsky141df2d2014-06-24 16:58:00 +0300249" heapsize=<n> -- set the heap size for the GC (default %ld)\n"
250, heap_size);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300251 impl_opts_cnt++;
252#endif
253
254 if (impl_opts_cnt == 0) {
255 printf(" (none)\n");
256 }
257
Damien George136f6752014-01-07 14:54:15 +0000258 return 1;
259}
260
Damien Georgeb0261342014-09-23 18:10:17 +0100261#if MICROPY_MEM_STATS
Damien George0b13f3e2014-10-24 23:12:25 +0100262STATIC mp_obj_t mem_info(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeb0261342014-09-23 18:10:17 +0100263 printf("mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
Paul Sokolovsky914bcf12014-05-31 02:34:39 +0300264 m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
Damien George4abff752014-08-30 14:59:21 +0100265 printf("stack: " UINT_FMT "\n", mp_stack_usage());
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300266#if MICROPY_ENABLE_GC
Paul Sokolovsky59a2f482014-04-17 18:32:36 +0300267 gc_dump_info();
Damien George0b13f3e2014-10-24 23:12:25 +0100268 if (n_args == 1) {
269 // arg given means dump gc allocation table
270 gc_dump_alloc_table();
271 }
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300272#endif
Damien George4d5b28c2014-01-29 18:56:46 +0000273 return mp_const_none;
274}
Damien George0b13f3e2014-10-24 23:12:25 +0100275STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mem_info_obj, 0, 1, mem_info);
Damien Georgeb0261342014-09-23 18:10:17 +0100276#endif
Damien George4d5b28c2014-01-29 18:56:46 +0000277
Damien George3c658a42014-08-24 16:28:17 +0100278STATIC mp_obj_t qstr_info(void) {
Damien George39dc1452014-10-03 19:52:22 +0100279 mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
Damien George4d5b28c2014-01-29 18:56:46 +0000280 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
Damien George39dc1452014-10-03 19:52:22 +0100281 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 +0000282 return mp_const_none;
283}
Damien George3c658a42014-08-24 16:28:17 +0100284STATIC MP_DEFINE_CONST_FUN_OBJ_0(qstr_info_obj, qstr_info);
Damien George4d5b28c2014-01-29 18:56:46 +0000285
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200286// Process options which set interpreter init options
287void pre_process_options(int argc, char **argv) {
288 for (int a = 1; a < argc; a++) {
289 if (argv[a][0] == '-') {
290 if (strcmp(argv[a], "-X") == 0) {
291 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300292 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200293 }
Damien George5e349092014-03-08 19:04:47 +0000294 if (0) {
Damien Georgef22626e2014-04-10 11:30:35 +0100295 } else if (strcmp(argv[a + 1], "compile-only") == 0) {
296 compile_only = true;
Damien George65cad122014-04-06 11:48:15 +0100297 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
Damien George96f137b2014-05-12 22:35:37 +0100298 emit_opt = MP_EMIT_OPT_BYTECODE;
Damien George65cad122014-04-06 11:48:15 +0100299 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
300 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
301 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
302 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200303#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000304 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskyfcff4662014-10-25 04:43:08 +0300305 char *end;
306 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, &end, 0);
307 // Don't bring unneeded libc dependencies like tolower()
308 if ((*end | 0x20) == 'k') {
309 heap_size *= 1024;
310 } else if ((*end | 0x20) == 'm') {
311 heap_size *= 1024 * 1024;
312 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200313#endif
Damien George5e349092014-03-08 19:04:47 +0000314 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300315 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000316 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200317 a++;
318 }
319 }
320 }
321}
322
stijndf3ab072014-06-05 12:33:55 +0200323#ifdef _WIN32
324#define PATHLIST_SEP_CHAR ';'
325#else
326#define PATHLIST_SEP_CHAR ':'
327#endif
328
Damien5ac1b2e2013-10-18 19:58:12 +0100329int main(int argc, char **argv) {
Paul Sokolovskycaa73342014-07-01 02:13:42 +0300330 mp_stack_set_limit(32768);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200331
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200332 pre_process_options(argc, argv);
333
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200334#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200335 char *heap = malloc(heap_size);
336 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200337#endif
338
Damien Georged17926d2014-03-30 13:35:08 +0100339 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100340
Damien Georgec76af322014-10-26 00:42:41 +0100341 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100342 // create keyboard interrupt object
343 keyboard_interrupt_obj = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
Damien Georgec76af322014-10-26 00:42:41 +0100344 #endif
Damien George124df6f2014-10-25 18:19:55 +0100345
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200346 char *home = getenv("HOME");
347 char *path = getenv("MICROPYPATH");
348 if (path == NULL) {
349 path = "~/.micropython/lib:/usr/lib/micropython";
350 }
Damien George9c4cbe22014-08-30 14:04:14 +0100351 mp_uint_t path_num = 1; // [0] is for current dir (or base dir of the script)
stijndf3ab072014-06-05 12:33:55 +0200352 for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200353 path_num++;
354 if (p != NULL) {
355 p++;
356 }
357 }
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300358 mp_obj_list_init(mp_sys_path, path_num);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200359 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100360 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200361 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200362 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200363 for (int i = 1; i < path_num; i++) {
stijndf3ab072014-06-05 12:33:55 +0200364 char *p1 = strchr(p, PATHLIST_SEP_CHAR);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200365 if (p1 == NULL) {
366 p1 = p + strlen(p);
367 }
368 if (p[0] == '~' && p[1] == '/' && home != NULL) {
369 // Expand standalone ~ to $HOME
370 CHECKBUF(buf, PATH_MAX);
371 CHECKBUF_APPEND(buf, home, strlen(home));
372 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200373 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200374 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200375 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200376 }
377 p = p1 + 1;
378 }
379
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300380 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200381
Damien Georgeb0261342014-09-23 18:10:17 +0100382 #if MICROPY_MEM_STATS
Damien George3c658a42014-08-24 16:28:17 +0100383 mp_store_name(qstr_from_str("mem_info"), (mp_obj_t*)&mem_info_obj);
Damien Georgeb0261342014-09-23 18:10:17 +0100384 #endif
Damien George3c658a42014-08-24 16:28:17 +0100385 mp_store_name(qstr_from_str("qstr_info"), (mp_obj_t*)&qstr_info_obj);
Damien George12eacca2014-01-21 21:54:15 +0000386
Damien George062478e2014-01-09 20:57:50 +0000387 // Here is some example code to create a class and instance of that class.
388 // First is the Python, then the C code.
389 //
390 // class TestClass:
391 // pass
392 // test_obj = TestClass()
393 // test_obj.attr = 42
Damien Georgec327c0d2014-05-04 12:24:26 +0100394 //
395 // mp_obj_t test_class_type, test_class_instance;
396 // test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
397 // mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
398 // mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000399
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000400 /*
401 printf("bytes:\n");
402 printf(" total %d\n", m_get_total_bytes_allocated());
403 printf(" cur %d\n", m_get_current_bytes_allocated());
404 printf(" peak %d\n", m_get_peak_bytes_allocated());
405 */
406
stijn9e040b72014-05-10 19:42:06 +0200407 const int NOTHING_EXECUTED = -2;
408 int ret = NOTHING_EXECUTED;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200409 for (int a = 1; a < argc; a++) {
410 if (argv[a][0] == '-') {
411 if (strcmp(argv[a], "-c") == 0) {
412 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300413 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000414 }
stijn9e040b72014-05-10 19:42:06 +0200415 ret = do_str(argv[a + 1]);
Damien George762d5752014-10-01 23:18:39 +0100416 if (ret & FORCED_EXIT) {
417 break;
418 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200419 a += 1;
420 } else if (strcmp(argv[a], "-X") == 0) {
421 a += 1;
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300422 } else if (strcmp(argv[a], "-v") == 0) {
423 mp_verbose_flag++;
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300424 } else if (strncmp(argv[a], "-O", 2) == 0) {
425 if (isdigit(argv[a][2])) {
426 mp_optimise_value = argv[a][2] & 0xf;
427 } else {
428 mp_optimise_value = 0;
429 for (char *p = argv[a] + 1; *p && *p == 'O'; p++, mp_optimise_value++);
430 }
Damien George136f6752014-01-07 14:54:15 +0000431 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300432 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000433 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200434 } else {
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300435 char *pathbuf = malloc(PATH_MAX);
436 char *basedir = realpath(argv[a], pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300437 if (basedir == NULL) {
Paul Sokolovskydd0dee32014-06-03 01:01:39 +0300438 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[a], errno);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300439 perror("");
440 // CPython exits with 2 in such case
stijn9e040b72014-05-10 19:42:06 +0200441 ret = 2;
442 break;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200443 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300444
445 // Set base dir of the script as first entry in sys.path
446 char *p = strrchr(basedir, '/');
447 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300448 free(pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300449
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200450 for (int i = a; i < argc; i++) {
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300451 mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200452 }
stijn9e040b72014-05-10 19:42:06 +0200453 ret = do_file(argv[a]);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200454 break;
Damien George136f6752014-01-07 14:54:15 +0000455 }
Damien5ac1b2e2013-10-18 19:58:12 +0100456 }
Damien George136f6752014-01-07 14:54:15 +0000457
stijn9e040b72014-05-10 19:42:06 +0200458 if (ret == NOTHING_EXECUTED) {
Damien George762d5752014-10-01 23:18:39 +0100459 ret = do_repl();
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200460 }
461
Damien Georged17926d2014-03-30 13:35:08 +0100462 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100463
Damien George762d5752014-10-01 23:18:39 +0100464#if MICROPY_ENABLE_GC && !defined(NDEBUG)
465 // We don't really need to free memory since we are about to exit the
466 // process, but doing so helps to find memory leaks.
467 free(heap);
468#endif
469
Damien429d7192013-10-04 19:53:11 +0100470 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
stijn9e040b72014-05-10 19:42:06 +0200471 return ret;
Damien429d7192013-10-04 19:53:11 +0100472}
Damien087d2182013-11-09 20:14:30 +0000473
Damien Georgee09ffa12014-02-05 23:57:48 +0000474uint mp_import_stat(const char *path) {
475 struct stat st;
476 if (stat(path, &st) == 0) {
477 if (S_ISDIR(st.st_mode)) {
478 return MP_IMPORT_STAT_DIR;
479 } else if (S_ISREG(st.st_mode)) {
480 return MP_IMPORT_STAT_FILE;
481 }
482 }
483 return MP_IMPORT_STAT_NO_EXIST;
484}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200485
486int DEBUG_printf(const char *fmt, ...) {
487 va_list ap;
488 va_start(ap, fmt);
489 int ret = vfprintf(stderr, fmt, ap);
490 va_end(ap);
491 return ret;
492}
Damien George26cf55a2014-04-08 14:08:14 +0000493
494void nlr_jump_fail(void *val) {
495 printf("FATAL: uncaught NLR %p\n", val);
496 exit(1);
497}