main.cpp 3.1 KB

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