blob: 716c7b11426ff6c31c1c05ba96eb765ae0d8d238 [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 George1fb03172014-01-03 14:22:03 +000014#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "runtime0.h"
16#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010017#include "repl.h"
Paul Sokolovskye7db8172014-02-11 00:44:37 +020018#include "gc.h"
Damien429d7192013-10-04 19:53:11 +010019
Paul Sokolovskyd674bd52014-01-04 19:38:19 +020020#if MICROPY_USE_READLINE
Damien5ac1b2e2013-10-18 19:58:12 +010021#include <readline/readline.h>
Paul Sokolovsky903b24f2014-01-01 14:54:39 +020022#include <readline/history.h>
Paul Sokolovskyfa027672014-01-01 18:28:01 +020023#endif
Damien429d7192013-10-04 19:53:11 +010024
Paul Sokolovskye7db8172014-02-11 00:44:37 +020025// Heap size of GC heap (if enabled)
26// TODO: allow to specify on command line
27#define HEAP_SIZE 128*1024
28
29// Stack top at the start of program
30void *stack_top;
31
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +020032void file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +020033void microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020034void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020035void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020036
Damien George136f6752014-01-07 14:54:15 +000037static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
38 if (lex == NULL) {
39 return;
40 }
41
42 if (0) {
43 // just tokenise
44 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
45 mp_token_show(mp_lexer_cur(lex));
46 mp_lexer_to_next(lex);
47 }
48 mp_lexer_free(lex);
49 return;
50 }
51
Damien George9528cd62014-01-15 21:23:31 +000052 qstr parse_exc_id;
53 const char *parse_exc_msg;
54 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_exc_id, &parse_exc_msg);
Damien George136f6752014-01-07 14:54:15 +000055
56 if (pn == MP_PARSE_NODE_NULL) {
57 // parse error
Damien George9528cd62014-01-15 21:23:31 +000058 mp_lexer_show_error_pythonic_prefix(lex);
59 printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
60 mp_lexer_free(lex);
Damien George136f6752014-01-07 14:54:15 +000061 return;
62 }
63
Damien George08335002014-01-18 23:24:36 +000064 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000065 mp_lexer_free(lex);
66
Damien Georgecbd2f742014-01-19 11:48:48 +000067 /*
68 printf("----------------\n");
69 mp_parse_node_print(pn, 0);
70 printf("----------------\n");
71 */
Damien George136f6752014-01-07 14:54:15 +000072
Damien George08335002014-01-18 23:24:36 +000073 mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
Damien George136f6752014-01-07 14:54:15 +000074
75 if (module_fun == mp_const_none) {
76 // compile error
77 return;
78 }
79
80 // execute it
81 nlr_buf_t nlr;
82 if (nlr_push(&nlr) == 0) {
83 rt_call_function_0(module_fun);
84 nlr_pop();
85 } else {
86 // uncaught exception
Damien George136b1492014-01-19 12:38:49 +000087 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +000088 }
89}
90
Paul Sokolovskya0757412014-02-11 12:19:06 +020091static char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +010092 int l1 = strlen(s1);
93 int l2 = strlen(s2);
94 char *s = m_new(char, l1 + l2 + 2);
95 memcpy(s, s1, l1);
96 if (sep_char != 0) {
97 s[l1] = sep_char;
98 l1 += 1;
99 }
100 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100101 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100102 return s;
103}
104
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200105static char *prompt(char *p) {
Paul Sokolovskyd674bd52014-01-04 19:38:19 +0200106#if MICROPY_USE_READLINE
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200107 char *line = readline(p);
108 if (line) {
109 add_history(line);
110 }
111#else
112 static char buf[256];
113 fputs(p, stdout);
114 char *s = fgets(buf, sizeof(buf), stdin);
115 if (!s) {
116 return NULL;
117 }
118 int l = strlen(buf);
119 if (buf[l - 1] == '\n') {
120 buf[l - 1] = 0;
121 } else {
122 l++;
123 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200124 char *line = malloc(l);
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200125 memcpy(line, buf, l);
126#endif
127 return line;
128}
129
Damiend99b0522013-12-21 18:17:45 +0000130static void do_repl(void) {
Damien5ac1b2e2013-10-18 19:58:12 +0100131 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200132 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100133 if (line == NULL) {
134 // EOF
135 return;
136 }
Damiend99b0522013-12-21 18:17:45 +0000137 if (mp_repl_is_compound_stmt(line)) {
Damien5ac1b2e2013-10-18 19:58:12 +0100138 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200139 char *line2 = prompt("... ");
Damien5ac1b2e2013-10-18 19:58:12 +0100140 if (line2 == NULL || strlen(line2) == 0) {
141 break;
142 }
Paul Sokolovskya0757412014-02-11 12:19:06 +0200143 char *line3 = strjoin(line, '\n', line2);
Damien732407f2013-12-29 19:33:23 +0000144 free(line);
145 free(line2);
Damien5ac1b2e2013-10-18 19:58:12 +0100146 line = line3;
147 }
148 }
Damienfa2162b2013-10-20 17:42:00 +0100149
Damien Georgeb829b5c2014-01-25 13:51:19 +0000150 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 +0000151 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200152 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100153 }
154}
155
Damien George136f6752014-01-07 14:54:15 +0000156static void do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000157 mp_lexer_t *lex = mp_lexer_new_from_file(file);
Damien George136f6752014-01-07 14:54:15 +0000158 execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
159}
Damien429d7192013-10-04 19:53:11 +0100160
Damien George136f6752014-01-07 14:54:15 +0000161static void do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000162 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 +0000163 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100164}
Damien429d7192013-10-04 19:53:11 +0100165
Damiend99b0522013-12-21 18:17:45 +0000166typedef struct _test_obj_t {
167 mp_obj_base_t base;
Damien George97209d32014-01-07 15:58:30 +0000168 int value;
Damiend99b0522013-12-21 18:17:45 +0000169} test_obj_t;
170
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200171static 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 +0000172 test_obj_t *self = self_in;
173 print(env, "<test %d>", self->value);
Damiena53f6942013-11-02 23:58:38 +0000174}
175
Damiend99b0522013-12-21 18:17:45 +0000176static mp_obj_t test_get(mp_obj_t self_in) {
177 test_obj_t *self = self_in;
178 return mp_obj_new_int(self->value);
Damiena53f6942013-11-02 23:58:38 +0000179}
180
Damiend99b0522013-12-21 18:17:45 +0000181static mp_obj_t test_set(mp_obj_t self_in, mp_obj_t arg) {
182 test_obj_t *self = self_in;
183 self->value = mp_obj_get_int(arg);
184 return mp_const_none;
Damiena53f6942013-11-02 23:58:38 +0000185}
186
Damiend99b0522013-12-21 18:17:45 +0000187static MP_DEFINE_CONST_FUN_OBJ_1(test_get_obj, test_get);
188static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
189
Damien George97209d32014-01-07 15:58:30 +0000190static const mp_method_t test_methods[] = {
191 { "get", &test_get_obj },
192 { "set", &test_set_obj },
193 { NULL, NULL },
194};
195
Damiend99b0522013-12-21 18:17:45 +0000196static const mp_obj_type_t test_type = {
197 { &mp_const_type },
Damiena53f6942013-11-02 23:58:38 +0000198 "Test",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200199 .print = test_print,
Damien George97209d32014-01-07 15:58:30 +0000200 .methods = test_methods,
Damiena53f6942013-11-02 23:58:38 +0000201};
202
Damiend99b0522013-12-21 18:17:45 +0000203mp_obj_t test_obj_new(int value) {
204 test_obj_t *o = m_new_obj(test_obj_t);
205 o->base.type = &test_type;
206 o->value = value;
207 return o;
208}
209
Damien George136f6752014-01-07 14:54:15 +0000210int usage(void) {
211 printf("usage: py [-c <command>] [<filename>]\n");
212 return 1;
213}
214
Damien George4d5b28c2014-01-29 18:56:46 +0000215mp_obj_t mem_info(void) {
216 printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
217 return mp_const_none;
218}
219
220mp_obj_t qstr_info(void) {
221 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
222 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
223 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);
224 return mp_const_none;
225}
226
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200227#if MICROPY_ENABLE_GC
228// TODO: this doesn't belong here
229static mp_obj_t pyb_gc(void) {
230 gc_collect();
231 return mp_const_none;
232}
233MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
234#endif
235
Damien5ac1b2e2013-10-18 19:58:12 +0100236int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200237 volatile int stack_dummy;
238 stack_top = (void*)&stack_dummy;
239
240#if MICROPY_ENABLE_GC
241 char *heap = malloc(HEAP_SIZE);
242 gc_init(heap, heap + HEAP_SIZE);
243#endif
244
Damien5ac1b2e2013-10-18 19:58:12 +0100245 qstr_init();
246 rt_init();
247
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200248 char *home = getenv("HOME");
249 char *path = getenv("MICROPYPATH");
250 if (path == NULL) {
251 path = "~/.micropython/lib:/usr/lib/micropython";
252 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200253 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200254 for (char *p = path; p != NULL; p = strchr(p, ':')) {
255 path_num++;
256 if (p != NULL) {
257 p++;
258 }
259 }
260 sys_path = mp_obj_new_list(path_num, NULL);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200261 mp_obj_t *path_items;
262 mp_obj_list_get(sys_path, &path_num, &path_items);
263 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200264 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200265 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200266 char *p1 = strchr(p, ':');
267 if (p1 == NULL) {
268 p1 = p + strlen(p);
269 }
270 if (p[0] == '~' && p[1] == '/' && home != NULL) {
271 // Expand standalone ~ to $HOME
272 CHECKBUF(buf, PATH_MAX);
273 CHECKBUF_APPEND(buf, home, strlen(home));
274 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200275 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200276 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200277 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200278 }
279 p = p1 + 1;
280 }
281
Damien George55baff42014-01-21 21:40:13 +0000282 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200283 rt_store_attr(m_sys, MP_QSTR_path, sys_path);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200284 mp_obj_t py_argv = mp_obj_new_list(0, NULL);
Damien George55baff42014-01-21 21:40:13 +0000285 rt_store_attr(m_sys, MP_QSTR_argv, py_argv);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200286
Damien George55baff42014-01-21 21:40:13 +0000287 rt_store_name(qstr_from_str("test"), test_obj_new(42));
Damien George4d5b28c2014-01-29 18:56:46 +0000288 rt_store_name(qstr_from_str("mem_info"), rt_make_function_n(0, mem_info));
289 rt_store_name(qstr_from_str("qstr_info"), rt_make_function_n(0, qstr_info));
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200290#if MICROPY_ENABLE_GC
291 rt_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
292#endif
Damien George12eacca2014-01-21 21:54:15 +0000293
Paul Sokolovsky51ee44a2014-01-20 23:49:56 +0200294 file_init();
Paul Sokolovsky9945f332014-02-08 21:10:18 +0200295 microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +0200296#if MICROPY_MOD_TIME
297 time_init();
298#endif
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200299#if MICROPY_MOD_FFI
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +0200300 ffi_init();
Paul Sokolovskyed1239f2014-02-01 20:06:55 +0200301#endif
Damiena53f6942013-11-02 23:58:38 +0000302
Damien George062478e2014-01-09 20:57:50 +0000303 // Here is some example code to create a class and instance of that class.
304 // First is the Python, then the C code.
305 //
306 // class TestClass:
307 // pass
308 // test_obj = TestClass()
309 // test_obj.attr = 42
310 mp_obj_t test_class_type, test_class_instance;
Damien George5fa93b62014-01-22 14:35:10 +0000311 test_class_type = mp_obj_new_type("TestClass", mp_const_empty_tuple, mp_obj_new_dict(0));
Damien George55baff42014-01-21 21:40:13 +0000312 rt_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
313 rt_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000314
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000315 /*
316 printf("bytes:\n");
317 printf(" total %d\n", m_get_total_bytes_allocated());
318 printf(" cur %d\n", m_get_current_bytes_allocated());
319 printf(" peak %d\n", m_get_peak_bytes_allocated());
320 */
321
Damien5ac1b2e2013-10-18 19:58:12 +0100322 if (argc == 1) {
323 do_repl();
Damien5ac1b2e2013-10-18 19:58:12 +0100324 } else {
Damien George136f6752014-01-07 14:54:15 +0000325 for (int a = 1; a < argc; a++) {
326 if (argv[a][0] == '-') {
327 if (strcmp(argv[a], "-c") == 0) {
328 if (a + 1 >= argc) {
329 return usage();
330 }
331 do_str(argv[a + 1]);
332 a += 1;
333 } else {
334 return usage();
335 }
336 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200337 // Set base dir of the script as first entry in sys.path
338 char *basedir = realpath(argv[a], NULL);
339 if (basedir != NULL) {
340 char *p = strrchr(basedir, '/');
341 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
342 free(basedir);
343 }
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200344 for (int i = a; i < argc; i++) {
Damien George55baff42014-01-21 21:40:13 +0000345 rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200346 }
Damien George136f6752014-01-07 14:54:15 +0000347 do_file(argv[a]);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200348 break;
Damien George136f6752014-01-07 14:54:15 +0000349 }
350 }
Damien5ac1b2e2013-10-18 19:58:12 +0100351 }
Damien George136f6752014-01-07 14:54:15 +0000352
Damien429d7192013-10-04 19:53:11 +0100353 rt_deinit();
354
355 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
356 return 0;
357}
Damien087d2182013-11-09 20:14:30 +0000358
359// for sqrt
360#include <math.h>
361machine_float_t machine_sqrt(machine_float_t x) {
362 return sqrt(x);
363}
Damien Georgee09ffa12014-02-05 23:57:48 +0000364
365#include <sys/stat.h>
366
367uint mp_import_stat(const char *path) {
368 struct stat st;
369 if (stat(path, &st) == 0) {
370 if (S_ISDIR(st.st_mode)) {
371 return MP_IMPORT_STAT_DIR;
372 } else if (S_ISREG(st.st_mode)) {
373 return MP_IMPORT_STAT_FILE;
374 }
375 }
376 return MP_IMPORT_STAT_NO_EXIST;
377}