mp_flipper_compiler.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <string.h>
  2. #include "py/compile.h"
  3. #include "py/runtime.h"
  4. #include "py/persistentcode.h"
  5. #include "py/gc.h"
  6. #include "py/stackctrl.h"
  7. #include "shared/runtime/gchelper.h"
  8. #include "mp_flipper_runtime.h"
  9. #include "mp_flipper_compiler.h"
  10. #include "mp_flipper_halport.h"
  11. void mp_flipper_exec_str(const char* code) {
  12. #if MP_FLIPPER_IS_COMPILER
  13. nlr_buf_t nlr;
  14. if(nlr_push(&nlr) == 0) {
  15. // Compile, parse and execute the given string
  16. mp_lexer_t* lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, code, strlen(code), 0);
  17. mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(lex->source_name));
  18. mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
  19. mp_obj_t module_fun = mp_compile(&parse_tree, lex->source_name, true);
  20. mp_call_function_0(module_fun);
  21. nlr_pop();
  22. } else {
  23. // Uncaught exception: print it out.
  24. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  25. }
  26. #endif
  27. }
  28. void mp_flipper_exec_py_file(const char* file_path) {
  29. #if MP_FLIPPER_IS_COMPILER
  30. nlr_buf_t nlr;
  31. if(nlr_push(&nlr) == 0) {
  32. do {
  33. // check if file exists
  34. if(mp_flipper_import_stat(file_path) == MP_FLIPPER_IMPORT_STAT_NO_EXIST) {
  35. mp_raise_OSError_with_filename(MP_ENOENT, file_path);
  36. break;
  37. }
  38. // Compile, parse and execute the given file
  39. mp_lexer_t* lex = mp_lexer_new_from_file(qstr_from_str(file_path));
  40. mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(lex->source_name));
  41. mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
  42. mp_obj_t module_fun = mp_compile(&parse_tree, lex->source_name, false);
  43. mp_call_function_0(module_fun);
  44. } while(false);
  45. nlr_pop();
  46. } else {
  47. // Uncaught exception: print it out.
  48. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  49. }
  50. #endif
  51. }
  52. void mp_flipper_compile_and_save_file(const char* py_file_path, const char* mpy_file_path) {
  53. #if MP_FLIPPER_IS_COMPILER
  54. nlr_buf_t nlr;
  55. if(nlr_push(&nlr) == 0) {
  56. mp_lexer_t* lex = mp_lexer_new_from_file(qstr_from_str(py_file_path));
  57. mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(lex->source_name));
  58. mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
  59. mp_compiled_module_t cm;
  60. cm.context = m_new_obj(mp_module_context_t);
  61. mp_compile_to_raw_code(&parse_tree, lex->source_name, false, &cm);
  62. mp_print_t* print = malloc(sizeof(mp_print_t));
  63. print->data = mp_flipper_print_data_alloc();
  64. print->print_strn = mp_flipper_print_strn;
  65. mp_raw_code_save(&cm, print);
  66. const char* data = mp_flipper_print_get_data(print->data);
  67. size_t size = mp_flipper_print_get_data_length(print->data);
  68. mp_flipper_save_file(mpy_file_path, data, size);
  69. mp_flipper_print_data_free(print->data);
  70. free(print);
  71. nlr_pop();
  72. } else {
  73. // Uncaught exception: print it out.
  74. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  75. }
  76. #endif
  77. }