From ef04932ec9dd8ed64b54a49023579a9eb5b22e97 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 8 Feb 2023 13:11:36 +0800 Subject: [PATCH] some refactor --- src/error.h | 2 +- src/main.cpp | 2 +- src/obj.h | 1 + src/parser.h | 2 +- src/pocketpy.h | 238 ++++++++++++++++++++++++------------------------- src/str.h | 48 +++++----- src/vm.h | 25 +++--- 7 files changed, 160 insertions(+), 158 deletions(-) diff --git a/src/error.h b/src/error.h index 1094a542..91369578 100644 --- a/src/error.h +++ b/src/error.h @@ -51,7 +51,7 @@ struct SourceData { _Str line = ""; int removedSpaces = 0; if(pair.first && pair.second){ - line = _Str(pair.first, pair.second-pair.first).__lstrip(); + line = _Str(pair.first, pair.second-pair.first).lstrip(); removedSpaces = pair.second - pair.first - line.size(); if(line.empty()) line = ""; } diff --git a/src/main.cpp b/src/main.cpp index 02e32488..97798339 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,7 @@ struct Timer{ int main(int argc, char** argv){ VM* vm = pkpy_new_vm(true); - vm->bindBuiltinFunc<0>("input", [](VM* vm, const pkpy::Args& args){ + vm->bind_builtin_func<0>("input", [](VM* vm, const pkpy::Args& args){ static std::string line; std::getline(std::cin, line); return vm->PyStr(line); diff --git a/src/obj.h b/src/obj.h index 175dd2b0..431c7e04 100644 --- a/src/obj.h +++ b/src/obj.h @@ -100,6 +100,7 @@ struct Py_ : PyObject { #define PY_CLASS(mod, name) \ inline static PyVar _type(VM* vm) { return vm->_modules[#mod]->attribs[#name]; } \ + inline static const char* _mod() { return #mod; } \ inline static const char* _name() { return #name; } #define PY_BUILTIN_CLASS(name) inline static PyVar _type(VM* vm) { return vm->_tp_##name; } \ No newline at end of file diff --git a/src/parser.h b/src/parser.h index 18f56188..9f67a891 100644 --- a/src/parser.h +++ b/src/parser.h @@ -210,7 +210,7 @@ struct Parser { value |= (b & 0b00111111) << (6*(u8bytes-k-1)); } } - if(__isLoChar(value)) curr_char += u8bytes; + if(is_unicode_Lo_char(value)) curr_char += u8bytes; else break; } diff --git a/src/pocketpy.h b/src/pocketpy.h index cf757c41..ec8d2fb3 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -15,7 +15,7 @@ _Code VM::compile(_Str source, _Str filename, CompileMode mode) { } #define BIND_NUM_ARITH_OPT(name, op) \ - _vm->bindMethodMulti<1>({"int","float"}, #name, [](VM* vm, const pkpy::Args& args){ \ + _vm->_bind_methods<1>({"int","float"}, #name, [](VM* vm, const pkpy::Args& args){ \ if(args[0]->is_type(vm->_tp_int) && args[1]->is_type(vm->_tp_int)){ \ return vm->PyInt(vm->PyInt_AS_C(args[0]) op vm->PyInt_AS_C(args[1])); \ }else{ \ @@ -24,7 +24,7 @@ _Code VM::compile(_Str source, _Str filename, CompileMode mode) { }); #define BIND_NUM_LOGICAL_OPT(name, op, is_eq) \ - _vm->bindMethodMulti<1>({"int","float"}, #name, [](VM* vm, const pkpy::Args& args){ \ + _vm->_bind_methods<1>({"int","float"}, #name, [](VM* vm, const pkpy::Args& args){ \ bool _0 = args[0]->is_type(vm->_tp_int) || args[0]->is_type(vm->_tp_float); \ bool _1 = args[1]->is_type(vm->_tp_int) || args[1]->is_type(vm->_tp_float); \ if(!_0 || !_1){ \ @@ -35,7 +35,7 @@ _Code VM::compile(_Str source, _Str filename, CompileMode mode) { }); -void __initializeBuiltinFunctions(VM* _vm) { +void init_builtins(VM* _vm) { BIND_NUM_ARITH_OPT(__add__, +) BIND_NUM_ARITH_OPT(__sub__, -) BIND_NUM_ARITH_OPT(__mul__, *) @@ -50,73 +50,73 @@ void __initializeBuiltinFunctions(VM* _vm) { #undef BIND_NUM_ARITH_OPT #undef BIND_NUM_LOGICAL_OPT - _vm->bindBuiltinFunc<1>("__sys_stdout_write", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<1>("__sys_stdout_write", [](VM* vm, const pkpy::Args& args) { (*vm->_stdout) << vm->PyStr_AS_C(args[0]); return vm->None; }); - _vm->bindBuiltinFunc<0>("super", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<0>("super", [](VM* vm, const pkpy::Args& 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<1>("eval", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<1>("eval", [](VM* vm, const pkpy::Args& args) { _Code code = vm->compile(vm->PyStr_AS_C(args[0]), "", EVAL_MODE); return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals); }); - _vm->bindBuiltinFunc<1>("exec", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<1>("exec", [](VM* vm, const pkpy::Args& args) { _Code code = vm->compile(vm->PyStr_AS_C(args[0]), "", EXEC_MODE); vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals); return vm->None; }); - _vm->bindBuiltinFunc<-1>("exit", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<-1>("exit", [](VM* vm, const pkpy::Args& args) { if(args.size() == 0) std::exit(0); else if(args.size() == 1) std::exit((int)vm->PyInt_AS_C(args[0])); else vm->typeError("exit() takes at most 1 argument"); return vm->None; }); - _vm->bindBuiltinFunc<1>("repr", CPP_LAMBDA(vm->asRepr(args[0]))); - _vm->bindBuiltinFunc<1>("hash", CPP_LAMBDA(vm->PyInt(vm->hash(args[0])))); - _vm->bindBuiltinFunc<1>("len", CPP_LAMBDA(vm->call(args[0], __len__, pkpy::noArg()))); + _vm->bind_builtin_func<1>("repr", CPP_LAMBDA(vm->asRepr(args[0]))); + _vm->bind_builtin_func<1>("hash", CPP_LAMBDA(vm->PyInt(vm->hash(args[0])))); + _vm->bind_builtin_func<1>("len", CPP_LAMBDA(vm->call(args[0], __len__, pkpy::noArg()))); - _vm->bindBuiltinFunc<1>("chr", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<1>("chr", [](VM* vm, const pkpy::Args& 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<1>("ord", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<1>("ord", [](VM* vm, const pkpy::Args& 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.c_str()[0])); }); - _vm->bindBuiltinFunc<2>("hasattr", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<2>("hasattr", [](VM* vm, const pkpy::Args& args) { return vm->PyBool(vm->getattr(args[0], vm->PyStr_AS_C(args[1]), false) != nullptr); }); - _vm->bindBuiltinFunc<3>("setattr", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<3>("setattr", [](VM* vm, const pkpy::Args& args) { PyVar obj = args[0]; vm->setattr(obj, vm->PyStr_AS_C(args[1]), args[2]); return vm->None; }); - _vm->bindBuiltinFunc<2>("getattr", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<2>("getattr", [](VM* vm, const pkpy::Args& args) { _Str name = vm->PyStr_AS_C(args[1]); return vm->getattr(args[0], name); }); - _vm->bindBuiltinFunc<1>("hex", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<1>("hex", [](VM* vm, const pkpy::Args& args) { std::stringstream ss; ss << std::hex << vm->PyInt_AS_C(args[0]); return vm->PyStr("0x" + ss.str()); }); - _vm->bindBuiltinFunc<1>("dir", [](VM* vm, const pkpy::Args& args) { + _vm->bind_builtin_func<1>("dir", [](VM* vm, const pkpy::Args& args) { std::vector<_Str> names; for (auto& [k, _] : args[0]->attribs) names.push_back(k); for (auto& [k, _] : args[0]->type->attribs) { @@ -131,7 +131,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyList(ret); }); - _vm->bindMethod<0>("object", "__repr__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("object", "__repr__", [](VM* vm, const pkpy::Args& args) { PyVar _self = args[0]; std::stringstream ss; ss << std::hex << (uintptr_t)_self.get(); @@ -139,12 +139,12 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyStr(s); }); - _vm->bindMethod<1>("object", "__eq__", CPP_LAMBDA(vm->PyBool(args[0] == args[1]))); - _vm->bindMethod<1>("object", "__ne__", CPP_LAMBDA(vm->PyBool(args[0] != args[1]))); + _vm->bind_method<1>("object", "__eq__", CPP_LAMBDA(vm->PyBool(args[0] == args[1]))); + _vm->bind_method<1>("object", "__ne__", CPP_LAMBDA(vm->PyBool(args[0] != args[1]))); - _vm->bindStaticMethod<1>("type", "__new__", CPP_LAMBDA(args[0]->type)); + _vm->bind_static_method<1>("type", "__new__", CPP_LAMBDA(args[0]->type)); - _vm->bindStaticMethod<-1>("range", "__new__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_static_method<-1>("range", "__new__", [](VM* vm, const pkpy::Args& args) { _Range r; switch (args.size()) { case 1: r.stop = vm->PyInt_AS_C(args[0]); break; @@ -155,20 +155,20 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyRange(r); }); - _vm->bindMethod<0>("range", "__iter__", CPP_LAMBDA( + _vm->bind_method<0>("range", "__iter__", CPP_LAMBDA( vm->PyIter(pkpy::make_shared(vm, args[0])) )); - _vm->bindMethod<0>("NoneType", "__repr__", CPP_LAMBDA(vm->PyStr("None"))); - _vm->bindMethod<0>("NoneType", "__json__", CPP_LAMBDA(vm->PyStr("null"))); + _vm->bind_method<0>("NoneType", "__repr__", CPP_LAMBDA(vm->PyStr("None"))); + _vm->bind_method<0>("NoneType", "__json__", CPP_LAMBDA(vm->PyStr("null"))); - _vm->bindMethodMulti<1>({"int", "float"}, "__truediv__", [](VM* vm, const pkpy::Args& args) { + _vm->_bind_methods<1>({"int", "float"}, "__truediv__", [](VM* vm, const pkpy::Args& args) { f64 rhs = vm->num_to_float(args[1]); if (rhs == 0) vm->zeroDivisionError(); return vm->PyFloat(vm->num_to_float(args[0]) / rhs); }); - _vm->bindMethodMulti<1>({"int", "float"}, "__pow__", [](VM* vm, const pkpy::Args& args) { + _vm->_bind_methods<1>({"int", "float"}, "__pow__", [](VM* vm, const pkpy::Args& args) { if(args[0]->is_type(vm->_tp_int) && args[1]->is_type(vm->_tp_int)){ return vm->PyInt((i64)round(pow(vm->PyInt_AS_C(args[0]), vm->PyInt_AS_C(args[1])))); }else{ @@ -177,7 +177,7 @@ void __initializeBuiltinFunctions(VM* _vm) { }); /************ PyInt ************/ - _vm->bindStaticMethod<1>("int", "__new__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_static_method<1>("int", "__new__", [](VM* vm, const pkpy::Args& 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); @@ -196,28 +196,28 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->None; }); - _vm->bindMethod<1>("int", "__floordiv__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("int", "__floordiv__", [](VM* vm, const pkpy::Args& args) { i64 rhs = vm->PyInt_AS_C(args[1]); if(rhs == 0) vm->zeroDivisionError(); return vm->PyInt(vm->PyInt_AS_C(args[0]) / rhs); }); - _vm->bindMethod<1>("int", "__mod__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("int", "__mod__", [](VM* vm, const pkpy::Args& args) { i64 rhs = vm->PyInt_AS_C(args[1]); if(rhs == 0) vm->zeroDivisionError(); return vm->PyInt(vm->PyInt_AS_C(args[0]) % rhs); }); - _vm->bindMethod<0>("int", "__repr__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("int", "__repr__", [](VM* vm, const pkpy::Args& args) { return vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0]))); }); - _vm->bindMethod<0>("int", "__json__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("int", "__json__", [](VM* vm, const pkpy::Args& args) { return vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0]))); }); #define __INT_BITWISE_OP(name,op) \ - _vm->bindMethod<1>("int", #name, [](VM* vm, const pkpy::Args& args) { \ + _vm->bind_method<1>("int", #name, [](VM* vm, const pkpy::Args& args) { \ return vm->PyInt(vm->PyInt_AS_C(args[0]) op vm->PyInt_AS_C(args[1])); \ }); @@ -230,7 +230,7 @@ void __initializeBuiltinFunctions(VM* _vm) { #undef __INT_BITWISE_OP /************ PyFloat ************/ - _vm->bindStaticMethod<1>("float", "__new__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_static_method<1>("float", "__new__", [](VM* vm, const pkpy::Args& 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); @@ -249,7 +249,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->None; }); - _vm->bindMethod<0>("float", "__repr__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("float", "__repr__", [](VM* vm, const pkpy::Args& 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; @@ -259,61 +259,61 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyStr(s); }); - _vm->bindMethod<0>("float", "__json__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("float", "__json__", [](VM* vm, const pkpy::Args& 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->bindStaticMethod<1>("str", "__new__", CPP_LAMBDA(vm->asStr(args[0]))); + _vm->bind_static_method<1>("str", "__new__", CPP_LAMBDA(vm->asStr(args[0]))); - _vm->bindMethod<1>("str", "__add__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "__add__", [](VM* vm, const pkpy::Args& 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<0>("str", "__len__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("str", "__len__", [](VM* vm, const pkpy::Args& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); return vm->PyInt(_self.u8_length()); }); - _vm->bindMethod<1>("str", "__contains__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "__contains__", [](VM* vm, const pkpy::Args& 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<0>("str", "__str__", CPP_LAMBDA(args[0])); + _vm->bind_method<0>("str", "__str__", CPP_LAMBDA(args[0])); - _vm->bindMethod<0>("str", "__iter__", CPP_LAMBDA( + _vm->bind_method<0>("str", "__iter__", CPP_LAMBDA( vm->PyIter(pkpy::make_shared(vm, args[0])) )); - _vm->bindMethod<0>("str", "__repr__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("str", "__repr__", [](VM* vm, const pkpy::Args& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); - return vm->PyStr(_self.__escape(true)); + return vm->PyStr(_self.escape(true)); }); - _vm->bindMethod<0>("str", "__json__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("str", "__json__", [](VM* vm, const pkpy::Args& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); - return vm->PyStr(_self.__escape(false)); + return vm->PyStr(_self.escape(false)); }); - _vm->bindMethod<1>("str", "__eq__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "__eq__", [](VM* vm, const pkpy::Args& 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]); }); - _vm->bindMethod<1>("str", "__ne__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "__ne__", [](VM* vm, const pkpy::Args& 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]); }); - _vm->bindMethod<1>("str", "__getitem__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "__getitem__", [](VM* vm, const pkpy::Args& args) { const _Str& _self (vm->PyStr_AS_C(args[0])); if(args[1]->is_type(vm->_tp_slice)){ @@ -327,19 +327,19 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyStr(_self.u8_getitem(_index)); }); - _vm->bindMethod<1>("str", "__gt__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "__gt__", [](VM* vm, const pkpy::Args& 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<1>("str", "__lt__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "__lt__", [](VM* vm, const pkpy::Args& 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<2>("str", "replace", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<2>("str", "replace", [](VM* vm, const pkpy::Args& 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]); @@ -353,19 +353,19 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyStr(_copy); }); - _vm->bindMethod<1>("str", "startswith", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "startswith", [](VM* vm, const pkpy::Args& 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<1>("str", "endswith", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "endswith", [](VM* vm, const pkpy::Args& 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<1>("str", "join", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("str", "join", [](VM* vm, const pkpy::Args& args) { const _Str& _self = vm->PyStr_AS_C(args[0]); PyVarList* _list = nullptr; if(args[1]->is_type(vm->_tp_list)){ @@ -384,19 +384,19 @@ void __initializeBuiltinFunctions(VM* _vm) { }); /************ PyList ************/ - _vm->bindMethod<0>("list", "__iter__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("list", "__iter__", [](VM* vm, const pkpy::Args& args) { return vm->PyIter( pkpy::make_shared(vm, args[0]) ); }); - _vm->bindMethod<1>("list", "append", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("list", "append", [](VM* vm, const pkpy::Args& args) { PyVarList& _self = vm->PyList_AS_C(args[0]); _self.push_back(args[1]); return vm->None; }); - _vm->bindMethod<2>("list", "insert", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<2>("list", "insert", [](VM* vm, const pkpy::Args& args) { PyVarList& _self = vm->PyList_AS_C(args[0]); int _index = (int)vm->PyInt_AS_C(args[1]); if(_index < 0) _index += _self.size(); @@ -406,16 +406,16 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->None; }); - _vm->bindMethod<0>("list", "clear", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("list", "clear", [](VM* vm, const pkpy::Args& args) { vm->PyList_AS_C(args[0]).clear(); return vm->None; }); - _vm->bindMethod<0>("list", "copy", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("list", "copy", [](VM* vm, const pkpy::Args& args) { return vm->PyList(vm->PyList_AS_C(args[0])); }); - _vm->bindMethod<1>("list", "__add__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("list", "__add__", [](VM* vm, const pkpy::Args& args) { const PyVarList& _self = vm->PyList_AS_C(args[0]); const PyVarList& _obj = vm->PyList_AS_C(args[1]); PyVarList _new_list = _self; @@ -423,12 +423,12 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->PyList(_new_list); }); - _vm->bindMethod<0>("list", "__len__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("list", "__len__", [](VM* vm, const pkpy::Args& args) { const PyVarList& _self = vm->PyList_AS_C(args[0]); return vm->PyInt(_self.size()); }); - _vm->bindMethodMulti<1>({"list", "tuple"}, "__getitem__", [](VM* vm, const pkpy::Args& args) { + _vm->_bind_methods<1>({"list", "tuple"}, "__getitem__", [](VM* vm, const pkpy::Args& args) { bool list = args[0]->is_type(vm->_tp_list); const PyVarList& _self = list ? vm->PyList_AS_C(args[0]) : vm->PyTuple_AS_C(args[0]); @@ -445,7 +445,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return _self[_index]; }); - _vm->bindMethod<2>("list", "__setitem__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<2>("list", "__setitem__", [](VM* vm, const pkpy::Args& args) { PyVarList& _self = vm->PyList_AS_C(args[0]); int _index = (int)vm->PyInt_AS_C(args[1]); _index = vm->normalized_index(_index, _self.size()); @@ -453,7 +453,7 @@ void __initializeBuiltinFunctions(VM* _vm) { return vm->None; }); - _vm->bindMethod<1>("list", "__delitem__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("list", "__delitem__", [](VM* vm, const pkpy::Args& args) { PyVarList& _self = vm->PyList_AS_C(args[0]); int _index = (int)vm->PyInt_AS_C(args[1]); _index = vm->normalized_index(_index, _self.size()); @@ -462,40 +462,40 @@ void __initializeBuiltinFunctions(VM* _vm) { }); /************ PyTuple ************/ - _vm->bindStaticMethod<1>("tuple", "__new__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_static_method<1>("tuple", "__new__", [](VM* vm, const pkpy::Args& args) { PyVarList _list = vm->PyList_AS_C(vm->call(vm->builtins->attribs["list"], args)); return vm->PyTuple(_list); }); - _vm->bindMethod<0>("tuple", "__iter__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("tuple", "__iter__", [](VM* vm, const pkpy::Args& args) { return vm->PyIter(pkpy::make_shared(vm, args[0])); }); - _vm->bindMethod<0>("tuple", "__len__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("tuple", "__len__", [](VM* vm, const pkpy::Args& args) { const PyVarList& _self = vm->PyTuple_AS_C(args[0]); return vm->PyInt(_self.size()); }); /************ PyBool ************/ - _vm->bindStaticMethod<1>("bool", "__new__", CPP_LAMBDA(vm->asBool(args[0]))); + _vm->bind_static_method<1>("bool", "__new__", CPP_LAMBDA(vm->asBool(args[0]))); - _vm->bindMethod<0>("bool", "__repr__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("bool", "__repr__", [](VM* vm, const pkpy::Args& args) { bool val = vm->PyBool_AS_C(args[0]); return vm->PyStr(val ? "True" : "False"); }); - _vm->bindMethod<0>("bool", "__json__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<0>("bool", "__json__", [](VM* vm, const pkpy::Args& args) { bool val = vm->PyBool_AS_C(args[0]); return vm->PyStr(val ? "true" : "false"); }); - _vm->bindMethod<1>("bool", "__xor__", [](VM* vm, const pkpy::Args& args) { + _vm->bind_method<1>("bool", "__xor__", [](VM* vm, const pkpy::Args& args) { bool _self = vm->PyBool_AS_C(args[0]); bool _obj = vm->PyBool_AS_C(args[1]); return vm->PyBool(_self ^ _obj); }); - _vm->bindMethod<0>("ellipsis", "__repr__", CPP_LAMBDA(vm->PyStr("Ellipsis"))); + _vm->bind_method<0>("ellipsis", "__repr__", CPP_LAMBDA(vm->PyStr("Ellipsis"))); } #include "builtins.h" @@ -512,25 +512,25 @@ void __initializeBuiltinFunctions(VM* _vm) { #endif -void __add_module_time(VM* vm){ +void add_module_time(VM* vm){ PyVar mod = vm->new_module("time"); - vm->bindFunc<0>(mod, "time", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<0>(mod, "time", [](VM* vm, const pkpy::Args& args) { auto now = std::chrono::high_resolution_clock::now(); return vm->PyFloat(std::chrono::duration_cast(now.time_since_epoch()).count() / 1000000.0); }); } -void __add_module_sys(VM* vm){ +void add_module_sys(VM* vm){ PyVar mod = vm->new_module("sys"); - vm->bindFunc<1>(mod, "getrefcount", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<1>(mod, "getrefcount", [](VM* vm, const pkpy::Args& args) { return vm->PyInt(args[0].use_count()); }); - vm->bindFunc<0>(mod, "getrecursionlimit", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<0>(mod, "getrecursionlimit", [](VM* vm, const pkpy::Args& args) { return vm->PyInt(vm->maxRecursionDepth); }); - vm->bindFunc<1>(mod, "setrecursionlimit", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<1>(mod, "setrecursionlimit", [](VM* vm, const pkpy::Args& args) { vm->maxRecursionDepth = (int)vm->PyInt_AS_C(args[0]); return vm->None; }); @@ -538,35 +538,35 @@ void __add_module_sys(VM* vm){ vm->setattr(mod, "version", vm->PyStr(PK_VERSION)); } -void __add_module_json(VM* vm){ +void add_module_json(VM* vm){ PyVar mod = vm->new_module("json"); - vm->bindFunc<1>(mod, "loads", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<1>(mod, "loads", [](VM* vm, const pkpy::Args& 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()->_locals); }); - vm->bindFunc<1>(mod, "dumps", CPP_LAMBDA(vm->call(args[0], __json__))); + vm->bind_func<1>(mod, "dumps", CPP_LAMBDA(vm->call(args[0], __json__))); } -void __add_module_math(VM* vm){ +void add_module_math(VM* vm){ PyVar mod = vm->new_module("math"); vm->setattr(mod, "pi", vm->PyFloat(3.1415926535897932384)); vm->setattr(mod, "e" , vm->PyFloat(2.7182818284590452354)); - vm->bindFunc<1>(mod, "log", CPP_LAMBDA(vm->PyFloat(log(vm->num_to_float(args[0]))))); - vm->bindFunc<1>(mod, "log10", CPP_LAMBDA(vm->PyFloat(log10(vm->num_to_float(args[0]))))); - vm->bindFunc<1>(mod, "log2", CPP_LAMBDA(vm->PyFloat(log2(vm->num_to_float(args[0]))))); - vm->bindFunc<1>(mod, "sin", CPP_LAMBDA(vm->PyFloat(sin(vm->num_to_float(args[0]))))); - vm->bindFunc<1>(mod, "cos", CPP_LAMBDA(vm->PyFloat(cos(vm->num_to_float(args[0]))))); - vm->bindFunc<1>(mod, "tan", CPP_LAMBDA(vm->PyFloat(tan(vm->num_to_float(args[0]))))); - vm->bindFunc<1>(mod, "isnan", CPP_LAMBDA(vm->PyBool(std::isnan(vm->num_to_float(args[0]))))); - vm->bindFunc<1>(mod, "isinf", CPP_LAMBDA(vm->PyBool(std::isinf(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "log", CPP_LAMBDA(vm->PyFloat(log(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "log10", CPP_LAMBDA(vm->PyFloat(log10(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "log2", CPP_LAMBDA(vm->PyFloat(log2(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "sin", CPP_LAMBDA(vm->PyFloat(sin(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "cos", CPP_LAMBDA(vm->PyFloat(cos(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "tan", CPP_LAMBDA(vm->PyFloat(tan(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(vm->PyBool(std::isnan(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(vm->PyBool(std::isinf(vm->num_to_float(args[0]))))); } -void __add_module_dis(VM* vm){ +void add_module_dis(VM* vm){ PyVar mod = vm->new_module("dis"); - vm->bindFunc<1>(mod, "dis", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<1>(mod, "dis", [](VM* vm, const pkpy::Args& args) { _Code code = vm->PyFunction_AS_C(args[0])->code; (*vm->_stdout) << vm->disassemble(code); return vm->None; @@ -582,16 +582,16 @@ struct ReMatch { ReMatch(i64 start, i64 end, std::smatch m) : start(start), end(end), m(m) {} static void _register(VM* vm, PyVar mod, PyVar type){ - vm->bindMethod<-1>(type, "__init__", CPP_NOT_IMPLEMENTED()); - vm->bindMethod<0>(type, "start", CPP_LAMBDA(vm->PyInt(vm->py_cast(args[0]).start))); - vm->bindMethod<0>(type, "end", CPP_LAMBDA(vm->PyInt(vm->py_cast(args[0]).end))); + vm->bind_method<-1>(type, "__init__", CPP_NOT_IMPLEMENTED()); + vm->bind_method<0>(type, "start", CPP_LAMBDA(vm->PyInt(vm->py_cast(args[0]).start))); + vm->bind_method<0>(type, "end", CPP_LAMBDA(vm->PyInt(vm->py_cast(args[0]).end))); - vm->bindMethod<0>(type, "span", [](VM* vm, const pkpy::Args& args) { + vm->bind_method<0>(type, "span", [](VM* vm, const pkpy::Args& args) { auto& self = vm->py_cast(args[0]); return vm->PyTuple({ vm->PyInt(self.start), vm->PyInt(self.end) }); }); - vm->bindMethod<1>(type, "group", [](VM* vm, const pkpy::Args& args) { + vm->bind_method<1>(type, "group", [](VM* vm, const pkpy::Args& args) { auto& self = vm->py_cast(args[0]); int index = (int)vm->PyInt_AS_C(args[1]); index = vm->normalized_index(index, self.m.size()); @@ -600,35 +600,35 @@ struct ReMatch { } }; -PyVar __regex_search(const _Str& pattern, const _Str& string, bool fromStart, VM* vm){ +PyVar _regex_search(const _Str& pattern, const _Str& string, bool fromStart, VM* vm){ std::regex re(pattern); std::smatch m; if(std::regex_search(string, m, re)){ 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()); + i64 start = string._to_u8_index(m.position()); + i64 end = string._to_u8_index(m.position() + m.length()); return vm->new_object(start, end, m); } return vm->None; }; -void __add_module_re(VM* vm){ +void add_module_re(VM* vm){ PyVar mod = vm->new_module("re"); vm->register_class(mod); - vm->bindFunc<2>(mod, "match", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<2>(mod, "match", [](VM* vm, const pkpy::Args& 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); + return _regex_search(pattern, string, true, vm); }); - vm->bindFunc<2>(mod, "search", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<2>(mod, "search", [](VM* vm, const pkpy::Args& 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); + return _regex_search(pattern, string, false, vm); }); - vm->bindFunc<3>(mod, "sub", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<3>(mod, "sub", [](VM* vm, const pkpy::Args& 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]); @@ -636,7 +636,7 @@ void __add_module_re(VM* vm){ return vm->PyStr(std::regex_replace(string, re, repl)); }); - vm->bindFunc<2>(mod, "split", [](VM* vm, const pkpy::Args& args) { + vm->bind_func<2>(mod, "split", [](VM* vm, const pkpy::Args& args) { const _Str& pattern = vm->PyStr_AS_C(args[0]); const _Str& string = vm->PyStr_AS_C(args[1]); std::regex re(pattern); @@ -755,13 +755,13 @@ extern "C" { /// Create a virtual machine. VM* pkpy_new_vm(bool use_stdio){ VM* vm = pkpy_allocate(VM, use_stdio); - __initializeBuiltinFunctions(vm); - __add_module_sys(vm); - __add_module_time(vm); - __add_module_json(vm); - __add_module_math(vm); - __add_module_re(vm); - __add_module_dis(vm); + init_builtins(vm); + add_module_sys(vm); + add_module_time(vm); + add_module_json(vm); + add_module_math(vm); + add_module_re(vm); + add_module_dis(vm); // add builtins | no exception handler | must succeed _Code code = vm->compile(__BUILTINS_CODE, "", EXEC_MODE); @@ -784,8 +784,8 @@ extern "C" { _Str _stdout = s_out->str(); _Str _stderr = s_err->str(); _StrStream ss; - ss << '{' << "\"stdout\": " << _stdout.__escape(false); - ss << ", " << "\"stderr\": " << _stderr.__escape(false) << '}'; + ss << '{' << "\"stdout\": " << _stdout.escape(false); + ss << ", " << "\"stderr\": " << _stderr.escape(false) << '}'; s_out->str(""); s_err->str(""); return strdup(ss.str().c_str()); } @@ -821,7 +821,7 @@ extern "C" { for(int i=0; name[i]; i++) if(name[i] == ' ') return nullptr; std::string f_header = std::string(mod) + '.' + name + '#' + std::to_string(kGlobalBindId++); PyVar obj = vm->_modules.contains(mod) ? vm->_modules[mod] : vm->new_module(mod); - vm->bindFunc<-1>(obj, name, [ret_code, f_header](VM* vm, const pkpy::Args& args){ + vm->bind_func<-1>(obj, name, [ret_code, f_header](VM* vm, const pkpy::Args& args){ _StrStream ss; ss << f_header; for(int i=0; ibegin(), _u8_index->end(), index); if(p != _u8_index->end() && *p != index) UNREACHABLE(); @@ -75,7 +75,7 @@ public: return substr(_u8_index->at(start), c_end - _u8_index->at(start)); } - _Str __lstrip() const { + _Str lstrip() const { _Str copy(*this); copy.erase(copy.begin(), std::find_if(copy.begin(), copy.end(), [](char c) { // std::isspace(c) does not working on windows (Debug) @@ -84,7 +84,7 @@ public: return _Str(copy); } - _Str __escape(bool single_quote) const { + _Str escape(bool single_quote) const { _StrStream ss; ss << (single_quote ? '\'' : '"'); for (int i=0; ikwArgs.contains(key)){ - typeError(key.__escape(true) + " is an invalid keyword argument for " + fn->name + "()"); + typeError(key.escape(true) + " is an invalid keyword argument for " + fn->name + "()"); } const PyVar& val = kwargs[i+1]; if(!positional_overrided_keys.empty()){ @@ -671,34 +671,34 @@ public: } template - void bindMethod(PyVar obj, _Str funcName, _CppFuncRaw fn) { + void bind_method(PyVar obj, _Str funcName, _CppFuncRaw fn) { check_type(obj, _tp_type); setattr(obj, funcName, PyNativeFunction(_CppFunc(fn, ARGC, true))); } template - void bindFunc(PyVar obj, _Str funcName, _CppFuncRaw fn) { + void bind_func(PyVar obj, _Str funcName, _CppFuncRaw fn) { setattr(obj, funcName, PyNativeFunction(_CppFunc(fn, ARGC, false))); } template - void bindMethod(_Str typeName, _Str funcName, _CppFuncRaw fn) { - bindMethod(_types[typeName], funcName, fn); + void bind_method(_Str typeName, _Str funcName, _CppFuncRaw fn) { + bind_method(_types[typeName], funcName, fn); } template - void bindStaticMethod(_Str typeName, _Str funcName, _CppFuncRaw fn) { - bindFunc(_types[typeName], funcName, fn); + void bind_static_method(_Str typeName, _Str funcName, _CppFuncRaw fn) { + bind_func(_types[typeName], funcName, fn); } template - void bindMethodMulti(std::vector<_Str> typeNames, _Str funcName, _CppFuncRaw fn) { - for(auto& typeName : typeNames) bindMethod(typeName, funcName, fn); + void _bind_methods(std::vector<_Str> typeNames, _Str funcName, _CppFuncRaw fn) { + for(auto& typeName : typeNames) bind_method(typeName, funcName, fn); } template - void bindBuiltinFunc(_Str funcName, _CppFuncRaw fn) { - bindFunc(builtins, funcName, fn); + void bind_builtin_func(_Str funcName, _CppFuncRaw fn) { + bind_func(builtins, funcName, fn); } inline f64 num_to_float(const PyVar& obj){ @@ -763,7 +763,7 @@ public: argStr += " (" + PyStr_AS_C(asRepr(code->consts[byte.arg])) + ")"; } if(byte.op == OP_LOAD_NAME_REF || byte.op == OP_LOAD_NAME || byte.op == OP_RAISE){ - argStr += " (" + code->names[byte.arg].first.__escape(true) + ")"; + argStr += " (" + code->names[byte.arg].first.escape(true) + ")"; } ss << pad(argStr, 20); // may overflow ss << code->blocks[byte.block].to_string(); @@ -941,6 +941,7 @@ public: template PyVar register_class(PyVar mod){ PyVar type = new_user_type_object(mod, T::_name(), _tp_object); + if(OBJ_NAME(mod) != T::_mod()) UNREACHABLE(); T::_register(this, mod, type); return type; }