Starting with __len__ Next we have to implement token acces as a sequence in RPNExpr, but to do this we have to implement a module allowing token access from python.
215 lines
6.5 KiB
C
215 lines
6.5 KiB
C
/*
|
|
* Copyright (C) 2020 Weber Yann
|
|
*
|
|
* This file is part of pyrpn.
|
|
*
|
|
* pyrpn is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* any later version.
|
|
*
|
|
* pyrpn is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with pyrpn. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#ifndef _PYTHON_RPNEXPR_H__
|
|
#define _PYTHON_RPNEXPR_H__
|
|
|
|
#include "config.h"
|
|
|
|
#include <errno.h>
|
|
#include <float.h>
|
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
#include <Python.h>
|
|
#include "structmember.h"
|
|
|
|
#include "rpn_jit.h"
|
|
#include "python_const.h"
|
|
|
|
/**@defgroup python_type RPNExpr Python class
|
|
* @brief Exposed Python class : RPNExpr
|
|
* @ingroup python_module
|
|
*/
|
|
|
|
/**@file python_rpnexpr.h
|
|
* @brief Python RPNExpr type headers
|
|
* @ingroup python_type
|
|
*
|
|
* This file is the header of the RPNExpr Python class
|
|
*
|
|
* @todo implements tp_as_sequence in order to give access to tokens from
|
|
* expr. To do this we have to implement a python module representing tokens.
|
|
*/
|
|
|
|
/**@brief RPNExpr Python class methods list
|
|
* @ingroup python_type */
|
|
extern PyMethodDef RPNExpr_methods[];
|
|
/**@brief RPNExpr Python class members list
|
|
* @ingroup python_type */
|
|
extern PyMemberDef RPNExpr_members[];
|
|
/**@brief RPNExpr Python class type definition
|
|
* @ingroup python_type */
|
|
extern PyTypeObject RPNExprType;
|
|
|
|
/**@brief Structure holding RPNExpr objects
|
|
* @ingroup python_type */
|
|
typedef struct
|
|
{
|
|
PyObject_VAR_HEAD;
|
|
|
|
/**@brief Pointer on @ref rpn_expr_s */
|
|
rpn_expr_t *rpn;
|
|
|
|
/**@brief Array storing expression argument
|
|
* @note As attribute of rpn_expr allowing malloc & free only once */
|
|
rpn_value_t *args;
|
|
|
|
/**@brief If true, someone else will take care of freeing the
|
|
* rpn expression at deletion */
|
|
short borrowed_expr;
|
|
} PyRPNExpr_t;
|
|
|
|
/**@brief Organize PyRPNExpr_t state data
|
|
* @see rpnexpr_getstate
|
|
* @see rpnexpr_setstate */
|
|
typedef struct
|
|
{
|
|
size_t total_sz;
|
|
size_t argc;
|
|
unsigned char stack_sz;
|
|
size_t token_sz;
|
|
} PyRPNExpr_state_t;
|
|
|
|
/**@brief Return a new instance of RPNExpr with a borrowed expression
|
|
* @note Borrowed expression means that the rpn_expr_t is handled by someone
|
|
* else and should not be freed when the instance is deleted
|
|
*/
|
|
PyObject* rpnexpr_init_borrowing(rpn_expr_t *borrowed);
|
|
|
|
/**@brief RpnExpr __new__ method
|
|
* @param subtype Type of object being created (pyrpn.RPNExpr)
|
|
* @param args positional arguments for subtype
|
|
* @param kwargs keyword argumenrs for subtype
|
|
* @return The new Python RPNExpr object
|
|
*/
|
|
PyObject* rpnexpr_new(PyTypeObject *subtype, PyObject *args, PyObject* kwds);
|
|
|
|
/**@brief RpnExpr constructor
|
|
* @param self New RPNExpr instance
|
|
* @param args Positional arguments list
|
|
* @param kwds Keywords arguments dict
|
|
* @return 0 if no error else -1
|
|
* @ingroup python_type
|
|
*/
|
|
int rpnexpr_init(PyObject *self, PyObject *args, PyObject *kwds);
|
|
|
|
/**@brief RPNExpr __del__ method
|
|
* @param self RPNExpr instance
|
|
* @ingroup python_type
|
|
*/
|
|
void rpnexpr_del(PyObject *self);
|
|
|
|
/**@brief Returns a byte representation of expression (like getstate but
|
|
* without the stack informations
|
|
* @param self RPNExpr instance
|
|
* @param noargs Not an argument...
|
|
* @return A bytes Python instance
|
|
*/
|
|
PyObject* rpnexpr_getexprstate(PyObject *self, PyObject *noargs);
|
|
|
|
/**@brief RPNExpr __getstate__ method for pickling
|
|
* @param self RPNExpr type object
|
|
* @param noargs Not an argument...
|
|
* @return A bytes Python instance suitable as argument for
|
|
* @ref rpnexpr_setstate
|
|
*/
|
|
PyObject* rpnexpr_getstate(PyObject *self, PyObject *noargs);
|
|
|
|
/**@brief RPNExpr __setstate__ method for pickling
|
|
* @param cls RPNExpr type object
|
|
* @param state Should by a bytes Python instance returned by @ref
|
|
* rpnexpr_getstate
|
|
* @return A bytes Python instance suitable as argument for
|
|
* @ref rpnexpr_getstate
|
|
*/
|
|
PyObject* rpnexpr_setstate(PyObject *cls, PyObject *state);
|
|
|
|
/**@brief RPNExpr __copy__ method for cloning
|
|
* @param self RPNExpr instance
|
|
* @param noargs Not an argument...
|
|
* @return A new cloned instance
|
|
* @ref rpnexpr_setstate
|
|
*/
|
|
PyObject* rpnexpr_copy(PyObject *cls, PyObject *noargs);
|
|
|
|
/**@brief RPNExpr.__len__() method
|
|
* @return A integer with the number of tokens in expression
|
|
*/
|
|
Py_ssize_t rpnexpr_len(PyObject *self);
|
|
|
|
/**@brief Eval an RPN expression given arguments and return the
|
|
* value
|
|
* @param self RPNExpr instance
|
|
* @param argv Array of PyObject* arguments
|
|
* @param argc Number of arguments
|
|
* @return The value resulting of evaluation
|
|
* @ingroup python_type
|
|
*/
|
|
PyObject* rpnexpr_eval(PyObject* self, PyObject** argv, Py_ssize_t argc);
|
|
|
|
/**@brief Mutate an RPN expression given arguments and return the value
|
|
* @param PyObject* RPNExpr instance
|
|
* @param PyObject* Positionnal arguments
|
|
* @param PyObject* Keyword arguments
|
|
*/
|
|
PyObject* rpnexpr_mutate(PyObject* self, PyObject *args, PyObject *kwds);
|
|
|
|
/**@brief Set all stack item to zero
|
|
* @param self RPNExpr instance
|
|
* @param noargs Dummy argument for METH_NOARG
|
|
* @return None
|
|
* @ingroup python_type
|
|
*/
|
|
PyObject* rpnexpr_reset_stack(PyObject *self, PyObject *noargs);
|
|
|
|
/**@brief RPNExpr.__repr__()
|
|
* @param self RPNExpr instance
|
|
* @ingroup python_type
|
|
*/
|
|
PyObject* rpnexpr_repr(PyObject *self);
|
|
|
|
/**@brief RPNExpr.__str__()
|
|
* @param self RPNExpr instance
|
|
* @ingroup python_type
|
|
*/
|
|
PyObject* rpnexpr_str(PyObject *self);
|
|
|
|
/**@brief Return a new Python str with a random RPN expression
|
|
* @param mod pyrpn module object
|
|
* @param args Position arguments in Python list
|
|
* @param kwds Keywords arguments in Python dict
|
|
* @ingroup python_module
|
|
*/
|
|
PyObject* rpnexpr_random(PyObject *cls, PyObject *args, PyObject *kwds);
|
|
|
|
/**@brief Return a new named tuple containing default mutation parameters
|
|
* @param PyObject* The class (class method)
|
|
* @param PyObject** The arguments (FASTCALL)
|
|
* @param Py_ssize_t The number of arguments
|
|
* @return The named tuple
|
|
*/
|
|
PyObject* rpnexpr_default_mutation_params(PyObject *cls, PyObject **argv, Py_ssize_t argc);
|
|
|
|
/**@brief Try to convert a python object into a rpn_mutation_params_t
|
|
* @param PyObject* The python object
|
|
* @param rpn_mutation_params_t A pointer on the parameters to initialize
|
|
* @return -1 on failure and raise an exception
|
|
*/
|
|
int rpnexpr_pyobj_to_mutation_params(PyObject* py_params, rpn_mutation_params_t *params);
|
|
|
|
#endif
|