|
|
@@ -17,7 +17,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::ArgList& args){ \
|
|
|
+ _vm->bindMethodMulti<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{ \
|
|
|
@@ -26,7 +26,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::ArgList& args){ \
|
|
|
+ _vm->bindMethodMulti<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){ \
|
|
|
@@ -52,23 +52,23 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
#undef BIND_NUM_ARITH_OPT
|
|
|
#undef BIND_NUM_LOGICAL_OPT
|
|
|
|
|
|
- _vm->bindBuiltinFunc<1>("__sys_stdout_write", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<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::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<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::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<1>("eval", [](VM* vm, const pkpy::Args& args) {
|
|
|
_Code code = vm->compile(vm->PyStr_AS_C(args[0]), "<eval>", EVAL_MODE);
|
|
|
return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
|
|
|
});
|
|
|
|
|
|
- _vm->bindBuiltinFunc<1>("exec", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<1>("exec", [](VM* vm, const pkpy::Args& args) {
|
|
|
_Code code = vm->compile(vm->PyStr_AS_C(args[0]), "<exec>", EXEC_MODE);
|
|
|
vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
|
|
|
return vm->None;
|
|
|
@@ -78,40 +78,40 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
_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->bindBuiltinFunc<1>("chr", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<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::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<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::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<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::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<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::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<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::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<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::ArgList& args) {
|
|
|
+ _vm->bindBuiltinFunc<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) {
|
|
|
@@ -126,7 +126,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return vm->PyList(ret);
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<0>("object", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<0>("object", "__repr__", [](VM* vm, const pkpy::Args& args) {
|
|
|
PyVar _self = args[0];
|
|
|
std::stringstream ss;
|
|
|
ss << std::hex << (uintptr_t)_self.get();
|
|
|
@@ -138,7 +138,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
_vm->bindMethod<1>("object", "__ne__", CPP_LAMBDA(vm->PyBool(args[0] != args[1])));
|
|
|
_vm->bindStaticMethod<1>("type", "__new__", CPP_LAMBDA(args[0]->_type));
|
|
|
|
|
|
- _vm->bindStaticMethod<-1>("range", "__new__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindStaticMethod<-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;
|
|
|
@@ -156,13 +156,13 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
_vm->bindMethod<0>("NoneType", "__repr__", CPP_LAMBDA(vm->PyStr("None")));
|
|
|
_vm->bindMethod<0>("NoneType", "__json__", CPP_LAMBDA(vm->PyStr("null")));
|
|
|
|
|
|
- _vm->bindMethodMulti<1>({"int", "float"}, "__truediv__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethodMulti<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::ArgList& args) {
|
|
|
+ _vm->bindMethodMulti<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{
|
|
|
@@ -171,7 +171,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
});
|
|
|
|
|
|
/************ PyInt ************/
|
|
|
- _vm->bindStaticMethod<1>("int", "__new__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindStaticMethod<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);
|
|
|
@@ -190,28 +190,28 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return vm->None;
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<1>("int", "__floordiv__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<0>("int", "__json__", [](VM* vm, const pkpy::Args& args) {
|
|
|
return vm->PyStr(std::to_string((int)vm->PyInt_AS_C(args[0])));
|
|
|
});
|
|
|
|
|
|
#define __INT_BITWISE_OP(name,op) \
|
|
|
- _vm->bindMethod<1>("int", #name, [](VM* vm, const pkpy::ArgList& args) { \
|
|
|
+ _vm->bindMethod<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])); \
|
|
|
});
|
|
|
|
|
|
@@ -224,7 +224,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
#undef __INT_BITWISE_OP
|
|
|
|
|
|
/************ PyFloat ************/
|
|
|
- _vm->bindStaticMethod<1>("float", "__new__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindStaticMethod<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);
|
|
|
@@ -243,7 +243,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return vm->None;
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<0>("float", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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;
|
|
|
@@ -253,7 +253,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return vm->PyStr(s);
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<0>("float", "__json__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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));
|
|
|
@@ -262,18 +262,18 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
/************ PyString ************/
|
|
|
_vm->bindStaticMethod<1>("str", "__new__", CPP_LAMBDA(vm->asStr(args[0])));
|
|
|
|
|
|
- _vm->bindMethod<1>("str", "__add__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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);
|
|
|
@@ -285,29 +285,29 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
vm->PyIter(pkpy::make_shared<BaseIterator, StringIterator>(vm, args[0]))
|
|
|
));
|
|
|
|
|
|
- _vm->bindMethod<0>("str", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<0>("str", "__repr__", [](VM* vm, const pkpy::Args& args) {
|
|
|
const _Str& _self = vm->PyStr_AS_C(args[0]);
|
|
|
return vm->PyStr(_self.__escape(true));
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<0>("str", "__json__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<0>("str", "__json__", [](VM* vm, const pkpy::Args& args) {
|
|
|
const _Str& _self = vm->PyStr_AS_C(args[0]);
|
|
|
return vm->PyStr(_self.__escape(false));
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<1>("str", "__eq__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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)){
|
|
|
@@ -321,19 +321,19 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return vm->PyStr(_self.u8_getitem(_index));
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<1>("str", "__gt__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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]);
|
|
|
@@ -347,19 +347,19 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return vm->PyStr(_copy);
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<1>("str", "startswith", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<1>("str", "join", [](VM* vm, const pkpy::Args& args) {
|
|
|
const _Str& _self = vm->PyStr_AS_C(args[0]);
|
|
|
PyVarList* _list;
|
|
|
if(args[1]->is_type(vm->_tp_list)){
|
|
|
@@ -378,19 +378,19 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
});
|
|
|
|
|
|
/************ PyList ************/
|
|
|
- _vm->bindMethod<0>("list", "__iter__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<0>("list", "__iter__", [](VM* vm, const pkpy::Args& args) {
|
|
|
return vm->PyIter(
|
|
|
pkpy::make_shared<BaseIterator, VectorIterator>(vm, args[0])
|
|
|
);
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<1>("list", "append", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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();
|
|
|
@@ -400,16 +400,16 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return vm->None;
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<0>("list", "clear", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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;
|
|
|
@@ -417,12 +417,12 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return vm->PyList(_new_list);
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<0>("list", "__len__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethodMulti<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]);
|
|
|
|
|
|
@@ -439,7 +439,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return _self[_index];
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<2>("list", "__setitem__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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());
|
|
|
@@ -447,7 +447,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
return vm->None;
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<1>("list", "__delitem__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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());
|
|
|
@@ -456,16 +456,16 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
});
|
|
|
|
|
|
/************ PyTuple ************/
|
|
|
- _vm->bindStaticMethod<1>("tuple", "__new__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindStaticMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<0>("tuple", "__iter__", [](VM* vm, const pkpy::Args& args) {
|
|
|
return vm->PyIter(pkpy::make_shared<BaseIterator, VectorIterator>(vm, args[0]));
|
|
|
});
|
|
|
|
|
|
- _vm->bindMethod<0>("tuple", "__len__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<0>("tuple", "__len__", [](VM* vm, const pkpy::Args& args) {
|
|
|
const PyVarList& _self = vm->PyTuple_AS_C(args[0]);
|
|
|
return vm->PyInt(_self.size());
|
|
|
});
|
|
|
@@ -473,17 +473,17 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
/************ PyBool ************/
|
|
|
_vm->bindStaticMethod<1>("bool", "__new__", CPP_LAMBDA(vm->asBool(args[0])));
|
|
|
|
|
|
- _vm->bindMethod<0>("bool", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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::ArgList& args) {
|
|
|
+ _vm->bindMethod<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);
|
|
|
@@ -508,7 +508,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|
|
|
|
|
void __add_module_time(VM* vm){
|
|
|
PyVar mod = vm->new_module("time");
|
|
|
- vm->bindFunc<0>(mod, "time", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindFunc<0>(mod, "time", [](VM* vm, const pkpy::Args& args) {
|
|
|
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);
|
|
|
});
|
|
|
@@ -516,15 +516,15 @@ void __add_module_time(VM* vm){
|
|
|
|
|
|
void __add_module_sys(VM* vm){
|
|
|
PyVar mod = vm->new_module("sys");
|
|
|
- vm->bindFunc<1>(mod, "getrefcount", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindFunc<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::ArgList& args) {
|
|
|
+ vm->bindFunc<0>(mod, "getrecursionlimit", [](VM* vm, const pkpy::Args& args) {
|
|
|
return vm->PyInt(vm->maxRecursionDepth);
|
|
|
});
|
|
|
|
|
|
- vm->bindFunc<1>(mod, "setrecursionlimit", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindFunc<1>(mod, "setrecursionlimit", [](VM* vm, const pkpy::Args& args) {
|
|
|
vm->maxRecursionDepth = (int)vm->PyInt_AS_C(args[0]);
|
|
|
return vm->None;
|
|
|
});
|
|
|
@@ -534,13 +534,13 @@ void __add_module_sys(VM* vm){
|
|
|
|
|
|
void __add_module_json(VM* vm){
|
|
|
PyVar mod = vm->new_module("json");
|
|
|
- vm->bindFunc<1>(mod, "loads", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindFunc<1>(mod, "loads", [](VM* vm, const pkpy::Args& args) {
|
|
|
const _Str& expr = vm->PyStr_AS_C(args[0]);
|
|
|
_Code code = vm->compile(expr, "<json>", JSON_MODE);
|
|
|
return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
|
|
|
});
|
|
|
|
|
|
- vm->bindFunc<1>(mod, "dumps", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindFunc<1>(mod, "dumps", [](VM* vm, const pkpy::Args& args) {
|
|
|
return vm->asJson(args[0]);
|
|
|
});
|
|
|
}
|
|
|
@@ -562,7 +562,7 @@ void __add_module_math(VM* vm){
|
|
|
|
|
|
void __add_module_dis(VM* vm){
|
|
|
PyVar mod = vm->new_module("dis");
|
|
|
- vm->bindFunc<1>(mod, "dis", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindFunc<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;
|
|
|
@@ -584,12 +584,12 @@ struct ReMatch {
|
|
|
vm->bindMethod<0>(_tp_match, "start", CPP_LAMBDA(vm->PyInt(UNION_GET(ReMatch, args[0]).start)));
|
|
|
vm->bindMethod<0>(_tp_match, "end", CPP_LAMBDA(vm->PyInt(UNION_GET(ReMatch, args[0]).end)));
|
|
|
|
|
|
- vm->bindMethod<0>(_tp_match, "span", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindMethod<0>(_tp_match, "span", [](VM* vm, const pkpy::Args& args) {
|
|
|
auto& m = UNION_GET(ReMatch, args[0]);
|
|
|
return vm->PyTuple({ vm->PyInt(m.start), vm->PyInt(m.end) });
|
|
|
});
|
|
|
|
|
|
- vm->bindMethod<1>(_tp_match, "group", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindMethod<1>(_tp_match, "group", [](VM* vm, const pkpy::Args& args) {
|
|
|
auto& m = UNION_GET(ReMatch, args[0]);
|
|
|
int index = (int)vm->PyInt_AS_C(args[1]);
|
|
|
index = vm->normalized_index(index, m.m.size());
|
|
|
@@ -615,19 +615,19 @@ void __add_module_re(VM* vm){
|
|
|
PyVar mod = vm->new_module("re");
|
|
|
ReMatch::_bind(vm);
|
|
|
|
|
|
- vm->bindFunc<2>(mod, "match", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindFunc<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);
|
|
|
});
|
|
|
|
|
|
- vm->bindFunc<2>(mod, "search", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindFunc<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);
|
|
|
});
|
|
|
|
|
|
- vm->bindFunc<3>(mod, "sub", [](VM* vm, const pkpy::ArgList& args) {
|
|
|
+ vm->bindFunc<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]);
|
|
|
@@ -635,7 +635,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::ArgList& args) {
|
|
|
+ vm->bindFunc<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);
|