mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 11:42:49 -06:00
Add functionIsConstructor function
This commit is contained in:
@@ -1,11 +1,9 @@
|
|||||||
#define PY_SSIZE_T_CLEAN
|
#define PY_SSIZE_T_CLEAN
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <pgf/pgf.h>
|
#include <pgf/pgf.h>
|
||||||
|
|
||||||
#include "./compat.h"
|
#include "./compat.h"
|
||||||
#include "./expr.h"
|
#include "./expr.h"
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#define PY_SSIZE_T_CLEAN
|
#define PY_SSIZE_T_CLEAN
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
#include "structmember.h"
|
#include <structmember.h>
|
||||||
|
|
||||||
#include <pgf/pgf.h>
|
#include <pgf/pgf.h>
|
||||||
#include "./compat.h"
|
#include "./compat.h"
|
||||||
@@ -2047,6 +2047,28 @@ PGF_functionType(PGFObject *self, PyObject *args)
|
|||||||
return (TypeObject *)type;
|
return (TypeObject *)type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
PGF_functionIsConstructor(PGFObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
const char *s;
|
||||||
|
Py_ssize_t size;
|
||||||
|
if (!PyArg_ParseTuple(args, "s#", &s, &size))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
PgfText *funname = (PgfText *)PyMem_Malloc(sizeof(PgfText)+size+1);
|
||||||
|
memcpy(funname->text, s, size+1);
|
||||||
|
funname->size = size;
|
||||||
|
|
||||||
|
PgfExn err;
|
||||||
|
int isCon = pgf_function_is_constructor(self->db, self->revision, funname, &err);
|
||||||
|
PyMem_Free(funname);
|
||||||
|
if (handleError(err) != PGF_EXN_NONE) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return PyBool_FromLong(isCon);
|
||||||
|
}
|
||||||
|
|
||||||
// static IterObject*
|
// static IterObject*
|
||||||
// PGF_generateAll(PGFObject* self, PyObject *args, PyObject *keywds)
|
// PGF_generateAll(PGFObject* self, PyObject *args, PyObject *keywds)
|
||||||
// {
|
// {
|
||||||
@@ -2382,6 +2404,9 @@ static PyMethodDef PGF_methods[] = {
|
|||||||
{"functionType", (PyCFunction)PGF_functionType, METH_VARARGS,
|
{"functionType", (PyCFunction)PGF_functionType, METH_VARARGS,
|
||||||
"Returns the type of a function"
|
"Returns the type of a function"
|
||||||
},
|
},
|
||||||
|
{"functionIsConstructor", (PyCFunction)PGF_functionIsConstructor, METH_VARARGS,
|
||||||
|
"Checks whether a function is a constructor"
|
||||||
|
},
|
||||||
// {"generateAll", (PyCFunction)PGF_generateAll, METH_VARARGS | METH_KEYWORDS,
|
// {"generateAll", (PyCFunction)PGF_generateAll, METH_VARARGS | METH_KEYWORDS,
|
||||||
// "Generates abstract syntax trees of given category in decreasing probability order"
|
// "Generates abstract syntax trees of given category in decreasing probability order"
|
||||||
// },
|
// },
|
||||||
|
|||||||
@@ -100,6 +100,18 @@ def test_categoryContext_3(PGF):
|
|||||||
def test_categoryContext_4(PGF):
|
def test_categoryContext_4(PGF):
|
||||||
assert PGF.categoryContext("X") == []
|
assert PGF.categoryContext("X") == []
|
||||||
|
|
||||||
|
def test_functionIsConstructor_1(PGF):
|
||||||
|
assert PGF.functionIsConstructor("s") == True
|
||||||
|
|
||||||
|
def test_functionIsConstructor_2(PGF):
|
||||||
|
assert PGF.functionIsConstructor("z") == True
|
||||||
|
|
||||||
|
def test_functionIsConstructor_3(PGF):
|
||||||
|
assert PGF.functionIsConstructor("c") == True
|
||||||
|
|
||||||
|
def test_functionIsConstructor_4(PGF):
|
||||||
|
assert PGF.functionIsConstructor("ind") == False
|
||||||
|
|
||||||
# types
|
# types
|
||||||
|
|
||||||
def test_readType_invalid():
|
def test_readType_invalid():
|
||||||
|
|||||||
Reference in New Issue
Block a user