Merge branch 'majestic' of github.com:GrammaticalFramework/gf-core into majestic

This commit is contained in:
krangelov
2021-09-03 20:02:22 +02:00
7 changed files with 1370 additions and 1211 deletions

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
View 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_

View File

@@ -0,0 +1,106 @@
// #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)
{
PyObject *i = PyLong_FromUnsignedLong(*v);
return (PgfLiteral) i;
}
PgfLiteral lflt(PgfUnmarshaller *this, double v)
{
PyObject *d = PyFloat_FromDouble(v);
return (PgfLiteral) d;
}
PgfLiteral lstr(PgfUnmarshaller *this, PgfText *v)
{
PyObject *s = PyUnicode_FromStringAndSize(v->text, v->size);
return (PgfLiteral) s;
}
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)
{
Py_XDECREF(x);
}
static PgfUnmarshallerVtbl unmarshallervtbl =
{
eabs,
eapp,
elit,
emeta,
efun,
evar,
etyped,
eimplarg,
lint,
lflt,
lstr,
dtyp,
free_ref
};
/* static */
PgfUnmarshaller unmarshaller = { &unmarshallervtbl };

View 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

View File

@@ -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'])

View File

@@ -15,7 +15,7 @@ def test_readPGF_GF():
with pytest.raises(pgf.PGFError):
pgf.readPGF("../haskell/tests/basic.gf")
@pytest.mark.skip(reason="Bug in runtime")
@pytest.mark.skip(reason="Unhandled case in runtime")
def test_readPGF_NGF(NGF):
with pytest.raises(pgf.PGFError):
pgf.readPGF("./basic.ngf")
@@ -34,7 +34,7 @@ def test_bootNGF_GF():
with pytest.raises(pgf.PGFError):
pgf.bootNGF("../haskell/tests/basic.gf", "./abc.ngf")
@pytest.mark.skip(reason="Bug in runtime")
@pytest.mark.skip(reason="Unhandled case in runtime")
def test_bootNGF_NGF(NGF):
with pytest.raises(pgf.PGFError):
pgf.bootNGF("./basic.ngf", "./abc.ngf")
@@ -45,17 +45,17 @@ def test_bootNGF_existing(NGF):
# readNGF
@pytest.mark.skip(reason="Bug in runtime")
@pytest.mark.skip(reason="Unhandled case in runtime")
def test_readNGF_non_existant():
with pytest.raises(FileNotFoundError):
pgf.readNGF("./abc.ngf")
@pytest.mark.skip(reason="Bug in runtime")
@pytest.mark.skip(reason="Unhandled case in runtime")
def test_readNGF_GF():
with pytest.raises(pgf.PGFError):
pgf.readNGF("../haskell/tests/basic.gf")
@pytest.mark.skip(reason="Bug in runtime")
@pytest.mark.skip(reason="Unhandled case in runtime")
def test_readNGF_PGF():
with pytest.raises(pgf.PGFError):
pgf.readNGF("../haskell/tests/basic.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()