Added unapplying of Expr in py-bindings.

This commit is contained in:
jordi.saludes
2010-07-25 14:48:25 +00:00
parent ea67281820
commit 0e3cf2e5f2
3 changed files with 80 additions and 5 deletions

View File

@@ -225,6 +225,38 @@ DEALLOCFN(expr_dealloc, Expr, gf_freeExpr, "freeExpr")
REPRCB(expr_repr, Expr, gf_showExpr)
static PyObject*
unapp(Expr *self) {
PyObject* obj = gf_unapp(self);
if (!obj) {
char* s = gf_unstr(self);
if (s) {
obj = PyString_FromString(s);
free(s);
} else {
long n = gf_unint(self);
if (n != -9) {
obj = PyInt_FromLong(n);
} else {
PyErr_Format(PyExc_TypeError, "Cannot unapply expr.");
}
}
}
return obj;
}
static PyMethodDef tree_methods[] = {
{"unapply", (PyCFunction)unapp, METH_NOARGS,"Unapply a tree."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static PyMethodDef expr_methods[] = {
{"unapply", (PyCFunction)unapp, METH_NOARGS,"Unapply an expression."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
/* tree typr: methods, constructor and destructor */
// Are Expr and Tree equivalent ?
@@ -232,6 +264,9 @@ REPRCB(tree_repr, Tree, gf_showExpr)
DEALLOCFN(Tree_dealloc, Tree, gf_freeTree, "freeTree")
/* gf module methods */
static PyMethodDef gf_methods[] = {
@@ -260,7 +295,9 @@ initgf(void)
READYTYPE(PGFType, pgf_repr, PGF_dealloc)
READYTYPE(LangType, lang_repr, Lang_dealloc)
READYTYPE(gfTypeType, gfType_repr, gfType_dealloc)
ExprType.tp_methods = expr_methods;
READYTYPE(ExprType, expr_repr, expr_dealloc)
TreeType.tp_methods = tree_methods;
READYTYPE(TreeType, tree_repr, Tree_dealloc)
m = Py_InitModule3("gf", gf_methods,