Fast IFS using RPN notation
python
c
x86-64
nasm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

python_rpnexpr.h 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2020 Weber Yann
  3. *
  4. * This file is part of pyrpn.
  5. *
  6. * pyrpn is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * pyrpn is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with pyrpn. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _PYTHON_RPNEXPR_H__
  20. #define _PYTHON_RPNEXPR_H__
  21. #include "config.h"
  22. #include <errno.h>
  23. #include <float.h>
  24. #define PY_SSIZE_T_CLEAN
  25. #include <Python.h>
  26. #include "structmember.h"
  27. #include "rpn_jit.h"
  28. #include "python_rpntoken.h"
  29. #include "python_mutation.h"
  30. #include "python_const.h"
  31. /**@file python_rpnexpr.h
  32. * @brief pyrpn.RPNExpr definition
  33. * @ingroup python_ext
  34. * @ingroup pymod_pyrpn_RPNExpr
  35. *
  36. * This file is the header of the RPNExpr Python class
  37. */
  38. /**@defgroup pymod_pyrpn_RPNExpr pyrpn.RPNExpr
  39. * @brief Exposed Python class : RPNExpr
  40. * @ingroup pymod_pyrpn
  41. */
  42. /**@brief RPNExpr Python class type definition
  43. * @ingroup pymod_pyrpn_RPNExpr */
  44. extern PyTypeObject RPNExprType;
  45. /**@brief Structure holding RPNExpr objects
  46. * @ingroup pymod_pyrpn_RPNExpr */
  47. typedef struct
  48. {
  49. /** Python's type definition */
  50. PyObject_VAR_HEAD;
  51. /**@brief Pointer on @ref rpn_expr_s */
  52. rpn_expr_t *rpn;
  53. /**@brief Array storing expression argument
  54. * @note As attribute of rpn_expr allowing malloc & free only once */
  55. rpn_value_t *args;
  56. /**@brief If true, someone else will take care of freeing the
  57. * rpn expression at deletion */
  58. short borrowed_expr;
  59. } PyRPNExpr_t;
  60. /**@brief Organize PyRPNExpr_t state data
  61. * @see rpnexpr_getstate
  62. * @see rpnexpr_setstate
  63. * @ingroup pymod_pyrpn_RPNExpr */
  64. typedef struct
  65. {
  66. /**@brief The total size of the state */
  67. size_t total_sz;
  68. /**@brief Expression's argument count */
  69. size_t argc;
  70. /**@brief Expression's stack size */
  71. unsigned char stack_sz;
  72. /**@brief Number of tokens in expression */
  73. size_t token_sz;
  74. } PyRPNExpr_state_t;
  75. /**@brief Return a new instance of RPNExpr with a borrowed expression
  76. * @param borrowed An rpn expression to borrow
  77. * @note Borrowed expression means that the rpn_expr_t is handled by someone
  78. * else and should not be freed when the instance is deleted
  79. * @return A new RPNExpr instance
  80. * @ingroup pymod_pyrpn_RPNExpr
  81. */
  82. PyObject* rpnexpr_init_borrowing(rpn_expr_t *borrowed);
  83. /**@brief RpnExpr __new__ method
  84. * @param subtype Type of object being created (pyrpn.RPNExpr)
  85. * @param args positional arguments for subtype
  86. * @param kwds keyword argumenrs for subtype
  87. * @return The new Python RPNExpr object
  88. * @ingroup pymod_pyrpn_RPNExpr
  89. */
  90. PyObject* rpnexpr_new(PyTypeObject *subtype, PyObject *args, PyObject* kwds);
  91. /**@brief RpnExpr constructor
  92. * @param self New RPNExpr instance
  93. * @param args Positional arguments list
  94. * @param kwds Keywords arguments dict
  95. * @return 0 if no error else -1
  96. * @ingroup pymod_pyrpn_RPNExpr
  97. */
  98. int rpnexpr_init(PyObject *self, PyObject *args, PyObject *kwds);
  99. /**@brief RPNExpr __del__ method
  100. * @param self RPNExpr instance
  101. * @ingroup pymod_pyrpn_RPNExpr
  102. */
  103. void rpnexpr_del(PyObject *self);
  104. /**@brief Returns a byte representation of expression (like getstate but
  105. * without the stack informations
  106. * @param self RPNExpr instance
  107. * @param noargs Not an argument...
  108. * @return A bytes Python instance
  109. * @ingroup pymod_pyrpn_RPNExpr
  110. */
  111. PyObject* rpnexpr_getexprstate(PyObject *self, PyObject *noargs);
  112. /**@brief RPNExpr __getstate__ method for pickling
  113. * @param self RPNExpr type object
  114. * @param noargs Not an argument...
  115. * @return A bytes Python instance suitable as argument for
  116. * @ref rpnexpr_setstate
  117. * @ingroup pymod_pyrpn_RPNExpr
  118. */
  119. PyObject* rpnexpr_getstate(PyObject *self, PyObject *noargs);
  120. /**@brief RPNExpr __setstate__ method for pickling
  121. * @param cls RPNExpr type object
  122. * @param state Should by a bytes Python instance returned by @ref
  123. * rpnexpr_getstate
  124. * @return A bytes Python instance suitable as argument for
  125. * @ref rpnexpr_getstate
  126. * @ingroup pymod_pyrpn_RPNExpr
  127. */
  128. PyObject* rpnexpr_setstate(PyObject *cls, PyObject *state);
  129. /**@brief RPNExpr __copy__ method for cloning
  130. * @param cls RPNExpr class
  131. * @param noargs Not an argument...
  132. * @return A new cloned instance
  133. * @ref rpnexpr_setstate
  134. * @ingroup pymod_pyrpn_RPNExpr
  135. */
  136. PyObject* rpnexpr_copy(PyObject *cls, PyObject *noargs);
  137. /**@brief RPNExpr.__len__() method
  138. * @param self RPNExpr instance
  139. * @return A integer with the number of tokens in expression
  140. * @ingroup pymod_pyrpn_RPNExpr
  141. */
  142. Py_ssize_t rpnexpr_len(PyObject *self);
  143. /**@brief Sequence method for __getitem__ token getter
  144. * @param self RPNExpr instance
  145. * @param idx The item index (can be negative)
  146. * @return A @ref RPNTokenType subclass instance
  147. * @ingroup pymod_pyrpn_RPNExpr
  148. */
  149. PyObject* rpnexpr_token_item(PyObject *self, Py_ssize_t idx);
  150. /**@brief Sequence method for __setitem__ token setter
  151. * @param self RPNExpr instance
  152. * @param idx The item index
  153. * @param elt An RPNTokenType subclass token to set
  154. * @return 0 or -1 on error and raise IndexError
  155. * @ingroup pymod_pyrpn_RPNExpr
  156. */
  157. int rpnexpr_token_ass_item(PyObject *self, Py_ssize_t idx, PyObject* elt);
  158. /**@brief Eval an RPN expression given arguments and return the
  159. * value
  160. * @param self RPNExpr instance
  161. * @param argv Array of PyObject* arguments
  162. * @param argc Number of arguments
  163. * @return The value resulting of evaluation
  164. * @ingroup pymod_pyrpn_RPNExpr
  165. */
  166. PyObject* rpnexpr_eval(PyObject* self, PyObject** argv, Py_ssize_t argc);
  167. /**@brief Mutate an RPN expression given arguments and return the value
  168. * @param self RPNExpr instance
  169. * @param args Positionnal arguments
  170. * @param kwds Keyword arguments
  171. * @return Py_None
  172. * @ingroup pymod_pyrpn_RPNExpr
  173. */
  174. PyObject* rpnexpr_mutate(PyObject* self, PyObject *args, PyObject *kwds);
  175. /**@brief Set all stack item to zero
  176. * @param self RPNExpr instance
  177. * @param noargs Dummy argument for METH_NOARG
  178. * @return None
  179. * @ingroup pymod_pyrpn_RPNExpr
  180. */
  181. PyObject* rpnexpr_reset_stack(PyObject *self, PyObject *noargs);
  182. /**@brief RPNExpr.__repr__()
  183. * @param self RPNExpr instance
  184. * @return An str instance representing the exppression
  185. * @ingroup pymod_pyrpn_RPNExpr
  186. */
  187. PyObject* rpnexpr_repr(PyObject *self);
  188. /**@brief RPNExpr.__str__()
  189. * @param self RPNExpr instance
  190. * @return An str instance representing the exppression
  191. * @ingroup pymod_pyrpn_RPNExpr
  192. */
  193. PyObject* rpnexpr_str(PyObject *self);
  194. /**@brief RPNExpr PEP-207 richcompare method
  195. * @param _self RPNExpr instance
  196. * @param other RPNExpr instance to compare to
  197. * @param op The comparison to be done
  198. * @return Py_True or Py_False
  199. * @ingroup pymod_pyrpn_RPNExpr
  200. */
  201. PyObject* rpnexpr_richcompare(PyObject *_self, PyObject *other, int op);
  202. /**@brief Return a new Python str with a random RPN expression
  203. * @param cls pyrpn module object
  204. * @param args Position arguments in Python list
  205. * @param kwds Keywords arguments in Python dict
  206. * @return A str instance
  207. * @ingroup pymod_pyrpn
  208. */
  209. PyObject* rpnexpr_random(PyObject *cls, PyObject *args, PyObject *kwds);
  210. /**@brief Return a new named tuple containing default mutation parameters
  211. * @param cls The class (class method)
  212. * @param argv The arguments (FASTCALL)
  213. * @param argc The number of arguments
  214. * @return The named tuple
  215. * @ingroup pymod_pyrpn_RPNExpr
  216. */
  217. PyObject* rpnexpr_default_mutation_params(PyObject *cls, PyObject **argv, Py_ssize_t argc);
  218. #endif