diff --git a/src/runtime/python/expr.c b/src/runtime/python/expr.c index e617cbc48..2d5b67f43 100644 --- a/src/runtime/python/expr.c +++ b/src/runtime/python/expr.c @@ -1,6 +1,7 @@ #define PY_SSIZE_T_CLEAN #include #include +#include #include #include "./expr.h" @@ -20,9 +21,9 @@ static int Type_init(TypeObject *self, PyObject *args, PyObject *kwds) { PyObject* hypos; - PyObject* cat; + PyObject* name; PyObject* exprs; - if (!PyArg_ParseTuple(args, "O!UO!", &PyList_Type, &hypos, &cat, &PyList_Type, &exprs)) { + if (!PyArg_ParseTuple(args, "O!UO!", &PyList_Type, &hypos, &name, &PyList_Type, &exprs)) { return -1; } @@ -54,10 +55,10 @@ Type_init(TypeObject *self, PyObject *args, PyObject *kwds) // Py_INCREF(&exprs[i]); } self->hypos = hypos; - self->cat = cat; + self->name = name; self->exprs = exprs; Py_INCREF(hypos); - // Py_INCREF(cat); + // Py_INCREF(name); Py_INCREF(exprs); return 0; @@ -79,7 +80,7 @@ Type_richcompare(TypeObject *t1, PyObject *p2, int op) if (!PyObject_TypeCheck(p2, &pgf_TypeType)) goto done; TypeObject *t2 = (TypeObject *)p2; - if (PyUnicode_Compare(t1->cat, t2->cat) != 0) goto done; + if (PyUnicode_Compare(t1->name, t2->name) != 0) goto done; if (PyList_Size(t1->hypos) != PyList_Size(t2->hypos)) goto done; for (Py_ssize_t n = 0; n < PyList_Size(t1->hypos); n++) { @@ -114,36 +115,12 @@ static PyMethodDef Type_methods[] = { {NULL} /* Sentinel */ }; -static PyObject * -Type_getattro(TypeObject *self, PyObject *attr) -{ - if (PyUnicode_CompareWithASCIIString(attr, "hypos") == 0) { - return self->hypos; - } else if (PyUnicode_CompareWithASCIIString(attr, "cat") == 0) { - return self->cat; - } else if (PyUnicode_CompareWithASCIIString(attr, "exprs") == 0) { - return self->exprs; - } else { - PyErr_Format(PyExc_AttributeError, "'pgf.Type' object has no attribute '%U'", attr); - return NULL; - } -} - -// static PyGetSetDef Type_getseters[] = { -// {"hypos", -// (getter)Type_getHypos, NULL, -// "list of hypotheses in the type signature", -// NULL}, -// {"cat", -// (getter)Type_getCat, NULL, -// "name of the category", -// NULL}, -// {"exprs", -// (getter)Type_getExprs, NULL, -// "list of indices for the category", -// NULL}, -// {NULL} /* Sentinel */ -// }; +static PyMemberDef Type_members[] = { + {"hypos", T_OBJECT_EX, offsetof(TypeObject, hypos), 0, "list of hypotheses in the type signature"}, + {"cat", T_OBJECT_EX, offsetof(TypeObject, name), 0, "name of the category"}, + {"exprs", T_OBJECT_EX, offsetof(TypeObject, exprs), 0, "list of indices for the category"}, + {NULL} /* Sentinel */ +}; PyTypeObject pgf_TypeType = { PyVarObject_HEAD_INIT(NULL, 0) @@ -163,7 +140,7 @@ PyTypeObject pgf_TypeType = { 0, /*tp_hash */ 0, /*tp_call*/ (reprfunc) Type_str, /*tp_str*/ - (getattrofunc) Type_getattro, /*tp_getattro*/ + 0, //(getattrofunc) Type_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ @@ -175,7 +152,7 @@ PyTypeObject pgf_TypeType = { 0, /*tp_iter */ 0, /*tp_iternext */ Type_methods, /*tp_methods */ - 0, /*tp_members */ + Type_members, /*tp_members */ 0, //Type_getseters, /*tp_getset */ 0, /*tp_base */ 0, /*tp_dict */ @@ -204,30 +181,6 @@ static PyMethodDef Expr_methods[] = { }; static PyGetSetDef Expr_getseters[] = { -// {"fun", -// NULL, NULL, -// "this is the function in a function application", -// NULL}, -// {"arg", -// NULL, NULL, -// "this is the argument in a function application", -// NULL}, -// {"val", -// NULL, NULL, -// "this is the value of a literal", -// NULL}, -// {"id", -// NULL, NULL, -// "this is the id of a meta variable", -// NULL}, -// {"name", -// NULL, NULL, -// "this is the name of a function", -// NULL}, -// {"index", -// NULL, NULL, -// "this is the de Bruijn index of a variable", -// NULL}, {NULL} /* Sentinel */ }; @@ -285,18 +238,18 @@ ExprAbs_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) static int ExprAbs_init(ExprAbsObject *self, PyObject *args, PyObject *kwds) { - PyObject* bindType = NULL; - PyObject* var = NULL; - ExprObject* expr = NULL; - if (!PyArg_ParseTuple(args, "O!UO!", &PyLong_Type, &bindType, &var, &pgf_ExprType, &expr)) { + PyObject* bind_type = NULL; + PyObject* name = NULL; + ExprObject* body = NULL; + if (!PyArg_ParseTuple(args, "O!UO!", &PyLong_Type, &bind_type, &name, &pgf_ExprType, &body)) { return -1; } - self->bindType = bindType; - self->var = var; - self->expr = expr; - Py_INCREF(bindType); - Py_INCREF(var); - Py_INCREF(expr); + self->bind_type = bind_type; + self->name = name; + self->body = body; + Py_INCREF(bind_type); + Py_INCREF(name); + Py_INCREF(body); return 0; } @@ -306,9 +259,9 @@ ExprAbs_richcompare(ExprAbsObject *e1, PyObject *p2, int op) bool same = false; if (!PyObject_TypeCheck(p2, &pgf_ExprAbsType)) goto done; ExprAbsObject *e2 = (ExprAbsObject *)p2; - if (!PyObject_RichCompareBool(e1->bindType, e2->bindType, Py_EQ)) goto done; - if (PyUnicode_Compare(e1->var, e2->var) != 0) goto done; - if (!PyObject_RichCompareBool((PyObject*)e1->expr, (PyObject*)e2->expr, Py_EQ)) goto done; + if (!PyObject_RichCompareBool(e1->bind_type, e2->bind_type, Py_EQ)) goto done; + if (PyUnicode_Compare(e1->name, e2->name) != 0) goto done; + if (!PyObject_RichCompareBool((PyObject*)e1->body, (PyObject*)e2->body, Py_EQ)) goto done; same = true; done: @@ -323,20 +276,12 @@ done: } } -static PyObject * -ExprAbs_getattro(ExprAbsObject *self, PyObject *attr) -{ - if (PyUnicode_CompareWithASCIIString(attr, "bindType") == 0) { - return self->bindType; - } else if (PyUnicode_CompareWithASCIIString(attr, "var") == 0) { - return self->var; - } else if (PyUnicode_CompareWithASCIIString(attr, "expr") == 0) { - return (PyObject *)self->expr; - } else { - PyErr_Format(PyExc_AttributeError, "'pgf.ExprAbs' object has no attribute '%U'", attr); - return NULL; - } -} +static PyMemberDef ExprAbs_members[] = { + {"bind_type", T_OBJECT_EX, offsetof(ExprAbsObject, bind_type), 0, "bind type (explicit or implicit)"}, + {"name", T_OBJECT_EX, offsetof(ExprAbsObject, name), 0, "name of the abstraction"}, + {"body", T_OBJECT_EX, offsetof(ExprAbsObject, body), 0, "body of the abstraction"}, + {NULL} /* Sentinel */ +}; PyTypeObject pgf_ExprAbsType = { PyVarObject_HEAD_INIT(NULL, 0) @@ -356,7 +301,7 @@ PyTypeObject pgf_ExprAbsType = { 0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(reprfunc) Expr_str, /*tp_str*/ - (getattrofunc) ExprAbs_getattro, /*tp_getattro*/ + 0, //(getattrofunc) ExprAbs_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ @@ -368,7 +313,7 @@ PyTypeObject pgf_ExprAbsType = { 0, /*tp_iter */ 0, /*tp_iternext */ 0, //Expr_methods, /*tp_methods */ - 0, /*tp_members */ + ExprAbs_members, /*tp_members */ 0, //Expr_getseters, /*tp_getset */ &pgf_ExprType, /*tp_base */ 0, /*tp_dict */ @@ -392,15 +337,15 @@ ExprApp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) static int ExprApp_init(ExprAppObject *self, PyObject *args, PyObject *kwds) { - PyObject* e1 = NULL; - PyObject* e2 = NULL; - if (!PyArg_ParseTuple(args, "O!O!", &pgf_ExprType, &e1, &pgf_ExprType, &e2)) { + PyObject* fun = NULL; + PyObject* arg = NULL; + if (!PyArg_ParseTuple(args, "O!O!", &pgf_ExprType, &fun, &pgf_ExprType, &arg)) { return -1; } - self->e1 = (ExprObject *)e1; - self->e2 = (ExprObject *)e2; - Py_INCREF(e1); - Py_INCREF(e2); + self->fun = (ExprObject *)fun; + self->arg = (ExprObject *)arg; + Py_INCREF(fun); + Py_INCREF(arg); return 0; } @@ -410,8 +355,8 @@ ExprApp_richcompare(ExprAppObject *e1, PyObject *p2, int op) bool same = false; if (!PyObject_TypeCheck(p2, &pgf_ExprAppType)) goto done; ExprAppObject *e2 = (ExprAppObject *)p2; - if (!PyObject_RichCompareBool((PyObject*)e1->e1, (PyObject*)e2->e1, Py_EQ)) goto done; - if (!PyObject_RichCompareBool((PyObject*)e1->e2, (PyObject*)e2->e2, Py_EQ)) goto done; + if (!PyObject_RichCompareBool((PyObject*)e1->fun, (PyObject*)e2->fun, Py_EQ)) goto done; + if (!PyObject_RichCompareBool((PyObject*)e1->arg, (PyObject*)e2->arg, Py_EQ)) goto done; same = true; done: @@ -426,18 +371,11 @@ done: } } -static PyObject * -ExprApp_getattro(ExprAppObject *self, PyObject *attr) -{ - if (PyUnicode_CompareWithASCIIString(attr, "e1") == 0) { - return (PyObject *)self->e1; - } else if (PyUnicode_CompareWithASCIIString(attr, "e2") == 0) { - return (PyObject *)self->e2; - } else { - PyErr_Format(PyExc_AttributeError, "'pgf.ExprApp' object has no attribute '%U'", attr); - return NULL; - } -} +static PyMemberDef ExprApp_members[] = { + {"fun", T_OBJECT_EX, offsetof(ExprAppObject, fun), 0, "the function in a function application"}, + {"arg", T_OBJECT_EX, offsetof(ExprAppObject, arg), 0, "the argument in a function application"}, + {NULL} /* Sentinel */ +}; PyTypeObject pgf_ExprAppType = { PyVarObject_HEAD_INIT(NULL, 0) @@ -457,7 +395,7 @@ PyTypeObject pgf_ExprAppType = { 0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(reprfunc) Expr_str, /*tp_str*/ - (getattrofunc) ExprApp_getattro, /*tp_getattro*/ + 0, //(getattrofunc) ExprApp_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ @@ -469,7 +407,7 @@ PyTypeObject pgf_ExprAppType = { 0, /*tp_iter */ 0, /*tp_iternext */ 0, //Expr_methods, /*tp_methods */ - 0, /*tp_members */ + ExprApp_members, /*tp_members */ 0, //Expr_getseters, /*tp_getset */ &pgf_ExprType, /*tp_base */ 0, /*tp_dict */ @@ -498,7 +436,7 @@ ExprLit_init(ExprLitObject *self, PyObject *args, PyObject *kwds) return -1; } if (PyLong_Check(lit) || PyFloat_Check(lit) || PyUnicode_Check(lit)) { - self->value = lit; + self->lit = lit; Py_INCREF(lit); return 0; } else { @@ -514,18 +452,18 @@ ExprLit_richcompare(ExprLitObject *e1, PyObject *p2, int op) if (!PyObject_TypeCheck(p2, &pgf_ExprLitType)) goto done; ExprLitObject *e2 = (ExprLitObject *)p2; - if (PyLong_Check(e1->value)) { - if (!PyLong_Check(e2->value)) goto done; + if (PyLong_Check(e1->lit)) { + if (!PyLong_Check(e2->lit)) goto done; int o1, o2; - int l1 = PyLong_AsLongAndOverflow(e1->value, &o1); - int l2 = PyLong_AsLongAndOverflow(e2->value, &o2); + int l1 = PyLong_AsLongAndOverflow(e1->lit, &o1); + int l2 = PyLong_AsLongAndOverflow(e2->lit, &o2); if (!(l1 == l2 && o1 == o2)) goto done; - } else if (PyFloat_Check(e1->value)) { - if (!PyFloat_Check(e2->value)) goto done; - if (PyFloat_AsDouble(e1->value) != PyFloat_AsDouble(e2->value)) goto done; - } else if (PyUnicode_Check(e1->value)) { - if (!PyUnicode_Check(e2->value)) goto done; - if (PyUnicode_Compare(e1->value, e2->value) != 0) goto done; + } else if (PyFloat_Check(e1->lit)) { + if (!PyFloat_Check(e2->lit)) goto done; + if (PyFloat_AsDouble(e1->lit) != PyFloat_AsDouble(e2->lit)) goto done; + } else if (PyUnicode_Check(e1->lit)) { + if (!PyUnicode_Check(e2->lit)) goto done; + if (PyUnicode_Compare(e1->lit, e2->lit) != 0) goto done; } else { PyErr_SetString(PyExc_TypeError, "unknown literal type"); return NULL; @@ -544,16 +482,10 @@ done: } } -static PyObject * -ExprLit_getattro(ExprLitObject *self, PyObject *attr) -{ - if (PyUnicode_CompareWithASCIIString(attr, "value") == 0) { - return self->value; - } else { - PyErr_Format(PyExc_AttributeError, "'pgf.ExprLit' object has no attribute '%U'", attr); - return NULL; - } -} +static PyMemberDef ExprLit_members[] = { + {"val", T_OBJECT_EX, offsetof(ExprLitObject, lit), 0, "the value of the literal"}, + {NULL} /* Sentinel */ +}; PyTypeObject pgf_ExprLitType = { PyVarObject_HEAD_INIT(NULL, 0) @@ -573,7 +505,7 @@ PyTypeObject pgf_ExprLitType = { 0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(reprfunc) Expr_str, /*tp_str*/ - (getattrofunc) ExprLit_getattro, /*tp_getattro*/ + 0, //(getattrofunc) ExprLit_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ @@ -585,7 +517,7 @@ PyTypeObject pgf_ExprLitType = { 0, /*tp_iter */ 0, /*tp_iternext */ 0, /*tp_methods */ - 0, /*tp_members */ + ExprLit_members, /*tp_members */ 0, /*tp_getset */ &pgf_ExprType, /*tp_base */ 0, /*tp_dict */ @@ -647,16 +579,10 @@ done: } } -static PyObject * -ExprMeta_getattro(ExprMetaObject *self, PyObject *attr) -{ - if (PyUnicode_CompareWithASCIIString(attr, "id") == 0) { - return (PyObject *)self->id; - } else { - PyErr_Format(PyExc_AttributeError, "'pgf.ExprMeta' object has no attribute '%U'", attr); - return NULL; - } -} +static PyMemberDef ExprMeta_members[] = { + {"id", T_OBJECT_EX, offsetof(ExprMetaObject, id), 0, "the id of a meta variable"}, + {NULL} /* Sentinel */ +}; PyTypeObject pgf_ExprMetaType = { PyVarObject_HEAD_INIT(NULL, 0) @@ -676,7 +602,7 @@ PyTypeObject pgf_ExprMetaType = { 0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(reprfunc) Expr_str, /*tp_str*/ - (getattrofunc) ExprMeta_getattro, /*tp_getattro*/ + 0, //(getattrofunc) ExprMeta_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ @@ -688,7 +614,7 @@ PyTypeObject pgf_ExprMetaType = { 0, /*tp_iter */ 0, /*tp_iternext */ 0, //Expr_methods, /*tp_methods */ - 0, /*tp_members */ + ExprMeta_members, /*tp_members */ 0, //Expr_getseters, /*tp_getset */ &pgf_ExprType, /*tp_base */ 0, /*tp_dict */ @@ -742,16 +668,10 @@ done: } } -static PyObject * -ExprFun_getattro(ExprFunObject *self, PyObject *attr) -{ - if (PyUnicode_CompareWithASCIIString(attr, "name") == 0) { - return (PyObject *)self->name; - } else { - PyErr_Format(PyExc_AttributeError, "'pgf.ExprFun' object has no attribute '%U'", attr); - return NULL; - } -} +static PyMemberDef ExprFun_members[] = { + {"name", T_OBJECT_EX, offsetof(ExprFunObject, name), 0, "the name of the function"}, + {NULL} /* Sentinel */ +}; PyTypeObject pgf_ExprFunType = { PyVarObject_HEAD_INIT(NULL, 0) @@ -771,7 +691,7 @@ PyTypeObject pgf_ExprFunType = { 0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(reprfunc) Expr_str, /*tp_str*/ - (getattrofunc) ExprFun_getattro, /*tp_getattro*/ + 0, //(getattrofunc) ExprFun_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ @@ -783,7 +703,7 @@ PyTypeObject pgf_ExprFunType = { 0, /*tp_iter */ 0, /*tp_iternext */ 0, //Expr_methods, /*tp_methods */ - 0, /*tp_members */ + ExprFun_members, /*tp_members */ 0, //Expr_getseters, /*tp_getset */ &pgf_ExprType, /*tp_base */ 0, /*tp_dict */ @@ -812,10 +732,10 @@ ExprVar_init(ExprVarObject *self, PyObject *args, PyObject *kwds) return -1; } if (lit == NULL) { - self->index = PyLong_FromLong(0); + self->var = PyLong_FromLong(0); return 0; } else if (PyLong_Check(lit)) { - self->index = lit; + self->var = lit; Py_INCREF(lit); return 0; } else { @@ -830,7 +750,7 @@ ExprVar_richcompare(ExprVarObject *e1, PyObject *p2, int op) bool same = false; if (!PyObject_TypeCheck(p2, &pgf_ExprVarType)) goto done; ExprVarObject *e2 = (ExprVarObject *)p2; - if (!PyObject_RichCompareBool(e1->index, e2->index, Py_EQ)) goto done; + if (!PyObject_RichCompareBool(e1->var, e2->var, Py_EQ)) goto done; same = true; done: @@ -845,16 +765,10 @@ done: } } -static PyObject * -ExprVar_getattro(ExprVarObject *self, PyObject *attr) -{ - if (PyUnicode_CompareWithASCIIString(attr, "index") == 0) { - return (PyObject *)self->index; - } else { - PyErr_Format(PyExc_AttributeError, "'pgf.ExprVar' object has no attribute '%U'", attr); - return NULL; - } -} +static PyMemberDef ExprVar_members[] = { + {"index", T_OBJECT_EX, offsetof(ExprVarObject, var), 0, "the de Bruijn index of a variable"}, + {NULL} /* Sentinel */ +}; PyTypeObject pgf_ExprVarType = { PyVarObject_HEAD_INIT(NULL, 0) @@ -874,7 +788,7 @@ PyTypeObject pgf_ExprVarType = { 0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(reprfunc) Expr_str, /*tp_str*/ - (getattrofunc) ExprVar_getattro, /*tp_getattro*/ + 0, //(getattrofunc) ExprVar_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ @@ -886,7 +800,7 @@ PyTypeObject pgf_ExprVarType = { 0, /*tp_iter */ 0, /*tp_iternext */ 0, //Expr_methods, /*tp_methods */ - 0, /*tp_members */ + ExprVar_members, /*tp_members */ 0, //Expr_getseters, /*tp_getset */ &pgf_ExprType, /*tp_base */ 0, /*tp_dict */ @@ -944,18 +858,11 @@ done: } } -static PyObject * -ExprTyped_getattro(ExprTypedObject *self, PyObject *attr) -{ - if (PyUnicode_CompareWithASCIIString(attr, "expr") == 0) { - return (PyObject *)self->expr; - } else if (PyUnicode_CompareWithASCIIString(attr, "type") == 0) { - return (PyObject *)self->type; - } else { - PyErr_Format(PyExc_AttributeError, "'pgf.ExprTyped' object has no attribute '%U'", attr); - return NULL; - } -} +static PyMemberDef ExprTyped_members[] = { + {"expr", T_OBJECT_EX, offsetof(ExprTypedObject, expr), 0, "the expression"}, + {"type", T_OBJECT_EX, offsetof(ExprTypedObject, type), 0, "the type"}, + {NULL} /* Sentinel */ +}; PyTypeObject pgf_ExprTypedType = { PyVarObject_HEAD_INIT(NULL, 0) @@ -975,7 +882,7 @@ PyTypeObject pgf_ExprTypedType = { 0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(reprfunc) Expr_str, /*tp_str*/ - (getattrofunc) ExprTyped_getattro, /*tp_getattro*/ + 0, //(getattrofunc) ExprTyped_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ @@ -987,7 +894,7 @@ PyTypeObject pgf_ExprTypedType = { 0, /*tp_iter */ 0, /*tp_iternext */ 0, //Expr_methods, /*tp_methods */ - 0, /*tp_members */ + ExprTyped_members, /*tp_members */ 0, //Expr_getseters, /*tp_getset */ &pgf_ExprType, /*tp_base */ 0, /*tp_dict */ @@ -1041,16 +948,10 @@ done: } } -static PyObject * -ExprImplArg_getattro(ExprImplArgObject *self, PyObject *attr) -{ - if (PyUnicode_CompareWithASCIIString(attr, "expr") == 0) { - return (PyObject *)self->expr; - } else { - PyErr_Format(PyExc_AttributeError, "'pgf.ExprImplArg' object has no attribute '%U'", attr); - return NULL; - } -} +static PyMemberDef ExprImplArg_members[] = { + {"expr", T_OBJECT_EX, offsetof(ExprImplArgObject, expr), 0, "the inner expression"}, + {NULL} /* Sentinel */ +}; PyTypeObject pgf_ExprImplArgType = { PyVarObject_HEAD_INIT(NULL, 0) @@ -1070,7 +971,7 @@ PyTypeObject pgf_ExprImplArgType = { 0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(reprfunc) Expr_str, /*tp_str*/ - (getattrofunc) ExprImplArg_getattro, /*tp_getattro*/ + 0, //(getattrofunc) ExprImplArg_getattro, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ @@ -1082,7 +983,7 @@ PyTypeObject pgf_ExprImplArgType = { 0, /*tp_iter */ 0, /*tp_iternext */ 0, //Expr_methods, /*tp_methods */ - 0, /*tp_members */ + ExprImplArg_members, /*tp_members */ 0, //Expr_getseters, /*tp_getset */ &pgf_ExprType, /*tp_base */ 0, /*tp_dict */ diff --git a/src/runtime/python/expr.h b/src/runtime/python/expr.h index 5b623519f..1174f916f 100644 --- a/src/runtime/python/expr.h +++ b/src/runtime/python/expr.h @@ -9,7 +9,7 @@ typedef struct { PyObject_HEAD PyObject *hypos; // PyListObject of PyTupleObject: (bind_type: int, cid: string, type: TypeObject) - PyObject *cat; // PyUnicodeObject + PyObject *name; // PyUnicodeObject PyObject *exprs; // PyListObject of ExprObject } TypeObject; @@ -21,20 +21,20 @@ typedef struct { typedef struct { PyObject_HEAD - PyObject *bindType; // PyLongObject - PyObject *var; // PyUnicodeObject - ExprObject *expr; + PyObject *bind_type; // PyLongObject + PyObject *name; // PyUnicodeObject + ExprObject *body; } ExprAbsObject; typedef struct { PyObject_HEAD - ExprObject *e1; // ExprObject - ExprObject *e2; // ExprObject + ExprObject *fun; // ExprObject + ExprObject *arg; // ExprObject } ExprAppObject; typedef struct { PyObject_HEAD - PyObject *value; // PyLongObject | PyFloatObject | PyUnicodeObject + PyObject *lit; // PyLongObject | PyFloatObject | PyUnicodeObject } ExprLitObject; typedef struct { @@ -49,7 +49,7 @@ typedef struct { typedef struct { PyObject_HEAD - PyObject *index; // PyLongObject + PyObject *var; // PyLongObject } ExprVarObject; typedef struct { diff --git a/src/runtime/python/ffi.c b/src/runtime/python/ffi.c index fbe91746c..68d35ba0c 100644 --- a/src/runtime/python/ffi.c +++ b/src/runtime/python/ffi.c @@ -174,9 +174,9 @@ static PgfExpr eabs(PgfUnmarshaller *this, PgfBindType btype, PgfText *name, PgfExpr body) { ExprAbsObject *pyexpr = (ExprAbsObject *)pgf_ExprAbsType.tp_alloc(&pgf_ExprAbsType, 0); - pyexpr->bindType = PyLong_FromLong(btype); - pyexpr->var = PyUnicode_FromPgfText(name); - pyexpr->expr = (ExprObject *)body; + pyexpr->bind_type = PyLong_FromLong(btype); + pyexpr->name = PyUnicode_FromPgfText(name); + pyexpr->body = (ExprObject *)body; // Py_INCREF(body); return (PgfExpr) pyexpr; } @@ -185,8 +185,8 @@ static PgfExpr eapp(PgfUnmarshaller *this, PgfExpr fun, PgfExpr arg) { ExprAppObject *pyexpr = (ExprAppObject *)pgf_ExprAppType.tp_alloc(&pgf_ExprAppType, 0); - pyexpr->e1 = (ExprObject *)fun; - pyexpr->e2 = (ExprObject *)arg; + pyexpr->fun = (ExprObject *)fun; + pyexpr->arg = (ExprObject *)arg; // Py_INCREF(fun); // Py_INCREF(arg); return (PgfExpr) pyexpr; @@ -197,7 +197,7 @@ elit(PgfUnmarshaller *this, PgfLiteral lit) { ExprLitObject *pyexpr = (ExprLitObject *)pgf_ExprLitType.tp_alloc(&pgf_ExprLitType, 0); PyObject *pyobj = (PyObject *)lit; - pyexpr->value = pyobj; + pyexpr->lit = pyobj; Py_INCREF(pyobj); return (PgfExpr) pyexpr; } @@ -224,7 +224,7 @@ static PgfExpr evar(PgfUnmarshaller *this, int index) { ExprVarObject *pyexpr = (ExprVarObject *)pgf_ExprVarType.tp_alloc(&pgf_ExprVarType, 0); - pyexpr->index = PyLong_FromLong(index); + pyexpr->var = PyLong_FromLong(index); return (PgfExpr) pyexpr; } @@ -293,7 +293,7 @@ dtyp(PgfUnmarshaller *this, int n_hypos, PgfTypeHypo *hypos, PgfText *cat, int n TypeObject *pytype = (TypeObject *)pgf_TypeType.tp_alloc(&pgf_TypeType, 0); pytype->hypos = PyList_FromHypos(hypos, n_hypos); - pytype->cat = PyUnicode_FromStringAndSize(cat->text, cat->size); + pytype->name = PyUnicode_FromStringAndSize(cat->text, cat->size); pytype->exprs = PyList_New(n_exprs); for (int i = 0; i < n_exprs; i++) { PyList_SetItem(pytype->exprs, i, (PyObject *)exprs[i]); @@ -384,15 +384,15 @@ match_expr(PgfMarshaller *this, PgfUnmarshaller *u, PgfExpr expr) if (PyObject_TypeCheck(pyobj, &pgf_ExprAbsType)) { ExprAbsObject *eabs = (ExprAbsObject *)expr; - return u->vtbl->eabs(u, PyLong_AsLong(eabs->bindType), PyUnicode_AsPgfText(eabs->var), (PgfExpr) eabs->expr); + return u->vtbl->eabs(u, PyLong_AsLong(eabs->bind_type), PyUnicode_AsPgfText(eabs->name), (PgfExpr) eabs->body); } else if (PyObject_TypeCheck(pyobj, &pgf_ExprAppType)) { ExprAppObject *eapp = (ExprAppObject *)expr; - return u->vtbl->eapp(u, (PgfExpr) eapp->e1, (PgfExpr) eapp->e2); + return u->vtbl->eapp(u, (PgfExpr) eapp->fun, (PgfExpr) eapp->arg); } else if (PyObject_TypeCheck(pyobj, &pgf_ExprLitType)) { ExprLitObject *elit = (ExprLitObject *)expr; - return this->vtbl->match_lit(this, u, (PgfLiteral) elit->value); + return this->vtbl->match_lit(this, u, (PgfLiteral) elit->lit); } else if (PyObject_TypeCheck(pyobj, &pgf_ExprMetaType)) { ExprMetaObject *emeta = (ExprMetaObject *)expr; @@ -404,7 +404,7 @@ match_expr(PgfMarshaller *this, PgfUnmarshaller *u, PgfExpr expr) } else if (PyObject_TypeCheck(pyobj, &pgf_ExprVarType)) { ExprVarObject *evar = (ExprVarObject *)expr; - return u->vtbl->evar(u, PyLong_AsLong(evar->index)); + return u->vtbl->evar(u, PyLong_AsLong(evar->var)); } else if (PyObject_TypeCheck(pyobj, &pgf_ExprTypedType)) { ExprTypedObject *etyped = (ExprTypedObject *)expr; @@ -429,7 +429,7 @@ match_type(PgfMarshaller *this, PgfUnmarshaller *u, PgfType ty) if (PyErr_Occurred()) return 0; - PgfText *cat = PyUnicode_AsPgfText(type->cat); + PgfText *cat = PyUnicode_AsPgfText(type->name); if (cat == NULL) { return 0; } diff --git a/src/runtime/python/tests/test_basic.py b/src/runtime/python/tests/test_basic.py index 3310cebfb..a574bf09f 100644 --- a/src/runtime/python/tests/test_basic.py +++ b/src/runtime/python/tests/test_basic.py @@ -281,9 +281,9 @@ def test_readExpr_lstr_newline(): assert str(ExprLit("ab\nc")) == "\"ab\\nc\"" def test_ExprLit_getters(): - assert ExprLit(123).value == 123 - assert ExprLit("123").value == "123" - assert ExprLit(1.23).value == 1.23 + assert ExprLit(123).val == 123 + assert ExprLit("123").val == "123" + assert ExprLit(1.23).val == 1.23 with pytest.raises(AttributeError): ExprLit(1.23).fake @@ -345,8 +345,8 @@ def test_ExprApp_getters(): e1 = ExprFun("f") e2 = ExprFun("x") expr = ExprApp(e1, e2) - assert expr.e1 == e1 - assert expr.e2 == e2 + assert expr.fun == e1 + assert expr.arg == e2 with pytest.raises(AttributeError): expr.fake @@ -438,9 +438,9 @@ def test_showExpr_eabs_freshvars_3(): def test_ExprAbs_getters(): e0 = ExprAbs(BIND_TYPE_EXPLICIT, "v", ExprVar(1)) expr = ExprAbs(BIND_TYPE_EXPLICIT, "v", e0) - assert expr.bindType == BIND_TYPE_EXPLICIT - assert expr.var == "v" - assert expr.expr == e0 + assert expr.bind_type == BIND_TYPE_EXPLICIT + assert expr.name == "v" + assert expr.body == e0 with pytest.raises(AttributeError): expr.fake