Add ExprMeta type, with two basic tests

This commit is contained in:
John J. Camilleri
2021-09-13 16:18:32 +02:00
parent d8a7aef46b
commit c5ce2fd4b7
5 changed files with 164 additions and 21 deletions

View File

@@ -215,7 +215,7 @@ PyTypeObject pgf_ExprLitType = {
0, //Expr_methods, /*tp_methods */
0, /*tp_members */
0, //Expr_getseters, /*tp_getset */
&pgf_ExprType, /*tp_base */
&pgf_ExprType, /*tp_base */
0, /*tp_dict */
0, /*tp_descr_get */
0, /*tp_descr_set */
@@ -227,6 +227,96 @@ PyTypeObject pgf_ExprLitType = {
// ----------------------------------------------------------------------------
static ExprMetaObject *
ExprMeta_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
{
ExprMetaObject* self = (ExprMetaObject *)subtype->tp_alloc(subtype, 0);
return self;
}
static int
ExprMeta_init(ExprMetaObject *self, PyObject *args, PyObject *kwds)
{
PyObject* lit = NULL;
if (!PyArg_ParseTuple(args, "|O", &lit)) {
return -1;
}
if (lit == NULL) {
self->index = PyLong_FromLong(0);
return 0;
} else if (PyLong_Check(lit)) {
self->index = lit;
return 0;
} else {
PyErr_SetString(PyExc_TypeError, "invalid argument in ExprMeta_init");
return -1;
}
}
static PyObject *
ExprMeta_richcompare(ExprMetaObject *t1, ExprMetaObject *t2, int op)
{
bool same = false;
if (PyObject_RichCompareBool(t1->index, t2->index, Py_EQ) != 1) 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_ExprMetaType = {
PyVarObject_HEAD_INIT(NULL, 0)
//0, /*ob_size*/
"pgf.ExprMeta", /*tp_name*/
sizeof(ExprMetaObject), /*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*/
"meta variable", /*tp_doc*/
0, /*tp_traverse */
0, /*tp_clear */
(richcmpfunc) ExprMeta_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) ExprMeta_init, /*tp_init */
0, /*tp_alloc */
(newfunc) ExprMeta_new, /*tp_new */
};
// ----------------------------------------------------------------------------
static PyObject *
Type_str(TypeObject *self)
{