some refactor

This commit is contained in:
blueloveTH 2025-06-14 15:44:18 +08:00
parent 3966777d4b
commit 25cbd9c1f1
3 changed files with 64 additions and 70 deletions

View File

@ -251,8 +251,6 @@ PK_API py_Name py_namev(c11_sv);
/// Convert a name to a `c11_sv`. /// Convert a name to a `c11_sv`.
PK_API c11_sv py_name2sv(py_Name); PK_API c11_sv py_name2sv(py_Name);
#define py_ismagicname(name) (true)
/************* Meta Operations *************/ /************* Meta Operations *************/
/// Create a new type. /// Create a new type.
@ -612,12 +610,18 @@ PK_API int py_equal(py_Ref lhs, py_Ref rhs) PY_RAISE;
/// 1: lhs < rhs, 0: lhs >= rhs, -1: error /// 1: lhs < rhs, 0: lhs >= rhs, -1: error
PK_API int py_less(py_Ref lhs, py_Ref rhs) PY_RAISE; PK_API int py_less(py_Ref lhs, py_Ref rhs) PY_RAISE;
#define py_eq(lhs, rhs) py_binaryop(lhs, rhs, __eq__, __eq__) /// lhs == rhs
#define py_ne(lhs, rhs) py_binaryop(lhs, rhs, __ne__, __ne__) PK_API bool py_eq(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
#define py_lt(lhs, rhs) py_binaryop(lhs, rhs, __lt__, __gt__) /// lhs != rhs
#define py_le(lhs, rhs) py_binaryop(lhs, rhs, __le__, __ge__) PK_API bool py_ne(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
#define py_gt(lhs, rhs) py_binaryop(lhs, rhs, __gt__, __lt__) /// lhs < rhs
#define py_ge(lhs, rhs) py_binaryop(lhs, rhs, __ge__, __le__) PK_API bool py_lt(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// lhs <= rhs
PK_API bool py_le(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// lhs > rhs
PK_API bool py_gt(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// lhs >= rhs
PK_API bool py_ge(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// Python equivalent to `callable(val)`. /// Python equivalent to `callable(val)`.
PK_API bool py_callable(py_Ref val); PK_API bool py_callable(py_Ref val);

View File

@ -602,12 +602,12 @@ FrameResult VM__run_top_frame(VM* self) {
DISPATCH(); DISPATCH();
} }
/*****************************/ /*****************************/
#define CASE_BINARY_OP(label, op, rop)\ #define CASE_BINARY_OP(label, op, rop) \
case label: {\ case label: { \
if(!pk_stack_binaryop(self, op, rop)) goto __ERROR;\ if(!pk_stack_binaryop(self, op, rop)) goto __ERROR; \
POP();\ POP(); \
*TOP() = self->last_retval;\ *TOP() = self->last_retval; \
DISPATCH();\ DISPATCH(); \
} }
CASE_BINARY_OP(OP_BINARY_ADD, __add__, __radd__) CASE_BINARY_OP(OP_BINARY_ADD, __add__, __radd__)
CASE_BINARY_OP(OP_BINARY_SUB, __sub__, __rsub__) CASE_BINARY_OP(OP_BINARY_SUB, __sub__, __rsub__)
@ -1319,17 +1319,11 @@ bool py_binaryop(py_Ref lhs, py_Ref rhs, py_Name op, py_Name rop) {
return ok; return ok;
} }
bool py_binaryadd(py_Ref lhs, py_Ref rhs) { bool py_binaryadd(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __add__, __radd__); }
return py_binaryop(lhs, rhs, __add__, __radd__);
}
bool py_binarysub(py_Ref lhs, py_Ref rhs) { bool py_binarysub(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __sub__, __rsub__); }
return py_binaryop(lhs, rhs, __sub__, __rsub__);
}
bool py_binarymul(py_Ref lhs, py_Ref rhs) { bool py_binarymul(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __mul__, __rmul__); }
return py_binaryop(lhs, rhs, __mul__, __rmul__);
}
bool py_binarytruediv(py_Ref lhs, py_Ref rhs) { bool py_binarytruediv(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __truediv__, __rtruediv__); return py_binaryop(lhs, rhs, __truediv__, __rtruediv__);
@ -1339,37 +1333,33 @@ bool py_binaryfloordiv(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __floordiv__, __rfloordiv__); return py_binaryop(lhs, rhs, __floordiv__, __rfloordiv__);
} }
bool py_binarymod(py_Ref lhs, py_Ref rhs) { bool py_binarymod(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __mod__, __rmod__); }
return py_binaryop(lhs, rhs, __mod__, __rmod__);
}
bool py_binarypow(py_Ref lhs, py_Ref rhs) { bool py_binarypow(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __pow__, __rpow__); }
return py_binaryop(lhs, rhs, __pow__, __rpow__);
}
bool py_binarylshift(py_Ref lhs, py_Ref rhs) { bool py_binarylshift(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __lshift__, 0); }
return py_binaryop(lhs, rhs, __lshift__, 0);
}
bool py_binaryrshift(py_Ref lhs, py_Ref rhs) { bool py_binaryrshift(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __rshift__, 0); }
return py_binaryop(lhs, rhs, __rshift__, 0);
}
bool py_binaryand(py_Ref lhs, py_Ref rhs) { bool py_binaryand(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __and__, 0); }
return py_binaryop(lhs, rhs, __and__, 0);
}
bool py_binaryor(py_Ref lhs, py_Ref rhs) { bool py_binaryor(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __or__, 0); }
return py_binaryop(lhs, rhs, __or__, 0);
}
bool py_binaryxor(py_Ref lhs, py_Ref rhs) { bool py_binaryxor(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __xor__, 0); }
return py_binaryop(lhs, rhs, __xor__, 0);
}
bool py_binarymatmul(py_Ref lhs, py_Ref rhs) { bool py_binarymatmul(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __matmul__, 0); }
return py_binaryop(lhs, rhs, __matmul__, 0);
} bool py_eq(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __eq__, __eq__); }
bool py_ne(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __ne__, __ne__); }
bool py_lt(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __lt__, __gt__); }
bool py_le(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __le__, __ge__); }
bool py_gt(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __gt__, __lt__); }
bool py_ge(py_Ref lhs, py_Ref rhs) { return py_binaryop(lhs, rhs, __ge__, __le__); }
static bool stack_format_object(VM* self, c11_sv spec) { static bool stack_format_object(VM* self, c11_sv spec) {
// format TOS via `spec` inplace // format TOS via `spec` inplace

View File

@ -233,7 +233,7 @@ bool pk_loadmethod(py_StackRef self, py_Name name) {
} }
py_Ref py_tpfindmagic(py_Type t, py_Name name) { py_Ref py_tpfindmagic(py_Type t, py_Name name) {
assert(py_ismagicname(name)); // assert(py_ismagicname(name));
return py_tpfindname(t, name); return py_tpfindname(t, name);
} }
@ -260,7 +260,7 @@ py_Type py_tpbase(py_Type t) {
} }
PK_DEPRECATED py_Ref py_tpgetmagic(py_Type type, py_Name name) { PK_DEPRECATED py_Ref py_tpgetmagic(py_Type type, py_Name name) {
assert(py_ismagicname(name)); // assert(py_ismagicname(name));
py_TypeInfo* ti = pk__type_info(type); py_TypeInfo* ti = pk__type_info(type);
py_Ref retval = py_getdict(&ti->self, name); py_Ref retval = py_getdict(&ti->self, name);
return retval != NULL ? retval : py_NIL(); return retval != NULL ? retval : py_NIL();
@ -283,7 +283,7 @@ bool py_tpcall(py_Type type, int argc, py_Ref argv) {
bool pk_callmagic(py_Name name, int argc, py_Ref argv) { bool pk_callmagic(py_Name name, int argc, py_Ref argv) {
assert(argc >= 1); assert(argc >= 1);
assert(py_ismagicname(name)); // assert(py_ismagicname(name));
py_Ref tmp = py_tpfindmagic(argv->type, name); py_Ref tmp = py_tpfindmagic(argv->type, name);
if(!tmp) return AttributeError(argv, name); if(!tmp) return AttributeError(argv, name);
return py_call(tmp, argc, argv); return py_call(tmp, argc, argv);