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.8KB

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