1
0
forked from GitHub/gf-core

Make hypos tuples again 😥

This commit is contained in:
John J. Camilleri
2021-09-28 10:35:19 +02:00
parent 9863f32d05
commit 388829d63d
6 changed files with 255 additions and 176 deletions

View File

@@ -28,18 +28,34 @@ Type_init(TypeObject *self, PyObject *args, PyObject *kwds)
}
for (Py_ssize_t i = 0; i < PyList_Size(hypos); i++) {
if (!PyObject_TypeCheck(PyList_GetItem(hypos, i), &pgf_HypoType)) {
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type initialisation");
// if (!PyObject_TypeCheck(PyList_GetItem(hypos, i), &pgf_HypoType)) {
// PyErr_SetString(PyExc_TypeError, "invalid hypo in Type initialisation");
// return -1;
// }
PyObject *tup = PyList_GetItem(hypos, i);
if (!PyObject_TypeCheck(tup, &PyTuple_Type)) {
PyErr_SetString(PyExc_TypeError, "hypothesis must be a tuple");
return -1;
}
if (!PyLong_Check(PyTuple_GetItem(tup, 0))) {
PyErr_SetString(PyExc_TypeError, "hypothesis bind type must be a boolean");
return -1;
}
if (!PyUnicode_Check(PyTuple_GetItem(tup, 1))) {
PyErr_SetString(PyExc_TypeError, "hypothesis variable must be a string");
return -1;
}
if (!PyObject_TypeCheck(PyTuple_GetItem(tup, 2), &pgf_TypeType)) {
PyErr_SetString(PyExc_TypeError, "hypothesis type must be a Type");
return -1;
}
// Py_INCREF(&hypos[i]);
}
for (Py_ssize_t i = 0; i < PyList_Size(exprs); i++) {
if (!PyObject_TypeCheck(PyList_GetItem(exprs, i), &pgf_ExprType)) {
PyErr_SetString(PyExc_TypeError, "invalid expression in Type initialisation");
return -1;
}
// Py_INCREF(&exprs[i]);
}
self->hypos = hypos;
self->name = name;
@@ -163,114 +179,114 @@ PyTypeObject pgf_TypeType = {
// ----------------------------------------------------------------------------
// hypos
static HypoObject *
Hypo_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
{
HypoObject *self = (HypoObject *)subtype->tp_alloc(subtype, 0);
return self;
}
static int
Hypo_init(HypoObject *self, PyObject *args, PyObject *kwds)
{
PyObject *bind_type;
PyObject *cid;
TypeObject *type;
if (!PyArg_ParseTuple(args, "O!UO!", &PyBool_Type, &bind_type, &cid, &pgf_TypeType, &type)) {
return -1;
}
self->bind_type = bind_type;
self->cid = cid;
self->type = type;
Py_INCREF(self->bind_type);
Py_INCREF(self->cid);
Py_INCREF(self->type);
return 0;
}
static void
Hypo_dealloc(HypoObject *self)
{
Py_XDECREF(self->bind_type);
Py_XDECREF(self->cid);
Py_XDECREF(self->type);
Py_TYPE(self)->tp_free(self);
}
static PyObject *
Hypo_richcompare(HypoObject *t1, PyObject *p2, int op)
{
bool same = false;
if (!PyObject_TypeCheck(p2, &pgf_HypoType)) goto done;
HypoObject *t2 = (HypoObject *)p2;
if (!PyObject_RichCompareBool(t1->bind_type, t2->bind_type, Py_EQ)) goto done;
if (PyUnicode_Compare(t1->cid, t2->cid) != 0) goto done;
if (!PyObject_RichCompareBool((PyObject *)t1->type, (PyObject *)t2->type, Py_EQ)) goto done;
same = true;
done:
if (op == Py_EQ) {
if (same) Py_RETURN_TRUE; else Py_RETURN_FALSE;
} else if (op == Py_NE) {
if (same) Py_RETURN_FALSE; else Py_RETURN_TRUE;
} else {
PyErr_SetString(PyExc_TypeError, "comparison operation not supported");
Py_RETURN_NOTIMPLEMENTED;
}
}
static PyMemberDef Hypo_members[] = {
{"bind_type", T_OBJECT_EX, offsetof(HypoObject, bind_type), READONLY, "bind type (explicit or implicit)"},
{"cid", T_OBJECT_EX, offsetof(HypoObject, cid), READONLY, "category name"},
{"type", T_OBJECT_EX, offsetof(HypoObject, type), READONLY, "type"},
{NULL} /* Sentinel */
};
PyTypeObject pgf_HypoType = {
PyVarObject_HEAD_INIT(NULL, 0)
//0, /*ob_size*/
"pgf.Hypo", /*tp_name*/
sizeof(HypoObject), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor) Hypo_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"hypothesis in a type", /*tp_doc*/
0, /*tp_traverse */
0, /*tp_clear */
(richcmpfunc) Hypo_richcompare, /*tp_richcompare */
0, /*tp_weaklistoffset */
0, /*tp_iter */
0, /*tp_iternext */
0, /*tp_methods */
Hypo_members, /*tp_members */
0, /*tp_getset */
0, /*tp_base */
0, /*tp_dict */
0, /*tp_descr_get */
0, /*tp_descr_set */
0, /*tp_dictoffset */
(initproc) Hypo_init, /*tp_init */
0, /*tp_alloc */
(newfunc) Hypo_new, /*tp_new */
};
// static HypoObject *
// Hypo_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
// {
// HypoObject *self = (HypoObject *)subtype->tp_alloc(subtype, 0);
// return self;
// }
//
// static int
// Hypo_init(HypoObject *self, PyObject *args, PyObject *kwds)
// {
// PyObject *bind_type;
// PyObject *cid;
// TypeObject *type;
// if (!PyArg_ParseTuple(args, "O!UO!", &PyBool_Type, &bind_type, &cid, &pgf_TypeType, &type)) {
// return -1;
// }
//
// self->bind_type = bind_type;
// self->cid = cid;
// self->type = type;
// Py_INCREF(self->bind_type);
// Py_INCREF(self->cid);
// Py_INCREF(self->type);
//
// return 0;
// }
//
// static void
// Hypo_dealloc(HypoObject *self)
// {
// Py_XDECREF(self->bind_type);
// Py_XDECREF(self->cid);
// Py_XDECREF(self->type);
// Py_TYPE(self)->tp_free(self);
// }
//
// static PyObject *
// Hypo_richcompare(HypoObject *t1, PyObject *p2, int op)
// {
// bool same = false;
// if (!PyObject_TypeCheck(p2, &pgf_HypoType)) goto done;
// HypoObject *t2 = (HypoObject *)p2;
//
// if (!PyObject_RichCompareBool(t1->bind_type, t2->bind_type, Py_EQ)) goto done;
// if (PyUnicode_Compare(t1->cid, t2->cid) != 0) goto done;
// if (!PyObject_RichCompareBool((PyObject *)t1->type, (PyObject *)t2->type, Py_EQ)) goto done;
//
// same = true;
// done:
//
// if (op == Py_EQ) {
// if (same) Py_RETURN_TRUE; else Py_RETURN_FALSE;
// } else if (op == Py_NE) {
// if (same) Py_RETURN_FALSE; else Py_RETURN_TRUE;
// } else {
// PyErr_SetString(PyExc_TypeError, "comparison operation not supported");
// Py_RETURN_NOTIMPLEMENTED;
// }
// }
//
// static PyMemberDef Hypo_members[] = {
// {"bind_type", T_OBJECT_EX, offsetof(HypoObject, bind_type), READONLY, "bind type (explicit or implicit)"},
// {"cid", T_OBJECT_EX, offsetof(HypoObject, cid), READONLY, "category name"},
// {"type", T_OBJECT_EX, offsetof(HypoObject, type), READONLY, "type"},
// {NULL} /* Sentinel */
// };
//
// PyTypeObject pgf_HypoType = {
// PyVarObject_HEAD_INIT(NULL, 0)
// //0, /*ob_size*/
// "pgf.Hypo", /*tp_name*/
// sizeof(HypoObject), /*tp_basicsize*/
// 0, /*tp_itemsize*/
// (destructor) Hypo_dealloc, /*tp_dealloc*/
// 0, /*tp_print*/
// 0, /*tp_getattr*/
// 0, /*tp_setattr*/
// 0, /*tp_compare*/
// 0, /*tp_repr*/
// 0, /*tp_as_number*/
// 0, /*tp_as_sequence*/
// 0, /*tp_as_mapping*/
// 0, /*tp_hash */
// 0, /*tp_call*/
// 0, /*tp_str*/
// 0, /*tp_getattro*/
// 0, /*tp_setattro*/
// 0, /*tp_as_buffer*/
// Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
// "hypothesis in a type", /*tp_doc*/
// 0, /*tp_traverse */
// 0, /*tp_clear */
// (richcmpfunc) Hypo_richcompare, /*tp_richcompare */
// 0, /*tp_weaklistoffset */
// 0, /*tp_iter */
// 0, /*tp_iternext */
// 0, /*tp_methods */
// Hypo_members, /*tp_members */
// 0, /*tp_getset */
// 0, /*tp_base */
// 0, /*tp_dict */
// 0, /*tp_descr_get */
// 0, /*tp_descr_set */
// 0, /*tp_dictoffset */
// (initproc) Hypo_init, /*tp_init */
// 0, /*tp_alloc */
// (newfunc) Hypo_new, /*tp_new */
// };
// ----------------------------------------------------------------------------
// expressions