diff --git a/build_cpp.sh b/build_cpp.sh index 7886562e..3a241834 100644 --- a/build_cpp.sh +++ b/build_cpp.sh @@ -1 +1 @@ -g++ -o pocketpy src/main.cpp --std=c++17 -O1 -Wall -Wno-sign-compare -Wno-unused-variable -fno-rtti \ No newline at end of file +g++ -o pocketpy src/main.cpp --std=c++17 -O2 -Wall -Wno-sign-compare -Wno-unused-variable -fno-rtti \ No newline at end of file diff --git a/src/builtins.h b/src/builtins.h index 64459fcd..f4255194 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -183,14 +183,6 @@ def __list4pop(self, i=-1): list.pop = __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): if len(self) != len(other): return False diff --git a/src/main.cpp b/src/main.cpp index 99109004..4dae6a43 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,10 @@ int main(int argc, char** argv){ std::string src((std::istreambuf_iterator(file)), std::istreambuf_iterator()); PyVarOrNull ret = nullptr; ret = vm->exec(src.c_str(), filename, EXEC_MODE); + + // for(auto& [k,v]: pkpy::_stats){ + // std::cout << k << ": " << v << std::endl; + // } pkpy_delete(vm); return ret != nullptr ? 0 : 1; } diff --git a/src/pocketpy.h b/src/pocketpy.h index ba30d98f..b2e8b09e 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -392,11 +392,20 @@ void init_builtins(VM* _vm) { /************ PyList ************/ _vm->bind_method<1>("list", "append", [](VM* vm, const pkpy::Args& args) { - pkpy::List& _self = vm->PyList_AS_C(args[0]); - _self.push_back(args[1]); + pkpy::List& self = vm->PyList_AS_C(args[0]); + self.push_back(args[1]); 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) { pkpy::List& _self = vm->PyList_AS_C(args[0]); int _index = (int)vm->PyInt_AS_C(args[1]); diff --git a/src/vm.h b/src/vm.h index ec94fdc3..2d6b2040 100644 --- a/src/vm.h +++ b/src/vm.h @@ -12,6 +12,7 @@ return new_object(ptype, value); \ } +// static std::map _stats; class VM { std::stack< std::unique_ptr > callstack; @@ -452,6 +453,7 @@ public: return f(this, args); } else if((*callable)->is_type(tp_function)){ const pkpy::Function_& fn = PyFunction_AS_C((*callable)); + // pkpy::_stats[fn->name] += 1; pkpy::shared_ptr _locals = pkpy::make_shared(); pkpy::NameDict& locals = *_locals;