main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <fstream>
  2. #include <filesystem>
  3. #include <iostream>
  4. #include <sstream>
  5. #include "pocketpy_c.h"
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. std::string pkpy_platform_getline(bool* eof){
  9. HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
  10. std::wstringstream wss;
  11. WCHAR buf;
  12. DWORD read;
  13. while (ReadConsoleW(hStdin, &buf, 1, &read, NULL) && buf != L'\n') {
  14. if(eof && buf == L'\x1A') *eof = true; // Ctrl+Z
  15. wss << buf;
  16. }
  17. std::wstring wideInput = wss.str();
  18. int length = WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(), (int)wideInput.length(), NULL, 0, NULL, NULL);
  19. std::string output;
  20. output.resize(length);
  21. WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(), (int)wideInput.length(), &output[0], length, NULL, NULL);
  22. if(!output.empty() && output.back() == '\r') output.pop_back();
  23. return output;
  24. }
  25. #else
  26. std::string pkpy_platform_getline(bool* eof){
  27. std::string output;
  28. if(!std::getline(std::cin, output)){
  29. if(eof) *eof = true;
  30. }
  31. return output;
  32. }
  33. #endif
  34. static int f_input(pkpy_vm* vm){
  35. if(!pkpy_is_none(vm, -1)){
  36. pkpy_CString prompt;
  37. bool ok = pkpy_to_string(vm, -1, &prompt);
  38. if(!ok) return 0;
  39. std::cout << prompt << std::flush;
  40. }
  41. bool eof;
  42. std::string output = pkpy_platform_getline(&eof);
  43. pkpy_push_string(vm, pkpy_string(output.c_str()));
  44. return 1;
  45. }
  46. int main(int argc, char** argv){
  47. #if _WIN32
  48. SetConsoleCP(CP_UTF8);
  49. SetConsoleOutputCP(CP_UTF8);
  50. #endif
  51. pkpy_vm* vm = pkpy_new_vm(true);
  52. pkpy_push_function(vm, "input(prompt=None) -> str", f_input);
  53. pkpy_eval(vm, "__import__('builtins')");
  54. pkpy_setattr(vm, pkpy_name("input"));
  55. if(argc == 1){
  56. void* repl = pkpy_new_repl(vm);
  57. bool need_more_lines = false;
  58. while(true){
  59. std::cout << (need_more_lines ? "... " : ">>> ");
  60. bool eof = false;
  61. std::string line = pkpy_platform_getline(&eof);
  62. if(eof) break;
  63. need_more_lines = pkpy_repl_input(repl, line.c_str());
  64. }
  65. pkpy_delete_vm(vm);
  66. return 0;
  67. }
  68. if(argc == 2){
  69. std::string argv_1 = argv[1];
  70. if(argv_1 == "-h" || argv_1 == "--help") goto __HELP;
  71. std::filesystem::path filepath(argv[1]);
  72. filepath = std::filesystem::absolute(filepath);
  73. if(!std::filesystem::exists(filepath)){
  74. std::cerr << "File not found: " << argv_1 << std::endl;
  75. return 2;
  76. }
  77. std::ifstream file(filepath);
  78. if(!file.is_open()){
  79. std::cerr << "Failed to open file: " << argv_1 << std::endl;
  80. return 3;
  81. }
  82. std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  83. file.close();
  84. pkpy_set_main_argv(vm, argc, argv);
  85. bool ok = pkpy_exec_2(vm, src.c_str(), filepath.filename().string().c_str(), 0, NULL);
  86. if(!ok) pkpy_clear_error(vm, NULL);
  87. pkpy_delete_vm(vm);
  88. return ok ? 0 : 1;
  89. }
  90. __HELP:
  91. std::cout << "Usage: pocketpy [filename]" << std::endl;
  92. return 0;
  93. }