Changed calling style in py-bindings.

This commit is contained in:
jordi.saludes
2010-07-28 14:15:06 +00:00
parent dcac7254f3
commit c3e35728eb
2 changed files with 49 additions and 46 deletions

View File

@@ -1,6 +1,7 @@
{-# LANGUAGE ForeignFunctionInterface #-} {-# LANGUAGE ForeignFunctionInterface #-}
-- GF Python bindings -- Jordi Saludes, upc.edu 2010 -- GF Python bindings -- Jordi Saludes, upc.edu 2010
-- Jordi Saludes, upc.edu 2010
--
module PyGF where module PyGF where
import PGF import PGF
@@ -92,11 +93,12 @@ gf_printCId p = do
newCString (showCId c) newCString (showCId c)
-} -}
foreign export ccall gf_readPGF :: Ptr PGF -> CString -> IO () foreign export ccall gf_readPGF :: CString -> IO (Ptr PGF)
gf_readPGF pt path = do gf_readPGF path = do
p <- (peekCString path) ppgf <- pyPGF
result <- (readPGF p) p <- peekCString path
poke pt result readPGF p >>= poke ppgf
return ppgf
foreign export ccall gf_readLanguage :: Ptr Language -> CString -> IO Bool foreign export ccall gf_readLanguage :: Ptr Language -> CString -> IO Bool
gf_readLanguage pt str = do gf_readLanguage pt str = do
@@ -107,10 +109,12 @@ gf_readLanguage pt str = do
return True return True
Nothing -> return False Nothing -> return False
foreign export ccall gf_startCat :: Ptr PGF -> Ptr Type -> IO () foreign export ccall gf_startCat :: Ptr PGF -> IO (Ptr Type)
gf_startCat ppgf pcat= do gf_startCat ppgf = do
pgf <- peek ppgf pgf <- peek ppgf
pcat <- pyType
poke pcat (startCat pgf) poke pcat (startCat pgf)
return pcat
foreign export ccall gf_parse :: Ptr PGF -> Ptr Language -> Ptr Type -> CString -> IO (Ptr ()) foreign export ccall gf_parse :: Ptr PGF -> Ptr Language -> Ptr Type -> CString -> IO (Ptr ())
gf_parse ppgf plang pcat input = do gf_parse ppgf plang pcat input = do
@@ -157,10 +161,12 @@ gf_showPrintName ppgf plang pcid = do
cid <- peek pcid cid <- peek pcid
newCString (showPrintName pgf lang cid) newCString (showPrintName pgf lang cid)
foreign export ccall gf_abstractName :: Ptr PGF -> Ptr Language -> IO () foreign export ccall gf_abstractName :: Ptr PGF -> IO (Ptr Language)
gf_abstractName ppgf pabs = do gf_abstractName ppgf = do
pabs <- pyLang
pgf <- peek ppgf pgf <- peek ppgf
poke pabs $ abstractName pgf poke pabs $ abstractName pgf
return pabs
foreign export ccall gf_linearize :: Ptr PGF -> Ptr Language -> Ptr Tree -> IO CString foreign export ccall gf_linearize :: Ptr PGF -> Ptr Language -> Ptr Tree -> IO CString
gf_linearize ppgf plang ptree = do gf_linearize ppgf plang ptree = do
@@ -221,12 +227,16 @@ 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 () foreign export ccall gf_inferexpr :: Ptr PGF -> Ptr Expr -> IO (Ptr Type)
gf_inferexpr ppgf pexp ptype = do gf_inferexpr ppgf pexp = do
pgf <- peek ppgf pgf <- peek ppgf
exp <- peek pexp exp <- peek pexp
let Right (_,t) = inferExpr pgf exp case inferExpr pgf exp of
poke ptype t Right (_,t) -> do
ptype <- pyType
poke ptype t
return ptype
Left _ -> return nullPtr
foreign export ccall gf_functions :: Ptr PGF -> IO (Ptr ()) 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 "newLang" pyLang :: IO (Ptr Language)
foreign import ccall "newPGF" pyPGF :: IO (Ptr PGF)
foreign import ccall "newTree" pyTree :: IO (Ptr Tree) foreign import ccall "newTree" pyTree :: IO (Ptr Tree)
foreign import ccall "newgfType" pyType :: IO (Ptr Type) foreign import ccall "newgfType" pyType :: IO (Ptr Type)
foreign import ccall "newCId" pyCId :: IO (Ptr CId) foreign import ccall "newCId" pyCId :: IO (Ptr CId)

View File

