mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 03:32:51 -06:00
Implement Expr_str correctly (but doesn't handle big ints yet)
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
static PyObject *
|
static PyObject *
|
||||||
Expr_str(ExprObject *self)
|
Expr_str(ExprObject *self)
|
||||||
{
|
{
|
||||||
PgfText *s = pgf_print_expr((PgfExpr) self, NULL, 1, &marshaller);
|
PgfText *s = pgf_print_expr((PgfExpr) self, NULL, 0, &marshaller);
|
||||||
PyObject *str = PyUnicode_FromStringAndSize(s->text, s->size);
|
PyObject *str = PyUnicode_FromStringAndSize(s->text, s->size);
|
||||||
free(s);
|
free(s);
|
||||||
return str;
|
return str;
|
||||||
|
|||||||
@@ -16,13 +16,13 @@
|
|||||||
PgfExpr eabs(PgfUnmarshaller *this, PgfBindType btype, PgfText *name, PgfExpr body)
|
PgfExpr eabs(PgfUnmarshaller *this, PgfBindType btype, PgfText *name, PgfExpr body)
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_NotImplementedError, "eabs not implemented");
|
PyErr_SetString(PyExc_NotImplementedError, "eabs not implemented");
|
||||||
Py_RETURN_NOTIMPLEMENTED;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PgfExpr eapp(PgfUnmarshaller *this, PgfExpr fun, PgfExpr arg)
|
PgfExpr eapp(PgfUnmarshaller *this, PgfExpr fun, PgfExpr arg)
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_NotImplementedError, "eapp not implemented");
|
PyErr_SetString(PyExc_NotImplementedError, "eapp not implemented");
|
||||||
Py_RETURN_NOTIMPLEMENTED;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PgfExpr elit(PgfUnmarshaller *this, PgfLiteral lit)
|
PgfExpr elit(PgfUnmarshaller *this, PgfLiteral lit)
|
||||||
@@ -37,31 +37,31 @@ PgfExpr elit(PgfUnmarshaller *this, PgfLiteral lit)
|
|||||||
PgfExpr emeta(PgfUnmarshaller *this, PgfMetaId meta)
|
PgfExpr emeta(PgfUnmarshaller *this, PgfMetaId meta)
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_NotImplementedError, "emeta not implemented");
|
PyErr_SetString(PyExc_NotImplementedError, "emeta not implemented");
|
||||||
Py_RETURN_NOTIMPLEMENTED;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PgfExpr efun(PgfUnmarshaller *this, PgfText *name)
|
PgfExpr efun(PgfUnmarshaller *this, PgfText *name)
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_NotImplementedError, "efun not implemented");
|
PyErr_SetString(PyExc_NotImplementedError, "efun not implemented");
|
||||||
Py_RETURN_NOTIMPLEMENTED;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PgfExpr evar(PgfUnmarshaller *this, int index)
|
PgfExpr evar(PgfUnmarshaller *this, int index)
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_NotImplementedError, "evar not implemented");
|
PyErr_SetString(PyExc_NotImplementedError, "evar not implemented");
|
||||||
Py_RETURN_NOTIMPLEMENTED;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PgfExpr etyped(PgfUnmarshaller *this, PgfExpr expr, PgfType typ)
|
PgfExpr etyped(PgfUnmarshaller *this, PgfExpr expr, PgfType typ)
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_NotImplementedError, "etyped not implemented");
|
PyErr_SetString(PyExc_NotImplementedError, "etyped not implemented");
|
||||||
Py_RETURN_NOTIMPLEMENTED;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PgfExpr eimplarg(PgfUnmarshaller *this, PgfExpr expr)
|
PgfExpr eimplarg(PgfUnmarshaller *this, PgfExpr expr)
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_NotImplementedError, "eimplarg not implemented");
|
PyErr_SetString(PyExc_NotImplementedError, "eimplarg not implemented");
|
||||||
Py_RETURN_NOTIMPLEMENTED;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PgfLiteral lint(PgfUnmarshaller *this, size_t size, uintmax_t *v)
|
PgfLiteral lint(PgfUnmarshaller *this, size_t size, uintmax_t *v)
|
||||||
@@ -115,7 +115,7 @@ PgfType dtyp(PgfUnmarshaller *this, int n_hypos, PgfTypeHypo *hypos, PgfText *ca
|
|||||||
|
|
||||||
pytype->exprs = PyList_New(n_exprs);
|
pytype->exprs = PyList_New(n_exprs);
|
||||||
for (int i = 0; i < n_exprs; i++) {
|
for (int i = 0; i < n_exprs; i++) {
|
||||||
PyList_SetItem(pytype->exprs, i, exprs[i]);
|
PyList_SetItem(pytype->exprs, i, (PyObject *)exprs[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (PgfType) pytype;
|
return (PgfType) pytype;
|
||||||
@@ -175,7 +175,7 @@ object match_lit(PgfMarshaller *this, PgfUnmarshaller *u, PgfLiteral lit)
|
|||||||
|
|
||||||
if (PyLong_Check(pyobj)) {
|
if (PyLong_Check(pyobj)) {
|
||||||
// TODO
|
// TODO
|
||||||
uintmax_t i = PyLong_AsUnsignedLong(pyobj);
|
uintmax_t i = PyLong_AsLong(pyobj);
|
||||||
size_t size = 1;
|
size_t size = 1;
|
||||||
return u->vtbl->lint(u, size, &i);
|
return u->vtbl->lint(u, size, &i);
|
||||||
} else if (PyFloat_Check(pyobj)) {
|
} else if (PyFloat_Check(pyobj)) {
|
||||||
@@ -190,19 +190,25 @@ object match_lit(PgfMarshaller *this, PgfUnmarshaller *u, PgfLiteral lit)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object match_expr(PgfMarshaller *this, PgfUnmarshaller *u, PgfExpr expr)
|
object match_expr(PgfMarshaller *this, PgfUnmarshaller *u, PgfExpr ex)
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_NotImplementedError, "match_expr not implemented");
|
ExprObject *expr = (ExprObject *)ex;
|
||||||
Py_RETURN_NOTIMPLEMENTED;
|
|
||||||
|
if (expr->ob_base.ob_type == &pgf_ExprLitType) {
|
||||||
|
ExprLitObject *elit= (ExprLitObject *)expr;
|
||||||
|
return this->vtbl->match_lit(this, u, (PgfLiteral) elit->value);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object match_type(PgfMarshaller *this, PgfUnmarshaller *u, PgfType ty)
|
object match_type(PgfMarshaller *this, PgfUnmarshaller *u, PgfType ty)
|
||||||
{
|
{
|
||||||
TypeObject *type = (TypeObject *)ty;
|
TypeObject *type = (TypeObject *)ty;
|
||||||
|
|
||||||
size_t n_hypos = PyList_Size(type->hypos);
|
Py_ssize_t n_hypos = PyList_Size(type->hypos);
|
||||||
PgfTypeHypo *hypos = alloca(sizeof(PgfTypeHypo)*n_hypos);
|
PgfTypeHypo *hypos = alloca(sizeof(PgfTypeHypo)*n_hypos);
|
||||||
for (size_t i = 0; i < n_hypos; i++) {
|
for (Py_ssize_t i = 0; i < n_hypos; i++) {
|
||||||
PyObject *hytup = (PyObject *)PyList_GetItem(type->hypos, i);
|
PyObject *hytup = (PyObject *)PyList_GetItem(type->hypos, i);
|
||||||
hypos[i].bind_type = PyLong_AsLong(PyTuple_GetItem(hytup, 0));
|
hypos[i].bind_type = PyLong_AsLong(PyTuple_GetItem(hytup, 0));
|
||||||
hypos[i].cid = PyUnicode_AsPgfText(PyTuple_GetItem(hytup, 1));
|
hypos[i].cid = PyUnicode_AsPgfText(PyTuple_GetItem(hytup, 1));
|
||||||
@@ -215,24 +221,22 @@ object match_type(PgfMarshaller *this, PgfUnmarshaller *u, PgfType ty)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t n_exprs = PyList_Size(type->exprs);
|
Py_ssize_t n_exprs = PyList_Size(type->exprs);
|
||||||
PgfExpr *exprs = alloca(sizeof(PgfExpr)*n_exprs);
|
PgfExpr *exprs = alloca(sizeof(PgfExpr)*n_exprs);
|
||||||
for (size_t i = 0; i < n_exprs; i++) {
|
for (Py_ssize_t i = 0; i < n_exprs; i++) {
|
||||||
exprs[i] = (PgfExpr *)PyList_GetItem(type->exprs, i);
|
exprs[i] = (PgfExpr)PyList_GetItem(type->exprs, i);
|
||||||
Py_INCREF(exprs[i]);
|
Py_INCREF(exprs[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
object res = u->vtbl->dtyp(u, n_hypos, hypos, cat, n_exprs, exprs);
|
object res = u->vtbl->dtyp(u, n_hypos, hypos, cat, n_exprs, exprs);
|
||||||
|
|
||||||
for (size_t i = 0; i < n_exprs; i++) {
|
for (Py_ssize_t i = 0; i < n_exprs; i++) {
|
||||||
Py_DECREF(exprs[i]);
|
Py_DECREF(exprs[i]);
|
||||||
}
|
}
|
||||||
|
for (Py_ssize_t i = 0; i < n_hypos; i++) {
|
||||||
for (size_t i = 0; i < n_hypos; i++) {
|
|
||||||
free(hypos[i].cid);
|
free(hypos[i].cid);
|
||||||
Py_DECREF(hypos[i].type);
|
Py_DECREF(hypos[i].type);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(cat);
|
free(cat);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ if libraries==['']:
|
|||||||
pgf_module = Extension(
|
pgf_module = Extension(
|
||||||
'pgf',
|
'pgf',
|
||||||
sources = ['pypgf.c', 'marshaller.c', 'expr.c'],
|
sources = ['pypgf.c', 'marshaller.c', 'expr.c'],
|
||||||
extra_compile_args = ['-std=c99', '-Werror', '-Wno-error=int-conversion', '-Wno-error=unused-variable', '-Wno-comment'],
|
extra_compile_args = ['-std=c99', '-Werror', '-Wno-error=unused-variable', '-Wno-comment'],
|
||||||
include_dirs = includes,
|
include_dirs = includes,
|
||||||
library_dirs = libraries,
|
library_dirs = libraries,
|
||||||
libraries = ['pgf'])
|
libraries = ['pgf'])
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ def test_readType_inequality_2():
|
|||||||
assert pgf.readType("A -> B") != pgf.readType("B->B")
|
assert pgf.readType("A -> B") != pgf.readType("B->B")
|
||||||
|
|
||||||
def test_readType_str():
|
def test_readType_str():
|
||||||
assert str(pgf.readType("A-> BÄ")) == "A -> BÄ"
|
assert str(pgf.readType("A-> BÄ->C")) == "A -> BÄ -> C"
|
||||||
|
|
||||||
def test_functionType_1(PGF):
|
def test_functionType_1(PGF):
|
||||||
assert PGF.functionType("z") == pgf.readType("N")
|
assert PGF.functionType("z") == pgf.readType("N")
|
||||||
@@ -159,5 +159,20 @@ def test_readExpr_equality_string():
|
|||||||
def test_readExpr_inequality_string():
|
def test_readExpr_inequality_string():
|
||||||
assert pgf.readExpr("\"abc\"") != pgf.readExpr("\"def\"")
|
assert pgf.readExpr("\"abc\"") != pgf.readExpr("\"def\"")
|
||||||
|
|
||||||
# def test_readExpr_str_int():
|
def test_readExpr_str_int():
|
||||||
# assert str(pgf.readExpr("123")) == "123"
|
assert str(pgf.readExpr("123")) == "123"
|
||||||
|
|
||||||
|
def test_readExpr_str_int_neg():
|
||||||
|
assert str(pgf.readExpr("-123")) == "-123"
|
||||||
|
|
||||||
|
# def test_readExpr_str_int_big():
|
||||||
|
# assert str(pgf.readExpr("774763251095801167872")) == "774763251095801167872"
|
||||||
|
#
|
||||||
|
# def test_readExpr_str_int_big_neg():
|
||||||
|
# assert str(pgf.readExpr("-774763251095801167872")) == "-774763251095801167872"
|
||||||
|
|
||||||
|
def test_readExpr_str_float():
|
||||||
|
assert str(pgf.readExpr("3.142")) == "3.142"
|
||||||
|
|
||||||
|
def test_readExpr_str_string():
|
||||||
|
assert str(pgf.readExpr("\"açġħ\"")) == "\"açġħ\""
|
||||||
|
|||||||
Reference in New Issue
Block a user