main.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include "pocketpy.h"
  5. char* read_file(const char* path) {
  6. FILE* file = fopen(path, "r");
  7. if(file == NULL) {
  8. printf("Error: file not found\n");
  9. return NULL;
  10. }
  11. fseek(file, 0, SEEK_END);
  12. long size = ftell(file);
  13. fseek(file, 0, SEEK_SET);
  14. char* buffer = malloc(size + 1);
  15. fread(buffer, 1, size, file);
  16. buffer[size] = 0;
  17. return buffer;
  18. }
  19. static char buf[2048];
  20. int main(int argc, char** argv) {
  21. #if _WIN32
  22. SetConsoleCP(CP_UTF8);
  23. SetConsoleOutputCP(CP_UTF8);
  24. #endif
  25. if(argc > 2){
  26. printf("Usage: pocketpy [filename]\n");
  27. return 0;
  28. }
  29. py_initialize();
  30. if(argc == 1){
  31. printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
  32. printf("[%d bit] on %s" "\n", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
  33. printf("https://github.com/pocketpy/pocketpy" "\n");
  34. printf("Type \"exit()\" to exit." "\n");
  35. while(true){
  36. int size = py_replinput(buf);
  37. assert(size < sizeof(buf));
  38. if(size >= 0){
  39. if(!py_exec2(buf, "<stdin>", REPL_MODE)) py_printexc();
  40. }
  41. }
  42. }else{
  43. char* source = read_file(argv[1]);
  44. if(!py_exec(source)) py_printexc();
  45. free(source);
  46. }
  47. py_finalize();
  48. return 0;
  49. }