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.

turmit.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifndef _TURMIT_H_
  2. #define _TURMIT_H_
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <ctype.h>
  8. /**@Todo For the moment there is no way to track wether a variable is used
  9. * or not in result processing */
  10. typedef unsigned long long int turmit_int;
  11. typedef struct turmit_op_s turmit_op_t;
  12. typedef struct turmit_s turmit_t;
  13. typedef struct turmit_sym_s turmit_sym_t;
  14. typedef void (*turmit_op_f)(turmit_t*);
  15. #define SCUR(t) (t->stack[t->stack_cur])
  16. #define SDEC(t) (t->stack_cur = (t->stack_cur>0?t->stack_cur:t->stack_sz) -1)
  17. #define SINC(t) (t->stack_cur = (t->stack_cur + 1) % t->stack_sz)
  18. #define SPOP(t) (__SPOP(t))
  19. #define SPUSH(t, v) (t->stack[SINC(t)] = v % t->int_max)
  20. #define TURMIT_JMP(t, v) (((t->op_expr_sz - t->op_cur) <= v)?\
  21. (t->op_end=1):\
  22. (t->op_cur += (v>0?v-1:0) ))
  23. #define TURMIT_EXPR_MAX_SZ 0xFFFFFFFF
  24. #define TURMIT_LAZY 0
  25. #define TURMIT_AUTOCOMP 1
  26. #define TURMIT_OK 0
  27. #define TURMIT_ERROR -1
  28. #define TURMIT_SYM_OP 1
  29. #define TURMIT_SYM_VAL 2
  30. #define TURMIT_SYM_VAR 3
  31. #define TURMIT_VAR_L "xyrgb"
  32. #define TURMIT_OPSYM(NAME) {#NAME, NULL, turmit_op_##NAME}
  33. #define TURMIT_OPSYMALIAS(NAME, alias) {#NAME, alias, turmit_op_##NAME}
  34. // OPSYMALIAS has to be first !
  35. #define TURMIT_OP_LST {\
  36. TURMIT_OPSYMALIAS(add, "+"),\
  37. TURMIT_OPSYMALIAS(sub, "-"),\
  38. TURMIT_OPSYMALIAS(mul, "*"),\
  39. TURMIT_OPSYMALIAS(div, "/"),\
  40. TURMIT_OPSYMALIAS(mod, "%"),\
  41. TURMIT_OPSYMALIAS(bin_or, "|"),\
  42. TURMIT_OPSYMALIAS(bin_and, "&"),\
  43. TURMIT_OPSYMALIAS(bin_xor, "^"),\
  44. TURMIT_OPSYMALIAS(lshift, "<<"),\
  45. TURMIT_OPSYM(mem_sz),\
  46. TURMIT_OPSYM(dup),\
  47. TURMIT_OPSYM(pop),\
  48. TURMIT_OPSYM(swp),\
  49. TURMIT_OPSYM(jmp),\
  50. TURMIT_OPSYM(jz),\
  51. TURMIT_OPSYM(jcmp),\
  52. NULL,\
  53. }
  54. struct turmit_sym_s
  55. {
  56. char *str;
  57. char *alias;
  58. turmit_op_f op_fun;
  59. };
  60. union turmit_op_u
  61. {
  62. turmit_op_f op;
  63. turmit_int val;
  64. short var; /*!< interpreted % 5 */
  65. };
  66. struct turmit_op_s
  67. {
  68. union turmit_op_u op;
  69. /** 0 for NULL, 1 if operation pointer, 2 if value, 3 if variable */
  70. short value;
  71. };
  72. struct turmit_s
  73. {
  74. char *expr;
  75. unsigned char flags;
  76. ssize_t int_max;
  77. turmit_op_t *op_expr;
  78. ssize_t op_expr_sz;
  79. ssize_t op_cur;
  80. short op_end;
  81. ssize_t stack_sz;
  82. turmit_int *stack;
  83. ssize_t stack_cur;
  84. /**@brief 0 if no error
  85. * @note only for compilation error for the moment
  86. */
  87. char err;
  88. char *err_str;
  89. };
  90. inline static turmit_int __SPOP(turmit_t *t) {
  91. turmit_int r = t->stack[t->stack_cur];
  92. SDEC(t);
  93. return r;
  94. }
  95. #define TURMIT_OP(NAME) void turmit_op_##NAME (turmit_t *turmit)
  96. /**@brief Allocate memory and initialise a turmit
  97. * @param const char* The turmit expression
  98. * @param unsigned char One of TURMIT_LAZY or TURMIT_AUTOCOMP.
  99. * @return A pointer on the new turmit
  100. */
  101. turmit_t *turmit_alloc(ssize_t stack_sz, ssize_t int_max, const char *expr,
  102. unsigned char flags);
  103. /**@brief Initialize a turmit
  104. * @param turmit_t* The turmit to initialize
  105. * @param const char* The turmit expression
  106. * @param unsigned char One of TURMIT_LAZY or TURMIT_AUTOCOMP.
  107. * @return The pointer on the turmit
  108. */
  109. turmit_t *turmit_init(turmit_t *turmit, ssize_t stack_sz, ssize_t int_max,
  110. const char *expr, unsigned char flags);
  111. turmit_t *turmit_copy(turmit_t *turmit);
  112. void turmit_clean(turmit_t *turmit);
  113. /**@brief Free memory of a turmit
  114. * @param turmit the turmit to free
  115. */
  116. void turmit_free(turmit_t *turmit);
  117. /**@brief Exec a turmit expr
  118. * @param turmit_t* Concerned turmit
  119. * @param turmit_int* Arguments ( see @ref TURMIT_VAL_L )
  120. * @return Processed value
  121. */
  122. turmit_int turmit_exec(turmit_t *turmit, const turmit_int args[5]);
  123. /**@brief Populate turmit_t.op_expr and set op_expr_sz
  124. * @return 0 if no errors
  125. */
  126. int turmit_compile(turmit_t *turmit);
  127. void _turmit_stack_dump(turmit_t *turmit);
  128. TURMIT_OP(mem_sz);
  129. TURMIT_OP(add);
  130. TURMIT_OP(sub);
  131. TURMIT_OP(bin_and);
  132. TURMIT_OP(dup);
  133. TURMIT_OP(lshift);
  134. TURMIT_OP(mod);
  135. TURMIT_OP(mul);
  136. TURMIT_OP(div);
  137. TURMIT_OP(bin_or);
  138. TURMIT_OP(bin_xor);
  139. TURMIT_OP(pop);
  140. TURMIT_OP(swp);
  141. TURMIT_OP(jmp);
  142. TURMIT_OP(jz);
  143. TURMIT_OP(jcmp);
  144. #endif