mpy_main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include <shell.h>
  31. #include "py/compile.h"
  32. #include "py/runtime.h"
  33. #include "py/repl.h"
  34. #include "py/gc.h"
  35. #include "py/mperrno.h"
  36. #include "py/stackctrl.h"
  37. #include "lib/utils/pyexec.h"
  38. #include "rtt_getchar.h"
  39. #if MICROPY_ENABLE_COMPILER
  40. void do_str(const char *src, mp_parse_input_kind_t input_kind) {
  41. nlr_buf_t nlr;
  42. if (nlr_push(&nlr) == 0) {
  43. mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
  44. qstr source_name = lex->source_name;
  45. mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
  46. mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true);
  47. mp_call_function_0(module_fun);
  48. nlr_pop();
  49. } else {
  50. // uncaught exception
  51. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  52. }
  53. }
  54. #endif
  55. static char *stack_top;
  56. static char heap[MICROPY_HEAP_SIZE];
  57. void mpy_main(const char *filename) {
  58. int stack_dummy;
  59. stack_top = (char*)&stack_dummy;
  60. rt_kprintf("\n");
  61. rtt_getchar_init();
  62. mp_stack_set_top(stack_top);
  63. // Make MicroPython's stack limit somewhat smaller than full stack available
  64. mp_stack_set_limit(FINSH_THREAD_STACK_SIZE - 512);
  65. #if MICROPY_ENABLE_GC
  66. gc_init(heap, heap + sizeof(heap));
  67. #endif
  68. mp_init();
  69. if (filename) {
  70. pyexec_file(filename);
  71. } else {
  72. #if MICROPY_ENABLE_COMPILER
  73. #if MICROPY_REPL_EVENT_DRIVEN
  74. pyexec_event_repl_init();
  75. for (;;) {
  76. int c = mp_hal_stdin_rx_chr();
  77. if (pyexec_event_repl_process_char(c)) {
  78. break;
  79. }
  80. }
  81. #else
  82. pyexec_friendly_repl();
  83. #endif
  84. // do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
  85. // do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT);
  86. #else
  87. pyexec_frozen_module("frozentest.py");
  88. #endif
  89. }
  90. mp_deinit();
  91. rtt_getchar_deinit();
  92. }
  93. void gc_collect(void) {
  94. // WARNING: This gc_collect implementation doesn't try to get root
  95. // pointers from CPU registers, and thus may function incorrectly.
  96. void *dummy;
  97. gc_collect_start();
  98. gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
  99. gc_collect_end();
  100. gc_dump_info();
  101. }
  102. mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
  103. mp_raise_OSError(ENOENT);
  104. }
  105. mp_import_stat_t mp_import_stat(const char *path) {
  106. return MP_IMPORT_STAT_NO_EXIST;
  107. }
  108. mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
  109. return mp_const_none;
  110. }
  111. MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
  112. NORETURN void nlr_jump_fail(void *val) {
  113. while (1);
  114. }
  115. #ifndef NDEBUG
  116. void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
  117. printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
  118. __fatal_error("Assertion failed");
  119. }
  120. #endif
  121. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  122. #include <finsh.h>
  123. static void python(uint8_t argc, char **argv) {
  124. if (argc > 1) {
  125. mpy_main(argv[1]);
  126. } else {
  127. mpy_main(NULL);
  128. }
  129. }
  130. MSH_CMD_EXPORT(python, MicroPython: `python [file.py]` execute python script);
  131. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */