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_pyrpn.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "python_pyrpn.h"
  20. /**@file python_pyrpn.c
  21. * @brief Python module & type definition
  22. * @ingroup python_ext
  23. *
  24. * This file contains pyrpn Python module definition
  25. */
  26. PyMethodDef rpnmodule_methods[] = {
  27. {"get_ops", (PyCFunction)pyrpn_ops, METH_NOARGS,
  28. "Returns a valid operands dict"},
  29. {"random_expr", (PyCFunction)pyrpn_random, METH_VARARGS | METH_KEYWORDS,
  30. "Return a random RPN expression"},
  31. {NULL} // Sentinel
  32. };
  33. PyModuleDef rpnmodule = {
  34. PyModuleDef_HEAD_INIT,
  35. "pyrpn",
  36. "Python librarie for RPN evaluation",
  37. -1, // module size
  38. rpnmodule_methods,
  39. NULL, // m_slots
  40. NULL, // m_traverse
  41. NULL, // m_clear
  42. NULL // m_free
  43. };
  44. PyMODINIT_FUNC
  45. PyInit_pyrpn(void)
  46. {
  47. PyObject *mod, *const_mod;
  48. // init module & globals
  49. mod = PyModule_Create(&rpnmodule);
  50. if(mod == NULL) { return NULL; }
  51. //init constants module
  52. const_mod = Py_rpnconst_init();
  53. if(const_mod == NULL)
  54. {
  55. Py_DECREF(mod);
  56. return NULL;
  57. }
  58. Py_INCREF(const_mod);
  59. if(PyModule_AddObject(mod, "const", const_mod) < 0)
  60. {
  61. Py_DECREF(const_mod);
  62. Py_DECREF(mod);
  63. return NULL;
  64. }
  65. // Init RPNExpr type
  66. if(PyType_Ready(&RPNExprType) < 0)
  67. {
  68. return NULL;
  69. }
  70. // Add type to module
  71. Py_INCREF(&RPNExprType);
  72. if(PyModule_AddObject(mod, "RPNExpr", (PyObject*)&RPNExprType) < 0)
  73. {
  74. Py_DECREF(&RPNExprType);
  75. Py_DECREF(mod);
  76. Py_DECREF(const_mod);
  77. return NULL;
  78. }
  79. // Init RPNIterExpr type
  80. if(PyType_Ready(&RPNIterExprType) < 0)
  81. {
  82. return NULL;
  83. }
  84. Py_INCREF(&RPNIterExprType);
  85. if(PyModule_AddObject(mod, "RPNIterExpr", (PyObject*)&RPNIterExprType) < 0)
  86. {
  87. Py_DECREF(&RPNExprType);
  88. Py_DECREF(&RPNIterExprType);
  89. Py_DECREF(mod);
  90. return NULL;
  91. }
  92. return mod;
  93. }
  94. PyObject* pyrpn_ops(PyObject* mod, PyObject* noargs)
  95. {
  96. PyObject *ret, *value;
  97. const rpn_op_t *op;
  98. size_t i;
  99. ret = PyDict_New();
  100. if(!ret)
  101. {
  102. return ret;
  103. }
  104. foreach_rpn_ops(i)
  105. {
  106. op = &rpn_ops[i];
  107. value = Py_BuildValue("C", op->chr);
  108. if(PyDict_SetItemString(ret, op->str, value))
  109. {
  110. Py_DECREF(value);
  111. Py_DECREF(ret);
  112. return NULL;
  113. }
  114. Py_DECREF(value);
  115. }
  116. return ret;
  117. }
  118. PyObject* pyrpn_random(PyObject *mod, PyObject *args, PyObject *kwds)
  119. {
  120. long long int args_count, expr_sz;
  121. char *expr, err_str[128];
  122. PyObject *res;
  123. char *names[] = {"args_count", "token_count", NULL};
  124. expr_sz = 10;
  125. if(!PyArg_ParseTupleAndKeywords(args, kwds, "L|L:pyrpn.random_expr", names,
  126. &args_count, &expr_sz))
  127. {
  128. return NULL;
  129. }
  130. expr = rpn_random(expr_sz, args_count);
  131. if(!expr)
  132. {
  133. snprintf(err_str, 128,
  134. "Error generating random expression : %s",
  135. strerror(errno));
  136. PyErr_SetString(PyExc_RuntimeError, err_str);
  137. return NULL;
  138. }
  139. res = Py_BuildValue("s", expr);
  140. //free(expr);
  141. return res;
  142. }