mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
...
This commit is contained in:
parent
644b0766b8
commit
07be627cb8
@ -10,5 +10,5 @@ These are the undefined behaviours of pkpy. The behaviour of pkpy is undefined i
|
|||||||
3. Use goto statement to jump out of a context block.
|
3. Use goto statement to jump out of a context block.
|
||||||
4. Type `T`'s `__new__` returns an object that is not an instance of `T`.
|
4. Type `T`'s `__new__` returns an object that is not an instance of `T`.
|
||||||
5. Call `__new__` with a type that is not a subclass of `type`.
|
5. Call `__new__` with a type that is not a subclass of `type`.
|
||||||
6. `__eq__`, `__ne__` or `__contains__`, etc.. returns a value that is not a boolean.
|
6. `__eq__`, `__lt__` or `__contains__`, etc.. returns a value that is not a boolean.
|
||||||
7. Division by zero.
|
7. Division by zero.
|
@ -74,9 +74,6 @@ class set:
|
|||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return self.__xor__(other).__len__() == 0
|
return self.__xor__(other).__len__() == 0
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return self.__xor__(other).__len__() != 0
|
|
||||||
|
|
||||||
def isdisjoint(self, other):
|
def isdisjoint(self, other):
|
||||||
return self.__and__(other).__len__() == 0
|
return self.__and__(other).__len__() == 0
|
||||||
|
|
||||||
|
@ -73,10 +73,6 @@ class deque:
|
|||||||
t1, t2 = t1.next, t2.next
|
t1, t2 = t1.next, t2.next
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def __ne__(self, __o: object) -> bool:
|
|
||||||
return not self == __o
|
|
||||||
|
|
||||||
|
|
||||||
def Counter(iterable):
|
def Counter(iterable):
|
||||||
a = {}
|
a = {}
|
||||||
for x in iterable:
|
for x in iterable:
|
||||||
|
@ -337,7 +337,7 @@ __NEXT_STEP:;
|
|||||||
TARGET(COMPARE_NE)
|
TARGET(COMPARE_NE)
|
||||||
_1 = POPX();
|
_1 = POPX();
|
||||||
_0 = TOP();
|
_0 = TOP();
|
||||||
TOP() = VAR(py_not_equals(_0, _1));
|
TOP() = VAR(!py_equals(_0, _1));
|
||||||
DISPATCH()
|
DISPATCH()
|
||||||
TARGET(COMPARE_GT)
|
TARGET(COMPARE_GT)
|
||||||
BINARY_OP_SPECIAL(__gt__);
|
BINARY_OP_SPECIAL(__gt__);
|
||||||
|
11
src/cffi.h
11
src/cffi.h
@ -62,10 +62,6 @@ struct VoidP{
|
|||||||
if(!is_non_tagged_type(rhs, VoidP::_type(vm))) return false;
|
if(!is_non_tagged_type(rhs, VoidP::_type(vm))) return false;
|
||||||
return _CAST(VoidP&, lhs) == _CAST(VoidP&, rhs);
|
return _CAST(VoidP&, lhs) == _CAST(VoidP&, rhs);
|
||||||
});
|
});
|
||||||
vm->bind__ne__(OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
|
|
||||||
if(!is_non_tagged_type(rhs, VoidP::_type(vm))) return true;
|
|
||||||
return _CAST(VoidP&, lhs) != _CAST(VoidP&, rhs);
|
|
||||||
});
|
|
||||||
vm->bind__gt__(OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
|
vm->bind__gt__(OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
|
||||||
return _CAST(VoidP&, lhs).ptr > CAST(VoidP&, rhs).ptr;
|
return _CAST(VoidP&, lhs).ptr > CAST(VoidP&, rhs).ptr;
|
||||||
});
|
});
|
||||||
@ -229,13 +225,6 @@ struct C99Struct{
|
|||||||
return self.size == other.size && memcmp(self.p, other.p, self.size) == 0;
|
return self.size == other.size && memcmp(self.p, other.p, self.size) == 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind__ne__(OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
|
|
||||||
C99Struct& self = _CAST(C99Struct&, lhs);
|
|
||||||
if(!is_non_tagged_type(rhs, C99Struct::_type(vm))) return true;
|
|
||||||
C99Struct& other = _CAST(C99Struct&, rhs);
|
|
||||||
return self.size != other.size || memcmp(self.p, other.p, self.size) != 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// patch VoidP
|
// patch VoidP
|
||||||
type = vm->_t(VoidP::_type(vm));
|
type = vm->_t(VoidP::_type(vm));
|
||||||
|
|
||||||
|
@ -356,7 +356,6 @@ struct PyVec2: Vec2 {
|
|||||||
BIND_VEC_FLOAT_OP(2, __mul__, *)
|
BIND_VEC_FLOAT_OP(2, __mul__, *)
|
||||||
BIND_VEC_FLOAT_OP(2, __truediv__, /)
|
BIND_VEC_FLOAT_OP(2, __truediv__, /)
|
||||||
BIND_VEC_VEC_OP(2, __eq__, ==)
|
BIND_VEC_VEC_OP(2, __eq__, ==)
|
||||||
BIND_VEC_VEC_OP(2, __ne__, !=)
|
|
||||||
BIND_VEC_FIELD(2, x)
|
BIND_VEC_FIELD(2, x)
|
||||||
BIND_VEC_FIELD(2, y)
|
BIND_VEC_FIELD(2, y)
|
||||||
BIND_VEC_FUNCTION_1(2, dot)
|
BIND_VEC_FUNCTION_1(2, dot)
|
||||||
@ -399,7 +398,6 @@ struct PyVec3: Vec3 {
|
|||||||
BIND_VEC_FLOAT_OP(3, __mul__, *)
|
BIND_VEC_FLOAT_OP(3, __mul__, *)
|
||||||
BIND_VEC_FLOAT_OP(3, __truediv__, /)
|
BIND_VEC_FLOAT_OP(3, __truediv__, /)
|
||||||
BIND_VEC_VEC_OP(3, __eq__, ==)
|
BIND_VEC_VEC_OP(3, __eq__, ==)
|
||||||
BIND_VEC_VEC_OP(3, __ne__, !=)
|
|
||||||
BIND_VEC_FIELD(3, x)
|
BIND_VEC_FIELD(3, x)
|
||||||
BIND_VEC_FIELD(3, y)
|
BIND_VEC_FIELD(3, y)
|
||||||
BIND_VEC_FIELD(3, z)
|
BIND_VEC_FIELD(3, z)
|
||||||
@ -573,12 +571,6 @@ struct PyMat3x3: Mat3x3{
|
|||||||
return VAR(self == other);
|
return VAR(self == other);
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_method<1>(type, "__ne__", [](VM* vm, ArgsView args){
|
|
||||||
PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
|
|
||||||
PyMat3x3& other = CAST(PyMat3x3&, args[1]);
|
|
||||||
return VAR(self != other);
|
|
||||||
});
|
|
||||||
|
|
||||||
vm->bind_method<0>(type, "determinant", [](VM* vm, ArgsView args){
|
vm->bind_method<0>(type, "determinant", [](VM* vm, ArgsView args){
|
||||||
PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
|
PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
|
||||||
return VAR(self.determinant());
|
return VAR(self.determinant());
|
||||||
|
@ -70,7 +70,6 @@ inline void init_builtins(VM* _vm) {
|
|||||||
BIND_NUM_LOGICAL_OPT(__gt__, >, false)
|
BIND_NUM_LOGICAL_OPT(__gt__, >, false)
|
||||||
BIND_NUM_LOGICAL_OPT(__ge__, >=, false)
|
BIND_NUM_LOGICAL_OPT(__ge__, >=, false)
|
||||||
BIND_NUM_LOGICAL_OPT(__eq__, ==, true)
|
BIND_NUM_LOGICAL_OPT(__eq__, ==, true)
|
||||||
BIND_NUM_LOGICAL_OPT(__ne__, !=, true)
|
|
||||||
|
|
||||||
#undef BIND_NUM_ARITH_OPT
|
#undef BIND_NUM_ARITH_OPT
|
||||||
#undef BIND_NUM_LOGICAL_OPT
|
#undef BIND_NUM_LOGICAL_OPT
|
||||||
@ -206,7 +205,6 @@ inline void init_builtins(VM* _vm) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind__eq__(_vm->tp_object, [](VM* vm, PyObject* lhs, PyObject* rhs) { return lhs == rhs; });
|
_vm->bind__eq__(_vm->tp_object, [](VM* vm, PyObject* lhs, PyObject* rhs) { return lhs == rhs; });
|
||||||
_vm->bind__ne__(_vm->tp_object, [](VM* vm, PyObject* lhs, PyObject* rhs) { return lhs != rhs; });
|
|
||||||
_vm->bind__hash__(_vm->tp_object, [](VM* vm, PyObject* obj) { return BITS(obj); });
|
_vm->bind__hash__(_vm->tp_object, [](VM* vm, PyObject* obj) { return BITS(obj); });
|
||||||
|
|
||||||
_vm->bind_constructor<2>("type", CPP_LAMBDA(vm->_t(args[1])));
|
_vm->bind_constructor<2>("type", CPP_LAMBDA(vm->_t(args[1])));
|
||||||
@ -390,10 +388,6 @@ inline void init_builtins(VM* _vm) {
|
|||||||
if(!is_non_tagged_type(rhs, vm->tp_str)) return false;
|
if(!is_non_tagged_type(rhs, vm->tp_str)) return false;
|
||||||
return _CAST(Str&, lhs) == _CAST(Str&, rhs);
|
return _CAST(Str&, lhs) == _CAST(Str&, rhs);
|
||||||
});
|
});
|
||||||
_vm->bind__ne__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
|
||||||
if(!is_non_tagged_type(rhs, vm->tp_str)) return true;
|
|
||||||
return _CAST(Str&, lhs) != _CAST(Str&, rhs);
|
|
||||||
});
|
|
||||||
_vm->bind__gt__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
_vm->bind__gt__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
||||||
return _CAST(Str&, lhs) > CAST(Str&, rhs);
|
return _CAST(Str&, lhs) > CAST(Str&, rhs);
|
||||||
});
|
});
|
||||||
@ -506,15 +500,11 @@ inline void init_builtins(VM* _vm) {
|
|||||||
List& b = _CAST(List&, rhs);
|
List& b = _CAST(List&, rhs);
|
||||||
if(a.size() != b.size()) return false;
|
if(a.size() != b.size()) return false;
|
||||||
for(int i=0; i<a.size(); i++){
|
for(int i=0; i<a.size(); i++){
|
||||||
if(vm->py_not_equals(a[i], b[i])) return false;
|
if(!vm->py_equals(a[i], b[i])) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind__ne__(_vm->tp_list, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
|
||||||
return !vm->py_equals(lhs, rhs);
|
|
||||||
});
|
|
||||||
|
|
||||||
_vm->bind_method<1>("list", "index", [](VM* vm, ArgsView args) {
|
_vm->bind_method<1>("list", "index", [](VM* vm, ArgsView args) {
|
||||||
List& self = _CAST(List&, args[0]);
|
List& self = _CAST(List&, args[0]);
|
||||||
PyObject* obj = args[1];
|
PyObject* obj = args[1];
|
||||||
@ -663,15 +653,11 @@ inline void init_builtins(VM* _vm) {
|
|||||||
const Tuple& other = CAST(Tuple&, rhs);
|
const Tuple& other = CAST(Tuple&, rhs);
|
||||||
if(self.size() != other.size()) return false;
|
if(self.size() != other.size()) return false;
|
||||||
for(int i = 0; i < self.size(); i++) {
|
for(int i = 0; i < self.size(); i++) {
|
||||||
if(vm->py_not_equals(self[i], other[i])) return false;
|
if(!vm->py_equals(self[i], other[i])) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind__ne__(_vm->tp_tuple, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
|
||||||
return !vm->py_equals(lhs, rhs);
|
|
||||||
});
|
|
||||||
|
|
||||||
_vm->bind__hash__(_vm->tp_tuple, [](VM* vm, PyObject* obj) {
|
_vm->bind__hash__(_vm->tp_tuple, [](VM* vm, PyObject* obj) {
|
||||||
i64 x = 1000003;
|
i64 x = 1000003;
|
||||||
const Tuple& items = CAST(Tuple&, obj);
|
const Tuple& items = CAST(Tuple&, obj);
|
||||||
@ -718,9 +704,6 @@ inline void init_builtins(VM* _vm) {
|
|||||||
_vm->bind__eq__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
_vm->bind__eq__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
||||||
return _CAST(bool, lhs) == CAST(bool, rhs);
|
return _CAST(bool, lhs) == CAST(bool, rhs);
|
||||||
});
|
});
|
||||||
_vm->bind__ne__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
|
||||||
return _CAST(bool, lhs) != CAST(bool, rhs);
|
|
||||||
});
|
|
||||||
_vm->bind__repr__(_vm->_type("ellipsis"), [](VM* vm, PyObject* self) {
|
_vm->bind__repr__(_vm->_type("ellipsis"), [](VM* vm, PyObject* self) {
|
||||||
return VAR("Ellipsis");
|
return VAR("Ellipsis");
|
||||||
});
|
});
|
||||||
@ -787,10 +770,6 @@ inline void init_builtins(VM* _vm) {
|
|||||||
_vm->bind__eq__(_vm->tp_bytes, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
_vm->bind__eq__(_vm->tp_bytes, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
||||||
return _CAST(Bytes&, lhs) == _CAST(Bytes&, rhs);
|
return _CAST(Bytes&, lhs) == _CAST(Bytes&, rhs);
|
||||||
});
|
});
|
||||||
_vm->bind__ne__(_vm->tp_bytes, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
|
||||||
return _CAST(Bytes&, lhs) != _CAST(Bytes&, rhs);
|
|
||||||
});
|
|
||||||
|
|
||||||
/************ slice ************/
|
/************ slice ************/
|
||||||
_vm->bind_constructor<4>("slice", [](VM* vm, ArgsView args) {
|
_vm->bind_constructor<4>("slice", [](VM* vm, ArgsView args) {
|
||||||
return VAR(Slice(args[1], args[2], args[3]));
|
return VAR(Slice(args[1], args[2], args[3]));
|
||||||
@ -1016,10 +995,6 @@ inline void init_builtins(VM* _vm) {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind__ne__(_vm->tp_dict, [](VM* vm, PyObject* a, PyObject* b) {
|
|
||||||
return !vm->py_equals(a, b);
|
|
||||||
});
|
|
||||||
|
|
||||||
RangeIter::register_class(_vm, _vm->builtins);
|
RangeIter::register_class(_vm, _vm->builtins);
|
||||||
ArrayIter::register_class(_vm, _vm->builtins);
|
ArrayIter::register_class(_vm, _vm->builtins);
|
||||||
StringIter::register_class(_vm, _vm->builtins);
|
StringIter::register_class(_vm, _vm->builtins);
|
||||||
@ -1338,11 +1313,6 @@ inline void VM::post_init(){
|
|||||||
if(!is_non_tagged_type(rhs, vm->tp_bound_method)) return false;
|
if(!is_non_tagged_type(rhs, vm->tp_bound_method)) return false;
|
||||||
return _CAST(BoundMethod&, lhs) == _CAST(BoundMethod&, rhs);
|
return _CAST(BoundMethod&, lhs) == _CAST(BoundMethod&, rhs);
|
||||||
});
|
});
|
||||||
bind__ne__(tp_bound_method, [](VM* vm, PyObject* lhs, PyObject* rhs){
|
|
||||||
if(!is_non_tagged_type(rhs, vm->tp_bound_method)) return true;
|
|
||||||
return _CAST(BoundMethod&, lhs) != _CAST(BoundMethod&, rhs);
|
|
||||||
});
|
|
||||||
|
|
||||||
_t(tp_slice)->attr().set("start", property([](VM* vm, ArgsView args){
|
_t(tp_slice)->attr().set("start", property([](VM* vm, ArgsView args){
|
||||||
return CAST(Slice&, args[0]).start;
|
return CAST(Slice&, args[0]).start;
|
||||||
}));
|
}));
|
||||||
|
@ -389,7 +389,6 @@ const StrName __neg__ = StrName::get("__neg__"); // unused
|
|||||||
const StrName __bool__ = StrName::get("__bool__"); // unused
|
const StrName __bool__ = StrName::get("__bool__"); // unused
|
||||||
// logical operators
|
// logical operators
|
||||||
const StrName __eq__ = StrName::get("__eq__");
|
const StrName __eq__ = StrName::get("__eq__");
|
||||||
const StrName __ne__ = StrName::get("__ne__");
|
|
||||||
const StrName __lt__ = StrName::get("__lt__");
|
const StrName __lt__ = StrName::get("__lt__");
|
||||||
const StrName __le__ = StrName::get("__le__");
|
const StrName __le__ = StrName::get("__le__");
|
||||||
const StrName __gt__ = StrName::get("__gt__");
|
const StrName __gt__ = StrName::get("__gt__");
|
||||||
|
13
src/vm.h
13
src/vm.h
@ -67,7 +67,6 @@ struct PyTypeInfo{
|
|||||||
PyObject* (*m__bool__)(VM* vm, PyObject*) = nullptr;
|
PyObject* (*m__bool__)(VM* vm, PyObject*) = nullptr;
|
||||||
|
|
||||||
bool (*m__eq__)(VM* vm, PyObject*, PyObject*) = nullptr;
|
bool (*m__eq__)(VM* vm, PyObject*, PyObject*) = nullptr;
|
||||||
bool (*m__ne__)(VM* vm, PyObject*, PyObject*) = nullptr;
|
|
||||||
bool (*m__lt__)(VM* vm, PyObject*, PyObject*) = nullptr;
|
bool (*m__lt__)(VM* vm, PyObject*, PyObject*) = nullptr;
|
||||||
bool (*m__le__)(VM* vm, PyObject*, PyObject*) = nullptr;
|
bool (*m__le__)(VM* vm, PyObject*, PyObject*) = nullptr;
|
||||||
bool (*m__gt__)(VM* vm, PyObject*, PyObject*) = nullptr;
|
bool (*m__gt__)(VM* vm, PyObject*, PyObject*) = nullptr;
|
||||||
@ -325,6 +324,10 @@ public:
|
|||||||
return &_all_types[OBJ_GET(Type, obj)];
|
return &_all_types[OBJ_GET(Type, obj)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PyTypeInfo* _type_info(Type type){
|
||||||
|
return &_all_types[type];
|
||||||
|
}
|
||||||
|
|
||||||
const PyTypeInfo* _inst_type_info(PyObject* obj){
|
const PyTypeInfo* _inst_type_info(PyObject* obj){
|
||||||
if(is_int(obj)) return &_all_types[tp_int];
|
if(is_int(obj)) return &_all_types[tp_int];
|
||||||
if(is_float(obj)) return &_all_types[tp_float];
|
if(is_float(obj)) return &_all_types[tp_float];
|
||||||
@ -365,7 +368,6 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
BIND_LOGICAL_SPECIAL(__eq__)
|
BIND_LOGICAL_SPECIAL(__eq__)
|
||||||
BIND_LOGICAL_SPECIAL(__ne__)
|
|
||||||
BIND_LOGICAL_SPECIAL(__lt__)
|
BIND_LOGICAL_SPECIAL(__lt__)
|
||||||
BIND_LOGICAL_SPECIAL(__le__)
|
BIND_LOGICAL_SPECIAL(__le__)
|
||||||
BIND_LOGICAL_SPECIAL(__gt__)
|
BIND_LOGICAL_SPECIAL(__gt__)
|
||||||
@ -438,13 +440,6 @@ public:
|
|||||||
return call_method(lhs, __eq__, rhs) == True;
|
return call_method(lhs, __eq__, rhs) == True;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool py_not_equals(PyObject* lhs, PyObject* rhs){
|
|
||||||
if(lhs == rhs) return false;
|
|
||||||
const PyTypeInfo* ti = _inst_type_info(lhs);
|
|
||||||
if(ti->m__ne__) return ti->m__ne__(this, lhs, rhs);
|
|
||||||
return call_method(lhs, __ne__, rhs) == True;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
PyObject* bind_func(Str type, Str name, NativeFuncC fn) {
|
PyObject* bind_func(Str type, Str name, NativeFuncC fn) {
|
||||||
return bind_func<ARGC>(_find_type_object(type), name, fn);
|
return bind_func<ARGC>(_find_type_object(type), name, fn);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user