mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
Start work on marshalling in Python bindings
This commit is contained in:
1081
src/runtime/python/expr.c
Normal file
1081
src/runtime/python/expr.c
Normal file
File diff suppressed because it is too large
Load Diff
26
src/runtime/python/expr.h
Normal file
26
src/runtime/python/expr.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef PYPGF_EXPR_H_
|
||||
#define PYPGF_EXPR_H_
|
||||
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#include <Python.h>
|
||||
|
||||
#include <pgf/pgf.h>
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
PyObject* master;
|
||||
// GuPool* pool;
|
||||
// PgfType* type;
|
||||
PgfText *cat;
|
||||
} TypeObject;
|
||||
|
||||
extern PyTypeObject pgf_TypeType;
|
||||
|
||||
// typedef struct {
|
||||
// PyObject_HEAD
|
||||
// PyObject* master;
|
||||
// GuPool* pool;
|
||||
// PgfExpr expr;
|
||||
// } ExprObject;
|
||||
|
||||
#endif // PYPGF_EXPR_H_
|
||||
103
src/runtime/python/marshaller.c
Normal file
103
src/runtime/python/marshaller.c
Normal file
@@ -0,0 +1,103 @@
|
||||
// #define PY_SSIZE_T_CLEAN
|
||||
// #include <Python.h>
|
||||
|
||||
#include <pgf/pgf.h>
|
||||
#include "./expr.h"
|
||||
|
||||
/* The PgfUnmarshaller structure tells the runtime how to create
|
||||
* abstract syntax expressions and types in the heap of the host language.
|
||||
* In Python the expressions are normal objects.
|
||||
* From the point of view of the runtime, each node is a value of type object.
|
||||
* For Python that would be a PyObject pointer.
|
||||
*/
|
||||
|
||||
PgfExpr eabs(PgfUnmarshaller *this, PgfBindType btype, PgfText *name, PgfExpr body)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfExpr eapp(PgfUnmarshaller *this, PgfExpr fun, PgfExpr arg)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfExpr elit(PgfUnmarshaller *this, PgfLiteral lit)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfExpr emeta(PgfUnmarshaller *this, PgfMetaId meta)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfExpr efun(PgfUnmarshaller *this, PgfText *name)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfExpr evar(PgfUnmarshaller *this, int index)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfExpr etyped(PgfUnmarshaller *this, PgfExpr expr, PgfType typ)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfExpr eimplarg(PgfUnmarshaller *this, PgfExpr expr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfLiteral lint(PgfUnmarshaller *this, size_t size, uintmax_t *v)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfLiteral lflt(PgfUnmarshaller *this, double v)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfLiteral lstr(PgfUnmarshaller *this, PgfText *v)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
PgfType dtyp(PgfUnmarshaller *this, int n_hypos, PgfTypeHypo *hypos, PgfText *cat, int n_exprs, PgfExpr *exprs)
|
||||
{
|
||||
PgfText* catname = (PgfText*) malloc(sizeof(PgfText)+cat->size+1);
|
||||
memcpy(catname->text, cat->text, cat->size+1);
|
||||
catname->size = cat->size;
|
||||
|
||||
TypeObject* pytype = (TypeObject*) pgf_TypeType.tp_alloc(&pgf_TypeType, 0);
|
||||
pytype->cat = catname;
|
||||
return (PgfType) pytype;
|
||||
}
|
||||
|
||||
void free_ref(PgfUnmarshaller *this, object x)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
static PgfUnmarshallerVtbl unmarshallervtbl =
|
||||
{
|
||||
eabs,
|
||||
eapp,
|
||||
elit,
|
||||
emeta,
|
||||
efun,
|
||||
evar,
|
||||
etyped,
|
||||
eimplarg,
|
||||
lint,
|
||||
lflt,
|
||||
lstr,
|
||||
dtyp,
|
||||
free_ref
|
||||
};
|
||||
|
||||
/* static */
|
||||
PgfUnmarshaller unmarshaller = { &unmarshallervtbl };
|
||||
11
src/runtime/python/marshaller.h
Normal file
11
src/runtime/python/marshaller.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef PYPGF_MARSHALLER_H_
|
||||
#define PYPGF_MARSHALLER_H_
|
||||
|
||||
// #define PY_SSIZE_T_CLEAN
|
||||
// #include <Python.h>
|
||||
|
||||
#include <pgf/pgf.h>
|
||||
|
||||
extern PgfUnmarshaller unmarshaller;
|
||||
|
||||
#endif // PYPGF_MARSHALLER_H_
|
||||
File diff suppressed because it is too large
Load Diff
@@ -9,8 +9,8 @@ if libraries==['']:
|
||||
libraries=[]
|
||||
|
||||
pgf_module = Extension('pgf',
|
||||
sources = ['pypgf.c'],
|
||||
extra_compile_args = ['-std=c99'],
|
||||
sources = ['pypgf.c', 'marshaller.c', 'expr.c'],
|
||||
extra_compile_args = ['-std=c99', '-Werror', '-Wno-comment'],
|
||||
include_dirs = includes,
|
||||
library_dirs = libraries,
|
||||
libraries = ['pgf'])
|
||||
|
||||
@@ -83,3 +83,32 @@ def test_functionsByCat_2(PGF):
|
||||
|
||||
def test_functionsByCat_non_existant(PGF):
|
||||
assert PGF.functionsByCat("X") == []
|
||||
|
||||
def test_readType_invalid():
|
||||
with pytest.raises(pgf.PGFError):
|
||||
pgf.readType("->")
|
||||
|
||||
def test_readType_equality_1():
|
||||
assert pgf.readType("A") == pgf.readType("A")
|
||||
|
||||
def test_readType_equality_2():
|
||||
assert pgf.readType("A -> B") == pgf.readType("A->B")
|
||||
|
||||
def test_readType_inequality_1():
|
||||
assert pgf.readType("A") != pgf.readType("B")
|
||||
|
||||
def test_readType_inequality_2():
|
||||
assert pgf.readType("A -> B") != pgf.readType("B->B")
|
||||
|
||||
def test_functionType_1(PGF):
|
||||
assert PGF.functionType("z") == pgf.readType("N")
|
||||
|
||||
def test_functionType_2(PGF):
|
||||
assert PGF.functionType("s") == pgf.readType("N->N")
|
||||
|
||||
def test_functionType_3(PGF):
|
||||
assert PGF.functionType("c") == pgf.readType("N -> S")
|
||||
|
||||
def test_startCat(PGF):
|
||||
with pytest.raises(pgf.PGFError):
|
||||
PGF.startCat()
|
||||
|
||||
Reference in New Issue
Block a user