main.cpp 2.9 KB

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