Advertisement
Guest User

BrainF*ck interpreter in C

a guest
May 7th, 2021
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. /* This file is part of RexOS!.
  2.  *
  3.  * RexOS! is free software: you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation, either version 3 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * RexOS! is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with RexOS!.  If not, see <https://www.gnu.org/licenses/>.
  15.  */
  16.  
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21.  
  22. uint8_t tape[30000] = {0};
  23.  
  24. void _interpret(char *edx)
  25. {
  26.  
  27.     uint8_t *edi = tape;
  28.     uint32_t eax = 0;
  29.     uint16_t ch = 1;
  30.     uint32_t cl = 0;
  31.     size_t rax = -1;
  32.  
  33. loop:
  34.     rax++;
  35.     cl = edx[rax];
  36.  
  37.     if (cl == 0)
  38.     goto exit;
  39.  
  40.     if (cl == '>')
  41.     goto pinc;
  42.  
  43.     if (cl == '<')
  44.     goto pdec;
  45.  
  46.     if (cl == '+')
  47.     goto vinc;
  48.  
  49.     if (cl == '-')
  50.     goto vdec;
  51.  
  52.     if (cl == '.')
  53.     goto prnt;
  54.  
  55.     if (cl == ']')
  56.     goto bend;
  57.  
  58.     goto loop;
  59.  
  60. pinc:
  61.     ++edi;
  62.     goto loop;
  63.  
  64. pdec:
  65.     --edi;
  66.     goto loop;
  67.  
  68. vinc:
  69.     ++*edi;
  70.     goto loop;
  71.  
  72. vdec:
  73.     --*edi;
  74.     goto loop;
  75.  
  76. prnt:
  77.     putchar(*edi);
  78.     goto loop;
  79.  
  80. bend:
  81.     if (*edi == 0)
  82.     goto loop;
  83.  
  84.     ch = 1;
  85.  
  86. ilst:
  87.     if (ch <= 0)
  88.     goto loop;
  89.  
  90.     rax--;
  91.     cl = edx[rax];
  92.  
  93.     if (cl == '[')
  94.         goto dclp;
  95.  
  96.     if (cl == ']')
  97.         goto inlp;
  98.  
  99.     goto ilst;
  100.  
  101. dclp:
  102.     ch--;
  103.     goto ilst;
  104.  
  105. inlp:
  106.     ch++;
  107.     goto ilst;
  108.  
  109. exit:
  110.     return;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement