1
0
forked from GitHub/gf-core

Add ExprFun to Python bindings

This commit is contained in:
John J. Camilleri
2021-09-14 15:07:03 +02:00
parent 05813384e0
commit 6ebb8e5fda
6 changed files with 158 additions and 47 deletions

View File

@@ -15,25 +15,25 @@ Expr_str(ExprObject *self)
return str;
}
static PyObject *
Expr_richcompare(ExprObject *e1, ExprObject *e2, int op)
{
bool same = false;
// TODO
// 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 PyObject *
// Expr_richcompare(ExprObject *e1, ExprObject *e2, int op)
// {
// bool same = false;
//
// // TODO
//
// // 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 PyMethodDef Expr_methods[] = {
// {"unpack", (PyCFunction)Expr_unpack, METH_VARARGS,
@@ -106,7 +106,7 @@ PyTypeObject pgf_ExprType = {
"abstract syntax tree", /*tp_doc*/
0, /*tp_traverse */
0, /*tp_clear */
(richcmpfunc) Expr_richcompare, /*tp_richcompare */
0, //(richcmpfunc) Expr_richcompare, /*tp_richcompare */
0, /*tp_weaklistoffset */
0, /*tp_iter */
0, /*tp_iternext */
@@ -125,6 +125,88 @@ PyTypeObject pgf_ExprType = {
// ----------------------------------------------------------------------------
static ExprFunObject *
ExprFun_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
{
ExprFunObject* self = (ExprFunObject *)subtype->tp_alloc(subtype, 0);
return self;
}
static int
ExprFun_init(ExprFunObject *self, PyObject *args, PyObject *kwds)
{
PyObject* lit = NULL;
if (!PyArg_ParseTuple(args, "U", &lit)) {
return -1;
}
self->fun = lit;
return 0;
}
static PyObject *
ExprFun_richcompare(ExprFunObject *t1, ExprFunObject *t2, int op)
{
bool same = false;
if (PyUnicode_Compare(t1->fun, t2->fun) != 0) 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 */
PyTypeObject pgf_ExprFunType = {
PyVarObject_HEAD_INIT(NULL, 0)
//0, /*ob_size*/
"pgf.ExprFun", /*tp_name*/
sizeof(ExprFunObject), /*tp_basicsize*/
0, /*tp_itemsize*/
0, //(destructor)Expr_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, //(hashfunc) Expr_hash, /*tp_hash */
0, //(ternaryfunc) Expr_call, /*tp_call*/
0, //(reprfunc) Expr_str, /*tp_str*/
0, //(getattrofunc) Expr_getattro,/*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"function or data constructor", /*tp_doc*/
0, /*tp_traverse */
0, /*tp_clear */
(richcmpfunc) ExprFun_richcompare, /*tp_richcompare */
0, /*tp_weaklistoffset */
0, /*tp_iter */
0, /*tp_iternext */
0, //Expr_methods, /*tp_methods */
0, /*tp_members */
0, //Expr_getseters, /*tp_getset */
&pgf_ExprType, /*tp_base */
0, /*tp_dict */
0, /*tp_descr_get */
0, /*tp_descr_set */
0, /*tp_dictoffset */
(initproc) ExprFun_init, /*tp_init */
0, /*tp_alloc */
(newfunc) ExprFun_new, /*tp_new */
};
// ----------------------------------------------------------------------------
static ExprLitObject *
ExprLit_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
{