blob: 8e6a76bbad34e4d1065d42207cbdb9d239c16206 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
Edd Barrett8146aea2014-01-01 23:14:36 +00004#include <stdlib.h>
Damien429d7192013-10-04 19:53:11 +01005
Damience89a212013-10-15 22:25:17 +01006#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01007#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010010#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010011#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000013#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000014#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000015#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000016#include "runtime0.h"
17#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010018#include "repl.h"
Paul Sokolovskye7db8172014-02-11 00:44:37 +020019#include "gc.h"
Damien429d7192013-10-04 19:53:11 +010020
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020021#if MICROPY_USE_READLINE
Damien5ac1b2e2013-10-18 19:58:12 +010022#include <readline/readline.h>
Paul Sokolovsky903b24f2014-01-01 14:54:39 +020023#include <readline/history.h>
Paul Sokolovskyfa027672014-01-01 18:28:01 +020024#endif
Damien429d7192013-10-04 19:53:11 +010025
Paul Sokolovskye7db8172014-02-11 00:44:37 +020026// Heap size of GC heap (if enabled)
27// TODO: allow to specify on command line
28#define HEAP_SIZE 128*1024
29
30// Stack top at the start of program
31void *stack_top;
32
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +020033void file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +020034void microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020035void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020036void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020037
Damien George136f6752014-01-07 14:54:15 +000038static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
39 if (lex == NULL) {
40 return;
41 }
42
43 if (0) {
44 // just tokenise
45 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
46 mp_token_show(mp_lexer_cur(lex));
47 mp_lexer_to_next(lex);
48 }
49 mp_lexer_free(lex);
50 return;
51 }
52
Damien Georgec5966122014-02-15 16:10:44 +000053 mp_parse_error_kind_t parse_error_kind;
54 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +000055
56 if (pn == MP_PARSE_NODE_NULL) {
57 // parse error
Damien Georgec5966122014-02-15 16:10:44 +000058 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +000059 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000060 return;
61 }
62
Damien George08335002014-01-18 23:24:36 +000063 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000064 mp_lexer_free(lex);
65
Damien Georgecbd2f742014-01-19 11:48:48 +000066 /*
67 printf("----------------\n");
68 mp_parse_node_print(pn, 0);
69 printf("----------------\n");
70 */
Damien George136f6752014-01-07 14:54:15 +000071
Damien George08335002014-01-18 23:24:36 +000072 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000073
74 if (module_fun == mp_const_none) {
75 // compile error
76 return;
77 }
78
79 // execute it
80 nlr_buf_t nlr;
81 if (nlr_push(&nlr) == 0) {
82 rt_call_function_0(module_fun);
83 nlr_pop();
84 } else {
85 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000086 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000087 }
88}
89
Paul Sokolovskya0757412014-02-11 12:19:06 +020090static char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010091 int l1 = strlen(s1);
92 int l2 = strlen(s2);
93 char *s = m_new(char, l1 + l2 + 2);
94 memcpy(s, s1, l1);
95 if (sep_char != 0) {
96 s[l1] = sep_char;
97 l1 += 1;
98 }
99 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100100 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100101 return s;
102}
103
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200104static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200105#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200106 char *line = readline(p);
107 if (line) {
108 add_history(line);
109 }
110#else
111 static char buf[256];
112 fputs(p, stdout);
113 char *s = fgets(buf, sizeof(buf), stdin);
114 if (!s) {
115 return NULL;
116 }
117 int l = strlen(buf);
118 if (buf[l - 1] == '\n') {
119 buf[l - 1] = 0;
120 } else {
121 l++;
122 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200123 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200124 memcpy(line, buf, l);
125#endif
126 return line;
127}
128
Damiend99b0522013-12-21 18:17:45 +0000129static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100130 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200131 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100132 if (line == NULL) {
133 // EOF
134 return;
135 }
Damiend99b0522013-12-21 18:17:45 +0000136 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100137 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200138 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100139 if (line2 == NULL || strlen(line2) == 0) {
140 break;
141 }
Paul Sokolovskya0757412014-02-11 12:19:06 +0200142 char *line3 = strjoin(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000143 free(line);
144 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100145 line = line3;
146 }
147 }
Damienfa2162b2013-10-20 17:42:00 +0100148
Damien Georgeb829b5c2014-01-25 13:51:19 +0000149 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 +0000150 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200151 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100152 }
153}
154
Damien George136f6752014-01-07 14:54:15 +0000155static void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000156 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000157 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
158}
Damien429d7192013-10-04 19:53:11 +0100159
Damien George136f6752014-01-07 14:54:15 +0000160static void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000161 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 +0000162 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100163}
Damien429d7192013-10-04 19:53:11 +0100164
Damiend99b0522013-12-21 18:17:45 +0000165typedef struct _test_obj_t {
166 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000167 int value;
Damiend99b0522013-12-21 18:17:45 +0000168} test_obj_t;
169
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200170static 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 +0000171 test_obj_t *self = self_in;
172 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000173}
174
Damiend99b0522013-12-21 18:17:45 +0000175static mp_obj_t test_get(mp_obj_t self_in) {
176 test_obj_t *self = self_in;
177 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000178}
179
Damiend99b0522013-12-21 18:17:45 +0000180static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
181 test_obj_t *self = self_in;
182 self->value = mp_obj_get_int(arg);
183 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000184}
185
Damiend99b0522013-12-21 18:17:45 +0000186static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
187static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
188
Damien George97209d32014-01-07 15:58:30 +0000189static const mp_method_t test_methods[] = {
190 { "get", &test_get_obj },
191 { "set", &test_set_obj },
192 { NULL, NULL },
193};
194
Damiend99b0522013-12-21 18:17:45 +0000195static const mp_obj_type_t test_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000196 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000197 .name = MP_QSTR_Test,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200198 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000199 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000200};
201
Damiend99b0522013-12-21 18:17:45 +0000202mp_obj_t test_obj_new(int value) {
203 test_obj_t *o = m_new_obj(test_obj_t);
204 o->base.type = &test_type;
205 o->value = value;
206 return o;
207}
208
Damien George136f6752014-01-07 14:54:15 +0000209int usage(void) {
210 printf("usage: py [-c <command>] [<filename>]\n");
211 return 1;
212}
213
Damien George4d5b28c2014-01-29 18:56:46 +0000214mp_obj_t mem_info(void) {
215 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
216 return mp_const_none;
217}
218
219mp_obj_t qstr_info(void) {
220 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
221 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
222 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);
223 return mp_const_none;
224}
225
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200226#if MICROPY_ENABLE_GC
227// TODO: this doesn't belong here
228static mp_obj_t pyb_gc(void) {
229 gc_collect();
230 return mp_const_none;
231}
232MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
233#endif
234
Damien5ac1b2e2013-10-18 19:58:12 +0100235int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200236 volatile int stack_dummy;
237 stack_top = (void*)&stack_dummy;
238
239#if MICROPY_ENABLE_GC
240 char *heap = malloc(HEAP_SIZE);
241 gc_init(heap, heap + HEAP_SIZE);
242#endif
243
Damien5ac1b2e2013-10-18 19:58:12 +0100244 qstr_init();
245 rt_init();
246
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200247 char *home = getenv("HOME");
248 char *path = getenv("MICROPYPATH");
249 if (path == NULL) {
250 path = "~/.micropython/lib:/usr/lib/micropython";
251 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200252 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200253 for (char *p = path; p != NULL; p = strchr(p, ':')) {
254 path_num++;
255 if (p != NULL) {
256 p++;
257 }
258 }
259 sys_path = mp_obj_new_list(path_num, NULL);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200260 mp_obj_t *path_items;
261 mp_obj_list_get(sys_path, &path_num, &path_items);
262 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200263 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200264 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200265 char *p1 = strchr(p, ':');
266 if (p1 == NULL) {
267 p1 = p + strlen(p);
268 }
269 if (p[0] == '~' && p[1] == '/' && home != NULL) {
270 // Expand standalone ~ to $HOME
271 CHECKBUF(buf, PATH_MAX);
272 CHECKBUF_APPEND(buf, home, strlen(home));
273 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200274 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200275 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200276 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200277 }
278 p = p1 + 1;
279 }
280
Damien George55baff42014-01-21 21:40:13 +0000281 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200282 rt_store_attr(m_sys, MP_QSTR_path, sys_path);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200283 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien George55baff42014-01-21 21:40:13 +0000284 rt_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200285
Damien George55baff42014-01-21 21:40:13 +0000286 rt_store_name(qstr_from_str("test"), test_obj_new(42));
Damien George4d5b28c2014-01-29 18:56:46 +0000287 rt_store_name(qstr_from_str("mem_info"), rt_make_function_n(0, mem_info));
288 rt_store_name(qstr_from_str("qstr_info"), rt_make_function_n(0, qstr_info));
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200289#if MICROPY_ENABLE_GC
290 rt_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
291#endif
Damien George12eacca2014-01-21 21:54:15 +0000292
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200293 file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +0200294 microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +0200295#if MICROPY_MOD_TIME
296 time_init();
297#endif
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200298#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200299 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200300#endif
Damiena53f6942013-11-02 23:58:38 +0000301
Damien George062478e2014-01-09 20:57:50 +0000302 // Here is some example code to create a class and instance of that class.
303 // First is the Python, then the C code.
304 //
305 // class TestClass:
306 // pass
307 // test_obj = TestClass()
308 // test_obj.attr = 42
309 mp_obj_t test_class_type, test_class_instance;
Damien Georgea71c83a2014-02-15 11:34:50 +0000310 test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
Damien George55baff42014-01-21 21:40:13 +0000311 rt_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
312 rt_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000313
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000314 /*
315 printf("bytes:\n");
316 printf(" total %d\n", m_get_total_bytes_allocated());
317 printf(" cur %d\n", m_get_current_bytes_allocated());
318 printf(" peak %d\n", m_get_peak_bytes_allocated());
319 */
320
Damien5ac1b2e2013-10-18 19:58:12 +0100321 if (argc == 1) {
322 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100323 } else {
Damien George136f6752014-01-07 14:54:15 +0000324 for (int a = 1; a < argc; a++) {
325 if (argv[a][0] == '-') {
326 if (strcmp(argv[a], "-c") == 0) {
327 if (a + 1 >= argc) {
328 return usage();
329 }
330 do_str(argv[a + 1]);
331 a += 1;
332 } else {
333 return usage();
334 }
335 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200336 // Set base dir of the script as first entry in sys.path
337 char *basedir = realpath(argv[a], NULL);
338 if (basedir != NULL) {
339 char *p = strrchr(basedir, '/');
340 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
341 free(basedir);
342 }
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200343 for (int i = a; i < argc; i++) {
Damien George55baff42014-01-21 21:40:13 +0000344 rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200345 }
Damien George136f6752014-01-07 14:54:15 +0000346 do_file(argv[a]);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200347 break;
Damien George136f6752014-01-07 14:54:15 +0000348 }
349 }
Damien5ac1b2e2013-10-18 19:58:12 +0100350 }
Damien George136f6752014-01-07 14:54:15 +0000351
Damien429d7192013-10-04 19:53:11 +0100352 rt_deinit();
353
354 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
355 return 0;
356}
Damien087d2182013-11-09 20:14:30 +0000357
358// for sqrt
359#include <math.h>
360machine_float_t machine_sqrt(machine_float_t x) {
361 return sqrt(x);
362}
Damien Georgee09ffa12014-02-05 23:57:48 +0000363
364#include <sys/stat.h>
365
366uint mp_import_stat(const char *path) {
367 struct stat st;
368 if (stat(path, &st) == 0) {
369 if (S_ISDIR(st.st_mode)) {
370 return MP_IMPORT_STAT_DIR;
371 } else if (S_ISREG(st.st_mode)) {
372 return MP_IMPORT_STAT_FILE;
373 }
374 }
375 return MP_IMPORT_STAT_NO_EXIST;
376}