main.cpp 2.8 KB

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