main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. const char* filename = NULL;
  33. for(int i = 1; i < argc; i++) {
  34. if(strcmp(argv[i], "--profile") == 0) {
  35. profile = true;
  36. continue;
  37. }
  38. if(strcmp(argv[i], "--debug") == 0) {
  39. debug = true;
  40. continue;
  41. }
  42. if(filename == NULL) {
  43. filename = argv[i];
  44. continue;
  45. }
  46. printf("Usage: pocketpy [--profile] [--debug] filename\n");
  47. }
  48. if(debug && profile) {
  49. printf("Error: --debug and --profile cannot be used together.\n");
  50. return 1;
  51. }
  52. py_initialize();
  53. py_sys_setargv(argc, argv);
  54. if(filename == NULL) {
  55. if(profile) printf("Warning: --profile is ignored in REPL mode.\n");
  56. if(debug) printf("Warning: --debug is ignored in REPL mode.\n");
  57. printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
  58. printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
  59. #ifndef NDEBUG
  60. printf(" (DEBUG)");
  61. #endif
  62. printf("\n");
  63. printf("https://github.com/pocketpy/pocketpy\n");
  64. printf("Type \"exit()\" to exit.\n");
  65. while(true) {
  66. int size = py_replinput(buf, sizeof(buf));
  67. if(size == -1) { // Ctrl-D (i.e. EOF)
  68. printf("\n");
  69. break;
  70. }
  71. assert(size < sizeof(buf));
  72. if(size >= 0) {
  73. py_StackRef p0 = py_peek(0);
  74. if(!py_exec(buf, "<stdin>", SINGLE_MODE, NULL)) {
  75. py_printexc();
  76. py_clearexc(p0);
  77. }
  78. }
  79. }
  80. } else {
  81. if(profile) py_profiler_begin();
  82. if(debug) py_debugger_waitforattach("127.0.0.1", 6110);
  83. char* source = read_file(filename);
  84. if(source) {
  85. if(!py_exec(source, filename, EXEC_MODE, NULL))
  86. py_printexc();
  87. else {
  88. if(profile) {
  89. char* json_report = py_profiler_report();
  90. FILE* report_file = fopen("profiler_report.json", "w");
  91. if(report_file) {
  92. fprintf(report_file, "%s", json_report);
  93. fclose(report_file);
  94. }
  95. PK_FREE(json_report);
  96. }
  97. }
  98. PK_FREE(source);
  99. }
  100. }
  101. int code = py_checkexc(false) ? 1 : 0;
  102. py_finalize();
  103. if(debug) py_debugger_exit(code);
  104. return code;
  105. }