mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 11:42:49 -06:00
Add showExpr and tests for it using variable expressions
This commit is contained in:
@@ -154,7 +154,7 @@ PgfUnmarshaller unmarshaller = { &unmarshallerVtbl };
|
|||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
static PgfText *
|
PgfText *
|
||||||
PyUnicode_AsPgfText(PyObject *pystr)
|
PyUnicode_AsPgfText(PyObject *pystr)
|
||||||
{
|
{
|
||||||
if (!PyUnicode_Check(pystr)) {
|
if (!PyUnicode_Check(pystr)) {
|
||||||
@@ -166,7 +166,7 @@ PyUnicode_AsPgfText(PyObject *pystr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Py_ssize_t size;
|
Py_ssize_t size;
|
||||||
const char * enc = PyUnicode_AsUTF8AndSize(pystr, &size);
|
const char *enc = PyUnicode_AsUTF8AndSize(pystr, &size);
|
||||||
PgfText *ptext = malloc(sizeof(PgfText)+size+1);
|
PgfText *ptext = malloc(sizeof(PgfText)+size+1);
|
||||||
memcpy(ptext->text, enc, size+1);
|
memcpy(ptext->text, enc, size+1);
|
||||||
ptext->size = size;
|
ptext->size = size;
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#include <pgf/pgf.h>
|
#include <pgf/pgf.h>
|
||||||
|
|
||||||
|
PgfText *PyUnicode_AsPgfText(PyObject *pystr);
|
||||||
|
|
||||||
extern PgfUnmarshaller unmarshaller;
|
extern PgfUnmarshaller unmarshaller;
|
||||||
|
|
||||||
extern PgfMarshaller marshaller;
|
extern PgfMarshaller marshaller;
|
||||||
|
|||||||
@@ -2541,7 +2541,7 @@ const char *fpath;
|
|||||||
return py_pgf;
|
return py_pgf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ExprObject*
|
static ExprObject *
|
||||||
pgf_readExpr(PyObject *self, PyObject *args)
|
pgf_readExpr(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
const char *s;
|
const char *s;
|
||||||
@@ -2563,6 +2563,36 @@ pgf_readExpr(PyObject *self, PyObject *args)
|
|||||||
return (ExprObject *)expr;
|
return (ExprObject *)expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
pgf_showExpr(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
PyObject *pylist;
|
||||||
|
PyObject *pyexpr;
|
||||||
|
if (!PyArg_ParseTuple(args, "O!O!", &PyList_Type, &pylist, &pgf_ExprType, &pyexpr))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
PgfPrintContext *ctxt = NULL;
|
||||||
|
for (Py_ssize_t i = PyList_Size(pylist); i > 0 ; i--) {
|
||||||
|
PyObject *item = PyList_GetItem(pylist, i-1);
|
||||||
|
if (!PyUnicode_Check(item)) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "invalid variable argument in showExpr");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
PgfText *input = PyUnicode_AsPgfText(item);
|
||||||
|
|
||||||
|
// TODO a better way to copy into this->name?
|
||||||
|
PgfPrintContext *this = (PgfPrintContext *)PyMem_Malloc(sizeof(PgfPrintContext *) + sizeof(PgfText) + input->size + 1);
|
||||||
|
this->next = ctxt;
|
||||||
|
memcpy(&this->name, input, sizeof(PgfText) + input->size + 1);
|
||||||
|
ctxt = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfText *s = pgf_print_expr((PgfExpr) pyexpr, ctxt, 0, &marshaller);
|
||||||
|
PyObject *str = PyUnicode_FromStringAndSize(s->text, s->size);
|
||||||
|
free(s);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
static TypeObject *
|
static TypeObject *
|
||||||
pgf_readType(PyObject *self, PyObject *args)
|
pgf_readType(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
@@ -2594,6 +2624,8 @@ static PyMethodDef module_methods[] = {
|
|||||||
"Reads an NGF file into memory"},
|
"Reads an NGF file into memory"},
|
||||||
{"readExpr", (void*)pgf_readExpr, METH_VARARGS,
|
{"readExpr", (void*)pgf_readExpr, METH_VARARGS,
|
||||||
"Parses a string as an abstract tree"},
|
"Parses a string as an abstract tree"},
|
||||||
|
{"showExpr", (void*)pgf_showExpr, METH_VARARGS,
|
||||||
|
"Renders an expression as a string"},
|
||||||
{"readType", (void*)pgf_readType, METH_VARARGS,
|
{"readType", (void*)pgf_readType, METH_VARARGS,
|
||||||
"Parses a string as an abstract type"},
|
"Parses a string as an abstract type"},
|
||||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||||
|
|||||||
@@ -222,11 +222,6 @@ def test_readExpr_lstr_str():
|
|||||||
|
|
||||||
# expressions: variables
|
# expressions: variables
|
||||||
|
|
||||||
# ,TestCase (assertEqual "show expression 4" "x" (showExpr ["x"] (EVar 0)))
|
|
||||||
# ,TestCase (assertEqual "show expression 5" "#1" (showExpr ["x"] (EVar 1)))
|
|
||||||
# ,TestCase (assertEqual "show expression 6" "z" (showExpr ["z","y","x"] (EVar 0)))
|
|
||||||
# ,TestCase (assertEqual "show expression 7" "y" (showExpr ["z","y","x"] (EVar 1)))
|
|
||||||
|
|
||||||
# def test_readExpr_evar_equality_1():
|
# def test_readExpr_evar_equality_1():
|
||||||
# assert pgf.readExpr("#0") == pgf.ExprVar()
|
# assert pgf.readExpr("#0") == pgf.ExprVar()
|
||||||
# assert pgf.readExpr("#0") == pgf.ExprVar(0)
|
# assert pgf.readExpr("#0") == pgf.ExprVar(0)
|
||||||
@@ -240,6 +235,18 @@ def test_readExpr_evar_str_1():
|
|||||||
def test_readExpr_evar_str_2():
|
def test_readExpr_evar_str_2():
|
||||||
assert str(pgf.ExprVar(42)) == "#42"
|
assert str(pgf.ExprVar(42)) == "#42"
|
||||||
|
|
||||||
|
def test_showExpr_evar_1():
|
||||||
|
assert pgf.showExpr(["x"], pgf.ExprVar(0)) == "x"
|
||||||
|
|
||||||
|
def test_showExpr_evar_2():
|
||||||
|
assert pgf.showExpr(["x"], pgf.ExprVar(1)) == "#1"
|
||||||
|
|
||||||
|
def test_showExpr_evar_3():
|
||||||
|
assert pgf.showExpr(["z", "y", "x"], pgf.ExprVar(0)) == "z"
|
||||||
|
|
||||||
|
def test_showExpr_evar_4():
|
||||||
|
assert pgf.showExpr(["z", "y", "x"], pgf.ExprVar(1)) == "y"
|
||||||
|
|
||||||
# expressions: lambda abstractions
|
# expressions: lambda abstractions
|
||||||
|
|
||||||
# ,TestCase (assertEqual "show expression 8" "\\w->w" (showExpr ["z","y","x"] (EAbs Explicit "w" (EVar 0))))
|
# ,TestCase (assertEqual "show expression 8" "\\w->w" (showExpr ["z","y","x"] (EAbs Explicit "w" (EVar 0))))
|
||||||
|
|||||||
Reference in New Issue
Block a user