main.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. py_initialize();
  25. const char* source = "1 < 2";
  26. if(py_eval(source)) {
  27. py_Error* err = py_lasterror();
  28. py_Error__print(err);
  29. } else {
  30. // handle the result
  31. bool _L0 = py_tobool(py_lastretval());
  32. printf("%d\n", _L0);
  33. }
  34. py_Ref r0 = py_reg(0);
  35. py_Ref r1 = py_reg(1);
  36. py_newint(r0, 1);
  37. py_newfloat(r1, 2.5);
  38. bool ok = py_binaryadd(r0, r1);
  39. assert(ok);
  40. double res = py_tofloat(py_lastretval());
  41. printf("%f\n", res);
  42. py_finalize();
  43. return 0;
  44. // if(argc != 2) goto __HELP;
  45. // char* source = read_file(argv[1]);
  46. // py_initialize();
  47. // if(py_exec(source)){
  48. // py_Error* err = py_getlasterror();
  49. // py_Error__print(err);
  50. // }
  51. // py_finalize();
  52. // free(source);
  53. // __HELP:
  54. // printf("Usage: pocketpy [filename]\n");
  55. // return 0;
  56. }