From bc001143f2beba2dbef1ee1d1ef1025815cec1dd Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 27 Jan 2023 19:44:12 +0800 Subject: [PATCH] up --- src/__stl__.h | 2 +- src/main.cpp | 2 +- src/obj.h | 11 +- src/pocketpy.h | 311 ++++++++++++++++++++----------------------------- src/vm.h | 66 ++++++----- 5 files changed, 176 insertions(+), 216 deletions(-) diff --git a/src/__stl__.h b/src/__stl__.h index 46063e37..c6101789 100644 --- a/src/__stl__.h +++ b/src/__stl__.h @@ -20,7 +20,7 @@ #include #include #include -// #include +#include #include #include diff --git a/src/main.cpp b/src/main.cpp index 286aab28..6d92eed0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,7 @@ #include "pocketpy.h" -//#define PK_DEBUG_TIME +#define PK_DEBUG_TIME struct Timer{ const char* title; diff --git a/src/obj.h b/src/obj.h index 867d94af..4ab35b6f 100644 --- a/src/obj.h +++ b/src/obj.h @@ -7,10 +7,19 @@ struct BaseRef; class VM; class Frame; -typedef PyVar (*_CppFunc)(VM*, const pkpy::ArgList&); +typedef PyVar (*_CppFuncRaw)(VM*, const pkpy::ArgList&); //typedef std::function _CppFunc; typedef pkpy::shared_ptr _Code; +struct _CppFunc { + _CppFuncRaw f; + int argc; // DONOT include self + bool method; + + _CppFunc(_CppFuncRaw f, int argc, bool method) : f(f), argc(argc), method(method) {} + inline PyVar operator()(VM* vm, const pkpy::ArgList& args) const; +}; + struct Function { _Str name; _Code code; diff --git a/src/pocketpy.h b/src/pocketpy.h index 44336733..b5c08c1a 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -16,7 +16,7 @@ _Code VM::compile(_Str source, _Str filename, CompileMode mode) { } #define BIND_NUM_ARITH_OPT(name, op) \ - _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){ \ + _vm->bindMethodMulti<1>({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){ \ if(!vm->is_int_or_float(args[0], args[1])) \ vm->typeError("unsupported operand type(s) for " #op ); \ if(args._index(0)->is_type(vm->_tp_int) && args._index(1)->is_type(vm->_tp_int)){ \ @@ -27,7 +27,7 @@ _Code VM::compile(_Str source, _Str filename, CompileMode mode) { }); #define BIND_NUM_LOGICAL_OPT(name, op, is_eq) \ - _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){ \ + _vm->bindMethodMulti<1>({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){ \ if(!vm->is_int_or_float(args[0], args[1])){ \ if constexpr(is_eq) return vm->PyBool(args[0] == args[1]); \ vm->typeError("unsupported operand type(s) for " #op ); \ @@ -50,57 +50,48 @@ void __initializeBuiltinFunctions(VM* _vm) { #undef BIND_NUM_ARITH_OPT #undef BIND_NUM_LOGICAL_OPT - _vm->bindBuiltinFunc("__sys_stdout_write", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindBuiltinFunc<1>("__sys_stdout_write", [](VM* vm, const pkpy::ArgList& args) { (*vm->_stdout) << vm->PyStr_AS_C(args[0]); return vm->None; }); - _vm->bindBuiltinFunc("super", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 0); + _vm->bindBuiltinFunc<0>("super", [](VM* vm, const pkpy::ArgList& args) { auto it = vm->top_frame()->f_locals.find(m_self); if(it == vm->top_frame()->f_locals.end()) vm->typeError("super() can only be called in a class method"); return vm->new_object(vm->_tp_super, it->second); }); - _vm->bindBuiltinFunc("eval", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindBuiltinFunc<1>("eval", [](VM* vm, const pkpy::ArgList& args) { const _Str& expr = vm->PyStr_AS_C(args[0]); _Code code = vm->compile(expr, "", EVAL_MODE); return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->f_locals_copy()); }); - _vm->bindBuiltinFunc("repr", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindBuiltinFunc<1>("repr", [](VM* vm, const pkpy::ArgList& args) { return vm->asRepr(args[0]); }); - _vm->bindBuiltinFunc("hash", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindBuiltinFunc<1>("hash", [](VM* vm, const pkpy::ArgList& args) { return vm->PyInt(vm->hash(args[0])); }); - _vm->bindBuiltinFunc("len", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindBuiltinFunc<1>("len", [](VM* vm, const pkpy::ArgList& args) { return vm->call(args[0], __len__, pkpy::noArg()); }); - _vm->bindBuiltinFunc("chr", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindBuiltinFunc<1>("chr", [](VM* vm, const pkpy::ArgList& args) { i64 i = vm->PyInt_AS_C(args[0]); if (i < 0 || i > 128) vm->valueError("chr() arg not in range(128)"); return vm->PyStr(std::string(1, (char)i)); }); - _vm->bindBuiltinFunc("ord", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindBuiltinFunc<1>("ord", [](VM* vm, const pkpy::ArgList& args) { _Str s = vm->PyStr_AS_C(args[0]); if (s.size() != 1) vm->typeError("ord() expected an ASCII character"); return vm->PyInt((i64)s[0]); }); - _vm->bindBuiltinFunc("globals", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 0); + _vm->bindBuiltinFunc<0>("globals", [](VM* vm, const pkpy::ArgList& args) { const auto& d = vm->top_frame()->f_globals(); PyVar obj = vm->call(vm->builtins->attribs["dict"]); for (const auto& [k, v] : d) { @@ -109,8 +100,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return obj; }); - _vm->bindBuiltinFunc("locals", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 0); + _vm->bindBuiltinFunc<0>("locals", [](VM* vm, const pkpy::ArgList& args) { const auto& d = vm->top_frame()->f_locals; PyVar obj = vm->call(vm->builtins->attribs["dict"]); for (const auto& [k, v] : d) { @@ -119,15 +109,13 @@ void __initializeBuiltinFunctions(VM* _vm) { return obj; }); - _vm->bindBuiltinFunc("hex", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindBuiltinFunc<1>("hex", [](VM* vm, const pkpy::ArgList& args) { std::stringstream ss; ss << std::hex << vm->PyInt_AS_C(args[0]); return vm->PyStr("0x" + ss.str()); }); - _vm->bindBuiltinFunc("dir", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindBuiltinFunc<1>("dir", [](VM* vm, const pkpy::ArgList& args) { std::vector<_Str> names; for (auto& [k, _] : args[0]->attribs) names.push_back(k); for (auto& [k, _] : args[0]->_type->attribs) { @@ -142,7 +130,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyList(ret); }); - _vm->bindMethod("object", "__repr__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("object", "__repr__", [](VM* vm, const pkpy::ArgList& args) { PyVar _self = args[0]; std::stringstream ss; ss << std::hex << (uintptr_t)_self.get(); @@ -150,17 +138,15 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyStr(s); }); - _vm->bindMethod("type", "__new__", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindStaticMethod<1>("type", "__new__", [](VM* vm, const pkpy::ArgList& args) { return args[0]->_type; }); - _vm->bindMethod("type", "__eq__", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2, true); + _vm->bindMethod<1>("type", "__eq__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyBool(args[0] == args[1]); }); - _vm->bindMethod("range", "__new__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindStaticMethod<-1>("range", "__new__", [](VM* vm, const pkpy::ArgList& args) { _Range r; switch (args.size()) { case 1: r.stop = vm->PyInt_AS_C(args[0]); break; @@ -171,26 +157,25 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyRange(r); }); - _vm->bindMethod("range", "__iter__", [](VM* vm, const pkpy::ArgList& args) { - vm->check_type(args[0], vm->_tp_range); + _vm->bindMethod<0>("range", "__iter__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyIter( pkpy::make_shared(vm, args[0]) ); }); - _vm->bindMethod("NoneType", "__repr__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("NoneType", "__repr__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyStr("None"); }); - _vm->bindMethod("NoneType", "__json__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("NoneType", "__json__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyStr("null"); }); - _vm->bindMethod("NoneType", "__eq__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("NoneType", "__eq__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyBool(args[0] == args[1]); }); - _vm->bindMethodMulti({"int", "float"}, "__truediv__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethodMulti<1>({"int", "float"}, "__truediv__", [](VM* vm, const pkpy::ArgList& args) { if(!vm->is_int_or_float(args[0], args[1])) vm->typeError("unsupported operand type(s) for " "/" ); f64 rhs = vm->num_to_float(args[1]); @@ -198,7 +183,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyFloat(vm->num_to_float(args[0]) / rhs); }); - _vm->bindMethodMulti({"int", "float"}, "__pow__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethodMulti<1>({"int", "float"}, "__pow__", [](VM* vm, const pkpy::ArgList& args) { if(!vm->is_int_or_float(args[0], args[1])) vm->typeError("unsupported operand type(s) for " "**" ); if(args[0]->is_type(vm->_tp_int) && args[1]->is_type(vm->_tp_int)){ @@ -209,9 +194,7 @@ void __initializeBuiltinFunctions(VM* _vm) { }); /************ PyInt ************/ - _vm->bindMethod("int", "__new__", [](VM* vm, const pkpy::ArgList& args) { - if(args.size() == 0) return vm->PyInt(0); - vm->check_args_size(args, 1); + _vm->bindStaticMethod<1>("int", "__new__", [](VM* vm, const pkpy::ArgList& args) { if (args[0]->is_type(vm->_tp_int)) return args[0]; if (args[0]->is_type(vm->_tp_float)) return vm->PyInt((i64)vm->PyFloat_AS_C(args[0])); if (args[0]->is_type(vm->_tp_bool)) return vm->PyInt(vm->PyBool_AS_C(args[0]) ? 1 : 0); @@ -230,7 +213,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->None; }); - _vm->bindMethod("int", "__floordiv__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("int", "__floordiv__", [](VM* vm, const pkpy::ArgList& args) { if(!args[0]->is_type(vm->_tp_int) || !args[1]->is_type(vm->_tp_int)) vm->typeError("unsupported operand type(s) for " "//" ); i64 rhs = vm->PyInt_AS_C(args._index(1)); @@ -238,7 +221,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyInt(vm->PyInt_AS_C(args._index(0)) / rhs); }); - _vm->bindMethod("int", "__mod__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("int", "__mod__", [](VM* vm, const pkpy::ArgList& args) { if(!args[0]->is_type(vm->_tp_int) || !args[1]->is_type(vm->_tp_int)) vm->typeError("unsupported operand type(s) for " "%" ); i64 rhs = vm->PyInt_AS_C(args._index(1)); @@ -246,17 +229,16 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyInt(vm->PyInt_AS_C(args._index(0)) % rhs); }); - _vm->bindMethod("int", "__repr__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("int", "__repr__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0]))); }); - _vm->bindMethod("int", "__json__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("int", "__json__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyStr(std::to_string((int)vm->PyInt_AS_C(args[0]))); }); #define __INT_BITWISE_OP(name,op) \ - _vm->bindMethod("int", #name, [](VM* vm, const pkpy::ArgList& args) { \ - vm->check_args_size(args, 2, true); \ + _vm->bindMethod<1>("int", #name, [](VM* vm, const pkpy::ArgList& args) { \ return vm->PyInt(vm->PyInt_AS_C(args._index(0)) op vm->PyInt_AS_C(args._index(1))); \ }); @@ -269,9 +251,7 @@ void __initializeBuiltinFunctions(VM* _vm) { #undef __INT_BITWISE_OP /************ PyFloat ************/ - _vm->bindMethod("float", "__new__", [](VM* vm, const pkpy::ArgList& args) { - if(args.size() == 0) return vm->PyFloat(0.0); - vm->check_args_size(args, 1); + _vm->bindStaticMethod<1>("float", "__new__", [](VM* vm, const pkpy::ArgList& args) { if (args[0]->is_type(vm->_tp_int)) return vm->PyFloat((f64)vm->PyInt_AS_C(args[0])); if (args[0]->is_type(vm->_tp_float)) return args[0]; if (args[0]->is_type(vm->_tp_bool)) return vm->PyFloat(vm->PyBool_AS_C(args[0]) ? 1.0 : 0.0); @@ -290,7 +270,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->None; }); - _vm->bindMethod("float", "__repr__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("float", "__repr__", [](VM* vm, const pkpy::ArgList& args) { f64 val = vm->PyFloat_AS_C(args[0]); if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val)); _StrStream ss; @@ -300,60 +280,59 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyStr(s); }); - _vm->bindMethod("float", "__json__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("float", "__json__", [](VM* vm, const pkpy::ArgList& args) { f64 val = vm->PyFloat_AS_C(args[0]); if(std::isinf(val) || std::isnan(val)) vm->valueError("cannot jsonify 'nan' or 'inf'"); return vm->PyStr(std::to_string(val)); }); /************ PyString ************/ - _vm->bindMethod("str", "__new__", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindStaticMethod<1>("str", "__new__", [](VM* vm, const pkpy::ArgList& args) { return vm->asStr(args._index(0)); }); - _vm->bindMethod("str", "__add__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("str", "__add__", [](VM* vm, const pkpy::ArgList& args) { const _Str& lhs = vm->PyStr_AS_C(args[0]); const _Str& rhs = vm->PyStr_AS_C(args[1]); return vm->PyStr(lhs + rhs); }); - _vm->bindMethod("str", "__len__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("str", "__len__", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); return vm->PyInt(_self.u8_length()); }); - _vm->bindMethod("str", "__contains__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("str", "__contains__", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); const _Str& _other = vm->PyStr_AS_C(args[1]); return vm->PyBool(_self.find(_other) != _Str::npos); }); - _vm->bindMethod("str", "__str__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("str", "__str__", [](VM* vm, const pkpy::ArgList& args) { return args[0]; // str is immutable }); - _vm->bindMethod("str", "__iter__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("str", "__iter__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyIter(pkpy::make_shared(vm, args[0])); }); - _vm->bindMethod("str", "__repr__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("str", "__repr__", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); return vm->PyStr(_self.__escape(true)); }); - _vm->bindMethod("str", "__json__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("str", "__json__", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); return vm->PyStr(_self.__escape(false)); }); - _vm->bindMethod("str", "__eq__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("str", "__eq__", [](VM* vm, const pkpy::ArgList& args) { if(args[0]->is_type(vm->_tp_str) && args[1]->is_type(vm->_tp_str)) return vm->PyBool(vm->PyStr_AS_C(args[0]) == vm->PyStr_AS_C(args[1])); return vm->PyBool(args[0] == args[1]); // fallback }); - _vm->bindMethod("str", "__getitem__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("str", "__getitem__", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self (vm->PyStr_AS_C(args[0])); if(args[1]->is_type(vm->_tp_slice)){ @@ -367,20 +346,19 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyStr(_self.u8_getitem(_index)); }); - _vm->bindMethod("str", "__gt__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("str", "__gt__", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self (vm->PyStr_AS_C(args[0])); const _Str& _obj (vm->PyStr_AS_C(args[1])); return vm->PyBool(_self > _obj); }); - _vm->bindMethod("str", "__lt__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("str", "__lt__", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self (vm->PyStr_AS_C(args[0])); const _Str& _obj (vm->PyStr_AS_C(args[1])); return vm->PyBool(_self < _obj); }); - _vm->bindMethod("str", "replace", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 3, true); + _vm->bindMethod<2>("str", "replace", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); const _Str& _old = vm->PyStr_AS_C(args[1]); const _Str& _new = vm->PyStr_AS_C(args[2]); @@ -394,22 +372,19 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyStr(_copy); }); - _vm->bindMethod("str", "startswith", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2, true); + _vm->bindMethod<1>("str", "startswith", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); const _Str& _prefix = vm->PyStr_AS_C(args[1]); return vm->PyBool(_self.find(_prefix) == 0); }); - _vm->bindMethod("str", "endswith", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2, true); + _vm->bindMethod<1>("str", "endswith", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); const _Str& _suffix = vm->PyStr_AS_C(args[1]); return vm->PyBool(_self.rfind(_suffix) == _self.length() - _suffix.length()); }); - _vm->bindMethod("str", "join", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2, true); + _vm->bindMethod<1>("str", "join", [](VM* vm, const pkpy::ArgList& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); PyVarList* _list; if(args[1]->is_type(vm->_tp_list)){ @@ -428,22 +403,19 @@ void __initializeBuiltinFunctions(VM* _vm) { }); /************ PyList ************/ - _vm->bindMethod("list", "__iter__", [](VM* vm, const pkpy::ArgList& args) { - vm->check_type(args[0], vm->_tp_list); + _vm->bindMethod<0>("list", "__iter__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyIter( pkpy::make_shared(vm, args[0]) ); }); - _vm->bindMethod("list", "append", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2, true); + _vm->bindMethod<1>("list", "append", [](VM* vm, const pkpy::ArgList& args) { PyVarList& _self = vm->PyList_AS_C(args[0]); _self.push_back(args._index(1)); return vm->None; }); - _vm->bindMethod("list", "insert", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 3, true); + _vm->bindMethod<2>("list", "insert", [](VM* vm, const pkpy::ArgList& args) { PyVarList& _self = vm->PyList_AS_C(args[0]); int _index = (int)vm->PyInt_AS_C(args[1]); if(_index < 0) _index += _self.size(); @@ -453,18 +425,16 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->None; }); - _vm->bindMethod("list", "clear", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1, true); + _vm->bindMethod<0>("list", "clear", [](VM* vm, const pkpy::ArgList& args) { vm->PyList_AS_C(args[0]).clear(); return vm->None; }); - _vm->bindMethod("list", "copy", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1, true); + _vm->bindMethod<0>("list", "copy", [](VM* vm, const pkpy::ArgList& args) { return vm->PyList(vm->PyList_AS_C(args[0])); }); - _vm->bindMethod("list", "__add__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("list", "__add__", [](VM* vm, const pkpy::ArgList& args) { const PyVarList& _self = vm->PyList_AS_C(args[0]); const PyVarList& _obj = vm->PyList_AS_C(args[1]); PyVarList _new_list = _self; @@ -472,12 +442,12 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyList(_new_list); }); - _vm->bindMethod("list", "__len__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("list", "__len__", [](VM* vm, const pkpy::ArgList& args) { const PyVarList& _self = vm->PyList_AS_C(args[0]); return vm->PyInt(_self.size()); }); - _vm->bindMethod("list", "__getitem__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("list", "__getitem__", [](VM* vm, const pkpy::ArgList& args) { const PyVarList& _self = vm->PyList_AS_C(args[0]); if(args[1]->is_type(vm->_tp_slice)){ @@ -494,7 +464,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return _self[_index]; }); - _vm->bindMethod("list", "__setitem__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<2>("list", "__setitem__", [](VM* vm, const pkpy::ArgList& args) { PyVarList& _self = vm->PyList_AS_C(args[0]); int _index = (int)vm->PyInt_AS_C(args[1]); _index = vm->normalizedIndex(_index, _self.size()); @@ -502,7 +472,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->None; }); - _vm->bindMethod("list", "__delitem__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("list", "__delitem__", [](VM* vm, const pkpy::ArgList& args) { PyVarList& _self = vm->PyList_AS_C(args[0]); int _index = (int)vm->PyInt_AS_C(args[1]); _index = vm->normalizedIndex(_index, _self.size()); @@ -511,25 +481,21 @@ void __initializeBuiltinFunctions(VM* _vm) { }); /************ PyTuple ************/ - _vm->bindMethod("tuple", "__new__", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindStaticMethod<1>("tuple", "__new__", [](VM* vm, const pkpy::ArgList& args) { PyVarList _list = vm->PyList_AS_C(vm->call(vm->builtins->attribs["list"], args)); return vm->PyTuple(_list); }); - _vm->bindMethod("tuple", "__iter__", [](VM* vm, const pkpy::ArgList& args) { - vm->check_type(args[0], vm->_tp_tuple); - return vm->PyIter( - pkpy::make_shared(vm, args[0]) - ); + _vm->bindMethod<0>("tuple", "__iter__", [](VM* vm, const pkpy::ArgList& args) { + return vm->PyIter(pkpy::make_shared(vm, args[0])); }); - _vm->bindMethod("tuple", "__len__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("tuple", "__len__", [](VM* vm, const pkpy::ArgList& args) { const PyVarList& _self = vm->PyTuple_AS_C(args[0]); return vm->PyInt(_self.size()); }); - _vm->bindMethod("tuple", "__getitem__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("tuple", "__getitem__", [](VM* vm, const pkpy::ArgList& args) { const PyVarList& _self = vm->PyTuple_AS_C(args[0]); int _index = (int)vm->PyInt_AS_C(args[1]); _index = vm->normalizedIndex(_index, _self.size()); @@ -537,52 +503,33 @@ void __initializeBuiltinFunctions(VM* _vm) { }); /************ PyBool ************/ - _vm->bindMethod("bool", "__new__", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + _vm->bindStaticMethod<1>("bool", "__new__", [](VM* vm, const pkpy::ArgList& args) { return vm->asBool(args[0]); }); - _vm->bindMethod("bool", "__repr__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("bool", "__repr__", [](VM* vm, const pkpy::ArgList& args) { bool val = vm->PyBool_AS_C(args[0]); return vm->PyStr(val ? "True" : "False"); }); - _vm->bindMethod("bool", "__json__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("bool", "__json__", [](VM* vm, const pkpy::ArgList& args) { bool val = vm->PyBool_AS_C(args[0]); return vm->PyStr(val ? "true" : "false"); }); - _vm->bindMethod("bool", "__eq__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("bool", "__eq__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyBool(args[0] == args[1]); }); - _vm->bindMethod("bool", "__xor__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<1>("bool", "__xor__", [](VM* vm, const pkpy::ArgList& args) { bool _self = vm->PyBool_AS_C(args[0]); bool _obj = vm->PyBool_AS_C(args[1]); return vm->PyBool(_self ^ _obj); }); - _vm->bindMethod("ellipsis", "__repr__", [](VM* vm, const pkpy::ArgList& args) { + _vm->bindMethod<0>("ellipsis", "__repr__", [](VM* vm, const pkpy::ArgList& args) { return vm->PyStr("Ellipsis"); }); - - _vm->bindMethod("_native_function", "__call__", [](VM* vm, const pkpy::ArgList& args) { - const _CppFunc& _self = vm->PyNativeFunction_AS_C(args[0]); - return _self(vm, args.subList(1)); - }); - - _vm->bindMethod("function", "__call__", [](VM* vm, const pkpy::ArgList& args) { - return vm->call(args[0], args.subList(1)); - }); - - _vm->bindMethod("_bounded_method", "__call__", [](VM* vm, const pkpy::ArgList& args) { - vm->check_type(args[0], vm->_tp_bounded_method); - const _BoundedMethod& _self = vm->PyBoundedMethod_AS_C(args[0]); - pkpy::ArgList newArgs(args.size()); - newArgs[0] = _self.obj; - for(int i = 1; i < args.size(); i++) newArgs[i] = args[i]; - return vm->call(_self.method, newArgs); - }); } #include "builtins.h" @@ -601,7 +548,7 @@ void __initializeBuiltinFunctions(VM* _vm) { void __addModuleTime(VM* vm){ PyVar mod = vm->newModule("time"); - vm->bindFunc(mod, "time", [](VM* vm, const pkpy::ArgList& args) { + vm->bindFunc<0>(mod, "time", [](VM* vm, const pkpy::ArgList& args) { auto now = std::chrono::high_resolution_clock::now(); return vm->PyFloat(std::chrono::duration_cast(now.time_since_epoch()).count() / 1000000.0); }); @@ -609,18 +556,15 @@ void __addModuleTime(VM* vm){ void __addModuleSys(VM* vm){ PyVar mod = vm->newModule("sys"); - vm->bindFunc(mod, "getrefcount", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "getrefcount", [](VM* vm, const pkpy::ArgList& args) { return vm->PyInt(args[0].use_count()); }); - vm->bindFunc(mod, "getrecursionlimit", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 0); + vm->bindFunc<0>(mod, "getrecursionlimit", [](VM* vm, const pkpy::ArgList& args) { return vm->PyInt(vm->maxRecursionDepth); }); - vm->bindFunc(mod, "setrecursionlimit", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "setrecursionlimit", [](VM* vm, const pkpy::ArgList& args) { vm->maxRecursionDepth = (int)vm->PyInt_AS_C(args[0]); return vm->None; }); @@ -630,15 +574,13 @@ void __addModuleSys(VM* vm){ void __addModuleJson(VM* vm){ PyVar mod = vm->newModule("json"); - vm->bindFunc(mod, "loads", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "loads", [](VM* vm, const pkpy::ArgList& args) { const _Str& expr = vm->PyStr_AS_C(args[0]); _Code code = vm->compile(expr, "", JSON_MODE); return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->f_locals_copy()); }); - vm->bindFunc(mod, "dumps", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "dumps", [](VM* vm, const pkpy::ArgList& args) { return vm->asJson(args[0]); }); } @@ -648,63 +590,88 @@ void __addModuleMath(VM* vm){ vm->setattr(mod, "pi", vm->PyFloat(3.1415926535897932384)); vm->setattr(mod, "e" , vm->PyFloat(2.7182818284590452354)); - vm->bindFunc(mod, "log", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "log", [](VM* vm, const pkpy::ArgList& args) { return vm->PyFloat(log(vm->num_to_float(args[0]))); }); - vm->bindFunc(mod, "log10", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "log10", [](VM* vm, const pkpy::ArgList& args) { return vm->PyFloat(log10(vm->num_to_float(args[0]))); }); - vm->bindFunc(mod, "log2", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "log2", [](VM* vm, const pkpy::ArgList& args) { return vm->PyFloat(log2(vm->num_to_float(args[0]))); }); - vm->bindFunc(mod, "sin", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "sin", [](VM* vm, const pkpy::ArgList& args) { return vm->PyFloat(sin(vm->num_to_float(args[0]))); }); - vm->bindFunc(mod, "cos", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "cos", [](VM* vm, const pkpy::ArgList& args) { return vm->PyFloat(cos(vm->num_to_float(args[0]))); }); - vm->bindFunc(mod, "tan", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "tan", [](VM* vm, const pkpy::ArgList& args) { return vm->PyFloat(tan(vm->num_to_float(args[0]))); }); - vm->bindFunc(mod, "isclose", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2); + vm->bindFunc<2>(mod, "isclose", [](VM* vm, const pkpy::ArgList& args) { f64 a = vm->num_to_float(args[0]); f64 b = vm->num_to_float(args[1]); return vm->PyBool(fabs(a - b) < 1e-9); }); - vm->bindFunc(mod, "isnan", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "isnan", [](VM* vm, const pkpy::ArgList& args) { return vm->PyBool(std::isnan(vm->num_to_float(args[0]))); }); - vm->bindFunc(mod, "isinf", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1); + vm->bindFunc<1>(mod, "isinf", [](VM* vm, const pkpy::ArgList& args) { return vm->PyBool(std::isinf(vm->num_to_float(args[0]))); }); } +#define PY_CLASS(mod, name) inline static PyVar _tp(VM* vm) { return vm->_modules[#mod]->attribs[#name]; } + struct ReMatch { + PY_CLASS(re, Match) + i64 start; i64 end; std::smatch m; ReMatch(i64 start, i64 end, std::smatch m) : start(start), end(end), m(m) {} + + static PyVar _bind(VM* vm){ + PyVar _tp_match = vm->new_user_type_object(vm->_modules["re"], "Match", vm->_tp_object); + vm->bindMethod<0>(_tp_match, "start", [](VM* vm, const pkpy::ArgList& args) { + return vm->PyInt(UNION_GET(ReMatch, args[0]).start); + }); + + vm->bindMethod<0>(_tp_match, "end", [](VM* vm, const pkpy::ArgList& args) { + return vm->PyInt(UNION_GET(ReMatch, args[0]).end); + }); + + vm->bindMethod<0>(_tp_match, "span", [](VM* vm, const pkpy::ArgList& args) { + auto& m = UNION_GET(ReMatch, args[0]); + PyVarList vec = { vm->PyInt(m.start), vm->PyInt(m.end) }; + return vm->PyTuple(vec); + }); + + vm->bindMethod<1>(_tp_match, "group", [](VM* vm, const pkpy::ArgList& args) { + auto& m = UNION_GET(ReMatch, args[0]); + int index = (int)vm->PyInt_AS_C(args[1]); + vm->normalizedIndex(index, m.m.size()); + return vm->PyStr(m.m[index].str()); + }); + return _tp_match; + } }; +// C绑定过程 +// 一个C原生类 +// static PyVar _tp(VM* vm); 实现从VM中获取该类的类型对象 +// static PyVar _bind(VM* vm); 实现初始化该类的绑定,在VM中注册该类的类型对象,并返回生成的类型对象 + PyVar __regex_search(const _Str& pattern, const _Str& string, bool fromStart, VM* vm){ std::regex re(pattern); std::smatch m; @@ -712,55 +679,28 @@ PyVar __regex_search(const _Str& pattern, const _Str& string, bool fromStart, VM if(fromStart && m.position() != 0) return vm->None; i64 start = string.__to_u8_index(m.position()); i64 end = string.__to_u8_index(m.position() + m.length()); - return vm->new_object(vm->_userTypes["re.Match"], ReMatch(start, end, m)); + return vm->new_object_c(start, end, m); } return vm->None; }; void __addModuleRe(VM* vm){ PyVar mod = vm->newModule("re"); - PyVar _tp_match = vm->new_user_type_object(mod, "Match", vm->_tp_object); - vm->bindMethod("re.Match", "start", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1, true); - return vm->PyInt(UNION_GET(ReMatch, args[0]).start); - }); + ReMatch::_bind(vm); - vm->bindMethod("re.Match", "end", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1, true); - return vm->PyInt(UNION_GET(ReMatch, args[0]).end); - }); - - vm->bindMethod("re.Match", "span", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 1, true); - auto& m = UNION_GET(ReMatch, args[0]); - PyVarList vec = { vm->PyInt(m.start), vm->PyInt(m.end) }; - return vm->PyTuple(vec); - }); - - vm->bindMethod("re.Match", "group", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2, true); - auto& m = UNION_GET(ReMatch, args[0]); - int index = (int)vm->PyInt_AS_C(args[1]); - vm->normalizedIndex(index, m.m.size()); - return vm->PyStr(m.m[index].str()); - }); - - vm->bindFunc(mod, "match", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2); + vm->bindFunc<2>(mod, "match", [](VM* vm, const pkpy::ArgList& args) { const _Str& pattern = vm->PyStr_AS_C(args[0]); const _Str& string = vm->PyStr_AS_C(args[1]); return __regex_search(pattern, string, true, vm); }); - vm->bindFunc(mod, "search", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2); + vm->bindFunc<2>(mod, "search", [](VM* vm, const pkpy::ArgList& args) { const _Str& pattern = vm->PyStr_AS_C(args[0]); const _Str& string = vm->PyStr_AS_C(args[1]); return __regex_search(pattern, string, false, vm); }); - vm->bindFunc(mod, "sub", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 3); + vm->bindFunc<3>(mod, "sub", [](VM* vm, const pkpy::ArgList& args) { const _Str& pattern = vm->PyStr_AS_C(args[0]); const _Str& repl = vm->PyStr_AS_C(args[1]); const _Str& string = vm->PyStr_AS_C(args[2]); @@ -768,8 +708,7 @@ void __addModuleRe(VM* vm){ return vm->PyStr(std::regex_replace(string, re, repl)); }); - vm->bindFunc(mod, "split", [](VM* vm, const pkpy::ArgList& args) { - vm->check_args_size(args, 2); + vm->bindFunc<2>(mod, "split", [](VM* vm, const pkpy::ArgList& args) { const _Str& pattern = vm->PyStr_AS_C(args[0]); const _Str& string = vm->PyStr_AS_C(args[1]); std::regex re(pattern); diff --git a/src/vm.h b/src/vm.h index 64e63db3..67d819ab 100644 --- a/src/vm.h +++ b/src/vm.h @@ -22,7 +22,6 @@ class VM { std::vector _small_integers; // [-5, 256] - PyVarDict _modules; // loaded modules emhash8::HashMap<_Str, _Str> _lazy_modules; // lazy loaded modules protected: std::deque< std::unique_ptr > callstack; @@ -340,7 +339,7 @@ protected: public: PyVarDict _types; - PyVarDict _userTypes; + PyVarDict _modules; // loaded modules PyVar None, True, False, Ellipsis; bool use_stdio; @@ -578,7 +577,6 @@ public: setattr(obj, __base__, base); _Str fullName = UNION_NAME(mod) + "." +name; setattr(obj, __name__, PyStr(fullName)); - _userTypes[fullName] = obj; setattr(mod, name, obj); return obj; } @@ -597,6 +595,11 @@ public: return pkpy::make_shared>(_value, type); } + template + inline PyVar new_object_c(Args&&... args) { + return new_object(T::_tp(this), T(std::forward(args)...)); + } + PyVar newModule(_Str name) { PyVar obj = new_object(_tp_module, (i64)-2); setattr(obj, __name__, PyStr(name)); @@ -658,28 +661,35 @@ public: setattr(obj.get(), name, value); } - void bindMethod(_Str typeName, _Str funcName, _CppFunc fn) { - PyVar* type = _types.try_get(typeName); - if(type == nullptr) type = _userTypes.try_get(typeName); - if(type == nullptr) UNREACHABLE(); - PyVar func = PyNativeFunction(fn); - setattr(*type, funcName, func); + template + void bindMethod(PyVar obj, _Str funcName, _CppFuncRaw fn) { + check_type(obj, _tp_type); + setattr(obj, funcName, PyNativeFunction(_CppFunc(fn, ARGC, true))); } - void bindMethodMulti(std::vector<_Str> typeNames, _Str funcName, _CppFunc fn) { - for(auto& typeName : typeNames){ - bindMethod(typeName, funcName, fn); - } + template + void bindFunc(PyVar obj, _Str funcName, _CppFuncRaw fn) { + setattr(obj, funcName, PyNativeFunction(_CppFunc(fn, ARGC, false))); } - void bindBuiltinFunc(_Str funcName, _CppFunc fn) { - bindFunc(builtins, funcName, fn); + template + void bindMethod(_Str typeName, _Str funcName, _CppFuncRaw fn) { + bindMethod(_types[typeName], funcName, fn); } - void bindFunc(PyVar module, _Str funcName, _CppFunc fn) { - check_type(module, _tp_module); - PyVar func = PyNativeFunction(fn); - setattr(module, funcName, func); + template + void bindStaticMethod(_Str typeName, _Str funcName, _CppFuncRaw fn) { + bindFunc(_types[typeName], funcName, fn); + } + + template + void bindMethodMulti(std::vector<_Str> typeNames, _Str funcName, _CppFuncRaw fn) { + for(auto& typeName : typeNames) bindMethod(typeName, funcName, fn); + } + + template + void bindBuiltinFunc(_Str funcName, _CppFuncRaw fn) { + bindFunc(builtins, funcName, fn); } inline bool is_int_or_float(const PyVar& obj) const{ @@ -848,8 +858,8 @@ public: _tp_bounded_method = new_type_object("_bounded_method"); _tp_super = new_type_object("super"); - this->None = new_object(_types["NoneType"], (i64)0); - this->Ellipsis = new_object(_types["ellipsis"], (i64)0); + this->None = new_object(_types["NoneType"], DUMMY_VAL); + this->Ellipsis = new_object(_types["ellipsis"], DUMMY_VAL); this->True = new_object(_tp_bool, true); this->False = new_object(_tp_bool, false); this->builtins = newModule("builtins"); @@ -925,12 +935,6 @@ public: if(!obj->is_type(type)) typeError("expected '" + UNION_NAME(type) + "', but got '" + UNION_TP_NAME(obj) + "'"); } - inline void check_args_size(const pkpy::ArgList& args, int size, bool method=false){ - if(args.size() == size) return; - if(method) typeError(args.size()>size ? "too many arguments" : "too few arguments"); - else typeError("expected " + std::to_string(size) + " arguments, but got " + std::to_string(args.size())); - } - virtual ~VM() { if(!use_stdio){ delete _stdout; @@ -1058,4 +1062,12 @@ PyVar RangeIterator::next(){ PyVar StringIterator::next(){ return vm->PyStr(str.u8_getitem(index++)); +} + +PyVar _CppFunc::operator()(VM* vm, const pkpy::ArgList& args) const{ + int args_size = args.size() - (int)method; // remove self + if(argc != -1 && args_size != argc) { + vm->typeError("expected " + std::to_string(argc) + " arguments, but got " + std::to_string(args_size)); + } + return f(vm, args); } \ No newline at end of file