This commit is contained in:
blueloveTH 2024-06-02 23:29:20 +08:00
parent 426993552c
commit 2ea64ce41f
3 changed files with 37 additions and 29 deletions

View File

@ -2,3 +2,5 @@
BasedOnStyle: Google BasedOnStyle: Google
IndentWidth: 4 IndentWidth: 4
UseTab: Never UseTab: Never
IndentPPDirectives: BeforeHash

View File

@ -15,7 +15,8 @@ def get_all_files(root: str):
yield fullpath yield fullpath
if __name__ == '__main__': if __name__ == '__main__':
files = list(get_all_files('src')) files = []
# files.extend(get_all_files('include'))
# files.extend(get_all_files('src'))
files.extend(get_all_files('src2')) files.extend(get_all_files('src2'))
files.extend(get_all_files('include')) subprocess.run(['clang-format', '-i'] + files, check=True)
subprocess.run(['clang-format-15', '-i'] + files, check=True)

View File

@ -1,5 +1,5 @@
#include <fstream>
#include <filesystem> #include <filesystem>
#include <fstream>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -12,43 +12,46 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
std::string pkpy_platform_getline(bool* eof){ std::string pkpy_platform_getline(bool* eof) {
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
std::wstringstream wss; std::wstringstream wss;
WCHAR buf; WCHAR buf;
DWORD read; DWORD read;
while (ReadConsoleW(hStdin, &buf, 1, &read, NULL) && buf != L'\n') { while (ReadConsoleW(hStdin, &buf, 1, &read, NULL) && buf != L'\n') {
if(eof && buf == L'\x1A') *eof = true; // Ctrl+Z if (eof && buf == L'\x1A') *eof = true; // Ctrl+Z
wss << buf; wss << buf;
} }
std::wstring wideInput = wss.str(); std::wstring wideInput = wss.str();
int length = WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(), (int)wideInput.length(), NULL, 0, NULL, NULL); int length =
WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(),
(int)wideInput.length(), NULL, 0, NULL, NULL);
std::string output; std::string output;
output.resize(length); output.resize(length);
WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(), (int)wideInput.length(), &output[0], length, NULL, NULL); WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(), (int)wideInput.length(),
if(!output.empty() && output.back() == '\r') output.pop_back(); &output[0], length, NULL, NULL);
if (!output.empty() && output.back() == '\r') output.pop_back();
return output; return output;
} }
#else #else
std::string pkpy_platform_getline(bool* eof){ std::string pkpy_platform_getline(bool* eof) {
std::string output; std::string output;
if(!std::getline(std::cin, output)){ if (!std::getline(std::cin, output)) {
if(eof) *eof = true; if (eof) *eof = true;
} }
return output; return output;
} }
#endif #endif
static int f_input(pkpy_vm* vm){ static int f_input(pkpy_vm* vm) {
if(!pkpy_is_none(vm, -1)){ if (!pkpy_is_none(vm, -1)) {
pkpy_CString prompt; pkpy_CString prompt;
bool ok = pkpy_to_string(vm, -1, &prompt); bool ok = pkpy_to_string(vm, -1, &prompt);
if(!ok) return 0; if (!ok) return 0;
std::cout << prompt << std::flush; std::cout << prompt << std::flush;
} }
bool eof; bool eof;
@ -57,7 +60,7 @@ static int f_input(pkpy_vm* vm){
return 1; return 1;
} }
int main(int argc, char** argv){ int main(int argc, char** argv) {
#if _WIN32 #if _WIN32
SetConsoleCP(CP_UTF8); SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8); SetConsoleOutputCP(CP_UTF8);
@ -68,42 +71,44 @@ int main(int argc, char** argv){
pkpy_py_import(vm, "builtins"); pkpy_py_import(vm, "builtins");
pkpy_setattr(vm, pkpy_name("input")); pkpy_setattr(vm, pkpy_name("input"));
if(argc == 1){ if (argc == 1) {
void* repl = pkpy_new_repl(vm); void* repl = pkpy_new_repl(vm);
bool need_more_lines = false; bool need_more_lines = false;
while(true){ while (true) {
std::cout << (need_more_lines ? "... " : ">>> "); std::cout << (need_more_lines ? "... " : ">>> ");
bool eof = false; bool eof = false;
std::string line = pkpy_platform_getline(&eof); std::string line = pkpy_platform_getline(&eof);
if(eof) break; if (eof) break;
need_more_lines = pkpy_repl_input(repl, line.c_str()); need_more_lines = pkpy_repl_input(repl, line.c_str());
} }
pkpy_delete_vm(vm); pkpy_delete_vm(vm);
return 0; return 0;
} }
if(argc == 2){ if (argc == 2) {
std::string argv_1 = argv[1]; std::string argv_1 = argv[1];
if(argv_1 == "-h" || argv_1 == "--help") goto __HELP; if (argv_1 == "-h" || argv_1 == "--help") goto __HELP;
std::filesystem::path filepath(argv[1]); std::filesystem::path filepath(argv[1]);
filepath = std::filesystem::absolute(filepath); filepath = std::filesystem::absolute(filepath);
if(!std::filesystem::exists(filepath)){ if (!std::filesystem::exists(filepath)) {
std::cerr << "File not found: " << argv_1 << std::endl; std::cerr << "File not found: " << argv_1 << std::endl;
return 2; return 2;
} }
std::ifstream file(filepath); std::ifstream file(filepath);
if(!file.is_open()){ if (!file.is_open()) {
std::cerr << "Failed to open file: " << argv_1 << std::endl; std::cerr << "Failed to open file: " << argv_1 << std::endl;
return 3; return 3;
} }
std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); std::string src((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close(); file.close();
pkpy_set_main_argv(vm, argc, argv); pkpy_set_main_argv(vm, argc, argv);
bool ok = pkpy_exec_2(vm, src.c_str(), filepath.filename().string().c_str(), 0, NULL); bool ok = pkpy_exec_2(vm, src.c_str(),
if(!ok) pkpy_clear_error(vm, NULL); filepath.filename().string().c_str(), 0, NULL);
if (!ok) pkpy_clear_error(vm, NULL);
pkpy_delete_vm(vm); pkpy_delete_vm(vm);
return ok ? 0 : 1; return ok ? 0 : 1;
} }