1
0

main.c 4.4 KB

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