main.c 851 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "pocketpy.h"
  4. char* read_file(const char* path) {
  5. FILE* file = fopen(path, "r");
  6. if(file == NULL) {
  7. printf("Error: file not found\n");
  8. return NULL;
  9. }
  10. fseek(file, 0, SEEK_END);
  11. long size = ftell(file);
  12. fseek(file, 0, SEEK_SET);
  13. char* buffer = malloc(size + 1);
  14. fread(buffer, 1, size, file);
  15. buffer[size] = 0;
  16. return buffer;
  17. }
  18. int main(int argc, char** argv) {
  19. #if _WIN32
  20. SetConsoleCP(CP_UTF8);
  21. SetConsoleOutputCP(CP_UTF8);
  22. #endif
  23. if(argc != 2) goto __HELP;
  24. char* source = read_file(argv[1]);
  25. py_initialize();
  26. if(py_exec(source)){
  27. py_Error* err = py_getlasterror();
  28. py_Error__print(err);
  29. }
  30. py_finalize();
  31. free(source);
  32. __HELP:
  33. printf("Usage: pocketpy [filename]\n");
  34. return 0;
  35. }