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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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;
  48. // init module & globals
  49. mod = PyModule_Create(&rpnmodule);
  50. if(mod == NULL) { return NULL; }
  51. // Init RPNExpr type
  52. if(PyType_Ready(&RPNExprType) < 0)
  53. {
  54. return NULL;
  55. }
  56. // Add type to module
  57. Py_INCREF(&RPNExprType);
  58. if(PyModule_AddObject(mod, "RPNExpr", (PyObject*)&RPNExprType) < 0)
  59. {
  60. Py_DECREF(&RPNExprType);
  61. Py_DECREF(mod);
  62. return NULL;
  63. }
  64. return mod;
  65. }
  66. PyObject* pyrpn_ops(PyObject* mod, PyObject* noargs)
  67. {
  68. PyObject *ret, *value;
  69. const rpn_op_t *op;
  70. size_t i;
  71. ret = PyDict_New();
  72. if(!ret)
  73. {
  74. return ret;
  75. }
  76. foreach_rpn_ops(i)
  77. {
  78. op = &rpn_ops[i];
  79. value = Py_BuildValue("C", op->chr);
  80. if(PyDict_SetItemString(ret, op->str, value))
  81. {
  82. Py_DECREF(value);
  83. Py_DECREF(ret);
  84. return NULL;
  85. }
  86. Py_DECREF(value);
  87. }
  88. return ret;
  89. }
  90. PyObject* pyrpn_random(PyObject *mod, PyObject *args, PyObject *kwds)
  91. {
  92. long long int args_count, expr_sz;
  93. char *expr, err_str[128];
  94. PyObject *res;
  95. char *names[] = {"args_count", "token_count", NULL};
  96. expr_sz = 10;
  97. if(!PyArg_ParseTupleAndKeywords(args, kwds, "L|L:pyrpn.random_expr", names,
  98. &args_count, &expr_sz))
  99. {
  100. return NULL;
  101. }
  102. expr = rpn_random(expr_sz, args_count);
  103. if(!expr)
  104. {
  105. snprintf(err_str, 128,
  106. "Error generating random expression : %s",
  107. strerror(errno));
  108. PyErr_SetString(PyExc_RuntimeError, err_str);
  109. return NULL;
  110. }
  111. res = Py_BuildValue("s", expr);
  112. //free(expr);
  113. return res;
  114. }