mirror of
https://github.com/pocketpy/pocketpy
synced 2025-12-07 10:40:16 +00:00
fix eq problem
This commit is contained in:
parent
553d02592f
commit
630828f6e9
@ -73,12 +73,10 @@ tuple.__contains__ = __iterable4__contains__
|
|||||||
|
|
||||||
# https://github.com/python/cpython/blob/main/Objects/dictobject.c
|
# https://github.com/python/cpython/blob/main/Objects/dictobject.c
|
||||||
class dict:
|
class dict:
|
||||||
def __init__(self, tuples):
|
def __init__(self):
|
||||||
self._capacity = 8
|
self._capacity = 8
|
||||||
self._a = [None] * self._capacity
|
self._a = [None] * self._capacity
|
||||||
self._len = 0
|
self._len = 0
|
||||||
for i in tuples:
|
|
||||||
self[i[0]] = i[1]
|
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self._len
|
return self._len
|
||||||
@ -92,20 +90,17 @@ class dict:
|
|||||||
return False,i
|
return False,i
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
ret = self.__probe(key)
|
ok, i = self.__probe(key)
|
||||||
ok = ret[0]; i = ret[1]
|
|
||||||
if not ok:
|
if not ok:
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
return self._a[i][1]
|
return self._a[i][1]
|
||||||
|
|
||||||
def __contains__(self, key):
|
def __contains__(self, key):
|
||||||
ret = self.__probe(key)
|
ok, i = self.__probe(key)
|
||||||
ok = ret[0]; i = ret[1]
|
|
||||||
return ok
|
return ok
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
ret = self.__probe(key)
|
ok, i = self.__probe(key)
|
||||||
ok = ret[0]; i = ret[1]
|
|
||||||
if ok:
|
if ok:
|
||||||
self._a[i][1] = value
|
self._a[i][1] = value
|
||||||
else:
|
else:
|
||||||
@ -115,8 +110,7 @@ class dict:
|
|||||||
self.__resize_2x()
|
self.__resize_2x()
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
ret = self.__probe(key)
|
ok, i = self.__probe(key)
|
||||||
ok = ret[0]; i = ret[1]
|
|
||||||
if not ok:
|
if not ok:
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
self._a[i] = None
|
self._a[i] = None
|
||||||
|
|||||||
@ -400,7 +400,6 @@ public:
|
|||||||
matchNewLines();
|
matchNewLines();
|
||||||
if (peek() == TK("}")) break;
|
if (peek() == TK("}")) break;
|
||||||
EXPR();consume(TK(":"));EXPR();
|
EXPR();consume(TK(":"));EXPR();
|
||||||
emitCode(OP_BUILD_SMART_TUPLE, 2);
|
|
||||||
size++;
|
size++;
|
||||||
matchNewLines();
|
matchNewLines();
|
||||||
} while (match(TK(",")));
|
} while (match(TK(",")));
|
||||||
|
|||||||
@ -39,7 +39,6 @@ void __initializeBuiltinFunctions(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
|
||||||
@ -200,15 +199,9 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
_vm->bindMethod("str", "__eq__", [](VM* vm, PyVarList args) {
|
_vm->bindMethod("str", "__eq__", [](VM* vm, PyVarList args) {
|
||||||
const _Str& _self = vm->PyStr_AS_C(args[0]);
|
if(args.at(0)->isType(vm->_tp_str) && args.at(1)->isType(vm->_tp_str))
|
||||||
const _Str& _other = vm->PyStr_AS_C(args[1]);
|
return vm->PyBool(vm->PyStr_AS_C(args[0]) == vm->PyStr_AS_C(args[1]));
|
||||||
return vm->PyBool(_self == _other);
|
return vm->PyBool(args[0] == args[1]); // fallback
|
||||||
});
|
|
||||||
|
|
||||||
_vm->bindMethod("str", "__ne__", [](VM* vm, PyVarList args) {
|
|
||||||
const _Str& _self = vm->PyStr_AS_C(args[0]);
|
|
||||||
const _Str& _other = vm->PyStr_AS_C(args[1]);
|
|
||||||
return vm->PyBool(_self != _other);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bindMethod("str", "__getitem__", [](VM* vm, PyVarList args) {
|
_vm->bindMethod("str", "__getitem__", [](VM* vm, PyVarList args) {
|
||||||
|
|||||||
@ -147,7 +147,7 @@ const _Str& __init__ = _Str("__init__");
|
|||||||
|
|
||||||
const _Str CMP_SPECIAL_METHODS[] = {
|
const _Str CMP_SPECIAL_METHODS[] = {
|
||||||
"__lt__", "__le__", "__eq__", "__ne__", "__gt__", "__ge__"
|
"__lt__", "__le__", "__eq__", "__ne__", "__gt__", "__ge__"
|
||||||
};
|
}; // __ne__ should not be used
|
||||||
|
|
||||||
const _Str BIN_SPECIAL_METHODS[] = {
|
const _Str BIN_SPECIAL_METHODS[] = {
|
||||||
"__add__", "__sub__", "__mul__", "__truediv__", "__floordiv__", "__mod__", "__pow__"
|
"__add__", "__sub__", "__mul__", "__truediv__", "__floordiv__", "__mod__", "__pow__"
|
||||||
|
|||||||
18
src/vm.h
18
src/vm.h
@ -240,7 +240,16 @@ public:
|
|||||||
} break;
|
} break;
|
||||||
case OP_POP_TOP: frame->popValue(this); break;
|
case OP_POP_TOP: frame->popValue(this); break;
|
||||||
case OP_BINARY_OP: BINARY_XXX(byte.arg) break;
|
case OP_BINARY_OP: BINARY_XXX(byte.arg) break;
|
||||||
case OP_COMPARE_OP: COMPARE_XXX(byte.arg) break;
|
case OP_COMPARE_OP:
|
||||||
|
{
|
||||||
|
PyVar rhs = frame->popValue(this);
|
||||||
|
PyVar lhs = frame->popValue(this);
|
||||||
|
// for __ne__ we use the negation of __eq__
|
||||||
|
int op = byte.arg == 3 ? 2 : byte.arg;
|
||||||
|
PyVar res = fastCall(lhs, CMP_SPECIAL_METHODS[op], {lhs,rhs});
|
||||||
|
if(op != byte.arg) res = PyBool(!PyBool_AS_C(res));
|
||||||
|
frame->push(res);
|
||||||
|
} break;
|
||||||
case OP_IS_OP:
|
case OP_IS_OP:
|
||||||
{
|
{
|
||||||
bool ret_c = frame->popValue(this) == frame->popValue(this);
|
bool ret_c = frame->popValue(this) == frame->popValue(this);
|
||||||
@ -290,8 +299,11 @@ public:
|
|||||||
} break;
|
} break;
|
||||||
case OP_BUILD_MAP:
|
case OP_BUILD_MAP:
|
||||||
{
|
{
|
||||||
PyVarList items = frame->popNValuesReversed(this, byte.arg);
|
PyVarList items = frame->popNValuesReversed(this, byte.arg*2);
|
||||||
PyVar obj = call(builtins->attribs["dict"], {PyList(items)});
|
PyVar obj = call(builtins->attribs["dict"], {});
|
||||||
|
for(int i=0; i<items.size(); i+=2){
|
||||||
|
call(obj, __setitem__, {items[i], items[i+1]});
|
||||||
|
}
|
||||||
frame->push(obj);
|
frame->push(obj);
|
||||||
} break;
|
} break;
|
||||||
case OP_DUP_TOP: frame->push(frame->topValue(this)); break;
|
case OP_DUP_TOP: frame->push(frame->topValue(this)); break;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user