BrainFizuck

Miscellaneous Forums/Blitz Showcase/BrainFizuck

BrainF*ck interpreter.

Const DEBUG=False
Dim MEM(30000)
Global Code$=" "
Global P=0
Global C=1
Global OUT$
Global S

;CODE$ = ".+[.+]"
;CODE$ = ">+++++++++++[<++++++++>-]<-.>++++[<++++>-]<+.-------.>+++++[<++++>-]<-.[-]>++++++++[<++++>-]<.>+++++++++[<++++++++>-]<+.++++++++++.[-]>++++++++[<++++>-]<.>+++++++++++[<++++++++>-]<+.----------.++++++.---.[-]>++++++++[<++++>-]<.>+++++++++++++[<++++++>-]<.-------------.++++++++++++.--------.>++++++[<------>-]<--.[-]+++++++++++++.---.[-]+[>,-------------]>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]<.[-]<-[<-]>[++++++++++++++.>]++++++++++."
;CODE$ = ",+[-.,+]"
;CODE$=" +++[--[>+]<]"

Restore NINE
Repeat	
	Read T$
	If T$="" Then Exit
	CODE$=CODE$+T$
Forever

While C <= Len(Code$)
	Select Mid(Code$,C,1)
		Case "+"
			MEM(P) = MEM(P) + 1		
				If MEM(P)>255 Then MEM(P)=0
			C=C+1
		Case "-"
			MEM(P) = MEM(P) - 1
				If MEM(P)<0 Then MEM(P)=255
			C=C+1
		Case ">"
			P=P+1					:C=C+1
		Case "<"
			P=P-1					:C=C+1
		Case "."
			OUT$=OUT$+Chr(MEM(P))
			If Right(OUT$,1)=Chr(10) Then Print OUT$:OUT$=""
			C=C+1
		Case ","
			Repeat
				IN=GetKey()
			Until IN
			MEM(P)=	IN:C=C+1		
		Case "["
			If MEM(P)=0 Then
				S=C-C
				F=C	
				.HERE
				Repeat
					F=F+1
				Until Mid(CODE$,F,1) = "]" Or Mid(CODE$,F,1) = "["
				I$= Mid(CODE$,F,1)
				Select True
					Case I$="]" And S=0
						C=F 
					Case I$="]" And S>0
						S=S-1
						Goto HERE
					Case I$="["
						S=S+1
						Goto HERE	
				End Select	
			End If
			C=C+1
		Case "]"
			S=C-C
			F=C
			.THERE
			Repeat
				F=F-1
			Until Mid(CODE$,F,1) = "[" Or Mid(CODE$,F,1) = "]"
			I$= Mid(CODE$,F,1)
			Select True
				Case I$="[" And S=0
					C=F 
				Case I$="[" And S>0
					S=S-1
					Goto THERE
				Case I$="]"
					S=S+1
					Goto THERE	
			End Select 
		Default
			C=C+1
	End Select
	If DEBUG
		For Z=0 To 9
			A$=A$+MEM(Z)
		Next
		A$=A$+" P="+P + " "
		A$=A$+" C="+C + " "
		A$=A$+" CODE="+Mid(CODE,C,1) + " "
		A$=A$+" MEM="+MEM(P) + " "
		DebugLog A$
		A$=""
	End If	
Wend

WaitKey


