random refactor

This commit is contained in:
blueloveTH 2024-01-25 00:48:46 +08:00
parent 837c59980a
commit d9c3f6c146
8 changed files with 27 additions and 41 deletions

View File

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

View File

@ -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"

View File

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

View File

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

View File

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

View File

@ -40,14 +40,33 @@ struct Random{
std::uniform_real_distribution<f64> 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<i64> 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>(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

View File

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

View File

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