diff --git a/src/runtime/python/expr.c b/src/runtime/python/expr.c index 1d710374c..3c2af1bf6 100644 --- a/src/runtime/python/expr.c +++ b/src/runtime/python/expr.c @@ -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 diff --git a/src/runtime/python/expr.h b/src/runtime/python/expr.h index 156c8bca1..45f955482 100644 --- a/src/runtime/python/expr.h +++ b/src/runtime/python/expr.h @@ -8,21 +8,21 @@ typedef struct { PyObject_HEAD - PyObject *hypos; // PyListObject of HypoObject + PyObject *hypos; // PyListObject of PyTupleObject: (PyBool, PyUnicodeObject, TypeObject) PyObject *name; // PyUnicodeObject PyObject *exprs; // PyListObject of ExprObject } TypeObject; PyTypeObject pgf_TypeType; -typedef struct { - PyObject_HEAD - PyObject *bind_type; // PyBool - PyObject *cid; // PyUnicodeObject - TypeObject *type; -} HypoObject; - -PyTypeObject pgf_HypoType; +// typedef struct { +// PyObject_HEAD +// PyObject *bind_type; // PyBool +// PyObject *cid; // PyUnicodeObject +// TypeObject *type; +// } HypoObject; +// +// PyTypeObject pgf_HypoType; typedef struct { PyObject_HEAD diff --git a/src/runtime/python/ffi.c b/src/runtime/python/ffi.c index 4cfc91083..beaf7b284 100644 --- a/src/runtime/python/ffi.c +++ b/src/runtime/python/ffi.c @@ -64,7 +64,7 @@ PgfTypeHypo * PyList_AsHypos(PyObject *pylist, Py_ssize_t *n_hypos) { if (!PyList_Check(pylist)) { - PyErr_SetString(PyExc_TypeError, "hypos must be a list"); + PyErr_SetString(PyExc_TypeError, "hypotheses must be a list"); return NULL; } Py_ssize_t n = PyList_Size(pylist); @@ -72,15 +72,43 @@ 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 *item = PyList_GetItem(pylist, i); - if (!PyObject_TypeCheck(item, &pgf_HypoType)) { - PyErr_SetString(PyExc_TypeError, "hypothesis must be of type Hypo"); + // PyObject *item = PyList_GetItem(pylist, i); + // if (!PyObject_TypeCheck(item, &pgf_HypoType)) { + // PyErr_SetString(PyExc_TypeError, "hypothesis must be of type Hypo"); + // return NULL; + // } + // HypoObject *hypo = (HypoObject *)item; + // hypos[i].bind_type = hypo->bind_type == Py_True ? 0 : 1; + // hypos[i].cid = PyUnicode_AsPgfText(hypo->cid); + // hypos[i].type = (PgfType) hypo->type; + + PyObject *tup = PyList_GetItem(pylist, i); + if (!PyTuple_Check(tup)) { + PyErr_SetString(PyExc_TypeError, "hypothesis must be a tuple"); return NULL; } - HypoObject *hypo = (HypoObject *)item; - hypos[i].bind_type = hypo->bind_type == Py_True ? 0 : 1; - hypos[i].cid = PyUnicode_AsPgfText(hypo->cid); - hypos[i].type = (PgfType) hypo->type; + + PyObject *t0 = PyTuple_GetItem(tup, 0); + if (!PyLong_Check(t0)) { + PyErr_SetString(PyExc_TypeError, "hypothesis bind type must be an boolean"); + return NULL; + } + hypos[i].bind_type = t0 == Py_True ? 0 : 1; + + PyObject *t1 = PyTuple_GetItem(tup, 1); + if (!PyUnicode_Check(t1)) { + PyErr_SetString(PyExc_TypeError, "hypothesis variable 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, "hypothesis type must be a Type"); + return NULL; + } + hypos[i].type = (PgfType) t2; + Py_INCREF(hypos[i].type); } @@ -96,13 +124,22 @@ PyList_FromHypos(PgfTypeHypo *hypos, const size_t n_hypos) } for (size_t i = 0; i < n_hypos; i++) { - HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); - hypo->bind_type = hypos[i].bind_type == 0 ? Py_True : Py_False; - hypo->cid = PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size); - hypo->type = (TypeObject *)hypos[i].type; - Py_INCREF(hypo->bind_type); - Py_INCREF(hypo->type); - PyList_SetItem(pylist, i, (PyObject *)hypo); + // HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); + // hypo->bind_type = hypos[i].bind_type == 0 ? Py_True : Py_False; + // hypo->cid = PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size); + // hypo->type = (TypeObject *)hypos[i].type; + // Py_INCREF(hypo->bind_type); + // Py_INCREF(hypo->type); + // PyList_SetItem(pylist, i, (PyObject *)hypo); + + PyObject *tup = PyTuple_New(3); + PyObject *bt = hypos[i].bind_type == 0 ? Py_True : Py_False; + PyTuple_SetItem(tup, 0, bt); + PyTuple_SetItem(tup, 1, PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size)); + PyTuple_SetItem(tup, 2, (PyObject *)hypos[i].type); + Py_INCREF(bt); + Py_INCREF(hypos[i].type); + PyList_SetItem(pylist, i, tup); } if (PyErr_Occurred()) { Py_DECREF(pylist); diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c index 052fcff93..02d9a756a 100644 --- a/src/runtime/python/pypgf.c +++ b/src/runtime/python/pypgf.c @@ -617,60 +617,83 @@ pgf_showType(PyObject *self, PyObject *args) return str; } -static HypoObject * +static PyObject * pgf_mkHypo(PyObject *self, PyObject *args) { - TypeObject *type; + PyObject *type; if (!PyArg_ParseTuple(args, "O!", &pgf_TypeType, &type)) return NULL; - HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); - hypo->bind_type = Py_True; // explicit - hypo->cid = PyUnicode_FromStringAndSize("_", 1); - hypo->type = type; - Py_INCREF(hypo->bind_type); - Py_INCREF(hypo->cid); - Py_INCREF(hypo->type); + // HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); + // hypo->bind_type = Py_True; // explicit + // hypo->cid = PyUnicode_FromStringAndSize("_", 1); + // hypo->type = type; + // Py_INCREF(hypo->bind_type); + // Py_INCREF(hypo->cid); + // Py_INCREF(hypo->type); + // return hypo; - return hypo; + PyObject *tup = PyTuple_New(3); + PyTuple_SetItem(tup, 0, Py_True); // explicit + PyTuple_SetItem(tup, 1, PyUnicode_FromStringAndSize("_", 1)); + PyTuple_SetItem(tup, 2, type); + Py_INCREF(Py_True); + Py_INCREF(type); + return tup; } -static HypoObject * +static PyObject * pgf_mkDepHypo(PyObject *self, PyObject *args) { PyObject *var; - TypeObject *type; + PyObject *type; if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type)) return NULL; - HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); - hypo->bind_type = Py_True; // explicit - hypo->cid = var; - hypo->type = type; - Py_INCREF(hypo->bind_type); - Py_INCREF(hypo->cid); - Py_INCREF(hypo->type); + // HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); + // hypo->bind_type = Py_True; // explicit + // hypo->cid = var; + // hypo->type = type; + // Py_INCREF(hypo->bind_type); + // Py_INCREF(hypo->cid); + // Py_INCREF(hypo->type); + // return hypo; - return hypo; + PyObject *tup = PyTuple_New(3); + PyTuple_SetItem(tup, 0, Py_True); // explicit + PyTuple_SetItem(tup, 1, var); + PyTuple_SetItem(tup, 2, type); + Py_INCREF(Py_True); + Py_INCREF(var); + Py_INCREF(type); + return tup; } -static HypoObject * +static PyObject * pgf_mkImplHypo(PyObject *self, PyObject *args) { PyObject *var; - TypeObject *type; + PyObject *type; if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type)) return NULL; - HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); - hypo->bind_type = Py_False; // implicit - hypo->cid = var; - hypo->type = type; - Py_INCREF(hypo->bind_type); - Py_INCREF(hypo->cid); - Py_INCREF(hypo->type); + // HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); + // hypo->bind_type = Py_False; // implicit + // hypo->cid = var; + // hypo->type = type; + // Py_INCREF(hypo->bind_type); + // Py_INCREF(hypo->cid); + // Py_INCREF(hypo->type); + // return hypo; - return hypo; + PyObject *tup = PyTuple_New(3); + PyTuple_SetItem(tup, 0, Py_False); // implicit + PyTuple_SetItem(tup, 1, var); + PyTuple_SetItem(tup, 2, type); + Py_INCREF(Py_True); + Py_INCREF(var); + Py_INCREF(type); + return tup; } static PyMethodDef module_methods[] = { @@ -753,7 +776,7 @@ MOD_INIT(pgf) TYPE_READY(pgf_ExprTypedType); TYPE_READY(pgf_ExprImplArgType); TYPE_READY(pgf_TypeType); - TYPE_READY(pgf_HypoType); + // TYPE_READY(pgf_HypoType); MOD_DEF(m, "pgf", "The Runtime for Portable Grammar Format in Python", module_methods); if (m == NULL) @@ -774,7 +797,7 @@ MOD_INIT(pgf) ADD_TYPE("ExprTyped", pgf_ExprTypedType); ADD_TYPE("ExprImplArg", pgf_ExprImplArgType); ADD_TYPE("Type", pgf_TypeType); - ADD_TYPE("Hypo", pgf_HypoType); + // ADD_TYPE("Hypo", pgf_HypoType); ADD_TYPE_DIRECT("BIND_TYPE_EXPLICIT", Py_True); ADD_TYPE_DIRECT("BIND_TYPE_IMPLICIT", Py_False); diff --git a/src/runtime/python/tests/test_basic.py b/src/runtime/python/tests/test_basic.py index 75df99a0c..332fb3d84 100644 --- a/src/runtime/python/tests/test_basic.py +++ b/src/runtime/python/tests/test_basic.py @@ -113,10 +113,13 @@ def test_categoryContext_3(PGF): cxt = PGF.categoryContext("P") assert len(cxt) == 1 hypo = cxt[0] - assert isinstance(hypo, Hypo) - assert hypo.bind_type == BIND_TYPE_EXPLICIT - assert hypo.cid == "_" - assert hypo.type == readType("N") + # assert isinstance(hypo, Hypo) + # assert hypo.bind_type == BIND_TYPE_EXPLICIT + # assert hypo.cid == "_" + # assert hypo.type == readType("N") + assert hypo[0] == BIND_TYPE_EXPLICIT + assert hypo[1] == "_" + assert hypo[2] == readType("N") def test_categoryContext_4(PGF): assert PGF.categoryContext("X") == None diff --git a/src/runtime/python/tests/test_transactions.py b/src/runtime/python/tests/test_transactions.py index f2c4a6182..e5c1b8fef 100644 --- a/src/runtime/python/tests/test_transactions.py +++ b/src/runtime/python/tests/test_transactions.py @@ -15,7 +15,7 @@ def gr2(gr1): gr = gr1 t = gr.newTransaction() t.createFunction("foo", ty, 0, prob) - t.createCategory("Q", [Hypo(BIND_TYPE_EXPLICIT, "x", ty)], prob) + t.createCategory("Q", [(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", [Hypo(BIND_TYPE_EXPLICIT, "x", ty)], prob) + t.createCategory("R", [(BIND_TYPE_EXPLICIT, "x", ty)], prob) yield gr @pytest.fixture(scope="function") @@ -89,7 +89,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") == [Hypo(BIND_TYPE_EXPLICIT, "x", ty)] + assert gr2.categoryContext("Q") == [(BIND_TYPE_EXPLICIT, "x", ty)] def test_extended_function_type(gr2): assert gr2.functionType("foo") == ty @@ -112,7 +112,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") == [Hypo(BIND_TYPE_EXPLICIT, "x", ty)] + assert gr3.categoryContext("R") == [(BIND_TYPE_EXPLICIT, "x", ty)] def test_branched_function_type(gr3): assert gr3.functionType("bar") == ty