main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <string.h>
  5. #include "pocketpy.h"
  6. #ifdef _WIN32
  7. #define WIN32_LEAN_AND_MEAN
  8. #include <windows.h>
  9. #endif
  10. static char buf[2048];
  11. int main(int argc, char** argv) {
  12. #if _WIN32
  13. SetConsoleCP(CP_UTF8);
  14. SetConsoleOutputCP(CP_UTF8);
  15. #endif
  16. bool profile = false;
  17. bool debug = false;
  18. bool compile = false;
  19. const char* arg1 = NULL;
  20. const char* arg2 = NULL;
  21. for(int i = 1; i < argc; i++) {
  22. if(strcmp(argv[i], "--profile") == 0) {
  23. profile = true;
  24. continue;
  25. }
  26. if(strcmp(argv[i], "--debug") == 0) {
  27. debug = true;
  28. continue;
  29. }
  30. if(strcmp(argv[i], "--compile") == 0) {
  31. compile = true;
  32. continue;
  33. }
  34. if(arg1 == NULL) {
  35. arg1 = argv[i];
  36. continue;
  37. }
  38. if(arg2 == NULL) {
  39. arg2 = argv[i];
  40. continue;
  41. }
  42. printf("Usage: pocketpy [--profile] [--debug] [--compile] filename\n");
  43. }
  44. if(debug && profile) {
  45. printf("Error: --debug and --profile cannot be used together.\n");
  46. return 1;
  47. }
  48. if(compile && (debug || profile)) {
  49. printf("Error: --compile cannot be used with --debug or --profile.\n");
  50. return 1;
  51. }
  52. py_initialize();
  53. py_sys_setargv(argc, argv);
  54. if(compile) {
  55. bool ok = py_compilefile(arg1, arg2);
  56. if(!ok) py_printexc();
  57. py_finalize();
  58. return ok ? 0 : 1;
  59. }
  60. const char* filename = arg1;
  61. if(filename == NULL) {
  62. if(profile) printf("Warning: --profile is ignored in REPL mode.\n");
  63. if(debug) printf("Warning: --debug is ignored in REPL mode.\n");
  64. printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
  65. printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
  66. #ifndef NDEBUG
  67. printf(" (DEBUG)");
  68. #endif
  69. printf("\n");
  70. printf("https://github.com/pocketpy/pocketpy\n");
  71. printf("Type \"exit()\" to exit.\n");
  72. while(true) {
  73. int size = py_replinput(buf, sizeof(buf));
  74. if(size == -1) { // Ctrl-D (i.e. EOF)
  75. printf("\n");
  76. break;
  77. }
  78. assert(size < sizeof(buf));
  79. if(size >= 0) {
  80. py_StackRef p0 = py_peek(0);
  81. if(!py_exec(buf, "<stdin>", SINGLE_MODE, NULL)) {
  82. py_printexc();
  83. py_clearexc(p0);
  84. }
  85. }
  86. }
  87. } else {
  88. if(profile) py_profiler_begin();
  89. if(debug) py_debugger_waitforattach("127.0.0.1", 6110);
  90. int data_size;
  91. char* data = py_callbacks()->importfile(filename, &data_size);
  92. // check filename endswith .pyc
  93. bool is_pyc = false;
  94. int filename_len = (int)strlen(filename);
  95. if(filename_len >= 4) {
  96. if(filename[filename_len - 4] == '.' &&
  97. filename[filename_len - 3] == 'p' &&
  98. filename[filename_len - 2] == 'y' &&
  99. filename[filename_len - 1] == 'c') {
  100. is_pyc = true;
  101. }
  102. }
  103. if(data) {
  104. bool ok;
  105. if(is_pyc) {
  106. ok = py_execo(data, data_size, filename, NULL);
  107. } else {
  108. ok = py_exec(data, filename, EXEC_MODE, NULL);
  109. }
  110. if(!ok) py_printexc();
  111. if(profile) {
  112. char* json_report = py_profiler_report();
  113. FILE* report_file = fopen("profiler_report.json", "w");
  114. if(report_file) {
  115. fprintf(report_file, "%s", json_report);
  116. fclose(report_file);
  117. }
  118. PK_FREE(json_report);
  119. }
  120. PK_FREE(data);
  121. } else {
  122. printf("Error: cannot open file '%s'\n", filename);
  123. py_finalize();
  124. return 1;
  125. }
  126. }
  127. int code = py_checkexc() ? 1 : 0;
  128. py_finalize();
  129. if(debug) py_debugger_exit(code);
  130. return code;
  131. }