mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-13 23:09:31 -06:00
Changed calling style in py-bindings.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||
-- GF Python bindings -- Jordi Saludes, upc.edu 2010
|
||||
|
||||
-- Jordi Saludes, upc.edu 2010
|
||||
--
|
||||
module PyGF where
|
||||
|
||||
import PGF
|
||||
@@ -92,11 +93,12 @@ gf_printCId p = do
|
||||
newCString (showCId c)
|
||||
-}
|
||||
|
||||
foreign export ccall gf_readPGF :: Ptr PGF -> CString -> IO ()
|
||||
gf_readPGF pt path = do
|
||||
p <- (peekCString path)
|
||||
result <- (readPGF p)
|
||||
poke pt result
|
||||
foreign export ccall gf_readPGF :: CString -> IO (Ptr PGF)
|
||||
gf_readPGF path = do
|
||||
ppgf <- pyPGF
|
||||
p <- peekCString path
|
||||
readPGF p >>= poke ppgf
|
||||
return ppgf
|
||||
|
||||
foreign export ccall gf_readLanguage :: Ptr Language -> CString -> IO Bool
|
||||
gf_readLanguage pt str = do
|
||||
@@ -107,10 +109,12 @@ gf_readLanguage pt str = do
|
||||
return True
|
||||
Nothing -> return False
|
||||
|
||||
foreign export ccall gf_startCat :: Ptr PGF -> Ptr Type -> IO ()
|
||||
gf_startCat ppgf pcat= do
|
||||
foreign export ccall gf_startCat :: Ptr PGF -> IO (Ptr Type)
|
||||
gf_startCat ppgf = do
|
||||
pgf <- peek ppgf
|
||||
pcat <- pyType
|
||||
poke pcat (startCat pgf)
|
||||
return pcat
|
||||
|
||||
foreign export ccall gf_parse :: Ptr PGF -> Ptr Language -> Ptr Type -> CString -> IO (Ptr ())
|
||||
gf_parse ppgf plang pcat input = do
|
||||
@@ -157,10 +161,12 @@ gf_showPrintName ppgf plang pcid = do
|
||||
cid <- peek pcid
|
||||
newCString (showPrintName pgf lang cid)
|
||||
|
||||
foreign export ccall gf_abstractName :: Ptr PGF -> Ptr Language -> IO ()
|
||||
gf_abstractName ppgf pabs = do
|
||||
foreign export ccall gf_abstractName :: Ptr PGF -> IO (Ptr Language)
|
||||
gf_abstractName ppgf = do
|
||||
pabs <- pyLang
|
||||
pgf <- peek ppgf
|
||||
poke pabs $ abstractName pgf
|
||||
return pabs
|
||||
|
||||
foreign export ccall gf_linearize :: Ptr PGF -> Ptr Language -> Ptr Tree -> IO CString
|
||||
gf_linearize ppgf plang ptree = do
|
||||
@@ -221,12 +227,16 @@ gf_unstr pexp = do
|
||||
Just s -> newCString s
|
||||
_ -> return nullPtr
|
||||
|
||||
foreign export ccall gf_inferexpr :: Ptr PGF -> Ptr Expr -> Ptr Type -> IO ()
|
||||
gf_inferexpr ppgf pexp ptype = do
|
||||
foreign export ccall gf_inferexpr :: Ptr PGF -> Ptr Expr -> IO (Ptr Type)
|
||||
gf_inferexpr ppgf pexp = do
|
||||
pgf <- peek ppgf
|
||||
exp <- peek pexp
|
||||
let Right (_,t) = inferExpr pgf exp
|
||||
poke ptype t
|
||||
case inferExpr pgf exp of
|
||||
Right (_,t) -> do
|
||||
ptype <- pyType
|
||||
poke ptype t
|
||||
return ptype
|
||||
Left _ -> return nullPtr
|
||||
|
||||
|
||||
foreign export ccall gf_functions :: Ptr PGF -> IO (Ptr ())
|
||||
@@ -247,6 +257,7 @@ gf_functiontype ppgf pcid = do
|
||||
|
||||
|
||||
foreign import ccall "newLang" pyLang :: IO (Ptr Language)
|
||||
foreign import ccall "newPGF" pyPGF :: IO (Ptr PGF)
|
||||
foreign import ccall "newTree" pyTree :: IO (Ptr Tree)
|
||||
foreign import ccall "newgfType" pyType :: IO (Ptr Type)
|
||||
foreign import ccall "newCId" pyCId :: IO (Ptr CId)
|
||||
|
||||
@@ -20,7 +20,7 @@ checkType(void* obj, PyTypeObject* tp)
|
||||
|
||||
/* new types and declarations */
|
||||
|
||||
NEWGF(CId,GF_CId,CIdType,"gf.cid","c identifier")
|
||||
NEWGF(CId,GF_CId,CIdType,"gf.cid","identifier")
|
||||
NEWGF(Lang,GF_Language,LangType,"gf.lang","language")
|
||||
NEWGF(gfType,GF_Type,gfTypeType,"gf.type","gf type")
|
||||
NEWGF(PGF,GF_PGF,PGFType,"gf.pgf","PGF module")
|
||||
@@ -39,14 +39,16 @@ DEALLOCFN(PGF_dealloc, PGF, gf_freePGF, "freePGF")
|
||||
|
||||
static PyObject*
|
||||
pgf_repr(PGF *self) {
|
||||
Lang lang;
|
||||
gf_abstractName(self, &lang);
|
||||
const char* abs = gf_showLanguage(&lang);
|
||||
gf_freeLanguage(&lang);
|
||||
return PyString_FromFormat("<gf.pgf with abstract %s at 0x%x>", abs, self->obj);
|
||||
Lang* lang = gf_abstractName(self);
|
||||
char* abs = gf_showLanguage(lang);
|
||||
// gf_freeLanguage(&lang);
|
||||
Py_DECREF(lang);
|
||||
PyObject* str = PyString_FromFormat("<gf.pgf with abstract %s>", abs);
|
||||
free(abs);
|
||||
return str;
|
||||
}
|
||||
|
||||
static gfType*
|
||||
/* static gfType*
|
||||
startCategory(PGF *self, PyObject *noarg)
|
||||
{
|
||||
gfType *cat;
|
||||
@@ -56,7 +58,7 @@ startCategory(PGF *self, PyObject *noarg)
|
||||
return cat;
|
||||
}
|
||||
|
||||
/* inline static PyObject*
|
||||
inline static PyObject*
|
||||
categories(PGF* self)
|
||||
{
|
||||
return gf_categories(self);
|
||||
@@ -98,22 +100,21 @@ linearize(PGF *self, PyObject *args)
|
||||
{
|
||||
Lang *lang;
|
||||
Tree *tree;
|
||||
if (!checkType(self,&PGFType)) return NULL;
|
||||
if (!PyArg_ParseTuple(args, "OO", &lang, &tree))
|
||||
return NULL;
|
||||
if (!checkType(lang,&LangType)) return NULL;
|
||||
if (!checkType(tree,&TreeType)) return NULL;
|
||||
char* c_lin = gf_linearize(self, lang, tree);
|
||||
return PyString_FromString(c_lin);
|
||||
PyObject* lin = PyString_FromString(c_lin);
|
||||
free(c_lin);
|
||||
return lin;
|
||||
}
|
||||
|
||||
static Lang*
|
||||
abstractName(PGF *self)
|
||||
{
|
||||
Lang* abs_name = (Lang*)LangType.tp_new(&LangType,NULL,NULL);
|
||||
if (!checkType(self,&PGFType)) return NULL;
|
||||
gf_abstractName(self, abs_name);
|
||||
return abs_name;
|
||||
return gf_abstractName(self);
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
@@ -142,19 +143,15 @@ functiontype(PGF *self, PyObject* args)
|
||||
PyErr_Format(PyExc_TypeError, "Must be a gf identifier.");
|
||||
return NULL;
|
||||
}
|
||||
// gftp = (gfType)gfTypeType.tp_new(&gfTypeType,NULL,NULL);
|
||||
return gf_functiontype(self, cid);
|
||||
// return gftp;
|
||||
}
|
||||
|
||||
|
||||
static PyObject*
|
||||
parse(PGF *self, PyObject *args, PyObject *kws)
|
||||
{
|
||||
PyObject *lang, *cat = NULL;
|
||||
//GF_PGF pgf;
|
||||
// GF_Language lang;
|
||||
// GF_Type cat;
|
||||
Lang *lang;
|
||||
gfType *cat = NULL;
|
||||
char *lexed;
|
||||
static char *kwlist[] = {"lexed", "lang", "cat", NULL};
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kws, "sO|O", kwlist,
|
||||
@@ -165,7 +162,7 @@ parse(PGF *self, PyObject *args, PyObject *kws)
|
||||
if (cat) {
|
||||
if (!checkType(cat, &gfTypeType)) return NULL;
|
||||
} else {
|
||||
cat = (PyObject*)startCategory(self,NULL);
|
||||
cat = gf_startCat(self);
|
||||
}
|
||||
return gf_parse(self, lang, cat, lexed);
|
||||
}
|
||||
@@ -175,17 +172,14 @@ readPGF(PyObject *self, PyObject *args)
|
||||
{
|
||||
char *path;
|
||||
struct stat info;
|
||||
PGF *pgf;
|
||||
if (!PyArg_ParseTuple(args, "s", &path))
|
||||
return NULL;
|
||||
if (stat(path, &info) == 0) {
|
||||
pgf = (PGF*)PGFType.tp_new(&PGFType,NULL,NULL);
|
||||
if (!pgf) return NULL;
|
||||
gf_readPGF(pgf, path);
|
||||
return pgf;
|
||||
PGF* pgf = gf_readPGF(path);
|
||||
return pgf;
|
||||
} else {
|
||||
PyErr_Format(PyExc_IOError, "No such file: %s", path);
|
||||
return NULL;
|
||||
PyErr_Format(PyExc_IOError, "No such file: %s", path);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,7 +191,7 @@ static PyMethodDef pgf_methods[] = {
|
||||
{"lang_code", (PyCFunction)languageCode, METH_VARARGS,"Get the language code."},
|
||||
{"print_name", (PyCFunction)printName, METH_VARARGS,"Get the print name for a id."},
|
||||
{"fun_type", (PyCFunction)functiontype, METH_VARARGS,"Get the type of a fun expression."},
|
||||
{"startcat", (PyCFunction)startCategory, METH_NOARGS,"Get the start category."},
|
||||
{"startcat", (PyCFunction)gf_startCat, METH_NOARGS,"Get the start category."},
|
||||
{"categories", (PyCFunction)gf_categories, METH_NOARGS,"Get all categories."},
|
||||
{"functions", (PyCFunction)gf_functions, METH_NOARGS,"Get all functions."},
|
||||
{"abstract", (PyCFunction)abstractName, METH_NOARGS,"Get the module abstract name."},
|
||||
@@ -269,9 +263,8 @@ infer_expr(Expr *self, PyObject* args) {
|
||||
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 (PyObject*)gftp;
|
||||
// gfType* gftp = (gfType*)gfTypeType.tp_new(&gfTypeType,NULL,NULL);
|
||||
return gf_inferexpr(pgf, self);
|
||||
}
|
||||
|
||||
|
||||
@@ -301,7 +294,6 @@ DEALLOCFN(Tree_dealloc, Tree, gf_freeTree, "freeTree")
|
||||
static PyMethodDef gf_methods[] = {
|
||||
{"read_pgf", (PyCFunction)readPGF, METH_VARARGS,"Read pgf file."},
|
||||
{"read_language", (PyCFunction)readLang, METH_VARARGS,"Get the language."},
|
||||
{"startcat", (PyCFunction)startCategory, METH_VARARGS,"Get the start category of a pgf module."},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user