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.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "cturmit.h"
  2. PyMODINIT_FUNC
  3. PyInit_cturmit(void)
  4. {
  5. PyObject* m;
  6. CTurmitType.tp_new = PyType_GenericNew;
  7. if (PyType_Ready(&CTurmitType) < 0)
  8. return NULL;
  9. m = PyModule_Create(&cturmitmodule);
  10. if (m == NULL)
  11. return NULL;
  12. Py_INCREF(&CTurmitType);
  13. PyModule_AddObject(m, "CTurmit", (PyObject *)&CTurmitType);
  14. return m;
  15. }
  16. static int
  17. CTurmit_init(CTurmit *self, PyObject *args, PyObject *kwds)
  18. {
  19. size_t sz;
  20. char err_msg[128];
  21. ssize_t arg_stk_sz, arg_int_max;
  22. char *arg_expr;
  23. int i, ret;
  24. CTurmit *turmit;
  25. PyObject *stack_size, *int_max, *expr, *keys, *asc_str;
  26. PyObject *errtype, *errvalue, *errbck;
  27. PyErr_Fetch(&errtype, &errvalue, &errbck);
  28. turmit = (CTurmit*)PyMapping_GetItemString(kwds, "turmit");
  29. if(turmit)
  30. {
  31. Py_XDECREF(errtype);
  32. Py_XDECREF(errvalue);
  33. Py_XDECREF(errbck);
  34. ret = _CTurmit_copy_init(self, turmit);
  35. Py_DECREF(turmit);
  36. return ret;
  37. }
  38. PyErr_Restore(errtype, errvalue, errbck);
  39. stack_size = int_max = expr = NULL;
  40. self->turmit = NULL;
  41. //Checking arguments
  42. if((sz = PySequence_Length(args)) >= 0)
  43. {
  44. if(sz > 1)
  45. {
  46. PyErr_Restore(errtype, errvalue, errbck);
  47. snprintf(err_msg, 128, "1 positional argument expected \
  48. but %ld found", sz);
  49. PyErr_SetString(PyExc_TypeError,
  50. err_msg);
  51. goto cturmit_init_error;
  52. }
  53. stack_size = PySequence_GetItem(args, 0);
  54. }
  55. if(!stack_size)
  56. {
  57. stack_size = PyMapping_GetItemString(kwds, "stack_size");
  58. PyMapping_DelItemString(kwds, "stack_size");
  59. }
  60. if(stack_size)
  61. {
  62. if(!PyLong_CheckExact(stack_size))
  63. {
  64. PyErr_Restore(errtype, errvalue, errbck);
  65. Py_DECREF(stack_size);
  66. snprintf(err_msg, 128,
  67. "stack_size expected to be an integer");
  68. PyErr_SetString(PyExc_TypeError,
  69. err_msg);
  70. goto cturmit_init_error;
  71. }
  72. arg_stk_sz = PyLong_AsSsize_t(stack_size);
  73. Py_DECREF(stack_size);
  74. if(arg_stk_sz == (size_t)-1 && PyErr_Occurred())
  75. {
  76. PyErr_Restore(errtype, errvalue, errbck);
  77. snprintf(err_msg, 128,
  78. "size_t overflow with stack_size");
  79. PyErr_SetString(PyExc_ValueError,
  80. err_msg);
  81. goto cturmit_init_error;
  82. }
  83. }
  84. else
  85. {
  86. arg_stk_sz = 8;
  87. }
  88. int_max = PyMapping_GetItemString(kwds, "max_int");
  89. PyMapping_DelItemString(kwds, "max_int");
  90. if(int_max)
  91. {
  92. if(!PyLong_CheckExact(int_max))
  93. {
  94. PyErr_Restore(errtype, errvalue, errbck);
  95. snprintf(err_msg, 128,
  96. "int_max expected to be an integer");
  97. PyErr_SetString(PyExc_TypeError,
  98. err_msg);
  99. goto cturmit_init_error;
  100. }
  101. arg_int_max = PyLong_AsSsize_t(int_max);
  102. Py_DECREF(int_max);
  103. if(arg_int_max == (size_t)-1 && PyErr_Occurred())
  104. {
  105. PyErr_Restore(errtype, errvalue, errbck);
  106. PyErr_SetString(PyExc_TypeError,
  107. "size_t overflow with stack_size");
  108. PyErr_SetString(PyExc_ValueError,
  109. err_msg);
  110. goto cturmit_init_error;
  111. }
  112. }
  113. else
  114. {
  115. arg_int_max = 0x10000;
  116. }
  117. expr = PyMapping_GetItemString(kwds, "prog");
  118. PyMapping_DelItemString(kwds, "prog");
  119. if(expr)
  120. {
  121. if(!PyUnicode_Check(expr) && !(expr = PyObject_Str(expr)))
  122. {
  123. PyErr_Restore(errtype, errvalue, errbck);
  124. snprintf(err_msg, 128,
  125. "prog expected to be a string !");
  126. PyErr_SetString(PyExc_TypeError,
  127. err_msg);
  128. goto cturmit_init_error;
  129. }
  130. asc_str = PyUnicode_AsASCIIString(expr);
  131. Py_DECREF(expr);
  132. if(!asc_str)
  133. {
  134. PyErr_Restore(errtype, errvalue, errbck);
  135. snprintf(err_msg, 128,
  136. "prog expected to be an ASCII string");
  137. PyErr_SetString(PyExc_ValueError,
  138. err_msg);
  139. goto cturmit_init_error;
  140. }
  141. arg_expr = (char*)PyBytes_AsString(asc_str);
  142. Py_DECREF(asc_str);
  143. if(!arg_expr)
  144. {
  145. PyErr_Restore(errtype, errvalue, errbck);
  146. snprintf(err_msg, 128,
  147. "Unable to convert prog to Cstring");
  148. PyErr_SetString(PyExc_RuntimeError,
  149. err_msg);
  150. goto cturmit_init_error;
  151. }
  152. }
  153. else
  154. {
  155. arg_expr = "";
  156. }
  157. if(PyMapping_Length(kwds) > 0)
  158. {
  159. keys = PyMapping_Keys(kwds);
  160. strncpy(err_msg, "Unrecognized arguments : '", 128);
  161. for(i=0; i<PyList_Size(keys); i++)
  162. {
  163. asc_str = PyUnicode_AsASCIIString(
  164. PyList_GetItem(keys, i));
  165. strncat(err_msg,
  166. (char*)PyBytes_AsString(asc_str),127);
  167. strncat(err_msg, "', '", 127);
  168. }
  169. Py_DECREF(keys);
  170. strncat(err_msg, "'", 128);
  171. PyErr_Restore(errtype, errvalue, errbck);
  172. PyErr_SetString(PyExc_RuntimeError,
  173. err_msg);
  174. goto cturmit_init_error;
  175. }
  176. PyErr_Restore(errtype, errvalue, errbck);
  177. self->turmit = turmit_alloc(arg_stk_sz, arg_int_max, arg_expr,
  178. TURMIT_AUTOCOMP);
  179. if(self->turmit->err)
  180. {
  181. PyErr_Restore(errtype, errvalue, errbck);
  182. snprintf(err_msg, 128,
  183. "Unable to compile '%s' err : %s", arg_expr,
  184. self->turmit->err_str);
  185. PyErr_SetString(PyExc_ValueError,
  186. err_msg);
  187. goto cturmit_init_error;
  188. }
  189. Py_XDECREF(errtype);
  190. Py_XDECREF(errvalue);
  191. Py_XDECREF(errbck);
  192. return 0;
  193. cturmit_init_error:
  194. Py_XDECREF(errtype);
  195. Py_XDECREF(errvalue);
  196. Py_XDECREF(errbck);
  197. return -1;
  198. }
  199. static PyObject *
  200. CTurmit_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  201. {
  202. CTurmit *self;
  203. self = (CTurmit *)type->tp_alloc(type, 0);
  204. if(self != NULL) {
  205. //do stuff
  206. }
  207. return (PyObject *)self;
  208. }
  209. void
  210. CTurmit_dealloc(CTurmit *self)
  211. {
  212. if(self->turmit) { turmit_free(self->turmit); }
  213. Py_TYPE(self)->tp_free((PyObject*)self);
  214. }
  215. static int _CTurmit_copy_init(CTurmit *self, CTurmit *t)
  216. {
  217. self->turmit = turmit_copy(t->turmit);
  218. if(!self->turmit)
  219. {
  220. //set error
  221. return -1;
  222. }
  223. return 0;
  224. }
  225. PyObject* CTurmit_call(PyObject *func, PyObject *args, PyObject *kwds)
  226. {
  227. turmit_int res;
  228. PyObject *arg;
  229. CTurmit *self;
  230. turmit_int exec_args[5];
  231. char *argsname[5] = {"x", "y", "r", "g", "b"}; // see TURMIT_VAR_L
  232. char err_msg[64];
  233. int i;
  234. PyObject *errtype, *errvalue, *errbck;
  235. self = (CTurmit*)func;
  236. PyErr_Fetch(&errtype, &errvalue, &errbck);
  237. for(i=0; i<5; i++)
  238. {
  239. arg = PyMapping_GetItemString(kwds, argsname[i]);
  240. if(PyErr_Occurred())
  241. {
  242. PyErr_Restore(errtype, errvalue, errbck);
  243. snprintf(err_msg, 64, "Argument %s missing",
  244. argsname[i]);
  245. PyErr_SetString(PyExc_TypeError,
  246. err_msg);
  247. Py_RETURN_NONE;
  248. }
  249. exec_args[i] = PyLong_AsUnsignedLongLong(arg);
  250. if(PyErr_Occurred())
  251. {
  252. PyErr_Restore(errtype, errvalue, errbck);
  253. snprintf(err_msg, 64, "An integer is required for %s",
  254. argsname[i]);
  255. PyErr_SetString(PyExc_TypeError,
  256. err_msg);
  257. Py_RETURN_NONE;
  258. }
  259. Py_DECREF(arg);
  260. }
  261. res = turmit_exec(self->turmit, exec_args);
  262. return PyLong_FromUnsignedLongLong(res);
  263. }
  264. PyObject* CTurmit__push(CTurmit* self, PyObject *args)
  265. {
  266. char err_msg[128];
  267. size_t sz;
  268. turmit_int ival;
  269. PyObject *val;
  270. PyObject *errtype, *errvalue, *errbck;
  271. PyErr_Fetch(&errtype, &errvalue, &errbck);
  272. if((sz = PySequence_Length(args)) >= 0)
  273. {
  274. if(sz > 1)
  275. {
  276. PyErr_Restore(errtype, errvalue, errbck);
  277. snprintf(err_msg, 128, "1 positional argument expected \
  278. but %ld found", sz);
  279. PyErr_SetString(PyExc_TypeError, err_msg);
  280. Py_RETURN_NONE;
  281. }
  282. val = PySequence_GetItem(args, 0);
  283. }
  284. if(!val)
  285. {
  286. PyErr_Restore(errtype, errvalue, errbck);
  287. PyErr_SetString(PyExc_TypeError,
  288. "Argument val is missing");
  289. Py_RETURN_NONE;
  290. }
  291. ival = PyLong_AsSize_t(val);
  292. if(ival == (size_t)-1 && PyErr_Occurred()) { Py_RETURN_NONE; }
  293. SPUSH(self->turmit, ival);
  294. PyErr_Restore(errtype, errvalue, errbck);
  295. Py_RETURN_NONE;
  296. }
  297. PyObject* CTurmit__pop(CTurmit* self)
  298. {
  299. return PyLong_FromUnsignedLongLong(SPOP(self->turmit));
  300. }
  301. PyObject* CTurmit_get_shead(CTurmit *self, void *ref)
  302. {
  303. return PyLong_FromUnsignedLongLong(SCUR(self->turmit));
  304. }
  305. PyObject* CTurmit_get__cur(CTurmit *self, void *ref)
  306. {
  307. return PyLong_FromSsize_t(self->turmit->stack_cur);
  308. }
  309. PyObject* CTurmit_get__stack(CTurmit *self, void *ref)
  310. {
  311. size_t i;
  312. PyObject *res;
  313. res = PyList_New(self->turmit->stack_sz);
  314. for(i=0; i<self->turmit->stack_sz;i++)
  315. {
  316. PyList_SET_ITEM(res, i,
  317. PyLong_FromUnsignedLongLong(self->turmit->stack[i]));
  318. }
  319. return res;
  320. }