mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
up
This commit is contained in:
parent
6446299055
commit
dbbcf29df0
@ -583,21 +583,25 @@ struct ReMatch {
|
|||||||
std::smatch m;
|
std::smatch m;
|
||||||
ReMatch(i64 start, i64 end, std::smatch m) : start(start), end(end), m(m) {}
|
ReMatch(i64 start, i64 end, std::smatch m) : start(start), end(end), m(m) {}
|
||||||
|
|
||||||
static PyVar _bind(VM* vm){
|
static PyVar _register(VM* vm, PyVar mod){
|
||||||
PyVar _tp_match = vm->new_user_type_object(vm->_modules["re"], "Match", vm->_tp_object);
|
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, "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, "end", CPP_LAMBDA(vm->PyInt(OBJ_GET(ReMatch, args[0]).end)));
|
||||||
|
|
||||||
vm->bindMethod<0>(_tp_match, "span", [](VM* vm, const pkpy::Args& args) {
|
vm->bindMethod<0>(_tp_match, "span", [](VM* vm, const pkpy::Args& args) {
|
||||||
auto& m = OBJ_GET(ReMatch, args[0]);
|
auto& self = OBJ_GET(ReMatch, args[0]);
|
||||||
return vm->PyTuple({ vm->PyInt(m.start), vm->PyInt(m.end) });
|
return vm->PyTuple({ vm->PyInt(self.start), vm->PyInt(self.end) });
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bindMethod<1>(_tp_match, "group", [](VM* vm, const pkpy::Args& args) {
|
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]);
|
int index = (int)vm->PyInt_AS_C(args[1]);
|
||||||
index = vm->normalized_index(index, m.m.size());
|
index = vm->normalized_index(index, self.m.size());
|
||||||
return vm->PyStr(m.m[index].str());
|
return vm->PyStr(self.m[index].str());
|
||||||
});
|
});
|
||||||
return _tp_match;
|
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){
|
void __add_module_re(VM* vm){
|
||||||
PyVar mod = vm->new_module("re");
|
PyVar mod = vm->new_module("re");
|
||||||
ReMatch::_bind(vm);
|
ReMatch::_register(vm, mod);
|
||||||
|
|
||||||
vm->bindFunc<2>(mod, "match", [](VM* vm, const pkpy::Args& args) {
|
vm->bindFunc<2>(mod, "match", [](VM* vm, const pkpy::Args& args) {
|
||||||
const _Str& pattern = vm->PyStr_AS_C(args[0]);
|
const _Str& pattern = vm->PyStr_AS_C(args[0]);
|
||||||
|
6
src/vm.h
6
src/vm.h
@ -454,7 +454,7 @@ public:
|
|||||||
|
|
||||||
if((*callable)->is_type(_tp_native_function)){
|
if((*callable)->is_type(_tp_native_function)){
|
||||||
const auto& f = OBJ_GET(_CppFunc, *callable);
|
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);
|
return f(this, args);
|
||||||
} else if((*callable)->is_type(_tp_function)){
|
} else if((*callable)->is_type(_tp_function)){
|
||||||
const _Func& fn = PyFunction_AS_C((*callable));
|
const _Func& fn = PyFunction_AS_C((*callable));
|
||||||
@ -588,6 +588,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
PyVar new_user_type_object(PyVar mod, _Str name, PyVar base){
|
PyVar new_user_type_object(PyVar mod, _Str name, PyVar base){
|
||||||
|
if(!base->is_type(_tp_type)) UNREACHABLE();
|
||||||
PyVar obj = pkpy::make_shared<PyObject, Py_<i64>>(_tp_type, DUMMY_VAL);
|
PyVar obj = pkpy::make_shared<PyObject, Py_<i64>>(_tp_type, DUMMY_VAL);
|
||||||
setattr(obj, __base__, base);
|
setattr(obj, __base__, base);
|
||||||
_Str fullName = name;
|
_Str fullName = name;
|
||||||
@ -729,7 +730,7 @@ public:
|
|||||||
int normalized_index(int index, int size){
|
int normalized_index(int index, int size){
|
||||||
if(index < 0) index += size;
|
if(index < 0) index += 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;
|
return index;
|
||||||
}
|
}
|
||||||
@ -928,6 +929,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
void notImplementedError(){ _error("NotImplementedError", ""); }
|
||||||
void typeError(const _Str& msg){ _error("TypeError", msg); }
|
void typeError(const _Str& msg){ _error("TypeError", msg); }
|
||||||
void zeroDivisionError(){ _error("ZeroDivisionError", "division by zero"); }
|
void zeroDivisionError(){ _error("ZeroDivisionError", "division by zero"); }
|
||||||
void indexError(const _Str& msg){ _error("IndexError", msg); }
|
void indexError(const _Str& msg){ _error("IndexError", msg); }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user