forked from GitHub/gf-core
Added inferExpr to py-bindings.
This commit is contained in:
@@ -219,6 +219,13 @@ gf_unstr pexp = do
|
|||||||
Just s -> newCString s
|
Just s -> newCString s
|
||||||
_ -> return nullPtr
|
_ -> return nullPtr
|
||||||
|
|
||||||
|
foreign export ccall gf_inferexpr :: Ptr PGF -> Ptr Expr -> Ptr Type -> IO ()
|
||||||
|
gf_inferexpr ppgf pexp ptype = do
|
||||||
|
pgf <- peek ppgf
|
||||||
|
exp <- peek pexp
|
||||||
|
let Right (_,t) = inferExpr pgf exp
|
||||||
|
poke ptype t
|
||||||
|
|
||||||
foreign import ccall "newLang" pyLang :: IO (Ptr Language)
|
foreign import ccall "newLang" pyLang :: IO (Ptr Language)
|
||||||
foreign import ccall "newTree" pyTree :: IO (Ptr Tree)
|
foreign import ccall "newTree" pyTree :: IO (Ptr Tree)
|
||||||
foreign import ccall "newCId" pyCId :: IO (Ptr CId)
|
foreign import ccall "newCId" pyCId :: IO (Ptr CId)
|
||||||
|
|||||||
@@ -245,13 +245,33 @@ unapp(Expr *self) {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject*
|
||||||
|
infer_expr(Expr *self, PyObject* args) {
|
||||||
|
PGF* pgf;
|
||||||
|
if (!PyArg_ParseTuple(args, "O", &pgf))
|
||||||
|
return NULL;
|
||||||
|
if (!checkType(pgf, &PGFType)) {
|
||||||
|
PyErr_Format(PyExc_ValueError, "Must be a pgf module.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
gfType* gftp = (gfType*)gfTypeType.tp_new(&gfTypeType,NULL,NULL);
|
||||||
|
gf_inferexpr(pgf, self, gftp);
|
||||||
|
return gftp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* todo: Is Tree == Expr ?
|
||||||
|
|
||||||
static PyMethodDef tree_methods[] = {
|
static PyMethodDef tree_methods[] = {
|
||||||
{"unapply", (PyCFunction)unapp, METH_NOARGS,"Unapply a tree."},
|
{"unapply", (PyCFunction)unapp, METH_NOARGS, "Unapply a tree."},
|
||||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
{NULL, NULL, 0, NULL} // * Sentinel * //
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
static PyMethodDef expr_methods[] = {
|
static PyMethodDef expr_methods[] = {
|
||||||
{"unapply", (PyCFunction)unapp, METH_NOARGS,"Unapply an expression."},
|
{"unapply", (PyCFunction)unapp, METH_NOARGS, "Unapply an expression."},
|
||||||
|
{"infer", (PyCFunction)infer_expr, METH_VARARGS, "Infer the type of an expression."},
|
||||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -297,7 +317,7 @@ initgf(void)
|
|||||||
READYTYPE(gfTypeType, gfType_repr, gfType_dealloc)
|
READYTYPE(gfTypeType, gfType_repr, gfType_dealloc)
|
||||||
ExprType.tp_methods = expr_methods;
|
ExprType.tp_methods = expr_methods;
|
||||||
READYTYPE(ExprType, expr_repr, expr_dealloc)
|
READYTYPE(ExprType, expr_repr, expr_dealloc)
|
||||||
TreeType.tp_methods = tree_methods;
|
TreeType.tp_methods = expr_methods; // Tree == Expr ?
|
||||||
READYTYPE(TreeType, tree_repr, Tree_dealloc)
|
READYTYPE(TreeType, tree_repr, Tree_dealloc)
|
||||||
|
|
||||||
m = Py_InitModule3("gf", gf_methods,
|
m = Py_InitModule3("gf", gf_methods,
|
||||||
|
|||||||
@@ -142,5 +142,17 @@ class TestUnapplyExpr(unittest.TestCase):
|
|||||||
uparsed = self.deep_unapp(parsed[0])
|
uparsed = self.deep_unapp(parsed[0])
|
||||||
self.assertEqual(abs,uparsed)
|
self.assertEqual(abs,uparsed)
|
||||||
|
|
||||||
|
def test_infer(self):
|
||||||
|
lg = 'eng'
|
||||||
|
lang = self.langs[lg]
|
||||||
|
cnc = self.samples[0][1]
|
||||||
|
parsed = self.pgf.parse(cnc[lg],lang)
|
||||||
|
exp = parsed[0]
|
||||||
|
for t in 'Question Object Int'.split():
|
||||||
|
self.assertEqual(`exp.infer(self.pgf)`, t)
|
||||||
|
uexp = exp.unapply()
|
||||||
|
if type(uexp) != type(2) and type(uexp) != type('2'):
|
||||||
|
exp = uexp[1]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user