mpy_main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Armink (armink.ztl@gmail.com)
  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 <stdint.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <rtthread.h>
  30. #ifdef RT_USING_DFS
  31. #include <dfs_posix.h>
  32. #endif
  33. #include <py/compile.h>
  34. #include <py/runtime.h>
  35. #include <py/repl.h>
  36. #include <py/gc.h>
  37. #include <py/mperrno.h>
  38. #include <py/stackctrl.h>
  39. #include <py/frozenmod.h>
  40. #include <lib/mp-readline/readline.h>
  41. #include <lib/utils/pyexec.h>
  42. #include "rtt_getchar.h"
  43. #if MICROPY_ENABLE_COMPILER
  44. void do_str(const char *src, mp_parse_input_kind_t input_kind) {
  45. nlr_buf_t nlr;
  46. if (nlr_push(&nlr) == 0) {
  47. mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
  48. qstr source_name = lex->source_name;
  49. mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
  50. mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true);
  51. mp_call_function_0(module_fun);
  52. nlr_pop();
  53. } else {
  54. // uncaught exception
  55. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  56. }
  57. }
  58. #endif
  59. static void *stack_top = RT_NULL;
  60. static char *heap = RT_NULL;
  61. void mpy_main(const char *filename) {
  62. int stack_dummy;
  63. stack_top = (void *)&stack_dummy;
  64. rtt_getchar_init();
  65. #if MICROPY_PY_THREAD
  66. mp_thread_init(rt_thread_self()->stack_addr, ((rt_uint32_t)stack_top - (rt_uint32_t)rt_thread_self()->stack_addr) / 4);
  67. #endif
  68. mp_stack_set_top(stack_top);
  69. // Make MicroPython's stack limit somewhat smaller than full stack available
  70. mp_stack_set_limit(rt_thread_self()->stack_size - 1024);
  71. #if MICROPY_ENABLE_GC
  72. heap = rt_malloc(MICROPY_HEAP_SIZE);
  73. if (!heap) {
  74. rt_kprintf("No memory for MicroPython Heap!\n");
  75. return;
  76. }
  77. gc_init(heap, heap + MICROPY_HEAP_SIZE);
  78. #endif
  79. /* MicroPython initialization */
  80. mp_init();
  81. /* system path initialization */
  82. mp_obj_list_init(mp_sys_path, 0);
  83. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
  84. mp_obj_list_append(mp_sys_path, mp_obj_new_str(MICROPY_PY_PATH, strlen(MICROPY_PY_PATH)));
  85. mp_obj_list_init(mp_sys_argv, 0);
  86. readline_init0();
  87. if (filename) {
  88. #ifndef MICROPYTHON_USING_UOS
  89. rt_kprintf("Please enable uos module in sys module option first.\n");
  90. #else
  91. pyexec_file(filename);
  92. #endif
  93. } else {
  94. #ifdef MICROPYTHON_USING_UOS
  95. // run boot-up scripts
  96. void *frozen_data;
  97. const char *_boot_file = "_boot.py", *boot_file = "boot.py", *main_file = "main.py";
  98. if (mp_find_frozen_module(_boot_file, strlen(_boot_file), &frozen_data) != MP_FROZEN_NONE) {
  99. pyexec_frozen_module(_boot_file);
  100. }
  101. if (!access(boot_file, 0)) {
  102. pyexec_file(boot_file);
  103. }
  104. // run main scripts
  105. if (!access(main_file, 0)) {
  106. if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
  107. pyexec_file(main_file);
  108. }
  109. }
  110. #endif /* MICROPYTHON_USING_UOS */
  111. rt_kprintf("\n");
  112. for (;;) {
  113. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  114. if (pyexec_raw_repl() != 0) {
  115. break;
  116. }
  117. } else {
  118. if (pyexec_friendly_repl() != 0) {
  119. break;
  120. }
  121. }
  122. }
  123. }
  124. gc_sweep_all();
  125. mp_deinit();
  126. #if MICROPY_PY_THREAD
  127. mp_thread_deinit();
  128. #endif
  129. rt_free(heap);
  130. rtt_getchar_deinit();
  131. }
  132. #if !MICROPY_PY_MODUOS_FILE
  133. mp_import_stat_t mp_import_stat(const char *path) {
  134. return MP_IMPORT_STAT_NO_EXIST;
  135. }
  136. #endif
  137. NORETURN void nlr_jump_fail(void *val) {
  138. DEBUG_printf("nlr_jump_fail\n");
  139. while (1);
  140. }
  141. #ifndef NDEBUG
  142. void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
  143. rt_kprintf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
  144. RT_ASSERT(0);
  145. }
  146. #endif
  147. #include <stdarg.h>
  148. int DEBUG_printf(const char *format, ...)
  149. {
  150. static char log_buf[512];
  151. va_list args;
  152. /* args point to the first variable parameter */
  153. va_start(args, format);
  154. /* must use vprintf to print */
  155. rt_vsprintf(log_buf, format, args);
  156. rt_kprintf("%s", log_buf);
  157. va_end(args);
  158. return 0;
  159. }
  160. #ifndef MICROPYTHON_USING_UOS
  161. mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
  162. mp_raise_OSError(MP_ENOENT);
  163. }
  164. #endif
  165. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  166. #include <finsh.h>
  167. static void python(uint8_t argc, char **argv) {
  168. if (argc > 1) {
  169. mpy_main(argv[1]);
  170. } else {
  171. mpy_main(NULL);
  172. }
  173. }
  174. MSH_CMD_EXPORT(python, MicroPython: `python [file.py]` execute python script);
  175. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */