main.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. int main(int argc, char** argv) {
  20. #if _WIN32
  21. SetConsoleCP(CP_UTF8);
  22. SetConsoleOutputCP(CP_UTF8);
  23. #endif
  24. #if 0
  25. py_initialize();
  26. const char* source = "1 < 2";
  27. if(py_eval(source)) {
  28. // handle the result
  29. bool _L0 = py_tobool(py_retval());
  30. printf("%d\n", _L0);
  31. }
  32. py_Ref r0 = py_reg(0);
  33. py_Ref r1 = py_reg(1);
  34. py_newint(r0, 1);
  35. py_newfloat(r1, 2.5);
  36. bool ok = py_binaryadd(r0, r1);
  37. assert(ok);
  38. double res = py_tofloat(py_retval());
  39. printf("%f\n", res);
  40. py_finalize();
  41. return 0;
  42. #endif
  43. if(argc != 2) goto __HELP;
  44. char* source = read_file(argv[1]);
  45. py_initialize();
  46. if(!py_exec(source)) py_printexc();
  47. py_finalize();
  48. free(source);
  49. __HELP:
  50. printf("Usage: pocketpy [filename]\n");
  51. return 0;
  52. }