.NINE;TY NINE BOTTLES O' BEER
Data ">++++++++++[<++++++++++>-]<->>>>>+++[>+++>+++<<-]<<<<+<[>[>+"
Data ">+<<-]>>[-<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]>>"
Data ">>>>[[-]<++++++++++<->>]<-[>+>+<<-]>[<+>-]+>[[-]<->]<<<<<<<<"
Data "<->>]<[>+>+<<-]>>[-<<+>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<"
Data "<[>>+>+<<<-]>>>[-<<<+>>>]++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<"
Data "<[>+<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<<[>>+>+<<<-]>>>[-<<<+>>>]+"
Data "+++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<"
Data "<+>>[-]]<<[[-]>>>++++++++[>>++++++<<-]>[<++++++++[>++++++<-]"
Data ">.<++++++++[>------<-]>[<<+>>-]]>.<<++++++++[>>------<<-]<[-"
Data ">>+<<]<++++++++[<++++>-]<.>+++++++[>+++++++++<-]>+++.<+++++["
Data ">+++++++++<-]>.+++++..--------.-------.++++++++++++++>>[>>>+"
Data ">+<<<<-]>>>>[-<<<<+>>>>]>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<<"
Data "[>>>+>+<<<<-]>>>>[-<<<<+>>>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>"
Data ">]<<<[>>+<<[-]]>[>+<[-]]++>>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<"
Data "+<[[-]>-<]>[<<<<<<<.>>>>>>>[-]]<<<<<<<<<.>>----.---------.<<"
Data ".>>----.+++..+++++++++++++.[-]<<[-]]<[>+>+<<-]>>[-<<+>>]+>+<"
Data "[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++"
Data ">+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<<+>"
Data ">[-]]<<[[-]>++++++++[<++++>-]<.>++++++++++[>+++++++++++<-]>+"
Data ".-.<<.>>++++++.------------.---.<<.>++++++[>+++<-]>.<++++++["
Data ">----<-]>++.+++++++++++..[-]<<[-]++++++++++.[-]]<[>+>+<<-]>>"
Data "[-<<+>>]+++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++."
Data ">+++++++++[>+++++++++<-]>+++.+++++++++++++.++++++++++.------"
Data ".<++++++++[>>++++<<-]>>.<++++++++++.-.---------.>.<-.+++++++"
Data "++++.++++++++.---------.>.<-------------.+++++++++++++.-----"
Data "-----.>.<++++++++++++.---------------.<+++[>++++++<-]>..>.<-"
Data "---------.+++++++++++.>.<<+++[>------<-]>-.+++++++++++++++++"
Data ".---.++++++.-------.----------.[-]>[-]<<<.[-]]<[>+>+<<-]>>[-"
Data "<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.["
Data "-]<[-]>]<+<]"
Data ""



Probably the most useless thing written in blitz :P

Ah well, Kinda cool :D

Here is a serpenski triangle :-)

                               >    
                               + +    
                              +   +    
                             [ < + +    
                            +       +    
                           + +     + +    
                          >   -   ]   >    
                         + + + + + + + +    
                        [               >    
                       + +             + +    
                      <   -           ]   >    
                     > + + >         > > + >    
                    >       >       +       <    
                   < <     < <     < <     < <    
                  <   [   -   [   -   >   +   <    
                 ] > [ - < + > > > . < < ] > > >    
                [                               [    
               - >                             + +    
              +   +                           +   +    
             + + [ >                         + + + +    
            <       -                       ]       >    
           . <     < [                     - >     + <    
          ]   +   >   [                   -   >   +   +    
         + + + + + + + +                 < < + > ] > . [    
        -               ]               >               ]    
       ] +             < <             < [             - [    
      -   >           +   <           ]   +           >   [    
     - < + >         > > - [         - > + <         ] + + >    
    [       -       <       -       >       ]       <       <    
   < ]     < <     < <     ] +     + +     + +     + +     + +    
  +   .   +   +   +   .   [   -   ]   <   ]   +   +   +   +   +    
 * * * * * M a d e * B y : * N Y Y R I K K I * 2 0 0 2 * * * * *    



alter the code with this"

Graphics 80*10,45*10,0,2

;optional
FIL =ReadFile("C:\BF.TXT")
Repeat
	CODE$=CODE$+ReadLine(FIL)
Until Eof(FIL)



Thats more like it :D

My interpreter is already better though :P (well, once I work out variables)

this one takes input from the console (0 terminated) and prints out a BF program that prints the BF source ;-)

+++++[>+++++++++<-],[[>--.++>+<<-]>+.->[<.>-]<<,]>>>>>>>>>++++++++++.


Now all you have to do is write BF in BF :D

lol how do you enter code?

Instead of having the input from the console, get each char from a file stream.

Huh... Looking at the source code above... > and < move forward or back one location in an array, while + and - incremenet or decrement the value at the current location.

. writes a character to a string called OUT$, unless that character is a line feed, (10) in which case it prints the data contained in OUT$ and clears OUT$.

, gets keyboard input and writes the key to the current array location.

And [ and ] appear to be some kind of loop but I can't figure out how the hell it works.


Interesting. But useless. :-)

"[" is simply a loop initiator
"]" will throw the pointer back to its matching "[" if the
value at the current pointer is not 0. Loops can be nested.

This program "++++++[-]" increments the value of the current pointer by 6 then enters a loop that decriments the value at that pointer until it hits 0 and the program ends.
The neat thing about it is that its a Turing complete language. you could write OO programs in it if you wanted to. not sure why anyone would though ;)

I forgot all about that language...