blob: f4b8c10fd60836f38ce4809419816a9e0bdeeef2 [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
Dave Hylandsf14b92b2014-03-12 18:06:26 -070027#include <stdlib.h>
Dave Hylandsf14b92b2014-03-12 18:06:26 -070028#include <stdio.h>
Dave Hylands4f1b7fe2014-06-15 22:33:14 -070029#include <stdint.h>
Dave Hylandsf14b92b2014-03-12 18:06:26 -070030
Damien George2cf6dfa2015-01-01 21:06:20 +000031#include "py/nlr.h"
32#include "py/parsehelper.h"
33#include "py/compile.h"
34#include "py/runtime.h"
35#include "py/repl.h"
36#include "py/gc.h"
37#include "py/pfenv.h"
Paul Sokolovskybf195862014-12-20 20:45:01 +020038#ifdef MICROPY_HAL_H
Damien George7a37f642014-07-02 13:42:37 +010039#include MICROPY_HAL_H
Paul Sokolovskybf195862014-12-20 20:45:01 +020040#endif
Dave Hylandsf14b92b2014-03-12 18:06:26 -070041#include "systick.h"
Damien Georgec9fd6642014-03-29 14:20:05 +000042#include "readline.h"
Dave Hylandsf14b92b2014-03-12 18:06:26 -070043#include "pyexec.h"
Damien George951ed9d2014-07-20 13:57:43 +010044#include "pybstdio.h"
Damien Georged553be52014-04-17 18:03:27 +010045#include "genhdr/py-version.h"
Dave Hylandsf14b92b2014-03-12 18:06:26 -070046
Damien Georgef704e7f2014-03-24 12:23:37 +000047pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
48STATIC bool repl_display_debugging_info = 0;
Dave Hylandsf14b92b2014-03-12 18:06:26 -070049
Damien Georgebc1488a2014-10-26 15:39:22 +000050#define EXEC_FLAG_PRINT_EOF (1)
51#define EXEC_FLAG_ALLOW_DEBUGGING (2)
52#define EXEC_FLAG_IS_REPL (4)
53
Dave Hylandsf14b92b2014-03-12 18:06:26 -070054// parses, compiles and executes the code in the lexer
55// frees the lexer before returning
Damien Georgebc1488a2014-10-26 15:39:22 +000056// EXEC_FLAG_PRINT_EOF prints 2 EOF chars: 1 after normal output, 1 after exception output
57// EXEC_FLAG_ALLOW_DEBUGGING allows debugging info to be printed after executing the code
58// EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile)
59STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, int exec_flags) {
60 int ret = 0;
61
Dave Hylandsf14b92b2014-03-12 18:06:26 -070062 mp_parse_error_kind_t parse_error_kind;
63 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien Georgea4c52c52014-12-05 19:35:18 +000064 qstr source_name = lex->source_name;
Dave Hylandsf14b92b2014-03-12 18:06:26 -070065
Damien Georgebc1488a2014-10-26 15:39:22 +000066 // check for parse error
Dave Hylandsf14b92b2014-03-12 18:06:26 -070067 if (pn == MP_PARSE_NODE_NULL) {
Damien Georgebc1488a2014-10-26 15:39:22 +000068 if (exec_flags & EXEC_FLAG_PRINT_EOF) {
69 stdout_tx_strn("\x04", 1);
70 }
Dave Hylandsf14b92b2014-03-12 18:06:26 -070071 mp_parse_show_exception(lex, parse_error_kind);
72 mp_lexer_free(lex);
Damien Georgebc1488a2014-10-26 15:39:22 +000073 goto finish;
Dave Hylandsf14b92b2014-03-12 18:06:26 -070074 }
75
76 mp_lexer_free(lex);
77
Damien Georgebc1488a2014-10-26 15:39:22 +000078 mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL);
Dave Hylandsf14b92b2014-03-12 18:06:26 -070079
Damien Georgebc1488a2014-10-26 15:39:22 +000080 // check for compile error
Damien Georgea91ac202014-10-05 19:01:34 +010081 if (mp_obj_is_exception_instance(module_fun)) {
Damien Georgebc1488a2014-10-26 15:39:22 +000082 if (exec_flags & EXEC_FLAG_PRINT_EOF) {
83 stdout_tx_strn("\x04", 1);
84 }
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +020085 mp_obj_print_exception(printf_wrapper, NULL, module_fun);
Damien Georgebc1488a2014-10-26 15:39:22 +000086 goto finish;
Dave Hylandsf14b92b2014-03-12 18:06:26 -070087 }
88
Damien Georgebc1488a2014-10-26 15:39:22 +000089 // execute code
Dave Hylandsf14b92b2014-03-12 18:06:26 -070090 nlr_buf_t nlr;
Dave Hylandsf14b92b2014-03-12 18:06:26 -070091 uint32_t start = HAL_GetTick();
92 if (nlr_push(&nlr) == 0) {
Damien George5cbc9e02014-11-27 16:58:31 +000093 mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
Damien Georged17926d2014-03-30 13:35:08 +010094 mp_call_function_0(module_fun);
Damien George5cbc9e02014-11-27 16:58:31 +000095 mp_hal_set_interrupt_char(-1); // disable interrupt
Dave Hylandsf14b92b2014-03-12 18:06:26 -070096 nlr_pop();
Dave Hylands8d62bbd2014-10-21 23:04:38 -070097 ret = 1;
Damien Georgebc1488a2014-10-26 15:39:22 +000098 if (exec_flags & EXEC_FLAG_PRINT_EOF) {
99 stdout_tx_strn("\x04", 1);
100 }
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700101 } else {
102 // uncaught exception
103 // FIXME it could be that an interrupt happens just before we disable it here
Damien George5cbc9e02014-11-27 16:58:31 +0000104 mp_hal_set_interrupt_char(-1); // disable interrupt
Damien Georgebc1488a2014-10-26 15:39:22 +0000105 // print EOF after normal output
106 if (exec_flags & EXEC_FLAG_PRINT_EOF) {
107 stdout_tx_strn("\x04", 1);
108 }
Dave Hylands8d62bbd2014-10-21 23:04:38 -0700109 // check for SystemExit
Damien George3be69842014-10-22 19:13:28 +0100110 if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_SystemExit)) {
111 // at the moment, the value of SystemExit is unused
112 ret = PYEXEC_FORCED_EXIT;
113 } else {
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200114 mp_obj_print_exception(printf_wrapper, NULL, (mp_obj_t)nlr.ret_val);
Damien George3be69842014-10-22 19:13:28 +0100115 ret = 0;
Dave Hylands8d62bbd2014-10-21 23:04:38 -0700116 }
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700117 }
118
119 // display debugging info if wanted
Damien Georgebc1488a2014-10-26 15:39:22 +0000120 if ((exec_flags & EXEC_FLAG_ALLOW_DEBUGGING) && repl_display_debugging_info) {
Damien George5cbc9e02014-11-27 16:58:31 +0000121 mp_uint_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly
122 printf("took " UINT_FMT " ms\n", ticks);
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700123 gc_collect();
124 // qstr info
125 {
Damien Georged03c6812014-10-05 21:51:54 +0100126 mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700127 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
Damien Georged03c6812014-10-05 21:51:54 +0100128 printf("qstr:\n n_pool=" UINT_FMT "\n n_qstr=" UINT_FMT "\n n_str_data_bytes=" UINT_FMT "\n n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700129 }
130
131 // GC info
Paul Sokolovsky6aaccc42014-12-21 00:24:58 +0200132 gc_dump_info();
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700133 }
134
Damien Georgebc1488a2014-10-26 15:39:22 +0000135finish:
136 if (exec_flags & EXEC_FLAG_PRINT_EOF) {
137 stdout_tx_strn("\x04", 1);
138 }
139
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700140 return ret;
141}
142
Damien Georgef704e7f2014-03-24 12:23:37 +0000143int pyexec_raw_repl(void) {
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700144 vstr_t line;
145 vstr_init(&line, 32);
146
147raw_repl_reset:
Damien Georgef704e7f2014-03-24 12:23:37 +0000148 stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700149
150 for (;;) {
151 vstr_reset(&line);
152 stdout_tx_str(">");
153 for (;;) {
154 char c = stdin_rx_chr();
Damien George5cbc9e02014-11-27 16:58:31 +0000155 if (c == CHAR_CTRL_A) {
Damien Georgef704e7f2014-03-24 12:23:37 +0000156 // reset raw REPL
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700157 goto raw_repl_reset;
Damien George5cbc9e02014-11-27 16:58:31 +0000158 } else if (c == CHAR_CTRL_B) {
Damien Georgef704e7f2014-03-24 12:23:37 +0000159 // change to friendly REPL
160 stdout_tx_str("\r\n");
161 vstr_clear(&line);
162 pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
163 return 0;
Damien George5cbc9e02014-11-27 16:58:31 +0000164 } else if (c == CHAR_CTRL_C) {
Damien Georgef704e7f2014-03-24 12:23:37 +0000165 // clear line
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700166 vstr_reset(&line);
Damien George5cbc9e02014-11-27 16:58:31 +0000167 } else if (c == CHAR_CTRL_D) {
Damien Georgef704e7f2014-03-24 12:23:37 +0000168 // input finished
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700169 break;
Damien Georged8b47d32014-04-03 22:11:43 +0100170 } else if (c <= 127) {
171 // let through any other ASCII character
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700172 vstr_add_char(&line, c);
173 }
174 }
175
Damien Georgef704e7f2014-03-24 12:23:37 +0000176 // indicate reception of command
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700177 stdout_tx_str("OK");
178
Damien Georgef704e7f2014-03-24 12:23:37 +0000179 if (line.len == 0) {
180 // exit for a soft reset
181 stdout_tx_str("\r\n");
182 vstr_clear(&line);
Dave Hylands8d62bbd2014-10-21 23:04:38 -0700183 return PYEXEC_FORCED_EXIT;
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700184 }
185
Damien Georgef704e7f2014-03-24 12:23:37 +0000186 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, 0);
Damien Georgeb0edec62014-05-10 17:48:46 +0100187 if (lex == NULL) {
Damien Georgebc1488a2014-10-26 15:39:22 +0000188 printf("\x04MemoryError\n\x04");
Damien Georgeb0edec62014-05-10 17:48:46 +0100189 } else {
Damien Georgebc1488a2014-10-26 15:39:22 +0000190 int ret = parse_compile_execute(lex, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF);
Dave Hylands8d62bbd2014-10-21 23:04:38 -0700191 if (ret & PYEXEC_FORCED_EXIT) {
192 return ret;
193 }
Damien Georgeb0edec62014-05-10 17:48:46 +0100194 }
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700195 }
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700196}
197
Paul Sokolovsky87bc8e22015-01-15 10:46:27 +0200198#if MICROPY_REPL_EVENT_DRIVEN
199
200typedef struct _friendly_repl_t {
201 vstr_t line;
202 bool cont_line;
203} friendly_repl_t;
204
205friendly_repl_t repl;
206
207void pyexec_friendly_repl_init(void) {
208 vstr_init(&repl.line, 32);
209 repl.cont_line = false;
210 readline_init(&repl.line);
211 stdout_tx_str(">>> ");
212}
213
214void pyexec_friendly_repl_reset() {
215 repl.cont_line = false;
216 vstr_reset(&repl.line);
217 readline_init(&repl.line);
218}
219
220int pyexec_friendly_repl_process_char(int c) {
221 int ret = readline_process_char(c);
222
223 if (!repl.cont_line) {
224
225 if (ret == CHAR_CTRL_A) {
226 // change to raw REPL
227 pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
228 stdout_tx_str("\r\n");
229 vstr_clear(&repl.line);
230 return PYEXEC_SWITCH_MODE;
231 } else if (ret == CHAR_CTRL_B) {
232 // reset friendly REPL
233 stdout_tx_str("\r\n");
234 goto friendly_repl_reset;
235 } else if (ret == CHAR_CTRL_C) {
236 // break
237 stdout_tx_str("\r\n");
238 goto input_restart;
239 } else if (ret == CHAR_CTRL_D) {
240 // exit for a soft reset
241 stdout_tx_str("\r\n");
242 vstr_clear(&repl.line);
243 return PYEXEC_FORCED_EXIT;
244 } else if (vstr_len(&repl.line) == 0) {
245 //goto input_restart;
246 }
247
248 if (ret < 0) {
249 return 0;
250 }
251
252 if (!mp_repl_continue_with_input(vstr_str(&repl.line))) {
253 goto exec;
254 }
255
256 vstr_add_char(&repl.line, '\n');
257 repl.cont_line = true;
258 stdout_tx_str("... ");
259 readline_note_newline();
260 return 0;
261
262 } else {
263
264 if (ret == CHAR_CTRL_C) {
265 // cancel everything
266 stdout_tx_str("\r\n");
267 repl.cont_line = false;
268 goto input_restart;
269 } else if (ret == CHAR_CTRL_D) {
270 // stop entering compound statement
271 goto exec;
272 }
273
274 if (ret < 0) {
275 return 0;
276 }
277
278 if (mp_repl_continue_with_input(vstr_str(&repl.line))) {
279 vstr_add_char(&repl.line, '\n');
280 stdout_tx_str("... ");
281 readline_note_newline();
282 return 0;
283 }
284
285exec: ;
286 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&repl.line), vstr_len(&repl.line), 0);
287 if (lex == NULL) {
288 printf("MemoryError\n");
289 } else {
290 int ret = parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL);
291 if (ret & PYEXEC_FORCED_EXIT) {
292 return ret;
293 }
294 }
295
296friendly_repl_reset: // TODO
297input_restart:
298 pyexec_friendly_repl_reset();
299 stdout_tx_str(">>> ");
300 return 0;
301 }
302}
303
304#else //MICROPY_REPL_EVENT_DRIVEN
305
Damien Georgef704e7f2014-03-24 12:23:37 +0000306int pyexec_friendly_repl(void) {
307 vstr_t line;
308 vstr_init(&line, 32);
309
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700310#if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
311 // in host mode, we enable the LCD for the repl
Damien Georged17926d2014-03-30 13:35:08 +0100312 mp_obj_t lcd_o = mp_call_function_0(mp_load_name(qstr_from_str("LCD")));
313 mp_call_function_1(mp_load_attr(lcd_o, qstr_from_str("light")), mp_const_true);
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700314#endif
315
Damien Georgef704e7f2014-03-24 12:23:37 +0000316friendly_repl_reset:
muxa75e3822014-06-27 00:35:53 +0200317 stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700318 stdout_tx_str("Type \"help()\" for more information.\r\n");
319
320 // to test ctrl-C
321 /*
322 {
323 uint32_t x[4] = {0x424242, 0xdeaddead, 0x242424, 0xdeadbeef};
324 for (;;) {
325 nlr_buf_t nlr;
326 printf("pyexec_repl: %p\n", x);
Damien George5cbc9e02014-11-27 16:58:31 +0000327 mp_hal_set_interrupt_char(CHAR_CTRL_C);
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700328 if (nlr_push(&nlr) == 0) {
329 for (;;) {
330 }
331 } else {
332 printf("break\n");
333 }
334 }
335 }
336 */
337
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700338 for (;;) {
Damien Georgebc1488a2014-10-26 15:39:22 +0000339 input_restart:
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700340 vstr_reset(&line);
341 int ret = readline(&line, ">>> ");
342
Damien George5cbc9e02014-11-27 16:58:31 +0000343 if (ret == CHAR_CTRL_A) {
Damien Georgef704e7f2014-03-24 12:23:37 +0000344 // change to raw REPL
345 stdout_tx_str("\r\n");
346 vstr_clear(&line);
347 pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
348 return 0;
Damien George5cbc9e02014-11-27 16:58:31 +0000349 } else if (ret == CHAR_CTRL_B) {
Damien Georgef704e7f2014-03-24 12:23:37 +0000350 // reset friendly REPL
351 stdout_tx_str("\r\n");
352 goto friendly_repl_reset;
Damien George5cbc9e02014-11-27 16:58:31 +0000353 } else if (ret == CHAR_CTRL_C) {
Damien Georgef704e7f2014-03-24 12:23:37 +0000354 // break
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700355 stdout_tx_str("\r\n");
356 continue;
Damien George5cbc9e02014-11-27 16:58:31 +0000357 } else if (ret == CHAR_CTRL_D) {
Damien Georgef704e7f2014-03-24 12:23:37 +0000358 // exit for a soft reset
359 stdout_tx_str("\r\n");
360 vstr_clear(&line);
Dave Hylands8d62bbd2014-10-21 23:04:38 -0700361 return PYEXEC_FORCED_EXIT;
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700362 } else if (vstr_len(&line) == 0) {
363 continue;
364 }
365
Damien George97790452014-04-08 11:04:29 +0000366 while (mp_repl_continue_with_input(vstr_str(&line))) {
367 vstr_add_char(&line, '\n');
368 int ret = readline(&line, "... ");
Damien George5cbc9e02014-11-27 16:58:31 +0000369 if (ret == CHAR_CTRL_C) {
Damien Georgebc1488a2014-10-26 15:39:22 +0000370 // cancel everything
371 stdout_tx_str("\r\n");
372 goto input_restart;
Damien George5cbc9e02014-11-27 16:58:31 +0000373 } else if (ret == CHAR_CTRL_D) {
Damien George97790452014-04-08 11:04:29 +0000374 // stop entering compound statement
375 break;
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700376 }
377 }
378
379 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0);
Damien Georgeb0edec62014-05-10 17:48:46 +0100380 if (lex == NULL) {
381 printf("MemoryError\n");
382 } else {
Damien Georgebc1488a2014-10-26 15:39:22 +0000383 int ret = parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL);
Dave Hylands8d62bbd2014-10-21 23:04:38 -0700384 if (ret & PYEXEC_FORCED_EXIT) {
385 return ret;
386 }
Damien Georgeb0edec62014-05-10 17:48:46 +0100387 }
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700388 }
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700389}
390
Paul Sokolovsky87bc8e22015-01-15 10:46:27 +0200391#endif //MICROPY_REPL_EVENT_DRIVEN
392
Damien George3be69842014-10-22 19:13:28 +0100393int pyexec_file(const char *filename) {
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700394 mp_lexer_t *lex = mp_lexer_new_from_file(filename);
395
396 if (lex == NULL) {
397 printf("could not open file '%s' for reading\n", filename);
398 return false;
399 }
400
Damien Georgebc1488a2014-10-26 15:39:22 +0000401 return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0);
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700402}
403
404mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
405 repl_display_debugging_info = mp_obj_get_int(o_value);
406 return mp_const_none;
407}
408
409MP_DEFINE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj, pyb_set_repl_info);