Bläddra i källkod

random refactor

blueloveTH 2 år sedan
förälder
incheckning
d9c3f6c146
8 ändrade filer med 27 tillägg och 41 borttagningar
  1. 2 2
      amalgamate.py
  2. 0 1
      include/pocketpy/pocketpy.h
  3. 0 9
      include/pocketpy/re.h
  4. 0 14
      python/random.py
  5. 0 1
      src/pocketpy.cpp
  6. 21 2
      src/random.cpp
  7. 0 9
      src/re.cpp
  8. 4 3
      tests/70_random.py

+ 2 - 2
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"],
 	["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"],
 	["obj.h", "dict.h", "codeobject.h", "frame.h"],
 	["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.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"]
 	["pocketpy.h", "pocketpy_c.h"]
 ]
 ]
 
 
@@ -34,7 +34,7 @@ def remove_copied_include(text):
 		key = m.group(1)
 		key = m.group(1)
 		if key.startswith("pocketpy/"):
 		if key.startswith("pocketpy/"):
 			key = key[9:]
 			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)
 			return m.group(0)
 		if key == "opcodes.h":
 		if key == "opcodes.h":
 			return OPCODES_TEXT
 			return OPCODES_TEXT

+ 0 - 1
include/pocketpy/pocketpy.h

@@ -10,7 +10,6 @@
 #include "easing.h"
 #include "easing.h"
 #include "io.h"
 #include "io.h"
 #include "vm.h"
 #include "vm.h"
-#include "re.h"
 #include "random.h"
 #include "random.h"
 #include "bindings.h"
 #include "bindings.h"
 #include "collections.h"
 #include "collections.h"

+ 0 - 9
include/pocketpy/re.h

@@ -1,9 +0,0 @@
-#pragma once
-
-#include "cffi.h"
-
-namespace pkpy{
-
-void add_module_re(VM* vm);
-
-} // namespace pkpy

+ 0 - 14
python/random.py

@@ -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)]

+ 0 - 1
src/pocketpy.cpp

@@ -1444,7 +1444,6 @@ void VM::post_init(){
     add_module_time(this);
     add_module_time(this);
     add_module_json(this);
     add_module_json(this);
     add_module_math(this);
     add_module_math(this);
-    add_module_re(this);
     add_module_dis(this);
     add_module_dis(this);
     add_module_c(this);
     add_module_c(this);
     add_module_gc(this);
     add_module_gc(this);

+ 21 - 2
src/random.cpp

@@ -40,14 +40,33 @@ struct Random{
             std::uniform_real_distribution<f64> dis(a, b);
             std::uniform_real_distribution<f64> dis(a, b);
             return VAR(dis(self.gen));
             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<i64> dis(0, L.size() - 1);
+            return L[dis(self.gen)];
+        });
     }
     }
 };
 };
 
 
 void add_module_random(VM* vm){
 void add_module_random(VM* vm){
     PyObject* mod = vm->new_module("random");
     PyObject* mod = vm->new_module("random");
     Random::register_class(vm, mod);
     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>(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
 }   // namespace pkpy

+ 0 - 9
src/re.cpp

@@ -1,9 +0,0 @@
-#include "pocketpy/re.h"
-
-namespace pkpy{
-
-void add_module_re(VM* vm){
-    // PyObject* mod = vm->new_module("re");
-}
-
-}   // namespace pkpy

+ 4 - 3
tests/70_random.py

@@ -12,9 +12,10 @@ for _ in range(100):
     assert 3.0 <= i <= 9.5
     assert 3.0 <= i <= 9.5
 
 
 a = [1, 2, 3, 4]
 a = [1, 2, 3, 4]
-b = (1, 2, 3)
 r.shuffle(a)
 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
+