main.c 3.7 KB

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