@@ -20,7 +20,7 @@ checkType(void* obj, PyTypeObject* tp)
/* new types and declarations */ /* 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(Lang,GF_Language,LangType,"gf.lang","language")
NEWGF(gfType,GF_Type,gfTypeType,"gf.type","gf type") NEWGF(gfType,GF_Type,gfTypeType,"gf.type","gf type")
NEWGF(PGF,GF_PGF,PGFType,"gf.pgf","PGF module") NEWGF(PGF,GF_PGF,PGFType,"gf.pgf","PGF module")
@@ -39,14 +39,16 @@ DEALLOCFN(PGF_dealloc, PGF, gf_freePGF, "freePGF")
static PyObject* static PyObject*
pgf_repr(PGF *self) { pgf_repr(PGF *self) {
Lang lang; Lang* lang = gf_abstractName(self);
gf_abstractName(self, &lang); char* abs = gf_showLanguage(lang);
const char* abs = gf_showLanguage(&lang); // gf_freeLanguage(&lang);
gf_freeLanguage(&lang); Py_DECREF(lang);
return PyString_FromFormat("<gf.pgf with abstract %s at 0x%x>", abs, self->obj); PyObject* str = PyString_FromFormat("<gf.pgf with abstract %s>", abs);
free(abs);
return str;
} }
static gfType* /* static gfType*
startCategory(PGF *self, PyObject *noarg) startCategory(PGF *self, PyObject *noarg)
{ {
gfType *cat; gfType *cat;
@@ -56,7 +58,7 @@ startCategory(PGF *self, PyObject *noarg)
return cat; return cat;
} }
/* inline static PyObject* inline static PyObject*
categories(PGF* self) categories(PGF* self)
{ {
return gf_categories(self); return gf_categories(self);
@@ -98,22 +100,21 @@ linearize(PGF *self, PyObject *args)
{ {
Lang *lang; Lang *lang;
Tree *tree; Tree *tree;
if (!checkType(self,&PGFType)) return NULL;
if (!PyArg_ParseTuple(args, "OO", &lang, &tree)) if (!PyArg_ParseTuple(args, "OO", &lang, &tree))
return NULL; return NULL;
if (!checkType(lang,&LangType)) return NULL; if (!checkType(lang,&LangType)) return NULL;
if (!checkType(tree,&TreeType)) return NULL; if (!checkType(tree,&TreeType)) return NULL;
char* c_lin = gf_linearize(self, lang, tree); 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* static Lang*
abstractName(PGF *self) abstractName(PGF *self)
{ {
Lang* abs_name = (Lang*)LangType.tp_new(&LangType,NULL,NULL);
if (!checkType(self,&PGFType)) return NULL; if (!checkType(self,&PGFType)) return NULL;
gf_abstractName(self, abs_name); return gf_abstractName(self);
return abs_name;
} }
static PyObject* static PyObject*
@@ -142,19 +143,15 @@ functiontype(PGF *self, PyObject* args)
PyErr_Format(PyExc_TypeError, "Must be a gf identifier."); PyErr_Format(PyExc_TypeError, "Must be a gf identifier.");
return NULL; return NULL;
} }
// gftp = (gfType)gfTypeType.tp_new(&gfTypeType,NULL,NULL);
return gf_functiontype(self, cid); return gf_functiontype(self, cid);
// return gftp;
} }
static PyObject* static PyObject*
parse(PGF *self, PyObject *args, PyObject *kws) parse(PGF *self, PyObject *args, PyObject *kws)
{ {
PyObject *lang, *cat = NULL; Lang *lang;
//GF_PGF pgf; gfType *cat = NULL;
// GF_Language lang;
// GF_Type cat;
char *lexed; char *lexed;
static char *kwlist[] = {"lexed", "lang", "cat", NULL}; static char *kwlist[] = {"lexed", "lang", "cat", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kws, "sO|O", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kws, "sO|O", kwlist,
@@ -165,7 +162,7 @@ parse(PGF *self, PyObject *args, PyObject *kws)
if (cat) { if (cat) {
if (!checkType(cat, &gfTypeType)) return NULL; if (!checkType(cat, &gfTypeType)) return NULL;
} else { } else {
cat = (PyObject*)startCategory(self,NULL); cat = gf_startCat(self);
} }
return gf_parse(self, lang, cat, lexed); return gf_parse(self, lang, cat, lexed);
} }
@@ -175,17 +172,14 @@ readPGF(PyObject *self, PyObject *args)
{ {
char *path; char *path;
struct stat info; struct stat info;
PGF *pgf;
if (!PyArg_ParseTuple(args, "s", &path)) if (!PyArg_ParseTuple(args, "s", &path))
return NULL; return NULL;
if (stat(path, &info) == 0) { if (stat(path, &info) == 0) {
pgf = (PGF*)PGFType.tp_new(&PGFType,NULL,NULL); PGF* pgf = gf_readPGF(path);
if (!pgf) return NULL; return pgf;
gf_readPGF(pgf, path);
return pgf;
} else { } else {
PyErr_Format(PyExc_IOError, "No such file: %s", path); PyErr_Format(PyExc_IOError, "No such file: %s", path);
return NULL; return NULL;
} }
} }
@@ -197,7 +191,7 @@ static PyMethodDef pgf_methods[] = {
{"lang_code", (PyCFunction)languageCode, METH_VARARGS,"Get the language code."}, {"lang_code", (PyCFunction)languageCode, METH_VARARGS,"Get the language code."},
{"print_name", (PyCFunction)printName, METH_VARARGS,"Get the print name for a id."}, {"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."}, {"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."}, {"categories", (PyCFunction)gf_categories, METH_NOARGS,"Get all categories."},
{"functions", (PyCFunction)gf_functions, METH_NOARGS,"Get all functions."}, {"functions", (PyCFunction)gf_functions, METH_NOARGS,"Get all functions."},
{"abstract", (PyCFunction)abstractName, METH_NOARGS,"Get the module abstract name."}, {"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."); PyErr_Format(PyExc_ValueError, "Must be a pgf module.");
return NULL; return NULL;
} }
gfType* gftp = (gfType*)gfTypeType.tp_new(&gfTypeType,NULL,NULL); // gfType* gftp = (gfType*)gfTypeType.tp_new(&gfTypeType,NULL,NULL);
gf_inferexpr(pgf, self, gftp); return gf_inferexpr(pgf, self);
return (PyObject*)gftp;
} }
@@ -301,7 +294,6 @@ DEALLOCFN(Tree_dealloc, Tree, gf_freeTree, "freeTree")
static PyMethodDef gf_methods[] = { static PyMethodDef gf_methods[] = {
{"read_pgf", (PyCFunction)readPGF, METH_VARARGS,"Read pgf file."}, {"read_pgf", (PyCFunction)readPGF, METH_VARARGS,"Read pgf file."},
{"read_language", (PyCFunction)readLang, METH_VARARGS,"Get the language."}, {"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 */ {NULL, NULL, 0, NULL} /* Sentinel */
}; };