main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <fstream>
  2. #include <filesystem>
  3. #include <iostream>
  4. #include "pocketpy_c.h"
  5. #ifdef _WIN32
  6. void pkpy_platform_getline(char* buffer, int size, 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. size = std::min<int>(size-1, output.size());
  22. for(int i=0; i<size; i++) buffer[i] = output[i];
  23. buffer[size] = '\0';
  24. }
  25. #else
  26. void pkpy_platform_getline(char* buffer, int size, bool* eof){
  27. std::string output;
  28. if(!std::getline(std::cin, output)){
  29. if(eof) *eof = true;
  30. }
  31. size = std::min<int>(size-1, output.size());
  32. for(int i=0; i<size; i++) buffer[i] = output[i];
  33. buffer[size] = '\0';
  34. }
  35. #endif
  36. // std::string f_input(){
  37. // return pkpy::platform_getline();
  38. // }
  39. int main(int argc, char** argv){
  40. char buffer[1024];
  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. void* vm = pkpy_new_vm();
  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. pkpy_platform_getline(buffer, 1024, &eof);
  57. if(eof) break;
  58. need_more_lines = pkpy_repl_input(repl, buffer);
  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. pkpy_vm_exec_2(vm, src.c_str(), filepath.filename().string().c_str(), 0, NULL);
  82. pkpy_delete_vm(vm);
  83. // return ret != nullptr ? 0 : 1;
  84. return 0;
  85. }
  86. __HELP:
  87. std::cout << "Usage: pocketpy [filename]" << std::endl;
  88. return 0;
  89. }