From d9c3f6c1466a7d325870065eb17f42d4e225c8b8 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 25 Jan 2024 00:48:46 +0800 Subject: [PATCH] random refactor --- amalgamate.py | 4 ++-- include/pocketpy/pocketpy.h | 1 - include/pocketpy/re.h | 9 --------- python/random.py | 14 -------------- src/pocketpy.cpp | 1 - src/random.cpp | 23 +++++++++++++++++++++-- src/re.cpp | 9 --------- tests/70_random.py | 7 ++++--- 8 files changed, 27 insertions(+), 41 deletions(-) delete mode 100644 include/pocketpy/re.h delete mode 100644 python/random.py delete mode 100644 src/re.cpp diff --git a/amalgamate.py b/amalgamate.py index 9bb75f13..176eafa2 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -9,7 +9,7 @@ pipeline = [ ["config.h", "export.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h"], ["obj.h", "dict.h", "codeobject.h", "frame.h"], ["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.h"], - ["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "random.h", "re.h", "linalg.h", "easing.h", "io.h", "modules.h"], + ["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "random.h", "linalg.h", "easing.h", "io.h", "modules.h"], ["pocketpy.h", "pocketpy_c.h"] ] @@ -34,7 +34,7 @@ def remove_copied_include(text): key = m.group(1) if key.startswith("pocketpy/"): key = key[9:] - if key in ["user_config.h", "box2dw.hpp", "cJSONw.hpp"]: + if key in ["user_config.h", "cJSONw.hpp"]: return m.group(0) if key == "opcodes.h": return OPCODES_TEXT diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 0534ad0e..9eea4de0 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -10,7 +10,6 @@ #include "easing.h" #include "io.h" #include "vm.h" -#include "re.h" #include "random.h" #include "bindings.h" #include "collections.h" diff --git a/include/pocketpy/re.h b/include/pocketpy/re.h deleted file mode 100644 index 1d97d434..00000000 --- a/include/pocketpy/re.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include "cffi.h" - -namespace pkpy{ - -void add_module_re(VM* vm); - -} // namespace pkpy \ No newline at end of file diff --git a/python/random.py b/python/random.py deleted file mode 100644 index 9f925f6b..00000000 --- a/python/random.py +++ /dev/null @@ -1,14 +0,0 @@ -_inst = Random() - -seed = _inst.seed -random = _inst.random -uniform = _inst.uniform -randint = _inst.randint - -def shuffle(L): - for i in range(len(L)): - j = randint(i, len(L) - 1) - L[i], L[j] = L[j], L[i] - -def choice(L): - return L[randint(0, len(L) - 1)] \ No newline at end of file diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 6759ab93..29ff308e 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -1444,7 +1444,6 @@ void VM::post_init(){ add_module_time(this); add_module_json(this); add_module_math(this); - add_module_re(this); add_module_dis(this); add_module_c(this); add_module_gc(this); diff --git a/src/random.cpp b/src/random.cpp index fab42055..463ff1cb 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -40,14 +40,33 @@ struct Random{ std::uniform_real_distribution dis(a, b); return VAR(dis(self.gen)); }); + + vm->bind_method<1>(type, "shuffle", [](VM* vm, ArgsView args) { + Random& self = _CAST(Random&, args[0]); + List& L = CAST(List&, args[1]); + std::shuffle(L.begin(), L.end(), self.gen); + return vm->None; + }); + + vm->bind_method<1>(type, "choice", [](VM* vm, ArgsView args) { + Random& self = _CAST(Random&, args[0]); + const List& L = CAST(List&, args[1]); + std::uniform_int_distribution dis(0, L.size() - 1); + return L[dis(self.gen)]; + }); } }; void add_module_random(VM* vm){ PyObject* mod = vm->new_module("random"); Random::register_class(vm, mod); - CodeObject_ code = vm->compile(kPythonLibs["random"], "random.py", EXEC_MODE); - vm->_exec(code, mod); + PyObject* instance = vm->heap.gcnew(Random::_type(vm)); + mod->attr().set("seed", vm->getattr(instance, "seed")); + mod->attr().set("random", vm->getattr(instance, "random")); + mod->attr().set("uniform", vm->getattr(instance, "uniform")); + mod->attr().set("randint", vm->getattr(instance, "randint")); + mod->attr().set("shuffle", vm->getattr(instance, "shuffle")); + mod->attr().set("choice", vm->getattr(instance, "choice")); } } // namespace pkpy \ No newline at end of file diff --git a/src/re.cpp b/src/re.cpp deleted file mode 100644 index d132ec6d..00000000 --- a/src/re.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "pocketpy/re.h" - -namespace pkpy{ - -void add_module_re(VM* vm){ - // PyObject* mod = vm->new_module("re"); -} - -} // namespace pkpy \ No newline at end of file diff --git a/tests/70_random.py b/tests/70_random.py index 2e5a0513..4f13ee86 100644 --- a/tests/70_random.py +++ b/tests/70_random.py @@ -12,9 +12,10 @@ for _ in range(100): assert 3.0 <= i <= 9.5 a = [1, 2, 3, 4] -b = (1, 2, 3) r.shuffle(a) -assert r.choice(a) in a -assert r.choice(b) in b + +for i in range(100): + assert r.choice(a) in a +