main.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // return pkpy::platform_getline();
  39. // }
  40. int main(int argc, char** argv){
  41. #if _WIN32
  42. // implicitly load pocketpy.dll in current directory
  43. #elif __linux__
  44. dlopen("libpocketpy.so", RTLD_NOW | RTLD_GLOBAL);
  45. #elif __APPLE__
  46. dlopen("libpocketpy.dylib", RTLD_NOW | RTLD_GLOBAL);
  47. #endif
  48. pkpy_vm* vm = pkpy_new_vm(true);
  49. // pkpy::_bind(vm, vm->builtins, "input() -> str", &f_input);
  50. if(argc == 1){
  51. void* repl = pkpy_new_repl(vm);
  52. bool need_more_lines = false;
  53. while(true){
  54. std::cout << (need_more_lines ? "... " : ">>> ");
  55. bool eof = false;
  56. std::string line = pkpy_platform_getline(&eof);
  57. if(eof) break;
  58. need_more_lines = pkpy_repl_input(repl, line.c_str());
  59. }
  60. pkpy_delete_vm(vm);
  61. return 0;
  62. }
  63. if(argc == 2){
  64. std::string argv_1 = argv[1];
  65. if(argv_1 == "-h" || argv_1 == "--help") goto __HELP;
  66. std::filesystem::path filepath(argv[1]);
  67. filepath = std::filesystem::absolute(filepath);
  68. if(!std::filesystem::exists(filepath)){
  69. std::cerr << "File not found: " << argv_1 << std::endl;
  70. return 2;
  71. }
  72. std::ifstream file(filepath);
  73. if(!file.is_open()){
  74. std::cerr << "Failed to open file: " << argv_1 << std::endl;
  75. return 3;
  76. }
  77. std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  78. file.close();
  79. // set parent path as cwd
  80. std::filesystem::current_path(filepath.parent_path());
  81. bool ok = pkpy_exec_2(vm, src.c_str(), filepath.filename().string().c_str(), 0, NULL);
  82. pkpy_delete_vm(vm);
  83. return ok ? 0 : 1;
  84. }
  85. __HELP:
  86. std::cout << "Usage: pocketpy [filename]" << std::endl;
  87. return 0;
  88. }