1
0

main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. static int f_input(pkpy_vm* vm) {
  40. if(!pkpy_is_none(vm, -1)) {
  41. pkpy_CString prompt;
  42. bool ok = pkpy_to_string(vm, -1, &prompt);
  43. if(!ok) return 0;
  44. std::cout << prompt << std::flush;
  45. }
  46. bool eof;
  47. std::string output = pkpy_platform_getline(&eof);
  48. pkpy_push_string(vm, pkpy_string(output.c_str()));
  49. return 1;
  50. }
  51. int main(int argc, char** argv) {
  52. #if _WIN32
  53. SetConsoleCP(CP_UTF8);
  54. SetConsoleOutputCP(CP_UTF8);
  55. #endif
  56. pkpy_vm* vm = pkpy_new_vm(true);
  57. pkpy_push_function(vm, "input(prompt=None) -> str", f_input);
  58. pkpy_py_import(vm, "builtins");
  59. pkpy_setattr(vm, pkpy_name("input"));
  60. if(argc == 1) {
  61. void* repl = pkpy_new_repl(vm);
  62. bool need_more_lines = false;
  63. while(true) {
  64. std::cout << (need_more_lines ? "... " : ">>> ");
  65. bool eof = false;
  66. std::string line = pkpy_platform_getline(&eof);
  67. if(eof) break;
  68. need_more_lines = pkpy_repl_input(repl, line.c_str());
  69. }
  70. pkpy_delete_vm(vm);
  71. return 0;
  72. }
  73. if(argc == 2) {
  74. std::string argv_1 = argv[1];
  75. if(argv_1 == "-h" || argv_1 == "--help") goto __HELP;
  76. std::filesystem::path filepath(argv[1]);
  77. filepath = std::filesystem::absolute(filepath);
  78. if(!std::filesystem::exists(filepath)) {
  79. std::cerr << "File not found: " << argv_1 << std::endl;
  80. return 2;
  81. }
  82. std::ifstream file(filepath);
  83. if(!file.is_open()) {
  84. std::cerr << "Failed to open file: " << argv_1 << std::endl;
  85. return 3;
  86. }
  87. std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  88. file.close();
  89. pkpy_set_main_argv(vm, argc, argv);
  90. bool ok = pkpy_exec_2(vm, src.c_str(), filepath.filename().string().c_str(), 0, NULL);
  91. if(!ok) pkpy_clear_error(vm, NULL);
  92. pkpy_delete_vm(vm);
  93. return ok ? 0 : 1;
  94. }
  95. __HELP:
  96. std::cout << "Usage: pocketpy [filename]" << std::endl;
  97. return 0;
  98. }