move list.__mul__ to C impl

This commit is contained in:
blueloveTH 2023-02-11 17:22:11 +08:00
parent d9717abc5c
commit 71050fc4bb
5 changed files with 18 additions and 11 deletions

View File

@ -1 +1 @@
g++ -o pocketpy src/main.cpp --std=c++17 -O1 -Wall -Wno-sign-compare -Wno-unused-variable -fno-rtti g++ -o pocketpy src/main.cpp --std=c++17 -O2 -Wall -Wno-sign-compare -Wno-unused-variable -fno-rtti

View File

@ -183,14 +183,6 @@ def __list4pop(self, i=-1):
list.pop = __list4pop list.pop = __list4pop
del __list4pop del __list4pop
def __list4__mul__(self, n):
a = []
for i in range(n):
a.extend(self)
return a
list.__mul__ = __list4__mul__
del __list4__mul__
def __iterable4__eq__(self, other): def __iterable4__eq__(self, other):
if len(self) != len(other): if len(self) != len(other):
return False return False

View File

@ -35,6 +35,10 @@ int main(int argc, char** argv){
std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
PyVarOrNull ret = nullptr; PyVarOrNull ret = nullptr;
ret = vm->exec(src.c_str(), filename, EXEC_MODE); ret = vm->exec(src.c_str(), filename, EXEC_MODE);
// for(auto& [k,v]: pkpy::_stats){
// std::cout << k << ": " << v << std::endl;
// }
pkpy_delete(vm); pkpy_delete(vm);
return ret != nullptr ? 0 : 1; return ret != nullptr ? 0 : 1;
} }

View File

@ -392,11 +392,20 @@ void init_builtins(VM* _vm) {
/************ PyList ************/ /************ PyList ************/
_vm->bind_method<1>("list", "append", [](VM* vm, const pkpy::Args& args) { _vm->bind_method<1>("list", "append", [](VM* vm, const pkpy::Args& args) {
pkpy::List& _self = vm->PyList_AS_C(args[0]); pkpy::List& self = vm->PyList_AS_C(args[0]);
_self.push_back(args[1]); self.push_back(args[1]);
return vm->None; return vm->None;
}); });
_vm->bind_method<1>("list", "__mul__", [](VM* vm, const pkpy::Args& args) {
const pkpy::List& self = vm->PyList_AS_C(args[0]);
int n = (int)vm->PyInt_AS_C(args[1]);
pkpy::List result;
result.reserve(self.size() * n);
for(int i = 0; i < n; i++) result.insert(result.end(), self.begin(), self.end());
return vm->PyList(std::move(result));
});
_vm->bind_method<2>("list", "insert", [](VM* vm, const pkpy::Args& args) { _vm->bind_method<2>("list", "insert", [](VM* vm, const pkpy::Args& args) {
pkpy::List& _self = vm->PyList_AS_C(args[0]); pkpy::List& _self = vm->PyList_AS_C(args[0]);
int _index = (int)vm->PyInt_AS_C(args[1]); int _index = (int)vm->PyInt_AS_C(args[1]);

View File

@ -12,6 +12,7 @@
return new_object(ptype, value); \ return new_object(ptype, value); \
} }
// static std::map<Str, int> _stats;
class VM { class VM {
std::stack< std::unique_ptr<Frame> > callstack; std::stack< std::unique_ptr<Frame> > callstack;
@ -452,6 +453,7 @@ public:
return f(this, args); return f(this, args);
} else if((*callable)->is_type(tp_function)){ } else if((*callable)->is_type(tp_function)){
const pkpy::Function_& fn = PyFunction_AS_C((*callable)); const pkpy::Function_& fn = PyFunction_AS_C((*callable));
// pkpy::_stats[fn->name] += 1;
pkpy::shared_ptr<pkpy::NameDict> _locals = pkpy::make_shared<pkpy::NameDict>(); pkpy::shared_ptr<pkpy::NameDict> _locals = pkpy::make_shared<pkpy::NameDict>();
pkpy::NameDict& locals = *_locals; pkpy::NameDict& locals = *_locals;