Rename fields to match those in runtime. Use tp_members instead of tp_getattro for getters.

This commit is contained in:
John J. Camilleri
2021-09-26 15:14:04 +02:00
parent 4a0efda0e6
commit 1fce5144f8
4 changed files with 131 additions and 230 deletions

View File

@@ -1,6 +1,7 @@
#define PY_SSIZE_T_CLEAN #define PY_SSIZE_T_CLEAN
#include <Python.h> #include <Python.h>
#include <stdbool.h> #include <stdbool.h>
#include <structmember.h>
#include <pgf/pgf.h> #include <pgf/pgf.h>
#include "./expr.h" #include "./expr.h"
@@ -20,9 +21,9 @@ static int
Type_init(TypeObject *self, PyObject *args, PyObject *kwds) Type_init(TypeObject *self, PyObject *args, PyObject *kwds)
{ {
PyObject* hypos; PyObject* hypos;
PyObject* cat; PyObject* name;
PyObject* exprs; 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; return -1;
} }
@@ -54,10 +55,10 @@ Type_init(TypeObject *self, PyObject *args, PyObject *kwds)
// Py_INCREF(&exprs[i]); // Py_INCREF(&exprs[i]);
} }
self->hypos = hypos; self->hypos = hypos;
self->cat = cat; self->name = name;
self->exprs = exprs; self->exprs = exprs;
Py_INCREF(hypos); Py_INCREF(hypos);
// Py_INCREF(cat); // Py_INCREF(name);
Py_INCREF(exprs); Py_INCREF(exprs);
return 0; return 0;
@@ -79,7 +80,7 @@ Type_richcompare(TypeObject *t1, PyObject *p2, int op)
if (!PyObject_TypeCheck(p2, &pgf_TypeType)) goto done; if (!PyObject_TypeCheck(p2, &pgf_TypeType)) goto done;
TypeObject *t2 = (TypeObject *)p2; 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; if (PyList_Size(t1->hypos) != PyList_Size(t2->hypos)) goto done;
for (Py_ssize_t n = 0; n < PyList_Size(t1->hypos); n++) { for (Py_ssize_t n = 0; n < PyList_Size(t1->hypos); n++) {
@@ -114,36 +115,12 @@ static PyMethodDef Type_methods[] = {
{NULL} /* Sentinel */ {NULL} /* Sentinel */
}; };
static PyObject * static PyMemberDef Type_members[] = {
Type_getattro(TypeObject *self, PyObject *attr) {"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"},
if (PyUnicode_CompareWithASCIIString(attr, "hypos") == 0) { {"exprs", T_OBJECT_EX, offsetof(TypeObject, exprs), 0, "list of indices for the category"},
return self->hypos; {NULL} /* Sentinel */
} 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 */
// };
PyTypeObject pgf_TypeType = { PyTypeObject pgf_TypeType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
@@ -163,7 +140,7 @@ PyTypeObject pgf_TypeType = {
0, /*tp_hash */ 0, /*tp_hash */
0, /*tp_call*/ 0, /*tp_call*/
(reprfunc) Type_str, /*tp_str*/ (reprfunc) Type_str, /*tp_str*/
(getattrofunc) Type_getattro, /*tp_getattro*/ 0, //(getattrofunc) Type_getattro, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
@@ -175,7 +152,7 @@ PyTypeObject pgf_TypeType = {
0, /*tp_iter */ 0, /*tp_iter */
0, /*tp_iternext */ 0, /*tp_iternext */
Type_methods, /*tp_methods */ Type_methods, /*tp_methods */
0, /*tp_members */ Type_members, /*tp_members */
0, //Type_getseters, /*tp_getset */ 0, //Type_getseters, /*tp_getset */
0, /*tp_base */ 0, /*tp_base */
0, /*tp_dict */ 0, /*tp_dict */
@@ -204,30 +181,6 @@ static PyMethodDef Expr_methods[] = {
}; };
static PyGetSetDef Expr_getseters[] = { 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 */ {NULL} /* Sentinel */
}; };
@@ -285,18 +238,18 @@ ExprAbs_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
static int static int
ExprAbs_init(ExprAbsObject *self, PyObject *args, PyObject *kwds) ExprAbs_init(ExprAbsObject *self, PyObject *args, PyObject *kwds)
{ {
PyObject* bindType = NULL; PyObject* bind_type = NULL;
PyObject* var = NULL; PyObject* name = NULL;
ExprObject* expr = NULL; ExprObject* body = NULL;
if (!PyArg_ParseTuple(args, "O!UO!", &PyLong_Type, &bindType, &var, &pgf_ExprType, &expr)) { if (!PyArg_ParseTuple(args, "O!UO!", &PyLong_Type, &bind_type, &name, &pgf_ExprType, &body)) {
return -1; return -1;
} }
self->bindType = bindType; self->bind_type = bind_type;
self->var = var; self->name = name;
self->expr = expr; self->body = body;
Py_INCREF(bindType); Py_INCREF(bind_type);
Py_INCREF(var); Py_INCREF(name);
Py_INCREF(expr); Py_INCREF(body);
return 0; return 0;
} }
@@ -306,9 +259,9 @@ ExprAbs_richcompare(ExprAbsObject *e1, PyObject *p2, int op)
bool same = false; bool same = false;
if (!PyObject_TypeCheck(p2, &pgf_ExprAbsType)) goto done; if (!PyObject_TypeCheck(p2, &pgf_ExprAbsType)) goto done;
ExprAbsObject *e2 = (ExprAbsObject *)p2; ExprAbsObject *e2 = (ExprAbsObject *)p2;
if (!PyObject_RichCompareBool(e1->bindType, e2->bindType, Py_EQ)) goto done; if (!PyObject_RichCompareBool(e1->bind_type, e2->bind_type, Py_EQ)) goto done;
if (PyUnicode_Compare(e1->var, e2->var) != 0) goto done; if (PyUnicode_Compare(e1->name, e2->name) != 0) goto done;
if (!PyObject_RichCompareBool((PyObject*)e1->expr, (PyObject*)e2->expr, Py_EQ)) goto done; if (!PyObject_RichCompareBool((PyObject*)e1->body, (PyObject*)e2->body, Py_EQ)) goto done;
same = true; same = true;
done: done:
@@ -323,20 +276,12 @@ done:
} }
} }
static PyObject * static PyMemberDef ExprAbs_members[] = {
ExprAbs_getattro(ExprAbsObject *self, PyObject *attr) {"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"},
if (PyUnicode_CompareWithASCIIString(attr, "bindType") == 0) { {"body", T_OBJECT_EX, offsetof(ExprAbsObject, body), 0, "body of the abstraction"},
return self->bindType; {NULL} /* Sentinel */
} 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;
}
}
PyTypeObject pgf_ExprAbsType = { PyTypeObject pgf_ExprAbsType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
@@ -356,7 +301,7 @@ PyTypeObject pgf_ExprAbsType = {
0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(hashfunc) Expr_hash, /*tp_hash */
0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(ternaryfunc) Expr_call, /*tp_call*/
0, //(reprfunc) Expr_str, /*tp_str*/ 0, //(reprfunc) Expr_str, /*tp_str*/
(getattrofunc) ExprAbs_getattro, /*tp_getattro*/ 0, //(getattrofunc) ExprAbs_getattro, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
@@ -368,7 +313,7 @@ PyTypeObject pgf_ExprAbsType = {
0, /*tp_iter */ 0, /*tp_iter */
0, /*tp_iternext */ 0, /*tp_iternext */
0, //Expr_methods, /*tp_methods */ 0, //Expr_methods, /*tp_methods */
0, /*tp_members */ ExprAbs_members, /*tp_members */
0, //Expr_getseters, /*tp_getset */ 0, //Expr_getseters, /*tp_getset */
&pgf_ExprType, /*tp_base */ &pgf_ExprType, /*tp_base */
0, /*tp_dict */ 0, /*tp_dict */
@@ -392,15 +337,15 @@ ExprApp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
static int static int
ExprApp_init(ExprAppObject *self, PyObject *args, PyObject *kwds) ExprApp_init(ExprAppObject *self, PyObject *args, PyObject *kwds)
{ {
PyObject* e1 = NULL; PyObject* fun = NULL;
PyObject* e2 = NULL; PyObject* arg = NULL;
if (!PyArg_ParseTuple(args, "O!O!", &pgf_ExprType, &e1, &pgf_ExprType, &e2)) { if (!PyArg_ParseTuple(args, "O!O!", &pgf_ExprType, &fun, &pgf_ExprType, &arg)) {
return -1; return -1;
} }
self->e1 = (ExprObject *)e1; self->fun = (ExprObject *)fun;
self->e2 = (ExprObject *)e2; self->arg = (ExprObject *)arg;
Py_INCREF(e1); Py_INCREF(fun);
Py_INCREF(e2); Py_INCREF(arg);
return 0; return 0;
} }
@@ -410,8 +355,8 @@ ExprApp_richcompare(ExprAppObject *e1, PyObject *p2, int op)
bool same = false; bool same = false;
if (!PyObject_TypeCheck(p2, &pgf_ExprAppType)) goto done; if (!PyObject_TypeCheck(p2, &pgf_ExprAppType)) goto done;
ExprAppObject *e2 = (ExprAppObject *)p2; ExprAppObject *e2 = (ExprAppObject *)p2;
if (!PyObject_RichCompareBool((PyObject*)e1->e1, (PyObject*)e2->e1, Py_EQ)) goto done; if (!PyObject_RichCompareBool((PyObject*)e1->fun, (PyObject*)e2->fun, Py_EQ)) goto done;
if (!PyObject_RichCompareBool((PyObject*)e1->e2, (PyObject*)e2->e2, Py_EQ)) goto done; if (!PyObject_RichCompareBool((PyObject*)e1->arg, (PyObject*)e2->arg, Py_EQ)) goto done;
same = true; same = true;
done: done:
@@ -426,18 +371,11 @@ done:
} }
} }
static PyObject * static PyMemberDef ExprApp_members[] = {
ExprApp_getattro(ExprAppObject *self, PyObject *attr) {"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"},
if (PyUnicode_CompareWithASCIIString(attr, "e1") == 0) { {NULL} /* Sentinel */
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;
}
}
PyTypeObject pgf_ExprAppType = { PyTypeObject pgf_ExprAppType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
@@ -457,7 +395,7 @@ PyTypeObject pgf_ExprAppType = {
0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(hashfunc) Expr_hash, /*tp_hash */
0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(ternaryfunc) Expr_call, /*tp_call*/
0, //(reprfunc) Expr_str, /*tp_str*/ 0, //(reprfunc) Expr_str, /*tp_str*/
(getattrofunc) ExprApp_getattro, /*tp_getattro*/ 0, //(getattrofunc) ExprApp_getattro, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
@@ -469,7 +407,7 @@ PyTypeObject pgf_ExprAppType = {
0, /*tp_iter */ 0, /*tp_iter */
0, /*tp_iternext */ 0, /*tp_iternext */
0, //Expr_methods, /*tp_methods */ 0, //Expr_methods, /*tp_methods */
0, /*tp_members */ ExprApp_members, /*tp_members */
0, //Expr_getseters, /*tp_getset */ 0, //Expr_getseters, /*tp_getset */
&pgf_ExprType, /*tp_base */ &pgf_ExprType, /*tp_base */
0, /*tp_dict */ 0, /*tp_dict */
@@ -498,7 +436,7 @@ ExprLit_init(ExprLitObject *self, PyObject *args, PyObject *kwds)
return -1; return -1;
} }
if (PyLong_Check(lit) || PyFloat_Check(lit) || PyUnicode_Check(lit)) { if (PyLong_Check(lit) || PyFloat_Check(lit) || PyUnicode_Check(lit)) {
self->value = lit; self->lit = lit;
Py_INCREF(lit); Py_INCREF(lit);
return 0; return 0;
} else { } else {
@@ -514,18 +452,18 @@ ExprLit_richcompare(ExprLitObject *e1, PyObject *p2, int op)
if (!PyObject_TypeCheck(p2, &pgf_ExprLitType)) goto done; if (!PyObject_TypeCheck(p2, &pgf_ExprLitType)) goto done;
ExprLitObject *e2 = (ExprLitObject *)p2; ExprLitObject *e2 = (ExprLitObject *)p2;
if (PyLong_Check(e1->value)) { if (PyLong_Check(e1->lit)) {
if (!PyLong_Check(e2->value)) goto done; if (!PyLong_Check(e2->lit)) goto done;
int o1, o2; int o1, o2;
int l1 = PyLong_AsLongAndOverflow(e1->value, &o1); int l1 = PyLong_AsLongAndOverflow(e1->lit, &o1);
int l2 = PyLong_AsLongAndOverflow(e2->value, &o2); int l2 = PyLong_AsLongAndOverflow(e2->lit, &o2);
if (!(l1 == l2 && o1 == o2)) goto done; if (!(l1 == l2 && o1 == o2)) goto done;
} else if (PyFloat_Check(e1->value)) { } else if (PyFloat_Check(e1->lit)) {
if (!PyFloat_Check(e2->value)) goto done; if (!PyFloat_Check(e2->lit)) goto done;
if (PyFloat_AsDouble(e1->value) != PyFloat_AsDouble(e2->value)) goto done; if (PyFloat_AsDouble(e1->lit) != PyFloat_AsDouble(e2->lit)) goto done;
} else if (PyUnicode_Check(e1->value)) { } else if (PyUnicode_Check(e1->lit)) {
if (!PyUnicode_Check(e2->value)) goto done; if (!PyUnicode_Check(e2->lit)) goto done;
if (PyUnicode_Compare(e1->value, e2->value) != 0) goto done; if (PyUnicode_Compare(e1->lit, e2->lit) != 0) goto done;
} else { } else {
PyErr_SetString(PyExc_TypeError, "unknown literal type"); PyErr_SetString(PyExc_TypeError, "unknown literal type");
return NULL; return NULL;
@@ -544,16 +482,10 @@ done:
} }
} }
static PyObject * static PyMemberDef ExprLit_members[] = {
ExprLit_getattro(ExprLitObject *self, PyObject *attr) {"val", T_OBJECT_EX, offsetof(ExprLitObject, lit), 0, "the value of the literal"},
{ {NULL} /* Sentinel */
if (PyUnicode_CompareWithASCIIString(attr, "value") == 0) { };
return self->value;
} else {
PyErr_Format(PyExc_AttributeError, "'pgf.ExprLit' object has no attribute '%U'", attr);
return NULL;
}
}
PyTypeObject pgf_ExprLitType = { PyTypeObject pgf_ExprLitType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
@@ -573,7 +505,7 @@ PyTypeObject pgf_ExprLitType = {
0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(hashfunc) Expr_hash, /*tp_hash */
0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(ternaryfunc) Expr_call, /*tp_call*/
0, //(reprfunc) Expr_str, /*tp_str*/ 0, //(reprfunc) Expr_str, /*tp_str*/
(getattrofunc) ExprLit_getattro, /*tp_getattro*/ 0, //(getattrofunc) ExprLit_getattro, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
@@ -585,7 +517,7 @@ PyTypeObject pgf_ExprLitType = {
0, /*tp_iter */ 0, /*tp_iter */
0, /*tp_iternext */ 0, /*tp_iternext */
0, /*tp_methods */ 0, /*tp_methods */
0, /*tp_members */ ExprLit_members, /*tp_members */
0, /*tp_getset */ 0, /*tp_getset */
&pgf_ExprType, /*tp_base */ &pgf_ExprType, /*tp_base */
0, /*tp_dict */ 0, /*tp_dict */
@@ -647,16 +579,10 @@ done:
} }
} }
static PyObject * static PyMemberDef ExprMeta_members[] = {
ExprMeta_getattro(ExprMetaObject *self, PyObject *attr) {"id", T_OBJECT_EX, offsetof(ExprMetaObject, id), 0, "the id of a meta variable"},
{ {NULL} /* Sentinel */
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;
}
}
PyTypeObject pgf_ExprMetaType = { PyTypeObject pgf_ExprMetaType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
@@ -676,7 +602,7 @@ PyTypeObject pgf_ExprMetaType = {
0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(hashfunc) Expr_hash, /*tp_hash */
0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(ternaryfunc) Expr_call, /*tp_call*/
0, //(reprfunc) Expr_str, /*tp_str*/ 0, //(reprfunc) Expr_str, /*tp_str*/
(getattrofunc) ExprMeta_getattro, /*tp_getattro*/ 0, //(getattrofunc) ExprMeta_getattro, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
@@ -688,7 +614,7 @@ PyTypeObject pgf_ExprMetaType = {
0, /*tp_iter */ 0, /*tp_iter */
0, /*tp_iternext */ 0, /*tp_iternext */
0, //Expr_methods, /*tp_methods */ 0, //Expr_methods, /*tp_methods */
0, /*tp_members */ ExprMeta_members, /*tp_members */
0, //Expr_getseters, /*tp_getset */ 0, //Expr_getseters, /*tp_getset */
&pgf_ExprType, /*tp_base */ &pgf_ExprType, /*tp_base */
0, /*tp_dict */ 0, /*tp_dict */
@@ -742,16 +668,10 @@ done:
} }
} }
static PyObject * static PyMemberDef ExprFun_members[] = {
ExprFun_getattro(ExprFunObject *self, PyObject *attr) {"name", T_OBJECT_EX, offsetof(ExprFunObject, name), 0, "the name of the function"},
{ {NULL} /* Sentinel */
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;
}
}
PyTypeObject pgf_ExprFunType = { PyTypeObject pgf_ExprFunType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
@@ -771,7 +691,7 @@ PyTypeObject pgf_ExprFunType = {
0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(hashfunc) Expr_hash, /*tp_hash */
0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(ternaryfunc) Expr_call, /*tp_call*/
0, //(reprfunc) Expr_str, /*tp_str*/ 0, //(reprfunc) Expr_str, /*tp_str*/
(getattrofunc) ExprFun_getattro, /*tp_getattro*/ 0, //(getattrofunc) ExprFun_getattro, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
@@ -783,7 +703,7 @@ PyTypeObject pgf_ExprFunType = {
0, /*tp_iter */ 0, /*tp_iter */
0, /*tp_iternext */ 0, /*tp_iternext */
0, //Expr_methods, /*tp_methods */ 0, //Expr_methods, /*tp_methods */
0, /*tp_members */ ExprFun_members, /*tp_members */
0, //Expr_getseters, /*tp_getset */ 0, //Expr_getseters, /*tp_getset */
&pgf_ExprType, /*tp_base */ &pgf_ExprType, /*tp_base */
0, /*tp_dict */ 0, /*tp_dict */
@@ -812,10 +732,10 @@ ExprVar_init(ExprVarObject *self, PyObject *args, PyObject *kwds)
return -1; return -1;
} }
if (lit == NULL) { if (lit == NULL) {
self->index = PyLong_FromLong(0); self->var = PyLong_FromLong(0);
return 0; return 0;
} else if (PyLong_Check(lit)) { } else if (PyLong_Check(lit)) {
self->index = lit; self->var = lit;
Py_INCREF(lit); Py_INCREF(lit);
return 0; return 0;
} else { } else {
@@ -830,7 +750,7 @@ ExprVar_richcompare(ExprVarObject *e1, PyObject *p2, int op)
bool same = false; bool same = false;
if (!PyObject_TypeCheck(p2, &pgf_ExprVarType)) goto done; if (!PyObject_TypeCheck(p2, &pgf_ExprVarType)) goto done;
ExprVarObject *e2 = (ExprVarObject *)p2; 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; same = true;
done: done:
@@ -845,16 +765,10 @@ done:
} }
} }
static PyObject * static PyMemberDef ExprVar_members[] = {
ExprVar_getattro(ExprVarObject *self, PyObject *attr) {"index", T_OBJECT_EX, offsetof(ExprVarObject, var), 0, "the de Bruijn index of a variable"},
{ {NULL} /* Sentinel */
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;
}
}
PyTypeObject pgf_ExprVarType = { PyTypeObject pgf_ExprVarType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
@@ -874,7 +788,7 @@ PyTypeObject pgf_ExprVarType = {
0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(hashfunc) Expr_hash, /*tp_hash */
0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(ternaryfunc) Expr_call, /*tp_call*/
0, //(reprfunc) Expr_str, /*tp_str*/ 0, //(reprfunc) Expr_str, /*tp_str*/
(getattrofunc) ExprVar_getattro, /*tp_getattro*/ 0, //(getattrofunc) ExprVar_getattro, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
@@ -886,7 +800,7 @@ PyTypeObject pgf_ExprVarType = {
0, /*tp_iter */ 0, /*tp_iter */
0, /*tp_iternext */ 0, /*tp_iternext */
0, //Expr_methods, /*tp_methods */ 0, //Expr_methods, /*tp_methods */
0, /*tp_members */ ExprVar_members, /*tp_members */
0, //Expr_getseters, /*tp_getset */ 0, //Expr_getseters, /*tp_getset */
&pgf_ExprType, /*tp_base */ &pgf_ExprType, /*tp_base */
0, /*tp_dict */ 0, /*tp_dict */
@@ -944,18 +858,11 @@ done:
} }
} }
static PyObject * static PyMemberDef ExprTyped_members[] = {
ExprTyped_getattro(ExprTypedObject *self, PyObject *attr) {"expr", T_OBJECT_EX, offsetof(ExprTypedObject, expr), 0, "the expression"},
{ {"type", T_OBJECT_EX, offsetof(ExprTypedObject, type), 0, "the type"},
if (PyUnicode_CompareWithASCIIString(attr, "expr") == 0) { {NULL} /* Sentinel */
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;
}
}
PyTypeObject pgf_ExprTypedType = { PyTypeObject pgf_ExprTypedType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
@@ -975,7 +882,7 @@ PyTypeObject pgf_ExprTypedType = {
0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(hashfunc) Expr_hash, /*tp_hash */
0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(ternaryfunc) Expr_call, /*tp_call*/
0, //(reprfunc) Expr_str, /*tp_str*/ 0, //(reprfunc) Expr_str, /*tp_str*/
(getattrofunc) ExprTyped_getattro, /*tp_getattro*/ 0, //(getattrofunc) ExprTyped_getattro, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
@@ -987,7 +894,7 @@ PyTypeObject pgf_ExprTypedType = {
0, /*tp_iter */ 0, /*tp_iter */
0, /*tp_iternext */ 0, /*tp_iternext */
0, //Expr_methods, /*tp_methods */ 0, //Expr_methods, /*tp_methods */
0, /*tp_members */ ExprTyped_members, /*tp_members */
0, //Expr_getseters, /*tp_getset */ 0, //Expr_getseters, /*tp_getset */
&pgf_ExprType, /*tp_base */ &pgf_ExprType, /*tp_base */
0, /*tp_dict */ 0, /*tp_dict */
@@ -1041,16 +948,10 @@ done:
} }
} }
static PyObject * static PyMemberDef ExprImplArg_members[] = {
ExprImplArg_getattro(ExprImplArgObject *self, PyObject *attr) {"expr", T_OBJECT_EX, offsetof(ExprImplArgObject, expr), 0, "the inner expression"},
{ {NULL} /* Sentinel */
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;
}
}
PyTypeObject pgf_ExprImplArgType = { PyTypeObject pgf_ExprImplArgType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
@@ -1070,7 +971,7 @@ PyTypeObject pgf_ExprImplArgType = {
0, //(hashfunc) Expr_hash, /*tp_hash */ 0, //(hashfunc) Expr_hash, /*tp_hash */
0, //(ternaryfunc) Expr_call, /*tp_call*/ 0, //(ternaryfunc) Expr_call, /*tp_call*/
0, //(reprfunc) Expr_str, /*tp_str*/ 0, //(reprfunc) Expr_str, /*tp_str*/
(getattrofunc) ExprImplArg_getattro, /*tp_getattro*/ 0, //(getattrofunc) ExprImplArg_getattro, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
@@ -1082,7 +983,7 @@ PyTypeObject pgf_ExprImplArgType = {
0, /*tp_iter */ 0, /*tp_iter */
0, /*tp_iternext */ 0, /*tp_iternext */
0, //Expr_methods, /*tp_methods */ 0, //Expr_methods, /*tp_methods */
0, /*tp_members */ ExprImplArg_members, /*tp_members */
0, //Expr_getseters, /*tp_getset */ 0, //Expr_getseters, /*tp_getset */
&pgf_ExprType, /*tp_base */ &pgf_ExprType, /*tp_base */
0, /*tp_dict */ 0, /*tp_dict */

View File

@@ -9,7 +9,7 @@
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
PyObject *hypos; // PyListObject of PyTupleObject: (bind_type: int, cid: string, type: TypeObject) PyObject *hypos; // PyListObject of PyTupleObject: (bind_type: int, cid: string, type: TypeObject)
PyObject *cat; // PyUnicodeObject PyObject *name; // PyUnicodeObject
PyObject *exprs; // PyListObject of ExprObject PyObject *exprs; // PyListObject of ExprObject
} TypeObject; } TypeObject;
@@ -21,20 +21,20 @@ typedef struct {
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
PyObject *bindType; // PyLongObject PyObject *bind_type; // PyLongObject
PyObject *var; // PyUnicodeObject PyObject *name; // PyUnicodeObject
ExprObject *expr; ExprObject *body;
} ExprAbsObject; } ExprAbsObject;
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
ExprObject *e1; // ExprObject ExprObject *fun; // ExprObject
ExprObject *e2; // ExprObject ExprObject *arg; // ExprObject
} ExprAppObject; } ExprAppObject;
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
PyObject *value; // PyLongObject | PyFloatObject | PyUnicodeObject PyObject *lit; // PyLongObject | PyFloatObject | PyUnicodeObject
} ExprLitObject; } ExprLitObject;
typedef struct { typedef struct {
@@ -49,7 +49,7 @@ typedef struct {
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
PyObject *index; // PyLongObject PyObject *var; // PyLongObject
} ExprVarObject; } ExprVarObject;
typedef struct { typedef struct {

View File

@@ -174,9 +174,9 @@ static PgfExpr
eabs(PgfUnmarshaller *this, PgfBindType btype, PgfText *name, PgfExpr body) eabs(PgfUnmarshaller *this, PgfBindType btype, PgfText *name, PgfExpr body)
{ {
ExprAbsObject *pyexpr = (ExprAbsObject *)pgf_ExprAbsType.tp_alloc(&pgf_ExprAbsType, 0); ExprAbsObject *pyexpr = (ExprAbsObject *)pgf_ExprAbsType.tp_alloc(&pgf_ExprAbsType, 0);
pyexpr->bindType = PyLong_FromLong(btype); pyexpr->bind_type = PyLong_FromLong(btype);
pyexpr->var = PyUnicode_FromPgfText(name); pyexpr->name = PyUnicode_FromPgfText(name);
pyexpr->expr = (ExprObject *)body; pyexpr->body = (ExprObject *)body;
// Py_INCREF(body); // Py_INCREF(body);
return (PgfExpr) pyexpr; return (PgfExpr) pyexpr;
} }
@@ -185,8 +185,8 @@ static PgfExpr
eapp(PgfUnmarshaller *this, PgfExpr fun, PgfExpr arg) eapp(PgfUnmarshaller *this, PgfExpr fun, PgfExpr arg)
{ {
ExprAppObject *pyexpr = (ExprAppObject *)pgf_ExprAppType.tp_alloc(&pgf_ExprAppType, 0); ExprAppObject *pyexpr = (ExprAppObject *)pgf_ExprAppType.tp_alloc(&pgf_ExprAppType, 0);
pyexpr->e1 = (ExprObject *)fun; pyexpr->fun = (ExprObject *)fun;
pyexpr->e2 = (ExprObject *)arg; pyexpr->arg = (ExprObject *)arg;
// Py_INCREF(fun); // Py_INCREF(fun);
// Py_INCREF(arg); // Py_INCREF(arg);
return (PgfExpr) pyexpr; return (PgfExpr) pyexpr;
@@ -197,7 +197,7 @@ elit(PgfUnmarshaller *this, PgfLiteral lit)
{ {
ExprLitObject *pyexpr = (ExprLitObject *)pgf_ExprLitType.tp_alloc(&pgf_ExprLitType, 0); ExprLitObject *pyexpr = (ExprLitObject *)pgf_ExprLitType.tp_alloc(&pgf_ExprLitType, 0);
PyObject *pyobj = (PyObject *)lit; PyObject *pyobj = (PyObject *)lit;
pyexpr->value = pyobj; pyexpr->lit = pyobj;
Py_INCREF(pyobj); Py_INCREF(pyobj);
return (PgfExpr) pyexpr; return (PgfExpr) pyexpr;
} }
@@ -224,7 +224,7 @@ static PgfExpr
evar(PgfUnmarshaller *this, int index) evar(PgfUnmarshaller *this, int index)
{ {
ExprVarObject *pyexpr = (ExprVarObject *)pgf_ExprVarType.tp_alloc(&pgf_ExprVarType, 0); ExprVarObject *pyexpr = (ExprVarObject *)pgf_ExprVarType.tp_alloc(&pgf_ExprVarType, 0);
pyexpr->index = PyLong_FromLong(index); pyexpr->var = PyLong_FromLong(index);
return (PgfExpr) pyexpr; 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); TypeObject *pytype = (TypeObject *)pgf_TypeType.tp_alloc(&pgf_TypeType, 0);
pytype->hypos = PyList_FromHypos(hypos, n_hypos); 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); pytype->exprs = PyList_New(n_exprs);
for (int i = 0; i < n_exprs; i++) { for (int i = 0; i < n_exprs; i++) {
PyList_SetItem(pytype->exprs, i, (PyObject *)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)) { if (PyObject_TypeCheck(pyobj, &pgf_ExprAbsType)) {
ExprAbsObject *eabs = (ExprAbsObject *)expr; 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 } else
if (PyObject_TypeCheck(pyobj, &pgf_ExprAppType)) { if (PyObject_TypeCheck(pyobj, &pgf_ExprAppType)) {
ExprAppObject *eapp = (ExprAppObject *)expr; 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 } else
if (PyObject_TypeCheck(pyobj, &pgf_ExprLitType)) { if (PyObject_TypeCheck(pyobj, &pgf_ExprLitType)) {
ExprLitObject *elit = (ExprLitObject *)expr; 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 } else
if (PyObject_TypeCheck(pyobj, &pgf_ExprMetaType)) { if (PyObject_TypeCheck(pyobj, &pgf_ExprMetaType)) {
ExprMetaObject *emeta = (ExprMetaObject *)expr; ExprMetaObject *emeta = (ExprMetaObject *)expr;
@@ -404,7 +404,7 @@ match_expr(PgfMarshaller *this, PgfUnmarshaller *u, PgfExpr expr)
} else } else
if (PyObject_TypeCheck(pyobj, &pgf_ExprVarType)) { if (PyObject_TypeCheck(pyobj, &pgf_ExprVarType)) {
ExprVarObject *evar = (ExprVarObject *)expr; ExprVarObject *evar = (ExprVarObject *)expr;
return u->vtbl->evar(u, PyLong_AsLong(evar->index)); return u->vtbl->evar(u, PyLong_AsLong(evar->var));
} else } else
if (PyObject_TypeCheck(pyobj, &pgf_ExprTypedType)) { if (PyObject_TypeCheck(pyobj, &pgf_ExprTypedType)) {
ExprTypedObject *etyped = (ExprTypedObject *)expr; ExprTypedObject *etyped = (ExprTypedObject *)expr;
@@ -429,7 +429,7 @@ match_type(PgfMarshaller *this, PgfUnmarshaller *u, PgfType ty)
if (PyErr_Occurred()) if (PyErr_Occurred())
return 0; return 0;
PgfText *cat = PyUnicode_AsPgfText(type->cat); PgfText *cat = PyUnicode_AsPgfText(type->name);
if (cat == NULL) { if (cat == NULL) {
return 0; return 0;
} }

View File

@@ -281,9 +281,9 @@ def test_readExpr_lstr_newline():
assert str(ExprLit("ab\nc")) == "\"ab\\nc\"" assert str(ExprLit("ab\nc")) == "\"ab\\nc\""
def test_ExprLit_getters(): def test_ExprLit_getters():
assert ExprLit(123).value == 123 assert ExprLit(123).val == 123
assert ExprLit("123").value == "123" assert ExprLit("123").val == "123"
assert ExprLit(1.23).value == 1.23 assert ExprLit(1.23).val == 1.23
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
ExprLit(1.23).fake ExprLit(1.23).fake
@@ -345,8 +345,8 @@ def test_ExprApp_getters():
e1 = ExprFun("f") e1 = ExprFun("f")
e2 = ExprFun("x") e2 = ExprFun("x")
expr = ExprApp(e1, e2) expr = ExprApp(e1, e2)
assert expr.e1 == e1 assert expr.fun == e1
assert expr.e2 == e2 assert expr.arg == e2
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
expr.fake expr.fake
@@ -438,9 +438,9 @@ def test_showExpr_eabs_freshvars_3():
def test_ExprAbs_getters(): def test_ExprAbs_getters():
e0 = ExprAbs(BIND_TYPE_EXPLICIT, "v", ExprVar(1)) e0 = ExprAbs(BIND_TYPE_EXPLICIT, "v", ExprVar(1))
expr = ExprAbs(BIND_TYPE_EXPLICIT, "v", e0) expr = ExprAbs(BIND_TYPE_EXPLICIT, "v", e0)
assert expr.bindType == BIND_TYPE_EXPLICIT assert expr.bind_type == BIND_TYPE_EXPLICIT
assert expr.var == "v" assert expr.name == "v"
assert expr.expr == e0 assert expr.body == e0
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
expr.fake expr.fake