|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2023 Jeff Epler for Adafruit Industries |
| 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 | +#include <string.h> |
| 27 | + |
| 28 | +#include "py/builtin.h" |
| 29 | +#include "py/obj.h" |
| 30 | +#include "py/runtime.h" |
| 31 | +#include "py/repl.h" |
| 32 | + |
| 33 | +STATIC const char *get_arg_str(mp_obj_t arg, qstr name) { |
| 34 | + return mp_obj_str_get_str(mp_arg_validate_type_string(arg, name)); |
| 35 | +} |
| 36 | + |
| 37 | +//| """Utilities to compile possibly incomplete Python source code.""" |
| 38 | +//| |
| 39 | + |
| 40 | +//| def compile_command(source: str, filename: str = "<input>", symbol: str = "single"): |
| 41 | +//| """Compile a command and determine whether it is incomplete |
| 42 | +//| |
| 43 | +//| The 'completeness' determination is slightly different than in standard Python |
| 44 | +//| (it's whatever the internal function ``mp_repl_continue_with_input`` does). |
| 45 | +//| In particular, it's important that the code not end with a newline character |
| 46 | +//| or it is likely to be treated as a complete command.""" |
| 47 | +//| |
| 48 | +STATIC mp_obj_t compile_command(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 49 | + enum { ARG_source, ARG_filename, ARG_symbol }; |
| 50 | + static const mp_arg_t allowed_args[] = { |
| 51 | + { MP_QSTR_source, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = mp_const_none } }, |
| 52 | + { MP_QSTR_filename, MP_ARG_OBJ, { .u_obj = MP_ROM_QSTR(MP_QSTR__lt_input_gt_) } }, |
| 53 | + { MP_QSTR_symbol, MP_ARG_OBJ, { .u_obj = MP_ROM_QSTR(MP_QSTR_single) } }, |
| 54 | + }; |
| 55 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 56 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 57 | + |
| 58 | + const char *source = get_arg_str(args[ARG_source].u_obj, MP_QSTR_source); |
| 59 | + if (mp_repl_continue_with_input(source)) { |
| 60 | + return mp_const_none; |
| 61 | + } |
| 62 | + |
| 63 | + return mp_call_function_n_kw((mp_obj_t)&mp_builtin_compile_obj, 3, 0, &args[0].u_obj); |
| 64 | +} |
| 65 | +MP_DEFINE_CONST_FUN_OBJ_KW(compile_command_obj, 1, compile_command); |
| 66 | + |
| 67 | +STATIC const mp_rom_map_elem_t codeop_module_globals_table[] = { |
| 68 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_codeop) }, |
| 69 | + { MP_ROM_QSTR(MP_QSTR_compile_command), MP_ROM_PTR(&compile_command_obj) }, |
| 70 | +}; |
| 71 | + |
| 72 | +STATIC MP_DEFINE_CONST_DICT(codeop_module_globals, codeop_module_globals_table); |
| 73 | + |
| 74 | +const mp_obj_module_t codeop_module = { |
| 75 | + .base = { &mp_type_module }, |
| 76 | + .globals = (mp_obj_dict_t *)&codeop_module_globals, |
| 77 | +}; |
| 78 | + |
| 79 | +MP_REGISTER_MODULE(MP_QSTR_codeop, codeop_module); |
0 commit comments