mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
Make Hypo its own class instead of using tuples
This commit is contained in:
@@ -28,21 +28,8 @@ Type_init(TypeObject *self, PyObject *args, PyObject *kwds)
|
||||
}
|
||||
|
||||
for (Py_ssize_t i = 0; i < PyList_Size(hypos); i++) {
|
||||
PyObject *tup = PyList_GetItem(hypos, i);
|
||||
if (!PyObject_TypeCheck(tup, &PyTuple_Type)) {
|
||||
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type_init: not a tuple");
|
||||
return -1;
|
||||
}
|
||||
if (!PyLong_Check(PyTuple_GetItem(tup, 0))) {
|
||||
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type_init: bind type not an integer");
|
||||
return -1;
|
||||
}
|
||||
if (!PyUnicode_Check(PyTuple_GetItem(tup, 1))) {
|
||||
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type_init: variable not a string");
|
||||
return -1;
|
||||
}
|
||||
if (!PyObject_TypeCheck(PyTuple_GetItem(tup, 2), &pgf_TypeType)) {
|
||||
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type_init: type not a type");
|
||||
if (!PyObject_TypeCheck(PyList_GetItem(hypos, i), &pgf_HypoType)) {
|
||||
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type initialisation");
|
||||
return -1;
|
||||
}
|
||||
// Py_INCREF(&hypos[i]);
|
||||
@@ -173,6 +160,124 @@ PyTypeObject pgf_TypeType = {
|
||||
(newfunc) Type_new, /*tp_new */
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// 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)
|
||||
{
|
||||
int bind_type;
|
||||
PyObject* cid;
|
||||
TypeObject* type;
|
||||
if (!PyArg_ParseTuple(args, "iUO!", &bind_type, &cid, &pgf_TypeType, &type)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bind_type == 0 || bind_type == 1) {
|
||||
self->bind_type = PyLong_FromLong(bind_type);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "invalid bind type in hypo initialisation");
|
||||
return -1;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -8,13 +8,22 @@
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
PyObject *hypos; // PyListObject of PyTupleObject: (bind_type: int, cid: string, type: TypeObject)
|
||||
PyObject *name; // PyUnicodeObject
|
||||
PyObject *hypos; // PyListObject of HypoObject
|
||||
PyObject *name; // PyUnicodeObject
|
||||
PyObject *exprs; // PyListObject of ExprObject
|
||||
} TypeObject;
|
||||
|
||||
PyTypeObject pgf_TypeType;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
PyObject *bind_type; // PyLongObject
|
||||
PyObject *cid; // PyUnicodeObject
|
||||
TypeObject *type;
|
||||
} HypoObject;
|
||||
|
||||
PyTypeObject pgf_HypoType;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
} ExprObject;
|
||||
|
||||
@@ -72,32 +72,15 @@ PyList_AsHypos(PyObject *pylist, Py_ssize_t *n_hypos)
|
||||
PgfTypeHypo *hypos = PyMem_RawMalloc(sizeof(PgfTypeHypo)*n);
|
||||
|
||||
for (Py_ssize_t i = 0; i < n; i++) {
|
||||
PyObject *tup = PyList_GetItem(pylist, i);
|
||||
if (!PyTuple_Check(tup)) {
|
||||
PyErr_SetString(PyExc_TypeError, "hypo must be a tuple");
|
||||
PyObject *item = PyList_GetItem(pylist, i);
|
||||
if (!PyObject_TypeCheck(item, &pgf_HypoType)) {
|
||||
PyErr_SetString(PyExc_TypeError, "hypothesis must be of type Hypo");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *t0 = PyTuple_GetItem(tup, 0);
|
||||
if (!PyLong_Check(t0)) {
|
||||
PyErr_SetString(PyExc_TypeError, "first element of hypo must be an integer");
|
||||
return NULL;
|
||||
}
|
||||
hypos[i].bind_type = PyLong_AsLong(t0);
|
||||
|
||||
PyObject *t1 = PyTuple_GetItem(tup, 1);
|
||||
if (!PyUnicode_Check(t1)) {
|
||||
PyErr_SetString(PyExc_TypeError, "second element of hypo must be a string");
|
||||
return NULL;
|
||||
}
|
||||
hypos[i].cid = PyUnicode_AsPgfText(t1);
|
||||
|
||||
PyObject *t2 = PyTuple_GetItem(tup, 2);
|
||||
if (!PyObject_TypeCheck(t2, &pgf_TypeType)) {
|
||||
PyErr_SetString(PyExc_TypeError, "third element of hypo must be a Type");
|
||||
return NULL;
|
||||
}
|
||||
hypos[i].type = (PgfType) t2;
|
||||
HypoObject *hypo = (HypoObject *)item;
|
||||
hypos[i].bind_type = PyLong_AsLong(hypo->bind_type);
|
||||
hypos[i].cid = PyUnicode_AsPgfText(hypo->cid);
|
||||
hypos[i].type = (PgfType) hypo->type;
|
||||
Py_INCREF(hypos[i].type);
|
||||
}
|
||||
|
||||
@@ -113,12 +96,12 @@ PyList_FromHypos(PgfTypeHypo *hypos, const size_t n_hypos)
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < n_hypos; i++) {
|
||||
PyObject *tup = PyTuple_New(3);
|
||||
PyTuple_SetItem(tup, 0, PyLong_FromLong(hypos[i].bind_type));
|
||||
PyTuple_SetItem(tup, 1, PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size));
|
||||
PyTuple_SetItem(tup, 2, (PyObject *)hypos[i].type);
|
||||
Py_INCREF(hypos[i].type);
|
||||
PyList_SetItem(pylist, i, tup);
|
||||
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
|
||||
hypo->bind_type = PyLong_FromLong(hypos[i].bind_type);
|
||||
hypo->cid = PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size);
|
||||
hypo->type = (TypeObject *)hypos[i].type;
|
||||
// Py_INCREF(hypo->type);
|
||||
PyList_SetItem(pylist, i, (PyObject *)hypo);
|
||||
}
|
||||
if (PyErr_Occurred()) {
|
||||
Py_DECREF(pylist);
|
||||
|
||||
@@ -532,56 +532,56 @@ pgf_showType(PyObject *self, PyObject *args)
|
||||
return str;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
static HypoObject *
|
||||
pgf_mkHypo(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *type;
|
||||
TypeObject *type;
|
||||
if (!PyArg_ParseTuple(args, "O!", &pgf_TypeType, &type))
|
||||
return NULL;
|
||||
|
||||
PyObject *tup = PyTuple_New(3);
|
||||
PyTuple_SetItem(tup, 0, PyLong_FromLong(0)); // explicit
|
||||
PyTuple_SetItem(tup, 1, PyUnicode_FromStringAndSize("_", 1));
|
||||
PyTuple_SetItem(tup, 2, type);
|
||||
Py_INCREF(type);
|
||||
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
|
||||
hypo->bind_type = PyLong_FromLong(0); // explicit
|
||||
hypo->cid = PyUnicode_FromStringAndSize("_", 1);
|
||||
hypo->type = type;
|
||||
Py_INCREF(hypo->type);
|
||||
|
||||
return tup;
|
||||
return hypo;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
static HypoObject *
|
||||
pgf_mkDepHypo(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *var;
|
||||
PyObject *type;
|
||||
TypeObject *type;
|
||||
if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type))
|
||||
return NULL;
|
||||
|
||||
PyObject *tup = PyTuple_New(3);
|
||||
PyTuple_SetItem(tup, 0, PyLong_FromLong(0)); // explicit
|
||||
PyTuple_SetItem(tup, 1, var);
|
||||
PyTuple_SetItem(tup, 2, type);
|
||||
Py_INCREF(var);
|
||||
Py_INCREF(type);
|
||||
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
|
||||
hypo->bind_type = PyLong_FromLong(0); // explicit
|
||||
hypo->cid = var;
|
||||
hypo->type = type;
|
||||
Py_INCREF(hypo->cid);
|
||||
Py_INCREF(hypo->type);
|
||||
|
||||
return tup;
|
||||
return hypo;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
static HypoObject *
|
||||
pgf_mkImplHypo(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *var;
|
||||
PyObject *type;
|
||||
TypeObject *type;
|
||||
if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type))
|
||||
return NULL;
|
||||
|
||||
PyObject *tup = PyTuple_New(3);
|
||||
PyTuple_SetItem(tup, 0, PyLong_FromLong(1)); // implicit
|
||||
PyTuple_SetItem(tup, 1, var);
|
||||
PyTuple_SetItem(tup, 2, type);
|
||||
Py_INCREF(var);
|
||||
Py_INCREF(type);
|
||||
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
|
||||
hypo->bind_type = PyLong_FromLong(1); // implicit
|
||||
hypo->cid = var;
|
||||
hypo->type = type;
|
||||
Py_INCREF(hypo->cid);
|
||||
Py_INCREF(hypo->type);
|
||||
|
||||
return tup;
|
||||
return hypo;
|
||||
}
|
||||
|
||||
static PyMethodDef module_methods[] = {
|
||||
@@ -668,6 +668,9 @@ MOD_INIT(pgf)
|
||||
if (PyType_Ready(&pgf_TypeType) < 0)
|
||||
return MOD_ERROR_VAL;
|
||||
|
||||
if (PyType_Ready(&pgf_HypoType) < 0)
|
||||
return MOD_ERROR_VAL;
|
||||
|
||||
MOD_DEF(m, "pgf", "The Runtime for Portable Grammar Format in Python", module_methods);
|
||||
if (m == NULL)
|
||||
return MOD_ERROR_VAL;
|
||||
@@ -712,6 +715,9 @@ MOD_INIT(pgf)
|
||||
PyModule_AddObject(m, "Type", (PyObject *) &pgf_TypeType);
|
||||
// Py_INCREF(&pgf_TypeType);
|
||||
|
||||
PyModule_AddObject(m, "Hypo", (PyObject *) &pgf_HypoType);
|
||||
// Py_INCREF(&pgf_TypeType);
|
||||
|
||||
PyModule_AddIntConstant(m, "BIND_TYPE_EXPLICIT", 0);
|
||||
|
||||
PyModule_AddIntConstant(m, "BIND_TYPE_IMPLICIT", 1);
|
||||
|
||||
@@ -106,10 +106,11 @@ def test_categoryContext_2(PGF):
|
||||
def test_categoryContext_3(PGF):
|
||||
cxt = PGF.categoryContext("P")
|
||||
assert len(cxt) == 1
|
||||
tup = cxt[0]
|
||||
assert tup[0] == 0 # explicit
|
||||
assert tup[1] == "_" # cid
|
||||
assert tup[2] == readType("N")
|
||||
hypo = cxt[0]
|
||||
assert isinstance(hypo, Hypo)
|
||||
assert hypo.bind_type == BIND_TYPE_EXPLICIT
|
||||
assert hypo.cid == "_"
|
||||
assert hypo.type == readType("N")
|
||||
|
||||
def test_categoryContext_4(PGF):
|
||||
assert PGF.categoryContext("X") == None
|
||||
|
||||
@@ -15,7 +15,7 @@ def gr2(gr1):
|
||||
gr = gr1
|
||||
t = gr.newTransaction()
|
||||
t.createFunction("foo", ty, 0, prob)
|
||||
t.createCategory("Q", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
|
||||
t.createCategory("Q", [Hypo(BIND_TYPE_EXPLICIT, "x", ty)], prob)
|
||||
t.commit()
|
||||
yield gr
|
||||
|
||||
@@ -24,7 +24,7 @@ def gr3(gr1):
|
||||
gr = gr1
|
||||
with gr.newTransaction("bar_branch") as t:
|
||||
t.createFunction("bar", ty, 0, prob)
|
||||
t.createCategory("R", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
|
||||
t.createCategory("R", [Hypo(BIND_TYPE_EXPLICIT, "x", ty)], prob)
|
||||
yield gr
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
@@ -79,7 +79,7 @@ def test_extended_categories(gr2):
|
||||
assert gr2.categories == ["Float","Int","N","P","Q","S","String"]
|
||||
|
||||
def test_extended_category_context(gr2):
|
||||
assert gr2.categoryContext("Q") == [(BIND_TYPE_EXPLICIT, "x", ty)]
|
||||
assert gr2.categoryContext("Q") == [Hypo(BIND_TYPE_EXPLICIT, "x", ty)]
|
||||
|
||||
def test_extended_function_type(gr2):
|
||||
assert gr2.functionType("foo") == ty
|
||||
@@ -98,7 +98,7 @@ def test_branched_categories(gr3):
|
||||
assert gr3.categories == ["Float","Int","N","P","R","S","String"]
|
||||
|
||||
def test_branched_category_context(gr3):
|
||||
assert gr3.categoryContext("R") == [(BIND_TYPE_EXPLICIT, "x", ty)]
|
||||
assert gr3.categoryContext("R") == [Hypo(BIND_TYPE_EXPLICIT, "x", ty)]
|
||||
|
||||
def test_branched_function_type(gr3):
|
||||
assert gr3.functionType("bar") == ty
|
||||
|
||||
Reference in New Issue
Block a user