Brainfuck-- interpreter

From Esolang
Jump to navigation Jump to search

This is the interpreter for brainfuck-- written by User:Wh1teWolf in Basic:

     DIM tape(33)
     DIM parentes(100)
     DIM parentesvar(100)
     tes=0
     ptr=0
     b=0
     i=0
     f=0
     lol=0
     REPEAT
       counter=0
       INPUT "" a$
       
       REPEAT
         b=b+1
         
         
         IF MID$(a$,b,1)=">" THEN
           ptr=ptr+1
           IF ptr=32 THEN ptr=0
         ENDIF
         
         
         IF MID$(a$,b,1)="." THEN
           lol=0
           IF tape(0)=1 THEN
             lol=1
           ENDIF
           IF tape(1)=1 THEN
             lol=lol+2
           ENDIF
           IF tape(2)=1 THEN
             lol=lol+4
           ENDIF
           IF tape(3)=1 THEN
             lol=lol+8
           ENDIF
           IF tape(4)=1 THEN
             lol=lol+16
           ENDIF
           IF tape(5)=1 THEN
             lol=lol+32
           ENDIF
           IF tape(6)=1 THEN
             lol=lol+64
           ENDIF
           IF tape(7)=1 THEN
             lol=lol+128
           ENDIF
           IF lol>0 THEN
             PRINT CHR$(lol)
           ELSE
             f=0
             REPEAT
               f=INKEY(1)
             UNTIL f<>-1
             PRINT CHR$(f)
             IF f>=128 THEN
               tape(7)=1
               f=f-128
             ENDIF
             IF f>=64   THEN
               tape(6)=1
               f=f-64
             ENDIF
             IF f>=32 THEN
               tape(5)=1
               f=f-32
             ENDIF
             IF f>=16 THEN
               tape(4)=1
               f=f-16
             ENDIF
             IF f>=8 THEN
               tape(3)=1
               f=f-8
             ENDIF
             IF f>=4 THEN
               tape(2)=1
               f=f-4
             ENDIF
             IF f>=2 THEN
               tape(1)=1
               f=f-2
             ENDIF
             IF f>=1 THEN
               tape(0)=1
               f=f-1
             ENDIF
           ENDIF
           
         ENDIF
         
         
         IF MID$(a$,b,1)="+" THEN
           IF tape(ptr)=1 THEN tape(ptr)=0 ELSE tape(ptr)=1
         ENDIF
         
         
         IF MID$(a$,b,1)="[" THEN
           tes=tes+1
           counter=tape(ptr)
           parentesvar=ptr
           parentes(tes)=b
           
           IF counter=0 THEN
             
             REPEAT
               b=b+1
             UNTIL MID$(a$,b,1)="]"
             
           ENDIF
         ENDIF
         
         
         
         
         IF MID$(a$,b,1)="]" THEN
           IF tape(parentesvar)<>0 THEN
             b=parentes(tes)
           ENDIF
           IF tape(parentesvar)=0 THEN
             tes=tes-1
           ENDIF
         ENDIF
         
         
       UNTIL b=1000
       
       a$=""
       b=0
       tes=0
       FOR i=1 TO 100
         parentes(i)=0
       NEXT
     UNTIL FALSE

See Also: