Genetic Turmit Evolver
python
c
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.

cturmit.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include<Python.h>
  2. #include"structmember.h"
  3. #include "turmit.h"
  4. /**
  5. * @todo search for decref and memory leaks :/
  6. */
  7. typedef struct
  8. {
  9. PyObject VAR_HEAD;
  10. Py_ssize_t len;
  11. turmit_t *turmit;
  12. } CTurmit;
  13. static PyModuleDef cturmitmodule = {
  14. PyModuleDef_HEAD_INIT,
  15. "cturmit",
  16. "Module that implements turmits in C",
  17. -1,
  18. NULL, NULL, NULL, NULL, NULL
  19. };
  20. static PyMemberDef CTurmit_members[] = {
  21. {"len", T_PYSSIZET, offsetof(CTurmit, len), 0,
  22. "Expression len"},
  23. /*
  24. {"heap_sz", T_PYSSIZET, offsetof(CTurmit, heap_sz), 0,
  25. "Item heap counter"},
  26. */
  27. {NULL} /* Sentinel */
  28. };
  29. /*
  30. Magic methods
  31. */
  32. Py_ssize_t CTurmit_len(CTurmit*);
  33. static PyMappingMethods CTurmit_asmapping[] = {{
  34. (void*)CTurmit_len,
  35. }};
  36. /*
  37. Other methods
  38. */
  39. #define TURMIT_PYOP(NAME) PyObject* CTurmit_op_##NAME(CTurmit* self) {\
  40. turmit_op_##NAME(self->turmit);\
  41. Py_RETURN_NONE;\
  42. }
  43. #define TURMIT_PYOPDEF(NAME) {#NAME, (PyCFunction)CTurmit_op_##NAME, \
  44. METH_NOARGS, NULL}
  45. PyObject* CTurmit__push(CTurmit* self, PyObject *args);
  46. PyObject* CTurmit__pop(CTurmit* self);
  47. TURMIT_PYOP(mem_sz)
  48. TURMIT_PYOP(add)
  49. TURMIT_PYOP(sub)
  50. TURMIT_PYOP(bin_and)
  51. TURMIT_PYOP(dup)
  52. TURMIT_PYOP(lshift)
  53. TURMIT_PYOP(mod)
  54. TURMIT_PYOP(mul)
  55. TURMIT_PYOP(div)
  56. TURMIT_PYOP(bin_or)
  57. TURMIT_PYOP(bin_xor)
  58. TURMIT_PYOP(pop)
  59. TURMIT_PYOP(swp)
  60. TURMIT_PYOP(jmp)
  61. TURMIT_PYOP(jz)
  62. TURMIT_PYOP(jcmp)
  63. static PyMethodDef CTurmit_methods[] = {
  64. {"_push", (PyCFunction)CTurmit__push, METH_KEYWORDS | METH_VARARGS,
  65. "Push a value on the stack"},
  66. {"_pop", (PyCFunction)CTurmit__pop, METH_NOARGS,
  67. "Pop a value from the stack"},
  68. TURMIT_PYOPDEF(mem_sz),
  69. TURMIT_PYOPDEF(add),
  70. TURMIT_PYOPDEF(sub),
  71. TURMIT_PYOPDEF(bin_and),
  72. TURMIT_PYOPDEF(dup),
  73. TURMIT_PYOPDEF(lshift),
  74. TURMIT_PYOPDEF(mod),
  75. TURMIT_PYOPDEF(mul),
  76. TURMIT_PYOPDEF(div),
  77. TURMIT_PYOPDEF(bin_or),
  78. TURMIT_PYOPDEF(bin_xor),
  79. TURMIT_PYOPDEF(pop),
  80. TURMIT_PYOPDEF(swp),
  81. TURMIT_PYOPDEF(jmp),
  82. TURMIT_PYOPDEF(jz),
  83. TURMIT_PYOPDEF(jcmp),
  84. {NULL} /* Sentinel */
  85. };
  86. PyObject* CTurmit_get_shead(CTurmit *self, void *ref);
  87. PyObject* CTurmit_get__cur(CTurmit *self, void *ref);
  88. PyObject* CTurmit_get__stack(CTurmit *self, void *ref);
  89. static struct PyGetSetDef CTurmit_getset[] = {
  90. {"shead", (getter)CTurmit_get_shead, NULL, NULL, NULL},
  91. {"_cur", (getter)CTurmit_get__cur, NULL, NULL, NULL},
  92. {"_stack", (getter)CTurmit_get__stack, NULL, NULL, NULL},
  93. {NULL} /* Sentinel */
  94. };
  95. static int _CTurmit_copy_init(CTurmit *self, CTurmit *t);
  96. static PyObject *
  97. CTurmit_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
  98. /**@brief CTurmit __init__ function
  99. * @param CTurmit* self
  100. * @param PyObject* can contain an interger representing stack size
  101. * @param Pyobject* contains kwargs. Can be : stack_size, max_int, prog
  102. */
  103. static int
  104. CTurmit_init(CTurmit *self, PyObject *args, PyObject *kwds);
  105. void
  106. CTurmit_dealloc(CTurmit *self);
  107. PyObject* CTurmit_call(PyObject *func, PyObject *args, PyObject *kwds);
  108. static PyTypeObject CTurmitType = {
  109. PyVarObject_HEAD_INIT(NULL, 0)
  110. "cturmit.CTurmit", /* tp_name */
  111. sizeof(CTurmit), /* tp_basicsize */
  112. 0, /* tp_itemsize */
  113. (destructor)CTurmit_dealloc, /* tp_dealloc */
  114. 0, /* tp_print */
  115. 0, /* tp_getattr */
  116. 0, /* tp_setattr */
  117. 0, /* tp_reserved */
  118. 0, /* tp_repr */
  119. 0, /* tp_as_number */
  120. 0, /* tp_as_sequence */
  121. CTurmit_asmapping, /* tp_as_mapping */
  122. 0, /* tp_hash */
  123. CTurmit_call, /* tp_call */
  124. 0, /* tp_str */
  125. 0, /* tp_getattro */
  126. 0, /* tp_setattro */
  127. 0, /* tp_as_buffer */
  128. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  129. "CTurmit objects", /* tp_doc */
  130. 0, /* tp_traverse */
  131. 0, /* tp_clear */
  132. 0, /* tp_richcompare */
  133. 0, /* tp_weaklistoffset */
  134. 0, /* tp_iter */
  135. 0, /* tp_iternext */
  136. CTurmit_methods, /* tp_methods */
  137. CTurmit_members, /* tp_members */
  138. CTurmit_getset, /* tp_getset */
  139. 0, /* tp_base */
  140. 0, /* tp_dict */
  141. 0, /* tp_descr_get */
  142. 0, /* tp_descr_set */
  143. 0, /* tp_dictoffset */
  144. (initproc)CTurmit_init, /* tp_init */
  145. 0, /* tp_alloc */
  146. CTurmit_new, /* tp_new */
  147. };