From dbbcf29df06c0bc61f91a91ed5a800a36256c09f Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Tue, 7 Feb 2023 21:01:46 +0000 Subject: [PATCH] up --- src/pocketpy.h | 20 ++++++++++++-------- src/vm.h | 6 ++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/pocketpy.h b/src/pocketpy.h index e41fbb1d..36fada69 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -583,21 +583,25 @@ struct ReMatch { std::smatch m; ReMatch(i64 start, i64 end, std::smatch m) : start(start), end(end), m(m) {} - static PyVar _bind(VM* vm){ - PyVar _tp_match = vm->new_user_type_object(vm->_modules["re"], "Match", vm->_tp_object); + static PyVar _register(VM* vm, PyVar mod){ + PyVar _tp_match = vm->new_user_type_object(mod, "Match", vm->_tp_object); + vm->bindMethod<-1>(_tp_match, "__init__", [](VM* vm, const pkpy::Args& args){ + vm->notImplementedError(); + return vm->None; + }); vm->bindMethod<0>(_tp_match, "start", CPP_LAMBDA(vm->PyInt(OBJ_GET(ReMatch, args[0]).start))); vm->bindMethod<0>(_tp_match, "end", CPP_LAMBDA(vm->PyInt(OBJ_GET(ReMatch, args[0]).end))); vm->bindMethod<0>(_tp_match, "span", [](VM* vm, const pkpy::Args& args) { - auto& m = OBJ_GET(ReMatch, args[0]); - return vm->PyTuple({ vm->PyInt(m.start), vm->PyInt(m.end) }); + auto& self = OBJ_GET(ReMatch, args[0]); + return vm->PyTuple({ vm->PyInt(self.start), vm->PyInt(self.end) }); }); vm->bindMethod<1>(_tp_match, "group", [](VM* vm, const pkpy::Args& args) { - auto& m = OBJ_GET(ReMatch, args[0]); + auto& self = OBJ_GET(ReMatch, args[0]); int index = (int)vm->PyInt_AS_C(args[1]); - index = vm->normalized_index(index, m.m.size()); - return vm->PyStr(m.m[index].str()); + index = vm->normalized_index(index, self.m.size()); + return vm->PyStr(self.m[index].str()); }); return _tp_match; } @@ -617,7 +621,7 @@ PyVar __regex_search(const _Str& pattern, const _Str& string, bool fromStart, VM void __add_module_re(VM* vm){ PyVar mod = vm->new_module("re"); - ReMatch::_bind(vm); + ReMatch::_register(vm, mod); vm->bindFunc<2>(mod, "match", [](VM* vm, const pkpy::Args& args) { const _Str& pattern = vm->PyStr_AS_C(args[0]); diff --git a/src/vm.h b/src/vm.h index dc10e8bf..93f8dc51 100644 --- a/src/vm.h +++ b/src/vm.h @@ -454,7 +454,7 @@ public: if((*callable)->is_type(_tp_native_function)){ const auto& f = OBJ_GET(_CppFunc, *callable); - // _CppFunc do not support kwargs + if(kwargs.size() != 0) typeError("_native_function does not accept keyword arguments"); return f(this, args); } else if((*callable)->is_type(_tp_function)){ const _Func& fn = PyFunction_AS_C((*callable)); @@ -588,6 +588,7 @@ public: } PyVar new_user_type_object(PyVar mod, _Str name, PyVar base){ + if(!base->is_type(_tp_type)) UNREACHABLE(); PyVar obj = pkpy::make_shared>(_tp_type, DUMMY_VAL); setattr(obj, __base__, base); _Str fullName = name; @@ -729,7 +730,7 @@ public: int normalized_index(int index, int size){ if(index < 0) index += size; if(index < 0 || index >= size){ - indexError("index out of range, " + std::to_string(index) + " not in [0, " + std::to_string(size) + ")"); + indexError(std::to_string(index) + " not in [0, " + std::to_string(size) + ")"); } return index; } @@ -928,6 +929,7 @@ private: } public: + void notImplementedError(){ _error("NotImplementedError", ""); } void typeError(const _Str& msg){ _error("TypeError", msg); } void zeroDivisionError(){ _error("ZeroDivisionError", "division by zero"); } void indexError(const _Str& msg){ _error("IndexError", msg); }