diff --git a/src/interpreter/py_number.c b/src/interpreter/py_number.c index 93729f23..0866a5e1 100644 --- a/src/interpreter/py_number.c +++ b/src/interpreter/py_number.c @@ -339,6 +339,53 @@ static bool _py_float__new__(int argc, py_Ref argv) { return true; } +// tp_bool +static bool _py_bool__new__(int argc, py_Ref argv){ + PY_CHECK_ARGC(1); + int res = py_bool(argv); + if(res == -1) return false; + py_newbool(py_retval(), res); + return true; +} + +static bool _py_bool__hash__(int argc, py_Ref argv){ + PY_CHECK_ARGC(1); + bool res = py_tobool(argv); + py_newint(py_retval(), res); + return true; +} + +static bool _py_bool__repr__(int argc, py_Ref argv){ + PY_CHECK_ARGC(1); + bool res = py_tobool(argv); + py_newstr(py_retval(), res ? "True" : "False"); + return true; +} + +static bool _py_bool__eq__(int argc, py_Ref argv){ + PY_CHECK_ARGC(2); + bool lhs = py_tobool(&argv[0]); + if(argv[1].type == tp_bool){ + bool rhs = py_tobool(&argv[1]); + py_newbool(py_retval(), lhs == rhs); + } else { + py_newnotimplemented(py_retval()); + } + return true; +} + +static bool _py_bool__ne__(int argc, py_Ref argv){ + PY_CHECK_ARGC(2); + bool lhs = py_tobool(&argv[0]); + if(argv[1].type == tp_bool){ + bool rhs = py_tobool(&argv[1]); + py_newbool(py_retval(), lhs != rhs); + } else { + py_newnotimplemented(py_retval()); + } + return true; +} + void pk_number__register() { /****** tp_int & tp_float ******/ py_bindmagic(tp_int, __add__, _py_int__add__); @@ -400,4 +447,11 @@ void pk_number__register() { // int.bit_length py_bindmethod(tp_int, "bit_length", _py_int__bit_length); + + /* tp_bool */ + py_bindmagic(tp_bool, __new__, _py_bool__new__); + py_bindmagic(tp_bool, __hash__, _py_bool__hash__); + py_bindmagic(tp_bool, __repr__, _py_bool__repr__); + py_bindmagic(tp_bool, __eq__, _py_bool__eq__); + py_bindmagic(tp_bool, __ne__, _py_bool__ne__); } \ No newline at end of file diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 6232aaf7..1ebf6e2a 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -98,8 +98,9 @@ void pk_VM__ctor(pk_VM* self) { validate(tp_int, pk_VM__new_type(self, "int", tp_object, NULL, false)); validate(tp_float, pk_VM__new_type(self, "float", tp_object, NULL, false)); - pk_number__register(); validate(tp_bool, pk_VM__new_type(self, "bool", tp_object, NULL, false)); + pk_number__register(); + validate(tp_str, pk_str__register()); validate(tp_list, pk_list__register());