pyexec.c 10 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
28
#include <stdlib.h>
#include <stdio.h>
Dave Hylands's avatar
Dave Hylands committed
29
#include <stdint.h>
30

31
#include "mpconfig.h"
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "nlr.h"
#include "misc.h"
#include "qstr.h"
#include "misc.h"
#include "lexer.h"
#include "parse.h"
#include "obj.h"
#include "parsehelper.h"
#include "compile.h"
#include "runtime.h"
#include "repl.h"
#include "gc.h"
#include "gccollect.h"
45
#include MICROPY_HAL_H
46
#include "systick.h"
47
#include "readline.h"
48
#include "pyexec.h"
49
#include "pybstdio.h"
50
#include "genhdr/py-version.h"
51

52
53
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
STATIC bool repl_display_debugging_info = 0;
54

55
56
57
58
#define EXEC_FLAG_PRINT_EOF (1)
#define EXEC_FLAG_ALLOW_DEBUGGING (2)
#define EXEC_FLAG_IS_REPL (4)

59
60
// parses, compiles and executes the code in the lexer
// frees the lexer before returning
61
62
63
64
65
66
// EXEC_FLAG_PRINT_EOF prints 2 EOF chars: 1 after normal output, 1 after exception output
// EXEC_FLAG_ALLOW_DEBUGGING allows debugging info to be printed after executing the code
// EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile)
STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, int exec_flags) {
    int ret = 0;

67
68
    mp_parse_error_kind_t parse_error_kind;
    mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
69
    qstr source_name = lex->source_name;
70

71
    // check for parse error
72
    if (pn == MP_PARSE_NODE_NULL) {
73
74
75
        if (exec_flags & EXEC_FLAG_PRINT_EOF) {
            stdout_tx_strn("\x04", 1);
        }
76
77
        mp_parse_show_exception(lex, parse_error_kind);
        mp_lexer_free(lex);
78
        goto finish;
79
80
81
82
    }

    mp_lexer_free(lex);

83
    mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL);
84

85
    // check for compile error
86
    if (mp_obj_is_exception_instance(module_fun)) {
87
88
89
        if (exec_flags & EXEC_FLAG_PRINT_EOF) {
            stdout_tx_strn("\x04", 1);
        }
90
        mp_obj_print_exception(module_fun);
91
        goto finish;
92
93
    }

94
    // execute code
95
96
97
    nlr_buf_t nlr;
    uint32_t start = HAL_GetTick();
    if (nlr_push(&nlr) == 0) {
98
        mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
Damien George's avatar
Damien George committed
99
        mp_call_function_0(module_fun);
100
        mp_hal_set_interrupt_char(-1); // disable interrupt
101
        nlr_pop();
102
        ret = 1;
103
104
105
        if (exec_flags & EXEC_FLAG_PRINT_EOF) {
            stdout_tx_strn("\x04", 1);
        }
106
107
108
    } else {
        // uncaught exception
        // FIXME it could be that an interrupt happens just before we disable it here
109
        mp_hal_set_interrupt_char(-1); // disable interrupt
110
111
112
113
        // print EOF after normal output
        if (exec_flags & EXEC_FLAG_PRINT_EOF) {
            stdout_tx_strn("\x04", 1);
        }
114
        // check for SystemExit
115
116
117
118
119
120
        if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_SystemExit)) {
            // at the moment, the value of SystemExit is unused
            ret = PYEXEC_FORCED_EXIT;
        } else {
            mp_obj_print_exception((mp_obj_t)nlr.ret_val);
            ret = 0;
121
        }
122
123
124
    }

    // display debugging info if wanted
125
    if ((exec_flags & EXEC_FLAG_ALLOW_DEBUGGING) && repl_display_debugging_info) {
126
127
        mp_uint_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly
        printf("took " UINT_FMT " ms\n", ticks);
128
129
130
        gc_collect();
        // qstr info
        {
131
            mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
132
            qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
133
            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);
134
135
136
137
138
139
140
        }

        // GC info
        {
            gc_info_t info;
            gc_info(&info);
            printf("GC:\n");
141
142
143
            printf("  " UINT_FMT " total\n", info.total);
            printf("  " UINT_FMT " : " UINT_FMT "\n", info.used, info.free);
            printf("  1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block);
144
145
146
        }
    }

147
148
149
150
151
finish:
    if (exec_flags & EXEC_FLAG_PRINT_EOF) {
        stdout_tx_strn("\x04", 1);
    }

152
153
154
    return ret;
}

