forked from GitHub/gf-core
Refactored py-bindings using Storable.
This commit is contained in:
@@ -4,16 +4,17 @@ cbind=../c-bindings
|
|||||||
pythoninc=/usr/include/python2.5
|
pythoninc=/usr/include/python2.5
|
||||||
debug= #-optc '-DDEBUG=1'
|
debug= #-optc '-DDEBUG=1'
|
||||||
exdir=../../examples/tutorial/embedded
|
exdir=../../examples/tutorial/embedded
|
||||||
conf:
|
|
||||||
ln -s $(cbind)/PGFFFI.hs
|
|
||||||
ln -s $(cbind)/pgf.h
|
|
||||||
build: gf.so
|
build: gf.so
|
||||||
|
|
||||||
gf.so: PGFFFI.hs gfmodule.c Query.pgf
|
gf.so: PyGF.hs gfmodule.c Query.pgf
|
||||||
ghc $(import) --make -fglasgow-exts -O2 -no-hs-main -c $<
|
ghc $(import) --make -fglasgow-exts -O2 -no-hs-main -c $<
|
||||||
ghc -O2 --make -fglasgow-exts -no-hs-main -optl '-shared' \
|
ghc -O2 --make -fglasgow-exts -no-hs-main -optl '-shared' \
|
||||||
-optc '-DMODULE=PGFFFI' $(debug) -optc '-I$(pythoninc)' -o $@ \
|
-optc '-DMODULE=$(basename $<)' $(debug) -optc '-I$(pythoninc)' -o $@ \
|
||||||
$(filter-out %.pgf, $^)
|
$(filter-out %.pgf, $^)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.hi *.o
|
rm -f *.hi *.o
|
||||||
rm -f *_stub.*
|
rm -f *_stub.*
|
||||||
@@ -21,7 +22,7 @@ clean:
|
|||||||
|
|
||||||
superclean:
|
superclean:
|
||||||
make clean
|
make clean
|
||||||
rm -f PGFFFI.hs pgf.h Query.pgf
|
rm -f Query.pgf
|
||||||
rm -f gf.so
|
rm -f gf.so
|
||||||
rm -f mtest
|
rm -f mtest
|
||||||
|
|
||||||
@@ -29,10 +30,4 @@ PyGF.hs: PyGF.hsc
|
|||||||
hsc2hs -I$(pythoninc) $<
|
hsc2hs -I$(pythoninc) $<
|
||||||
|
|
||||||
Query.pgf:
|
Query.pgf:
|
||||||
gf --make $(exdir)/QueryEng.gf $(exdir)/QuerySpa.gf
|
gf --make $(exdir)/QueryEng.gf $(exdir)/QuerySpa.gf
|
||||||
|
|
||||||
mtest: PyGF.hs mtest.c Query.pgf
|
|
||||||
ghc $(import) --make -fglasgow-exts -O2 -no-hs-main -c $<
|
|
||||||
ghc -O2 --make -fglasgow-exts -no-hs-main \
|
|
||||||
-optc '-DMODULE=PyGF' -optc '-I$(pythoninc)' -o $@ \
|
|
||||||
$(filter-out %.pgf, $^)
|
|
||||||
@@ -5,15 +5,17 @@ import PGF
|
|||||||
import Foreign
|
import Foreign
|
||||||
import CString
|
import CString
|
||||||
import Foreign.C.Types
|
import Foreign.C.Types
|
||||||
|
import Control.Monad
|
||||||
|
|
||||||
#include "pygf.h"
|
#include "pygf.h"
|
||||||
|
|
||||||
-- type PyPtr = Ptr Py
|
|
||||||
freeSp :: String -> Ptr a -> IO ()
|
freeSp :: String -> Ptr a -> IO ()
|
||||||
freeSp tname p = do
|
freeSp tname p = do
|
||||||
|
--DEBUG putStrLn $ "about to free pointer " ++ tname ++ " at " ++ (show p)
|
||||||
sp <- (#peek PyGF, sp) p
|
sp <- (#peek PyGF, sp) p
|
||||||
|
--DEBUG putStrLn "peeked"
|
||||||
freeStablePtr sp
|
freeStablePtr sp
|
||||||
putStrLn $ "freeing " ++ tname ++ " at " ++ (show p)
|
--DEBUG putStrLn $ "freeing " ++ tname ++ " at " ++ (show p)
|
||||||
|
|
||||||
instance Storable PGF where
|
instance Storable PGF where
|
||||||
sizeOf _ = (#size PyGF)
|
sizeOf _ = (#size PyGF)
|
||||||
@@ -55,12 +57,31 @@ instance Storable Tree where
|
|||||||
sp <- (#peek PyGF, sp) p
|
sp <- (#peek PyGF, sp) p
|
||||||
deRefStablePtr sp
|
deRefStablePtr sp
|
||||||
|
|
||||||
|
-- It is CId the same as Tree?
|
||||||
|
|
||||||
|
{- instance Storable CId where
|
||||||
|
sizeOf _ = (#size PyGF)
|
||||||
|
alignment _ = alignment (undefined::CInt)
|
||||||
|
poke p o = do
|
||||||
|
sp <- newStablePtr o
|
||||||
|
(#poke PyGF, sp) p sp
|
||||||
|
peek p = do
|
||||||
|
sp <- (#peek PyGF, sp) p
|
||||||
|
deRefStablePtr sp
|
||||||
|
-}
|
||||||
|
|
||||||
foreign export ccall gf_freePGF :: Ptr PGF -> IO ()
|
foreign export ccall gf_freePGF :: Ptr PGF -> IO ()
|
||||||
foreign export ccall gf_freeType :: Ptr Type -> IO ()
|
foreign export ccall gf_freeType :: Ptr Type -> IO ()
|
||||||
foreign export ccall gf_freeLanguage :: Ptr Language -> IO ()
|
foreign export ccall gf_freeLanguage :: Ptr Language -> IO ()
|
||||||
|
foreign export ccall gf_freeTree :: Ptr Tree -> IO ()
|
||||||
|
foreign export ccall gf_freeExpr :: Ptr Expr -> IO ()
|
||||||
|
foreign export ccall gf_freeCId :: Ptr CId -> IO ()
|
||||||
gf_freePGF = freeSp "pgf"
|
gf_freePGF = freeSp "pgf"
|
||||||
gf_freeType = freeSp "type"
|
gf_freeType = freeSp "type"
|
||||||
gf_freeLanguage = freeSp "language"
|
gf_freeLanguage = freeSp "language"
|
||||||
|
gf_freeTree = freeSp "tree"
|
||||||
|
gf_freeExpr = freeSp "expression"
|
||||||
|
gf_freeCId = freeSp "CId"
|
||||||
|
|
||||||
|
|
||||||
{-foreign export ccall gf_printCId :: Ptr CId-> IO CString
|
{-foreign export ccall gf_printCId :: Ptr CId-> IO CString
|
||||||
@@ -68,6 +89,7 @@ gf_printCId p = do
|
|||||||
c <- peek p
|
c <- peek p
|
||||||
newCString (showCId c)
|
newCString (showCId c)
|
||||||
-}
|
-}
|
||||||
|
|
||||||
foreign export ccall gf_readPGF :: Ptr PGF -> CString -> IO ()
|
foreign export ccall gf_readPGF :: Ptr PGF -> CString -> IO ()
|
||||||
gf_readPGF pt path = do
|
gf_readPGF pt path = do
|
||||||
p <- (peekCString path)
|
p <- (peekCString path)
|
||||||
@@ -88,29 +110,37 @@ gf_startCat ppgf pcat= do
|
|||||||
pgf <- peek ppgf
|
pgf <- peek ppgf
|
||||||
poke pcat (startCat pgf)
|
poke pcat (startCat pgf)
|
||||||
|
|
||||||
foreign export ccall gf_parse :: Ptr PGF -> Ptr Language -> Ptr Type -> CString -> IO (Ptr Tree)
|
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
|
||||||
p <- peek ppgf
|
p <- peek ppgf
|
||||||
c <- peek pcat
|
c <- peek pcat
|
||||||
i <- peekCString input
|
i <- peekCString input
|
||||||
l <- peek plang
|
l <- peek plang
|
||||||
let parsed = parse p l c i
|
let parsed = parse p l c i
|
||||||
-- putStrLn $ (show $ length parsed) ++ " parsings"
|
--DEBUG putStrLn $ (show $ length parsed) ++ " parsings"
|
||||||
listToArray $ parsed
|
listToPy pyTree parsed
|
||||||
|
|
||||||
foreign export ccall gf_showExpr :: Ptr Expr -> IO CString
|
foreign export ccall gf_showExpr :: Ptr Expr -> IO CString
|
||||||
gf_showExpr pexpr = do
|
gf_showExpr pexpr = do
|
||||||
e <- peek pexpr
|
e <- peek pexpr
|
||||||
newCString (showExpr [] e)
|
newCString (showExpr [] e)
|
||||||
|
|
||||||
listToArray :: Storable a => [a] -> IO (Ptr a)
|
listToPy :: Storable a => IO (Ptr a) -> [a] -> IO (Ptr ()) -- opaque -- IO (Ptr (Ptr Language))
|
||||||
listToArray list = do
|
listToPy mk ls = do
|
||||||
buf <- mallocBytes $ (#size PyGF) * (length list + 1)
|
let bufl = length ls + 1
|
||||||
sequence $ zipWith (dpoke buf) [0..] list
|
-- buf <- mallocBytes $ (#size PyGF) * bufl
|
||||||
return buf
|
pyls <- pyList
|
||||||
where
|
-- pokeElemOff buf (length ls) nullPtr
|
||||||
dpoke buf n x = do
|
mapM_ (mpoke pyls) ls
|
||||||
pokeElemOff buf n x
|
return pyls
|
||||||
|
where mpoke pyl l = do
|
||||||
|
pl <- mk
|
||||||
|
poke pl l
|
||||||
|
pyl << pl
|
||||||
|
|
||||||
|
|
||||||
|
-- foreign export ccall "gf_freeArray" free :: Ptr a -> IO ()
|
||||||
|
|
||||||
|
|
||||||
foreign export ccall gf_showLanguage :: Ptr Language -> IO CString
|
foreign export ccall gf_showLanguage :: Ptr Language -> IO CString
|
||||||
gf_showLanguage plang = do
|
gf_showLanguage plang = do
|
||||||
@@ -120,4 +150,53 @@ gf_showLanguage plang = do
|
|||||||
foreign export ccall gf_showType :: Ptr Type -> IO CString
|
foreign export ccall gf_showType :: Ptr Type -> IO CString
|
||||||
gf_showType ptp = do
|
gf_showType ptp = do
|
||||||
t <- peek ptp
|
t <- peek ptp
|
||||||
newCString $ showType [] t
|
newCString $ showType [] t
|
||||||
|
|
||||||
|
foreign export ccall gf_showPrintName :: Ptr PGF -> Ptr Language -> Ptr CId -> IO CString
|
||||||
|
gf_showPrintName ppgf plang pcid = do
|
||||||
|
pgf <- peek ppgf
|
||||||
|
lang <- peek plang
|
||||||
|
cid <- peek pcid
|
||||||
|
newCString (showPrintName pgf lang cid)
|
||||||
|
|
||||||
|
foreign export ccall gf_abstractName :: Ptr PGF -> Ptr Language -> IO ()
|
||||||
|
gf_abstractName ppgf pabs = do
|
||||||
|
pgf <- peek ppgf
|
||||||
|
poke pabs $ abstractName pgf
|
||||||
|
|
||||||
|
foreign export ccall gf_linearize :: Ptr PGF -> Ptr Language -> Ptr Tree -> IO CString
|
||||||
|
gf_linearize ppgf plang ptree = do
|
||||||
|
pgf <- peek ppgf
|
||||||
|
lang <- peek plang
|
||||||
|
tree <- peek ptree
|
||||||
|
newCString $ linearize pgf lang tree
|
||||||
|
|
||||||
|
foreign export ccall gf_languageCode :: Ptr PGF -> Ptr Language -> IO CString
|
||||||
|
gf_languageCode ppgf plang = do
|
||||||
|
pgf <- peek ppgf
|
||||||
|
lang <- peek plang
|
||||||
|
case languageCode pgf lang of
|
||||||
|
Just s -> newCString s
|
||||||
|
Nothing -> return nullPtr
|
||||||
|
|
||||||
|
foreign export ccall gf_languages :: Ptr PGF -> IO (Ptr ()) -- (Ptr (Ptr Language))
|
||||||
|
gf_languages ppgf = do
|
||||||
|
pgf <- peek ppgf
|
||||||
|
listToPy pyLang $ languages pgf
|
||||||
|
|
||||||
|
foreign export ccall gf_categories :: Ptr PGF -> IO (Ptr ())
|
||||||
|
gf_categories ppgf = do
|
||||||
|
pgf <- peek ppgf
|
||||||
|
listToPy pyCId $ categories pgf
|
||||||
|
|
||||||
|
foreign export ccall gf_showCId :: Ptr CId -> IO CString
|
||||||
|
gf_showCId pcid = do
|
||||||
|
cid <- peek pcid
|
||||||
|
newCString $ showCId cid
|
||||||
|
|
||||||
|
|
||||||
|
foreign import ccall "newLang" pyLang :: IO (Ptr Language)
|
||||||
|
foreign import ccall "newTree" pyTree :: IO (Ptr Tree)
|
||||||
|
foreign import ccall "newCId" pyCId :: IO (Ptr CId)
|
||||||
|
foreign import ccall "newList" pyList :: IO (Ptr ())
|
||||||
|
foreign import ccall "append" (<<) :: Ptr () -> Ptr a -> IO ()
|
||||||
|
|||||||
@@ -1,63 +1,9 @@
|
|||||||
// GF Python bindings
|
// GF Python bindings
|
||||||
// Jordi Saludes 2010
|
// Jordi Saludes, upc.edu 2010
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
#include "pgf.h"
|
#include "pygf.h"
|
||||||
|
|
||||||
#define NEWOBJECT(OBJ, GFTYPE) typedef struct {\
|
|
||||||
PyObject_HEAD \
|
|
||||||
GFTYPE obj; \
|
|
||||||
} OBJ;
|
|
||||||
#define NEWTYPE(TYPE,NAME,OBJECT,DOC) static PyTypeObject TYPE = {\
|
|
||||||
PyObject_HEAD_INIT(NULL)\
|
|
||||||
0, /*ob_size*/\
|
|
||||||
NAME, /*tp_name*/\
|
|
||||||
sizeof(OBJECT), /*tp_basicsize*/\
|
|
||||||
0, /*tp_itemsize*/\
|
|
||||||
0, /*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, /*tp_hash */\
|
|
||||||
0, /*tp_call*/\
|
|
||||||
0, /*tp_str*/\
|
|
||||||
0, /*tp_getattro*/\
|
|
||||||
0, /*tp_setattro*/\
|
|
||||||
0, /*tp_as_buffer*/\
|
|
||||||
Py_TPFLAGS_DEFAULT, /*tp_flags*/\
|
|
||||||
DOC, /* tp_doc */\
|
|
||||||
};
|
|
||||||
#define NEWGF(OBJ,GFTYPE,TYPE,NAME,DOC) NEWOBJECT(OBJ,GFTYPE) \
|
|
||||||
NEWTYPE(TYPE,NAME,OBJ,DOC)
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
#define DEALLOCFN(delname,t,cb,cbname) static void \
|
|
||||||
delname(t *self){ cb(self->obj);\
|
|
||||||
printf("gf_%s has been called for stable pointer 0x%x\n", cbname, self->obj);\
|
|
||||||
self->ob_type->tp_free((PyObject*)self); }
|
|
||||||
#else
|
|
||||||
#define DEALLOCFN(delname,t,cb,cbname) static void \
|
|
||||||
delname(t *self){ cb(self->obj);\
|
|
||||||
self->ob_type->tp_free((PyObject*)self); }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
#define REPRCB(cbid,t,gfcb) static PyObject* \
|
|
||||||
cbid(t *self) { \
|
|
||||||
const char *str = gfcb(self->obj); \
|
|
||||||
return PyString_FromFormat("0x%x: %s", self->obj, str); }
|
|
||||||
#else
|
|
||||||
#define REPRCB(cbid,t,gfcb) static PyObject* \
|
|
||||||
cbid(t *self) { \
|
|
||||||
const char *str = gfcb(self->obj); \
|
|
||||||
return PyString_FromString(str); }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* utilities */
|
/* utilities */
|
||||||
|
|
||||||
@@ -85,14 +31,14 @@ NEWGF(Tree,GF_Tree,TreeType,"gf.tree","gf tree")
|
|||||||
|
|
||||||
DEALLOCFN(CId_dealloc, CId, gf_freeCId, "freeCId")
|
DEALLOCFN(CId_dealloc, CId, gf_freeCId, "freeCId")
|
||||||
|
|
||||||
static PyObject*
|
/* static PyObject*
|
||||||
CId_repr(CId *self)
|
CId_repr(CId *self)
|
||||||
{
|
{
|
||||||
char* str_cid = gf_showCId(self->obj);
|
char* str_cid = gf_showCId(self->obj);
|
||||||
PyObject* repr = PyString_FromString(str_cid);
|
PyObject* repr = PyString_FromString(str_cid);
|
||||||
free(str_cid);
|
free(str_cid);
|
||||||
return repr;
|
return repr;
|
||||||
}
|
} */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -101,11 +47,13 @@ CId_repr(CId *self)
|
|||||||
|
|
||||||
DEALLOCFN(PGF_dealloc, PGFModule, gf_freePGF, "freePGF")
|
DEALLOCFN(PGF_dealloc, PGFModule, gf_freePGF, "freePGF")
|
||||||
|
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
pgf_repr(PGFModule *self) {
|
pgf_repr(PGFModule *self) {
|
||||||
GF_Language lang = gf_abstractName(self->obj);
|
Lang lang;
|
||||||
const char* abs = gf_showLanguage(lang);
|
gf_abstractName(self, &lang);
|
||||||
gf_freeLanguage(lang);
|
const char* abs = gf_showLanguage(&lang);
|
||||||
|
gf_freeLanguage(&lang);
|
||||||
return PyString_FromFormat("<gf.pgf with abstract %s at 0x%x>", abs, self->obj);
|
return PyString_FromFormat("<gf.pgf with abstract %s at 0x%x>", abs, self->obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,36 +65,40 @@ startCategory(PyObject *self, PyObject *noarg)
|
|||||||
gfType *cat;
|
gfType *cat;
|
||||||
if (!checkType(self, &PGFType)) return NULL;
|
if (!checkType(self, &PGFType)) return NULL;
|
||||||
cat = (gfType*)gfTypeType.tp_new(&gfTypeType,NULL,NULL);
|
cat = (gfType*)gfTypeType.tp_new(&gfTypeType,NULL,NULL);
|
||||||
cat->obj = gf_startCat(((PGFModule*)self)->obj);
|
gf_startCat((PGFModule*)self, cat);
|
||||||
return cat;
|
return cat;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject*
|
inline static PyObject*
|
||||||
categories(PGFModule* self)
|
categories(PGFModule* self)
|
||||||
{
|
{
|
||||||
PyObject* cats = PyList_New(0);
|
/* PyObject* cats = PyList_New(0);
|
||||||
GF_CId *p = gf_categories(self->obj);
|
GF_CId *p = gf_categories(self);
|
||||||
while (*p) {
|
while (*p) {
|
||||||
CId* c = (CId*)CIdType.tp_new(&CIdType,NULL,NULL);
|
CId* c = (CId*)CIdType.tp_new(&CIdType,NULL,NULL);
|
||||||
c->obj = *(p++);
|
c->obj = *(p++);
|
||||||
PyList_Append(cats, (PyObject*)c);
|
PyList_Append(cats, (PyObject*)c);
|
||||||
Py_DECREF(c); //?
|
Py_DECREF(c); //?
|
||||||
}
|
}
|
||||||
return cats;
|
return cats; */
|
||||||
|
return gf_categories(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject*
|
inline static PyObject*
|
||||||
languages(PGFModule* self)
|
languages(PGFModule* self)
|
||||||
{
|
{
|
||||||
PyObject *langs = PyList_New(0);
|
/* PyObject *langs = PyList_New(0);
|
||||||
GF_Language *p = gf_languages(self->obj);
|
PyGF **p = gf_languages(self);
|
||||||
|
// PyGF *q = p;
|
||||||
while (*p) {
|
while (*p) {
|
||||||
Lang* l = (Lang*)LangType.tp_new(&LangType,NULL,NULL);
|
printf("sp: %x\n", (*p)->sp);
|
||||||
l->obj = *(p++);
|
//Lang* l = (Lang*)LangType.tp_new(&LangType,NULL,NULL);
|
||||||
PyList_Append(langs, (PyObject*)l);
|
//l->obj = (p++)->sp;
|
||||||
Py_DECREF(l); //??
|
PyList_Append(langs, (PyObject*)(*(p++)));
|
||||||
}
|
// Py_DECREF(*(p++)); //??
|
||||||
return langs;
|
}
|
||||||
|
// gf_freeArray(q); */
|
||||||
|
return gf_languages(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
@@ -155,7 +107,7 @@ languageCode(PGFModule *self, PyObject *args)
|
|||||||
Lang *lang;
|
Lang *lang;
|
||||||
if (!PyArg_ParseTuple(args, "O", &lang))
|
if (!PyArg_ParseTuple(args, "O", &lang))
|
||||||
return NULL;
|
return NULL;
|
||||||
char* scode = gf_languageCode(self->obj, lang->obj);
|
char* scode = gf_languageCode(self, lang);
|
||||||
if (scode) {
|
if (scode) {
|
||||||
PyObject* result = PyString_FromString(scode);
|
PyObject* result = PyString_FromString(scode);
|
||||||
free(scode);
|
free(scode);
|
||||||
@@ -176,17 +128,17 @@ linearize(PGFModule *self, PyObject *args)
|
|||||||
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->obj, lang->obj, tree->obj);
|
char* c_lin = gf_linearize(self, lang, tree);
|
||||||
return PyString_FromString(c_lin);
|
return PyString_FromString(c_lin);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Lang*
|
static Lang*
|
||||||
abstractName(PGFModule* self)
|
abstractName(PGFModule* self)
|
||||||
{
|
{
|
||||||
Lang* abs = (Lang*)LangType.tp_new(&LangType,NULL,NULL);
|
Lang* abs_name = (Lang*)LangType.tp_new(&LangType,NULL,NULL);
|
||||||
if (!checkType(self,&PGFType)) return NULL;
|
if (!checkType(self,&PGFType)) return NULL;
|
||||||
abs->obj = gf_abstractName(self->obj);
|
gf_abstractName(self, abs_name);
|
||||||
return abs;
|
return abs_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -197,7 +149,7 @@ printName(PGFModule *self, PyObject *args)
|
|||||||
CId* id;
|
CId* id;
|
||||||
if (!PyArg_ParseTuple(args, "OO", &lang, &id))
|
if (!PyArg_ParseTuple(args, "OO", &lang, &id))
|
||||||
return NULL;
|
return NULL;
|
||||||
char *pname = gf_showPrintName(self->obj, lang->obj, id->obj);
|
char *pname = gf_showPrintName(self, lang, id);
|
||||||
PyObject* result = PyString_FromString(pname);
|
PyObject* result = PyString_FromString(pname);
|
||||||
free(pname);
|
free(pname);
|
||||||
return result;
|
return result;
|
||||||
@@ -207,24 +159,23 @@ printName(PGFModule *self, PyObject *args)
|
|||||||
static PyObject*
|
static PyObject*
|
||||||
parse(PyObject *self, PyObject *args, PyObject *kws)
|
parse(PyObject *self, PyObject *args, PyObject *kws)
|
||||||
{
|
{
|
||||||
PyObject *lang_pyob, *cat_pyob = NULL;
|
PyObject *lang, *cat = NULL;
|
||||||
GF_PGF pgf;
|
//GF_PGF pgf;
|
||||||
GF_Language lang;
|
// GF_Language lang;
|
||||||
GF_Type cat;
|
// 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,
|
||||||
&lexed, &lang_pyob, &cat_pyob))
|
&lexed, &lang, &cat))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!checkType(self, &PGFType)) return NULL;
|
if (!checkType(self, &PGFType)) return NULL;
|
||||||
if (!checkType(lang_pyob, &LangType)) return NULL;
|
if (!checkType(lang, &LangType)) return NULL;
|
||||||
if (cat_pyob) {
|
if (cat) {
|
||||||
if (!checkType(cat_pyob, &gfTypeType)) return NULL;
|
if (!checkType(cat, &gfTypeType)) return NULL;
|
||||||
cat = ((gfType*)cat_pyob)->obj;
|
|
||||||
} else {
|
} else {
|
||||||
cat = startCategory(self,NULL)->obj;
|
cat = (PyObject*)startCategory(self,NULL);
|
||||||
}
|
}
|
||||||
pgf = ((PGFModule*)self)->obj;
|
/* pgf = ((PGFModule*)self)->obj;
|
||||||
lang = ((Lang*)lang_pyob)->obj;
|
lang = ((Lang*)lang_pyob)->obj;
|
||||||
PyObject *parsed = PyList_New(0);
|
PyObject *parsed = PyList_New(0);
|
||||||
GF_Tree *p = gf_parse(pgf,lang,cat,lexed);
|
GF_Tree *p = gf_parse(pgf,lang,cat,lexed);
|
||||||
@@ -235,12 +186,13 @@ parse(PyObject *self, PyObject *args, PyObject *kws)
|
|||||||
expr->obj = *(p++);
|
expr->obj = *(p++);
|
||||||
PyList_Append(parsed, (PyObject*)expr);
|
PyList_Append(parsed, (PyObject*)expr);
|
||||||
Py_DECREF(expr); //??
|
Py_DECREF(expr); //??
|
||||||
/* char *str = gf_showExpr(exp);
|
// char *str = gf_showExpr(exp);
|
||||||
puts(str);
|
//puts(str);
|
||||||
free(str); */
|
//free(str);
|
||||||
} while (*p);
|
} while (*p);
|
||||||
}
|
}
|
||||||
return parsed;
|
return parsed; */
|
||||||
|
return gf_parse(self, lang, cat, lexed);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PGFModule*
|
static PGFModule*
|
||||||
@@ -252,7 +204,7 @@ readPGF(PyObject *self, PyObject *args)
|
|||||||
return NULL;
|
return NULL;
|
||||||
pgf = (PGFModule*)PGFType.tp_new(&PGFType,NULL,NULL);
|
pgf = (PGFModule*)PGFType.tp_new(&PGFType,NULL,NULL);
|
||||||
if (!pgf) return NULL;
|
if (!pgf) return NULL;
|
||||||
pgf->obj = gf_readPGF(path);
|
gf_readPGF(pgf, path);
|
||||||
return pgf;
|
return pgf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,7 +222,7 @@ static PyMethodDef pgf_methods[] = {
|
|||||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
REPRCB(CId_repr, CId, gf_showCId)
|
||||||
|
|
||||||
|
|
||||||
/* Language methods, constructor and destructor */
|
/* Language methods, constructor and destructor */
|
||||||
@@ -287,7 +239,7 @@ readLang(PyObject *self, PyObject *args)
|
|||||||
return NULL;
|
return NULL;
|
||||||
l = (Lang*)LangType.tp_new(&LangType,NULL,NULL);
|
l = (Lang*)LangType.tp_new(&LangType,NULL,NULL);
|
||||||
if(!l) return NULL;
|
if(!l) return NULL;
|
||||||
l->obj = gf_readLanguage(langName);
|
gf_readLanguage(l,langName);
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,7 +300,7 @@ initgf(void)
|
|||||||
static char *argv[] = {"gf.so", 0}, **argv_ = argv;
|
static char *argv[] = {"gf.so", 0}, **argv_ = argv;
|
||||||
static int argc = 1;
|
static int argc = 1;
|
||||||
gf_init (&argc, &argv_);
|
gf_init (&argc, &argv_);
|
||||||
hs_add_root (__stginit_PGFFFI);
|
hs_add_root (__stginit_PyGF);
|
||||||
|
|
||||||
#define ADDTYPE(t) Py_INCREF(&t);\
|
#define ADDTYPE(t) Py_INCREF(&t);\
|
||||||
PyModule_AddObject(m, "gf", (PyObject *)&t);
|
PyModule_AddObject(m, "gf", (PyObject *)&t);
|
||||||
@@ -359,3 +311,22 @@ PyModule_AddObject(m, "gf", (PyObject *)&t);
|
|||||||
ADDTYPE(TreeType)
|
ADDTYPE(TreeType)
|
||||||
ADDTYPE(ExprType)
|
ADDTYPE(ExprType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline Lang* newLang() {
|
||||||
|
return (Lang*)LangType.tp_new(&LangType,NULL,NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Tree* newTree() {
|
||||||
|
return (Tree*)TreeType.tp_new(&TreeType,NULL,NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline CId* newCId() {
|
||||||
|
return (CId*)CIdType.tp_new(&CIdType,NULL,NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline PyObject* newList() { return PyList_New(0); }
|
||||||
|
|
||||||
|
void append(PyObject* l, PyObject* ob) {
|
||||||
|
PyList_Append(l, ob);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
/* GF C Bindings
|
|
||||||
Copyright (C) 2008-2009 Kevin Kofler
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "pygf.h"
|
|
||||||
//#include "gf_lexing.h"
|
|
||||||
|
|
||||||
PyGF pgf, lang, cat;
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int has_lang;
|
|
||||||
gf_init(&argc, &argv);
|
|
||||||
printf("Passed init\n");
|
|
||||||
gf_readPGF(&pgf, "Query.pgf");
|
|
||||||
printf("passed readPGF\n");
|
|
||||||
if (!gf_readLanguage(&lang, "QueryEng"))
|
|
||||||
return 1;
|
|
||||||
printf("passed readLanguage: %s\n", gf_showLanguage(&lang));
|
|
||||||
gf_startCat(&pgf, &cat);
|
|
||||||
printf("passed startcat: %s\n", gf_showType(&cat));
|
|
||||||
// char *lexed = gf_stringOp("lextext")("Is 2 prime");
|
|
||||||
char lexed[] = "is 23 odd";
|
|
||||||
PyGF *result = gf_parse(&pgf, &lang, &cat, &lexed);
|
|
||||||
int k;
|
|
||||||
for (k = 0; result[k].sp ; k++) {
|
|
||||||
char *str = gf_showExpr(&result[k]);
|
|
||||||
puts(str);
|
|
||||||
free(str);
|
|
||||||
printf("next: 0x%x\n",result[k+1].sp);
|
|
||||||
}
|
|
||||||
/* // free(lexed);
|
|
||||||
PyGF *p = result;
|
|
||||||
if (result[0]) {
|
|
||||||
int k = 0;
|
|
||||||
do {
|
|
||||||
printf("tree %d\n",++k);
|
|
||||||
char *str = gf_showExpr(*(p++));
|
|
||||||
puts(str);
|
|
||||||
free(str);
|
|
||||||
} while (*p && k < 5);
|
|
||||||
} else
|
|
||||||
puts("no match");
|
|
||||||
gf_freeTrees(result); */
|
|
||||||
gf_freeType(&cat);
|
|
||||||
gf_freeLanguage(&lang);
|
|
||||||
gf_freePGF(&pgf);
|
|
||||||
gf_exit();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -32,6 +32,7 @@ typedef struct {
|
|||||||
HsStablePtr sp;
|
HsStablePtr sp;
|
||||||
} PyGF;
|
} PyGF;
|
||||||
|
|
||||||
|
|
||||||
#define NEWOBJECT(OBJ, GFTYPE) typedef struct {\
|
#define NEWOBJECT(OBJ, GFTYPE) typedef struct {\
|
||||||
PyObject_HEAD \
|
PyObject_HEAD \
|
||||||
GFTYPE obj; \
|
GFTYPE obj; \
|
||||||
@@ -65,4 +66,27 @@ typedef struct {
|
|||||||
NEWTYPE(TYPE,NAME,OBJ,DOC)
|
NEWTYPE(TYPE,NAME,OBJ,DOC)
|
||||||
|
|
||||||
|
|
||||||
NEWOBJECT(CID, GF_CId)
|
// NEWOBJECT(CID, GF_CId)
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
#define DEALLOCFN(delname,t,cb,cbname) static void \
|
||||||
|
delname(t *self){ cb(self);\
|
||||||
|
printf("gf_%s has been called for stable pointer 0x%x\n", cbname, self->obj);\
|
||||||
|
self->ob_type->tp_free((PyObject*)self); }
|
||||||
|
#else
|
||||||
|
#define DEALLOCFN(delname,t,cb,cbname) static void \
|
||||||
|
delname(t *self){ cb(self);\
|
||||||
|
self->ob_type->tp_free((PyObject*)self); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
#define REPRCB(cbid,t,gfcb) static PyObject* \
|
||||||
|
cbid(t *self) { \
|
||||||
|
const char *str = gfcb(self); \
|
||||||
|
return PyString_FromFormat("0x%x: %s", self->obj, str); }
|
||||||
|
#else
|
||||||
|
#define REPRCB(cbid,t,gfcb) static PyObject* \
|
||||||
|
cbid(t *self) { \
|
||||||
|
const char *str = gfcb(self); \
|
||||||
|
return PyString_FromString(str); }
|
||||||
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user