main.cpp 3.2 KB

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