From 1f7a7c86060bda219326b2587ce708a40ec8d9be Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 8 Jul 2023 17:30:15 +0800 Subject: [PATCH] ... --- include/linalg.pyi | 2 - include/pocketpy/cffi.h | 2 +- include/pocketpy/common.h | 13 +++++++ include/pocketpy/compiler.h | 3 -- include/pocketpy/linalg.h | 30 --------------- include/pocketpy/repl.h | 76 ++----------------------------------- src/linalg.cpp | 19 +++------- src/repl.cpp | 75 ++++++++++++++++++++++++++++++++++++ 8 files changed, 97 insertions(+), 123 deletions(-) create mode 100644 src/repl.cpp diff --git a/include/linalg.pyi b/include/linalg.pyi index 5d46dbd9..e27a621c 100644 --- a/include/linalg.pyi +++ b/include/linalg.pyi @@ -110,8 +110,6 @@ class mat3x3: def trs(t: vec2, r: float, s: vec2) -> mat3x3: ... def is_affine(self) -> bool: ... - def inverse_affine(self) -> mat3x3: ... - def matmul_affine(self, other: mat3x3) -> mat3x3: ... def translation(self) -> vec2: ... def rotation(self) -> float: ... diff --git a/include/pocketpy/cffi.h b/include/pocketpy/cffi.h index ed9895d2..8e5afabd 100644 --- a/include/pocketpy/cffi.h +++ b/include/pocketpy/cffi.h @@ -206,7 +206,7 @@ inline PyObject* _any_c_wrapper(VM* vm, ArgsView args){ } template -inline void bind_any_c_fp(VM* vm, PyObject* obj, Str name, T fp){ +void bind_any_c_fp(VM* vm, PyObject* obj, Str name, T fp){ static_assert(std::is_pod_v); static_assert(std::is_pointer_v); auto proxy = new NativeProxyFuncC(fp); diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 235d44f4..f9417faa 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -148,4 +148,17 @@ inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011; #define ADD_MODULE_PLACEHOLDER(name) namespace pkpy { inline void add_module_##name(void* vm) { (void)vm; } } +#ifdef _WIN32 + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#ifndef NOMINMAX +#define NOMINMAX +#endif + +#include +#endif + } // namespace pkpy \ No newline at end of file diff --git a/include/pocketpy/compiler.h b/include/pocketpy/compiler.h index e1a7fd5d..c815beb8 100644 --- a/include/pocketpy/compiler.h +++ b/include/pocketpy/compiler.h @@ -137,7 +137,4 @@ public: Compiler& operator=(const Compiler&) = delete; }; -#undef BC_NOARG -#undef BC_KEEPLINE - } // namespace pkpy \ No newline at end of file diff --git a/include/pocketpy/linalg.h b/include/pocketpy/linalg.h index 49e07b81..fc079b6a 100644 --- a/include/pocketpy/linalg.h +++ b/include/pocketpy/linalg.h @@ -240,36 +240,6 @@ struct Mat3x3{ return _31 == 0.0f && _32 == 0.0f && _33 == 1.0f; } - Mat3x3 inverse_affine() const{ - Mat3x3 ret; - float det = _11 * _22 - _12 * _21; - float inv_det = 1.0f / det; - ret._11 = _22 * inv_det; - ret._12 = -_12 * inv_det; - ret._13 = (_12 * _23 - _13 * _22) * inv_det; - ret._21 = -_21 * inv_det; - ret._22 = _11 * inv_det; - ret._23 = (_13 * _21 - _11 * _23) * inv_det; - ret._31 = 0.0f; - ret._32 = 0.0f; - ret._33 = 1.0f; - return ret; - } - - Mat3x3 matmul_affine(const Mat3x3& other) const{ - Mat3x3 ret; - ret._11 = _11 * other._11 + _12 * other._21; - ret._12 = _11 * other._12 + _12 * other._22; - ret._13 = _11 * other._13 + _12 * other._23 + _13; - ret._21 = _21 * other._11 + _22 * other._21; - ret._22 = _21 * other._12 + _22 * other._22; - ret._23 = _21 * other._13 + _22 * other._23 + _23; - ret._31 = 0.0f; - ret._32 = 0.0f; - ret._33 = 1.0f; - return ret; - } - Vec2 translation() const { return Vec2(_13, _23); } float rotation() const { return atan2f(_21, _11); } Vec2 scale() const { diff --git a/include/pocketpy/repl.h b/include/pocketpy/repl.h index 9d68f64f..399efa14 100644 --- a/include/pocketpy/repl.h +++ b/include/pocketpy/repl.h @@ -3,44 +3,9 @@ #include "compiler.h" #include "vm.h" -#ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#include -#endif - namespace pkpy{ -#ifdef _WIN32 - -inline 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); - if(!output.empty() && output.back() == '\r') output.pop_back(); - return output; -} - -#else - -inline std::string getline(bool* eof=nullptr){ - std::string line; - if(!std::getline(std::cin, line)){ - if(eof) *eof = true; - } - return line; -} - -#endif +std::string platform_getline(bool* eof=nullptr); class REPL { protected: @@ -48,43 +13,8 @@ protected: std::string buffer; VM* vm; public: - REPL(VM* vm) : vm(vm){ - vm->_stdout(vm, "pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); - vm->_stdout(vm, fmt("[", sizeof(void*)*8, " bit]" "\n")); - vm->_stdout(vm, "https://github.com/blueloveTH/pocketpy" "\n"); - vm->_stdout(vm, "Type \"exit()\" to exit." "\n"); - } - - bool input(std::string line){ - CompileMode mode = REPL_MODE; - if(need_more_lines){ - buffer += line; - buffer += '\n'; - int n = buffer.size(); - if(n>=need_more_lines){ - for(int i=buffer.size()-need_more_lines; iexec(line, "", mode); - }catch(NeedMoreLines& ne){ - buffer += line; - buffer += '\n'; - need_more_lines = ne.is_compiling_class ? 3 : 2; - if (need_more_lines) return true; - } - return false; - } + REPL(VM* vm); + bool input(std::string line); }; } // namespace pkpy \ No newline at end of file diff --git a/src/linalg.cpp b/src/linalg.cpp index 1ff34bea..855a0a72 100644 --- a/src/linalg.cpp +++ b/src/linalg.cpp @@ -353,8 +353,11 @@ namespace pkpy{ vm->bind_method<1>(type, "__eq__", [](VM* vm, ArgsView args){ PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - PyMat3x3& other = CAST(PyMat3x3&, args[1]); - return VAR(self == other); + if(is_non_tagged_type(args[1], PyMat3x3::_type(vm))){ + PyMat3x3& other = _CAST(PyMat3x3&, args[1]); + return VAR(self == other); + } + return vm->NotImplemented; }); vm->bind_method<0>(type, "determinant", [](VM* vm, ArgsView args){ @@ -411,18 +414,6 @@ namespace pkpy{ return VAR(self.is_affine()); }); - vm->bind_method<0>(type, "inverse_affine", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - return VAR_T(PyMat3x3, self.inverse_affine()); - }); - - vm->bind_method<1>(type, "matmul_affine", [](VM* vm, ArgsView args){ - PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - PyMat3x3& other = CAST(PyMat3x3&, args[1]); - return VAR_T(PyMat3x3, self.matmul_affine(other)); - }); - - vm->bind_method<0>(type, "translation", [](VM* vm, ArgsView args){ PyMat3x3& self = _CAST(PyMat3x3&, args[0]); return VAR_T(PyVec2, self.translation()); diff --git a/src/repl.cpp b/src/repl.cpp new file mode 100644 index 00000000..e82184c0 --- /dev/null +++ b/src/repl.cpp @@ -0,0 +1,75 @@ +#include "pocketpy/repl.h" + +namespace pkpy { + +#ifdef _WIN32 + +std::string platform_getline(bool* eof){ + 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); + if(!output.empty() && output.back() == '\r') output.pop_back(); + return output; +} + +#else + +std::string platform_getline(bool* eof){ + std::string line; + if(!std::getline(std::cin, line)){ + if(eof) *eof = true; + } + return line; +} + +#endif + + REPL::REPL(VM* vm) : vm(vm){ + vm->_stdout(vm, "pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); + vm->_stdout(vm, fmt("[", sizeof(void*)*8, " bit]" "\n")); + vm->_stdout(vm, "https://github.com/blueloveTH/pocketpy" "\n"); + vm->_stdout(vm, "Type \"exit()\" to exit." "\n"); + } + + bool REPL::input(std::string line){ + CompileMode mode = REPL_MODE; + if(need_more_lines){ + buffer += line; + buffer += '\n'; + int n = buffer.size(); + if(n>=need_more_lines){ + for(int i=buffer.size()-need_more_lines; iexec(line, "", mode); + }catch(NeedMoreLines& ne){ + buffer += line; + buffer += '\n'; + need_more_lines = ne.is_compiling_class ? 3 : 2; + if (need_more_lines) return true; + } + return false; + } + +} \ No newline at end of file