This commit is contained in:
blueloveTH 2022-11-10 17:02:02 +08:00
parent aa0c415f1d
commit 8f5c4e38a8
2 changed files with 16 additions and 3 deletions

View File

@ -32,7 +32,7 @@ VM* newVM(){
} }
void REPL(){ void REPL(){
std::cout << "pocketpy 0.1.0" << std::endl; std::cout << "pocketpy " PK_VERSION << std::endl;
std::cout << "https://github.com/blueloveTH/pocketpy" << std::endl; std::cout << "https://github.com/blueloveTH/pocketpy" << std::endl;
int need_more_lines = 0; int need_more_lines = 0;

View File

@ -5,6 +5,9 @@
#include <iomanip> #include <iomanip>
#include <cmath> #include <cmath>
#include <chrono>
#define PK_VERSION "0.2.0"
#define BIND_NUM_ARITH_OPT(name, op) \ #define BIND_NUM_ARITH_OPT(name, op) \
_vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, PyVarList args){ \ _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, PyVarList args){ \
@ -493,14 +496,23 @@ void __runCodeBuiltins(VM* vm, const char* src){
#define __EXPORT #define __EXPORT
#endif #endif
#include <ctime>
void __addModuleTime(VM* vm){ void __addModuleTime(VM* vm){
PyVar mod = vm->newModule("time"); PyVar mod = vm->newModule("time");
vm->bindFunc(mod, "time", [](VM* vm, PyVarList args) { vm->bindFunc(mod, "time", [](VM* vm, PyVarList args) {
return vm->PyFloat((_Int)std::time(nullptr)); auto now = std::chrono::high_resolution_clock::now();
return vm->PyFloat(std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count() / 1000000.0);
}); });
} }
void __addModuleSys(VM* vm){
PyVar mod = vm->newModule("sys");
vm->bindFunc(mod, "getrefcount", [](VM* vm, PyVarList args) {
vm->__checkArgSize(args, 1);
return vm->PyInt(args[0].use_count());
});
vm->setAttr(mod, "version", vm->PyStr(PK_VERSION));
}
extern "C" { extern "C" {
__EXPORT __EXPORT
@ -508,6 +520,7 @@ extern "C" {
VM* vm = new VM(); VM* vm = new VM();
__initializeBuiltinFunctions(vm); __initializeBuiltinFunctions(vm);
__runCodeBuiltins(vm, __BUILTINS_CODE); __runCodeBuiltins(vm, __BUILTINS_CODE);
__addModuleSys(vm);
__addModuleTime(vm); __addModuleTime(vm);
vm->_stdout = _stdout; vm->_stdout = _stdout;
vm->_stderr = _stderr; vm->_stderr = _stderr;