main.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <malloc.h>
  6. #include "py/obj.h"
  7. #include "py/compile.h"
  8. #include "py/runtime.h"
  9. #include "py/stackctrl.h"
  10. #include "py/gc.h"
  11. #include "py/repl.h"
  12. #include "py/mperrno.h"
  13. void do_str(const char *src, mp_parse_input_kind_t input_kind) {
  14. nlr_buf_t nlr;
  15. if (nlr_push(&nlr) == 0) {
  16. mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
  17. qstr source_name = lex->source_name;
  18. mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
  19. mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true);
  20. mp_call_function_0(module_fun);
  21. nlr_pop();
  22. } else {
  23. // uncaught exception
  24. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  25. }
  26. }
  27. int main(int argc, char **argv) {
  28. mp_stack_ctrl_init();
  29. mp_stack_set_limit(10240);
  30. void *heap = malloc(16 * 1024);
  31. gc_init(heap, (char*)heap + 16 * 1024);
  32. mp_init();
  33. do_str("print('hello world!')", MP_PARSE_SINGLE_INPUT);
  34. mp_deinit();
  35. return 0;
  36. }
  37. void gc_collect(void) {
  38. }
  39. mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
  40. mp_raise_OSError(MP_ENOENT);
  41. }
  42. mp_import_stat_t mp_import_stat(const char *path) {
  43. return MP_IMPORT_STAT_NO_EXIST;
  44. }
  45. mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
  46. return mp_const_none;
  47. }
  48. MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
  49. void nlr_jump_fail(void *val) {
  50. printf("uncaught NLR\n");
  51. exit(1);
  52. }