yabfi2.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* yabfi2 -- yet another optimizing brainfuck interpreter
  2. *
  3. * Copyright (C) 2004 Sascha Wilde
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. *-------------------------------------------------------------------------
  20. *
  21. * this is a simple BF interpreter, doing some on the fly optimization.
  22. * with madelbrot.b this is nearly four times faster than yabfi.c
  23. *
  24. *-------------------------------------------------------------------------
  25. * $Id: yabfi2.c,v 1.8 2004/06/15 19:36:50 wilde Exp $ */
  26. #define MEMSIZE 30000
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <sys/mman.h>
  33. #include <sys/stat.h>
  34. int
  35. main(int argc,char** argv)
  36. {
  37. int i, imax, c, lc, fd, *lut, j;
  38. static struct stat fd_stat;
  39. char *src;
  40. unsigned char *p, *pmin, *pmax;
  41. if ( 2 != argc ) {
  42. fprintf (stderr, "Usage: %s BF-SOURCE\n", argv[0]); exit(1); }
  43. if ((fd = open(argv[1], O_RDONLY)) == -1) {
  44. perror ("Cant open file\n"); exit(1); }
  45. (void) fstat (fd, &fd_stat);
  46. if ((src = (char*) mmap (0, (size_t) fd_stat.st_size,
  47. PROT_READ, MAP_PRIVATE, fd, 0)) == (void*) -1)
  48. {
  49. close (fd);
  50. perror (NULL);
  51. exit(1);
  52. }
  53. close (fd);
  54. lut = (int*) calloc (fd_stat.st_size, sizeof(int));
  55. imax = (fd_stat.st_size - 1);
  56. p = pmin = (unsigned char*) calloc (MEMSIZE, 1);
  57. pmax = pmin + (MEMSIZE - 1);
  58. i = 0;
  59. while (i <= imax )
  60. {
  61. switch (src[i])
  62. {
  63. case '>':
  64. if (lut[i]) {
  65. p += lut[i];
  66. i += lut[i];
  67. } else {
  68. j = i;
  69. do {
  70. ++lut[j];
  71. } while (src[++i] == '>');
  72. p += lut[j];
  73. }
  74. break;
  75. case '<':
  76. if (lut[i]) {
  77. p -= lut[i];
  78. i += lut[i];
  79. } else {
  80. j = i;
  81. do {
  82. ++lut[j];
  83. } while (src[++i] == '<');
  84. p -= lut[j];
  85. }
  86. break;
  87. case '+':
  88. if (lut[i]) {
  89. *p += lut[i];
  90. i += lut[i];
  91. } else {
  92. j = i;
  93. do {
  94. ++lut[j];
  95. } while (src[++i] == '+');
  96. *p += lut[j];
  97. }
  98. break;
  99. case '-':
  100. if (lut[i]) {
  101. *p -= lut[i];
  102. i += lut[i];
  103. } else {
  104. j = i;
  105. do {
  106. ++lut[j];
  107. } while (src[++i] == '-');
  108. *p -= lut[j];
  109. }
  110. break;
  111. case '.': putchar(*p);
  112. ++i;
  113. break;
  114. case ',': c = getchar();
  115. if (c == EOF)
  116. *p = 0;
  117. else
  118. *p = c;
  119. ++i;
  120. break;
  121. case '[':
  122. if (*p == 0)
  123. if (lut[i])
  124. i = lut[i];
  125. else {
  126. j = i;
  127. lc = 0;
  128. while ((src[++i] != ']') || (lc != 0))
  129. if (src[i] == '[')
  130. ++lc;
  131. else if (src[i] == ']')
  132. --lc;
  133. lut[j] = ++i;
  134. }
  135. else
  136. ++i;
  137. break;
  138. case ']':
  139. if (*p != 0)
  140. if (lut[i])
  141. i = lut[i];
  142. else {
  143. j = i;
  144. lc = 0;
  145. while ((src[--i] != '[') || (lc != 0))
  146. if (src[i] == ']')
  147. ++lc;
  148. else if (src[i] == '[')
  149. --lc;
  150. lut[j] = ++i;
  151. }
  152. else
  153. ++i;
  154. break;
  155. default: ++i;
  156. }
  157. if ((p < pmin) || (p > pmax)) {
  158. fprintf (stderr, "Range error in BF code!"); exit(1); }
  159. }
  160. free (pmin);
  161. free (lut);
  162. }