This commit is contained in:
blueloveTH 2023-07-08 17:30:15 +08:00
parent 40262b0f44
commit 1f7a7c8606
8 changed files with 97 additions and 123 deletions

View File

@ -110,8 +110,6 @@ class mat3x3:
def trs(t: vec2, r: float, s: vec2) -> mat3x3: ... def trs(t: vec2, r: float, s: vec2) -> mat3x3: ...
def is_affine(self) -> bool: ... def is_affine(self) -> bool: ...
def inverse_affine(self) -> mat3x3: ...
def matmul_affine(self, other: mat3x3) -> mat3x3: ...
def translation(self) -> vec2: ... def translation(self) -> vec2: ...
def rotation(self) -> float: ... def rotation(self) -> float: ...

View File

@ -206,7 +206,7 @@ inline PyObject* _any_c_wrapper(VM* vm, ArgsView args){
} }
template<typename T> template<typename T>
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<T>); static_assert(std::is_pod_v<T>);
static_assert(std::is_pointer_v<T>); static_assert(std::is_pointer_v<T>);
auto proxy = new NativeProxyFuncC(fp); auto proxy = new NativeProxyFuncC(fp);

View File

@ -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; } } #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 <Windows.h>
#endif
} // namespace pkpy } // namespace pkpy

View File

@ -137,7 +137,4 @@ public:
Compiler& operator=(const Compiler&) = delete; Compiler& operator=(const Compiler&) = delete;
}; };
#undef BC_NOARG
#undef BC_KEEPLINE
} // namespace pkpy } // namespace pkpy

View File

@ -240,36 +240,6 @@ struct Mat3x3{
return _31 == 0.0f && _32 == 0.0f && _33 == 1.0f; 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); } Vec2 translation() const { return Vec2(_13, _23); }
float rotation() const { return atan2f(_21, _11); } float rotation() const { return atan2f(_21, _11); }
Vec2 scale() const { Vec2 scale() const {

View File

@ -3,44 +3,9 @@
#include "compiler.h" #include "compiler.h"
#include "vm.h" #include "vm.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
namespace pkpy{ namespace pkpy{
#ifdef _WIN32 std::string platform_getline(bool* eof=nullptr);
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
class REPL { class REPL {
protected: protected:
@ -48,43 +13,8 @@ protected:
std::string buffer; std::string buffer;
VM* vm; VM* vm;
public: public:
REPL(VM* vm) : vm(vm){ REPL(VM* vm);
vm->_stdout(vm, "pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); bool input(std::string line);
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; i<buffer.size(); i++){
// no enough lines
if(buffer[i] != '\n') return true;
}
need_more_lines = 0;
line = buffer;
buffer.clear();
mode = CELL_MODE;
}else{
return true;
}
}
try{
vm->exec(line, "<stdin>", 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;
}
}; };
} // namespace pkpy } // namespace pkpy

View File

@ -353,8 +353,11 @@ namespace pkpy{
vm->bind_method<1>(type, "__eq__", [](VM* vm, ArgsView args){ vm->bind_method<1>(type, "__eq__", [](VM* vm, ArgsView args){
PyMat3x3& self = _CAST(PyMat3x3&, args[0]); PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
PyMat3x3& other = CAST(PyMat3x3&, args[1]); if(is_non_tagged_type(args[1], PyMat3x3::_type(vm))){
return VAR(self == other); PyMat3x3& other = _CAST(PyMat3x3&, args[1]);
return VAR(self == other);
}
return vm->NotImplemented;
}); });
vm->bind_method<0>(type, "determinant", [](VM* vm, ArgsView args){ vm->bind_method<0>(type, "determinant", [](VM* vm, ArgsView args){
@ -411,18 +414,6 @@ namespace pkpy{
return VAR(self.is_affine()); 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){ vm->bind_method<0>(type, "translation", [](VM* vm, ArgsView args){
PyMat3x3& self = _CAST(PyMat3x3&, args[0]); PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
return VAR_T(PyVec2, self.translation()); return VAR_T(PyVec2, self.translation());

75
src/repl.cpp Normal file
View File

@ -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; i<buffer.size(); i++){
// no enough lines
if(buffer[i] != '\n') return true;
}
need_more_lines = 0;
line = buffer;
buffer.clear();
mode = CELL_MODE;
}else{
return true;
}
}
try{
vm->exec(line, "<stdin>", 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;
}
}