main.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. py_initialize();
  24. const char* source = "[1, 'a']";
  25. if(py_eval(source, py_pushtmp())){
  26. py_Error* err = py_getlasterror();
  27. py_Error__print(err);
  28. }else{
  29. // handle the result
  30. py_Ref _0 = py_list__getitem(py_gettop(), 0);
  31. py_Ref _1 = py_list__getitem(py_gettop(), 1);
  32. int _L0 = py_toint(_0);
  33. const char* _L1 = py_tostr(_1);
  34. printf("%d, %s\n", _L0, _L1);
  35. py_poptmp(1);
  36. }
  37. py_finalize();
  38. return 0;
  39. // if(argc != 2) goto __HELP;
  40. // char* source = read_file(argv[1]);
  41. // py_initialize();
  42. // if(py_exec(source)){
  43. // py_Error* err = py_getlasterror();
  44. // py_Error__print(err);
  45. // }
  46. // py_finalize();
  47. // free(source);
  48. // __HELP:
  49. // printf("Usage: pocketpy [filename]\n");
  50. // return 0;
  51. }