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

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