blob: 05780bdeb0643c0e09dd30465ac0d6d92c969385 [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>
xbec93a2212014-03-15 23:24:34 -070033#include <sys/stat.h>
34#include <sys/types.h>
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +030035#include <errno.h>
Damien429d7192013-10-04 19:53:11 +010036
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030037#include "mpconfig.h"
Damience89a212013-10-15 22:25:17 +010038#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010039#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000040#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010041#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010042#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010043#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000044#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000045#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000046#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000047#include "runtime0.h"
48#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010049#include "repl.h"
Paul Sokolovskye7db8172014-02-11 00:44:37 +020050#include "gc.h"
Damien Georged553be52014-04-17 18:03:27 +010051#include "genhdr/py-version.h"
Damien429d7192013-10-04 19:53:11 +010052
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020053#if MICROPY_USE_READLINE
Damien5ac1b2e2013-10-18 19:58:12 +010054#include <readline/readline.h>
Paul Sokolovsky903b24f2014-01-01 14:54:39 +020055#include <readline/history.h>
Paul Sokolovskyfa027672014-01-01 18:28:01 +020056#endif
Damien429d7192013-10-04 19:53:11 +010057
Damien Georgef22626e2014-04-10 11:30:35 +010058// Command line options, with their defaults
59bool compile_only = false;
Damien George65cad122014-04-06 11:48:15 +010060uint emit_opt = MP_EMIT_OPT_NONE;
61
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.
65long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020066#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020067
68// Stack top at the start of program
69void *stack_top;
70
Paul Sokolovsky9945f332014-02-08 21:10:18 +020071void microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020072void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020073void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020074
Paul Sokolovskycd31d822014-04-04 20:25:53 +030075STATIC void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
Damien George136f6752014-01-07 14:54:15 +000076 if (lex == NULL) {
77 return;
78 }
79
80 if (0) {
81 // just tokenise
82 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
83 mp_token_show(mp_lexer_cur(lex));
84 mp_lexer_to_next(lex);
85 }
86 mp_lexer_free(lex);
87 return;
88 }
89
Damien Georgec5966122014-02-15 16:10:44 +000090 mp_parse_error_kind_t parse_error_kind;
91 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +000092
93 if (pn == MP_PARSE_NODE_NULL) {
94 // parse error
Damien Georgec5966122014-02-15 16:10:44 +000095 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +000096 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000097 return;
98 }
99
Damien George08335002014-01-18 23:24:36 +0000100 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +0000101 mp_lexer_free(lex);
102
Damien Georgecbd2f742014-01-19 11:48:48 +0000103 /*
104 printf("----------------\n");
105 mp_parse_node_print(pn, 0);
106 printf("----------------\n");
107 */
Damien George136f6752014-01-07 14:54:15 +0000108
Damien George65cad122014-04-06 11:48:15 +0100109 mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
Damien George136f6752014-01-07 14:54:15 +0000110
111 if (module_fun == mp_const_none) {
112 // compile error
113 return;
114 }
115
Damien Georgef22626e2014-04-10 11:30:35 +0100116 if (compile_only) {
117 return;
118 }
119
Damien George136f6752014-01-07 14:54:15 +0000120 // execute it
121 nlr_buf_t nlr;
122 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100123 mp_call_function_0(module_fun);
Damien George136f6752014-01-07 14:54:15 +0000124 nlr_pop();
125 } else {
126 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +0000127 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +0000128 }
129}
130
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300131STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100132 int l1 = strlen(s1);
133 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300134 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100135 memcpy(s, s1, l1);
136 if (sep_char != 0) {
137 s[l1] = sep_char;
138 l1 += 1;
139 }
140 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100141 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100142 return s;
143}
144
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300145STATIC char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200146#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200147 char *line = readline(p);
148 if (line) {
149 add_history(line);
150 }
151#else
152 static char buf[256];
153 fputs(p, stdout);
154 char *s = fgets(buf, sizeof(buf), stdin);
155 if (!s) {
156 return NULL;
157 }
158 int l = strlen(buf);
159 if (buf[l - 1] == '\n') {
160 buf[l - 1] = 0;
161 } else {
162 l++;
163 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200164 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200165 memcpy(line, buf, l);
166#endif
167 return line;
168}
169
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300170STATIC void do_repl(void) {
Damien George6ec835d2014-05-03 19:08:02 +0100171 printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; UNIX version\n");
Damien George6827f9f2014-04-07 13:27:50 +0100172
Damien5ac1b2e2013-10-18 19:58:12 +0100173 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200174 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100175 if (line == NULL) {
176 // EOF
177 return;
178 }
Damien George97790452014-04-08 11:04:29 +0000179 while (mp_repl_continue_with_input(line)) {
180 char *line2 = prompt("... ");
181 if (line2 == NULL) {
182 break;
Damien5ac1b2e2013-10-18 19:58:12 +0100183 }
Damien George97790452014-04-08 11:04:29 +0000184 char *line3 = strjoin(line, '\n', line2);
185 free(line);
186 free(line2);
187 line = line3;
Damien5ac1b2e2013-10-18 19:58:12 +0100188 }
Damienfa2162b2013-10-20 17:42:00 +0100189
Damien Georgeb829b5c2014-01-25 13:51:19 +0000190 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 +0000191 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200192 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100193 }
194}
195
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300196STATIC void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000197 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000198 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
199}
Damien429d7192013-10-04 19:53:11 +0100200
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300201STATIC void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000202 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
Damien George136f6752014-01-07 14:54:15 +0000203 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100204}
Damien429d7192013-10-04 19:53:11 +0100205
Damiend99b0522013-12-21 18:17:45 +0000206typedef struct _test_obj_t {
207 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000208 int value;
Damiend99b0522013-12-21 18:17:45 +0000209} test_obj_t;
210
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300211STATIC void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +0000212 test_obj_t *self = self_in;
213 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000214}
215
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300216STATIC mp_obj_t test_get(mp_obj_t self_in) {
Damiend99b0522013-12-21 18:17:45 +0000217 test_obj_t *self = self_in;
218 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000219}
220
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300221STATIC mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000222 test_obj_t *self = self_in;
223 self->value = mp_obj_get_int(arg);
224 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000225}
226
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300227STATIC MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
228STATIC MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
Damiend99b0522013-12-21 18:17:45 +0000229
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300230STATIC const mp_map_elem_t test_locals_dict_table[] = {
Damien George9b196cd2014-03-26 21:47:19 +0000231 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&test_get_obj },
232 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&test_set_obj },
Damien George97209d32014-01-07 15:58:30 +0000233};
234
Damien George9b196cd2014-03-26 21:47:19 +0000235STATIC MP_DEFINE_CONST_DICT(test_locals_dict, test_locals_dict_table);
236
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300237STATIC const mp_obj_type_t test_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000238 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000239 .name = MP_QSTR_Test,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200240 .print = test_print,
Damien George9b196cd2014-03-26 21:47:19 +0000241 .locals_dict = (mp_obj_t)&test_locals_dict,
Damiena53f6942013-11-02 23:58:38 +0000242};
243
Damiend99b0522013-12-21 18:17:45 +0000244mp_obj_t test_obj_new(int value) {
245 test_obj_t *o = m_new_obj(test_obj_t);
246 o->base.type = &test_type;
247 o->value = value;
248 return o;
249}
250
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300251int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000252 printf(
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300253"usage: %s [-X <opt>] [-c <command>] [<filename>]\n"
Damien George5e349092014-03-08 19:04:47 +0000254"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300255"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300256);
257 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100258 printf(
Damien Georgef22626e2014-04-10 11:30:35 +0100259" compile-only -- parse and compile only\n"
Damien George65cad122014-04-06 11:48:15 +0100260" emit={bytecode,native,viper} -- set the default code emitter\n"
261);
262 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300263#if MICROPY_ENABLE_GC
264 printf(
Damien George5e349092014-03-08 19:04:47 +0000265" heapsize=<n> -- set the heap size for the GC\n"
266);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300267 impl_opts_cnt++;
268#endif
269
270 if (impl_opts_cnt == 0) {
271 printf(" (none)\n");
272 }
273
Damien George136f6752014-01-07 14:54:15 +0000274 return 1;
275}
276
Damien George4d5b28c2014-01-29 18:56:46 +0000277mp_obj_t mem_info(void) {
278 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300279#if MICROPY_ENABLE_GC
Paul Sokolovsky59a2f482014-04-17 18:32:36 +0300280 gc_dump_info();
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300281#endif
Damien George4d5b28c2014-01-29 18:56:46 +0000282 return mp_const_none;
283}
284
285mp_obj_t qstr_info(void) {
286 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
287 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
288 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);
289 return mp_const_none;
290}
291
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200292#if MICROPY_ENABLE_GC
293// TODO: this doesn't belong here
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300294STATIC mp_obj_t pyb_gc(void) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200295 gc_collect();
296 return mp_const_none;
297}
298MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
299#endif
300
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200301// Process options which set interpreter init options
302void pre_process_options(int argc, char **argv) {
303 for (int a = 1; a < argc; a++) {
304 if (argv[a][0] == '-') {
305 if (strcmp(argv[a], "-X") == 0) {
306 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300307 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200308 }
Damien George5e349092014-03-08 19:04:47 +0000309 if (0) {
Damien Georgef22626e2014-04-10 11:30:35 +0100310 } else if (strcmp(argv[a + 1], "compile-only") == 0) {
311 compile_only = true;
Damien George65cad122014-04-06 11:48:15 +0100312 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
313 emit_opt = MP_EMIT_OPT_BYTE_CODE;
314 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
315 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
316 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
317 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200318#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000319 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200320 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200321#endif
Damien George5e349092014-03-08 19:04:47 +0000322 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300323 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000324 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200325 a++;
326 }
327 }
328 }
329}
330
Damien5ac1b2e2013-10-18 19:58:12 +0100331int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200332 volatile int stack_dummy;
333 stack_top = (void*)&stack_dummy;
334
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200335 pre_process_options(argc, argv);
336
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200337#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200338 char *heap = malloc(heap_size);
339 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200340#endif
341
Damien5ac1b2e2013-10-18 19:58:12 +0100342 qstr_init();
Damien Georged17926d2014-03-30 13:35:08 +0100343 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100344
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200345 char *home = getenv("HOME");
346 char *path = getenv("MICROPYPATH");
347 if (path == NULL) {
348 path = "~/.micropython/lib:/usr/lib/micropython";
349 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200350 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200351 for (char *p = path; p != NULL; p = strchr(p, ':')) {
352 path_num++;
353 if (p != NULL) {
354 p++;
355 }
356 }
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300357 mp_obj_list_init(mp_sys_path, path_num);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200358 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100359 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200360 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200361 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200362 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200363 char *p1 = strchr(p, ':');
364 if (p1 == NULL) {
365 p1 = p + strlen(p);
366 }
367 if (p[0] == '~' && p[1] == '/' && home != NULL) {
368 // Expand standalone ~ to $HOME
369 CHECKBUF(buf, PATH_MAX);
370 CHECKBUF_APPEND(buf, home, strlen(home));
371 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200372 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200373 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200374 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200375 }
376 p = p1 + 1;
377 }
378
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300379 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200380
Damien Georged17926d2014-03-30 13:35:08 +0100381 mp_store_name(qstr_from_str("test"), test_obj_new(42));
382 mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
383 mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200384#if MICROPY_ENABLE_GC
Damien Georged17926d2014-03-30 13:35:08 +0100385 mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200386#endif
Damien George12eacca2014-01-21 21:54:15 +0000387
Damien George062478e2014-01-09 20:57:50 +0000388 // Here is some example code to create a class and instance of that class.
389 // First is the Python, then the C code.
390 //
391 // class TestClass:
392 // pass
393 // test_obj = TestClass()
394 // test_obj.attr = 42
395 mp_obj_t test_class_type, test_class_instance;
Damien Georgea71c83a2014-02-15 11:34:50 +0000396 test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
Damien Georged17926d2014-03-30 13:35:08 +0100397 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
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200407 bool executed = false;
408 for (int a = 1; a < argc; a++) {
409 if (argv[a][0] == '-') {
410 if (strcmp(argv[a], "-c") == 0) {
411 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300412 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000413 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200414 do_str(argv[a + 1]);
415 executed = true;
416 a += 1;
417 } else if (strcmp(argv[a], "-X") == 0) {
418 a += 1;
Damien George136f6752014-01-07 14:54:15 +0000419 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300420 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000421 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200422 } else {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200423 char *basedir = realpath(argv[a], NULL);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300424 if (basedir == NULL) {
425 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno);
426 perror("");
427 // CPython exits with 2 in such case
428 exit(2);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200429 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300430
431 // Set base dir of the script as first entry in sys.path
432 char *p = strrchr(basedir, '/');
433 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
434 free(basedir);
435
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200436 for (int i = a; i < argc; i++) {
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300437 mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200438 }
439 do_file(argv[a]);
440 executed = true;
441 break;
Damien George136f6752014-01-07 14:54:15 +0000442 }
Damien5ac1b2e2013-10-18 19:58:12 +0100443 }
Damien George136f6752014-01-07 14:54:15 +0000444
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200445 if (!executed) {
446 do_repl();
447 }
448
Damien Georged17926d2014-03-30 13:35:08 +0100449 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100450
451 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
452 return 0;
453}
Damien087d2182013-11-09 20:14:30 +0000454
Damien Georgee09ffa12014-02-05 23:57:48 +0000455uint mp_import_stat(const char *path) {
456 struct stat st;
457 if (stat(path, &st) == 0) {
458 if (S_ISDIR(st.st_mode)) {
459 return MP_IMPORT_STAT_DIR;
460 } else if (S_ISREG(st.st_mode)) {
461 return MP_IMPORT_STAT_FILE;
462 }
463 }
464 return MP_IMPORT_STAT_NO_EXIST;
465}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200466
467int DEBUG_printf(const char *fmt, ...) {
468 va_list ap;
469 va_start(ap, fmt);
470 int ret = vfprintf(stderr, fmt, ap);
471 va_end(ap);
472 return ret;
473}
Damien George26cf55a2014-04-08 14:08:14 +0000474
475void nlr_jump_fail(void *val) {
476 printf("FATAL: uncaught NLR %p\n", val);
477 exit(1);
478}