mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
some refactor
This commit is contained in:
parent
5e9082b960
commit
ef04932ec9
@ -51,7 +51,7 @@ struct SourceData {
|
|||||||
_Str line = "<?>";
|
_Str line = "<?>";
|
||||||
int removedSpaces = 0;
|
int removedSpaces = 0;
|
||||||
if(pair.first && pair.second){
|
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();
|
removedSpaces = pair.second - pair.first - line.size();
|
||||||
if(line.empty()) line = "<?>";
|
if(line.empty()) line = "<?>";
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ struct Timer{
|
|||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
VM* vm = pkpy_new_vm(true);
|
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;
|
static std::string line;
|
||||||
std::getline(std::cin, line);
|
std::getline(std::cin, line);
|
||||||
return vm->PyStr(line);
|
return vm->PyStr(line);
|
||||||
|
@ -100,6 +100,7 @@ struct Py_ : PyObject {
|
|||||||
|
|
||||||
#define PY_CLASS(mod, name) \
|
#define PY_CLASS(mod, name) \
|
||||||
inline static PyVar _type(VM* vm) { return vm->_modules[#mod]->attribs[#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; }
|
inline static const char* _name() { return #name; }
|
||||||
|
|
||||||
#define PY_BUILTIN_CLASS(name) inline static PyVar _type(VM* vm) { return vm->_tp_##name; }
|
#define PY_BUILTIN_CLASS(name) inline static PyVar _type(VM* vm) { return vm->_tp_##name; }
|
@ -210,7 +210,7 @@ struct Parser {
|
|||||||
value |= (b & 0b00111111) << (6*(u8bytes-k-1));
|
value |= (b & 0b00111111) << (6*(u8bytes-k-1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(__isLoChar(value)) curr_char += u8bytes;
|
if(is_unicode_Lo_char(value)) curr_char += u8bytes;
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
238
src/pocketpy.h
238
src/pocketpy.h
@ -15,7 +15,7 @@ _Code VM::compile(_Str source, _Str filename, CompileMode mode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define BIND_NUM_ARITH_OPT(name, op) \
|
#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)){ \
|
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])); \
|
return vm->PyInt(vm->PyInt_AS_C(args[0]) op vm->PyInt_AS_C(args[1])); \
|
||||||
}else{ \
|
}else{ \
|
||||||
@ -24,7 +24,7 @@ _Code VM::compile(_Str source, _Str filename, CompileMode mode) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
#define BIND_NUM_LOGICAL_OPT(name, op, is_eq) \
|
#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 _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); \
|
bool _1 = args[1]->is_type(vm->_tp_int) || args[1]->is_type(vm->_tp_float); \
|
||||||
if(!_0 || !_1){ \
|
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(__add__, +)
|
||||||
BIND_NUM_ARITH_OPT(__sub__, -)
|
BIND_NUM_ARITH_OPT(__sub__, -)
|
||||||
BIND_NUM_ARITH_OPT(__mul__, *)
|
BIND_NUM_ARITH_OPT(__mul__, *)
|
||||||
@ -50,73 +50,73 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
#undef BIND_NUM_ARITH_OPT
|
#undef BIND_NUM_ARITH_OPT
|
||||||
#undef BIND_NUM_LOGICAL_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]);
|
(*vm->_stdout) << vm->PyStr_AS_C(args[0]);
|
||||||
return vm->None;
|
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);
|
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");
|
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);
|
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>", EVAL_MODE);
|
_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);
|
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>", EXEC_MODE);
|
_Code code = vm->compile(vm->PyStr_AS_C(args[0]), "<exec>", EXEC_MODE);
|
||||||
vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
|
vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
|
||||||
return vm->None;
|
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);
|
if(args.size() == 0) std::exit(0);
|
||||||
else if(args.size() == 1) std::exit((int)vm->PyInt_AS_C(args[0]));
|
else if(args.size() == 1) std::exit((int)vm->PyInt_AS_C(args[0]));
|
||||||
else vm->typeError("exit() takes at most 1 argument");
|
else vm->typeError("exit() takes at most 1 argument");
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bindBuiltinFunc<1>("repr", CPP_LAMBDA(vm->asRepr(args[0])));
|
_vm->bind_builtin_func<1>("repr", CPP_LAMBDA(vm->asRepr(args[0])));
|
||||||
_vm->bindBuiltinFunc<1>("hash", CPP_LAMBDA(vm->PyInt(vm->hash(args[0]))));
|
_vm->bind_builtin_func<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>("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]);
|
i64 i = vm->PyInt_AS_C(args[0]);
|
||||||
if (i < 0 || i > 128) vm->valueError("chr() arg not in range(128)");
|
if (i < 0 || i > 128) vm->valueError("chr() arg not in range(128)");
|
||||||
return vm->PyStr(std::string(1, (char)i));
|
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]);
|
_Str s = vm->PyStr_AS_C(args[0]);
|
||||||
if (s.size() != 1) vm->typeError("ord() expected an ASCII character");
|
if (s.size() != 1) vm->typeError("ord() expected an ASCII character");
|
||||||
return vm->PyInt((i64)(s.c_str()[0]));
|
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);
|
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];
|
PyVar obj = args[0];
|
||||||
vm->setattr(obj, vm->PyStr_AS_C(args[1]), args[2]);
|
vm->setattr(obj, vm->PyStr_AS_C(args[1]), args[2]);
|
||||||
return vm->None;
|
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]);
|
_Str name = vm->PyStr_AS_C(args[1]);
|
||||||
return vm->getattr(args[0], name);
|
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;
|
std::stringstream ss;
|
||||||
ss << std::hex << vm->PyInt_AS_C(args[0]);
|
ss << std::hex << vm->PyInt_AS_C(args[0]);
|
||||||
return vm->PyStr("0x" + ss.str());
|
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;
|
std::vector<_Str> names;
|
||||||
for (auto& [k, _] : args[0]->attribs) names.push_back(k);
|
for (auto& [k, _] : args[0]->attribs) names.push_back(k);
|
||||||
for (auto& [k, _] : args[0]->type->attribs) {
|
for (auto& [k, _] : args[0]->type->attribs) {
|
||||||
@ -131,7 +131,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyList(ret);
|
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];
|
PyVar _self = args[0];
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << std::hex << (uintptr_t)_self.get();
|
ss << std::hex << (uintptr_t)_self.get();
|
||||||
@ -139,12 +139,12 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyStr(s);
|
return vm->PyStr(s);
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bindMethod<1>("object", "__eq__", CPP_LAMBDA(vm->PyBool(args[0] == args[1])));
|
_vm->bind_method<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", "__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;
|
_Range r;
|
||||||
switch (args.size()) {
|
switch (args.size()) {
|
||||||
case 1: r.stop = vm->PyInt_AS_C(args[0]); break;
|
case 1: r.stop = vm->PyInt_AS_C(args[0]); break;
|
||||||
@ -155,20 +155,20 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyRange(r);
|
return vm->PyRange(r);
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bindMethod<0>("range", "__iter__", CPP_LAMBDA(
|
_vm->bind_method<0>("range", "__iter__", CPP_LAMBDA(
|
||||||
vm->PyIter(pkpy::make_shared<BaseIterator, RangeIterator>(vm, args[0]))
|
vm->PyIter(pkpy::make_shared<BaseIterator, RangeIterator>(vm, args[0]))
|
||||||
));
|
));
|
||||||
|
|
||||||
_vm->bindMethod<0>("NoneType", "__repr__", CPP_LAMBDA(vm->PyStr("None")));
|
_vm->bind_method<0>("NoneType", "__repr__", CPP_LAMBDA(vm->PyStr("None")));
|
||||||
_vm->bindMethod<0>("NoneType", "__json__", CPP_LAMBDA(vm->PyStr("null")));
|
_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]);
|
f64 rhs = vm->num_to_float(args[1]);
|
||||||
if (rhs == 0) vm->zeroDivisionError();
|
if (rhs == 0) vm->zeroDivisionError();
|
||||||
return vm->PyFloat(vm->num_to_float(args[0]) / rhs);
|
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)){
|
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]))));
|
return vm->PyInt((i64)round(pow(vm->PyInt_AS_C(args[0]), vm->PyInt_AS_C(args[1]))));
|
||||||
}else{
|
}else{
|
||||||
@ -177,7 +177,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/************ PyInt ************/
|
/************ 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_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_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);
|
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;
|
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]);
|
i64 rhs = vm->PyInt_AS_C(args[1]);
|
||||||
if(rhs == 0) vm->zeroDivisionError();
|
if(rhs == 0) vm->zeroDivisionError();
|
||||||
return vm->PyInt(vm->PyInt_AS_C(args[0]) / rhs);
|
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]);
|
i64 rhs = vm->PyInt_AS_C(args[1]);
|
||||||
if(rhs == 0) vm->zeroDivisionError();
|
if(rhs == 0) vm->zeroDivisionError();
|
||||||
return vm->PyInt(vm->PyInt_AS_C(args[0]) % rhs);
|
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])));
|
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])));
|
return vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0])));
|
||||||
});
|
});
|
||||||
|
|
||||||
#define __INT_BITWISE_OP(name,op) \
|
#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])); \
|
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
|
#undef __INT_BITWISE_OP
|
||||||
|
|
||||||
/************ PyFloat ************/
|
/************ 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_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_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);
|
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;
|
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]);
|
f64 val = vm->PyFloat_AS_C(args[0]);
|
||||||
if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val));
|
if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val));
|
||||||
_StrStream ss;
|
_StrStream ss;
|
||||||
@ -259,61 +259,61 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyStr(s);
|
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]);
|
f64 val = vm->PyFloat_AS_C(args[0]);
|
||||||
if(std::isinf(val) || std::isnan(val)) vm->valueError("cannot jsonify 'nan' or 'inf'");
|
if(std::isinf(val) || std::isnan(val)) vm->valueError("cannot jsonify 'nan' or 'inf'");
|
||||||
return vm->PyStr(std::to_string(val));
|
return vm->PyStr(std::to_string(val));
|
||||||
});
|
});
|
||||||
|
|
||||||
/************ PyString ************/
|
/************ 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& lhs = vm->PyStr_AS_C(args[0]);
|
||||||
const _Str& rhs = vm->PyStr_AS_C(args[1]);
|
const _Str& rhs = vm->PyStr_AS_C(args[1]);
|
||||||
return vm->PyStr(lhs + rhs);
|
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]);
|
const _Str& _self = vm->PyStr_AS_C(args[0]);
|
||||||
return vm->PyInt(_self.u8_length());
|
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& _self = vm->PyStr_AS_C(args[0]);
|
||||||
const _Str& _other = vm->PyStr_AS_C(args[1]);
|
const _Str& _other = vm->PyStr_AS_C(args[1]);
|
||||||
return vm->PyBool(_self.find(_other) != _Str::npos);
|
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<BaseIterator, StringIterator>(vm, args[0]))
|
vm->PyIter(pkpy::make_shared<BaseIterator, StringIterator>(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]);
|
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]);
|
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))
|
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(vm->PyStr_AS_C(args[0]) == vm->PyStr_AS_C(args[1]));
|
||||||
return vm->PyBool(args[0] == 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))
|
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(vm->PyStr_AS_C(args[0]) != vm->PyStr_AS_C(args[1]));
|
||||||
return vm->PyBool(args[0] != 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]));
|
const _Str& _self (vm->PyStr_AS_C(args[0]));
|
||||||
|
|
||||||
if(args[1]->is_type(vm->_tp_slice)){
|
if(args[1]->is_type(vm->_tp_slice)){
|
||||||
@ -327,19 +327,19 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyStr(_self.u8_getitem(_index));
|
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& _self (vm->PyStr_AS_C(args[0]));
|
||||||
const _Str& _obj (vm->PyStr_AS_C(args[1]));
|
const _Str& _obj (vm->PyStr_AS_C(args[1]));
|
||||||
return vm->PyBool(_self > _obj);
|
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& _self (vm->PyStr_AS_C(args[0]));
|
||||||
const _Str& _obj (vm->PyStr_AS_C(args[1]));
|
const _Str& _obj (vm->PyStr_AS_C(args[1]));
|
||||||
return vm->PyBool(_self < _obj);
|
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& _self = vm->PyStr_AS_C(args[0]);
|
||||||
const _Str& _old = vm->PyStr_AS_C(args[1]);
|
const _Str& _old = vm->PyStr_AS_C(args[1]);
|
||||||
const _Str& _new = vm->PyStr_AS_C(args[2]);
|
const _Str& _new = vm->PyStr_AS_C(args[2]);
|
||||||
@ -353,19 +353,19 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyStr(_copy);
|
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& _self = vm->PyStr_AS_C(args[0]);
|
||||||
const _Str& _prefix = vm->PyStr_AS_C(args[1]);
|
const _Str& _prefix = vm->PyStr_AS_C(args[1]);
|
||||||
return vm->PyBool(_self.find(_prefix) == 0);
|
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& _self = vm->PyStr_AS_C(args[0]);
|
||||||
const _Str& _suffix = vm->PyStr_AS_C(args[1]);
|
const _Str& _suffix = vm->PyStr_AS_C(args[1]);
|
||||||
return vm->PyBool(_self.rfind(_suffix) == _self.length() - _suffix.length());
|
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]);
|
const _Str& _self = vm->PyStr_AS_C(args[0]);
|
||||||
PyVarList* _list = nullptr;
|
PyVarList* _list = nullptr;
|
||||||
if(args[1]->is_type(vm->_tp_list)){
|
if(args[1]->is_type(vm->_tp_list)){
|
||||||
@ -384,19 +384,19 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/************ PyList ************/
|
/************ 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(
|
return vm->PyIter(
|
||||||
pkpy::make_shared<BaseIterator, VectorIterator>(vm, args[0])
|
pkpy::make_shared<BaseIterator, VectorIterator>(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]);
|
PyVarList& _self = vm->PyList_AS_C(args[0]);
|
||||||
_self.push_back(args[1]);
|
_self.push_back(args[1]);
|
||||||
return vm->None;
|
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]);
|
PyVarList& _self = vm->PyList_AS_C(args[0]);
|
||||||
int _index = (int)vm->PyInt_AS_C(args[1]);
|
int _index = (int)vm->PyInt_AS_C(args[1]);
|
||||||
if(_index < 0) _index += _self.size();
|
if(_index < 0) _index += _self.size();
|
||||||
@ -406,16 +406,16 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->None;
|
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();
|
vm->PyList_AS_C(args[0]).clear();
|
||||||
return vm->None;
|
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]));
|
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& _self = vm->PyList_AS_C(args[0]);
|
||||||
const PyVarList& _obj = vm->PyList_AS_C(args[1]);
|
const PyVarList& _obj = vm->PyList_AS_C(args[1]);
|
||||||
PyVarList _new_list = _self;
|
PyVarList _new_list = _self;
|
||||||
@ -423,12 +423,12 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyList(_new_list);
|
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]);
|
const PyVarList& _self = vm->PyList_AS_C(args[0]);
|
||||||
return vm->PyInt(_self.size());
|
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);
|
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]);
|
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];
|
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]);
|
PyVarList& _self = vm->PyList_AS_C(args[0]);
|
||||||
int _index = (int)vm->PyInt_AS_C(args[1]);
|
int _index = (int)vm->PyInt_AS_C(args[1]);
|
||||||
_index = vm->normalized_index(_index, _self.size());
|
_index = vm->normalized_index(_index, _self.size());
|
||||||
@ -453,7 +453,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->None;
|
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]);
|
PyVarList& _self = vm->PyList_AS_C(args[0]);
|
||||||
int _index = (int)vm->PyInt_AS_C(args[1]);
|
int _index = (int)vm->PyInt_AS_C(args[1]);
|
||||||
_index = vm->normalized_index(_index, _self.size());
|
_index = vm->normalized_index(_index, _self.size());
|
||||||
@ -462,40 +462,40 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/************ PyTuple ************/
|
/************ 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));
|
PyVarList _list = vm->PyList_AS_C(vm->call(vm->builtins->attribs["list"], args));
|
||||||
return vm->PyTuple(_list);
|
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<BaseIterator, VectorIterator>(vm, args[0]));
|
return vm->PyIter(pkpy::make_shared<BaseIterator, VectorIterator>(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]);
|
const PyVarList& _self = vm->PyTuple_AS_C(args[0]);
|
||||||
return vm->PyInt(_self.size());
|
return vm->PyInt(_self.size());
|
||||||
});
|
});
|
||||||
|
|
||||||
/************ PyBool ************/
|
/************ 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]);
|
bool val = vm->PyBool_AS_C(args[0]);
|
||||||
return vm->PyStr(val ? "True" : "False");
|
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]);
|
bool val = vm->PyBool_AS_C(args[0]);
|
||||||
return vm->PyStr(val ? "true" : "false");
|
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 _self = vm->PyBool_AS_C(args[0]);
|
||||||
bool _obj = vm->PyBool_AS_C(args[1]);
|
bool _obj = vm->PyBool_AS_C(args[1]);
|
||||||
return vm->PyBool(_self ^ _obj);
|
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"
|
#include "builtins.h"
|
||||||
@ -512,25 +512,25 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void __add_module_time(VM* vm){
|
void add_module_time(VM* vm){
|
||||||
PyVar mod = vm->new_module("time");
|
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();
|
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);
|
return vm->PyFloat(std::chrono::duration_cast<std::chrono::microseconds>(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");
|
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());
|
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);
|
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]);
|
vm->maxRecursionDepth = (int)vm->PyInt_AS_C(args[0]);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
@ -538,35 +538,35 @@ void __add_module_sys(VM* vm){
|
|||||||
vm->setattr(mod, "version", vm->PyStr(PK_VERSION));
|
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");
|
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]);
|
const _Str& expr = vm->PyStr_AS_C(args[0]);
|
||||||
_Code code = vm->compile(expr, "<json>", JSON_MODE);
|
_Code code = vm->compile(expr, "<json>", JSON_MODE);
|
||||||
return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
|
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");
|
PyVar mod = vm->new_module("math");
|
||||||
vm->setattr(mod, "pi", vm->PyFloat(3.1415926535897932384));
|
vm->setattr(mod, "pi", vm->PyFloat(3.1415926535897932384));
|
||||||
vm->setattr(mod, "e" , vm->PyFloat(2.7182818284590452354));
|
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->bind_func<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->bind_func<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->bind_func<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->bind_func<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->bind_func<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->bind_func<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->bind_func<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, "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");
|
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;
|
_Code code = vm->PyFunction_AS_C(args[0])->code;
|
||||||
(*vm->_stdout) << vm->disassemble(code);
|
(*vm->_stdout) << vm->disassemble(code);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
@ -582,16 +582,16 @@ struct ReMatch {
|
|||||||
ReMatch(i64 start, i64 end, std::smatch m) : start(start), end(end), m(m) {}
|
ReMatch(i64 start, i64 end, std::smatch m) : start(start), end(end), m(m) {}
|
||||||
|
|
||||||
static void _register(VM* vm, PyVar mod, PyVar type){
|
static void _register(VM* vm, PyVar mod, PyVar type){
|
||||||
vm->bindMethod<-1>(type, "__init__", CPP_NOT_IMPLEMENTED());
|
vm->bind_method<-1>(type, "__init__", CPP_NOT_IMPLEMENTED());
|
||||||
vm->bindMethod<0>(type, "start", CPP_LAMBDA(vm->PyInt(vm->py_cast<ReMatch>(args[0]).start)));
|
vm->bind_method<0>(type, "start", CPP_LAMBDA(vm->PyInt(vm->py_cast<ReMatch>(args[0]).start)));
|
||||||
vm->bindMethod<0>(type, "end", CPP_LAMBDA(vm->PyInt(vm->py_cast<ReMatch>(args[0]).end)));
|
vm->bind_method<0>(type, "end", CPP_LAMBDA(vm->PyInt(vm->py_cast<ReMatch>(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<ReMatch>(args[0]);
|
auto& self = vm->py_cast<ReMatch>(args[0]);
|
||||||
return vm->PyTuple({ vm->PyInt(self.start), vm->PyInt(self.end) });
|
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<ReMatch>(args[0]);
|
auto& self = vm->py_cast<ReMatch>(args[0]);
|
||||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
int index = (int)vm->PyInt_AS_C(args[1]);
|
||||||
index = vm->normalized_index(index, self.m.size());
|
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::regex re(pattern);
|
||||||
std::smatch m;
|
std::smatch m;
|
||||||
if(std::regex_search(string, m, re)){
|
if(std::regex_search(string, m, re)){
|
||||||
if(fromStart && m.position() != 0) return vm->None;
|
if(fromStart && m.position() != 0) return vm->None;
|
||||||
i64 start = string.__to_u8_index(m.position());
|
i64 start = string._to_u8_index(m.position());
|
||||||
i64 end = string.__to_u8_index(m.position() + m.length());
|
i64 end = string._to_u8_index(m.position() + m.length());
|
||||||
return vm->new_object<ReMatch>(start, end, m);
|
return vm->new_object<ReMatch>(start, end, m);
|
||||||
}
|
}
|
||||||
return vm->None;
|
return vm->None;
|
||||||
};
|
};
|
||||||
|
|
||||||
void __add_module_re(VM* vm){
|
void add_module_re(VM* vm){
|
||||||
PyVar mod = vm->new_module("re");
|
PyVar mod = vm->new_module("re");
|
||||||
vm->register_class<ReMatch>(mod);
|
vm->register_class<ReMatch>(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& pattern = vm->PyStr_AS_C(args[0]);
|
||||||
const _Str& string = vm->PyStr_AS_C(args[1]);
|
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& pattern = vm->PyStr_AS_C(args[0]);
|
||||||
const _Str& string = vm->PyStr_AS_C(args[1]);
|
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& pattern = vm->PyStr_AS_C(args[0]);
|
||||||
const _Str& repl = vm->PyStr_AS_C(args[1]);
|
const _Str& repl = vm->PyStr_AS_C(args[1]);
|
||||||
const _Str& string = vm->PyStr_AS_C(args[2]);
|
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));
|
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& pattern = vm->PyStr_AS_C(args[0]);
|
||||||
const _Str& string = vm->PyStr_AS_C(args[1]);
|
const _Str& string = vm->PyStr_AS_C(args[1]);
|
||||||
std::regex re(pattern);
|
std::regex re(pattern);
|
||||||
@ -755,13 +755,13 @@ extern "C" {
|
|||||||
/// Create a virtual machine.
|
/// Create a virtual machine.
|
||||||
VM* pkpy_new_vm(bool use_stdio){
|
VM* pkpy_new_vm(bool use_stdio){
|
||||||
VM* vm = pkpy_allocate(VM, use_stdio);
|
VM* vm = pkpy_allocate(VM, use_stdio);
|
||||||
__initializeBuiltinFunctions(vm);
|
init_builtins(vm);
|
||||||
__add_module_sys(vm);
|
add_module_sys(vm);
|
||||||
__add_module_time(vm);
|
add_module_time(vm);
|
||||||
__add_module_json(vm);
|
add_module_json(vm);
|
||||||
__add_module_math(vm);
|
add_module_math(vm);
|
||||||
__add_module_re(vm);
|
add_module_re(vm);
|
||||||
__add_module_dis(vm);
|
add_module_dis(vm);
|
||||||
|
|
||||||
// add builtins | no exception handler | must succeed
|
// add builtins | no exception handler | must succeed
|
||||||
_Code code = vm->compile(__BUILTINS_CODE, "<builtins>", EXEC_MODE);
|
_Code code = vm->compile(__BUILTINS_CODE, "<builtins>", EXEC_MODE);
|
||||||
@ -784,8 +784,8 @@ extern "C" {
|
|||||||
_Str _stdout = s_out->str();
|
_Str _stdout = s_out->str();
|
||||||
_Str _stderr = s_err->str();
|
_Str _stderr = s_err->str();
|
||||||
_StrStream ss;
|
_StrStream ss;
|
||||||
ss << '{' << "\"stdout\": " << _stdout.__escape(false);
|
ss << '{' << "\"stdout\": " << _stdout.escape(false);
|
||||||
ss << ", " << "\"stderr\": " << _stderr.__escape(false) << '}';
|
ss << ", " << "\"stderr\": " << _stderr.escape(false) << '}';
|
||||||
s_out->str(""); s_err->str("");
|
s_out->str(""); s_err->str("");
|
||||||
return strdup(ss.str().c_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;
|
for(int i=0; name[i]; i++) if(name[i] == ' ') return nullptr;
|
||||||
std::string f_header = std::string(mod) + '.' + name + '#' + std::to_string(kGlobalBindId++);
|
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);
|
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;
|
_StrStream ss;
|
||||||
ss << f_header;
|
ss << f_header;
|
||||||
for(int i=0; i<args.size(); i++){
|
for(int i=0; i<args.size(); i++){
|
||||||
|
48
src/str.h
48
src/str.h
@ -52,7 +52,7 @@ public:
|
|||||||
return _hash;
|
return _hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
i64 __to_u8_index(i64 index) const{
|
i64 _to_u8_index(i64 index) const{
|
||||||
utf8_lazy_init();
|
utf8_lazy_init();
|
||||||
auto p = std::lower_bound(_u8_index->begin(), _u8_index->end(), index);
|
auto p = std::lower_bound(_u8_index->begin(), _u8_index->end(), index);
|
||||||
if(p != _u8_index->end() && *p != index) UNREACHABLE();
|
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));
|
return substr(_u8_index->at(start), c_end - _u8_index->at(start));
|
||||||
}
|
}
|
||||||
|
|
||||||
_Str __lstrip() const {
|
_Str lstrip() const {
|
||||||
_Str copy(*this);
|
_Str copy(*this);
|
||||||
copy.erase(copy.begin(), std::find_if(copy.begin(), copy.end(), [](char c) {
|
copy.erase(copy.begin(), std::find_if(copy.begin(), copy.end(), [](char c) {
|
||||||
// std::isspace(c) does not working on windows (Debug)
|
// std::isspace(c) does not working on windows (Debug)
|
||||||
@ -84,7 +84,7 @@ public:
|
|||||||
return _Str(copy);
|
return _Str(copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
_Str __escape(bool single_quote) const {
|
_Str escape(bool single_quote) const {
|
||||||
_StrStream ss;
|
_StrStream ss;
|
||||||
ss << (single_quote ? '\'' : '"');
|
ss << (single_quote ? '\'' : '"');
|
||||||
for (int i=0; i<length(); i++) {
|
for (int i=0; i<length(); i++) {
|
||||||
@ -147,27 +147,27 @@ namespace std {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const _Str& __class__ = _Str("__class__");
|
const _Str __class__ = _Str("__class__");
|
||||||
const _Str& __base__ = _Str("__base__");
|
const _Str __base__ = _Str("__base__");
|
||||||
const _Str& __new__ = _Str("__new__");
|
const _Str __new__ = _Str("__new__");
|
||||||
const _Str& __iter__ = _Str("__iter__");
|
const _Str __iter__ = _Str("__iter__");
|
||||||
const _Str& __str__ = _Str("__str__");
|
const _Str __str__ = _Str("__str__");
|
||||||
const _Str& __repr__ = _Str("__repr__");
|
const _Str __repr__ = _Str("__repr__");
|
||||||
const _Str& __module__ = _Str("__module__");
|
const _Str __module__ = _Str("__module__");
|
||||||
const _Str& __getitem__ = _Str("__getitem__");
|
const _Str __getitem__ = _Str("__getitem__");
|
||||||
const _Str& __setitem__ = _Str("__setitem__");
|
const _Str __setitem__ = _Str("__setitem__");
|
||||||
const _Str& __delitem__ = _Str("__delitem__");
|
const _Str __delitem__ = _Str("__delitem__");
|
||||||
const _Str& __contains__ = _Str("__contains__");
|
const _Str __contains__ = _Str("__contains__");
|
||||||
const _Str& __init__ = _Str("__init__");
|
const _Str __init__ = _Str("__init__");
|
||||||
const _Str& __json__ = _Str("__json__");
|
const _Str __json__ = _Str("__json__");
|
||||||
const _Str& __name__ = _Str("__name__");
|
const _Str __name__ = _Str("__name__");
|
||||||
const _Str& __len__ = _Str("__len__");
|
const _Str __len__ = _Str("__len__");
|
||||||
|
|
||||||
const _Str& m_append = _Str("append");
|
const _Str m_append = _Str("append");
|
||||||
const _Str& m_eval = _Str("eval");
|
const _Str m_eval = _Str("eval");
|
||||||
const _Str& m_self = _Str("self");
|
const _Str m_self = _Str("self");
|
||||||
const _Str& __enter__ = _Str("__enter__");
|
const _Str __enter__ = _Str("__enter__");
|
||||||
const _Str& __exit__ = _Str("__exit__");
|
const _Str __exit__ = _Str("__exit__");
|
||||||
|
|
||||||
const _Str CMP_SPECIAL_METHODS[] = {
|
const _Str CMP_SPECIAL_METHODS[] = {
|
||||||
"__lt__", "__le__", "__eq__", "__ne__", "__gt__", "__ge__"
|
"__lt__", "__le__", "__eq__", "__ne__", "__gt__", "__ge__"
|
||||||
@ -184,7 +184,7 @@ const _Str BITWISE_SPECIAL_METHODS[] = {
|
|||||||
const uint32_t __LoRangeA[] = {170,186,443,448,660,1488,1519,1568,1601,1646,1649,1749,1774,1786,1791,1808,1810,1869,1969,1994,2048,2112,2144,2208,2230,2308,2365,2384,2392,2418,2437,2447,2451,2474,2482,2486,2493,2510,2524,2527,2544,2556,2565,2575,2579,2602,2610,2613,2616,2649,2654,2674,2693,2703,2707,2730,2738,2741,2749,2768,2784,2809,2821,2831,2835,2858,2866,2869,2877,2908,2911,2929,2947,2949,2958,2962,2969,2972,2974,2979,2984,2990,3024,3077,3086,3090,3114,3133,3160,3168,3200,3205,3214,3218,3242,3253,3261,3294,3296,3313,3333,3342,3346,3389,3406,3412,3423,3450,3461,3482,3507,3517,3520,3585,3634,3648,3713,3716,3718,3724,3749,3751,3762,3773,3776,3804,3840,3904,3913,3976,4096,4159,4176,4186,4193,4197,4206,4213,4238,4352,4682,4688,4696,4698,4704,4746,4752,4786,4792,4800,4802,4808,4824,4882,4888,4992,5121,5743,5761,5792,5873,5888,5902,5920,5952,5984,5998,6016,6108,6176,6212,6272,6279,6314,6320,6400,6480,6512,6528,6576,6656,6688,6917,6981,7043,7086,7098,7168,7245,7258,7401,7406,7413,7418,8501,11568,11648,11680,11688,11696,11704,11712,11720,11728,11736,12294,12348,12353,12447,12449,12543,12549,12593,12704,12784,13312,19968,40960,40982,42192,42240,42512,42538,42606,42656,42895,42999,43003,43011,43015,43020,43072,43138,43250,43259,43261,43274,43312,43360,43396,43488,43495,43514,43520,43584,43588,43616,43633,43642,43646,43697,43701,43705,43712,43714,43739,43744,43762,43777,43785,43793,43808,43816,43968,44032,55216,55243,63744,64112,64285,64287,64298,64312,64318,64320,64323,64326,64467,64848,64914,65008,65136,65142,65382,65393,65440,65474,65482,65490,65498,65536,65549,65576,65596,65599,65616,65664,66176,66208,66304,66349,66370,66384,66432,66464,66504,66640,66816,66864,67072,67392,67424,67584,67592,67594,67639,67644,67647,67680,67712,67808,67828,67840,67872,67968,68030,68096,68112,68117,68121,68192,68224,68288,68297,68352,68416,68448,68480,68608,68864,69376,69415,69424,69600,69635,69763,69840,69891,69956,69968,70006,70019,70081,70106,70108,70144,70163,70272,70280,70282,70287,70303,70320,70405,70415,70419,70442,70450,70453,70461,70480,70493,70656,70727,70751,70784,70852,70855,71040,71128,71168,71236,71296,71352,71424,71680,71935,72096,72106,72161,72163,72192,72203,72250,72272,72284,72349,72384,72704,72714,72768,72818,72960,72968,72971,73030,73056,73063,73066,73112,73440,73728,74880,77824,82944,92160,92736,92880,92928,93027,93053,93952,94032,94208,100352,110592,110928,110948,110960,113664,113776,113792,113808,123136,123214,123584,124928,126464,126469,126497,126500,126503,126505,126516,126521,126523,126530,126535,126537,126539,126541,126545,126548,126551,126553,126555,126557,126559,126561,126564,126567,126572,126580,126585,126590,126592,126603,126625,126629,126635,131072,173824,177984,178208,183984,194560};
|
const uint32_t __LoRangeA[] = {170,186,443,448,660,1488,1519,1568,1601,1646,1649,1749,1774,1786,1791,1808,1810,1869,1969,1994,2048,2112,2144,2208,2230,2308,2365,2384,2392,2418,2437,2447,2451,2474,2482,2486,2493,2510,2524,2527,2544,2556,2565,2575,2579,2602,2610,2613,2616,2649,2654,2674,2693,2703,2707,2730,2738,2741,2749,2768,2784,2809,2821,2831,2835,2858,2866,2869,2877,2908,2911,2929,2947,2949,2958,2962,2969,2972,2974,2979,2984,2990,3024,3077,3086,3090,3114,3133,3160,3168,3200,3205,3214,3218,3242,3253,3261,3294,3296,3313,3333,3342,3346,3389,3406,3412,3423,3450,3461,3482,3507,3517,3520,3585,3634,3648,3713,3716,3718,3724,3749,3751,3762,3773,3776,3804,3840,3904,3913,3976,4096,4159,4176,4186,4193,4197,4206,4213,4238,4352,4682,4688,4696,4698,4704,4746,4752,4786,4792,4800,4802,4808,4824,4882,4888,4992,5121,5743,5761,5792,5873,5888,5902,5920,5952,5984,5998,6016,6108,6176,6212,6272,6279,6314,6320,6400,6480,6512,6528,6576,6656,6688,6917,6981,7043,7086,7098,7168,7245,7258,7401,7406,7413,7418,8501,11568,11648,11680,11688,11696,11704,11712,11720,11728,11736,12294,12348,12353,12447,12449,12543,12549,12593,12704,12784,13312,19968,40960,40982,42192,42240,42512,42538,42606,42656,42895,42999,43003,43011,43015,43020,43072,43138,43250,43259,43261,43274,43312,43360,43396,43488,43495,43514,43520,43584,43588,43616,43633,43642,43646,43697,43701,43705,43712,43714,43739,43744,43762,43777,43785,43793,43808,43816,43968,44032,55216,55243,63744,64112,64285,64287,64298,64312,64318,64320,64323,64326,64467,64848,64914,65008,65136,65142,65382,65393,65440,65474,65482,65490,65498,65536,65549,65576,65596,65599,65616,65664,66176,66208,66304,66349,66370,66384,66432,66464,66504,66640,66816,66864,67072,67392,67424,67584,67592,67594,67639,67644,67647,67680,67712,67808,67828,67840,67872,67968,68030,68096,68112,68117,68121,68192,68224,68288,68297,68352,68416,68448,68480,68608,68864,69376,69415,69424,69600,69635,69763,69840,69891,69956,69968,70006,70019,70081,70106,70108,70144,70163,70272,70280,70282,70287,70303,70320,70405,70415,70419,70442,70450,70453,70461,70480,70493,70656,70727,70751,70784,70852,70855,71040,71128,71168,71236,71296,71352,71424,71680,71935,72096,72106,72161,72163,72192,72203,72250,72272,72284,72349,72384,72704,72714,72768,72818,72960,72968,72971,73030,73056,73063,73066,73112,73440,73728,74880,77824,82944,92160,92736,92880,92928,93027,93053,93952,94032,94208,100352,110592,110928,110948,110960,113664,113776,113792,113808,123136,123214,123584,124928,126464,126469,126497,126500,126503,126505,126516,126521,126523,126530,126535,126537,126539,126541,126545,126548,126551,126553,126555,126557,126559,126561,126564,126567,126572,126580,126585,126590,126592,126603,126625,126629,126635,131072,173824,177984,178208,183984,194560};
|
||||||
const uint32_t __LoRangeB[] = {170,186,443,451,660,1514,1522,1599,1610,1647,1747,1749,1775,1788,1791,1808,1839,1957,1969,2026,2069,2136,2154,2228,2237,2361,2365,2384,2401,2432,2444,2448,2472,2480,2482,2489,2493,2510,2525,2529,2545,2556,2570,2576,2600,2608,2611,2614,2617,2652,2654,2676,2701,2705,2728,2736,2739,2745,2749,2768,2785,2809,2828,2832,2856,2864,2867,2873,2877,2909,2913,2929,2947,2954,2960,2965,2970,2972,2975,2980,2986,3001,3024,3084,3088,3112,3129,3133,3162,3169,3200,3212,3216,3240,3251,3257,3261,3294,3297,3314,3340,3344,3386,3389,3406,3414,3425,3455,3478,3505,3515,3517,3526,3632,3635,3653,3714,3716,3722,3747,3749,3760,3763,3773,3780,3807,3840,3911,3948,3980,4138,4159,4181,4189,4193,4198,4208,4225,4238,4680,4685,4694,4696,4701,4744,4749,4784,4789,4798,4800,4805,4822,4880,4885,4954,5007,5740,5759,5786,5866,5880,5900,5905,5937,5969,5996,6000,6067,6108,6210,6264,6276,6312,6314,6389,6430,6509,6516,6571,6601,6678,6740,6963,6987,7072,7087,7141,7203,7247,7287,7404,7411,7414,7418,8504,11623,11670,11686,11694,11702,11710,11718,11726,11734,11742,12294,12348,12438,12447,12538,12543,12591,12686,12730,12799,19893,40943,40980,42124,42231,42507,42527,42539,42606,42725,42895,42999,43009,43013,43018,43042,43123,43187,43255,43259,43262,43301,43334,43388,43442,43492,43503,43518,43560,43586,43595,43631,43638,43642,43695,43697,43702,43709,43712,43714,43740,43754,43762,43782,43790,43798,43814,43822,44002,55203,55238,55291,64109,64217,64285,64296,64310,64316,64318,64321,64324,64433,64829,64911,64967,65019,65140,65276,65391,65437,65470,65479,65487,65495,65500,65547,65574,65594,65597,65613,65629,65786,66204,66256,66335,66368,66377,66421,66461,66499,66511,66717,66855,66915,67382,67413,67431,67589,67592,67637,67640,67644,67669,67702,67742,67826,67829,67861,67897,68023,68031,68096,68115,68119,68149,68220,68252,68295,68324,68405,68437,68466,68497,68680,68899,69404,69415,69445,69622,69687,69807,69864,69926,69956,70002,70006,70066,70084,70106,70108,70161,70187,70278,70280,70285,70301,70312,70366,70412,70416,70440,70448,70451,70457,70461,70480,70497,70708,70730,70751,70831,70853,70855,71086,71131,71215,71236,71338,71352,71450,71723,71935,72103,72144,72161,72163,72192,72242,72250,72272,72329,72349,72440,72712,72750,72768,72847,72966,72969,73008,73030,73061,73064,73097,73112,73458,74649,75075,78894,83526,92728,92766,92909,92975,93047,93071,94026,94032,100343,101106,110878,110930,110951,111355,113770,113788,113800,113817,123180,123214,123627,125124,126467,126495,126498,126500,126503,126514,126519,126521,126523,126530,126535,126537,126539,126543,126546,126548,126551,126553,126555,126557,126559,126562,126564,126570,126578,126583,126588,126590,126601,126619,126627,126633,126651,173782,177972,178205,183969,191456,195101};
|
const uint32_t __LoRangeB[] = {170,186,443,451,660,1514,1522,1599,1610,1647,1747,1749,1775,1788,1791,1808,1839,1957,1969,2026,2069,2136,2154,2228,2237,2361,2365,2384,2401,2432,2444,2448,2472,2480,2482,2489,2493,2510,2525,2529,2545,2556,2570,2576,2600,2608,2611,2614,2617,2652,2654,2676,2701,2705,2728,2736,2739,2745,2749,2768,2785,2809,2828,2832,2856,2864,2867,2873,2877,2909,2913,2929,2947,2954,2960,2965,2970,2972,2975,2980,2986,3001,3024,3084,3088,3112,3129,3133,3162,3169,3200,3212,3216,3240,3251,3257,3261,3294,3297,3314,3340,3344,3386,3389,3406,3414,3425,3455,3478,3505,3515,3517,3526,3632,3635,3653,3714,3716,3722,3747,3749,3760,3763,3773,3780,3807,3840,3911,3948,3980,4138,4159,4181,4189,4193,4198,4208,4225,4238,4680,4685,4694,4696,4701,4744,4749,4784,4789,4798,4800,4805,4822,4880,4885,4954,5007,5740,5759,5786,5866,5880,5900,5905,5937,5969,5996,6000,6067,6108,6210,6264,6276,6312,6314,6389,6430,6509,6516,6571,6601,6678,6740,6963,6987,7072,7087,7141,7203,7247,7287,7404,7411,7414,7418,8504,11623,11670,11686,11694,11702,11710,11718,11726,11734,11742,12294,12348,12438,12447,12538,12543,12591,12686,12730,12799,19893,40943,40980,42124,42231,42507,42527,42539,42606,42725,42895,42999,43009,43013,43018,43042,43123,43187,43255,43259,43262,43301,43334,43388,43442,43492,43503,43518,43560,43586,43595,43631,43638,43642,43695,43697,43702,43709,43712,43714,43740,43754,43762,43782,43790,43798,43814,43822,44002,55203,55238,55291,64109,64217,64285,64296,64310,64316,64318,64321,64324,64433,64829,64911,64967,65019,65140,65276,65391,65437,65470,65479,65487,65495,65500,65547,65574,65594,65597,65613,65629,65786,66204,66256,66335,66368,66377,66421,66461,66499,66511,66717,66855,66915,67382,67413,67431,67589,67592,67637,67640,67644,67669,67702,67742,67826,67829,67861,67897,68023,68031,68096,68115,68119,68149,68220,68252,68295,68324,68405,68437,68466,68497,68680,68899,69404,69415,69445,69622,69687,69807,69864,69926,69956,70002,70006,70066,70084,70106,70108,70161,70187,70278,70280,70285,70301,70312,70366,70412,70416,70440,70448,70451,70457,70461,70480,70497,70708,70730,70751,70831,70853,70855,71086,71131,71215,71236,71338,71352,71450,71723,71935,72103,72144,72161,72163,72192,72242,72250,72272,72329,72349,72440,72712,72750,72768,72847,72966,72969,73008,73030,73061,73064,73097,73112,73458,74649,75075,78894,83526,92728,92766,92909,92975,93047,93071,94026,94032,100343,101106,110878,110930,110951,111355,113770,113788,113800,113817,123180,123214,123627,125124,126467,126495,126498,126500,126503,126514,126519,126521,126523,126530,126535,126537,126539,126543,126546,126548,126551,126553,126555,126557,126559,126562,126564,126570,126578,126583,126588,126590,126601,126619,126627,126633,126651,173782,177972,178205,183969,191456,195101};
|
||||||
|
|
||||||
bool __isLoChar(uint32_t c) {
|
bool is_unicode_Lo_char(uint32_t c) {
|
||||||
auto index = std::lower_bound(__LoRangeA, __LoRangeA + 476, c) - __LoRangeA;
|
auto index = std::lower_bound(__LoRangeA, __LoRangeA + 476, c) - __LoRangeA;
|
||||||
if(c == __LoRangeA[index]) return true;
|
if(c == __LoRangeA[index]) return true;
|
||||||
index -= 1;
|
index -= 1;
|
||||||
|
25
src/vm.h
25
src/vm.h
@ -493,7 +493,7 @@ public:
|
|||||||
for(int i=0; i<kwargs.size(); i+=2){
|
for(int i=0; i<kwargs.size(); i+=2){
|
||||||
const _Str& key = PyStr_AS_C(kwargs[i]);
|
const _Str& key = PyStr_AS_C(kwargs[i]);
|
||||||
if(!fn->kwArgs.contains(key)){
|
if(!fn->kwArgs.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];
|
const PyVar& val = kwargs[i+1];
|
||||||
if(!positional_overrided_keys.empty()){
|
if(!positional_overrided_keys.empty()){
|
||||||
@ -671,34 +671,34 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bindMethod(PyVar obj, _Str funcName, _CppFuncRaw fn) {
|
void bind_method(PyVar obj, _Str funcName, _CppFuncRaw fn) {
|
||||||
check_type(obj, _tp_type);
|
check_type(obj, _tp_type);
|
||||||
setattr(obj, funcName, PyNativeFunction(_CppFunc(fn, ARGC, true)));
|
setattr(obj, funcName, PyNativeFunction(_CppFunc(fn, ARGC, true)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
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)));
|
setattr(obj, funcName, PyNativeFunction(_CppFunc(fn, ARGC, false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bindMethod(_Str typeName, _Str funcName, _CppFuncRaw fn) {
|
void bind_method(_Str typeName, _Str funcName, _CppFuncRaw fn) {
|
||||||
bindMethod<ARGC>(_types[typeName], funcName, fn);
|
bind_method<ARGC>(_types[typeName], funcName, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bindStaticMethod(_Str typeName, _Str funcName, _CppFuncRaw fn) {
|
void bind_static_method(_Str typeName, _Str funcName, _CppFuncRaw fn) {
|
||||||
bindFunc<ARGC>(_types[typeName], funcName, fn);
|
bind_func<ARGC>(_types[typeName], funcName, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bindMethodMulti(std::vector<_Str> typeNames, _Str funcName, _CppFuncRaw fn) {
|
void _bind_methods(std::vector<_Str> typeNames, _Str funcName, _CppFuncRaw fn) {
|
||||||
for(auto& typeName : typeNames) bindMethod<ARGC>(typeName, funcName, fn);
|
for(auto& typeName : typeNames) bind_method<ARGC>(typeName, funcName, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bindBuiltinFunc(_Str funcName, _CppFuncRaw fn) {
|
void bind_builtin_func(_Str funcName, _CppFuncRaw fn) {
|
||||||
bindFunc<ARGC>(builtins, funcName, fn);
|
bind_func<ARGC>(builtins, funcName, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline f64 num_to_float(const PyVar& obj){
|
inline f64 num_to_float(const PyVar& obj){
|
||||||
@ -763,7 +763,7 @@ public:
|
|||||||
argStr += " (" + PyStr_AS_C(asRepr(code->consts[byte.arg])) + ")";
|
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){
|
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 << pad(argStr, 20); // may overflow
|
||||||
ss << code->blocks[byte.block].to_string();
|
ss << code->blocks[byte.block].to_string();
|
||||||
@ -941,6 +941,7 @@ public:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
PyVar register_class(PyVar mod){
|
PyVar register_class(PyVar mod){
|
||||||
PyVar type = new_user_type_object(mod, T::_name(), _tp_object);
|
PyVar type = new_user_type_object(mod, T::_name(), _tp_object);
|
||||||
|
if(OBJ_NAME(mod) != T::_mod()) UNREACHABLE();
|
||||||
T::_register(this, mod, type);
|
T::_register(this, mod, type);
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user