155
int pyexec_raw_repl(void) {
156
157
158
159
    vstr_t line;
    vstr_init(&line, 32);

raw_repl_reset:
160
    stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
161
162
163
164
165
166

    for (;;) {
        vstr_reset(&line);
        stdout_tx_str(">");
        for (;;) {
            char c = stdin_rx_chr();
167
            if (c == CHAR_CTRL_A) {
168
                // reset raw REPL
169
                goto raw_repl_reset;
170
            } else if (c == CHAR_CTRL_B) {
171
172
173
174
175
                // change to friendly REPL
                stdout_tx_str("\r\n");
                vstr_clear(&line);
                pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
                return 0;
176
            } else if (c == CHAR_CTRL_C) {
177
                // clear line
178
                vstr_reset(&line);
179
            } else if (c == CHAR_CTRL_D) {
180
                // input finished
181
                break;
182
183
            } else if (c <= 127) {
                // let through any other ASCII character
184
185
186
187
                vstr_add_char(&line, c);
            }
        }

188
        // indicate reception of command
189
190
        stdout_tx_str("OK");

191
192
193
194
        if (line.len == 0) {
            // exit for a soft reset
            stdout_tx_str("\r\n");
            vstr_clear(&line);
195
            return PYEXEC_FORCED_EXIT;
196
197
        }

198
        mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, 0);
199
        if (lex == NULL) {
200
            printf("\x04MemoryError\n\x04");
201
        } else {
202
            int ret = parse_compile_execute(lex, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF);
203
204
205
            if (ret & PYEXEC_FORCED_EXIT) {
                return ret;
            }
206
        }
207
208
209
    }
}

210
211
212
213
int pyexec_friendly_repl(void) {
    vstr_t line;
    vstr_init(&line, 32);

214
215
#if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
    // in host mode, we enable the LCD for the repl
Damien George's avatar
Damien George committed
216
217
    mp_obj_t lcd_o = mp_call_function_0(mp_load_name(qstr_from_str("LCD")));
    mp_call_function_1(mp_load_attr(lcd_o, qstr_from_str("light")), mp_const_true);
218
219
#endif

220
friendly_repl_reset:
mux's avatar
mux committed
221
    stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
222
223
224
225
226
227
228
229
230
    stdout_tx_str("Type \"help()\" for more information.\r\n");

    // to test ctrl-C
    /*
    {
        uint32_t x[4] = {0x424242, 0xdeaddead, 0x242424, 0xdeadbeef};
        for (;;) {
            nlr_buf_t nlr;
            printf("pyexec_repl: %p\n", x);
231
            mp_hal_set_interrupt_char(CHAR_CTRL_C);
232
233
234
235
236
237
238
239
240
241
242
            if (nlr_push(&nlr) == 0) {
                for (;;) {
                }
            } else {
                printf("break\n");
            }
        }
    }
    */

    for (;;) {
243
    input_restart:
244
245
246
        vstr_reset(&line);
        int ret = readline(&line, ">>> ");

247
        if (ret == CHAR_CTRL_A) {
248
249
250
251
252
            // change to raw REPL
            stdout_tx_str("\r\n");
            vstr_clear(&line);
            pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
            return 0;
253
        } else if (ret == CHAR_CTRL_B) {
254
255
256
            // reset friendly REPL
            stdout_tx_str("\r\n");
            goto friendly_repl_reset;
257
        } else if (ret == CHAR_CTRL_C) {
258
            // break
259
260
            stdout_tx_str("\r\n");
            continue;
261
        } else if (ret == CHAR_CTRL_D) {
262
263
264
            // exit for a soft reset
            stdout_tx_str("\r\n");
            vstr_clear(&line);
265
            return PYEXEC_FORCED_EXIT;
266
267
268
269
        } else if (vstr_len(&line) == 0) {
            continue;
        }

270
271
272
        while (mp_repl_continue_with_input(vstr_str(&line))) {
            vstr_add_char(&line, '\n');
            int ret = readline(&line, "... ");
273
            if (ret == CHAR_CTRL_C) {
274
275
276
                // cancel everything
                stdout_tx_str("\r\n");
                goto input_restart;
277
            } else if (ret == CHAR_CTRL_D) {
278
279
                // stop entering compound statement
                break;
280
281
282
283
            }
        }

        mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0);
284
285
286
        if (lex == NULL) {
            printf("MemoryError\n");
        } else {
287
            int ret = parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL);
288
289
290
            if (ret & PYEXEC_FORCED_EXIT) {
                return ret;
            }
291
        }
292
293
294
    }
}

295
int pyexec_file(const char *filename) {
296
297
298
299
300
301
302
    mp_lexer_t *lex = mp_lexer_new_from_file(filename);

    if (lex == NULL) {
        printf("could not open file '%s' for reading\n", filename);
        return false;
    }

303
    return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0);
304
305
306
307
308
309
310
311
}

mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
    repl_display_debugging_info = mp_obj_get_int(o_value);
    return mp_const_none;
}

MP_DEFINE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj, pyb_set_repl_info);