mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
Update main.cpp
Update main.cpp
This commit is contained in:
parent
25f5cd1b72
commit
1e4c571685
43
src/main.cpp
43
src/main.cpp
@ -1,22 +1,55 @@
|
||||
#include <fstream>
|
||||
#include "pocketpy.h"
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
std::string getline(bool* eof=nullptr) {
|
||||
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
||||
std::wstringstream wss;
|
||||
WCHAR buf;
|
||||
DWORD read;
|
||||
while (ReadConsoleW(hStdin, &buf, 1, &read, NULL) && buf != L'\n') {
|
||||
if(eof && buf == L'\x1A') *eof = true; // Ctrl+Z
|
||||
wss << buf;
|
||||
}
|
||||
std::wstring wideInput = wss.str();
|
||||
int length = WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(), (int)wideInput.length(), NULL, 0, NULL, NULL);
|
||||
std::string output;
|
||||
output.resize(length);
|
||||
WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(), (int)wideInput.length(), &output[0], length, NULL, NULL);
|
||||
return output;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
std::string getline(bool* eof=nullptr){
|
||||
std::string line;
|
||||
if(!std::getline(std::cin, line)){
|
||||
if(eof) *eof = true;
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
|
||||
int main(int argc, char** argv){
|
||||
pkpy::VM* vm = pkpy_new_vm(true);
|
||||
vm->bind_builtin_func<0>("input", [](pkpy::VM* vm, pkpy::Args& args){
|
||||
static std::string line;
|
||||
std::getline(std::cin, line);
|
||||
return VAR(line);
|
||||
return VAR(getline());
|
||||
});
|
||||
if(argc == 1){
|
||||
pkpy::REPL* repl = pkpy_new_repl(vm);
|
||||
bool need_more_lines = false;
|
||||
while(true){
|
||||
(*vm->_stdout) << (need_more_lines ? "... " : ">>> ");
|
||||
std::string line;
|
||||
if (!std::getline(std::cin, line)) break;
|
||||
bool eof = false;
|
||||
std::string line = getline(&eof);
|
||||
if(eof) break;
|
||||
need_more_lines = pkpy_repl_input(repl, line.c_str());
|
||||
}
|
||||
pkpy_delete(vm);
|
||||
|
Loading…
x
Reference in New Issue
Block a user